blob: 1db14a253626db8e8a8223b7f4d826fc5625b812 [file] [log] [blame]
vladlosev0f3f5012010-05-13 18:19:26 +00001########################################################################
zhanyong.wan7dfbea42010-10-05 19:24:04 +00002# CMake build script for Google Mock.
vladlosev0f3f5012010-05-13 18:19:26 +00003#
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
vladlosev0f3f5012010-05-13 18:19:26 +00008option(gmock_build_tests "Build all of Google Mock's own tests." OFF)
9
10# A directory to find Google Test sources.
vladlosev0a781df2010-05-20 22:17:28 +000011if (EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/gtest/CMakeLists.txt")
vladlosev0f3f5012010-05-13 18:19:26 +000012 set(gtest_dir gtest)
13else()
Arnaud Lacombeeff38a72015-08-26 21:50:38 -070014 set(gtest_dir ../googletest)
vladlosev0f3f5012010-05-13 18:19:26 +000015endif()
16
zhanyong.wan7dfbea42010-10-05 19:24:04 +000017# Defines pre_project_set_up_hermetic_build() and set_up_hermetic_build().
vladlosev0f3f5012010-05-13 18:19:26 +000018include("${gtest_dir}/cmake/hermetic_build.cmake" OPTIONAL)
19
20if (COMMAND pre_project_set_up_hermetic_build)
21 # Google Test also calls hermetic setup functions from add_subdirectory,
22 # although its changes will not affect things at the current scope.
23 pre_project_set_up_hermetic_build()
24endif()
25
26########################################################################
27#
28# Project-wide settings
29
30# Name of the project.
31#
32# CMake files in this project can refer to the root source directory
33# as ${gmock_SOURCE_DIR} and to the root binary directory as
34# ${gmock_BINARY_DIR}.
35# Language "C" is required for find_package(Threads).
David Seifert8604c4a2017-08-14 13:45:56 +020036if (CMAKE_VERSION VERSION_LESS 3.0)
37 project(gmock CXX C)
38else()
39 cmake_policy(SET CMP0048 NEW)
Dakota Hawkins759ef7c2018-07-24 11:06:55 -040040 project(gmock VERSION ${GOOGLETEST_VERSION} LANGUAGES CXX C)
David Seifert8604c4a2017-08-14 13:45:56 +020041endif()
Craig Scottc0059a72016-01-01 11:01:15 +110042cmake_minimum_required(VERSION 2.6.4)
vladlosev0f3f5012010-05-13 18:19:26 +000043
44if (COMMAND set_up_hermetic_build)
45 set_up_hermetic_build()
46endif()
47
vladlosev0f3f5012010-05-13 18:19:26 +000048# Instructs CMake to process Google Test's CMakeLists.txt and add its
49# targets to the current scope. We are placing Google Test's binary
zhanyong.wan7dfbea42010-10-05 19:24:04 +000050# directory in a subdirectory of our own as VC compilation may break
51# if they are the same (the default).
vladlosev0f3f5012010-05-13 18:19:26 +000052add_subdirectory("${gtest_dir}" "${gmock_BINARY_DIR}/gtest")
53
Henry Fredrick Schreinerb22e8de2018-04-05 13:38:33 +020054
55# These commands only run if this is the main project
56if(CMAKE_PROJECT_NAME STREQUAL "gmock" OR CMAKE_PROJECT_NAME STREQUAL "googletest-distribution")
57
58 # BUILD_SHARED_LIBS is a standard CMake variable, but we declare it here to
59 # make it prominent in the GUI.
60 option(BUILD_SHARED_LIBS "Build shared libraries (DLLs)." OFF)
61
62else()
63
64 mark_as_advanced(gmock_build_tests)
65
66endif()
67
zhanyong.wan7dfbea42010-10-05 19:24:04 +000068# Although Google Test's CMakeLists.txt calls this function, the
69# changes there don't affect the current scope. Therefore we have to
70# call it again here.
71config_compiler_and_linker() # from ${gtest_dir}/cmake/internal_utils.cmake
72
vladlosev0f3f5012010-05-13 18:19:26 +000073# Adds Google Mock's and Google Test's header directories to the search path.
74include_directories("${gmock_SOURCE_DIR}/include"
75 "${gmock_SOURCE_DIR}"
76 "${gtest_SOURCE_DIR}/include"
77 # This directory is needed to build directly from Google
78 # Test sources.
79 "${gtest_SOURCE_DIR}")
80
kosakb93d0f12014-01-13 22:28:01 +000081# Summary of tuple support for Microsoft Visual Studio:
82# Compiler version(MS) version(cmake) Support
83# ---------- ----------- -------------- -----------------------------
84# <= VS 2010 <= 10 <= 1600 Use Google Tests's own tuple.
85# VS 2012 11 1700 std::tr1::tuple + _VARIADIC_MAX=10
86# VS 2013 12 1800 std::tr1::tuple
Wojciech Mamrak3464f762017-12-07 18:18:17 +010087# VS 2015 14 1900 std::tuple
88# VS 2017 15 >= 1910 std::tuple
kosakb93d0f12014-01-13 22:28:01 +000089if (MSVC AND MSVC_VERSION EQUAL 1700)
90 add_definitions(/D _VARIADIC_MAX=10)
91endif()
92
vladlosev0f3f5012010-05-13 18:19:26 +000093########################################################################
94#
95# Defines the gmock & gmock_main libraries. User tests should link
96# with one of them.
97
98# Google Mock libraries. We build them using more strict warnings than what
99# are used for other targets, to ensure that Google Mock can be compiled by
100# a user aggressive about warnings.
Romain Geissler0663ce92017-12-02 22:47:20 +0100101if (MSVC)
102 cxx_library(gmock
103 "${cxx_strict}"
104 "${gtest_dir}/src/gtest-all.cc"
105 src/gmock-all.cc)
vladlosev0f3f5012010-05-13 18:19:26 +0000106
Romain Geissler0663ce92017-12-02 22:47:20 +0100107 cxx_library(gmock_main
108 "${cxx_strict}"
109 "${gtest_dir}/src/gtest-all.cc"
110 src/gmock-all.cc
111 src/gmock_main.cc)
112else()
113 cxx_library(gmock "${cxx_strict}" src/gmock-all.cc)
114 target_link_libraries(gmock gtest)
115 cxx_library(gmock_main "${cxx_strict}" src/gmock_main.cc)
116 target_link_libraries(gmock_main gmock)
117endif()
vladlosev0f3f5012010-05-13 18:19:26 +0000118
Craig Scottf601ee12015-12-06 16:25:33 +1100119# If the CMake version supports it, attach header directory information
120# to the targets for when we are part of a parent build (ie being pulled
121# in via add_subdirectory() rather than being a standalone build).
122if (DEFINED CMAKE_VERSION AND NOT "${CMAKE_VERSION}" VERSION_LESS "2.8.11")
Dakota Hawkins759ef7c2018-07-24 11:06:55 -0400123 target_include_directories(gmock SYSTEM
124 INTERFACE
125 $<BUILD_INTERFACE:${gmock_SOURCE_DIR}/include>
126 $<BUILD_INTERFACE:${gmock_SOURCE_DIR}>
127 $<BUILD_INTERFACE:${gtest_SOURCE_DIR}/include>
128 $<BUILD_INTERFACE:${gtest_SOURCE_DIR}>
129 $<INSTALL_INTERFACE:include>)
130 target_include_directories(gmock_main SYSTEM
131 INTERFACE
132 $<BUILD_INTERFACE:${gmock_SOURCE_DIR}/include>
133 $<BUILD_INTERFACE:${gmock_SOURCE_DIR}>
134 $<BUILD_INTERFACE:${gtest_SOURCE_DIR}/include>
135 $<BUILD_INTERFACE:${gtest_SOURCE_DIR}>
136 $<INSTALL_INTERFACE:include>)
Craig Scottf601ee12015-12-06 16:25:33 +1100137endif()
138
vladlosev0f3f5012010-05-13 18:19:26 +0000139########################################################################
140#
Joan Puigcerver7c8ac482015-12-03 09:33:21 +0100141# Install rules
Matthew Woehlke0e8e0e02017-08-09 15:29:36 -0400142if(INSTALL_GMOCK)
Dakota Hawkins759ef7c2018-07-24 11:06:55 -0400143 install(TARGETS gmock
144 EXPORT gmockConfigInternal
Bryan Zimmerman1ae40962017-10-27 14:01:16 -0400145 RUNTIME DESTINATION "${CMAKE_INSTALL_BINDIR}"
Dakota Hawkins759ef7c2018-07-24 11:06:55 -0400146 ARCHIVE DESTINATION "${CMAKE_INSTALL_LIBDIR}"
147 LIBRARY DESTINATION "${CMAKE_INSTALL_LIBDIR}")
148 install(EXPORT gmockConfigInternal
149 DESTINATION "${INSTALL_CMAKE_DIR}"
150 NAMESPACE googletest_)
151 install(TARGETS gmock_main
152 EXPORT gmock_mainConfigInternal
153 RUNTIME DESTINATION "${CMAKE_INSTALL_BINDIR}"
154 ARCHIVE DESTINATION "${CMAKE_INSTALL_LIBDIR}"
155 LIBRARY DESTINATION "${CMAKE_INSTALL_LIBDIR}")
156 install(EXPORT gmock_mainConfigInternal
157 DESTINATION "${INSTALL_CMAKE_DIR}"
158 NAMESPACE googletest_)
159 set(googletest_install_targets
160 ${googletest_install_targets} gmock gmock_main PARENT_SCOPE)
161
Bryan Zimmerman1ae40962017-10-27 14:01:16 -0400162 install(DIRECTORY "${gmock_SOURCE_DIR}/include/gmock"
163 DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}")
David Seifert8604c4a2017-08-14 13:45:56 +0200164
165 # configure and install pkgconfig files
166 configure_file(
167 cmake/gmock.pc.in
Elias Daler9ca399a2018-08-13 23:01:00 +0300168 "${gmock_BINARY_DIR}/gmock.pc"
David Seifert8604c4a2017-08-14 13:45:56 +0200169 @ONLY)
170 configure_file(
171 cmake/gmock_main.pc.in
Elias Daler9ca399a2018-08-13 23:01:00 +0300172 "${gmock_BINARY_DIR}/gmock_main.pc"
David Seifert8604c4a2017-08-14 13:45:56 +0200173 @ONLY)
Elias Daler9ca399a2018-08-13 23:01:00 +0300174 install(FILES "${gmock_BINARY_DIR}/gmock.pc" "${gmock_BINARY_DIR}/gmock_main.pc"
David Seifert8604c4a2017-08-14 13:45:56 +0200175 DESTINATION "${CMAKE_INSTALL_LIBDIR}/pkgconfig")
Matthew Woehlke0e8e0e02017-08-09 15:29:36 -0400176endif()
Joan Puigcerver7c8ac482015-12-03 09:33:21 +0100177
178########################################################################
179#
vladlosev0f3f5012010-05-13 18:19:26 +0000180# Google Mock's own tests.
181#
182# You can skip this section if you aren't interested in testing
183# Google Mock itself.
184#
185# The tests are not built by default. To build them, set the
186# gmock_build_tests option to ON. You can do it by running ccmake
187# or specifying the -Dgmock_build_tests=ON flag when running cmake.
188
189if (gmock_build_tests)
190 # This must be set in the root directory for the tests to be run by
191 # 'make test' or ctest.
192 enable_testing()
193
194 ############################################################
195 # C++ tests built with standard compiler flags.
196
197 cxx_test(gmock-actions_test gmock_main)
198 cxx_test(gmock-cardinalities_test gmock_main)
zhanyong.wana1a98f82013-03-01 21:28:40 +0000199 cxx_test(gmock_ex_test gmock_main)
vladlosev0f3f5012010-05-13 18:19:26 +0000200 cxx_test(gmock-generated-actions_test gmock_main)
201 cxx_test(gmock-generated-function-mockers_test gmock_main)
202 cxx_test(gmock-generated-internal-utils_test gmock_main)
203 cxx_test(gmock-generated-matchers_test gmock_main)
204 cxx_test(gmock-internal-utils_test gmock_main)
205 cxx_test(gmock-matchers_test gmock_main)
206 cxx_test(gmock-more-actions_test gmock_main)
207 cxx_test(gmock-nice-strict_test gmock_main)
208 cxx_test(gmock-port_test gmock_main)
209 cxx_test(gmock-spec-builders_test gmock_main)
210 cxx_test(gmock_link_test gmock_main test/gmock_link2_test.cc)
vladlosev0f3f5012010-05-13 18:19:26 +0000211 cxx_test(gmock_test gmock_main)
212
Roman Lebedev1a62d1b2016-12-30 10:46:39 +0300213 if (DEFINED GTEST_HAS_PTHREAD)
vladlosev47be72a2011-05-11 08:18:45 +0000214 cxx_test(gmock_stress_test gmock)
215 endif()
216
vladlosev0f3f5012010-05-13 18:19:26 +0000217 # gmock_all_test is commented to save time building and running tests.
218 # Uncomment if necessary.
219 # cxx_test(gmock_all_test gmock_main)
220
221 ############################################################
222 # C++ tests built with non-standard compiler flags.
223
Romain Geissler0663ce92017-12-02 22:47:20 +0100224 if (MSVC)
225 cxx_library(gmock_main_no_exception "${cxx_no_exception}"
kosakb93d0f12014-01-13 22:28:01 +0000226 "${gtest_dir}/src/gtest-all.cc" src/gmock-all.cc src/gmock_main.cc)
227
Romain Geissler0663ce92017-12-02 22:47:20 +0100228 cxx_library(gmock_main_no_rtti "${cxx_no_rtti}"
229 "${gtest_dir}/src/gtest-all.cc" src/gmock-all.cc src/gmock_main.cc)
vladlosev0f3f5012010-05-13 18:19:26 +0000230
Romain Geissler0663ce92017-12-02 22:47:20 +0100231 if (MSVC_VERSION LESS 1600) # 1600 is Visual Studio 2010.
232 # Visual Studio 2010, 2012, and 2013 define symbols in std::tr1 that
233 # conflict with our own definitions. Therefore using our own tuple does not
234 # work on those compilers.
235 cxx_library(gmock_main_use_own_tuple "${cxx_use_own_tuple}"
236 "${gtest_dir}/src/gtest-all.cc" src/gmock-all.cc src/gmock_main.cc)
237
238 cxx_test_with_flags(gmock_use_own_tuple_test "${cxx_use_own_tuple}"
239 gmock_main_use_own_tuple test/gmock-spec-builders_test.cc)
240 endif()
241 else()
242 cxx_library(gmock_main_no_exception "${cxx_no_exception}" src/gmock_main.cc)
243 target_link_libraries(gmock_main_no_exception gmock)
244
245 cxx_library(gmock_main_no_rtti "${cxx_no_rtti}" src/gmock_main.cc)
246 target_link_libraries(gmock_main_no_rtti gmock)
247
248 cxx_library(gmock_main_use_own_tuple "${cxx_use_own_tuple}" src/gmock_main.cc)
249 target_link_libraries(gmock_main_use_own_tuple gmock)
250 endif()
vladlosev0f3f5012010-05-13 18:19:26 +0000251 cxx_test_with_flags(gmock-more-actions_no_exception_test "${cxx_no_exception}"
252 gmock_main_no_exception test/gmock-more-actions_test.cc)
253
254 cxx_test_with_flags(gmock_no_rtti_test "${cxx_no_rtti}"
255 gmock_main_no_rtti test/gmock-spec-builders_test.cc)
256
vladlosev587c1b32011-05-20 00:42:22 +0000257 cxx_shared_library(shared_gmock_main "${cxx_default}"
258 "${gtest_dir}/src/gtest-all.cc" src/gmock-all.cc src/gmock_main.cc)
259
260 # Tests that a binary can be built with Google Mock as a shared library. On
261 # some system configurations, it may not possible to run the binary without
262 # knowing more details about the system configurations. We do not try to run
263 # this binary. To get a more robust shared library coverage, configure with
264 # -DBUILD_SHARED_LIBS=ON.
265 cxx_executable_with_flags(shared_gmock_test_ "${cxx_default}"
266 shared_gmock_main test/gmock-spec-builders_test.cc)
267 set_target_properties(shared_gmock_test_
268 PROPERTIES
269 COMPILE_DEFINITIONS "GTEST_LINKED_AS_SHARED_LIBRARY=1")
270
vladlosev0f3f5012010-05-13 18:19:26 +0000271 ############################################################
272 # Python tests.
273
274 cxx_executable(gmock_leak_test_ test gmock_main)
275 py_test(gmock_leak_test)
276
277 cxx_executable(gmock_output_test_ test gmock)
278 py_test(gmock_output_test)
279endif()