• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1########################################################################
2# Experimental CMake build script for Google Test.
3#
4# Consider this a prototype.  It will change drastically.  For now,
5# this is only for people on the cutting edge.
6#
7# To run the tests for Google Test itself on Linux, use 'make test' or
8# ctest.  You can select which tests to run using 'ctest -R regex'.
9# For more options, run 'ctest --help'.
10
11# For hermetic builds, we may need to tell CMake to use compiler in a
12# specific location.
13if (gtest_compiler)
14  include(CMakeForceCompiler)
15  cmake_force_c_compiler("${gtest_compiler}" "")
16  cmake_force_cxx_compiler("${gtest_compiler}" "")
17endif()
18
19########################################################################
20#
21# Project-wide settings
22
23# Name of the project.
24#
25# CMake files in this project can refer to the root source directory
26# as ${gtest_SOURCE_DIR} and to the root binary directory as
27# ${gtest_BINARY_DIR}.
28# Language "C" is required for find_package(Threads).
29project(gtest CXX C)
30cmake_minimum_required(VERSION 2.6.4)
31
32if (MSVC)
33  # For MSVC, CMake sets certain flags to defaults we want to override.
34  # This replacement code is taken from sample in the CMake Wiki at
35  # http://www.cmake.org/Wiki/CMake_FAQ#Dynamic_Replace.
36  foreach (flag_var
37           CMAKE_CXX_FLAGS CMAKE_CXX_FLAGS_DEBUG CMAKE_CXX_FLAGS_RELEASE
38           CMAKE_CXX_FLAGS_MINSIZEREL CMAKE_CXX_FLAGS_RELWITHDEBINFO)
39    # In hermetic build environments, tests may not have access to MS runtime
40    # DLLs, so this replaces /MD (CRT libraries in DLLs) with /MT (static CRT
41    # libraries).
42    string(REPLACE "/MD" "-MT" ${flag_var} "${${flag_var}}")
43    # We prefer more strict warning checking for building Google Test.
44    # Replaces /W3 with /W4 in defaults.
45    string(REPLACE "/W3" "-W4" ${flag_var} "${${flag_var}}")
46  endforeach()
47endif()
48
49# Where gtest's .h files can be found.
50include_directories(
51  ${gtest_SOURCE_DIR}/include
52  ${gtest_SOURCE_DIR})
53
54# Where the gtest libraries can be found.
55link_directories(
56  ${gtest_BINARY_DIR}/src)
57
58# Defines CMAKE_USE_PTHREADS_INIT and CMAKE_THREAD_LIBS_INIT.
59find_package(Threads)
60
61# Defines the compiler/linker flags used to build gtest.  You can
62# tweak these definitions to suit your need.  A variable's value is
63# empty before it's explicitly assigned to.
64
65if (MSVC)
66  # Newlines inside flags variables break CMake's NMake generator.
67  set(cxx_base_flags "-GS -W4 -WX -wd4275 -nologo -J -Zi")
68  set(cxx_base_flags "${cxx_base_flags} -D_UNICODE -DUNICODE -DWIN32 -D_WIN32")
69  set(cxx_base_flags "${cxx_base_flags} -DSTRICT -DWIN32_LEAN_AND_MEAN")
70  set(cxx_exception_flags "-EHsc -D_HAS_EXCEPTIONS=1")
71  set(cxx_no_exception_flags "-D_HAS_EXCEPTIONS=0")
72  set(cxx_no_rtti_flags "-GR-")
73elseif (CMAKE_COMPILER_IS_GNUCXX)
74  set(cxx_base_flags "-Wall -Wshadow")
75  set(cxx_exception_flags "-fexceptions")
76  set(cxx_no_exception_flags "-fno-exceptions")
77  # Until version 4.3.2, GCC doesn't define a macro to indicate
78  # whether RTTI is enabled.  Therefore we define GTEST_HAS_RTTI
79  # explicitly.
80  set(cxx_no_rtti_flags "-fno-rtti -DGTEST_HAS_RTTI=0")
81  set(cxx_strict_flags "-Wextra")
82elseif (CMAKE_CXX_COMPILER_ID STREQUAL "SunPro")
83  set(cxx_exception_flags "-features=except")
84  # Sun Pro doesn't provide macros to indicate whether exceptions and
85  # RTTI are enabled, so we define GTEST_HAS_* explicitly.
86  set(cxx_no_exception_flags "-features=no%except -DGTEST_HAS_EXCEPTIONS=0")
87  set(cxx_no_rtti_flags "-features=no%rtti -DGTEST_HAS_RTTI=0")
88elseif (CMAKE_CXX_COMPILER_ID STREQUAL "VisualAge" OR
89        CMAKE_CXX_COMPILER_ID STREQUAL "XL")
90  # CMake 2.8 changes Visual Age's compiler ID to "XL".
91  set(cxx_exception_flags "-qeh")
92  set(cxx_no_exception_flags "-qnoeh")
93  # Until version 9.0, Visual Age doesn't define a macro to indicate
94  # whether RTTI is enabled.  Therefore we define GTEST_HAS_RTTI
95  # explicitly.
96  set(cxx_no_rtti_flags "-qnortti -DGTEST_HAS_RTTI=0")
97endif()
98
99if (CMAKE_USE_PTHREADS_INIT)  # The pthreads library is available.
100  set(cxx_base_flags "${cxx_base_flags} -DGTEST_HAS_PTHREAD=1")
101endif()
102
103# For building gtest's own tests and samples.
104set(cxx_exception "${CMAKE_CXX_FLAGS} ${cxx_base_flags} ${cxx_exception_flags}")
105set(cxx_no_exception
106    "${CMAKE_CXX_FLAGS} ${cxx_base_flags} ${cxx_no_exception_flags}")
107set(cxx_default "${cxx_exception}")
108set(cxx_no_rtti "${cxx_default} ${cxx_no_rtti_flags}")
109set(cxx_use_own_tuple "${cxx_default} -DGTEST_USE_OWN_TR1_TUPLE=1")
110
111# For building the gtest libraries.
112set(cxx_strict "${cxx_default} ${cxx_strict_flags}")
113
114########################################################################
115#
116# Defines the gtest & gtest_main libraries.  User tests should link
117# with one of them.
118function(cxx_library_with_type name type cxx_flags)
119  # type can be either STATIC or SHARED to denote a static or shared library.
120  # ARGN refers to additional arguments after 'cxx_flags'.
121  add_library(${name} ${type} ${ARGN})
122  set_target_properties(${name}
123    PROPERTIES
124    COMPILE_FLAGS "${cxx_flags}")
125    if (CMAKE_USE_PTHREADS_INIT)
126      target_link_libraries(${name} ${CMAKE_THREAD_LIBS_INIT})
127    endif()
128endfunction()
129
130function(cxx_static_library name cxx_flags)
131  cxx_library_with_type(${name} STATIC "${cxx_flags}" ${ARGN})
132endfunction()
133
134function(cxx_shared_library name cxx_flags)
135  cxx_library_with_type(${name} SHARED "${cxx_flags}" ${ARGN})
136endfunction()
137
138function(cxx_library name cxx_flags)
139  # TODO(vladl@google.com): Make static/shared a user option.
140  cxx_static_library(${name} "${cxx_flags}" ${ARGN})
141endfunction()
142
143# Static versions of Google Test libraries.  We build them using more
144# strict warnings than what are used for other targets, to ensure that
145# gtest can be compiled by a user aggressive about warnings.
146cxx_static_library(gtest "${cxx_strict}" src/gtest-all.cc)
147cxx_static_library(gtest_main "${cxx_strict}" src/gtest_main.cc)
148target_link_libraries(gtest_main gtest)
149
150########################################################################
151#
152# Samples on how to link user tests with gtest or gtest_main.
153#
154# They are not built by default.  To build them, set the
155# build_gtest_samples option to ON.  You can do it by running ccmake
156# or specifying the -Dbuild_gtest_samples=ON flag when running cmake.
157
158option(build_gtest_samples "Build gtest's sample programs." OFF)
159
160# cxx_executable_with_flags(name cxx_flags lib srcs...)
161#
162# creates a named C++ executable that depends on the given library and
163# is built from the given source files with the given compiler flags.
164function(cxx_executable_with_flags name cxx_flags lib)
165  add_executable(${name} ${ARGN})
166  if (cxx_flags)
167    set_target_properties(${name}
168      PROPERTIES
169      COMPILE_FLAGS "${cxx_flags}")
170  endif()
171  target_link_libraries(${name} ${lib})
172endfunction()
173
174# cxx_executable(name dir lib srcs...)
175#
176# creates a named target that depends on the given lib and is built
177# from the given source files.  dir/name.cc is implicitly included in
178# the source file list.
179function(cxx_executable name dir lib)
180  cxx_executable_with_flags(
181    ${name} "${cxx_default}" ${lib} "${dir}/${name}.cc" ${ARGN})
182endfunction()
183
184if (build_gtest_samples)
185  cxx_executable(sample1_unittest samples gtest_main samples/sample1.cc)
186  cxx_executable(sample2_unittest samples gtest_main samples/sample2.cc)
187  cxx_executable(sample3_unittest samples gtest_main)
188  cxx_executable(sample4_unittest samples gtest_main samples/sample4.cc)
189  cxx_executable(sample5_unittest samples gtest_main samples/sample1.cc)
190  cxx_executable(sample6_unittest samples gtest_main)
191  cxx_executable(sample7_unittest samples gtest_main)
192  cxx_executable(sample8_unittest samples gtest_main)
193  cxx_executable(sample9_unittest samples gtest)
194  cxx_executable(sample10_unittest samples gtest)
195endif()
196
197########################################################################
198#
199# Google Test's own tests.
200#
201# You can skip this section if you aren't interested in testing
202# Google Test itself.
203#
204# Most of the tests are not built by default.  To build them, set the
205# build_all_gtest_tests option to ON.  You can do it by running ccmake
206# or specifying the -Dbuild_all_gtest_tests=ON flag when running cmake.
207
208option(build_all_gtest_tests "Build all of gtest's own tests." OFF)
209
210# This must be set in the root directory for the tests to be run by
211# 'make test' or ctest.
212enable_testing()
213
214# Sets PYTHONINTERP_FOUND and PYTHON_EXECUTABLE.
215find_package(PythonInterp)
216
217############################################################
218# C++ tests built with standard compiler flags.
219
220# cxx_test_with_flags(name cxx_flags libs srcs...)
221#
222# creates a named C++ test that depends on the given libs and is built
223# from the given source files with the given compiler flags.
224function(cxx_test_with_flags name cxx_flags libs)
225  add_executable(${name} ${ARGN})
226  set_target_properties(${name}
227    PROPERTIES
228    COMPILE_FLAGS "${cxx_flags}")
229  # To support mixing linking in static and dynamic libraries, link each
230  # library in with an extra call to target_link_libraries.
231  foreach (lib "${libs}")
232    target_link_libraries(${name} ${lib})
233  endforeach()
234  add_test(${name} ${name})
235endfunction()
236
237# cxx_test(name libs srcs...)
238#
239# creates a named test target that depends on the given libs and is
240# built from the given source files.  Unlike cxx_test_with_flags,
241# test/name.cc is already implicitly included in the source file list.
242function(cxx_test name libs)
243  cxx_test_with_flags("${name}" "${cxx_default}" "${libs}"
244    "test/${name}.cc" ${ARGN})
245endfunction()
246
247cxx_test(gtest_unittest gtest_main)
248
249if (build_all_gtest_tests)
250  cxx_test(gtest-death-test_test gtest_main)
251  cxx_test(gtest_environment_test gtest)
252  cxx_test(gtest-filepath_test gtest_main)
253  cxx_test(gtest-linked_ptr_test gtest_main)
254  cxx_test(gtest-listener_test gtest_main)
255  cxx_test(gtest_main_unittest gtest_main)
256  cxx_test(gtest-message_test gtest_main)
257  cxx_test(gtest_no_test_unittest gtest)
258  cxx_test(gtest-options_test gtest_main)
259  cxx_test(gtest-param-test_test gtest
260    test/gtest-param-test2_test.cc)
261  cxx_test(gtest-port_test gtest_main)
262  cxx_test(gtest_pred_impl_unittest gtest_main)
263  cxx_test(gtest_prod_test gtest_main
264    test/production.cc)
265  cxx_test(gtest_repeat_test gtest)
266  cxx_test(gtest_sole_header_test gtest_main)
267  cxx_test(gtest_stress_test gtest)
268  cxx_test(gtest-test-part_test gtest_main)
269  cxx_test(gtest_throw_on_failure_ex_test gtest)
270  cxx_test(gtest-typed-test_test gtest_main
271    test/gtest-typed-test2_test.cc)
272  cxx_test(gtest-unittest-api_test gtest)
273endif()
274
275############################################################
276# C++ tests built with non-standard compiler flags.
277
278if (build_all_gtest_tests)
279  cxx_library(gtest_no_exception "${cxx_no_exception}"
280    src/gtest-all.cc)
281  cxx_library(gtest_main_no_rtti "${cxx_no_rtti}"
282    src/gtest-all.cc src/gtest_main.cc)
283
284  cxx_test_with_flags(gtest_no_rtti_unittest "${cxx_no_rtti}"
285    gtest_main_no_rtti test/gtest_unittest.cc)
286
287  set(cxx_use_shared_gtest "${cxx_default} -DGTEST_LINKED_AS_SHARED_LIBRARY=1")
288  set(cxx_build_shared_gtest "${cxx_default} -DGTEST_CREATE_SHARED_LIBRARY=1")
289  if (MSVC)
290    # Disables the "class 'X' needs to have dll-interface to be used
291    # by clients of class 'Y'" warning. This particularly concerns generic
292    # classes like vector that MS doesn't mark as exported.
293    set(cxx_use_shared_gtest "${cxx_use_shared_gtest} -wd4251")
294    set(cxx_build_shared_gtest "${cxx_build_shared_gtest} -wd4251")
295  endif()
296
297  cxx_shared_library(gtest_dll "${cxx_build_shared_gtest}"
298    src/gtest-all.cc)
299
300  # TODO(vladl): This and the next tests may not run in the hermetic
301  # environment on Windows. Re-evaluate and possibly make them
302  # platform-conditional after implementing hermetic builds.
303  cxx_executable_with_flags(gtest_dll_test_ "${cxx_use_shared_gtest}"
304    gtest_dll test/gtest_all_test.cc)
305
306  if (NOT(MSVC AND (MSVC_VERSION EQUAL 1600)))
307    # The C++ Standard specifies tuple_element<int, class>.
308    # Yet MSVC 10's <utility> declares tuple_element<size_t, class>.
309    # That declaration conflicts with our own standard-conforming
310    # tuple implementation.  Therefore using our own tuple with
311    # MSVC 10 doesn't compile.
312    cxx_library(gtest_main_use_own_tuple "${cxx_use_own_tuple}"
313      src/gtest-all.cc src/gtest_main.cc)
314
315    cxx_test_with_flags(gtest-tuple_test "${cxx_use_own_tuple}"
316      gtest_main_use_own_tuple test/gtest-tuple_test.cc)
317
318    cxx_test_with_flags(gtest_use_own_tuple_test "${cxx_use_own_tuple}"
319      gtest_main_use_own_tuple
320      test/gtest-param-test_test.cc test/gtest-param-test2_test.cc)
321  endif()
322
323endif()
324
325############################################################
326# Python tests.
327
328# py_test(name)
329#
330# creates a Python test with the given name whose main module is in
331# test/name.py.  It does nothing if Python is not installed.
332function(py_test name)
333  if (PYTHONINTERP_FOUND)
334    # ${gtest_BINARY_DIR} is known at configuration time, so we can
335    # directly bind it from cmake. ${CTEST_CONFIGURATION_TYPE} is known
336    # only at ctest runtime (by calling ctest -c <Configuration>), so
337    # we have to escape $ to delay variable substitution here.
338    add_test(${name}
339      ${PYTHON_EXECUTABLE} ${gtest_SOURCE_DIR}/test/${name}.py
340          --gtest_build_dir=${gtest_BINARY_DIR}/\${CTEST_CONFIGURATION_TYPE})
341  endif()
342endfunction()
343
344if (build_all_gtest_tests)
345  cxx_executable(gtest_break_on_failure_unittest_ test gtest)
346  py_test(gtest_break_on_failure_unittest)
347
348  cxx_executable(gtest_color_test_ test gtest)
349  py_test(gtest_color_test)
350
351  cxx_executable(gtest_env_var_test_ test gtest)
352  py_test(gtest_env_var_test)
353
354  cxx_executable(gtest_filter_unittest_ test gtest)
355  py_test(gtest_filter_unittest)
356
357  cxx_executable(gtest_help_test_ test gtest_main)
358  py_test(gtest_help_test)
359
360  cxx_executable(gtest_list_tests_unittest_ test gtest)
361  py_test(gtest_list_tests_unittest)
362
363  cxx_executable(gtest_output_test_ test gtest)
364  py_test(gtest_output_test)
365
366  cxx_executable(gtest_shuffle_test_ test gtest)
367  py_test(gtest_shuffle_test)
368
369  cxx_executable(gtest_throw_on_failure_test_ test gtest_no_exception)
370  set_target_properties(gtest_throw_on_failure_test_
371    PROPERTIES
372    COMPILE_FLAGS "${cxx_no_exception}")
373  py_test(gtest_throw_on_failure_test)
374
375  cxx_executable(gtest_uninitialized_test_ test gtest)
376  py_test(gtest_uninitialized_test)
377
378  cxx_executable(gtest_xml_outfile1_test_ test gtest_main)
379  cxx_executable(gtest_xml_outfile2_test_ test gtest_main)
380  py_test(gtest_xml_outfiles_test)
381
382  cxx_executable(gtest_xml_output_unittest_ test gtest)
383  py_test(gtest_xml_output_unittest)
384endif()
385