blob: 9ee79408c277c4a95dbd3cc4ab6f7ecc37b9aadb [file] [log] [blame]
Austin Schuh0cbef622015-09-06 17:34:52 -07001########################################################################
2# CMake build script for Google Test.
3#
4# To run the tests for Google Test itself on Linux, use 'make test' or
5# ctest. You can select which tests to run using 'ctest -R regex'.
6# For more options, run 'ctest --help'.
7
Austin Schuh0cbef622015-09-06 17:34:52 -07008# When other libraries are using a shared version of runtime libraries,
9# Google Test also has to use one.
10option(
11 gtest_force_shared_crt
12 "Use shared (DLL) run-time lib even when Google Test is built as static lib."
13 OFF)
14
15option(gtest_build_tests "Build all of gtest's own tests." OFF)
16
17option(gtest_build_samples "Build gtest's sample programs." OFF)
18
19option(gtest_disable_pthreads "Disable uses of pthreads in gtest." OFF)
20
Austin Schuh889ac432018-10-29 22:57:02 -070021option(
22 gtest_hide_internal_symbols
23 "Build gtest with internal symbols hidden in shared libraries."
24 OFF)
25
Austin Schuh0cbef622015-09-06 17:34:52 -070026# Defines pre_project_set_up_hermetic_build() and set_up_hermetic_build().
27include(cmake/hermetic_build.cmake OPTIONAL)
28
29if (COMMAND pre_project_set_up_hermetic_build)
30 pre_project_set_up_hermetic_build()
31endif()
32
33########################################################################
34#
35# Project-wide settings
36
37# Name of the project.
38#
39# CMake files in this project can refer to the root source directory
40# as ${gtest_SOURCE_DIR} and to the root binary directory as
41# ${gtest_BINARY_DIR}.
42# Language "C" is required for find_package(Threads).
Austin Schuh889ac432018-10-29 22:57:02 -070043if (CMAKE_VERSION VERSION_LESS 3.0)
44 project(gtest CXX C)
45else()
46 cmake_policy(SET CMP0048 NEW)
47 project(gtest VERSION ${GOOGLETEST_VERSION} LANGUAGES CXX C)
48endif()
49cmake_minimum_required(VERSION 2.6.4)
50
51if (POLICY CMP0063) # Visibility
52 cmake_policy(SET CMP0063 NEW)
53endif (POLICY CMP0063)
Austin Schuh0cbef622015-09-06 17:34:52 -070054
55if (COMMAND set_up_hermetic_build)
56 set_up_hermetic_build()
57endif()
58
Austin Schuh889ac432018-10-29 22:57:02 -070059# These commands only run if this is the main project
60if(CMAKE_PROJECT_NAME STREQUAL "gtest" OR CMAKE_PROJECT_NAME STREQUAL "googletest-distribution")
61
62 # BUILD_SHARED_LIBS is a standard CMake variable, but we declare it here to
63 # make it prominent in the GUI.
64 option(BUILD_SHARED_LIBS "Build shared libraries (DLLs)." OFF)
65
66else()
67
68 mark_as_advanced(
69 gtest_force_shared_crt
70 gtest_build_tests
71 gtest_build_samples
72 gtest_disable_pthreads
73 gtest_hide_internal_symbols)
74
75endif()
76
77
78if (gtest_hide_internal_symbols)
79 set(CMAKE_CXX_VISIBILITY_PRESET hidden)
80 set(CMAKE_VISIBILITY_INLINES_HIDDEN 1)
81endif()
82
Austin Schuh0cbef622015-09-06 17:34:52 -070083# Define helper functions and macros used by Google Test.
84include(cmake/internal_utils.cmake)
85
86config_compiler_and_linker() # Defined in internal_utils.cmake.
87
Austin Schuh889ac432018-10-29 22:57:02 -070088# Create the CMake package file descriptors.
89if (INSTALL_GTEST)
90 include(CMakePackageConfigHelpers)
91 set(cmake_package_name GTest)
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} COMPATIBILITY AnyNewerVersion)
97 install(EXPORT ${targets_export_name}
98 NAMESPACE ${cmake_package_name}::
99 DESTINATION ${cmake_files_install_dir})
100 set(config_file "${generated_dir}/${cmake_package_name}Config.cmake")
101 configure_package_config_file("${gtest_SOURCE_DIR}/cmake/Config.cmake.in"
102 "${config_file}" INSTALL_DESTINATION ${cmake_files_install_dir})
103 install(FILES ${version_file} ${config_file}
104 DESTINATION ${cmake_files_install_dir})
105endif()
Austin Schuh0cbef622015-09-06 17:34:52 -0700106
Austin Schuh889ac432018-10-29 22:57:02 -0700107# Where Google Test's .h files can be found.
108set(gtest_build_include_dirs
109 "${gtest_SOURCE_DIR}/include"
110 "${gtest_SOURCE_DIR}")
111include_directories(${gtest_build_include_dirs})
Austin Schuh0cbef622015-09-06 17:34:52 -0700112
113# Summary of tuple support for Microsoft Visual Studio:
114# Compiler version(MS) version(cmake) Support
115# ---------- ----------- -------------- -----------------------------
116# <= VS 2010 <= 10 <= 1600 Use Google Tests's own tuple.
117# VS 2012 11 1700 std::tr1::tuple + _VARIADIC_MAX=10
118# VS 2013 12 1800 std::tr1::tuple
Austin Schuh889ac432018-10-29 22:57:02 -0700119# VS 2015 14 1900 std::tuple
120# VS 2017 15 >= 1910 std::tuple
Austin Schuh0cbef622015-09-06 17:34:52 -0700121if (MSVC AND MSVC_VERSION EQUAL 1700)
122 add_definitions(/D _VARIADIC_MAX=10)
123endif()
124
125########################################################################
126#
127# Defines the gtest & gtest_main libraries. User tests should link
128# with one of them.
129
130# Google Test libraries. We build them using more strict warnings than what
131# are used for other targets, to ensure that gtest can be compiled by a user
132# aggressive about warnings.
133cxx_library(gtest "${cxx_strict}" src/gtest-all.cc)
134cxx_library(gtest_main "${cxx_strict}" src/gtest_main.cc)
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)
196 cxx_test(googletest-linked-ptr-test gtest_main)
197 cxx_test(googletest-listener-test gtest_main)
Austin Schuh0cbef622015-09-06 17:34:52 -0700198 cxx_test(gtest_main_unittest gtest_main)
Austin Schuh889ac432018-10-29 22:57:02 -0700199 cxx_test(googletest-message-test gtest_main)
Austin Schuh0cbef622015-09-06 17:34:52 -0700200 cxx_test(gtest_no_test_unittest gtest)
Austin Schuh889ac432018-10-29 22:57:02 -0700201 cxx_test(googletest-options-test gtest_main)
202 cxx_test(googletest-param-test-test gtest
203 test/googletest-param-test2-test.cc)
204 cxx_test(googletest-port-test gtest_main)
Austin Schuh0cbef622015-09-06 17:34:52 -0700205 cxx_test(gtest_pred_impl_unittest gtest_main)
206 cxx_test(gtest_premature_exit_test gtest
207 test/gtest_premature_exit_test.cc)
Austin Schuh889ac432018-10-29 22:57:02 -0700208 cxx_test(googletest-printers-test gtest_main)
Austin Schuh0cbef622015-09-06 17:34:52 -0700209 cxx_test(gtest_prod_test gtest_main
210 test/production.cc)
211 cxx_test(gtest_repeat_test gtest)
212 cxx_test(gtest_sole_header_test gtest_main)
213 cxx_test(gtest_stress_test gtest)
Austin Schuh889ac432018-10-29 22:57:02 -0700214 cxx_test(googletest-test-part-test gtest_main)
Austin Schuh0cbef622015-09-06 17:34:52 -0700215 cxx_test(gtest_throw_on_failure_ex_test gtest)
216 cxx_test(gtest-typed-test_test gtest_main
217 test/gtest-typed-test2_test.cc)
218 cxx_test(gtest_unittest gtest_main)
219 cxx_test(gtest-unittest-api_test gtest)
220
221 ############################################################
222 # C++ tests built with non-standard compiler flags.
223
224 # MSVC 7.1 does not support STL with exceptions disabled.
225 if (NOT MSVC OR MSVC_VERSION GREATER 1310)
226 cxx_library(gtest_no_exception "${cxx_no_exception}"
227 src/gtest-all.cc)
228 cxx_library(gtest_main_no_exception "${cxx_no_exception}"
229 src/gtest-all.cc src/gtest_main.cc)
230 endif()
231 cxx_library(gtest_main_no_rtti "${cxx_no_rtti}"
232 src/gtest-all.cc src/gtest_main.cc)
233
234 cxx_test_with_flags(gtest-death-test_ex_nocatch_test
235 "${cxx_exception} -DGTEST_ENABLE_CATCH_EXCEPTIONS_=0"
Austin Schuh889ac432018-10-29 22:57:02 -0700236 gtest test/googletest-death-test_ex_test.cc)
Austin Schuh0cbef622015-09-06 17:34:52 -0700237 cxx_test_with_flags(gtest-death-test_ex_catch_test
238 "${cxx_exception} -DGTEST_ENABLE_CATCH_EXCEPTIONS_=1"
Austin Schuh889ac432018-10-29 22:57:02 -0700239 gtest test/googletest-death-test_ex_test.cc)
Austin Schuh0cbef622015-09-06 17:34:52 -0700240
241 cxx_test_with_flags(gtest_no_rtti_unittest "${cxx_no_rtti}"
242 gtest_main_no_rtti test/gtest_unittest.cc)
243
244 cxx_shared_library(gtest_dll "${cxx_default}"
245 src/gtest-all.cc src/gtest_main.cc)
246
247 cxx_executable_with_flags(gtest_dll_test_ "${cxx_default}"
248 gtest_dll test/gtest_all_test.cc)
249 set_target_properties(gtest_dll_test_
250 PROPERTIES
251 COMPILE_DEFINITIONS "GTEST_LINKED_AS_SHARED_LIBRARY=1")
252
253 if (NOT MSVC OR MSVC_VERSION LESS 1600) # 1600 is Visual Studio 2010.
254 # Visual Studio 2010, 2012, and 2013 define symbols in std::tr1 that
255 # conflict with our own definitions. Therefore using our own tuple does not
256 # work on those compilers.
257 cxx_library(gtest_main_use_own_tuple "${cxx_use_own_tuple}"
258 src/gtest-all.cc src/gtest_main.cc)
259
Austin Schuh889ac432018-10-29 22:57:02 -0700260 cxx_test_with_flags(googletest-tuple-test "${cxx_use_own_tuple}"
261 gtest_main_use_own_tuple test/googletest-tuple-test.cc)
Austin Schuh0cbef622015-09-06 17:34:52 -0700262
263 cxx_test_with_flags(gtest_use_own_tuple_test "${cxx_use_own_tuple}"
264 gtest_main_use_own_tuple
Austin Schuh889ac432018-10-29 22:57:02 -0700265 test/googletest-param-test-test.cc test/googletest-param-test2-test.cc)
Austin Schuh0cbef622015-09-06 17:34:52 -0700266 endif()
267
268 ############################################################
269 # Python tests.
270
Austin Schuh889ac432018-10-29 22:57:02 -0700271 cxx_executable(googletest-break-on-failure-unittest_ test gtest)
272 py_test(googletest-break-on-failure-unittest)
Austin Schuh0cbef622015-09-06 17:34:52 -0700273
274 # Visual Studio .NET 2003 does not support STL with exceptions disabled.
275 if (NOT MSVC OR MSVC_VERSION GREATER 1310) # 1310 is Visual Studio .NET 2003
276 cxx_executable_with_flags(
Austin Schuh889ac432018-10-29 22:57:02 -0700277 googletest-catch-exceptions-no-ex-test_
Austin Schuh0cbef622015-09-06 17:34:52 -0700278 "${cxx_no_exception}"
279 gtest_main_no_exception
Austin Schuh889ac432018-10-29 22:57:02 -0700280 test/googletest-catch-exceptions-test_.cc)
Austin Schuh0cbef622015-09-06 17:34:52 -0700281 endif()
282
283 cxx_executable_with_flags(
Austin Schuh889ac432018-10-29 22:57:02 -0700284 googletest-catch-exceptions-ex-test_
Austin Schuh0cbef622015-09-06 17:34:52 -0700285 "${cxx_exception}"
286 gtest_main
Austin Schuh889ac432018-10-29 22:57:02 -0700287 test/googletest-catch-exceptions-test_.cc)
288 py_test(googletest-catch-exceptions-test)
Austin Schuh0cbef622015-09-06 17:34:52 -0700289
Austin Schuh889ac432018-10-29 22:57:02 -0700290 cxx_executable(googletest-color-test_ test gtest)
291 py_test(googletest-color-test)
Austin Schuh0cbef622015-09-06 17:34:52 -0700292
Austin Schuh889ac432018-10-29 22:57:02 -0700293 cxx_executable(googletest-env-var-test_ test gtest)
294 py_test(googletest-env-var-test)
Austin Schuh0cbef622015-09-06 17:34:52 -0700295
Austin Schuh889ac432018-10-29 22:57:02 -0700296 cxx_executable(googletest-filter-unittest_ test gtest)
297 py_test(googletest-filter-unittest)
Austin Schuh0cbef622015-09-06 17:34:52 -0700298
299 cxx_executable(gtest_help_test_ test gtest_main)
300 py_test(gtest_help_test)
301
Austin Schuh889ac432018-10-29 22:57:02 -0700302 cxx_executable(googletest-list-tests-unittest_ test gtest)
303 py_test(googletest-list-tests-unittest)
Austin Schuh0cbef622015-09-06 17:34:52 -0700304
Austin Schuh889ac432018-10-29 22:57:02 -0700305 cxx_executable(googletest-output-test_ test gtest)
306 py_test(googletest-output-test --no_stacktrace_support)
Austin Schuh0cbef622015-09-06 17:34:52 -0700307
Austin Schuh889ac432018-10-29 22:57:02 -0700308 cxx_executable(googletest-shuffle-test_ test gtest)
309 py_test(googletest-shuffle-test)
Austin Schuh0cbef622015-09-06 17:34:52 -0700310
311 # MSVC 7.1 does not support STL with exceptions disabled.
312 if (NOT MSVC OR MSVC_VERSION GREATER 1310)
Austin Schuh889ac432018-10-29 22:57:02 -0700313 cxx_executable(googletest-throw-on-failure-test_ test gtest_no_exception)
314 set_target_properties(googletest-throw-on-failure-test_
Austin Schuh0cbef622015-09-06 17:34:52 -0700315 PROPERTIES
316 COMPILE_FLAGS "${cxx_no_exception}")
Austin Schuh889ac432018-10-29 22:57:02 -0700317 py_test(googletest-throw-on-failure-test)
Austin Schuh0cbef622015-09-06 17:34:52 -0700318 endif()
319
Austin Schuh889ac432018-10-29 22:57:02 -0700320 cxx_executable(googletest-uninitialized-test_ test gtest)
321 py_test(googletest-uninitialized-test)
Austin Schuh0cbef622015-09-06 17:34:52 -0700322
323 cxx_executable(gtest_xml_outfile1_test_ test gtest_main)
324 cxx_executable(gtest_xml_outfile2_test_ test gtest_main)
325 py_test(gtest_xml_outfiles_test)
Austin Schuh889ac432018-10-29 22:57:02 -0700326 py_test(googletest-json-outfiles-test)
Austin Schuh0cbef622015-09-06 17:34:52 -0700327
328 cxx_executable(gtest_xml_output_unittest_ test gtest)
Austin Schuh889ac432018-10-29 22:57:02 -0700329 py_test(gtest_xml_output_unittest --no_stacktrace_support)
330 py_test(googletest-json-output-unittest --no_stacktrace_support)
Austin Schuh0cbef622015-09-06 17:34:52 -0700331endif()