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