CMakeLists.txt 14.4 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
########################################################################
# Experimental CMake build script for Google Test.
#
# Consider this a prototype.  It will change drastically.  For now,
# this is only for people on the cutting edge.
#
# 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'.

11
# For hermetic builds, we may need to tell CMake to use toolchain in a
12
# specific location.
13
string(REPLACE "\\" "/" CMAKE_RC_COMPILER "${CMAKE_RC_COMPILER}")
14
if (gtest_compiler)
15
  string(REPLACE "\\" "/" gtest_compiler "${gtest_compiler}")
16
17
18
19
20
  include(CMakeForceCompiler)
  cmake_force_c_compiler("${gtest_compiler}" "")
  cmake_force_cxx_compiler("${gtest_compiler}" "")
endif()

21
22
23
24
25
26
27
28
29
########################################################################
#
# 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}.
30
31
# Language "C" is required for find_package(Threads).
project(gtest CXX C)
32
cmake_minimum_required(VERSION 2.6.4)
33

34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
if (MSVC)
  # For MSVC, CMake sets certain flags to defaults we want to override.
  # This replacement code is taken from sample in the CMake Wiki at
  # http://www.cmake.org/Wiki/CMake_FAQ#Dynamic_Replace.
  foreach (flag_var
           CMAKE_CXX_FLAGS CMAKE_CXX_FLAGS_DEBUG CMAKE_CXX_FLAGS_RELEASE
           CMAKE_CXX_FLAGS_MINSIZEREL CMAKE_CXX_FLAGS_RELWITHDEBINFO)
    # In hermetic build environments, tests may not have access to MS runtime
    # DLLs, so this replaces /MD (CRT libraries in DLLs) with /MT (static CRT
    # libraries).
    string(REPLACE "/MD" "-MT" ${flag_var} "${${flag_var}}")
    # We prefer more strict warning checking for building Google Test.
    # Replaces /W3 with /W4 in defaults.
    string(REPLACE "/W3" "-W4" ${flag_var} "${${flag_var}}")
  endforeach()
endif()

51
52
53
54
55
56
57
58
59
# Where gtest's .h files can be found.
include_directories(
  ${gtest_SOURCE_DIR}/include
  ${gtest_SOURCE_DIR})

# Where the gtest libraries can be found.
link_directories(
  ${gtest_BINARY_DIR}/src)

60
61
62
# Defines CMAKE_USE_PTHREADS_INIT and CMAKE_THREAD_LIBS_INIT.
find_package(Threads)

63
# Defines the compiler/linker flags used to build gtest.  You can
64
65
66
# tweak these definitions to suit your need.  A variable's value is
# empty before it's explicitly assigned to.

67
if (MSVC)
68
  # Newlines inside flags variables break CMake's NMake generator.
69
70
71
  set(cxx_base_flags "-GS -W4 -WX -wd4275 -nologo -J -Zi")
  set(cxx_base_flags "${cxx_base_flags} -D_UNICODE -DUNICODE -DWIN32 -D_WIN32")
  set(cxx_base_flags "${cxx_base_flags} -DSTRICT -DWIN32_LEAN_AND_MEAN")
zhanyong.wan's avatar
zhanyong.wan committed
72
  set(cxx_exception_flags "-EHsc -D_HAS_EXCEPTIONS=1")
73
74
75
76
  set(cxx_no_exception_flags "-D_HAS_EXCEPTIONS=0")
  set(cxx_no_rtti_flags "-GR-")
elseif (CMAKE_COMPILER_IS_GNUCXX)
  set(cxx_base_flags "-Wall -Wshadow")
zhanyong.wan's avatar
zhanyong.wan committed
77
  set(cxx_exception_flags "-fexceptions")
78
  set(cxx_no_exception_flags "-fno-exceptions")
zhanyong.wan's avatar
zhanyong.wan committed
79
80
81
82
83
  # Until version 4.3.2, GCC doesn't define a macro to indicate
  # whether RTTI is enabled.  Therefore we define GTEST_HAS_RTTI
  # explicitly.
  set(cxx_no_rtti_flags "-fno-rtti -DGTEST_HAS_RTTI=0")
  set(cxx_strict_flags "-Wextra")
84
elseif (CMAKE_CXX_COMPILER_ID STREQUAL "SunPro")
zhanyong.wan's avatar
zhanyong.wan committed
85
86
87
  set(cxx_exception_flags "-features=except")
  # Sun Pro doesn't provide macros to indicate whether exceptions and
  # RTTI are enabled, so we define GTEST_HAS_* explicitly.
88
  set(cxx_no_exception_flags "-features=no%except -DGTEST_HAS_EXCEPTIONS=0")
zhanyong.wan's avatar
zhanyong.wan committed
89
  set(cxx_no_rtti_flags "-features=no%rtti -DGTEST_HAS_RTTI=0")
90
91
92
elseif (CMAKE_CXX_COMPILER_ID STREQUAL "VisualAge" OR
        CMAKE_CXX_COMPILER_ID STREQUAL "XL")
  # CMake 2.8 changes Visual Age's compiler ID to "XL".
zhanyong.wan's avatar
zhanyong.wan committed
93
  set(cxx_exception_flags "-qeh")
94
  set(cxx_no_exception_flags "-qnoeh")
95
96
97
98
  # Until version 9.0, Visual Age doesn't define a macro to indicate
  # whether RTTI is enabled.  Therefore we define GTEST_HAS_RTTI
  # explicitly.
  set(cxx_no_rtti_flags "-qnortti -DGTEST_HAS_RTTI=0")
99
endif()
100

101
102
103
104
105
if (CMAKE_USE_PTHREADS_INIT)  # The pthreads library is available.
  set(cxx_base_flags "${cxx_base_flags} -DGTEST_HAS_PTHREAD=1")
endif()

# For building gtest's own tests and samples.
zhanyong.wan's avatar
zhanyong.wan committed
106
set(cxx_exception "${CMAKE_CXX_FLAGS} ${cxx_base_flags} ${cxx_exception_flags}")
107
108
set(cxx_no_exception
    "${CMAKE_CXX_FLAGS} ${cxx_base_flags} ${cxx_no_exception_flags}")
zhanyong.wan's avatar
zhanyong.wan committed
109
110
set(cxx_default "${cxx_exception}")
set(cxx_no_rtti "${cxx_default} ${cxx_no_rtti_flags}")
111
112
113
114
set(cxx_use_own_tuple "${cxx_default} -DGTEST_USE_OWN_TR1_TUPLE=1")

# For building the gtest libraries.
set(cxx_strict "${cxx_default} ${cxx_strict_flags}")
115
116
117
118
119

########################################################################
#
# Defines the gtest & gtest_main libraries.  User tests should link
# with one of them.
120
121
function(cxx_library_with_type name type cxx_flags)
  # type can be either STATIC or SHARED to denote a static or shared library.
122
  # ARGN refers to additional arguments after 'cxx_flags'.
123
  add_library(${name} ${type} ${ARGN})
124
125
126
  set_target_properties(${name}
    PROPERTIES
    COMPILE_FLAGS "${cxx_flags}")
127
128
129
    if (CMAKE_USE_PTHREADS_INIT)
      target_link_libraries(${name} ${CMAKE_THREAD_LIBS_INIT})
    endif()
130
131
endfunction()

132
133
134
135
136
137
138
139
140
141
142
143
144
function(cxx_static_library name cxx_flags)
  cxx_library_with_type(${name} STATIC "${cxx_flags}" ${ARGN})
endfunction()

function(cxx_shared_library name cxx_flags)
  cxx_library_with_type(${name} SHARED "${cxx_flags}" ${ARGN})
endfunction()

function(cxx_library name cxx_flags)
  # TODO(vladl@google.com): Make static/shared a user option.
  cxx_static_library(${name} "${cxx_flags}" ${ARGN})
endfunction()

145
146
147
148
149
# Static versions of 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_static_library(gtest "${cxx_strict}" src/gtest-all.cc)
cxx_static_library(gtest_main "${cxx_strict}" src/gtest_main.cc)
150
151
152
153
154
155
156
157
158
159
160
161
target_link_libraries(gtest_main gtest)

########################################################################
#
# Samples on how to link user tests with gtest or gtest_main.
#
# They are not built by default.  To build them, set the
# build_gtest_samples option to ON.  You can do it by running ccmake
# or specifying the -Dbuild_gtest_samples=ON flag when running cmake.

option(build_gtest_samples "Build gtest's sample programs." OFF)

zhanyong.wan's avatar
zhanyong.wan committed
162
# cxx_executable_with_flags(name cxx_flags lib srcs...)
163
#
zhanyong.wan's avatar
zhanyong.wan committed
164
165
166
167
# creates a named C++ executable that depends on the given library and
# is built from the given source files with the given compiler flags.
function(cxx_executable_with_flags name cxx_flags lib)
  add_executable(${name} ${ARGN})
168
169
170
171
172
  if (cxx_flags)
    set_target_properties(${name}
      PROPERTIES
      COMPILE_FLAGS "${cxx_flags}")
  endif()
173
174
175
  target_link_libraries(${name} ${lib})
endfunction()

zhanyong.wan's avatar
zhanyong.wan committed
176
177
178
179
180
# cxx_executable(name dir lib srcs...)
#
# creates a named target that depends on the given lib and is built
# from the given source files.  dir/name.cc is implicitly included in
# the source file list.
181
function(cxx_executable name dir lib)
zhanyong.wan's avatar
zhanyong.wan committed
182
183
  cxx_executable_with_flags(
    ${name} "${cxx_default}" ${lib} "${dir}/${name}.cc" ${ARGN})
184
185
endfunction()

186
if (build_gtest_samples)
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
  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.
#
# Most of the tests are not built by default.  To build them, set the
# build_all_gtest_tests option to ON.  You can do it by running ccmake
# or specifying the -Dbuild_all_gtest_tests=ON flag when running cmake.

option(build_all_gtest_tests "Build all of gtest's own tests." OFF)

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

# Sets PYTHONINTERP_FOUND and PYTHON_EXECUTABLE.
217
find_package(PythonInterp)
218
219
220
221

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

222
# cxx_test_with_flags(name cxx_flags libs srcs...)
223
#
224
225
226
227
# creates a named C++ test that depends on the given libs and is built
# from the given source files with the given compiler flags.
function(cxx_test_with_flags name cxx_flags libs)
  add_executable(${name} ${ARGN})
228
229
  set_target_properties(${name}
    PROPERTIES
230
231
232
233
234
235
    COMPILE_FLAGS "${cxx_flags}")
  # To support mixing linking in static and dynamic libraries, link each
  # library in with an extra call to target_link_libraries.
  foreach (lib "${libs}")
    target_link_libraries(${name} ${lib})
  endforeach()
236
237
238
  add_test(${name} ${name})
endfunction()

239
240
241
242
243
244
245
246
247
248
# cxx_test(name libs srcs...)
#
# creates a named test target that depends on the given libs and is
# built from the given source files.  Unlike cxx_test_with_flags,
# test/name.cc is already implicitly included in the source file list.
function(cxx_test name libs)
  cxx_test_with_flags("${name}" "${cxx_default}" "${libs}"
    "test/${name}.cc" ${ARGN})
endfunction()

249
250
cxx_test(gtest_unittest gtest_main)

251
if (build_all_gtest_tests)
252
  cxx_test(gtest-death-test_test gtest_main)
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
  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)
  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)
  cxx_test(gtest-unittest-api_test gtest)
endif()

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

280
if (build_all_gtest_tests)
281
282
283
284
285
286
287
288
  cxx_library(gtest_no_exception "${cxx_no_exception}"
    src/gtest-all.cc)
  cxx_library(gtest_main_no_rtti "${cxx_no_rtti}"
    src/gtest-all.cc src/gtest_main.cc)

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

289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
  set(cxx_use_shared_gtest "${cxx_default} -DGTEST_LINKED_AS_SHARED_LIBRARY=1")
  set(cxx_build_shared_gtest "${cxx_default} -DGTEST_CREATE_SHARED_LIBRARY=1")
  if (MSVC)
    # Disables the "class 'X' needs to have dll-interface to be used
    # by clients of class 'Y'" warning. This particularly concerns generic
    # classes like vector that MS doesn't mark as exported.
    set(cxx_use_shared_gtest "${cxx_use_shared_gtest} -wd4251")
    set(cxx_build_shared_gtest "${cxx_build_shared_gtest} -wd4251")
  endif()

  cxx_shared_library(gtest_dll "${cxx_build_shared_gtest}"
    src/gtest-all.cc)

  # TODO(vladl): This and the next tests may not run in the hermetic
  # environment on Windows. Re-evaluate and possibly make them
  # platform-conditional after implementing hermetic builds.
zhanyong.wan's avatar
zhanyong.wan committed
305
306
  cxx_executable_with_flags(gtest_dll_test_ "${cxx_use_shared_gtest}"
    gtest_dll test/gtest_all_test.cc)
307

308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
  if (NOT(MSVC AND (MSVC_VERSION EQUAL 1600)))
    # The C++ Standard specifies tuple_element<int, class>.
    # Yet MSVC 10's <utility> declares tuple_element<size_t, class>.
    # That declaration conflicts with our own standard-conforming
    # tuple implementation.  Therefore using our own tuple with
    # MSVC 10 doesn't compile.
    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()
324
325
326
327
328
329
330
331
332
333
334
335

endif()

############################################################
# Python tests.

# py_test(name)
#
# creates a Python test with the given name whose main module is in
# test/name.py.  It does nothing if Python is not installed.
function(py_test name)
  if (PYTHONINTERP_FOUND)
336
337
338
339
    # ${gtest_BINARY_DIR} is known at configuration time, so we can
    # directly bind it from cmake. ${CTEST_CONFIGURATION_TYPE} is known
    # only at ctest runtime (by calling ctest -c <Configuration>), so
    # we have to escape $ to delay variable substitution here.
340
341
    add_test(${name}
      ${PYTHON_EXECUTABLE} ${gtest_SOURCE_DIR}/test/${name}.py
342
          --gtest_build_dir=${gtest_BINARY_DIR}/\${CTEST_CONFIGURATION_TYPE})
343
344
345
  endif()
endfunction()

346
if (build_all_gtest_tests)
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
  cxx_executable(gtest_break_on_failure_unittest_ test gtest)
  py_test(gtest_break_on_failure_unittest)

  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)

  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)

  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()