blob: abdd98b79a40b697c7207b63b304ed17fde7e66a [file] [log] [blame]
Austin Schuh0cbef622015-09-06 17:34:52 -07001########################################################################
James Kuszmaule2f15292021-05-10 22:37:32 -07002# Note: CMake support is community-based. The maintainers do not use CMake
3# internally.
4#
Austin Schuh0cbef622015-09-06 17:34:52 -07005# CMake build script for Google Test.
6#
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'.
10
Austin Schuh0cbef622015-09-06 17:34:52 -070011# When other libraries are using a shared version of runtime libraries,
12# Google Test also has to use one.
13option(
14 gtest_force_shared_crt
15 "Use shared (DLL) run-time lib even when Google Test is built as static lib."
16 OFF)
17
18option(gtest_build_tests "Build all of gtest's own tests." OFF)
19
20option(gtest_build_samples "Build gtest's sample programs." OFF)
21
22option(gtest_disable_pthreads "Disable uses of pthreads in gtest." OFF)
23
Austin Schuh889ac432018-10-29 22:57:02 -070024option(
25 gtest_hide_internal_symbols
26 "Build gtest with internal symbols hidden in shared libraries."
27 OFF)
28
Austin Schuh0cbef622015-09-06 17:34:52 -070029# Defines pre_project_set_up_hermetic_build() and set_up_hermetic_build().
30include(cmake/hermetic_build.cmake OPTIONAL)
31
32if (COMMAND pre_project_set_up_hermetic_build)
33 pre_project_set_up_hermetic_build()
34endif()
35
36########################################################################
37#
38# Project-wide settings
39
40# Name of the project.
41#
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).
James Kuszmaule2f15292021-05-10 22:37:32 -070046
47# Project version:
48
Austin Schuh889ac432018-10-29 22:57:02 -070049if (CMAKE_VERSION VERSION_LESS 3.0)
50 project(gtest CXX C)
James Kuszmaule2f15292021-05-10 22:37:32 -070051 set(PROJECT_VERSION ${GOOGLETEST_VERSION})
Austin Schuh889ac432018-10-29 22:57:02 -070052else()
53 cmake_policy(SET CMP0048 NEW)
54 project(gtest VERSION ${GOOGLETEST_VERSION} LANGUAGES CXX C)
55endif()
James Kuszmaule2f15292021-05-10 22:37:32 -070056cmake_minimum_required(VERSION 2.8.12)
Austin Schuh889ac432018-10-29 22:57:02 -070057
58if (POLICY CMP0063) # Visibility
59 cmake_policy(SET CMP0063 NEW)
60endif (POLICY CMP0063)
Austin Schuh0cbef622015-09-06 17:34:52 -070061
62if (COMMAND set_up_hermetic_build)
63 set_up_hermetic_build()
64endif()
65
Austin Schuh889ac432018-10-29 22:57:02 -070066# These commands only run if this is the main project
67if(CMAKE_PROJECT_NAME STREQUAL "gtest" OR CMAKE_PROJECT_NAME STREQUAL "googletest-distribution")
68
69 # BUILD_SHARED_LIBS is a standard CMake variable, but we declare it here to
70 # make it prominent in the GUI.
71 option(BUILD_SHARED_LIBS "Build shared libraries (DLLs)." OFF)
72
73else()
74
75 mark_as_advanced(
76 gtest_force_shared_crt
77 gtest_build_tests
78 gtest_build_samples
79 gtest_disable_pthreads
80 gtest_hide_internal_symbols)
81
82endif()
83
84
85if (gtest_hide_internal_symbols)
86 set(CMAKE_CXX_VISIBILITY_PRESET hidden)
87 set(CMAKE_VISIBILITY_INLINES_HIDDEN 1)
88endif()
89
Austin Schuh0cbef622015-09-06 17:34:52 -070090# Define helper functions and macros used by Google Test.
91include(cmake/internal_utils.cmake)
92
93config_compiler_and_linker() # Defined in internal_utils.cmake.
94
James Kuszmaule2f15292021-05-10 22:37:32 -070095# Needed to set the namespace for both the export targets and the
96# alias libraries
97set(cmake_package_name GTest CACHE INTERNAL "")
98
Austin Schuh889ac432018-10-29 22:57:02 -070099# Create the CMake package file descriptors.
100if (INSTALL_GTEST)
101 include(CMakePackageConfigHelpers)
Austin Schuh889ac432018-10-29 22:57:02 -0700102 set(targets_export_name ${cmake_package_name}Targets CACHE INTERNAL "")
103 set(generated_dir "${CMAKE_CURRENT_BINARY_DIR}/generated" CACHE INTERNAL "")
104 set(cmake_files_install_dir "${CMAKE_INSTALL_LIBDIR}/cmake/${cmake_package_name}")
105 set(version_file "${generated_dir}/${cmake_package_name}ConfigVersion.cmake")
James Kuszmaule2f15292021-05-10 22:37:32 -0700106 write_basic_package_version_file(${version_file} VERSION ${GOOGLETEST_VERSION} COMPATIBILITY AnyNewerVersion)
Austin Schuh889ac432018-10-29 22:57:02 -0700107 install(EXPORT ${targets_export_name}
108 NAMESPACE ${cmake_package_name}::
109 DESTINATION ${cmake_files_install_dir})
110 set(config_file "${generated_dir}/${cmake_package_name}Config.cmake")
111 configure_package_config_file("${gtest_SOURCE_DIR}/cmake/Config.cmake.in"
112 "${config_file}" INSTALL_DESTINATION ${cmake_files_install_dir})
113 install(FILES ${version_file} ${config_file}
114 DESTINATION ${cmake_files_install_dir})
115endif()
Austin Schuh0cbef622015-09-06 17:34:52 -0700116
Austin Schuh889ac432018-10-29 22:57:02 -0700117# Where Google Test's .h files can be found.
118set(gtest_build_include_dirs
119 "${gtest_SOURCE_DIR}/include"
120 "${gtest_SOURCE_DIR}")
121include_directories(${gtest_build_include_dirs})
Austin Schuh0cbef622015-09-06 17:34:52 -0700122
Austin Schuh0cbef622015-09-06 17:34:52 -0700123########################################################################
124#
125# Defines the gtest & gtest_main libraries. User tests should link
126# with one of them.
127
128# Google Test libraries. We build them using more strict warnings than what
129# are used for other targets, to ensure that gtest can be compiled by a user
130# aggressive about warnings.
131cxx_library(gtest "${cxx_strict}" src/gtest-all.cc)
James Kuszmaule2f15292021-05-10 22:37:32 -0700132set_target_properties(gtest PROPERTIES VERSION ${GOOGLETEST_VERSION})
Austin Schuh0cbef622015-09-06 17:34:52 -0700133cxx_library(gtest_main "${cxx_strict}" src/gtest_main.cc)
James Kuszmaule2f15292021-05-10 22:37:32 -0700134set_target_properties(gtest_main PROPERTIES VERSION ${GOOGLETEST_VERSION})
Austin Schuh889ac432018-10-29 22:57:02 -0700135# If the CMake version supports it, attach header directory information
136# to the targets for when we are part of a parent build (ie being pulled
137# in via add_subdirectory() rather than being a standalone build).
138if (DEFINED CMAKE_VERSION AND NOT "${CMAKE_VERSION}" VERSION_LESS "2.8.11")
139 target_include_directories(gtest SYSTEM INTERFACE
140 "$<BUILD_INTERFACE:${gtest_build_include_dirs}>"
141 "$<INSTALL_INTERFACE:$<INSTALL_PREFIX>/${CMAKE_INSTALL_INCLUDEDIR}>")
142 target_include_directories(gtest_main SYSTEM INTERFACE
143 "$<BUILD_INTERFACE:${gtest_build_include_dirs}>"
144 "$<INSTALL_INTERFACE:$<INSTALL_PREFIX>/${CMAKE_INSTALL_INCLUDEDIR}>")
145endif()
146target_link_libraries(gtest_main PUBLIC gtest)
147
148########################################################################
149#
150# Install rules
151install_project(gtest gtest_main)
Austin Schuh0cbef622015-09-06 17:34:52 -0700152
153########################################################################
154#
155# Samples on how to link user tests with gtest or gtest_main.
156#
157# They are not built by default. To build them, set the
158# gtest_build_samples option to ON. You can do it by running ccmake
159# or specifying the -Dgtest_build_samples=ON flag when running cmake.
160
161if (gtest_build_samples)
162 cxx_executable(sample1_unittest samples gtest_main samples/sample1.cc)
163 cxx_executable(sample2_unittest samples gtest_main samples/sample2.cc)
164 cxx_executable(sample3_unittest samples gtest_main)
165 cxx_executable(sample4_unittest samples gtest_main samples/sample4.cc)
166 cxx_executable(sample5_unittest samples gtest_main samples/sample1.cc)
167 cxx_executable(sample6_unittest samples gtest_main)
168 cxx_executable(sample7_unittest samples gtest_main)
169 cxx_executable(sample8_unittest samples gtest_main)
170 cxx_executable(sample9_unittest samples gtest)
171 cxx_executable(sample10_unittest samples gtest)
172endif()
173
174########################################################################
175#
176# Google Test's own tests.
177#
178# You can skip this section if you aren't interested in testing
179# Google Test itself.
180#
181# The tests are not built by default. To build them, set the
182# gtest_build_tests option to ON. You can do it by running ccmake
183# or specifying the -Dgtest_build_tests=ON flag when running cmake.
184
185if (gtest_build_tests)
186 # This must be set in the root directory for the tests to be run by
187 # 'make test' or ctest.
188 enable_testing()
189
190 ############################################################
191 # C++ tests built with standard compiler flags.
192
Austin Schuh889ac432018-10-29 22:57:02 -0700193 cxx_test(googletest-death-test-test gtest_main)
Austin Schuh0cbef622015-09-06 17:34:52 -0700194 cxx_test(gtest_environment_test gtest)
Austin Schuh889ac432018-10-29 22:57:02 -0700195 cxx_test(googletest-filepath-test gtest_main)
Austin Schuh889ac432018-10-29 22:57:02 -0700196 cxx_test(googletest-listener-test gtest_main)
Austin Schuh0cbef622015-09-06 17:34:52 -0700197 cxx_test(gtest_main_unittest gtest_main)
Austin Schuh889ac432018-10-29 22:57:02 -0700198 cxx_test(googletest-message-test gtest_main)
Austin Schuh0cbef622015-09-06 17:34:52 -0700199 cxx_test(gtest_no_test_unittest gtest)
Austin Schuh889ac432018-10-29 22:57:02 -0700200 cxx_test(googletest-options-test gtest_main)
201 cxx_test(googletest-param-test-test gtest
202 test/googletest-param-test2-test.cc)
203 cxx_test(googletest-port-test gtest_main)
Austin Schuh0cbef622015-09-06 17:34:52 -0700204 cxx_test(gtest_pred_impl_unittest gtest_main)
205 cxx_test(gtest_premature_exit_test gtest
206 test/gtest_premature_exit_test.cc)
Austin Schuh889ac432018-10-29 22:57:02 -0700207 cxx_test(googletest-printers-test gtest_main)
Austin Schuh0cbef622015-09-06 17:34:52 -0700208 cxx_test(gtest_prod_test gtest_main
209 test/production.cc)
210 cxx_test(gtest_repeat_test gtest)
211 cxx_test(gtest_sole_header_test gtest_main)
212 cxx_test(gtest_stress_test gtest)
Austin Schuh889ac432018-10-29 22:57:02 -0700213 cxx_test(googletest-test-part-test gtest_main)
Austin Schuh0cbef622015-09-06 17:34:52 -0700214 cxx_test(gtest_throw_on_failure_ex_test gtest)
215 cxx_test(gtest-typed-test_test gtest_main
216 test/gtest-typed-test2_test.cc)
217 cxx_test(gtest_unittest gtest_main)
218 cxx_test(gtest-unittest-api_test gtest)
James Kuszmaule2f15292021-05-10 22:37:32 -0700219 cxx_test(gtest_skip_in_environment_setup_test gtest_main)
220 cxx_test(gtest_skip_test gtest_main)
Austin Schuh0cbef622015-09-06 17:34:52 -0700221
222 ############################################################
223 # C++ tests built with non-standard compiler flags.
224
225 # MSVC 7.1 does not support STL with exceptions disabled.
226 if (NOT MSVC OR MSVC_VERSION GREATER 1310)
227 cxx_library(gtest_no_exception "${cxx_no_exception}"
228 src/gtest-all.cc)
229 cxx_library(gtest_main_no_exception "${cxx_no_exception}"
230 src/gtest-all.cc src/gtest_main.cc)
231 endif()
232 cxx_library(gtest_main_no_rtti "${cxx_no_rtti}"
233 src/gtest-all.cc src/gtest_main.cc)
234
235 cxx_test_with_flags(gtest-death-test_ex_nocatch_test
236 "${cxx_exception} -DGTEST_ENABLE_CATCH_EXCEPTIONS_=0"
Austin Schuh889ac432018-10-29 22:57:02 -0700237 gtest test/googletest-death-test_ex_test.cc)
Austin Schuh0cbef622015-09-06 17:34:52 -0700238 cxx_test_with_flags(gtest-death-test_ex_catch_test
239 "${cxx_exception} -DGTEST_ENABLE_CATCH_EXCEPTIONS_=1"
Austin Schuh889ac432018-10-29 22:57:02 -0700240 gtest test/googletest-death-test_ex_test.cc)
Austin Schuh0cbef622015-09-06 17:34:52 -0700241
242 cxx_test_with_flags(gtest_no_rtti_unittest "${cxx_no_rtti}"
243 gtest_main_no_rtti test/gtest_unittest.cc)
244
245 cxx_shared_library(gtest_dll "${cxx_default}"
246 src/gtest-all.cc src/gtest_main.cc)
247
248 cxx_executable_with_flags(gtest_dll_test_ "${cxx_default}"
249 gtest_dll test/gtest_all_test.cc)
250 set_target_properties(gtest_dll_test_
251 PROPERTIES
252 COMPILE_DEFINITIONS "GTEST_LINKED_AS_SHARED_LIBRARY=1")
253
Austin Schuh0cbef622015-09-06 17:34:52 -0700254 ############################################################
255 # Python tests.
256
Austin Schuh889ac432018-10-29 22:57:02 -0700257 cxx_executable(googletest-break-on-failure-unittest_ test gtest)
258 py_test(googletest-break-on-failure-unittest)
Austin Schuh0cbef622015-09-06 17:34:52 -0700259
James Kuszmaule2f15292021-05-10 22:37:32 -0700260 py_test(gtest_skip_check_output_test)
261 py_test(gtest_skip_environment_check_output_test)
262
Austin Schuh0cbef622015-09-06 17:34:52 -0700263 # Visual Studio .NET 2003 does not support STL with exceptions disabled.
264 if (NOT MSVC OR MSVC_VERSION GREATER 1310) # 1310 is Visual Studio .NET 2003
265 cxx_executable_with_flags(
Austin Schuh889ac432018-10-29 22:57:02 -0700266 googletest-catch-exceptions-no-ex-test_
Austin Schuh0cbef622015-09-06 17:34:52 -0700267 "${cxx_no_exception}"
268 gtest_main_no_exception
Austin Schuh889ac432018-10-29 22:57:02 -0700269 test/googletest-catch-exceptions-test_.cc)
Austin Schuh0cbef622015-09-06 17:34:52 -0700270 endif()
271
272 cxx_executable_with_flags(
Austin Schuh889ac432018-10-29 22:57:02 -0700273 googletest-catch-exceptions-ex-test_
Austin Schuh0cbef622015-09-06 17:34:52 -0700274 "${cxx_exception}"
275 gtest_main
Austin Schuh889ac432018-10-29 22:57:02 -0700276 test/googletest-catch-exceptions-test_.cc)
277 py_test(googletest-catch-exceptions-test)
Austin Schuh0cbef622015-09-06 17:34:52 -0700278
Austin Schuh889ac432018-10-29 22:57:02 -0700279 cxx_executable(googletest-color-test_ test gtest)
280 py_test(googletest-color-test)
Austin Schuh0cbef622015-09-06 17:34:52 -0700281
Austin Schuh889ac432018-10-29 22:57:02 -0700282 cxx_executable(googletest-env-var-test_ test gtest)
283 py_test(googletest-env-var-test)
Austin Schuh0cbef622015-09-06 17:34:52 -0700284
Austin Schuh889ac432018-10-29 22:57:02 -0700285 cxx_executable(googletest-filter-unittest_ test gtest)
286 py_test(googletest-filter-unittest)
Austin Schuh0cbef622015-09-06 17:34:52 -0700287
288 cxx_executable(gtest_help_test_ test gtest_main)
289 py_test(gtest_help_test)
290
Austin Schuh889ac432018-10-29 22:57:02 -0700291 cxx_executable(googletest-list-tests-unittest_ test gtest)
292 py_test(googletest-list-tests-unittest)
Austin Schuh0cbef622015-09-06 17:34:52 -0700293
Austin Schuh889ac432018-10-29 22:57:02 -0700294 cxx_executable(googletest-output-test_ test gtest)
295 py_test(googletest-output-test --no_stacktrace_support)
Austin Schuh0cbef622015-09-06 17:34:52 -0700296
Austin Schuh889ac432018-10-29 22:57:02 -0700297 cxx_executable(googletest-shuffle-test_ test gtest)
298 py_test(googletest-shuffle-test)
Austin Schuh0cbef622015-09-06 17:34:52 -0700299
300 # MSVC 7.1 does not support STL with exceptions disabled.
301 if (NOT MSVC OR MSVC_VERSION GREATER 1310)
Austin Schuh889ac432018-10-29 22:57:02 -0700302 cxx_executable(googletest-throw-on-failure-test_ test gtest_no_exception)
303 set_target_properties(googletest-throw-on-failure-test_
Austin Schuh0cbef622015-09-06 17:34:52 -0700304 PROPERTIES
305 COMPILE_FLAGS "${cxx_no_exception}")
Austin Schuh889ac432018-10-29 22:57:02 -0700306 py_test(googletest-throw-on-failure-test)
Austin Schuh0cbef622015-09-06 17:34:52 -0700307 endif()
308
Austin Schuh889ac432018-10-29 22:57:02 -0700309 cxx_executable(googletest-uninitialized-test_ test gtest)
310 py_test(googletest-uninitialized-test)
Austin Schuh0cbef622015-09-06 17:34:52 -0700311
James Kuszmaule2f15292021-05-10 22:37:32 -0700312 cxx_executable(gtest_list_output_unittest_ test gtest)
313 py_test(gtest_list_output_unittest)
314
Austin Schuh0cbef622015-09-06 17:34:52 -0700315 cxx_executable(gtest_xml_outfile1_test_ test gtest_main)
316 cxx_executable(gtest_xml_outfile2_test_ test gtest_main)
317 py_test(gtest_xml_outfiles_test)
Austin Schuh889ac432018-10-29 22:57:02 -0700318 py_test(googletest-json-outfiles-test)
Austin Schuh0cbef622015-09-06 17:34:52 -0700319
320 cxx_executable(gtest_xml_output_unittest_ test gtest)
Austin Schuh889ac432018-10-29 22:57:02 -0700321 py_test(gtest_xml_output_unittest --no_stacktrace_support)
322 py_test(googletest-json-output-unittest --no_stacktrace_support)
Austin Schuh0cbef622015-09-06 17:34:52 -0700323endif()