CMakeLists.txt revision b114ed83859d8d4964ac2284584733bcd2acf4f6
16b9e31f3eb3dbe20cbc8493b963bbc6530e392c6Keith Whitwellinclude(CompilerRTCompile)
26b9e31f3eb3dbe20cbc8493b963bbc6530e392c6Keith Whitwell
36b9e31f3eb3dbe20cbc8493b963bbc6530e392c6Keith Whitwellset(SANITIZER_UNITTESTS
46b9e31f3eb3dbe20cbc8493b963bbc6530e392c6Keith Whitwell  sanitizer_allocator_test.cc
56b9e31f3eb3dbe20cbc8493b963bbc6530e392c6Keith Whitwell  sanitizer_common_test.cc
66b9e31f3eb3dbe20cbc8493b963bbc6530e392c6Keith Whitwell  sanitizer_flags_test.cc
76b9e31f3eb3dbe20cbc8493b963bbc6530e392c6Keith Whitwell  sanitizer_libc_test.cc
86b9e31f3eb3dbe20cbc8493b963bbc6530e392c6Keith Whitwell  sanitizer_linux_test.cc
96b9e31f3eb3dbe20cbc8493b963bbc6530e392c6Keith Whitwell  sanitizer_list_test.cc
106b9e31f3eb3dbe20cbc8493b963bbc6530e392c6Keith Whitwell  sanitizer_mutex_test.cc
116b9e31f3eb3dbe20cbc8493b963bbc6530e392c6Keith Whitwell  sanitizer_printf_test.cc
126b9e31f3eb3dbe20cbc8493b963bbc6530e392c6Keith Whitwell  sanitizer_scanf_interceptor_test.cc
136b9e31f3eb3dbe20cbc8493b963bbc6530e392c6Keith Whitwell  sanitizer_stackdepot_test.cc
146b9e31f3eb3dbe20cbc8493b963bbc6530e392c6Keith Whitwell  sanitizer_stacktrace_test.cc
156b9e31f3eb3dbe20cbc8493b963bbc6530e392c6Keith Whitwell  sanitizer_test_main.cc
166b9e31f3eb3dbe20cbc8493b963bbc6530e392c6Keith Whitwell  )
176b9e31f3eb3dbe20cbc8493b963bbc6530e392c6Keith Whitwell
186b9e31f3eb3dbe20cbc8493b963bbc6530e392c6Keith Whitwellset(SANITIZER_TEST_HEADERS)
196b9e31f3eb3dbe20cbc8493b963bbc6530e392c6Keith Whitwellforeach(header ${SANITIZER_HEADERS})
206b9e31f3eb3dbe20cbc8493b963bbc6530e392c6Keith Whitwell  list(APPEND SANITIZER_TEST_HEADERS ${CMAKE_CURRENT_SOURCE_DIR}/../${header})
216b9e31f3eb3dbe20cbc8493b963bbc6530e392c6Keith Whitwellendforeach()
226b9e31f3eb3dbe20cbc8493b963bbc6530e392c6Keith Whitwell
236b9e31f3eb3dbe20cbc8493b963bbc6530e392c6Keith Whitwellinclude_directories(..)
246b9e31f3eb3dbe20cbc8493b963bbc6530e392c6Keith Whitwellinclude_directories(../..)
256b9e31f3eb3dbe20cbc8493b963bbc6530e392c6Keith Whitwell
266b9e31f3eb3dbe20cbc8493b963bbc6530e392c6Keith Whitwell# Adds static library which contains sanitizer_common object file
276b9e31f3eb3dbe20cbc8493b963bbc6530e392c6Keith Whitwell# (universal binary on Mac and arch-specific object files on Linux).
286b9e31f3eb3dbe20cbc8493b963bbc6530e392c6Keith Whitwellmacro(add_sanitizer_common_lib library)
296b9e31f3eb3dbe20cbc8493b963bbc6530e392c6Keith Whitwell  add_library(${library} STATIC ${ARGN})
306b9e31f3eb3dbe20cbc8493b963bbc6530e392c6Keith Whitwell  set_target_properties(${library} PROPERTIES
31f75843a517bd188639e6866db2a7b04de3524e16Dave Airlie    ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR})
32f75843a517bd188639e6866db2a7b04de3524e16Dave Airlieendmacro()
3334474fa4119378ef9fbb9fb557cc19c0a1ca1f7eEric Anholt
3434474fa4119378ef9fbb9fb557cc19c0a1ca1f7eEric Anholtfunction(get_sanitizer_common_lib_for_arch arch lib lib_name)
3534474fa4119378ef9fbb9fb557cc19c0a1ca1f7eEric Anholt  if(APPLE)
36f75843a517bd188639e6866db2a7b04de3524e16Dave Airlie    set(tgt_name "RTSanitizerCommon.test.osx")
37f75843a517bd188639e6866db2a7b04de3524e16Dave Airlie  else()
382e5a1a254ed81b1d3efa6064f48183eefac784d0Kenneth Graunke    set(tgt_name "RTSanitizerCommon.test.${arch}")
393628185f566e178a12b493fb89abf52b4b281f99Eric Anholt  endif()
403628185f566e178a12b493fb89abf52b4b281f99Eric Anholt  set(${lib} "${tgt_name}" PARENT_SCOPE)
41ecadb51bbcb972a79f3ed79e65a7986b9396e757Brian Paul  set(${lib_name} "lib${tgt_name}.a" PARENT_SCOPE)
423628185f566e178a12b493fb89abf52b4b281f99Eric Anholtendfunction()
436b9e31f3eb3dbe20cbc8493b963bbc6530e392c6Keith Whitwell
44434fc8bde41f07687ad8941ceba03c4b3e0e75bbPaul Berry# Sanitizer_common unit tests testsuite.
45434fc8bde41f07687ad8941ceba03c4b3e0e75bbPaul Berryadd_custom_target(SanitizerUnitTests)
46434fc8bde41f07687ad8941ceba03c4b3e0e75bbPaul Berryset_target_properties(SanitizerUnitTests PROPERTIES
47434fc8bde41f07687ad8941ceba03c4b3e0e75bbPaul Berry  FOLDER "Sanitizer unittests")
486b9e31f3eb3dbe20cbc8493b963bbc6530e392c6Keith Whitwell
496b9e31f3eb3dbe20cbc8493b963bbc6530e392c6Keith Whitwell# Adds sanitizer tests for architecture.
506b9e31f3eb3dbe20cbc8493b963bbc6530e392c6Keith Whitwellmacro(add_sanitizer_tests_for_arch arch)
516b9e31f3eb3dbe20cbc8493b963bbc6530e392c6Keith Whitwell  get_target_flags_for_arch(${arch} TARGET_FLAGS)
526b9e31f3eb3dbe20cbc8493b963bbc6530e392c6Keith Whitwell  set(SANITIZER_TEST_SOURCES ${SANITIZER_UNITTESTS}
536b9e31f3eb3dbe20cbc8493b963bbc6530e392c6Keith Whitwell                             ${COMPILER_RT_GTEST_SOURCE})
546b9e31f3eb3dbe20cbc8493b963bbc6530e392c6Keith Whitwell  set(SANITIZER_TEST_CFLAGS ${COMPILER_RT_GTEST_INCLUDE_CFLAGS}
556b9e31f3eb3dbe20cbc8493b963bbc6530e392c6Keith Whitwell                            -I${COMPILER_RT_SOURCE_DIR}/include
566b9e31f3eb3dbe20cbc8493b963bbc6530e392c6Keith Whitwell                            -I${COMPILER_RT_SOURCE_DIR}/lib
576b9e31f3eb3dbe20cbc8493b963bbc6530e392c6Keith Whitwell                            -I${COMPILER_RT_SOURCE_DIR}/lib/sanitizer_common
586b9e31f3eb3dbe20cbc8493b963bbc6530e392c6Keith Whitwell                            -O2 -g -Wall -Werror ${TARGET_FLAGS})
596b9e31f3eb3dbe20cbc8493b963bbc6530e392c6Keith Whitwell  set(SANITIZER_TEST_LINK_FLAGS -lstdc++ -lpthread -ldl ${TARGET_FLAGS})
606b9e31f3eb3dbe20cbc8493b963bbc6530e392c6Keith Whitwell  set(SANITIZER_TEST_OBJECTS)
618004a1cb95b8a195f3f4bbaa8d39d2f3297167deEric Anholt  foreach(source ${SANITIZER_TEST_SOURCES})
626b9e31f3eb3dbe20cbc8493b963bbc6530e392c6Keith Whitwell    get_filename_component(basename ${source} NAME)
636b9e31f3eb3dbe20cbc8493b963bbc6530e392c6Keith Whitwell    set(output_obj "${basename}.${arch}.o")
648db761409dadc2e899d4e7107eff3aa07b07aa11Eric Anholt    clang_compile(${output_obj} ${source}
656b9e31f3eb3dbe20cbc8493b963bbc6530e392c6Keith Whitwell                  CFLAGS ${SANITIZER_TEST_CFLAGS}
668db761409dadc2e899d4e7107eff3aa07b07aa11Eric Anholt                  DEPS gtest ${SANITIZER_RUNTIME_LIBRARIES}
676b9e31f3eb3dbe20cbc8493b963bbc6530e392c6Keith Whitwell                       ${SANITIZER_TEST_HEADERS})
686b9e31f3eb3dbe20cbc8493b963bbc6530e392c6Keith Whitwell    list(APPEND SANITIZER_TEST_OBJECTS ${output_obj})
696b9e31f3eb3dbe20cbc8493b963bbc6530e392c6Keith Whitwell  endforeach()
70f75843a517bd188639e6866db2a7b04de3524e16Dave Airlie  get_sanitizer_common_lib_for_arch(${arch} SANITIZER_COMMON_LIB
712d99588b3556928a0879b4160210ac771dbf1f0bKristian Høgsberg                                    SANITIZER_COMMON_LIB_NAME)
722d99588b3556928a0879b4160210ac771dbf1f0bKristian Høgsberg  # Add unittest target.
732d99588b3556928a0879b4160210ac771dbf1f0bKristian Høgsberg  set(SANITIZER_TEST_NAME "Sanitizer-${arch}-Test")
746b9e31f3eb3dbe20cbc8493b963bbc6530e392c6Keith Whitwell  add_compiler_rt_test(SanitizerUnitTests ${SANITIZER_TEST_NAME}
756b9e31f3eb3dbe20cbc8493b963bbc6530e392c6Keith Whitwell                       OBJECTS ${SANITIZER_TEST_OBJECTS}
766b9e31f3eb3dbe20cbc8493b963bbc6530e392c6Keith Whitwell                               ${SANITIZER_COMMON_LIB_NAME}
776b9e31f3eb3dbe20cbc8493b963bbc6530e392c6Keith Whitwell                       DEPS ${SANITIZER_TEST_OBJECTS} ${SANITIZER_COMMON_LIB}
786b9e31f3eb3dbe20cbc8493b963bbc6530e392c6Keith Whitwell                       LINK_FLAGS ${SANITIZER_TEST_LINK_FLAGS})
796b9e31f3eb3dbe20cbc8493b963bbc6530e392c6Keith Whitwellendmacro()
809087ba128089ed0dc00e6eb38f37126fb7557d3bKristian Høgsberg
811ba96651e12b3c74fb9c8f5a61b183ef36a27b1eEric Anholtif(COMPILER_RT_CAN_EXECUTE_TESTS)
821ba96651e12b3c74fb9c8f5a61b183ef36a27b1eEric Anholt  # We use just-built clang to build sanitizer_common unittests, so we must
83da011faf48155a5c02ebc1fe1fa20a4f54b8c657Eric Anholt  # be sure that produced binaries would work.
842e5a1a254ed81b1d3efa6064f48183eefac784d0Kenneth Graunke  if(APPLE)
856b9e31f3eb3dbe20cbc8493b963bbc6530e392c6Keith Whitwell    add_sanitizer_common_lib("RTSanitizerCommon.test.osx"
86c5c73c1b605611faf0f06df9b5d08d8984388238Kristian Høgsberg                             $<TARGET_OBJECTS:RTSanitizerCommon.osx>)
879087ba128089ed0dc00e6eb38f37126fb7557d3bKristian Høgsberg  else()
888db761409dadc2e899d4e7107eff3aa07b07aa11Eric Anholt    if(CAN_TARGET_x86_64)
898db761409dadc2e899d4e7107eff3aa07b07aa11Eric Anholt      add_sanitizer_common_lib("RTSanitizerCommon.test.x86_64"
90f56b569e9af356c11869ee49a4669bb01b75397eKristian Høgsberg                               $<TARGET_OBJECTS:RTSanitizerCommon.x86_64>)
91c5c73c1b605611faf0f06df9b5d08d8984388238Kristian Høgsberg    endif()
922e5a1a254ed81b1d3efa6064f48183eefac784d0Kenneth Graunke    if(CAN_TARGET_i386)
93f301932dba4cc75e810e0c051e39247128a899fcKristian Høgsberg      add_sanitizer_common_lib("RTSanitizerCommon.test.i386"
94f301932dba4cc75e810e0c051e39247128a899fcKristian Høgsberg                               $<TARGET_OBJECTS:RTSanitizerCommon.i386>)
956b9e31f3eb3dbe20cbc8493b963bbc6530e392c6Keith Whitwell    endif()
966b9e31f3eb3dbe20cbc8493b963bbc6530e392c6Keith Whitwell  endif()
976b9e31f3eb3dbe20cbc8493b963bbc6530e392c6Keith Whitwell  if(CAN_TARGET_x86_64)
986b9e31f3eb3dbe20cbc8493b963bbc6530e392c6Keith Whitwell    add_sanitizer_tests_for_arch(x86_64)
996b9e31f3eb3dbe20cbc8493b963bbc6530e392c6Keith Whitwell  endif()
1007c71ef3a3d0cf2620525f468960cdc76a0fb0d33Eric Anholt  if(CAN_TARGET_i386)
1017c71ef3a3d0cf2620525f468960cdc76a0fb0d33Eric Anholt    add_sanitizer_tests_for_arch(i386)
102f8377b411dfe3c879eaab11bb86f509178796bd1Chad Versace  endif()
103f8377b411dfe3c879eaab11bb86f509178796bd1Chad Versace
104f8377b411dfe3c879eaab11bb86f509178796bd1Chad Versace  # Run unittests as a part of lit testsuite.
105f8377b411dfe3c879eaab11bb86f509178796bd1Chad Versace  configure_lit_site_cfg(
1066b9e31f3eb3dbe20cbc8493b963bbc6530e392c6Keith Whitwell    ${CMAKE_CURRENT_SOURCE_DIR}/lit.site.cfg.in
1074cb1d6a25e4749ec5e0389ca3da468adbbe5299eEric Anholt    ${CMAKE_CURRENT_BINARY_DIR}/lit.site.cfg
1084cb1d6a25e4749ec5e0389ca3da468adbbe5299eEric Anholt    )
1094cb1d6a25e4749ec5e0389ca3da468adbbe5299eEric Anholt
1106b9e31f3eb3dbe20cbc8493b963bbc6530e392c6Keith Whitwell  add_lit_testsuite(check-sanitizer "Running sanitizer library unittests"
1117c71ef3a3d0cf2620525f468960cdc76a0fb0d33Eric Anholt    ${CMAKE_CURRENT_BINARY_DIR}
1126b9e31f3eb3dbe20cbc8493b963bbc6530e392c6Keith Whitwell    DEPENDS SanitizerUnitTests
1136b9e31f3eb3dbe20cbc8493b963bbc6530e392c6Keith Whitwell    )
1146b9e31f3eb3dbe20cbc8493b963bbc6530e392c6Keith Whitwell  set_target_properties(check-sanitizer PROPERTIES FOLDER "Sanitizer unittests")
1152e5a1a254ed81b1d3efa6064f48183eefac784d0Kenneth Graunkeendif()
1168f81a6468fdbc7320800ea497791e3e1b8f782caEric Anholt
1178f81a6468fdbc7320800ea497791e3e1b8f782caEric Anholtif(ANDROID)
1188f81a6468fdbc7320800ea497791e3e1b8f782caEric Anholt  # We assume that unit tests on Android are built in a build
1198f81a6468fdbc7320800ea497791e3e1b8f782caEric Anholt  # tree with fresh Clang as a host compiler.
1208f81a6468fdbc7320800ea497791e3e1b8f782caEric Anholt  add_executable(SanitizerTest
1218f81a6468fdbc7320800ea497791e3e1b8f782caEric Anholt    ${SANITIZER_UNITTESTS}
1228f81a6468fdbc7320800ea497791e3e1b8f782caEric Anholt    ${COMPILER_RT_GTEST_SOURCE}
1232e5a1a254ed81b1d3efa6064f48183eefac784d0Kenneth Graunke    $<TARGET_OBJECTS:RTSanitizerCommon.arm.android>
1248f81a6468fdbc7320800ea497791e3e1b8f782caEric Anholt    )
1256b9e31f3eb3dbe20cbc8493b963bbc6530e392c6Keith Whitwell  set_target_compile_flags(SanitizerTest
126a9a483b43ec090408148d069bc184c0a21323654Zou Nan hai    ${SANITIZER_COMMON_CFLAGS}
127a9a483b43ec090408148d069bc184c0a21323654Zou Nan hai    ${COMPILER_RT_GTEST_INCLUDE_CFLAGS}
128a9a483b43ec090408148d069bc184c0a21323654Zou Nan hai    -I${COMPILER_RT_SOURCE_DIR}/include
129a9a483b43ec090408148d069bc184c0a21323654Zou Nan hai    -I${COMPILER_RT_SOURCE_DIR}/lib
130a9a483b43ec090408148d069bc184c0a21323654Zou Nan hai    -I${COMPILER_RT_SOURCE_DIR}/lib/sanitizer_common
131a9a483b43ec090408148d069bc184c0a21323654Zou Nan hai    -O2 -g
132a9a483b43ec090408148d069bc184c0a21323654Zou Nan hai    )
133a9a483b43ec090408148d069bc184c0a21323654Zou Nan hai  # Setup correct output directory and link flags.
134a9a483b43ec090408148d069bc184c0a21323654Zou Nan hai  set_target_properties(SanitizerTest PROPERTIES
135a9a483b43ec090408148d069bc184c0a21323654Zou Nan hai    RUNTIME_OUTPUT_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR})
1363ec0e55b63db3c1067f3bbf4563beb3b98a19288Paul Berry  set_target_link_flags(SanitizerTest ${SANITIZER_TEST_LINK_FLAGS})
1373ec0e55b63db3c1067f3bbf4563beb3b98a19288Paul Berry  # Add unit test to test suite.
13896fd94ba9421c7c3072988f999ee869534f2bc2aPaul Berry  add_dependencies(SanitizerUnitTests SanitizerTest)
13996fd94ba9421c7c3072988f999ee869534f2bc2aPaul Berryendif()
1403ec0e55b63db3c1067f3bbf4563beb3b98a19288Paul Berry