1include(CompilerRTCompile)
2
3set(LIBFUZZER_UNITTEST_CFLAGS
4  ${COMPILER_RT_UNITTEST_CFLAGS}
5  ${COMPILER_RT_GTEST_CFLAGS}
6  -I${COMPILER_RT_SOURCE_DIR}/lib/fuzzer
7  -fno-rtti
8  -O2)
9
10if (APPLE)
11  set(FUZZER_SUPPORTED_OS osx)
12endif()
13
14add_custom_target(FuzzerUnitTests)
15set_target_properties(FuzzerUnitTests PROPERTIES FOLDER "Compiler-RT Tests")
16
17add_custom_target(FuzzedDataProviderUnitTests)
18set_target_properties(FuzzedDataProviderUnitTests PROPERTIES FOLDER "Compiler-RT Tests")
19
20set(LIBFUZZER_UNITTEST_LINK_FLAGS ${COMPILER_RT_UNITTEST_LINK_FLAGS})
21list(APPEND LIBFUZZER_UNITTEST_LINK_FLAGS --driver-mode=g++)
22
23if(WIN32)
24  list(APPEND LIBFUZZER_UNITTEST_LINK_FLAGS -Wl,-defaultlib:libcmt,-defaultlib:oldnames)
25else()
26  if (APPLE)
27    list(APPEND LIBFUZZER_UNITTEST_CFLAGS -isysroot ${DARWIN_osx_SYSROOT})
28    list(APPEND LIBFUZZER_UNITTEST_LINK_FLAGS -isysroot ${DARWIN_osx_SYSROOT})
29  endif()
30  list(APPEND LIBFUZZER_UNITTEST_LINK_FLAGS -lpthread)
31endif()
32
33if(CMAKE_SYSTEM_NAME STREQUAL "Linux" AND
34   COMPILER_RT_LIBCXX_PATH AND
35   COMPILER_RT_LIBCXXABI_PATH)
36  list(APPEND LIBFUZZER_UNITTEST_CFLAGS -nostdinc++)
37endif()
38
39if ("-fvisibility=hidden" IN_LIST LIBFUZZER_CFLAGS)
40  # Match visibility settings.
41  list(APPEND LIBFUZZER_UNITTEST_CFLAGS "-fvisibility=hidden")
42endif()
43
44if(COMPILER_RT_DEFAULT_TARGET_ARCH IN_LIST FUZZER_SUPPORTED_ARCH)
45  # libFuzzer unit tests are only run on the host machine.
46  set(arch ${COMPILER_RT_DEFAULT_TARGET_ARCH})
47
48  set(LIBFUZZER_TEST_RUNTIME RTFuzzerTest.${arch})
49  if(APPLE)
50    set(LIBFUZZER_TEST_RUNTIME_OBJECTS
51      $<TARGET_OBJECTS:RTfuzzer.osx>)
52  else()
53    set(LIBFUZZER_TEST_RUNTIME_OBJECTS
54      $<TARGET_OBJECTS:RTfuzzer.${arch}>)
55  endif()
56  add_library(${LIBFUZZER_TEST_RUNTIME} STATIC
57    ${LIBFUZZER_TEST_RUNTIME_OBJECTS})
58  set_target_properties(${LIBFUZZER_TEST_RUNTIME} PROPERTIES
59    ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
60    FOLDER "Compiler-RT Runtime tests")
61
62  if(CMAKE_SYSTEM_NAME STREQUAL "Linux" AND
63     COMPILER_RT_LIBCXX_PATH AND
64     COMPILER_RT_LIBCXXABI_PATH)
65    file(GLOB libfuzzer_headers ../*.h)
66    set(LIBFUZZER_TEST_RUNTIME_DEPS libcxx_fuzzer_${arch}-build ${libfuzzer_headers})
67    set(LIBFUZZER_TEST_RUNTIME_CFLAGS -isystem ${LIBCXX_${arch}_PREFIX}/include/c++/v1)
68    set(LIBFUZZER_TEST_RUNTIME_LINK_FLAGS ${LIBCXX_${arch}_PREFIX}/lib/libc++.a)
69  endif()
70
71  set(FuzzerTestObjects)
72  generate_compiler_rt_tests(FuzzerTestObjects
73    FuzzerUnitTests "Fuzzer-${arch}-Test" ${arch}
74    SOURCES FuzzerUnittest.cpp ${COMPILER_RT_GTEST_SOURCE}
75    RUNTIME ${LIBFUZZER_TEST_RUNTIME}
76    DEPS gtest ${LIBFUZZER_TEST_RUNTIME_DEPS}
77    CFLAGS ${LIBFUZZER_UNITTEST_CFLAGS} ${LIBFUZZER_TEST_RUNTIME_CFLAGS}
78    LINK_FLAGS ${LIBFUZZER_UNITTEST_LINK_FLAGS} ${LIBFUZZER_TEST_RUNTIME_LINK_FLAGS})
79  set_target_properties(FuzzerUnitTests PROPERTIES
80    RUNTIME_OUTPUT_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR})
81
82  set(FuzzedDataProviderTestObjects)
83  generate_compiler_rt_tests(FuzzedDataProviderTestObjects
84    FuzzedDataProviderUnitTests "FuzzerUtils-${arch}-Test" ${arch}
85    SOURCES FuzzedDataProviderUnittest.cpp ${COMPILER_RT_GTEST_SOURCE}
86    DEPS gtest ${LIBFUZZER_TEST_RUNTIME_DEPS} ${COMPILER_RT_SOURCE_DIR}/include/fuzzer/FuzzedDataProvider.h
87    CFLAGS ${LIBFUZZER_UNITTEST_CFLAGS} ${LIBFUZZER_TEST_RUNTIME_CFLAGS}
88    LINK_FLAGS ${LIBFUZZER_UNITTEST_LINK_FLAGS} ${LIBFUZZER_TEST_RUNTIME_LINK_FLAGS})
89  set_target_properties(FuzzedDataProviderUnitTests PROPERTIES
90    RUNTIME_OUTPUT_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR})
91endif()
92