summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAlexey Samsonov <samsonov@google.com>2014-05-09 22:11:03 +0000
committerAlexey Samsonov <samsonov@google.com>2014-05-09 22:11:03 +0000
commiteacb4d841723150106c801bee7f62b609852b6ed (patch)
tree93d670aac374b0c1536fd3cad3b543756524b555
parent04e2e665cb1cd7664b9b10427f8b29bd97523caf (diff)
downloadbcm5719-llvm-eacb4d841723150106c801bee7f62b609852b6ed.tar.gz
bcm5719-llvm-eacb4d841723150106c801bee7f62b609852b6ed.zip
[CMake] Use ExternalProject to build MSan-ified version of libcxx for unit tests.
This change lets MSan rely on libcxx's own build system instead of manually compiling its sources and setting up all the necessary compile flags. It would also simplify compiling libcxx with another sanitizers (in particular, TSan). The tricky part is to make sure libcxx is reconfigured/rebuilt when Clang or MSan runtime library is changed. "clobber" step used in this patch works well for me, but it's possible it would break for other configurations - will watch the buildbots. llvm-svn: 208451
-rw-r--r--compiler-rt/cmake/Modules/AddCompilerRT.cmake46
-rw-r--r--compiler-rt/lib/msan/CMakeLists.txt3
-rw-r--r--compiler-rt/lib/msan/tests/CMakeLists.txt75
3 files changed, 71 insertions, 53 deletions
diff --git a/compiler-rt/cmake/Modules/AddCompilerRT.cmake b/compiler-rt/cmake/Modules/AddCompilerRT.cmake
index 82947eac4e8..ba22927d733 100644
--- a/compiler-rt/cmake/Modules/AddCompilerRT.cmake
+++ b/compiler-rt/cmake/Modules/AddCompilerRT.cmake
@@ -1,4 +1,5 @@
include(AddLLVM)
+include(ExternalProject)
include(LLVMParseArguments)
include(CompilerRTUtils)
@@ -167,3 +168,48 @@ macro(add_compiler_rt_script name)
PERMISSIONS OWNER_READ OWNER_WRITE OWNER_EXECUTE GROUP_READ GROUP_EXECUTE WORLD_READ WORLD_EXECUTE
DESTINATION ${COMPILER_RT_INSTALL_PATH}/bin)
endmacro(add_compiler_rt_script src name)
+
+# Builds custom version of libc++ and installs it in <prefix>.
+# Can be used to build sanitized versions of libc++ for running unit tests.
+# add_custom_libcxx(<name> <prefix>
+# DEPS <list of build deps>
+# CFLAGS <list of compile flags>)
+macro(add_custom_libcxx name prefix)
+ if(NOT COMPILER_RT_HAS_LIBCXX_SOURCES)
+ message(FATAL_ERROR "libcxx not found!")
+ endif()
+
+ parse_arguments(LIBCXX "DEPS;CFLAGS" "" ${ARGN})
+ foreach(flag ${LIBCXX_CFLAGS})
+ set(flagstr "${flagstr} ${flag}")
+ endforeach()
+ set(LIBCXX_CFLAGS ${flagstr})
+
+ if(NOT COMPILER_RT_STANDALONE_BUILD)
+ list(APPEND LIBCXX_DEPS clang)
+ endif()
+
+ ExternalProject_Add(${name}
+ PREFIX ${prefix}
+ SOURCE_DIR ${COMPILER_RT_LIBCXX_PATH}
+ CMAKE_ARGS -DCMAKE_C_COMPILER=${COMPILER_RT_TEST_COMPILER}
+ -DCMAKE_CXX_COMPILER=${COMPILER_RT_TEST_COMPILER}
+ -DCMAKE_C_FLAGS=${LIBCXX_CFLAGS}
+ -DCMAKE_CXX_FLAGS=${LIBCXX_CFLAGS}
+ -DCMAKE_BUILD_TYPE=Release
+ -DCMAKE_INSTALL_PREFIX:PATH=<INSTALL_DIR>
+ )
+
+ ExternalProject_Add_Step(${name} force-reconfigure
+ DEPENDERS configure
+ ALWAYS 1
+ )
+
+ ExternalProject_Add_Step(${name} clobber
+ COMMAND ${CMAKE_COMMAND} -E remove_directory <BINARY_DIR>
+ COMMAND ${CMAKE_COMMAND} -E make_directory <BINARY_DIR>
+ COMMENT "Clobberring ${name} build directory..."
+ DEPENDERS configure
+ DEPENDS ${LIBCXX_DEPS}
+ )
+endmacro()
diff --git a/compiler-rt/lib/msan/CMakeLists.txt b/compiler-rt/lib/msan/CMakeLists.txt
index a05d324fd31..e046a7729e6 100644
--- a/compiler-rt/lib/msan/CMakeLists.txt
+++ b/compiler-rt/lib/msan/CMakeLists.txt
@@ -17,6 +17,8 @@ append_if(COMPILER_RT_HAS_FPIE_FLAG -fPIE MSAN_RTL_CFLAGS)
# Prevent clang from generating libc calls.
append_if(COMPILER_RT_HAS_FFREESTANDING_FLAG -ffreestanding MSAN_RTL_CFLAGS)
+set(MSAN_RUNTIME_LIBRARIES)
+
# Static runtime library.
add_custom_target(msan)
set(arch "x86_64")
@@ -28,6 +30,7 @@ if(CAN_TARGET_${arch})
$<TARGET_OBJECTS:RTSanitizerCommonLibc.${arch}>
CFLAGS ${MSAN_RTL_CFLAGS})
add_dependencies(msan clang_rt.msan-${arch})
+ list(APPEND MSAN_RUNTIME_LIBRARIES clang_rt.msan-${arch})
if(UNIX)
add_sanitizer_rt_symbols(clang_rt.msan-${arch} msan.syms.extra)
add_dependencies(msan clang_rt.msan-${arch}-symbols)
diff --git a/compiler-rt/lib/msan/tests/CMakeLists.txt b/compiler-rt/lib/msan/tests/CMakeLists.txt
index cc58d2e1e2e..c6546151780 100644
--- a/compiler-rt/lib/msan/tests/CMakeLists.txt
+++ b/compiler-rt/lib/msan/tests/CMakeLists.txt
@@ -5,29 +5,10 @@ include(CompilerRTLink)
include_directories(..)
include_directories(../..)
-# Instrumented libcxx sources and build flags.
-file(GLOB MSAN_LIBCXX_SOURCES ${COMPILER_RT_LIBCXX_PATH}/src/*.cpp)
set(MSAN_LIBCXX_CFLAGS
- -I${COMPILER_RT_LIBCXX_PATH}/include
-fsanitize=memory
-fsanitize-memory-track-origins
- -fPIC
- -Wno-\#warnings
- -Wno-pedantic
- -g
- -O2
- -fstrict-aliasing
- -fno-exceptions
- -nostdinc++
- -fno-omit-frame-pointer
- -mno-omit-leaf-frame-pointer)
-set(MSAN_LIBCXX_LINK_FLAGS
- -nodefaultlibs
- -lrt
- -lc
- -lstdc++
- -fsanitize=memory)
-append_if(COMPILER_RT_HAS_LIBPTHREAD -lpthread MSAN_LIBCXX_LINK_FLAGS)
+ -Wno-pedantic)
# Unittest sources and build flags.
set(MSAN_UNITTEST_SOURCES msan_test.cc msan_test_main.cc)
@@ -103,46 +84,23 @@ macro(msan_link_shared so_list so_name arch kind)
list(APPEND ${so_list} ${output_so})
endmacro()
-# Link MSan unit test for a given architecture from a set
-# of objects in ${ARGN}.
-macro(add_msan_test test_suite test_name arch)
- get_target_flags_for_arch(${arch} TARGET_LINK_FLAGS)
- set(TEST_DEPS ${ARGN} ${MSAN_LOADABLE_SO})
- if(NOT COMPILER_RT_STANDALONE_BUILD)
- list(APPEND TEST_DEPS msan)
- endif()
- add_compiler_rt_test(${test_suite} ${test_name}
- OBJECTS ${ARGN}
- DEPS ${TEST_DEPS}
- LINK_FLAGS ${MSAN_UNITTEST_LINK_FLAGS}
- ${TARGET_LINK_FLAGS}
- "-Wl,-rpath=${CMAKE_CURRENT_BINARY_DIR}")
-endmacro()
-
# Main MemorySanitizer unit tests.
add_custom_target(MsanUnitTests)
set_target_properties(MsanUnitTests PROPERTIES FOLDER "MSan unit tests")
# Adds MSan unit tests and benchmarks for architecture.
macro(add_msan_tests_for_arch arch kind)
+ set(LIBCXX_PREFIX ${CMAKE_CURRENT_BINARY_DIR}/../libcxx_msan${kind})
+ add_custom_libcxx(libcxx_msan${kind} ${LIBCXX_PREFIX}
+ DEPS ${MSAN_RUNTIME_LIBRARIES}
+ CFLAGS ${MSAN_LIBCXX_CFLAGS} ${ARGN})
+ set(MSAN_LIBCXX_SO ${LIBCXX_PREFIX}/lib/libc++.so)
+
# Build gtest instrumented with MSan.
set(MSAN_INST_GTEST)
msan_compile(MSAN_INST_GTEST ${COMPILER_RT_GTEST_SOURCE} ${arch} "${kind}"
${MSAN_UNITTEST_INSTRUMENTED_CFLAGS} ${ARGN})
- # Build libcxx instrumented with MSan.
- set(MSAN_INST_LIBCXX_OBJECTS)
- foreach(SOURCE ${MSAN_LIBCXX_SOURCES})
- msan_compile(MSAN_INST_LIBCXX_OBJECTS ${SOURCE} ${arch} "${kind}"
- ${MSAN_LIBCXX_CFLAGS} ${ARGN})
- endforeach(SOURCE)
-
- set(MSAN_INST_LIBCXX)
- msan_link_shared(MSAN_INST_LIBCXX "libcxx" ${arch} "${kind}"
- OBJECTS ${MSAN_INST_LIBCXX_OBJECTS}
- LINKFLAGS ${MSAN_LIBCXX_LINK_FLAGS}
- DEPS ${MSAN_INST_LIBCXX_OBJECTS})
-
# Instrumented tests.
set(MSAN_INST_TEST_OBJECTS)
foreach (SOURCE ${MSAN_UNITTEST_SOURCES})
@@ -172,10 +130,21 @@ macro(add_msan_tests_for_arch arch kind)
OBJECTS ${MSANDR_TEST_OBJECTS}
DEPS ${MSANDR_TEST_OBJECTS})
- # Link everything together.
- add_msan_test(MsanUnitTests "Msan-${arch}${kind}-Test" ${arch}
- ${MSAN_INST_TEST_OBJECTS} ${MSAN_INST_GTEST}
- ${MSAN_INST_LIBCXX} ${MSANDR_TEST_SO})
+ set(MSAN_TEST_OBJECTS ${MSAN_INST_TEST_OBJECTS} ${MSAN_INST_GTEST}
+ ${MSANDR_TEST_SO})
+ set(MSAN_TEST_DEPS ${MSAN_TEST_OBJECTS} libcxx_msan${kind}
+ ${MSAN_LOADABLE_SO})
+ if(NOT COMPILER_RT_STANDALONE_BUILD)
+ list(APPEND MSAN_TEST_DEPS msan)
+ endif()
+ get_target_flags_for_arch(${arch} TARGET_LINK_FLAGS)
+ add_compiler_rt_test(MsanUnitTests "Msan-${arch}${kind}-Test" ${arch}
+ OBJECTS ${MSAN_TEST_OBJECTS} ${MSAN_LIBCXX_SO}
+ DEPS ${MSAN_TEST_DEPS}
+ LINK_FLAGS ${MSAN_UNITTEST_LINK_FLAGS}
+ ${TARGET_LINK_FLAGS}
+ "-Wl,-rpath=${CMAKE_CURRENT_BINARY_DIR}"
+ "-Wl,-rpath=${LIBCXX_PREFIX}/lib")
endmacro()
# We should only build MSan unit tests if we can build instrumented libcxx.
OpenPOWER on IntegriCloud