• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#------------------------------------------------------------------------------
2# Build the google test library
3
4# We compile Google Test ourselves instead of using pre-compiled libraries.
5# See the Google Test FAQ "Why is it not recommended to install a
6# pre-compiled copy of Google Test (for example, into /usr/local)?"
7# at http://code.google.com/p/googletest/wiki/FAQ for more details.
8add_library(gmock STATIC
9  gmock-gtest-all.cc gmock/gmock.h gtest/gtest.h gtest/gtest-spi.h)
10target_compile_options(gmock PUBLIC ${CPP11_FLAG})
11target_compile_definitions(gmock PUBLIC GTEST_HAS_STD_WSTRING=1)
12target_include_directories(gmock PUBLIC .)
13
14find_package(Threads)
15if (Threads_FOUND)
16  target_link_libraries(gmock ${CMAKE_THREAD_LIBS_INIT})
17else ()
18  target_compile_definitions(gmock PUBLIC GTEST_HAS_PTHREAD=0)
19endif ()
20
21if (NOT SUPPORTS_VARIADIC_TEMPLATES OR NOT SUPPORTS_INITIALIZER_LIST)
22  target_compile_definitions(gmock PUBLIC GTEST_LANG_CXX11=0)
23endif ()
24
25# Workaround a bug in implementation of variadic templates in MSVC11.
26if (MSVC)
27  target_compile_definitions(gmock PUBLIC _VARIADIC_MAX=10)
28endif ()
29
30# GTest doesn't detect <tuple> with clang.
31if (CMAKE_CXX_COMPILER_ID STREQUAL "Clang")
32  target_compile_definitions(gmock PUBLIC GTEST_USE_OWN_TR1_TUPLE=1)
33endif ()
34
35#------------------------------------------------------------------------------
36# Build the actual library tests
37
38set(TEST_MAIN_SRC test-main.cc gtest-extra.cc gtest-extra.h util.cc)
39add_library(test-main STATIC ${TEST_MAIN_SRC})
40target_compile_definitions(test-main PUBLIC
41  FMT_USE_FILE_DESCRIPTORS=$<BOOL:${HAVE_OPEN}>)
42target_link_libraries(test-main gmock fmt)
43
44include(CheckCXXCompilerFlag)
45
46# Workaround GTest bug https://github.com/google/googletest/issues/705.
47check_cxx_compiler_flag(
48  -fno-delete-null-pointer-checks HAVE_FNO_DELETE_NULL_POINTER_CHECKS)
49if (HAVE_FNO_DELETE_NULL_POINTER_CHECKS)
50  target_compile_options(test-main PUBLIC -fno-delete-null-pointer-checks)
51endif ()
52
53# Use less strict pedantic flags for the tests because GMock doesn't compile
54# cleanly with -pedantic and -std=c++98.
55if (CMAKE_COMPILER_IS_GNUCXX OR (CMAKE_CXX_COMPILER_ID MATCHES "Clang"))
56  set(PEDANTIC_COMPILE_FLAGS -Wall -Wextra -Wno-long-long -Wno-variadic-macros)
57endif ()
58
59function(add_fmt_executable name)
60  add_executable(${name} ${ARGN})
61  if (MINGW)
62    target_link_libraries(${name} -static-libgcc -static-libstdc++)
63  endif ()
64endfunction()
65
66# Adds a test.
67# Usage: add_fmt_test(name srcs...)
68function(add_fmt_test name)
69  add_fmt_executable(${name} ${name}.cc ${ARGN})
70  target_link_libraries(${name} test-main)
71
72  # define if certain c++ features can be used
73  target_compile_definitions(${name} PRIVATE
74    FMT_USE_TYPE_TRAITS=$<BOOL:${SUPPORTS_TYPE_TRAITS}>
75    FMT_USE_ENUM_BASE=$<BOOL:${SUPPORTS_ENUM_BASE}>)
76  if (FMT_PEDANTIC)
77    target_compile_options(${name} PRIVATE ${PEDANTIC_COMPILE_FLAGS})
78  endif ()
79  add_test(NAME ${name} COMMAND ${name})
80endfunction()
81
82add_fmt_test(assert-test)
83add_fmt_test(gtest-extra-test)
84add_fmt_test(format-test)
85add_fmt_test(format-impl-test)
86add_fmt_test(ostream-test)
87add_fmt_test(printf-test)
88add_fmt_test(string-test)
89add_fmt_test(time-test)
90add_fmt_test(util-test mock-allocator.h)
91add_fmt_test(macro-test)
92add_fmt_test(custom-formatter-test)
93
94# Enable stricter options for one test to make sure that the header is free of
95# warnings.
96if (FMT_PEDANTIC AND MSVC)
97  target_compile_options(format-test PRIVATE /W4)
98endif ()
99
100if (HAVE_OPEN)
101  add_fmt_executable(posix-mock-test
102    posix-mock-test.cc ../fmt/format.cc ../fmt/printf.cc ${TEST_MAIN_SRC})
103  target_include_directories(posix-mock-test PRIVATE ${PROJECT_SOURCE_DIR})
104  target_compile_definitions(posix-mock-test PRIVATE FMT_USE_FILE_DESCRIPTORS=1)
105  target_link_libraries(posix-mock-test gmock)
106  add_test(NAME posix-mock-test COMMAND posix-mock-test)
107  add_fmt_test(posix-test)
108endif ()
109
110add_fmt_executable(header-only-test
111  header-only-test.cc header-only-test2.cc test-main.cc)
112target_link_libraries(header-only-test gmock)
113if (TARGET fmt-header-only)
114  target_link_libraries(header-only-test fmt-header-only)
115else ()
116  target_include_directories(header-only-test PRIVATE ${PROJECT_SOURCE_DIR})
117  target_compile_definitions(header-only-test PRIVATE FMT_HEADER_ONLY=1)
118endif ()
119
120# Test that the library can be compiled with exceptions disabled.
121check_cxx_compiler_flag(-fno-exceptions HAVE_FNO_EXCEPTIONS_FLAG)
122if (HAVE_FNO_EXCEPTIONS_FLAG)
123  add_library(noexception-test ../fmt/format.cc)
124  target_include_directories(noexception-test PRIVATE ${PROJECT_SOURCE_DIR})
125  target_compile_options(noexception-test PRIVATE -fno-exceptions)
126endif ()
127
128if (FMT_PEDANTIC)
129  # Test that the library compiles without windows.h.
130  if (CMAKE_SYSTEM_NAME STREQUAL "Windows")
131    add_library(no-windows-h-test ../fmt/format.cc)
132    target_include_directories(no-windows-h-test PRIVATE ${PROJECT_SOURCE_DIR})
133    target_compile_definitions(no-windows-h-test PRIVATE FMT_USE_WINDOWS_H=0)
134  endif ()
135
136  add_test(compile-test ${CMAKE_CTEST_COMMAND}
137    --build-and-test
138    "${CMAKE_CURRENT_SOURCE_DIR}/compile-test"
139    "${CMAKE_CURRENT_BINARY_DIR}/compile-test"
140    --build-generator ${CMAKE_GENERATOR}
141    --build-makeprogram ${CMAKE_MAKE_PROGRAM}
142    --build-options
143    "-DCMAKE_CXX_COMPILER=${CMAKE_CXX_COMPILER}"
144    "-DCPP11_FLAG=${CPP11_FLAG}"
145    "-DSUPPORTS_USER_DEFINED_LITERALS=${SUPPORTS_USER_DEFINED_LITERALS}")
146
147  # test if the targets are findable from the build directory
148  add_test(find-package-test ${CMAKE_CTEST_COMMAND}
149    -C ${CMAKE_BUILD_TYPE}
150    --build-and-test
151    "${CMAKE_CURRENT_SOURCE_DIR}/find-package-test"
152    "${CMAKE_CURRENT_BINARY_DIR}/find-package-test"
153    --build-generator ${CMAKE_GENERATOR}
154    --build-makeprogram ${CMAKE_MAKE_PROGRAM}
155    --build-options
156    "-DCMAKE_CXX_COMPILER=${CMAKE_CXX_COMPILER}"
157    "-DFMT_DIR=${PROJECT_BINARY_DIR}"
158    "-DCMAKE_BUILD_TYPE=${CMAKE_BUILD_TYPE}")
159
160  # test if the targets are findable when add_subdirectory is used
161  add_test(add-subdirectory-test ${CMAKE_CTEST_COMMAND}
162    -C ${CMAKE_BUILD_TYPE}
163    --build-and-test
164    "${CMAKE_CURRENT_SOURCE_DIR}/add-subdirectory-test"
165    "${CMAKE_CURRENT_BINARY_DIR}/add-subdirectory-test"
166    --build-generator ${CMAKE_GENERATOR}
167    --build-makeprogram ${CMAKE_MAKE_PROGRAM}
168    --build-options
169    "-DCMAKE_CXX_COMPILER=${CMAKE_CXX_COMPILER}"
170    "-DCMAKE_BUILD_TYPE=${CMAKE_BUILD_TYPE}")
171endif ()
172