CMakeLists.txt 11 KB
Newer Older
1
########################################################################
2
# CMake build script for Google Test.
3
4
5
6
7
#
# To run the tests for Google Test 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'.

8
9
10
11
# 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)

12
13
14
15
16
17
18
# When other libraries are using a shared version of runtime libraries,
# Google Test also has to use one.
option(
  gtest_force_shared_crt
  "Use shared (DLL) run-time lib even when Google Test is built as static lib."
  OFF)

19
option(gtest_build_tests "Build all of gtest's own tests." OFF)
20

21
option(gtest_build_samples "Build gtest's sample programs." OFF)
22

23
24
option(gtest_disable_pthreads "Disable uses of pthreads in gtest." OFF)

25
26
27
28
29
option(
  gtest_hide_internal_symbols
  "Build gtest with internal symbols hidden in shared libraries."
  OFF)

30
31
set(CMAKE_DEBUG_POSTFIX "d" CACHE STRING "Generate debug library name with a postfix.")

32
# Defines pre_project_set_up_hermetic_build() and set_up_hermetic_build().
33
34
35
36
include(cmake/hermetic_build.cmake OPTIONAL)

if (COMMAND pre_project_set_up_hermetic_build)
  pre_project_set_up_hermetic_build()
37
38
endif()

39
40
41
42
43
44
45
46
47
########################################################################
#
# Project-wide settings

# Name of the project.
#
# CMake files in this project can refer to the root source directory
# as ${gtest_SOURCE_DIR} and to the root binary directory as
# ${gtest_BINARY_DIR}.
48
# Language "C" is required for find_package(Threads).
David Seifert's avatar
David Seifert committed
49
50
51
52
53
54
if (CMAKE_VERSION VERSION_LESS 3.0)
  project(gtest CXX C)
else()
  cmake_policy(SET CMP0048 NEW)
  project(gtest VERSION 1.9.0 LANGUAGES CXX C)
endif()
55
cmake_minimum_required(VERSION 2.6.4)
56

57
58
59
60
if (POLICY CMP0063) # Visibility
  cmake_policy(SET CMP0063 NEW)
endif (POLICY CMP0063)

61
62
63
if (COMMAND set_up_hermetic_build)
  set_up_hermetic_build()
endif()
64

65
66
67
68
69
if (gtest_hide_internal_symbols)
  set(CMAKE_CXX_VISIBILITY_PRESET hidden)
  set(CMAKE_VISIBILITY_INLINES_HIDDEN 1)
endif()

70
# Define helper functions and macros used by Google Test.
71
72
include(cmake/internal_utils.cmake)

73
config_compiler_and_linker()  # Defined in internal_utils.cmake.
74

vladlosev's avatar
vladlosev committed
75
# Where Google Test's .h files can be found.
76
77
78
79
include_directories(
  ${gtest_SOURCE_DIR}/include
  ${gtest_SOURCE_DIR})

80
81
82
83
84
85
86
87
88
89
# 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
if (MSVC AND MSVC_VERSION EQUAL 1700)
  add_definitions(/D _VARIADIC_MAX=10)
endif()

90
91
92
93
########################################################################
#
# Defines the gtest & gtest_main libraries.  User tests should link
# with one of them.
94

95
96
97
98
99
# Google Test libraries.  We build them using more strict warnings than what
# are used for other targets, to ensure that gtest can be compiled by a user
# aggressive about warnings.
cxx_library(gtest "${cxx_strict}" src/gtest-all.cc)
cxx_library(gtest_main "${cxx_strict}" src/gtest_main.cc)
100
101
target_link_libraries(gtest_main gtest)

102
103
104
105
106
107
108
109
# 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")
  target_include_directories(gtest      INTERFACE "${gtest_SOURCE_DIR}/include")
  target_include_directories(gtest_main INTERFACE "${gtest_SOURCE_DIR}/include")
endif()

110
111
112
########################################################################
#
# Install rules
113
114
115
116
117
118
119
if(INSTALL_GTEST)
  install(TARGETS gtest gtest_main
    RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
    ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
    LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR})
  install(DIRECTORY ${gtest_SOURCE_DIR}/include/gtest
    DESTINATION ${CMAKE_INSTALL_INCLUDEDIR})
David Seifert's avatar
David Seifert committed
120
121
122
123
124
125
126
127
128
129
130
131

  # configure and install pkgconfig files
  configure_file(
    cmake/gtest.pc.in
    "${CMAKE_BINARY_DIR}/gtest.pc"
    @ONLY)
  configure_file(
    cmake/gtest_main.pc.in
    "${CMAKE_BINARY_DIR}/gtest_main.pc"
    @ONLY)
  install(FILES "${CMAKE_BINARY_DIR}/gtest.pc" "${CMAKE_BINARY_DIR}/gtest_main.pc"
    DESTINATION "${CMAKE_INSTALL_LIBDIR}/pkgconfig")
132
endif()
133

134
135
136
137
138
########################################################################
#
# Samples on how to link user tests with gtest or gtest_main.
#
# They are not built by default.  To build them, set the
139
# gtest_build_samples option to ON.  You can do it by running ccmake
140
# or specifying the -Dgtest_build_samples=ON flag when running cmake.
141

142
if (gtest_build_samples)
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
  cxx_executable(sample1_unittest samples gtest_main samples/sample1.cc)
  cxx_executable(sample2_unittest samples gtest_main samples/sample2.cc)
  cxx_executable(sample3_unittest samples gtest_main)
  cxx_executable(sample4_unittest samples gtest_main samples/sample4.cc)
  cxx_executable(sample5_unittest samples gtest_main samples/sample1.cc)
  cxx_executable(sample6_unittest samples gtest_main)
  cxx_executable(sample7_unittest samples gtest_main)
  cxx_executable(sample8_unittest samples gtest_main)
  cxx_executable(sample9_unittest samples gtest)
  cxx_executable(sample10_unittest samples gtest)
endif()

########################################################################
#
# Google Test's own tests.
#
# You can skip this section if you aren't interested in testing
# Google Test itself.
#
vladlosev's avatar
vladlosev committed
162
# The tests are not built by default.  To build them, set the
163
164
# gtest_build_tests option to ON.  You can do it by running ccmake
# or specifying the -Dgtest_build_tests=ON flag when running cmake.
165

166
if (gtest_build_tests)
167
168
169
  # This must be set in the root directory for the tests to be run by
  # 'make test' or ctest.
  enable_testing()
170

171
172
  ############################################################
  # C++ tests built with standard compiler flags.
173

174
  cxx_test(gtest-death-test_test gtest_main)
175
176
177
178
179
180
181
182
183
184
185
186
  cxx_test(gtest_environment_test gtest)
  cxx_test(gtest-filepath_test gtest_main)
  cxx_test(gtest-linked_ptr_test gtest_main)
  cxx_test(gtest-listener_test gtest_main)
  cxx_test(gtest_main_unittest gtest_main)
  cxx_test(gtest-message_test gtest_main)
  cxx_test(gtest_no_test_unittest gtest)
  cxx_test(gtest-options_test gtest_main)
  cxx_test(gtest-param-test_test gtest
    test/gtest-param-test2_test.cc)
  cxx_test(gtest-port_test gtest_main)
  cxx_test(gtest_pred_impl_unittest gtest_main)
187
188
  cxx_test(gtest_premature_exit_test gtest
    test/gtest_premature_exit_test.cc)
189
  cxx_test(gtest-printers_test gtest_main)
190
191
192
193
194
195
196
197
198
  cxx_test(gtest_prod_test gtest_main
    test/production.cc)
  cxx_test(gtest_repeat_test gtest)
  cxx_test(gtest_sole_header_test gtest_main)
  cxx_test(gtest_stress_test gtest)
  cxx_test(gtest-test-part_test gtest_main)
  cxx_test(gtest_throw_on_failure_ex_test gtest)
  cxx_test(gtest-typed-test_test gtest_main
    test/gtest-typed-test2_test.cc)
199
  cxx_test(gtest_unittest gtest_main)
200
201
  cxx_test(gtest-unittest-api_test gtest)

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

vladlosev's avatar
vladlosev committed
205
206
207
208
209
210
211
  # MSVC 7.1 does not support STL with exceptions disabled.
  if (NOT MSVC OR MSVC_VERSION GREATER 1310)
    cxx_library(gtest_no_exception "${cxx_no_exception}"
      src/gtest-all.cc)
    cxx_library(gtest_main_no_exception "${cxx_no_exception}"
      src/gtest-all.cc src/gtest_main.cc)
  endif()
212
213
214
  cxx_library(gtest_main_no_rtti "${cxx_no_rtti}"
    src/gtest-all.cc src/gtest_main.cc)

215
216
217
218
219
220
221
  cxx_test_with_flags(gtest-death-test_ex_nocatch_test
    "${cxx_exception} -DGTEST_ENABLE_CATCH_EXCEPTIONS_=0"
    gtest test/gtest-death-test_ex_test.cc)
  cxx_test_with_flags(gtest-death-test_ex_catch_test
    "${cxx_exception} -DGTEST_ENABLE_CATCH_EXCEPTIONS_=1"
    gtest test/gtest-death-test_ex_test.cc)

222
223
224
  cxx_test_with_flags(gtest_no_rtti_unittest "${cxx_no_rtti}"
    gtest_main_no_rtti test/gtest_unittest.cc)

225
226
  cxx_shared_library(gtest_dll "${cxx_default}"
    src/gtest-all.cc src/gtest_main.cc)
227

228
  cxx_executable_with_flags(gtest_dll_test_ "${cxx_default}"
zhanyong.wan's avatar
zhanyong.wan committed
229
    gtest_dll test/gtest_all_test.cc)
230
231
232
  set_target_properties(gtest_dll_test_
                        PROPERTIES
                        COMPILE_DEFINITIONS "GTEST_LINKED_AS_SHARED_LIBRARY=1")
233

234
235
236
237
  if (NOT MSVC OR 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.
238
239
240
241
242
243
244
245
246
247
    cxx_library(gtest_main_use_own_tuple "${cxx_use_own_tuple}"
      src/gtest-all.cc src/gtest_main.cc)

    cxx_test_with_flags(gtest-tuple_test "${cxx_use_own_tuple}"
      gtest_main_use_own_tuple test/gtest-tuple_test.cc)

    cxx_test_with_flags(gtest_use_own_tuple_test "${cxx_use_own_tuple}"
      gtest_main_use_own_tuple
      test/gtest-param-test_test.cc test/gtest-param-test2_test.cc)
  endif()
248

249
250
  ############################################################
  # Python tests.
251
252
253
254

  cxx_executable(gtest_break_on_failure_unittest_ test gtest)
  py_test(gtest_break_on_failure_unittest)

255
256
  # Visual Studio .NET 2003 does not support STL with exceptions disabled.
  if (NOT MSVC OR MSVC_VERSION GREATER 1310)  # 1310 is Visual Studio .NET 2003
vladlosev's avatar
vladlosev committed
257
258
259
260
261
262
263
    cxx_executable_with_flags(
      gtest_catch_exceptions_no_ex_test_
      "${cxx_no_exception}"
      gtest_main_no_exception
      test/gtest_catch_exceptions_test_.cc)
  endif()

264
265
266
267
268
269
270
  cxx_executable_with_flags(
    gtest_catch_exceptions_ex_test_
    "${cxx_exception}"
    gtest_main
    test/gtest_catch_exceptions_test_.cc)
  py_test(gtest_catch_exceptions_test)

271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
  cxx_executable(gtest_color_test_ test gtest)
  py_test(gtest_color_test)

  cxx_executable(gtest_env_var_test_ test gtest)
  py_test(gtest_env_var_test)

  cxx_executable(gtest_filter_unittest_ test gtest)
  py_test(gtest_filter_unittest)

  cxx_executable(gtest_help_test_ test gtest_main)
  py_test(gtest_help_test)

  cxx_executable(gtest_list_tests_unittest_ test gtest)
  py_test(gtest_list_tests_unittest)

  cxx_executable(gtest_output_test_ test gtest)
  py_test(gtest_output_test)

  cxx_executable(gtest_shuffle_test_ test gtest)
  py_test(gtest_shuffle_test)

vladlosev's avatar
vladlosev committed
292
293
294
295
296
297
298
299
  # MSVC 7.1 does not support STL with exceptions disabled.
  if (NOT MSVC OR MSVC_VERSION GREATER 1310)
    cxx_executable(gtest_throw_on_failure_test_ test gtest_no_exception)
    set_target_properties(gtest_throw_on_failure_test_
      PROPERTIES
      COMPILE_FLAGS "${cxx_no_exception}")
    py_test(gtest_throw_on_failure_test)
  endif()
300
301
302
303
304
305
306
307
308
309
310

  cxx_executable(gtest_uninitialized_test_ test gtest)
  py_test(gtest_uninitialized_test)

  cxx_executable(gtest_xml_outfile1_test_ test gtest_main)
  cxx_executable(gtest_xml_outfile2_test_ test gtest_main)
  py_test(gtest_xml_outfiles_test)

  cxx_executable(gtest_xml_output_unittest_ test gtest)
  py_test(gtest_xml_output_unittest)
endif()