[compiler-rt] r208451 - [CMake] Use ExternalProject to build MSan-ified version of libcxx for unit tests.

Alexey Samsonov samsonov at google.com
Fri May 9 15:11:03 PDT 2014


Author: samsonov
Date: Fri May  9 17:11:03 2014
New Revision: 208451

URL: http://llvm.org/viewvc/llvm-project?rev=208451&view=rev
Log:
[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.

Modified:
    compiler-rt/trunk/cmake/Modules/AddCompilerRT.cmake
    compiler-rt/trunk/lib/msan/CMakeLists.txt
    compiler-rt/trunk/lib/msan/tests/CMakeLists.txt

Modified: compiler-rt/trunk/cmake/Modules/AddCompilerRT.cmake
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/cmake/Modules/AddCompilerRT.cmake?rev=208451&r1=208450&r2=208451&view=diff
==============================================================================
--- compiler-rt/trunk/cmake/Modules/AddCompilerRT.cmake (original)
+++ compiler-rt/trunk/cmake/Modules/AddCompilerRT.cmake Fri May  9 17:11:03 2014
@@ -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()

Modified: compiler-rt/trunk/lib/msan/CMakeLists.txt
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/msan/CMakeLists.txt?rev=208451&r1=208450&r2=208451&view=diff
==============================================================================
--- compiler-rt/trunk/lib/msan/CMakeLists.txt (original)
+++ compiler-rt/trunk/lib/msan/CMakeLists.txt Fri May  9 17:11:03 2014
@@ -17,6 +17,8 @@ append_if(COMPILER_RT_HAS_FPIE_FLAG -fPI
 # 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)

Modified: compiler-rt/trunk/lib/msan/tests/CMakeLists.txt
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/msan/tests/CMakeLists.txt?rev=208451&r1=208450&r2=208451&view=diff
==============================================================================
--- compiler-rt/trunk/lib/msan/tests/CMakeLists.txt (original)
+++ compiler-rt/trunk/lib/msan/tests/CMakeLists.txt Fri May  9 17:11:03 2014
@@ -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 a
   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.





More information about the llvm-commits mailing list