1 ########################################################################
2 # Note: CMake support is community-based. The maintainers do not use CMake
5 # CMake build script for Google Test.
7 # To run the tests for Google Test itself on Linux, use 'make test' or
8 # ctest. You can select which tests to run using 'ctest -R regex'.
9 # For more options, run 'ctest --help'.
11 # When other libraries are using a shared version of runtime libraries,
12 # Google Test also has to use one.
14 gtest_force_shared_crt
15 "Use shared (DLL) run-time lib even when Google Test is built as static lib."
18 option(gtest_build_tests "Build all of gtest's own tests." OFF)
20 option(gtest_build_samples "Build gtest's sample programs." OFF)
22 option(gtest_disable_pthreads "Disable uses of pthreads in gtest." OFF)
25 gtest_hide_internal_symbols
26 "Build gtest with internal symbols hidden in shared libraries."
29 # Defines pre_project_set_up_hermetic_build() and set_up_hermetic_build().
30 include(cmake/hermetic_build.cmake OPTIONAL)
32 if (COMMAND pre_project_set_up_hermetic_build)
33 pre_project_set_up_hermetic_build()
36 ########################################################################
38 # Project-wide settings.
40 # Name of the project.
42 # CMake files in this project can refer to the root source directory
43 # as ${gtest_SOURCE_DIR} and to the root binary directory as
44 # ${gtest_BINARY_DIR}.
45 # Language "C" is required for find_package(Threads).
49 cmake_minimum_required(VERSION 3.13)
50 project(gtest VERSION ${GOOGLETEST_VERSION} LANGUAGES CXX C)
52 if (COMMAND set_up_hermetic_build)
53 set_up_hermetic_build()
56 # These commands only run if this is the main project.
57 if(CMAKE_PROJECT_NAME STREQUAL "gtest" OR CMAKE_PROJECT_NAME STREQUAL "googletest-distribution")
59 # BUILD_SHARED_LIBS is a standard CMake variable, but we declare it here to
60 # make it prominent in the GUI.
61 option(BUILD_SHARED_LIBS "Build shared libraries (DLLs)." OFF)
66 gtest_force_shared_crt
69 gtest_disable_pthreads
70 gtest_hide_internal_symbols)
75 if (gtest_hide_internal_symbols)
76 set(CMAKE_CXX_VISIBILITY_PRESET hidden)
77 set(CMAKE_VISIBILITY_INLINES_HIDDEN 1)
80 # Define helper functions and macros used by Google Test.
81 include(cmake/internal_utils.cmake)
83 config_compiler_and_linker() # Defined in internal_utils.cmake.
85 # Needed to set the namespace for both the export targets and the
87 set(cmake_package_name GTest CACHE INTERNAL "")
89 # Create the CMake package file descriptors.
91 include(CMakePackageConfigHelpers)
92 set(targets_export_name ${cmake_package_name}Targets CACHE INTERNAL "")
93 set(generated_dir "${CMAKE_CURRENT_BINARY_DIR}/generated" CACHE INTERNAL "")
94 set(cmake_files_install_dir "${CMAKE_INSTALL_LIBDIR}/cmake/${cmake_package_name}")
95 set(version_file "${generated_dir}/${cmake_package_name}ConfigVersion.cmake")
96 write_basic_package_version_file(${version_file} VERSION ${GOOGLETEST_VERSION} COMPATIBILITY AnyNewerVersion)
97 install(EXPORT ${targets_export_name}
98 COMPONENT "${PROJECT_NAME}"
99 NAMESPACE ${cmake_package_name}::
100 DESTINATION ${cmake_files_install_dir})
101 set(config_file "${generated_dir}/${cmake_package_name}Config.cmake")
102 configure_package_config_file("${gtest_SOURCE_DIR}/cmake/Config.cmake.in"
103 "${config_file}" INSTALL_DESTINATION ${cmake_files_install_dir})
104 install(FILES ${version_file} ${config_file}
105 COMPONENT "${PROJECT_NAME}"
106 DESTINATION ${cmake_files_install_dir})
109 # Where Google Test's .h files can be found.
110 set(gtest_build_include_dirs
111 "${gtest_SOURCE_DIR}/include"
112 "${gtest_SOURCE_DIR}")
113 include_directories(${gtest_build_include_dirs})
115 ########################################################################
117 # Defines the gtest & gtest_main libraries. User tests should link
120 # Google Test libraries. We build them using more strict warnings than what
121 # are used for other targets, to ensure that gtest can be compiled by a user
122 # aggressive about warnings.
123 cxx_library(gtest "${cxx_strict}" src/gtest-all.cc)
124 set_target_properties(gtest PROPERTIES VERSION ${GOOGLETEST_VERSION})
126 target_compile_definitions(gtest PUBLIC GTEST_HAS_ABSL=1)
127 target_link_libraries(gtest PUBLIC
128 absl::failure_signal_handler
132 absl::flags_reflection
141 cxx_library(gtest_main "${cxx_strict}" src/gtest_main.cc)
142 set_target_properties(gtest_main PROPERTIES VERSION ${GOOGLETEST_VERSION})
143 string(REPLACE ";" "$<SEMICOLON>" dirs "${gtest_build_include_dirs}")
144 target_include_directories(gtest SYSTEM INTERFACE
145 "$<BUILD_INTERFACE:${dirs}>"
146 "$<INSTALL_INTERFACE:$<INSTALL_PREFIX>/${CMAKE_INSTALL_INCLUDEDIR}>")
147 target_include_directories(gtest_main SYSTEM INTERFACE
148 "$<BUILD_INTERFACE:${dirs}>"
149 "$<INSTALL_INTERFACE:$<INSTALL_PREFIX>/${CMAKE_INSTALL_INCLUDEDIR}>")
150 if(CMAKE_SYSTEM_NAME MATCHES "QNX" AND CMAKE_SYSTEM_VERSION VERSION_GREATER_EQUAL 7.1)
151 target_link_libraries(gtest PUBLIC regex)
153 target_link_libraries(gtest_main PUBLIC gtest)
155 ########################################################################
158 install_project(gtest gtest_main)
160 ########################################################################
162 # Samples on how to link user tests with gtest or gtest_main.
164 # They are not built by default. To build them, set the
165 # gtest_build_samples option to ON. You can do it by running ccmake
166 # or specifying the -Dgtest_build_samples=ON flag when running cmake.
168 if (gtest_build_samples)
169 cxx_executable(sample1_unittest samples gtest_main samples/sample1.cc)
170 cxx_executable(sample2_unittest samples gtest_main samples/sample2.cc)
171 cxx_executable(sample3_unittest samples gtest_main)
172 cxx_executable(sample4_unittest samples gtest_main samples/sample4.cc)
173 cxx_executable(sample5_unittest samples gtest_main samples/sample1.cc)
174 cxx_executable(sample6_unittest samples gtest_main)
175 cxx_executable(sample7_unittest samples gtest_main)
176 cxx_executable(sample8_unittest samples gtest_main)
177 cxx_executable(sample9_unittest samples gtest)
178 cxx_executable(sample10_unittest samples gtest)
181 ########################################################################
183 # Google Test's own tests.
185 # You can skip this section if you aren't interested in testing
186 # Google Test itself.
188 # The tests are not built by default. To build them, set the
189 # gtest_build_tests option to ON. You can do it by running ccmake
190 # or specifying the -Dgtest_build_tests=ON flag when running cmake.
192 if (gtest_build_tests)
193 # This must be set in the root directory for the tests to be run by
194 # 'make test' or ctest.
197 ############################################################
198 # C++ tests built with standard compiler flags.
200 cxx_test(googletest-death-test-test gtest_main)
201 cxx_test(gtest_environment_test gtest)
202 cxx_test(googletest-filepath-test gtest_main)
203 cxx_test(googletest-listener-test gtest_main)
204 cxx_test(gtest_main_unittest gtest_main)
205 cxx_test(googletest-message-test gtest_main)
206 cxx_test(gtest_no_test_unittest gtest)
207 cxx_test(googletest-options-test gtest_main)
208 cxx_test(googletest-param-test-test gtest
209 test/googletest-param-test2-test.cc)
210 cxx_test(googletest-port-test gtest_main)
211 cxx_test(gtest_pred_impl_unittest gtest_main)
212 cxx_test(gtest_premature_exit_test gtest
213 test/gtest_premature_exit_test.cc)
214 cxx_test(googletest-printers-test gtest_main)
215 cxx_test(gtest_prod_test gtest_main
217 cxx_test(gtest_repeat_test gtest)
218 cxx_test(gtest_sole_header_test gtest_main)
219 cxx_test(gtest_stress_test gtest)
220 cxx_test(googletest-test-part-test gtest_main)
221 cxx_test(gtest_throw_on_failure_ex_test gtest)
222 cxx_test(gtest-typed-test_test gtest_main
223 test/gtest-typed-test2_test.cc)
224 cxx_test(gtest_unittest gtest_main)
225 cxx_test(gtest-unittest-api_test gtest)
226 cxx_test(gtest_skip_in_environment_setup_test gtest_main)
227 cxx_test(gtest_skip_test gtest_main)
229 ############################################################
230 # C++ tests built with non-standard compiler flags.
232 # MSVC 7.1 does not support STL with exceptions disabled.
233 if (NOT MSVC OR MSVC_VERSION GREATER 1310)
234 cxx_library(gtest_no_exception "${cxx_no_exception}"
236 cxx_library(gtest_main_no_exception "${cxx_no_exception}"
237 src/gtest-all.cc src/gtest_main.cc)
239 cxx_library(gtest_main_no_rtti "${cxx_no_rtti}"
240 src/gtest-all.cc src/gtest_main.cc)
242 cxx_test_with_flags(gtest-death-test_ex_nocatch_test
243 "${cxx_exception} -DGTEST_ENABLE_CATCH_EXCEPTIONS_=0"
244 gtest test/googletest-death-test_ex_test.cc)
245 cxx_test_with_flags(gtest-death-test_ex_catch_test
246 "${cxx_exception} -DGTEST_ENABLE_CATCH_EXCEPTIONS_=1"
247 gtest test/googletest-death-test_ex_test.cc)
249 cxx_test_with_flags(gtest_no_rtti_unittest "${cxx_no_rtti}"
250 gtest_main_no_rtti test/gtest_unittest.cc)
252 cxx_shared_library(gtest_dll "${cxx_default}"
253 src/gtest-all.cc src/gtest_main.cc)
255 cxx_executable_with_flags(gtest_dll_test_ "${cxx_default}"
256 gtest_dll test/gtest_all_test.cc)
257 set_target_properties(gtest_dll_test_
259 COMPILE_DEFINITIONS "GTEST_LINKED_AS_SHARED_LIBRARY=1")
261 ############################################################
264 cxx_executable(googletest-break-on-failure-unittest_ test gtest)
265 py_test(googletest-break-on-failure-unittest)
267 py_test(gtest_skip_check_output_test)
268 py_test(gtest_skip_environment_check_output_test)
270 # Visual Studio .NET 2003 does not support STL with exceptions disabled.
271 if (NOT MSVC OR MSVC_VERSION GREATER 1310) # 1310 is Visual Studio .NET 2003
272 cxx_executable_with_flags(
273 googletest-catch-exceptions-no-ex-test_
274 "${cxx_no_exception}"
275 gtest_main_no_exception
276 test/googletest-catch-exceptions-test_.cc)
279 cxx_executable_with_flags(
280 googletest-catch-exceptions-ex-test_
283 test/googletest-catch-exceptions-test_.cc)
284 py_test(googletest-catch-exceptions-test)
286 cxx_executable(googletest-color-test_ test gtest)
287 py_test(googletest-color-test)
289 cxx_executable(googletest-env-var-test_ test gtest)
290 py_test(googletest-env-var-test)
292 cxx_executable(googletest-filter-unittest_ test gtest)
293 py_test(googletest-filter-unittest)
295 cxx_executable(gtest_help_test_ test gtest_main)
296 py_test(gtest_help_test)
298 cxx_executable(googletest-list-tests-unittest_ test gtest)
299 py_test(googletest-list-tests-unittest)
301 cxx_executable(googletest-output-test_ test gtest)
302 py_test(googletest-output-test --no_stacktrace_support)
304 cxx_executable(googletest-shuffle-test_ test gtest)
305 py_test(googletest-shuffle-test)
307 # MSVC 7.1 does not support STL with exceptions disabled.
308 if (NOT MSVC OR MSVC_VERSION GREATER 1310)
309 cxx_executable(googletest-throw-on-failure-test_ test gtest_no_exception)
310 set_target_properties(googletest-throw-on-failure-test_
312 COMPILE_FLAGS "${cxx_no_exception}")
313 py_test(googletest-throw-on-failure-test)
316 cxx_executable(googletest-uninitialized-test_ test gtest)
317 py_test(googletest-uninitialized-test)
319 cxx_executable(gtest_list_output_unittest_ test gtest)
320 py_test(gtest_list_output_unittest)
322 cxx_executable(gtest_xml_outfile1_test_ test gtest_main)
323 cxx_executable(gtest_xml_outfile2_test_ test gtest_main)
324 py_test(gtest_xml_outfiles_test)
325 py_test(googletest-json-outfiles-test)
327 cxx_executable(gtest_xml_output_unittest_ test gtest)
328 py_test(gtest_xml_output_unittest --no_stacktrace_support)
329 py_test(googletest-json-output-unittest --no_stacktrace_support)