|
| 1 | +######################################################################## |
| 2 | +# Note: CMake support is community-based. The maintainers do not use CMake |
| 3 | +# internally. |
| 4 | +# |
| 5 | +# 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 | + |
| 11 | +# When other libraries are using a shared version of runtime libraries, |
| 12 | +# Google Test also has to use one. |
| 13 | +option( |
| 14 | + gtest_force_shared_crt |
| 15 | + "Use shared (DLL) run-time lib even when Google Test is built as static lib." |
| 16 | + OFF) |
| 17 | + |
| 18 | +option(gtest_build_tests "Build all of gtest's own tests." OFF) |
| 19 | + |
| 20 | +option(gtest_build_samples "Build gtest's sample programs." OFF) |
| 21 | + |
| 22 | +option(gtest_disable_pthreads "Disable uses of pthreads in gtest." OFF) |
| 23 | + |
| 24 | +option( |
| 25 | + gtest_hide_internal_symbols |
| 26 | + "Build gtest with internal symbols hidden in shared libraries." |
| 27 | + OFF) |
| 28 | + |
| 29 | +# Defines pre_project_set_up_hermetic_build() and set_up_hermetic_build(). |
| 30 | +include(cmake/hermetic_build.cmake OPTIONAL) |
| 31 | + |
| 32 | +if (COMMAND pre_project_set_up_hermetic_build) |
| 33 | + pre_project_set_up_hermetic_build() |
| 34 | +endif() |
| 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). |
| 46 | + |
| 47 | +# Project version: |
| 48 | + |
| 49 | +set(GOOGLETEST_VERSION "1.10.0") |
| 50 | +if (CMAKE_VERSION VERSION_LESS 3.0) |
| 51 | + project(gtest CXX C) |
| 52 | + set(PROJECT_VERSION ${GOOGLETEST_VERSION}) |
| 53 | +else() |
| 54 | + cmake_policy(SET CMP0048 NEW) |
| 55 | + project(gtest VERSION ${GOOGLETEST_VERSION} LANGUAGES CXX C) |
| 56 | +endif() |
| 57 | +cmake_minimum_required(VERSION 2.6.4) |
| 58 | + |
| 59 | +if (POLICY CMP0063) # Visibility |
| 60 | + cmake_policy(SET CMP0063 NEW) |
| 61 | +endif (POLICY CMP0063) |
| 62 | + |
| 63 | +if (COMMAND set_up_hermetic_build) |
| 64 | + set_up_hermetic_build() |
| 65 | +endif() |
| 66 | + |
| 67 | +# These commands only run if this is the main project |
| 68 | +if(CMAKE_PROJECT_NAME STREQUAL "gtest" OR CMAKE_PROJECT_NAME STREQUAL "googletest-distribution") |
| 69 | + |
| 70 | + # BUILD_SHARED_LIBS is a standard CMake variable, but we declare it here to |
| 71 | + # make it prominent in the GUI. |
| 72 | + option(BUILD_SHARED_LIBS "Build shared libraries (DLLs)." OFF) |
| 73 | + |
| 74 | +else() |
| 75 | + |
| 76 | + mark_as_advanced( |
| 77 | + gtest_force_shared_crt |
| 78 | + gtest_build_tests |
| 79 | + gtest_build_samples |
| 80 | + gtest_disable_pthreads |
| 81 | + gtest_hide_internal_symbols) |
| 82 | + |
| 83 | +endif() |
| 84 | + |
| 85 | + |
| 86 | +if (gtest_hide_internal_symbols) |
| 87 | + set(CMAKE_CXX_VISIBILITY_PRESET hidden) |
| 88 | + set(CMAKE_VISIBILITY_INLINES_HIDDEN 1) |
| 89 | +endif() |
| 90 | + |
| 91 | +# Define helper functions and macros used by Google Test. |
| 92 | +include(cmake/internal_utils.cmake) |
| 93 | + |
| 94 | +config_compiler_and_linker() # Defined in internal_utils.cmake. |
| 95 | + |
| 96 | +# Create the CMake package file descriptors. |
| 97 | +if (INSTALL_GTEST) |
| 98 | + include(CMakePackageConfigHelpers) |
| 99 | + set(cmake_package_name GTest) |
| 100 | + set(targets_export_name ${cmake_package_name}Targets CACHE INTERNAL "") |
| 101 | + set(generated_dir "${CMAKE_CURRENT_BINARY_DIR}/generated" CACHE INTERNAL "") |
| 102 | + set(cmake_files_install_dir "${CMAKE_INSTALL_LIBDIR}/cmake/${cmake_package_name}") |
| 103 | + set(version_file "${generated_dir}/${cmake_package_name}ConfigVersion.cmake") |
| 104 | + write_basic_package_version_file(${version_file} VERSION ${GOOGLETEST_VERSION} COMPATIBILITY AnyNewerVersion) |
| 105 | + install(EXPORT ${targets_export_name} |
| 106 | + NAMESPACE ${cmake_package_name}:: |
| 107 | + DESTINATION ${cmake_files_install_dir}) |
| 108 | + set(config_file "${generated_dir}/${cmake_package_name}Config.cmake") |
| 109 | + configure_package_config_file("${gtest_SOURCE_DIR}/cmake/Config.cmake.in" |
| 110 | + "${config_file}" INSTALL_DESTINATION ${cmake_files_install_dir}) |
| 111 | + install(FILES ${version_file} ${config_file} |
| 112 | + DESTINATION ${cmake_files_install_dir}) |
| 113 | +endif() |
| 114 | + |
| 115 | +# Where Google Test's .h files can be found. |
| 116 | +set(gtest_build_include_dirs |
| 117 | + "${gtest_SOURCE_DIR}/include" |
| 118 | + "${gtest_SOURCE_DIR}") |
| 119 | +include_directories(${gtest_build_include_dirs}) |
| 120 | + |
| 121 | +######################################################################## |
| 122 | +# |
| 123 | +# Defines the gtest & gtest_main libraries. User tests should link |
| 124 | +# with one of them. |
| 125 | + |
| 126 | +# Google Test libraries. We build them using more strict warnings than what |
| 127 | +# are used for other targets, to ensure that gtest can be compiled by a user |
| 128 | +# aggressive about warnings. |
| 129 | +cxx_library(gtest "${cxx_strict}" src/gtest-all.cc) |
| 130 | +cxx_library(gtest_main "${cxx_strict}" src/gtest_main.cc) |
| 131 | +# If the CMake version supports it, attach header directory information |
| 132 | +# to the targets for when we are part of a parent build (ie being pulled |
| 133 | +# in via add_subdirectory() rather than being a standalone build). |
| 134 | +if (DEFINED CMAKE_VERSION AND NOT "${CMAKE_VERSION}" VERSION_LESS "2.8.11") |
| 135 | + target_include_directories(gtest SYSTEM INTERFACE |
| 136 | + "$<BUILD_INTERFACE:${gtest_build_include_dirs}>" |
| 137 | + "$<INSTALL_INTERFACE:$<INSTALL_PREFIX>/${CMAKE_INSTALL_INCLUDEDIR}>") |
| 138 | + target_include_directories(gtest_main SYSTEM INTERFACE |
| 139 | + "$<BUILD_INTERFACE:${gtest_build_include_dirs}>" |
| 140 | + "$<INSTALL_INTERFACE:$<INSTALL_PREFIX>/${CMAKE_INSTALL_INCLUDEDIR}>") |
| 141 | +endif() |
| 142 | +target_link_libraries(gtest_main PUBLIC gtest) |
| 143 | + |
| 144 | +######################################################################## |
| 145 | +# |
| 146 | +# Install rules |
| 147 | +install_project(gtest gtest_main) |
| 148 | + |
| 149 | +######################################################################## |
| 150 | +# |
| 151 | +# Samples on how to link user tests with gtest or gtest_main. |
| 152 | +# |
| 153 | +# They are not built by default. To build them, set the |
| 154 | +# gtest_build_samples option to ON. You can do it by running ccmake |
| 155 | +# or specifying the -Dgtest_build_samples=ON flag when running cmake. |
| 156 | + |
| 157 | +if (gtest_build_samples) |
| 158 | + cxx_executable(sample1_unittest samples gtest_main samples/sample1.cc) |
| 159 | + cxx_executable(sample2_unittest samples gtest_main samples/sample2.cc) |
| 160 | + cxx_executable(sample3_unittest samples gtest_main) |
| 161 | + cxx_executable(sample4_unittest samples gtest_main samples/sample4.cc) |
| 162 | + cxx_executable(sample5_unittest samples gtest_main samples/sample1.cc) |
| 163 | + cxx_executable(sample6_unittest samples gtest_main) |
| 164 | + cxx_executable(sample7_unittest samples gtest_main) |
| 165 | + cxx_executable(sample8_unittest samples gtest_main) |
| 166 | + cxx_executable(sample9_unittest samples gtest) |
| 167 | + cxx_executable(sample10_unittest samples gtest) |
| 168 | +endif() |
| 169 | + |
| 170 | +######################################################################## |
| 171 | +# |
| 172 | +# Google Test's own tests. |
| 173 | +# |
| 174 | +# You can skip this section if you aren't interested in testing |
| 175 | +# Google Test itself. |
| 176 | +# |
| 177 | +# The tests are not built by default. To build them, set the |
| 178 | +# gtest_build_tests option to ON. You can do it by running ccmake |
| 179 | +# or specifying the -Dgtest_build_tests=ON flag when running cmake. |
| 180 | + |
| 181 | +if (gtest_build_tests) |
| 182 | + # This must be set in the root directory for the tests to be run by |
| 183 | + # 'make test' or ctest. |
| 184 | + enable_testing() |
| 185 | + |
| 186 | + if (WIN32) |
| 187 | + file(GENERATE OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/$<CONFIG>/RunTest.ps1" |
| 188 | + CONTENT |
| 189 | +"$project_bin = \"${CMAKE_BINARY_DIR}/bin/$<CONFIG>\" |
| 190 | +$env:Path = \"$project_bin;$env:Path\" |
| 191 | +& $args") |
| 192 | + elseif (MINGW OR CYGWIN) |
| 193 | + file(GENERATE OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/RunTest.ps1" |
| 194 | + CONTENT |
| 195 | +"$project_bin = (cygpath --windows ${CMAKE_BINARY_DIR}/bin) |
| 196 | +$env:Path = \"$project_bin;$env:Path\" |
| 197 | +& $args") |
| 198 | + endif() |
| 199 | + |
| 200 | + ############################################################ |
| 201 | + # C++ tests built with standard compiler flags. |
| 202 | + |
| 203 | + cxx_test(googletest-death-test-test gtest_main) |
| 204 | + cxx_test(gtest_environment_test gtest) |
| 205 | + cxx_test(googletest-filepath-test gtest_main) |
| 206 | + cxx_test(googletest-listener-test gtest_main) |
| 207 | + cxx_test(gtest_main_unittest gtest_main) |
| 208 | + cxx_test(googletest-message-test gtest_main) |
| 209 | + cxx_test(gtest_no_test_unittest gtest) |
| 210 | + cxx_test(googletest-options-test gtest_main) |
| 211 | + cxx_test(googletest-param-test-test gtest |
| 212 | + test/googletest-param-test2-test.cc) |
| 213 | + cxx_test(googletest-port-test gtest_main) |
| 214 | + cxx_test(gtest_pred_impl_unittest gtest_main) |
| 215 | + cxx_test(gtest_premature_exit_test gtest |
| 216 | + test/gtest_premature_exit_test.cc) |
| 217 | + cxx_test(googletest-printers-test gtest_main) |
| 218 | + cxx_test(gtest_prod_test gtest_main |
| 219 | + test/production.cc) |
| 220 | + cxx_test(gtest_repeat_test gtest) |
| 221 | + cxx_test(gtest_sole_header_test gtest_main) |
| 222 | + cxx_test(gtest_stress_test gtest) |
| 223 | + cxx_test(googletest-test-part-test gtest_main) |
| 224 | + cxx_test(gtest_throw_on_failure_ex_test gtest) |
| 225 | + cxx_test(gtest-typed-test_test gtest_main |
| 226 | + test/gtest-typed-test2_test.cc) |
| 227 | + cxx_test(gtest_unittest gtest_main) |
| 228 | + cxx_test(gtest-unittest-api_test gtest) |
| 229 | + cxx_test(gtest_skip_in_environment_setup_test gtest_main) |
| 230 | + cxx_test(gtest_skip_test gtest_main) |
| 231 | + |
| 232 | + ############################################################ |
| 233 | + # C++ tests built with non-standard compiler flags. |
| 234 | + |
| 235 | + # MSVC 7.1 does not support STL with exceptions disabled. |
| 236 | + if (NOT MSVC OR MSVC_VERSION GREATER 1310) |
| 237 | + cxx_library(gtest_no_exception "${cxx_no_exception}" |
| 238 | + src/gtest-all.cc) |
| 239 | + cxx_library(gtest_main_no_exception "${cxx_no_exception}" |
| 240 | + src/gtest-all.cc src/gtest_main.cc) |
| 241 | + endif() |
| 242 | + cxx_library(gtest_main_no_rtti "${cxx_no_rtti}" |
| 243 | + src/gtest-all.cc src/gtest_main.cc) |
| 244 | + |
| 245 | + cxx_test_with_flags(gtest-death-test_ex_nocatch_test |
| 246 | + "${cxx_exception} -DGTEST_ENABLE_CATCH_EXCEPTIONS_=0" |
| 247 | + gtest test/googletest-death-test_ex_test.cc) |
| 248 | + cxx_test_with_flags(gtest-death-test_ex_catch_test |
| 249 | + "${cxx_exception} -DGTEST_ENABLE_CATCH_EXCEPTIONS_=1" |
| 250 | + gtest test/googletest-death-test_ex_test.cc) |
| 251 | + |
| 252 | + cxx_test_with_flags(gtest_no_rtti_unittest "${cxx_no_rtti}" |
| 253 | + gtest_main_no_rtti test/gtest_unittest.cc) |
| 254 | + |
| 255 | + cxx_shared_library(gtest_dll "${cxx_default}" |
| 256 | + src/gtest-all.cc src/gtest_main.cc) |
| 257 | + |
| 258 | + cxx_executable_with_flags(gtest_dll_test_ "${cxx_default}" |
| 259 | + gtest_dll test/gtest_all_test.cc) |
| 260 | + set_target_properties(gtest_dll_test_ |
| 261 | + PROPERTIES |
| 262 | + COMPILE_DEFINITIONS "GTEST_LINKED_AS_SHARED_LIBRARY=1") |
| 263 | + |
| 264 | + ############################################################ |
| 265 | + # Python tests. |
| 266 | + |
| 267 | + cxx_executable(googletest-break-on-failure-unittest_ test gtest) |
| 268 | + py_test(googletest-break-on-failure-unittest) |
| 269 | + |
| 270 | + py_test(gtest_skip_environment_check_output_test) |
| 271 | + |
| 272 | + # Visual Studio .NET 2003 does not support STL with exceptions disabled. |
| 273 | + if (NOT MSVC OR MSVC_VERSION GREATER 1310) # 1310 is Visual Studio .NET 2003 |
| 274 | + cxx_executable_with_flags( |
| 275 | + googletest-catch-exceptions-no-ex-test_ |
| 276 | + "${cxx_no_exception}" |
| 277 | + gtest_main_no_exception |
| 278 | + test/googletest-catch-exceptions-test_.cc) |
| 279 | + endif() |
| 280 | + |
| 281 | + cxx_executable_with_flags( |
| 282 | + googletest-catch-exceptions-ex-test_ |
| 283 | + "${cxx_exception}" |
| 284 | + gtest_main |
| 285 | + test/googletest-catch-exceptions-test_.cc) |
| 286 | + py_test(googletest-catch-exceptions-test) |
| 287 | + |
| 288 | + cxx_executable(googletest-color-test_ test gtest) |
| 289 | + py_test(googletest-color-test) |
| 290 | + |
| 291 | + cxx_executable(googletest-env-var-test_ test gtest) |
| 292 | + py_test(googletest-env-var-test) |
| 293 | + |
| 294 | + cxx_executable(googletest-filter-unittest_ test gtest) |
| 295 | + py_test(googletest-filter-unittest) |
| 296 | + |
| 297 | + cxx_executable(gtest_help_test_ test gtest_main) |
| 298 | + py_test(gtest_help_test) |
| 299 | + |
| 300 | + cxx_executable(googletest-list-tests-unittest_ test gtest) |
| 301 | + py_test(googletest-list-tests-unittest) |
| 302 | + |
| 303 | + cxx_executable(googletest-output-test_ test gtest) |
| 304 | + py_test(googletest-output-test --no_stacktrace_support) |
| 305 | + |
| 306 | + cxx_executable(googletest-shuffle-test_ test gtest) |
| 307 | + py_test(googletest-shuffle-test) |
| 308 | + |
| 309 | + # MSVC 7.1 does not support STL with exceptions disabled. |
| 310 | + if (NOT MSVC OR MSVC_VERSION GREATER 1310) |
| 311 | + cxx_executable(googletest-throw-on-failure-test_ test gtest_no_exception) |
| 312 | + set_target_properties(googletest-throw-on-failure-test_ |
| 313 | + PROPERTIES |
| 314 | + COMPILE_FLAGS "${cxx_no_exception}") |
| 315 | + py_test(googletest-throw-on-failure-test) |
| 316 | + endif() |
| 317 | + |
| 318 | + cxx_executable(googletest-uninitialized-test_ test gtest) |
| 319 | + py_test(googletest-uninitialized-test) |
| 320 | + |
| 321 | + cxx_executable(gtest_xml_outfile1_test_ test gtest_main) |
| 322 | + cxx_executable(gtest_xml_outfile2_test_ test gtest_main) |
| 323 | + py_test(gtest_xml_outfiles_test) |
| 324 | + py_test(googletest-json-outfiles-test) |
| 325 | + |
| 326 | + cxx_executable(gtest_xml_output_unittest_ test gtest) |
| 327 | + py_test(gtest_xml_output_unittest --no_stacktrace_support) |
| 328 | + py_test(googletest-json-output-unittest --no_stacktrace_support) |
| 329 | +endif() |
0 commit comments