blob: d32b70b5be0e0ae74f5376fb03a2226065ad599a [file] [log] [blame]
vladlosev0f3f5012010-05-13 18:19:26 +00001########################################################################
Enji Cooper699943f2019-05-19 23:29:18 -07002# Note: CMake support is community-based. The maintainers do not use CMake
misterg63be3dc2019-04-29 17:18:42 -04003# internally.
4#
zhanyong.wan7dfbea42010-10-05 19:24:04 +00005# CMake build script for Google Mock.
vladlosev0f3f5012010-05-13 18:19:26 +00006#
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
vladlosev0f3f5012010-05-13 18:19:26 +000011option(gmock_build_tests "Build all of Google Mock's own tests." OFF)
12
13# A directory to find Google Test sources.
vladlosev0a781df2010-05-20 22:17:28 +000014if (EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/gtest/CMakeLists.txt")
vladlosev0f3f5012010-05-13 18:19:26 +000015 set(gtest_dir gtest)
16else()
Arnaud Lacombeeff38a72015-08-26 21:50:38 -070017 set(gtest_dir ../googletest)
vladlosev0f3f5012010-05-13 18:19:26 +000018endif()
19
zhanyong.wan7dfbea42010-10-05 19:24:04 +000020# Defines pre_project_set_up_hermetic_build() and set_up_hermetic_build().
vladlosev0f3f5012010-05-13 18:19:26 +000021include("${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).
David Seifert8604c4a2017-08-14 13:45:56 +020039if (CMAKE_VERSION VERSION_LESS 3.0)
40 project(gmock CXX C)
41else()
42 cmake_policy(SET CMP0048 NEW)
Dakota Hawkins759ef7c2018-07-24 11:06:55 -040043 project(gmock VERSION ${GOOGLETEST_VERSION} LANGUAGES CXX C)
David Seifert8604c4a2017-08-14 13:45:56 +020044endif()
Craig Scottc0059a72016-01-01 11:01:15 +110045cmake_minimum_required(VERSION 2.6.4)
vladlosev0f3f5012010-05-13 18:19:26 +000046
47if (COMMAND set_up_hermetic_build)
48 set_up_hermetic_build()
49endif()
50
vladlosev0f3f5012010-05-13 18:19:26 +000051# Instructs CMake to process Google Test's CMakeLists.txt and add its
52# targets to the current scope. We are placing Google Test's binary
zhanyong.wan7dfbea42010-10-05 19:24:04 +000053# directory in a subdirectory of our own as VC compilation may break
54# if they are the same (the default).
Enji Cooper9b6de412019-03-04 12:40:12 -080055add_subdirectory("${gtest_dir}" "${gmock_BINARY_DIR}/${gtest_dir}")
vladlosev0f3f5012010-05-13 18:19:26 +000056
Henry Fredrick Schreinerb22e8de2018-04-05 13:38:33 +020057
58# These commands only run if this is the main project
59if(CMAKE_PROJECT_NAME STREQUAL "gmock" OR CMAKE_PROJECT_NAME STREQUAL "googletest-distribution")
Henry Fredrick Schreinerb22e8de2018-04-05 13:38:33 +020060 # BUILD_SHARED_LIBS is a standard CMake variable, but we declare it here to
61 # make it prominent in the GUI.
62 option(BUILD_SHARED_LIBS "Build shared libraries (DLLs)." OFF)
Henry Fredrick Schreinerb22e8de2018-04-05 13:38:33 +020063else()
Henry Fredrick Schreinerb22e8de2018-04-05 13:38:33 +020064 mark_as_advanced(gmock_build_tests)
Henry Fredrick Schreinerb22e8de2018-04-05 13:38:33 +020065endif()
66
zhanyong.wan7dfbea42010-10-05 19:24:04 +000067# Although Google Test's CMakeLists.txt calls this function, the
68# changes there don't affect the current scope. Therefore we have to
69# call it again here.
70config_compiler_and_linker() # from ${gtest_dir}/cmake/internal_utils.cmake
71
vladlosev0f3f5012010-05-13 18:19:26 +000072# Adds Google Mock's and Google Test's header directories to the search path.
Stefano Soffiaaff03792017-12-01 12:48:46 +010073set(gmock_build_include_dirs
74 "${gmock_SOURCE_DIR}/include"
75 "${gmock_SOURCE_DIR}"
76 "${gtest_SOURCE_DIR}/include"
77 # This directory is needed to build directly from Google Test sources.
78 "${gtest_SOURCE_DIR}")
79include_directories(${gmock_build_include_dirs})
vladlosev0f3f5012010-05-13 18:19:26 +000080
81########################################################################
82#
83# Defines the gmock & gmock_main libraries. User tests should link
84# with one of them.
85
86# Google Mock libraries. We build them using more strict warnings than what
87# are used for other targets, to ensure that Google Mock can be compiled by
88# a user aggressive about warnings.
Romain Geissler0663ce92017-12-02 22:47:20 +010089if (MSVC)
90 cxx_library(gmock
91 "${cxx_strict}"
92 "${gtest_dir}/src/gtest-all.cc"
93 src/gmock-all.cc)
vladlosev0f3f5012010-05-13 18:19:26 +000094
Romain Geissler0663ce92017-12-02 22:47:20 +010095 cxx_library(gmock_main
96 "${cxx_strict}"
97 "${gtest_dir}/src/gtest-all.cc"
98 src/gmock-all.cc
99 src/gmock_main.cc)
100else()
101 cxx_library(gmock "${cxx_strict}" src/gmock-all.cc)
Stefano Soffiaaff03792017-12-01 12:48:46 +0100102 target_link_libraries(gmock PUBLIC gtest)
Romain Geissler0663ce92017-12-02 22:47:20 +0100103 cxx_library(gmock_main "${cxx_strict}" src/gmock_main.cc)
Stefano Soffiaaff03792017-12-01 12:48:46 +0100104 target_link_libraries(gmock_main PUBLIC gmock)
Romain Geissler0663ce92017-12-02 22:47:20 +0100105endif()
Craig Scottf601ee12015-12-06 16:25:33 +1100106# If the CMake version supports it, attach header directory information
107# to the targets for when we are part of a parent build (ie being pulled
108# in via add_subdirectory() rather than being a standalone build).
109if (DEFINED CMAKE_VERSION AND NOT "${CMAKE_VERSION}" VERSION_LESS "2.8.11")
Stefano Soffiaaff03792017-12-01 12:48:46 +0100110 target_include_directories(gmock SYSTEM INTERFACE
111 "$<BUILD_INTERFACE:${gmock_build_include_dirs}>"
Dakota Hawkinsb19292e2018-08-23 01:42:34 -0400112 "$<INSTALL_INTERFACE:$<INSTALL_PREFIX>/${CMAKE_INSTALL_INCLUDEDIR}>")
Stefano Soffiaaff03792017-12-01 12:48:46 +0100113 target_include_directories(gmock_main SYSTEM INTERFACE
114 "$<BUILD_INTERFACE:${gmock_build_include_dirs}>"
Dakota Hawkinsb19292e2018-08-23 01:42:34 -0400115 "$<INSTALL_INTERFACE:$<INSTALL_PREFIX>/${CMAKE_INSTALL_INCLUDEDIR}>")
Craig Scottf601ee12015-12-06 16:25:33 +1100116endif()
117
vladlosev0f3f5012010-05-13 18:19:26 +0000118########################################################################
119#
Joan Puigcerver7c8ac482015-12-03 09:33:21 +0100120# Install rules
Stefano Soffiaaff03792017-12-01 12:48:46 +0100121install_project(gmock gmock_main)
Joan Puigcerver7c8ac482015-12-03 09:33:21 +0100122
123########################################################################
124#
vladlosev0f3f5012010-05-13 18:19:26 +0000125# Google Mock's own tests.
126#
127# You can skip this section if you aren't interested in testing
128# Google Mock itself.
129#
130# The tests are not built by default. To build them, set the
131# gmock_build_tests option to ON. You can do it by running ccmake
132# or specifying the -Dgmock_build_tests=ON flag when running cmake.
133
134if (gmock_build_tests)
135 # This must be set in the root directory for the tests to be run by
136 # 'make test' or ctest.
137 enable_testing()
138
Matthieuf5260ae2018-10-02 17:40:37 -0400139 if (WIN32)
140 file(GENERATE OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/$<CONFIG>/RunTest.ps1"
141 CONTENT
142"$project_bin = \"${CMAKE_BINARY_DIR}/bin/$<CONFIG>\"
143$env:Path = \"$project_bin;$env:Path\"
144& $args")
Adam Badura1be5ce02019-07-11 23:22:35 +0200145 elseif (MINGW OR CYGWIN)
Matthieuf5260ae2018-10-02 17:40:37 -0400146 file(GENERATE OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/RunTest.ps1"
147 CONTENT
148"$project_bin = (cygpath --windows ${CMAKE_BINARY_DIR}/bin)
149$env:Path = \"$project_bin;$env:Path\"
150& $args")
151 endif()
152
Adam Badura6dbddd32019-08-11 21:10:06 +0200153 if (MINGW OR CYGWIN)
154 if (CMAKE_VERSION VERSION_LESS "2.8.12")
155 add_compile_options("-Wa,-mbig-obj")
156 else()
157 add_definitions("-Wa,-mbig-obj")
158 endif()
159 endif()
160
vladlosev0f3f5012010-05-13 18:19:26 +0000161 ############################################################
162 # C++ tests built with standard compiler flags.
163
164 cxx_test(gmock-actions_test gmock_main)
165 cxx_test(gmock-cardinalities_test gmock_main)
zhanyong.wana1a98f82013-03-01 21:28:40 +0000166 cxx_test(gmock_ex_test gmock_main)
Abseil Teamc5f08bf2018-11-14 13:00:04 -0500167 cxx_test(gmock-function-mocker_test gmock_main)
vladlosev0f3f5012010-05-13 18:19:26 +0000168 cxx_test(gmock-generated-actions_test gmock_main)
169 cxx_test(gmock-generated-function-mockers_test gmock_main)
vladlosev0f3f5012010-05-13 18:19:26 +0000170 cxx_test(gmock-generated-matchers_test gmock_main)
171 cxx_test(gmock-internal-utils_test gmock_main)
172 cxx_test(gmock-matchers_test gmock_main)
173 cxx_test(gmock-more-actions_test gmock_main)
174 cxx_test(gmock-nice-strict_test gmock_main)
175 cxx_test(gmock-port_test gmock_main)
176 cxx_test(gmock-spec-builders_test gmock_main)
177 cxx_test(gmock_link_test gmock_main test/gmock_link2_test.cc)
vladlosev0f3f5012010-05-13 18:19:26 +0000178 cxx_test(gmock_test gmock_main)
179
Roman Lebedev1a62d1b2016-12-30 10:46:39 +0300180 if (DEFINED GTEST_HAS_PTHREAD)
vladlosev47be72a2011-05-11 08:18:45 +0000181 cxx_test(gmock_stress_test gmock)
182 endif()
183
vladlosev0f3f5012010-05-13 18:19:26 +0000184 # gmock_all_test is commented to save time building and running tests.
185 # Uncomment if necessary.
186 # cxx_test(gmock_all_test gmock_main)
187
188 ############################################################
189 # C++ tests built with non-standard compiler flags.
190
Romain Geissler0663ce92017-12-02 22:47:20 +0100191 if (MSVC)
192 cxx_library(gmock_main_no_exception "${cxx_no_exception}"
kosakb93d0f12014-01-13 22:28:01 +0000193 "${gtest_dir}/src/gtest-all.cc" src/gmock-all.cc src/gmock_main.cc)
194
Romain Geissler0663ce92017-12-02 22:47:20 +0100195 cxx_library(gmock_main_no_rtti "${cxx_no_rtti}"
196 "${gtest_dir}/src/gtest-all.cc" src/gmock-all.cc src/gmock_main.cc)
vladlosev0f3f5012010-05-13 18:19:26 +0000197
Romain Geissler0663ce92017-12-02 22:47:20 +0100198 else()
199 cxx_library(gmock_main_no_exception "${cxx_no_exception}" src/gmock_main.cc)
Stefano Soffiaaff03792017-12-01 12:48:46 +0100200 target_link_libraries(gmock_main_no_exception PUBLIC gmock)
Romain Geissler0663ce92017-12-02 22:47:20 +0100201
202 cxx_library(gmock_main_no_rtti "${cxx_no_rtti}" src/gmock_main.cc)
Stefano Soffiaaff03792017-12-01 12:48:46 +0100203 target_link_libraries(gmock_main_no_rtti PUBLIC gmock)
Romain Geissler0663ce92017-12-02 22:47:20 +0100204 endif()
vladlosev0f3f5012010-05-13 18:19:26 +0000205 cxx_test_with_flags(gmock-more-actions_no_exception_test "${cxx_no_exception}"
206 gmock_main_no_exception test/gmock-more-actions_test.cc)
207
208 cxx_test_with_flags(gmock_no_rtti_test "${cxx_no_rtti}"
209 gmock_main_no_rtti test/gmock-spec-builders_test.cc)
210
vladlosev587c1b32011-05-20 00:42:22 +0000211 cxx_shared_library(shared_gmock_main "${cxx_default}"
212 "${gtest_dir}/src/gtest-all.cc" src/gmock-all.cc src/gmock_main.cc)
213
214 # Tests that a binary can be built with Google Mock as a shared library. On
215 # some system configurations, it may not possible to run the binary without
216 # knowing more details about the system configurations. We do not try to run
217 # this binary. To get a more robust shared library coverage, configure with
218 # -DBUILD_SHARED_LIBS=ON.
219 cxx_executable_with_flags(shared_gmock_test_ "${cxx_default}"
220 shared_gmock_main test/gmock-spec-builders_test.cc)
221 set_target_properties(shared_gmock_test_
222 PROPERTIES
223 COMPILE_DEFINITIONS "GTEST_LINKED_AS_SHARED_LIBRARY=1")
224
vladlosev0f3f5012010-05-13 18:19:26 +0000225 ############################################################
226 # Python tests.
227
228 cxx_executable(gmock_leak_test_ test gmock_main)
229 py_test(gmock_leak_test)
230
231 cxx_executable(gmock_output_test_ test gmock)
232 py_test(gmock_output_test)
233endif()