From 984f6cf119e26ec25463d58235d613bfea114127 Mon Sep 17 00:00:00 2001 From: Chandler Carruth Date: Wed, 27 Jun 2012 09:01:24 +0000 Subject: [PATCH] Add support for building the ASan instrumentation unit tests with the just-built Clang binary, and linking them against the just-built ASan runtime. This is *very* brittle. I expect it will require tweaking, and I've pro-actively disabled it on non-Unix builds and on cross-builds. It is also currently missing dependency edges on GoogleTest header files and a few other corner cases, but those can be fixed. This is the major milestone of a mini-bootstrap-like build of the unittest. git-svn-id: https://llvm.org/svn/llvm-project/compiler-rt/trunk@159255 91177308-0d34-0410-b5e6-96231b3b80d8 --- CMakeLists.txt | 10 +++++++++ lib/asan/CMakeLists.txt | 19 ++++++++++++----- lib/asan/tests/CMakeLists.txt | 49 +++++++++++++++++++++++++++---------------- 3 files changed, 55 insertions(+), 23 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 1e6a2acbc..97835a1e9 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -48,6 +48,16 @@ try_compile(CAN_TARGET_I386 ${CMAKE_BINARY_DIR} ${SIMPLE_SOURCE32} COMPILE_DEFINITIONS "${TARGET_I386_CFLAGS}" CMAKE_FLAGS "-DCMAKE_EXE_LINKER_FLAGS:STRING=${TARGET_I386_CFLAGS}") +# Because compiler-rt spends a lot of time setting up custom compile flags, +# define a handy helper function for it. The compile flags setting in CMake +# has serious issues that make its syntax challenging at best. +function(set_target_compile_flags target) + foreach(arg ${ARGN}) + set(argstring "${argstring} ${arg}") + endforeach() + set_property(TARGET ${target} PROPERTY COMPILE_FLAGS "${argstring}") +endfunction() + add_subdirectory(lib) if(LLVM_INCLUDE_TESTS) diff --git a/lib/asan/CMakeLists.txt b/lib/asan/CMakeLists.txt index ef0dbbc75..e06ebf200 100644 --- a/lib/asan/CMakeLists.txt +++ b/lib/asan/CMakeLists.txt @@ -23,7 +23,12 @@ set(ASAN_SOURCES include_directories(..) -set(ASAN_CFLAGS "-fPIC -fno-exceptions -funwind-tables -fvisibility=hidden") +set(ASAN_CFLAGS + -fPIC + -fno-exceptions + -funwind-tables + -fvisibility=hidden + ) set(ASAN_COMMON_DEFINITIONS ASAN_HAS_EXCEPTIONS=1 @@ -36,8 +41,10 @@ if(CAN_TARGET_X86_64) $ $ ) - set_property(TARGET clang_rt.asan-x86_64 PROPERTY COMPILE_FLAGS - "${ASAN_CFLAGS} ${TARGET_X86_64_CFLAGS}") + set_target_compile_flags(clang_rt.asan-x86_64 + ${ASAN_CFLAGS} + ${TARGET_X86_64_CFLAGS} + ) set_property(TARGET clang_rt.asan-x86_64 APPEND PROPERTY COMPILE_DEFINITIONS ${ASAN_COMMON_DEFINITIONS}) endif() @@ -47,8 +54,10 @@ if(CAN_TARGET_I386) $ $ ) - set_property(TARGET clang_rt.asan-i386 PROPERTY COMPILE_FLAGS - "${ASAN_CFLAGS} ${TARGET_I386_CFLAGS}") + set_target_compile_flags(clang_rt.asan-i386 + ${ASAN_CFLAGS} + ${TARGET_I386_CFLAGS} + ) set_property(TARGET clang_rt.asan-x86_64 APPEND PROPERTY COMPILE_DEFINITIONS ${ASAN_COMMON_DEFINITIONS}) endif() diff --git a/lib/asan/tests/CMakeLists.txt b/lib/asan/tests/CMakeLists.txt index 1fe3dc047..d8432bc52 100644 --- a/lib/asan/tests/CMakeLists.txt +++ b/lib/asan/tests/CMakeLists.txt @@ -30,26 +30,39 @@ add_asan_test(AsanNoInstrumentationTests asan_break_optimization.cc ) -# FIXME: Currently, this detection isn't working. Assume we're doing -# a bootstrap build for now. -set(HOST_HAS_ASAN on) -#check_cxx_compiler_flag("-faddress-sanitizer" HOST_HAS_ASAN) -if(HOST_HAS_ASAN) +# We only support building instrumented tests when we're not cross compiling +# and targeting a unix-like system where we can predict viable compilation and +# linking strategies. +if("${CMAKE_HOST_SYSTEM}" STREQUAL "${CMAKE_SYSTEM}" AND UNIX) + + # This function is a custom routine to manage manually compiling source files + # for unit tests with the just-built Clang binary, using the ASan + # instrumentation, and linking them into a test executable. + function(add_asan_compile_command source) + add_custom_command( + OUTPUT "${source}.asan.o" + COMMAND clang + -faddress-sanitizer ${ASAN_CFLAGS} + -mllvm "-asan-blacklist=${CMAKE_CURRENT_SOURCE_DIR}/asan_test.ignore" + -DASAN_HAS_BLACKLIST=1 + -DASAN_HAS_EXCEPTIONS=1 + -DASAN_NEEDS_SEGV=1 + -DASAN_UAR=0 + -c -o "${source}.asan.o" + ${CMAKE_CURRENT_SOURCE_DIR}/${source} + MAIN_DEPENDENCY ${source} + DEPENDS clang ${ARGN} + ) + endfunction() + + add_asan_compile_command(asan_globals_test.cc) + add_asan_compile_command(asan_test.cc) + add_asan_test(AsanInstrumentationTests - asan_globals_test.cc - asan_test.cc + asan_globals_test.cc.asan.o + asan_test.cc.asan.o asan_break_optimization.cc ) - set_property(TARGET AsanInstrumentationTests APPEND_STRING PROPERTY COMPILE_FLAGS - " -faddress-sanitizer ${ASAN_CFLAGS}") - set_property(TARGET AsanInstrumentationTests APPEND_STRING PROPERTY COMPILE_FLAGS - " -mllvm -asan-blacklist=${CMAKE_CURRENT_SOURCE_DIR}/asan_test.ignore") - set_property(TARGET AsanInstrumentationTests APPEND PROPERTY COMPILE_DEFINITIONS - ASAN_HAS_BLACKLIST=1 - ASAN_HAS_EXCEPTIONS=1 - ASAN_NEEDS_SEGV=1 - ASAN_UAR=0 - ) -endif() +endif() -- 2.11.4.GIT