CMakeLists.txt 12 KB

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