blob: f7bad8afc2c22a6ba5a33c786dc6367abc508959 [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
8# BUILD_SHARED_LIBS is a standard CMake variable, but we declare it here to
9# make it prominent in the GUI.
10option(BUILD_SHARED_LIBS "Build shared libraries (DLLs)." OFF)
11
vladlosev0f3f5012010-05-13 18:19:26 +000012option(gmock_build_tests "Build all of Google Mock's own tests." OFF)
13
14# A directory to find Google Test sources.
vladlosev0a781df2010-05-20 22:17:28 +000015if (EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/gtest/CMakeLists.txt")
vladlosev0f3f5012010-05-13 18:19:26 +000016 set(gtest_dir gtest)
17else()
Arnaud Lacombeeff38a72015-08-26 21:50:38 -070018 set(gtest_dir ../googletest)
vladlosev0f3f5012010-05-13 18:19:26 +000019endif()
20
zhanyong.wan7dfbea42010-10-05 19:24:04 +000021# Defines pre_project_set_up_hermetic_build() and set_up_hermetic_build().
vladlosev0f3f5012010-05-13 18:19:26 +000022include("${gtest_dir}/cmake/hermetic_build.cmake" OPTIONAL)
23
24if (COMMAND pre_project_set_up_hermetic_build)
25 # Google Test also calls hermetic setup functions from add_subdirectory,
26 # although its changes will not affect things at the current scope.
27 pre_project_set_up_hermetic_build()
28endif()
29
30########################################################################
31#
32# Project-wide settings
33
34# Name of the project.
35#
36# CMake files in this project can refer to the root source directory
37# as ${gmock_SOURCE_DIR} and to the root binary directory as
38# ${gmock_BINARY_DIR}.
39# Language "C" is required for find_package(Threads).
David Seifert8604c4a2017-08-14 13:45:56 +020040if (CMAKE_VERSION VERSION_LESS 3.0)
41 project(gmock CXX C)
42else()
43 cmake_policy(SET CMP0048 NEW)
44 project(gmock VERSION 1.9.0 LANGUAGES CXX C)
45endif()
Craig Scottc0059a72016-01-01 11:01:15 +110046cmake_minimum_required(VERSION 2.6.4)
vladlosev0f3f5012010-05-13 18:19:26 +000047
48if (COMMAND set_up_hermetic_build)
49 set_up_hermetic_build()
50endif()
51
vladlosev0f3f5012010-05-13 18:19:26 +000052# Instructs CMake to process Google Test's CMakeLists.txt and add its
53# targets to the current scope. We are placing Google Test's binary
zhanyong.wan7dfbea42010-10-05 19:24:04 +000054# directory in a subdirectory of our own as VC compilation may break
55# if they are the same (the default).
vladlosev0f3f5012010-05-13 18:19:26 +000056add_subdirectory("${gtest_dir}" "${gmock_BINARY_DIR}/gtest")
57
zhanyong.wan7dfbea42010-10-05 19:24:04 +000058# Although Google Test's CMakeLists.txt calls this function, the
59# changes there don't affect the current scope. Therefore we have to
60# call it again here.
61config_compiler_and_linker() # from ${gtest_dir}/cmake/internal_utils.cmake
62
vladlosev0f3f5012010-05-13 18:19:26 +000063# Adds Google Mock's and Google Test's header directories to the search path.
64include_directories("${gmock_SOURCE_DIR}/include"
65 "${gmock_SOURCE_DIR}"
66 "${gtest_SOURCE_DIR}/include"
67 # This directory is needed to build directly from Google
68 # Test sources.
69 "${gtest_SOURCE_DIR}")
70
kosakb93d0f12014-01-13 22:28:01 +000071# Summary of tuple support for Microsoft Visual Studio:
72# Compiler version(MS) version(cmake) Support
73# ---------- ----------- -------------- -----------------------------
74# <= VS 2010 <= 10 <= 1600 Use Google Tests's own tuple.
75# VS 2012 11 1700 std::tr1::tuple + _VARIADIC_MAX=10
76# VS 2013 12 1800 std::tr1::tuple
77if (MSVC AND MSVC_VERSION EQUAL 1700)
78 add_definitions(/D _VARIADIC_MAX=10)
79endif()
80
vladlosev0f3f5012010-05-13 18:19:26 +000081########################################################################
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)
102 target_link_libraries(gmock gtest)
103 cxx_library(gmock_main "${cxx_strict}" src/gmock_main.cc)
104 target_link_libraries(gmock_main gmock)
105endif()
vladlosev0f3f5012010-05-13 18:19:26 +0000106
Craig Scottf601ee12015-12-06 16:25:33 +1100107# If the CMake version supports it, attach header directory information
108# to the targets for when we are part of a parent build (ie being pulled
109# in via add_subdirectory() rather than being a standalone build).
110if (DEFINED CMAKE_VERSION AND NOT "${CMAKE_VERSION}" VERSION_LESS "2.8.11")
111 target_include_directories(gmock INTERFACE "${gmock_SOURCE_DIR}/include")
112 target_include_directories(gmock_main INTERFACE "${gmock_SOURCE_DIR}/include")
113endif()
114
vladlosev0f3f5012010-05-13 18:19:26 +0000115########################################################################
116#
Joan Puigcerver7c8ac482015-12-03 09:33:21 +0100117# Install rules
Matthew Woehlke0e8e0e02017-08-09 15:29:36 -0400118if(INSTALL_GMOCK)
119 install(TARGETS gmock gmock_main
120 RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
121 LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
122 ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR})
123 install(DIRECTORY ${gmock_SOURCE_DIR}/include/gmock
124 DESTINATION ${CMAKE_INSTALL_INCLUDEDIR})
David Seifert8604c4a2017-08-14 13:45:56 +0200125
126 # configure and install pkgconfig files
127 configure_file(
128 cmake/gmock.pc.in
129 "${CMAKE_BINARY_DIR}/gmock.pc"
130 @ONLY)
131 configure_file(
132 cmake/gmock_main.pc.in
133 "${CMAKE_BINARY_DIR}/gmock_main.pc"
134 @ONLY)
135 install(FILES "${CMAKE_BINARY_DIR}/gmock.pc" "${CMAKE_BINARY_DIR}/gmock_main.pc"
136 DESTINATION "${CMAKE_INSTALL_LIBDIR}/pkgconfig")
Matthew Woehlke0e8e0e02017-08-09 15:29:36 -0400137endif()
Joan Puigcerver7c8ac482015-12-03 09:33:21 +0100138
139########################################################################
140#
vladlosev0f3f5012010-05-13 18:19:26 +0000141# Google Mock's own tests.
142#
143# You can skip this section if you aren't interested in testing
144# Google Mock itself.
145#
146# The tests are not built by default. To build them, set the
147# gmock_build_tests option to ON. You can do it by running ccmake
148# or specifying the -Dgmock_build_tests=ON flag when running cmake.
149
150if (gmock_build_tests)
151 # This must be set in the root directory for the tests to be run by
152 # 'make test' or ctest.
153 enable_testing()
154
155 ############################################################
156 # C++ tests built with standard compiler flags.
157
158 cxx_test(gmock-actions_test gmock_main)
159 cxx_test(gmock-cardinalities_test gmock_main)
zhanyong.wana1a98f82013-03-01 21:28:40 +0000160 cxx_test(gmock_ex_test gmock_main)
vladlosev0f3f5012010-05-13 18:19:26 +0000161 cxx_test(gmock-generated-actions_test gmock_main)
162 cxx_test(gmock-generated-function-mockers_test gmock_main)
163 cxx_test(gmock-generated-internal-utils_test gmock_main)
164 cxx_test(gmock-generated-matchers_test gmock_main)
165 cxx_test(gmock-internal-utils_test gmock_main)
166 cxx_test(gmock-matchers_test gmock_main)
167 cxx_test(gmock-more-actions_test gmock_main)
168 cxx_test(gmock-nice-strict_test gmock_main)
169 cxx_test(gmock-port_test gmock_main)
170 cxx_test(gmock-spec-builders_test gmock_main)
171 cxx_test(gmock_link_test gmock_main test/gmock_link2_test.cc)
vladlosev0f3f5012010-05-13 18:19:26 +0000172 cxx_test(gmock_test gmock_main)
173
Roman Lebedev1a62d1b2016-12-30 10:46:39 +0300174 if (DEFINED GTEST_HAS_PTHREAD)
vladlosev47be72a2011-05-11 08:18:45 +0000175 cxx_test(gmock_stress_test gmock)
176 endif()
177
vladlosev0f3f5012010-05-13 18:19:26 +0000178 # gmock_all_test is commented to save time building and running tests.
179 # Uncomment if necessary.
180 # cxx_test(gmock_all_test gmock_main)
181
182 ############################################################
183 # C++ tests built with non-standard compiler flags.
184
Romain Geissler0663ce92017-12-02 22:47:20 +0100185 if (MSVC)
186 cxx_library(gmock_main_no_exception "${cxx_no_exception}"
kosakb93d0f12014-01-13 22:28:01 +0000187 "${gtest_dir}/src/gtest-all.cc" src/gmock-all.cc src/gmock_main.cc)
188
Romain Geissler0663ce92017-12-02 22:47:20 +0100189 cxx_library(gmock_main_no_rtti "${cxx_no_rtti}"
190 "${gtest_dir}/src/gtest-all.cc" src/gmock-all.cc src/gmock_main.cc)
vladlosev0f3f5012010-05-13 18:19:26 +0000191
Romain Geissler0663ce92017-12-02 22:47:20 +0100192 if (MSVC_VERSION LESS 1600) # 1600 is Visual Studio 2010.
193 # Visual Studio 2010, 2012, and 2013 define symbols in std::tr1 that
194 # conflict with our own definitions. Therefore using our own tuple does not
195 # work on those compilers.
196 cxx_library(gmock_main_use_own_tuple "${cxx_use_own_tuple}"
197 "${gtest_dir}/src/gtest-all.cc" src/gmock-all.cc src/gmock_main.cc)
198
199 cxx_test_with_flags(gmock_use_own_tuple_test "${cxx_use_own_tuple}"
200 gmock_main_use_own_tuple test/gmock-spec-builders_test.cc)
201 endif()
202 else()
203 cxx_library(gmock_main_no_exception "${cxx_no_exception}" src/gmock_main.cc)
204 target_link_libraries(gmock_main_no_exception gmock)
205
206 cxx_library(gmock_main_no_rtti "${cxx_no_rtti}" src/gmock_main.cc)
207 target_link_libraries(gmock_main_no_rtti gmock)
208
209 cxx_library(gmock_main_use_own_tuple "${cxx_use_own_tuple}" src/gmock_main.cc)
210 target_link_libraries(gmock_main_use_own_tuple gmock)
211 endif()
vladlosev0f3f5012010-05-13 18:19:26 +0000212 cxx_test_with_flags(gmock-more-actions_no_exception_test "${cxx_no_exception}"
213 gmock_main_no_exception test/gmock-more-actions_test.cc)
214
215 cxx_test_with_flags(gmock_no_rtti_test "${cxx_no_rtti}"
216 gmock_main_no_rtti test/gmock-spec-builders_test.cc)
217
vladlosev587c1b32011-05-20 00:42:22 +0000218 cxx_shared_library(shared_gmock_main "${cxx_default}"
219 "${gtest_dir}/src/gtest-all.cc" src/gmock-all.cc src/gmock_main.cc)
220
221 # Tests that a binary can be built with Google Mock as a shared library. On
222 # some system configurations, it may not possible to run the binary without
223 # knowing more details about the system configurations. We do not try to run
224 # this binary. To get a more robust shared library coverage, configure with
225 # -DBUILD_SHARED_LIBS=ON.
226 cxx_executable_with_flags(shared_gmock_test_ "${cxx_default}"
227 shared_gmock_main test/gmock-spec-builders_test.cc)
228 set_target_properties(shared_gmock_test_
229 PROPERTIES
230 COMPILE_DEFINITIONS "GTEST_LINKED_AS_SHARED_LIBRARY=1")
231
vladlosev0f3f5012010-05-13 18:19:26 +0000232 ############################################################
233 # Python tests.
234
235 cxx_executable(gmock_leak_test_ test gmock_main)
236 py_test(gmock_leak_test)
237
238 cxx_executable(gmock_output_test_ test gmock)
239 py_test(gmock_output_test)
240endif()