CMakeLists.txt 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. ########################################################################
  2. # CMake build script for Google Mock.
  3. #
  4. # To run the tests for Google Mock 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. option(gmock_build_tests "Build all of Google Mock's own tests." OFF)
  8. # A directory to find Google Test sources.
  9. if (EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/gtest/CMakeLists.txt")
  10. set(gtest_dir gtest)
  11. else()
  12. set(gtest_dir ../googletest)
  13. endif()
  14. # Defines pre_project_set_up_hermetic_build() and set_up_hermetic_build().
  15. include("${gtest_dir}/cmake/hermetic_build.cmake" OPTIONAL)
  16. if (COMMAND pre_project_set_up_hermetic_build)
  17. # Google Test also calls hermetic setup functions from add_subdirectory,
  18. # although its changes will not affect things at the current scope.
  19. pre_project_set_up_hermetic_build()
  20. endif()
  21. ########################################################################
  22. #
  23. # Project-wide settings
  24. # Name of the project.
  25. #
  26. # CMake files in this project can refer to the root source directory
  27. # as ${gmock_SOURCE_DIR} and to the root binary directory as
  28. # ${gmock_BINARY_DIR}.
  29. # Language "C" is required for find_package(Threads).
  30. if (CMAKE_VERSION VERSION_LESS 3.0)
  31. project(gmock CXX C)
  32. else()
  33. cmake_policy(SET CMP0048 NEW)
  34. project(gmock VERSION ${GOOGLETEST_VERSION} LANGUAGES CXX C)
  35. endif()
  36. cmake_minimum_required(VERSION 2.6.4)
  37. if (COMMAND set_up_hermetic_build)
  38. set_up_hermetic_build()
  39. endif()
  40. # Instructs CMake to process Google Test's CMakeLists.txt and add its
  41. # targets to the current scope. We are placing Google Test's binary
  42. # directory in a subdirectory of our own as VC compilation may break
  43. # if they are the same (the default).
  44. add_subdirectory("${gtest_dir}" "${gmock_BINARY_DIR}/gtest")
  45. # These commands only run if this is the main project
  46. if(CMAKE_PROJECT_NAME STREQUAL "gmock" OR CMAKE_PROJECT_NAME STREQUAL "googletest-distribution")
  47. # BUILD_SHARED_LIBS is a standard CMake variable, but we declare it here to
  48. # make it prominent in the GUI.
  49. option(BUILD_SHARED_LIBS "Build shared libraries (DLLs)." OFF)
  50. else()
  51. mark_as_advanced(gmock_build_tests)
  52. endif()
  53. # Although Google Test's CMakeLists.txt calls this function, the
  54. # changes there don't affect the current scope. Therefore we have to
  55. # call it again here.
  56. config_compiler_and_linker() # from ${gtest_dir}/cmake/internal_utils.cmake
  57. # Adds Google Mock's and Google Test's header directories to the search path.
  58. set(gmock_build_include_dirs
  59. "${gmock_SOURCE_DIR}/include"
  60. "${gmock_SOURCE_DIR}"
  61. "${gtest_SOURCE_DIR}/include"
  62. # This directory is needed to build directly from Google Test sources.
  63. "${gtest_SOURCE_DIR}")
  64. include_directories(${gmock_build_include_dirs})
  65. # Summary of tuple support for Microsoft Visual Studio:
  66. # Compiler version(MS) version(cmake) Support
  67. # ---------- ----------- -------------- -----------------------------
  68. # <= VS 2010 <= 10 <= 1600 Use Google Tests's own tuple.
  69. # VS 2012 11 1700 std::tr1::tuple + _VARIADIC_MAX=10
  70. # VS 2013 12 1800 std::tr1::tuple
  71. # VS 2015 14 1900 std::tuple
  72. # VS 2017 15 >= 1910 std::tuple
  73. if (MSVC AND MSVC_VERSION EQUAL 1700)
  74. add_definitions(/D _VARIADIC_MAX=10)
  75. endif()
  76. ########################################################################
  77. #
  78. # Defines the gmock & gmock_main libraries. User tests should link
  79. # with one of them.
  80. # Google Mock libraries. We build them using more strict warnings than what
  81. # are used for other targets, to ensure that Google Mock can be compiled by
  82. # a user aggressive about warnings.
  83. if (MSVC)
  84. cxx_library(gmock
  85. "${cxx_strict}"
  86. "${gtest_dir}/src/gtest-all.cc"
  87. src/gmock-all.cc)
  88. cxx_library(gmock_main
  89. "${cxx_strict}"
  90. "${gtest_dir}/src/gtest-all.cc"
  91. src/gmock-all.cc
  92. src/gmock_main.cc)
  93. else()
  94. cxx_library(gmock "${cxx_strict}" src/gmock-all.cc)
  95. target_link_libraries(gmock PUBLIC gtest)
  96. cxx_library(gmock_main "${cxx_strict}" src/gmock_main.cc)
  97. target_link_libraries(gmock_main PUBLIC gmock)
  98. endif()
  99. # If the CMake version supports it, attach header directory information
  100. # to the targets for when we are part of a parent build (ie being pulled
  101. # in via add_subdirectory() rather than being a standalone build).
  102. if (DEFINED CMAKE_VERSION AND NOT "${CMAKE_VERSION}" VERSION_LESS "2.8.11")
  103. target_include_directories(gmock SYSTEM INTERFACE
  104. "$<BUILD_INTERFACE:${gmock_build_include_dirs}>"
  105. "$<INSTALL_INTERFACE:$<INSTALL_PREFIX>/${CMAKE_INSTALL_INCLUDEDIR}>")
  106. target_include_directories(gmock_main SYSTEM INTERFACE
  107. "$<BUILD_INTERFACE:${gmock_build_include_dirs}>"
  108. "$<INSTALL_INTERFACE:$<INSTALL_PREFIX>/${CMAKE_INSTALL_INCLUDEDIR}>")
  109. endif()
  110. ########################################################################
  111. #
  112. # Install rules
  113. install_project(gmock gmock_main)
  114. ########################################################################
  115. #
  116. # Google Mock's own tests.
  117. #
  118. # You can skip this section if you aren't interested in testing
  119. # Google Mock itself.
  120. #
  121. # The tests are not built by default. To build them, set the
  122. # gmock_build_tests option to ON. You can do it by running ccmake
  123. # or specifying the -Dgmock_build_tests=ON flag when running cmake.
  124. if (gmock_build_tests)
  125. # This must be set in the root directory for the tests to be run by
  126. # 'make test' or ctest.
  127. enable_testing()
  128. ############################################################
  129. # C++ tests built with standard compiler flags.
  130. cxx_test(gmock-actions_test gmock_main)
  131. cxx_test(gmock-cardinalities_test gmock_main)
  132. cxx_test(gmock_ex_test gmock_main)
  133. cxx_test(gmock-generated-actions_test gmock_main)
  134. cxx_test(gmock-generated-function-mockers_test gmock_main)
  135. cxx_test(gmock-generated-internal-utils_test gmock_main)
  136. cxx_test(gmock-generated-matchers_test gmock_main)
  137. cxx_test(gmock-internal-utils_test gmock_main)
  138. cxx_test(gmock-matchers_test gmock_main)
  139. cxx_test(gmock-more-actions_test gmock_main)
  140. cxx_test(gmock-nice-strict_test gmock_main)
  141. cxx_test(gmock-port_test gmock_main)
  142. cxx_test(gmock-spec-builders_test gmock_main)
  143. cxx_test(gmock_link_test gmock_main test/gmock_link2_test.cc)
  144. cxx_test(gmock_test gmock_main)
  145. if (DEFINED GTEST_HAS_PTHREAD)
  146. cxx_test(gmock_stress_test gmock)
  147. endif()
  148. # gmock_all_test is commented to save time building and running tests.
  149. # Uncomment if necessary.
  150. # cxx_test(gmock_all_test gmock_main)
  151. ############################################################
  152. # C++ tests built with non-standard compiler flags.
  153. if (MSVC)
  154. cxx_library(gmock_main_no_exception "${cxx_no_exception}"
  155. "${gtest_dir}/src/gtest-all.cc" src/gmock-all.cc src/gmock_main.cc)
  156. cxx_library(gmock_main_no_rtti "${cxx_no_rtti}"
  157. "${gtest_dir}/src/gtest-all.cc" src/gmock-all.cc src/gmock_main.cc)
  158. if (MSVC_VERSION LESS 1600) # 1600 is Visual Studio 2010.
  159. # Visual Studio 2010, 2012, and 2013 define symbols in std::tr1 that
  160. # conflict with our own definitions. Therefore using our own tuple does not
  161. # work on those compilers.
  162. cxx_library(gmock_main_use_own_tuple "${cxx_use_own_tuple}"
  163. "${gtest_dir}/src/gtest-all.cc" src/gmock-all.cc src/gmock_main.cc)
  164. cxx_test_with_flags(gmock_use_own_tuple_test "${cxx_use_own_tuple}"
  165. gmock_main_use_own_tuple test/gmock-spec-builders_test.cc)
  166. endif()
  167. else()
  168. cxx_library(gmock_main_no_exception "${cxx_no_exception}" src/gmock_main.cc)
  169. target_link_libraries(gmock_main_no_exception PUBLIC gmock)
  170. cxx_library(gmock_main_no_rtti "${cxx_no_rtti}" src/gmock_main.cc)
  171. target_link_libraries(gmock_main_no_rtti PUBLIC gmock)
  172. cxx_library(gmock_main_use_own_tuple "${cxx_use_own_tuple}" src/gmock_main.cc)
  173. target_link_libraries(gmock_main_use_own_tuple PUBLIC gmock)
  174. endif()
  175. cxx_test_with_flags(gmock-more-actions_no_exception_test "${cxx_no_exception}"
  176. gmock_main_no_exception test/gmock-more-actions_test.cc)
  177. cxx_test_with_flags(gmock_no_rtti_test "${cxx_no_rtti}"
  178. gmock_main_no_rtti test/gmock-spec-builders_test.cc)
  179. cxx_shared_library(shared_gmock_main "${cxx_default}"
  180. "${gtest_dir}/src/gtest-all.cc" src/gmock-all.cc src/gmock_main.cc)
  181. # Tests that a binary can be built with Google Mock as a shared library. On
  182. # some system configurations, it may not possible to run the binary without
  183. # knowing more details about the system configurations. We do not try to run
  184. # this binary. To get a more robust shared library coverage, configure with
  185. # -DBUILD_SHARED_LIBS=ON.
  186. cxx_executable_with_flags(shared_gmock_test_ "${cxx_default}"
  187. shared_gmock_main test/gmock-spec-builders_test.cc)
  188. set_target_properties(shared_gmock_test_
  189. PROPERTIES
  190. COMPILE_DEFINITIONS "GTEST_LINKED_AS_SHARED_LIBRARY=1")
  191. ############################################################
  192. # Python tests.
  193. cxx_executable(gmock_leak_test_ test gmock_main)
  194. py_test(gmock_leak_test)
  195. cxx_executable(gmock_output_test_ test gmock)
  196. py_test(gmock_output_test)
  197. endif()