You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

323 lines
12 KiB

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