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.

233 lines
8.5 KiB

  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. if (CMAKE_VERSION VERSION_LESS 3.0)
  34. project(gmock CXX C)
  35. else()
  36. cmake_policy(SET CMP0048 NEW)
  37. project(gmock VERSION ${GOOGLETEST_VERSION} LANGUAGES CXX C)
  38. endif()
  39. cmake_minimum_required(VERSION 2.6.4)
  40. if (COMMAND set_up_hermetic_build)
  41. set_up_hermetic_build()
  42. endif()
  43. # Instructs CMake to process Google Test's CMakeLists.txt and add its
  44. # targets to the current scope. We are placing Google Test's binary
  45. # directory in a subdirectory of our own as VC compilation may break
  46. # if they are the same (the default).
  47. add_subdirectory("${gtest_dir}" "${gmock_BINARY_DIR}/${gtest_dir}")
  48. # These commands only run if this is the main project
  49. if(CMAKE_PROJECT_NAME STREQUAL "gmock" OR CMAKE_PROJECT_NAME STREQUAL "googletest-distribution")
  50. # BUILD_SHARED_LIBS is a standard CMake variable, but we declare it here to
  51. # make it prominent in the GUI.
  52. option(BUILD_SHARED_LIBS "Build shared libraries (DLLs)." OFF)
  53. else()
  54. mark_as_advanced(gmock_build_tests)
  55. endif()
  56. # Although Google Test's CMakeLists.txt calls this function, the
  57. # changes there don't affect the current scope. Therefore we have to
  58. # call it again here.
  59. config_compiler_and_linker() # from ${gtest_dir}/cmake/internal_utils.cmake
  60. # Adds Google Mock's and Google Test's header directories to the search path.
  61. set(gmock_build_include_dirs
  62. "${gmock_SOURCE_DIR}/include"
  63. "${gmock_SOURCE_DIR}"
  64. "${gtest_SOURCE_DIR}/include"
  65. # This directory is needed to build directly from Google Test sources.
  66. "${gtest_SOURCE_DIR}")
  67. include_directories(${gmock_build_include_dirs})
  68. ########################################################################
  69. #
  70. # Defines the gmock & gmock_main libraries. User tests should link
  71. # with one of them.
  72. # Google Mock libraries. We build them using more strict warnings than what
  73. # are used for other targets, to ensure that Google Mock can be compiled by
  74. # a user aggressive about warnings.
  75. if (MSVC)
  76. cxx_library(gmock
  77. "${cxx_strict}"
  78. "${gtest_dir}/src/gtest-all.cc"
  79. src/gmock-all.cc)
  80. cxx_library(gmock_main
  81. "${cxx_strict}"
  82. "${gtest_dir}/src/gtest-all.cc"
  83. src/gmock-all.cc
  84. src/gmock_main.cc)
  85. else()
  86. cxx_library(gmock "${cxx_strict}" src/gmock-all.cc)
  87. target_link_libraries(gmock PUBLIC gtest)
  88. cxx_library(gmock_main "${cxx_strict}" src/gmock_main.cc)
  89. target_link_libraries(gmock_main PUBLIC gmock)
  90. endif()
  91. # If the CMake version supports it, attach header directory information
  92. # to the targets for when we are part of a parent build (ie being pulled
  93. # in via add_subdirectory() rather than being a standalone build).
  94. if (DEFINED CMAKE_VERSION AND NOT "${CMAKE_VERSION}" VERSION_LESS "2.8.11")
  95. target_include_directories(gmock SYSTEM INTERFACE
  96. "$<BUILD_INTERFACE:${gmock_build_include_dirs}>"
  97. "$<INSTALL_INTERFACE:$<INSTALL_PREFIX>/${CMAKE_INSTALL_INCLUDEDIR}>")
  98. target_include_directories(gmock_main SYSTEM INTERFACE
  99. "$<BUILD_INTERFACE:${gmock_build_include_dirs}>"
  100. "$<INSTALL_INTERFACE:$<INSTALL_PREFIX>/${CMAKE_INSTALL_INCLUDEDIR}>")
  101. endif()
  102. ########################################################################
  103. #
  104. # Install rules
  105. install_project(gmock gmock_main)
  106. ########################################################################
  107. #
  108. # Google Mock's own tests.
  109. #
  110. # You can skip this section if you aren't interested in testing
  111. # Google Mock itself.
  112. #
  113. # The tests are not built by default. To build them, set the
  114. # gmock_build_tests option to ON. You can do it by running ccmake
  115. # or specifying the -Dgmock_build_tests=ON flag when running cmake.
  116. if (gmock_build_tests)
  117. # This must be set in the root directory for the tests to be run by
  118. # 'make test' or ctest.
  119. enable_testing()
  120. if (WIN32)
  121. file(GENERATE OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/$<CONFIG>/RunTest.ps1"
  122. CONTENT
  123. "$project_bin = \"${CMAKE_BINARY_DIR}/bin/$<CONFIG>\"
  124. $env:Path = \"$project_bin;$env:Path\"
  125. & $args")
  126. elseif (MINGW OR CYGWIN)
  127. file(GENERATE OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/RunTest.ps1"
  128. CONTENT
  129. "$project_bin = (cygpath --windows ${CMAKE_BINARY_DIR}/bin)
  130. $env:Path = \"$project_bin;$env:Path\"
  131. & $args")
  132. endif()
  133. if (MINGW OR CYGWIN)
  134. if (CMAKE_VERSION VERSION_LESS "2.8.12")
  135. add_compile_options("-Wa,-mbig-obj")
  136. else()
  137. add_definitions("-Wa,-mbig-obj")
  138. endif()
  139. endif()
  140. ############################################################
  141. # C++ tests built with standard compiler flags.
  142. cxx_test(gmock-actions_test gmock_main)
  143. cxx_test(gmock-cardinalities_test gmock_main)
  144. cxx_test(gmock_ex_test gmock_main)
  145. cxx_test(gmock-function-mocker_test gmock_main)
  146. cxx_test(gmock-generated-actions_test gmock_main)
  147. cxx_test(gmock-generated-function-mockers_test gmock_main)
  148. cxx_test(gmock-generated-matchers_test gmock_main)
  149. cxx_test(gmock-internal-utils_test gmock_main)
  150. cxx_test(gmock-matchers_test gmock_main)
  151. cxx_test(gmock-more-actions_test gmock_main)
  152. cxx_test(gmock-nice-strict_test gmock_main)
  153. cxx_test(gmock-port_test gmock_main)
  154. cxx_test(gmock-spec-builders_test gmock_main)
  155. cxx_test(gmock_link_test gmock_main test/gmock_link2_test.cc)
  156. cxx_test(gmock_test gmock_main)
  157. if (DEFINED GTEST_HAS_PTHREAD)
  158. cxx_test(gmock_stress_test gmock)
  159. endif()
  160. # gmock_all_test is commented to save time building and running tests.
  161. # Uncomment if necessary.
  162. # cxx_test(gmock_all_test gmock_main)
  163. ############################################################
  164. # C++ tests built with non-standard compiler flags.
  165. if (MSVC)
  166. cxx_library(gmock_main_no_exception "${cxx_no_exception}"
  167. "${gtest_dir}/src/gtest-all.cc" src/gmock-all.cc src/gmock_main.cc)
  168. cxx_library(gmock_main_no_rtti "${cxx_no_rtti}"
  169. "${gtest_dir}/src/gtest-all.cc" src/gmock-all.cc src/gmock_main.cc)
  170. else()
  171. cxx_library(gmock_main_no_exception "${cxx_no_exception}" src/gmock_main.cc)
  172. target_link_libraries(gmock_main_no_exception PUBLIC gmock)
  173. cxx_library(gmock_main_no_rtti "${cxx_no_rtti}" src/gmock_main.cc)
  174. target_link_libraries(gmock_main_no_rtti PUBLIC gmock)
  175. endif()
  176. cxx_test_with_flags(gmock-more-actions_no_exception_test "${cxx_no_exception}"
  177. gmock_main_no_exception test/gmock-more-actions_test.cc)
  178. cxx_test_with_flags(gmock_no_rtti_test "${cxx_no_rtti}"
  179. gmock_main_no_rtti test/gmock-spec-builders_test.cc)
  180. cxx_shared_library(shared_gmock_main "${cxx_default}"
  181. "${gtest_dir}/src/gtest-all.cc" src/gmock-all.cc src/gmock_main.cc)
  182. # Tests that a binary can be built with Google Mock as a shared library. On
  183. # some system configurations, it may not possible to run the binary without
  184. # knowing more details about the system configurations. We do not try to run
  185. # this binary. To get a more robust shared library coverage, configure with
  186. # -DBUILD_SHARED_LIBS=ON.
  187. cxx_executable_with_flags(shared_gmock_test_ "${cxx_default}"
  188. shared_gmock_main test/gmock-spec-builders_test.cc)
  189. set_target_properties(shared_gmock_test_
  190. PROPERTIES
  191. COMPILE_DEFINITIONS "GTEST_LINKED_AS_SHARED_LIBRARY=1")
  192. ############################################################
  193. # Python tests.
  194. cxx_executable(gmock_leak_test_ test gmock_main)
  195. py_test(gmock_leak_test)
  196. cxx_executable(gmock_output_test_ test gmock)
  197. py_test(gmock_output_test)
  198. endif()