summaryrefslogtreecommitdiff
path: root/cmake/CompileGTest.cmake
diff options
context:
space:
mode:
Diffstat (limited to 'cmake/CompileGTest.cmake')
-rw-r--r--cmake/CompileGTest.cmake62
1 files changed, 62 insertions, 0 deletions
diff --git a/cmake/CompileGTest.cmake b/cmake/CompileGTest.cmake
new file mode 100644
index 00000000..9aa4712f
--- /dev/null
+++ b/cmake/CompileGTest.cmake
@@ -0,0 +1,62 @@
1# Find and compile the GTest library.
2
3include(CheckCXXCompilerFlag)
4include(CheckIncludeFileCXX)
5
6message(STATUS "Checking for gtest")
7
8# Look for the sources.
9find_file(GTEST_ALL_CC gtest-all.cc PATHS
10 ${CMAKE_SOURCE_DIR}/third_party/googletest/googletest/src
11 /usr/src/gtest/src
12 NO_DEFAULT_PATH
13)
14
15if(GTEST_ALL_CC)
16 # ../.. from the source file is the source root.
17 get_filename_component(GTEST_SRC_DIR ${GTEST_ALL_CC} DIRECTORY)
18 get_filename_component(GTEST_SRC_ROOT ${GTEST_SRC_DIR} DIRECTORY)
19
20 # Look for the header file.
21 include(CheckIncludeFileCXX)
22 include_directories(SYSTEM ${GTEST_SRC_ROOT}/include)
23 check_include_file_cxx("gtest/gtest.h" HAVE_GTEST_GTEST_H)
24
25 if(HAVE_GTEST_GTEST_H)
26 message(STATUS "Found gtest: ${GTEST_SRC_ROOT}")
27
28 add_library(gtest
29 ${GTEST_SRC_DIR}/gtest-all.cc
30 ${GTEST_SRC_DIR}/gtest_main.cc)
31 target_include_directories(gtest PRIVATE ${GTEST_SRC_ROOT})
32
33 # Ignore all warnings for gtest. We don't care about their implementation.
34 check_cxx_compiler_flag("-w" HAVE_CXX_W QUIET)
35 if(HAVE_CXX_W)
36 set_target_properties(gtest PROPERTIES COMPILE_FLAGS "-w")
37 endif()
38
39 set(HAVE_GTEST TRUE)
40 set(TEST_CXX_FLAGS "")
41
42 check_cxx_compiler_flag("-Wno-global-constructors" HAVE_CXX_W_NO_GLOBAL_CONSTRUCTORS QUIET)
43 if(HAVE_CXX_W_NO_GLOBAL_CONSTRUCTORS)
44 set(TEST_CXX_FLAGS "${TEST_CXX_FLAGS} -Wno-global-constructors")
45 endif()
46
47 check_cxx_compiler_flag("-Wno-zero-as-null-pointer-constant" HAVE_CXX_W_NO_ZERO_AS_NULL_POINTER_CONSTANT QUIET)
48 if(HAVE_CXX_W_NO_ZERO_AS_NULL_POINTER_CONSTANT)
49 set(TEST_CXX_FLAGS "${TEST_CXX_FLAGS} -Wno-zero-as-null-pointer-constant")
50 endif()
51 endif()
52endif()
53
54function(unit_test subdir target)
55 if(HAVE_GTEST)
56 add_executable(unit_${target}_test ${subdir}/${target}_test.cc)
57 target_link_modules(unit_${target}_test toxcore gtest)
58 set_target_properties(unit_${target}_test PROPERTIES COMPILE_FLAGS "${TEST_CXX_FLAGS}")
59 add_test(NAME ${target} COMMAND ${CROSSCOMPILING_EMULATOR} unit_${target}_test)
60 set_property(TEST ${target} PROPERTY ENVIRONMENT "LLVM_PROFILE_FILE=${target}.profraw")
61 endif()
62endfunction()