CMakeLists.txt 9.45 KB
Newer Older
vladlosev's avatar
vladlosev committed
1
########################################################################
2
# CMake build script for Google Mock.
vladlosev's avatar
vladlosev committed
3
4
5
6
7
8
9
10
#
# To run the tests for Google Mock itself on Linux, use 'make test' or
# ctest.  You can select which tests to run using 'ctest -R regex'.
# For more options, run 'ctest --help'.

option(gmock_build_tests "Build all of Google Mock's own tests." OFF)

# A directory to find Google Test sources.
11
if (EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/gtest/CMakeLists.txt")
vladlosev's avatar
vladlosev committed
12
13
  set(gtest_dir gtest)
else()
Arnaud Lacombe's avatar
Arnaud Lacombe committed
14
  set(gtest_dir ../googletest)
vladlosev's avatar
vladlosev committed
15
16
endif()

17
# Defines pre_project_set_up_hermetic_build() and set_up_hermetic_build().
vladlosev's avatar
vladlosev committed
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
include("${gtest_dir}/cmake/hermetic_build.cmake" OPTIONAL)

if (COMMAND pre_project_set_up_hermetic_build)
  # Google Test also calls hermetic setup functions from add_subdirectory,
  # although its changes will not affect things at the current scope.
  pre_project_set_up_hermetic_build()
endif()

########################################################################
#
# Project-wide settings

# Name of the project.
#
# CMake files in this project can refer to the root source directory
# as ${gmock_SOURCE_DIR} and to the root binary directory as
# ${gmock_BINARY_DIR}.
# Language "C" is required for find_package(Threads).
David Seifert's avatar
David Seifert committed
36
37
38
39
40
41
if (CMAKE_VERSION VERSION_LESS 3.0)
  project(gmock CXX C)
else()
  cmake_policy(SET CMP0048 NEW)
  project(gmock VERSION 1.9.0 LANGUAGES CXX C)
endif()
42
cmake_minimum_required(VERSION 2.6.4)
vladlosev's avatar
vladlosev committed
43
44
45
46
47
48
49

if (COMMAND set_up_hermetic_build)
  set_up_hermetic_build()
endif()

# Instructs CMake to process Google Test's CMakeLists.txt and add its
# targets to the current scope.  We are placing Google Test's binary
50
51
# directory in a subdirectory of our own as VC compilation may break
# if they are the same (the default).
vladlosev's avatar
vladlosev committed
52
53
add_subdirectory("${gtest_dir}" "${gmock_BINARY_DIR}/gtest")

54
55
56
57
58
59
60
61
62
63
64
65
66
67

# These commands only run if this is the main project
if(CMAKE_PROJECT_NAME STREQUAL "gmock" OR CMAKE_PROJECT_NAME STREQUAL "googletest-distribution")

  # BUILD_SHARED_LIBS is a standard CMake variable, but we declare it here to
  # make it prominent in the GUI.
  option(BUILD_SHARED_LIBS "Build shared libraries (DLLs)." OFF)

else()

  mark_as_advanced(gmock_build_tests)

endif()

68
69
70
71
72
# Although Google Test's CMakeLists.txt calls this function, the
# changes there don't affect the current scope.  Therefore we have to
# call it again here.
config_compiler_and_linker()  # from ${gtest_dir}/cmake/internal_utils.cmake

vladlosev's avatar
vladlosev committed
73
74
75
76
77
78
79
80
# Adds Google Mock's and Google Test's header directories to the search path.
include_directories("${gmock_SOURCE_DIR}/include"
                    "${gmock_SOURCE_DIR}"
                    "${gtest_SOURCE_DIR}/include"
                    # This directory is needed to build directly from Google
                    # Test sources.
                    "${gtest_SOURCE_DIR}")

81
82
83
84
85
86
# Summary of tuple support for Microsoft Visual Studio:
# Compiler    version(MS)  version(cmake)  Support
# ----------  -----------  --------------  -----------------------------
# <= VS 2010  <= 10        <= 1600         Use Google Tests's own tuple.
# VS 2012     11           1700            std::tr1::tuple + _VARIADIC_MAX=10
# VS 2013     12           1800            std::tr1::tuple
87
88
# VS 2015     14           1900            std::tuple
# VS 2017     15           >= 1910         std::tuple
89
90
91
92
if (MSVC AND MSVC_VERSION EQUAL 1700)
  add_definitions(/D _VARIADIC_MAX=10)
endif()

vladlosev's avatar
vladlosev committed
93
94
95
96
97
98
99
100
########################################################################
#
# Defines the gmock & gmock_main libraries.  User tests should link
# with one of them.

# Google Mock libraries.  We build them using more strict warnings than what
# are used for other targets, to ensure that Google Mock can be compiled by
# a user aggressive about warnings.
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
if (MSVC)
  cxx_library(gmock
              "${cxx_strict}"
              "${gtest_dir}/src/gtest-all.cc"
              src/gmock-all.cc)

  cxx_library(gmock_main
              "${cxx_strict}"
              "${gtest_dir}/src/gtest-all.cc"
              src/gmock-all.cc
              src/gmock_main.cc)
else()
  cxx_library(gmock "${cxx_strict}" src/gmock-all.cc)
  target_link_libraries(gmock gtest)
  cxx_library(gmock_main "${cxx_strict}" src/gmock_main.cc)
  target_link_libraries(gmock_main gmock)
endif()
vladlosev's avatar
vladlosev committed
118

119
120
121
122
# If the CMake version supports it, attach header directory information
# to the targets for when we are part of a parent build (ie being pulled
# in via add_subdirectory() rather than being a standalone build).
if (DEFINED CMAKE_VERSION AND NOT "${CMAKE_VERSION}" VERSION_LESS "2.8.11")
Sam Lunt's avatar
Sam Lunt committed
123
124
  target_include_directories(gmock      SYSTEM INTERFACE "${gmock_SOURCE_DIR}/include")
  target_include_directories(gmock_main SYSTEM INTERFACE "${gmock_SOURCE_DIR}/include")
125
126
endif()

127
128
129
########################################################################
#
# Install rules
130
131
if(INSTALL_GMOCK)
  install(TARGETS gmock gmock_main
132
133
134
135
136
    RUNTIME DESTINATION "${CMAKE_INSTALL_BINDIR}"
    LIBRARY DESTINATION "${CMAKE_INSTALL_LIBDIR}"
    ARCHIVE DESTINATION "${CMAKE_INSTALL_LIBDIR}")
  install(DIRECTORY "${gmock_SOURCE_DIR}/include/gmock"
    DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}")
David Seifert's avatar
David Seifert committed
137
138
139
140

  # configure and install pkgconfig files
  configure_file(
    cmake/gmock.pc.in
141
    "${gmock_BINARY_DIR}/gmock.pc"
David Seifert's avatar
David Seifert committed
142
143
144
    @ONLY)
  configure_file(
    cmake/gmock_main.pc.in
145
    "${gmock_BINARY_DIR}/gmock_main.pc"
David Seifert's avatar
David Seifert committed
146
    @ONLY)
147
  install(FILES "${gmock_BINARY_DIR}/gmock.pc" "${gmock_BINARY_DIR}/gmock_main.pc"
David Seifert's avatar
David Seifert committed
148
    DESTINATION "${CMAKE_INSTALL_LIBDIR}/pkgconfig")
149
endif()
150

vladlosev's avatar
vladlosev committed
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
########################################################################
#
# Google Mock's own tests.
#
# You can skip this section if you aren't interested in testing
# Google Mock itself.
#
# The tests are not built by default.  To build them, set the
# gmock_build_tests option to ON.  You can do it by running ccmake
# or specifying the -Dgmock_build_tests=ON flag when running cmake.

if (gmock_build_tests)
  # This must be set in the root directory for the tests to be run by
  # 'make test' or ctest.
  enable_testing()

  ############################################################
  # C++ tests built with standard compiler flags.

  cxx_test(gmock-actions_test gmock_main)
  cxx_test(gmock-cardinalities_test gmock_main)
172
  cxx_test(gmock_ex_test gmock_main)
vladlosev's avatar
vladlosev committed
173
174
175
176
177
178
179
180
181
182
183
184
185
  cxx_test(gmock-generated-actions_test gmock_main)
  cxx_test(gmock-generated-function-mockers_test gmock_main)
  cxx_test(gmock-generated-internal-utils_test gmock_main)
  cxx_test(gmock-generated-matchers_test gmock_main)
  cxx_test(gmock-internal-utils_test gmock_main)
  cxx_test(gmock-matchers_test gmock_main)
  cxx_test(gmock-more-actions_test gmock_main)
  cxx_test(gmock-nice-strict_test gmock_main)
  cxx_test(gmock-port_test gmock_main)
  cxx_test(gmock-spec-builders_test gmock_main)
  cxx_test(gmock_link_test gmock_main test/gmock_link2_test.cc)
  cxx_test(gmock_test gmock_main)

186
  if (DEFINED GTEST_HAS_PTHREAD)
187
188
189
    cxx_test(gmock_stress_test gmock)
  endif()

vladlosev's avatar
vladlosev committed
190
191
192
193
194
195
196
  # gmock_all_test is commented to save time building and running tests.
  # Uncomment if necessary.
  # cxx_test(gmock_all_test gmock_main)

  ############################################################
  # C++ tests built with non-standard compiler flags.

197
198
199
  if (MSVC)
    cxx_library(gmock_main_no_exception "${cxx_no_exception}"
      "${gtest_dir}/src/gtest-all.cc" src/gmock-all.cc src/gmock_main.cc)
200

201
    cxx_library(gmock_main_no_rtti "${cxx_no_rtti}"
202
203
      "${gtest_dir}/src/gtest-all.cc" src/gmock-all.cc src/gmock_main.cc)

204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
    if (MSVC_VERSION LESS 1600)  # 1600 is Visual Studio 2010.
      # Visual Studio 2010, 2012, and 2013 define symbols in std::tr1 that
      # conflict with our own definitions. Therefore using our own tuple does not
      # work on those compilers.
      cxx_library(gmock_main_use_own_tuple "${cxx_use_own_tuple}"
        "${gtest_dir}/src/gtest-all.cc" src/gmock-all.cc src/gmock_main.cc)

      cxx_test_with_flags(gmock_use_own_tuple_test "${cxx_use_own_tuple}"
        gmock_main_use_own_tuple test/gmock-spec-builders_test.cc)
    endif()
  else()
    cxx_library(gmock_main_no_exception "${cxx_no_exception}" src/gmock_main.cc)
    target_link_libraries(gmock_main_no_exception gmock)

    cxx_library(gmock_main_no_rtti "${cxx_no_rtti}" src/gmock_main.cc)
    target_link_libraries(gmock_main_no_rtti gmock)

    cxx_library(gmock_main_use_own_tuple "${cxx_use_own_tuple}" src/gmock_main.cc)
    target_link_libraries(gmock_main_use_own_tuple gmock)
223
  endif()
vladlosev's avatar
vladlosev committed
224
225
226
227
228
229
  cxx_test_with_flags(gmock-more-actions_no_exception_test "${cxx_no_exception}"
    gmock_main_no_exception test/gmock-more-actions_test.cc)

  cxx_test_with_flags(gmock_no_rtti_test "${cxx_no_rtti}"
    gmock_main_no_rtti test/gmock-spec-builders_test.cc)

230
231
232
233
234
235
236
237
238
239
240
241
242
243
  cxx_shared_library(shared_gmock_main "${cxx_default}"
    "${gtest_dir}/src/gtest-all.cc" src/gmock-all.cc src/gmock_main.cc)

  # Tests that a binary can be built with Google Mock as a shared library.  On
  # some system configurations, it may not possible to run the binary without
  # knowing more details about the system configurations. We do not try to run
  # this binary. To get a more robust shared library coverage, configure with
  # -DBUILD_SHARED_LIBS=ON.
  cxx_executable_with_flags(shared_gmock_test_ "${cxx_default}"
    shared_gmock_main test/gmock-spec-builders_test.cc)
  set_target_properties(shared_gmock_test_
    PROPERTIES
    COMPILE_DEFINITIONS "GTEST_LINKED_AS_SHARED_LIBRARY=1")

vladlosev's avatar
vladlosev committed
244
245
246
247
248
249
250
251
252
  ############################################################
  # Python tests.

  cxx_executable(gmock_leak_test_ test gmock_main)
  py_test(gmock_leak_test)

  cxx_executable(gmock_output_test_ test gmock)
  py_test(gmock_output_test)
endif()