CMakeLists.txt 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. ########################################################################
  2. # Note: CMake support is community-based. The maintainers do not use CMake
  3. # internally.
  4. #
  5. # CMake build script for Google Mock.
  6. #
  7. # To run the tests for Google Mock 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. option(gmock_build_tests "Build all of Google Mock's own tests." OFF)
  11. # A directory to find Google Test sources.
  12. if (EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/gtest/CMakeLists.txt")
  13. set(gtest_dir gtest)
  14. else()
  15. set(gtest_dir ../googletest)
  16. endif()
  17. # Defines pre_project_set_up_hermetic_build() and set_up_hermetic_build().
  18. include("${gtest_dir}/cmake/hermetic_build.cmake" OPTIONAL)
  19. if (COMMAND pre_project_set_up_hermetic_build)
  20. # Google Test also calls hermetic setup functions from add_subdirectory,
  21. # although its changes will not affect things at the current scope.
  22. pre_project_set_up_hermetic_build()
  23. endif()
  24. ########################################################################
  25. #
  26. # Project-wide settings
  27. # Name of the project.
  28. #
  29. # CMake files in this project can refer to the root source directory
  30. # as ${gmock_SOURCE_DIR} and to the root binary directory as
  31. # ${gmock_BINARY_DIR}.
  32. # Language "C" is required for find_package(Threads).
  33. cmake_minimum_required(VERSION 3.13)
  34. project(gmock VERSION ${GOOGLETEST_VERSION} LANGUAGES CXX C)
  35. if (COMMAND set_up_hermetic_build)
  36. set_up_hermetic_build()
  37. endif()
  38. # Instructs CMake to process Google Test's CMakeLists.txt and add its
  39. # targets to the current scope. We are placing Google Test's binary
  40. # directory in a subdirectory of our own as VC compilation may break
  41. # if they are the same (the default).
  42. add_subdirectory("${gtest_dir}" "${gmock_BINARY_DIR}/${gtest_dir}")
  43. # These commands only run if this is the main project
  44. if(CMAKE_PROJECT_NAME STREQUAL "gmock" OR CMAKE_PROJECT_NAME STREQUAL "googletest-distribution")
  45. # BUILD_SHARED_LIBS is a standard CMake variable, but we declare it here to
  46. # make it prominent in the GUI.
  47. option(BUILD_SHARED_LIBS "Build shared libraries (DLLs)." OFF)
  48. else()
  49. mark_as_advanced(gmock_build_tests)
  50. endif()
  51. # Although Google Test's CMakeLists.txt calls this function, the
  52. # changes there don't affect the current scope. Therefore we have to
  53. # call it again here.
  54. config_compiler_and_linker() # from ${gtest_dir}/cmake/internal_utils.cmake
  55. # Adds Google Mock's and Google Test's header directories to the search path.
  56. # Get Google Test's include dirs from the target, gtest_SOURCE_DIR is broken
  57. # when using fetch-content with the name "GTest".
  58. get_target_property(gtest_include_dirs gtest INCLUDE_DIRECTORIES)
  59. set(gmock_build_include_dirs
  60. "${gmock_SOURCE_DIR}/include"
  61. "${gmock_SOURCE_DIR}"
  62. "${gtest_include_dirs}")
  63. include_directories(${gmock_build_include_dirs})
  64. ########################################################################
  65. #
  66. # Defines the gmock & gmock_main libraries. User tests should link
  67. # with one of them.
  68. # Google Mock libraries. We build them using more strict warnings than what
  69. # are used for other targets, to ensure that Google Mock can be compiled by
  70. # a user aggressive about warnings.
  71. if (MSVC)
  72. cxx_library(gmock
  73. "${cxx_strict}"
  74. "${gtest_dir}/src/gtest-all.cc"
  75. src/gmock-all.cc)
  76. cxx_library(gmock_main
  77. "${cxx_strict}"
  78. "${gtest_dir}/src/gtest-all.cc"
  79. src/gmock-all.cc
  80. src/gmock_main.cc)
  81. else()
  82. cxx_library(gmock "${cxx_strict}" src/gmock-all.cc)
  83. target_link_libraries(gmock PUBLIC gtest)
  84. set_target_properties(gmock PROPERTIES VERSION ${GOOGLETEST_VERSION})
  85. cxx_library(gmock_main "${cxx_strict}" src/gmock_main.cc)
  86. target_link_libraries(gmock_main PUBLIC gmock)
  87. set_target_properties(gmock_main PROPERTIES VERSION ${GOOGLETEST_VERSION})
  88. endif()
  89. string(REPLACE ";" "$<SEMICOLON>" dirs "${gmock_build_include_dirs}")
  90. target_include_directories(gmock SYSTEM INTERFACE
  91. "$<BUILD_INTERFACE:${dirs}>"
  92. "$<INSTALL_INTERFACE:$<INSTALL_PREFIX>/${CMAKE_INSTALL_INCLUDEDIR}>")
  93. target_include_directories(gmock_main SYSTEM INTERFACE
  94. "$<BUILD_INTERFACE:${dirs}>"
  95. "$<INSTALL_INTERFACE:$<INSTALL_PREFIX>/${CMAKE_INSTALL_INCLUDEDIR}>")
  96. ########################################################################
  97. #
  98. # Install rules.
  99. install_project(gmock gmock_main)
  100. ########################################################################
  101. #
  102. # Google Mock's own tests.
  103. #
  104. # You can skip this section if you aren't interested in testing
  105. # Google Mock itself.
  106. #
  107. # The tests are not built by default. To build them, set the
  108. # gmock_build_tests option to ON. You can do it by running ccmake
  109. # or specifying the -Dgmock_build_tests=ON flag when running cmake.
  110. if (gmock_build_tests)
  111. # This must be set in the root directory for the tests to be run by
  112. # 'make test' or ctest.
  113. enable_testing()
  114. if (MINGW OR CYGWIN)
  115. add_compile_options("-Wa,-mbig-obj")
  116. endif()
  117. ############################################################
  118. # C++ tests built with standard compiler flags.
  119. cxx_test(gmock-actions_test gmock_main)
  120. cxx_test(gmock-cardinalities_test gmock_main)
  121. cxx_test(gmock_ex_test gmock_main)
  122. cxx_test(gmock-function-mocker_test gmock_main)
  123. cxx_test(gmock-internal-utils_test gmock_main)
  124. cxx_test(gmock-matchers-arithmetic_test gmock_main)
  125. cxx_test(gmock-matchers-comparisons_test gmock_main)
  126. cxx_test(gmock-matchers-containers_test gmock_main)
  127. cxx_test(gmock-matchers-misc_test gmock_main)
  128. cxx_test(gmock-more-actions_test gmock_main)
  129. cxx_test(gmock-nice-strict_test gmock_main)
  130. cxx_test(gmock-port_test gmock_main)
  131. cxx_test(gmock-spec-builders_test gmock_main)
  132. cxx_test(gmock_link_test gmock_main test/gmock_link2_test.cc)
  133. cxx_test(gmock_test gmock_main)
  134. if (DEFINED GTEST_HAS_PTHREAD)
  135. cxx_test(gmock_stress_test gmock)
  136. endif()
  137. # gmock_all_test is commented to save time building and running tests.
  138. # Uncomment if necessary.
  139. # cxx_test(gmock_all_test gmock_main)
  140. ############################################################
  141. # C++ tests built with non-standard compiler flags.
  142. if (MSVC)
  143. cxx_library(gmock_main_no_exception "${cxx_no_exception}"
  144. "${gtest_dir}/src/gtest-all.cc" src/gmock-all.cc src/gmock_main.cc)
  145. cxx_library(gmock_main_no_rtti "${cxx_no_rtti}"
  146. "${gtest_dir}/src/gtest-all.cc" src/gmock-all.cc src/gmock_main.cc)
  147. else()
  148. cxx_library(gmock_main_no_exception "${cxx_no_exception}" src/gmock_main.cc)
  149. target_link_libraries(gmock_main_no_exception PUBLIC gmock)
  150. cxx_library(gmock_main_no_rtti "${cxx_no_rtti}" src/gmock_main.cc)
  151. target_link_libraries(gmock_main_no_rtti PUBLIC gmock)
  152. endif()
  153. cxx_test_with_flags(gmock-more-actions_no_exception_test "${cxx_no_exception}"
  154. gmock_main_no_exception test/gmock-more-actions_test.cc)
  155. cxx_test_with_flags(gmock_no_rtti_test "${cxx_no_rtti}"
  156. gmock_main_no_rtti test/gmock-spec-builders_test.cc)
  157. cxx_shared_library(shared_gmock_main "${cxx_default}"
  158. "${gtest_dir}/src/gtest-all.cc" src/gmock-all.cc src/gmock_main.cc)
  159. # Tests that a binary can be built with Google Mock as a shared library. On
  160. # some system configurations, it may not possible to run the binary without
  161. # knowing more details about the system configurations. We do not try to run
  162. # this binary. To get a more robust shared library coverage, configure with
  163. # -DBUILD_SHARED_LIBS=ON.
  164. cxx_executable_with_flags(shared_gmock_test_ "${cxx_default}"
  165. shared_gmock_main test/gmock-spec-builders_test.cc)
  166. set_target_properties(shared_gmock_test_
  167. PROPERTIES
  168. COMPILE_DEFINITIONS "GTEST_LINKED_AS_SHARED_LIBRARY=1")
  169. ############################################################
  170. # Python tests.
  171. cxx_executable(gmock_leak_test_ test gmock_main)
  172. py_test(gmock_leak_test)
  173. cxx_executable(gmock_output_test_ test gmock)
  174. py_test(gmock_output_test)
  175. endif()