CMakeLists.txt 11.6 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
12
13
14
# 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)

15
option(gtest_build_tests "Build all of gtest's own tests." OFF)
16

17
option(gtest_build_samples "Build gtest's sample programs." OFF)
18

19
20
option(gtest_disable_pthreads "Disable uses of pthreads in gtest." OFF)

21
22
23
24
25
option(
  gtest_hide_internal_symbols
  "Build gtest with internal symbols hidden in shared libraries."
  OFF)

26
# Defines pre_project_set_up_hermetic_build() and set_up_hermetic_build().
27
28
29
30
include(cmake/hermetic_build.cmake OPTIONAL)

if (COMMAND pre_project_set_up_hermetic_build)
  pre_project_set_up_hermetic_build()
31
32
endif()

33
34
35
36
37
38
39
40
41
########################################################################
#
# 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}.
42
# Language "C" is required for find_package(Threads).
43
44
45

# Project version:

David Seifert's avatar
David Seifert committed
46
47
if (CMAKE_VERSION VERSION_LESS 3.0)
  project(gtest CXX C)
48
  set(PROJECT_VERSION ${GOOGLETEST_VERSION})
David Seifert's avatar
David Seifert committed
49
50
else()
  cmake_policy(SET CMP0048 NEW)
51
  project(gtest VERSION ${GOOGLETEST_VERSION} LANGUAGES CXX C)
David Seifert's avatar
David Seifert committed
52
endif()
53
cmake_minimum_required(VERSION 2.6.4)
54

55
56
57
58
if (POLICY CMP0063) # Visibility
  cmake_policy(SET CMP0063 NEW)
endif (POLICY CMP0063)

59
60
61
if (COMMAND set_up_hermetic_build)
  set_up_hermetic_build()
endif()
62

63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# These commands only run if this is the main project
if(CMAKE_PROJECT_NAME STREQUAL "gtest" 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(
    gtest_force_shared_crt
    gtest_build_tests
    gtest_build_samples
    gtest_disable_pthreads
    gtest_hide_internal_symbols)

endif()


82
83
84
85
86
if (gtest_hide_internal_symbols)
  set(CMAKE_CXX_VISIBILITY_PRESET hidden)
  set(CMAKE_VISIBILITY_INLINES_HIDDEN 1)
endif()

87
# Define helper functions and macros used by Google Test.
88
89
include(cmake/internal_utils.cmake)

90
config_compiler_and_linker()  # Defined in internal_utils.cmake.
91

Stefano Soffia's avatar
Stefano Soffia committed
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# Create the CMake package file descriptors.
if (INSTALL_GTEST)
  include(CMakePackageConfigHelpers)
  set(cmake_package_name GTest)
  set(targets_export_name ${cmake_package_name}Targets CACHE INTERNAL "")
  set(generated_dir "${CMAKE_CURRENT_BINARY_DIR}/generated" CACHE INTERNAL "")
  set(cmake_files_install_dir "${CMAKE_INSTALL_LIBDIR}/cmake/${cmake_package_name}")
  set(version_file "${generated_dir}/${cmake_package_name}ConfigVersion.cmake")
  write_basic_package_version_file(${version_file} COMPATIBILITY AnyNewerVersion)
  install(EXPORT ${targets_export_name}
    NAMESPACE ${cmake_package_name}::
    DESTINATION ${cmake_files_install_dir})
  set(config_file "${generated_dir}/${cmake_package_name}Config.cmake")
  configure_package_config_file("${gtest_SOURCE_DIR}/cmake/Config.cmake.in"
    "${config_file}" INSTALL_DESTINATION ${cmake_files_install_dir})
  install(FILES ${version_file} ${config_file}
    DESTINATION ${cmake_files_install_dir})
endif()

vladlosev's avatar
vladlosev committed
111
# Where Google Test's .h files can be found.
Stefano Soffia's avatar
Stefano Soffia committed
112
set(gtest_build_include_dirs
113
114
  "${gtest_SOURCE_DIR}/include"
  "${gtest_SOURCE_DIR}")
Stefano Soffia's avatar
Stefano Soffia committed
115
include_directories(${gtest_build_include_dirs})
116
117
118
119
120

########################################################################
#
# Defines the gtest & gtest_main libraries.  User tests should link
# with one of them.
121

122
123
124
125
126
# 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)
127
128
129
130
# 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")
Stefano Soffia's avatar
Stefano Soffia committed
131
132
  target_include_directories(gtest SYSTEM INTERFACE
    "$<BUILD_INTERFACE:${gtest_build_include_dirs}>"
133
    "$<INSTALL_INTERFACE:$<INSTALL_PREFIX>/${CMAKE_INSTALL_INCLUDEDIR}>")
Stefano Soffia's avatar
Stefano Soffia committed
134
135
  target_include_directories(gtest_main SYSTEM INTERFACE
    "$<BUILD_INTERFACE:${gtest_build_include_dirs}>"
136
    "$<INSTALL_INTERFACE:$<INSTALL_PREFIX>/${CMAKE_INSTALL_INCLUDEDIR}>")
137
endif()
Stefano Soffia's avatar
Stefano Soffia committed
138
target_link_libraries(gtest_main PUBLIC gtest)
139

140
141
142
########################################################################
#
# Install rules
Stefano Soffia's avatar
Stefano Soffia committed
143
install_project(gtest gtest_main)
144

145
146
147
148
149
########################################################################
#
# Samples on how to link user tests with gtest or gtest_main.
#
# They are not built by default.  To build them, set the
150
# gtest_build_samples option to ON.  You can do it by running ccmake
151
# or specifying the -Dgtest_build_samples=ON flag when running cmake.
152

153
if (gtest_build_samples)
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
  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
173
# The tests are not built by default.  To build them, set the
174
175
# gtest_build_tests option to ON.  You can do it by running ccmake
# or specifying the -Dgtest_build_tests=ON flag when running cmake.
176

177
if (gtest_build_tests)
178
179
180
  # This must be set in the root directory for the tests to be run by
  # 'make test' or ctest.
  enable_testing()
181

182
183
184
185
186
187
188
189
190
191
192
193
194
195
  if (WIN32)
    file(GENERATE OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/$<CONFIG>/RunTest.ps1"
         CONTENT
"$project_bin = \"${CMAKE_BINARY_DIR}/bin/$<CONFIG>\"
$env:Path = \"$project_bin;$env:Path\"
& $args")
  elseif (MINGW)
    file(GENERATE OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/RunTest.ps1"
         CONTENT
"$project_bin = (cygpath --windows ${CMAKE_BINARY_DIR}/bin)
$env:Path = \"$project_bin;$env:Path\"
& $args")
  endif()

196
197
  ############################################################
  # C++ tests built with standard compiler flags.
198

Gennadiy Civil's avatar
 
Gennadiy Civil committed
199
  cxx_test(googletest-death-test-test gtest_main)
200
  cxx_test(gtest_environment_test gtest)
Gennadiy Civil's avatar
 
Gennadiy Civil committed
201
202
  cxx_test(googletest-filepath-test gtest_main)
  cxx_test(googletest-listener-test gtest_main)
203
  cxx_test(gtest_main_unittest gtest_main)
Gennadiy Civil's avatar
 
Gennadiy Civil committed
204
  cxx_test(googletest-message-test gtest_main)
205
  cxx_test(gtest_no_test_unittest gtest)
Gennadiy Civil's avatar
 
Gennadiy Civil committed
206
207
208
209
  cxx_test(googletest-options-test gtest_main)
  cxx_test(googletest-param-test-test gtest
    test/googletest-param-test2-test.cc)
  cxx_test(googletest-port-test gtest_main)
210
  cxx_test(gtest_pred_impl_unittest gtest_main)
211
212
  cxx_test(gtest_premature_exit_test gtest
    test/gtest_premature_exit_test.cc)
Gennadiy Civil's avatar
 
Gennadiy Civil committed
213
  cxx_test(googletest-printers-test gtest_main)
214
215
216
217
218
  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)
Gennadiy Civil's avatar
 
Gennadiy Civil committed
219
  cxx_test(googletest-test-part-test gtest_main)
220
221
222
  cxx_test(gtest_throw_on_failure_ex_test gtest)
  cxx_test(gtest-typed-test_test gtest_main
    test/gtest-typed-test2_test.cc)
223
  cxx_test(gtest_unittest gtest_main)
224
  cxx_test(gtest-unittest-api_test gtest)
225
  cxx_test(gtest_skip_in_environment_setup_test gtest_main)
226
  cxx_test(gtest_skip_test gtest_main)
227

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

vladlosev's avatar
vladlosev committed
231
232
233
234
235
236
237
  # 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()
238
239
240
  cxx_library(gtest_main_no_rtti "${cxx_no_rtti}"
    src/gtest-all.cc src/gtest_main.cc)

241
242
  cxx_test_with_flags(gtest-death-test_ex_nocatch_test
    "${cxx_exception} -DGTEST_ENABLE_CATCH_EXCEPTIONS_=0"
Gennadiy Civil's avatar
 
Gennadiy Civil committed
243
    gtest test/googletest-death-test_ex_test.cc)
244
245
  cxx_test_with_flags(gtest-death-test_ex_catch_test
    "${cxx_exception} -DGTEST_ENABLE_CATCH_EXCEPTIONS_=1"
Gennadiy Civil's avatar
 
Gennadiy Civil committed
246
    gtest test/googletest-death-test_ex_test.cc)
247

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

251
252
  cxx_shared_library(gtest_dll "${cxx_default}"
    src/gtest-all.cc src/gtest_main.cc)
253

254
  cxx_executable_with_flags(gtest_dll_test_ "${cxx_default}"
zhanyong.wan's avatar
zhanyong.wan committed
255
    gtest_dll test/gtest_all_test.cc)
256
257
258
  set_target_properties(gtest_dll_test_
                        PROPERTIES
                        COMPILE_DEFINITIONS "GTEST_LINKED_AS_SHARED_LIBRARY=1")
259

260
261
  ############################################################
  # Python tests.
262

Gennadiy Civil's avatar
 
Gennadiy Civil committed
263
  cxx_executable(googletest-break-on-failure-unittest_ test gtest)
Gennadiy Civil's avatar
 
Gennadiy Civil committed
264
  py_test(googletest-break-on-failure-unittest)
265

266
267
  py_test(gtest_skip_environment_check_output_test)

268
269
  # 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
270
    cxx_executable_with_flags(
Gennadiy Civil's avatar
Gennadiy Civil committed
271
      googletest-catch-exceptions-no-ex-test_
vladlosev's avatar
vladlosev committed
272
273
      "${cxx_no_exception}"
      gtest_main_no_exception
Gennadiy Civil's avatar
 
Gennadiy Civil committed
274
      test/googletest-catch-exceptions-test_.cc)
vladlosev's avatar
vladlosev committed
275
276
  endif()

277
  cxx_executable_with_flags(
Gennadiy Civil's avatar
 
Gennadiy Civil committed
278
    googletest-catch-exceptions-ex-test_
279
280
    "${cxx_exception}"
    gtest_main
Gennadiy Civil's avatar
 
Gennadiy Civil committed
281
    test/googletest-catch-exceptions-test_.cc)
Gennadiy Civil's avatar
 
Gennadiy Civil committed
282
  py_test(googletest-catch-exceptions-test)
283

Gennadiy Civil's avatar
 
Gennadiy Civil committed
284
  cxx_executable(googletest-color-test_ test gtest)
Gennadiy Civil's avatar
 
Gennadiy Civil committed
285
  py_test(googletest-color-test)
286

Gennadiy Civil's avatar
 
Gennadiy Civil committed
287
  cxx_executable(googletest-env-var-test_ test gtest)
Gennadiy Civil's avatar
 
Gennadiy Civil committed
288
  py_test(googletest-env-var-test)
289

Gennadiy Civil's avatar
 
Gennadiy Civil committed
290
  cxx_executable(googletest-filter-unittest_ test gtest)
Gennadiy Civil's avatar
 
Gennadiy Civil committed
291
  py_test(googletest-filter-unittest)
292
293
294
295

  cxx_executable(gtest_help_test_ test gtest_main)
  py_test(gtest_help_test)

Gennadiy Civil's avatar
 
Gennadiy Civil committed
296
  cxx_executable(googletest-list-tests-unittest_ test gtest)
Gennadiy Civil's avatar
 
Gennadiy Civil committed
297
  py_test(googletest-list-tests-unittest)
298

Gennadiy Civil's avatar
 
Gennadiy Civil committed
299
  cxx_executable(googletest-output-test_ test gtest)
Gennadiy Civil's avatar
 
Gennadiy Civil committed
300
  py_test(googletest-output-test --no_stacktrace_support)
301

Gennadiy Civil's avatar
 
Gennadiy Civil committed
302
  cxx_executable(googletest-shuffle-test_ test gtest)
Gennadiy Civil's avatar
 
Gennadiy Civil committed
303
  py_test(googletest-shuffle-test)
304

vladlosev's avatar
vladlosev committed
305
306
  # MSVC 7.1 does not support STL with exceptions disabled.
  if (NOT MSVC OR MSVC_VERSION GREATER 1310)
Gennadiy Civil's avatar
 
Gennadiy Civil committed
307
308
    cxx_executable(googletest-throw-on-failure-test_ test gtest_no_exception)
    set_target_properties(googletest-throw-on-failure-test_
vladlosev's avatar
vladlosev committed
309
310
      PROPERTIES
      COMPILE_FLAGS "${cxx_no_exception}")
Gennadiy Civil's avatar
 
Gennadiy Civil committed
311
    py_test(googletest-throw-on-failure-test)
vladlosev's avatar
vladlosev committed
312
  endif()
313

Gennadiy Civil's avatar
 
Gennadiy Civil committed
314
  cxx_executable(googletest-uninitialized-test_ test gtest)
Gennadiy Civil's avatar
 
Gennadiy Civil committed
315
  py_test(googletest-uninitialized-test)
316
317
318
319

  cxx_executable(gtest_xml_outfile1_test_ test gtest_main)
  cxx_executable(gtest_xml_outfile2_test_ test gtest_main)
  py_test(gtest_xml_outfiles_test)
Gennadiy Civil's avatar
 
Gennadiy Civil committed
320
  py_test(googletest-json-outfiles-test)
321
322

  cxx_executable(gtest_xml_output_unittest_ test gtest)
323
  py_test(gtest_xml_output_unittest --no_stacktrace_support)
Gennadiy Civil's avatar
 
Gennadiy Civil committed
324
  py_test(googletest-json-output-unittest --no_stacktrace_support)
325
endif()