1########################################################################
2# CMake build script for Google Test.
3#
4# To run the tests for Google Test itself on Linux, use 'make test' or
5# ctest.  You can select which tests to run using 'ctest -R regex'.
6# For more options, run 'ctest --help'.
7
8# BUILD_SHARED_LIBS is a standard CMake variable, but we declare it here to
9# make it prominent in the GUI.
10option(BUILD_SHARED_LIBS "Build shared libraries (DLLs)." OFF)
11
12# When other libraries are using a shared version of runtime libraries,
13# Google Test also has to use one.
14option(
15  gtest_force_shared_crt
16  "Use shared (DLL) run-time lib even when Google Test is built as static lib."
17  OFF)
18
19option(gtest_build_tests "Build all of gtest's own tests." OFF)
20
21option(gtest_build_samples "Build gtest's sample programs." OFF)
22
23option(gtest_disable_pthreads "Disable uses of pthreads in gtest." OFF)
24
25# Defines pre_project_set_up_hermetic_build() and set_up_hermetic_build().
26include(cmake/hermetic_build.cmake OPTIONAL)
27
28if (COMMAND pre_project_set_up_hermetic_build)
29  pre_project_set_up_hermetic_build()
30endif()
31
32########################################################################
33#
34# Project-wide settings
35
36# Name of the project.
37#
38# CMake files in this project can refer to the root source directory
39# as ${gtest_SOURCE_DIR} and to the root binary directory as
40# ${gtest_BINARY_DIR}.
41# Language "C" is required for find_package(Threads).
42project(gtest CXX C)
43cmake_minimum_required(VERSION 2.6.2)
44
45if (COMMAND set_up_hermetic_build)
46  set_up_hermetic_build()
47endif()
48
49# Define helper functions and macros used by Google Test.
50include(cmake/internal_utils.cmake)
51
52config_compiler_and_linker()  # Defined in internal_utils.cmake.
53
54# Where Google Test's .h files can be found.
55include_directories(
56  ${gtest_SOURCE_DIR}/include
57  ${gtest_SOURCE_DIR})
58
59# Where Google Test's libraries can be found.
60link_directories(${gtest_BINARY_DIR}/src)
61
62########################################################################
63#
64# Defines the gtest & gtest_main libraries.  User tests should link
65# with one of them.
66
67# Google Test libraries.  We build them using more strict warnings than what
68# are used for other targets, to ensure that gtest can be compiled by a user
69# aggressive about warnings.
70cxx_library(gtest "${cxx_strict}" src/gtest-all.cc)
71cxx_library(gtest_main "${cxx_strict}" src/gtest_main.cc)
72target_link_libraries(gtest_main gtest)
73
74########################################################################
75#
76# Samples on how to link user tests with gtest or gtest_main.
77#
78# They are not built by default.  To build them, set the
79# gtest_build_samples option to ON.  You can do it by running ccmake
80# or specifying the -Dbuild_gtest_samples=ON flag when running cmake.
81
82if (gtest_build_samples)
83  cxx_executable(sample1_unittest samples gtest_main samples/sample1.cc)
84  cxx_executable(sample2_unittest samples gtest_main samples/sample2.cc)
85  cxx_executable(sample3_unittest samples gtest_main)
86  cxx_executable(sample4_unittest samples gtest_main samples/sample4.cc)
87  cxx_executable(sample5_unittest samples gtest_main samples/sample1.cc)
88  cxx_executable(sample6_unittest samples gtest_main)
89  cxx_executable(sample7_unittest samples gtest_main)
90  cxx_executable(sample8_unittest samples gtest_main)
91  cxx_executable(sample9_unittest samples gtest)
92  cxx_executable(sample10_unittest samples gtest)
93endif()
94
95########################################################################
96#
97# Google Test's own tests.
98#
99# You can skip this section if you aren't interested in testing
100# Google Test itself.
101#
102# The tests are not built by default.  To build them, set the
103# gtest_build_tests option to ON.  You can do it by running ccmake
104# or specifying the -Dgtest_build_tests=ON flag when running cmake.
105
106if (gtest_build_tests)
107  # This must be set in the root directory for the tests to be run by
108  # 'make test' or ctest.
109  enable_testing()
110
111  ############################################################
112  # C++ tests built with standard compiler flags.
113
114  cxx_test(gtest-death-test_test gtest_main)
115  cxx_test(gtest_environment_test gtest)
116  cxx_test(gtest-filepath_test gtest_main)
117  cxx_test(gtest-linked_ptr_test gtest_main)
118  cxx_test(gtest-listener_test gtest_main)
119  cxx_test(gtest_main_unittest gtest_main)
120  cxx_test(gtest-message_test gtest_main)
121  cxx_test(gtest_no_test_unittest gtest)
122  cxx_test(gtest-options_test gtest_main)
123  cxx_test(gtest-param-test_test gtest
124    test/gtest-param-test2_test.cc)
125  cxx_test(gtest-port_test gtest_main)
126  cxx_test(gtest_pred_impl_unittest gtest_main)
127  cxx_test(gtest-printers_test gtest_main)
128  cxx_test(gtest_prod_test gtest_main
129    test/production.cc)
130  cxx_test(gtest_repeat_test gtest)
131  cxx_test(gtest_sole_header_test gtest_main)
132  cxx_test(gtest_stress_test gtest)
133  cxx_test(gtest-test-part_test gtest_main)
134  cxx_test(gtest_throw_on_failure_ex_test gtest)
135  cxx_test(gtest-typed-test_test gtest_main
136    test/gtest-typed-test2_test.cc)
137  cxx_test(gtest_unittest gtest_main)
138  cxx_test(gtest-unittest-api_test gtest)
139
140  ############################################################
141  # C++ tests built with non-standard compiler flags.
142
143  # MSVC 7.1 does not support STL with exceptions disabled.
144  if (NOT MSVC OR MSVC_VERSION GREATER 1310)
145    cxx_library(gtest_no_exception "${cxx_no_exception}"
146      src/gtest-all.cc)
147    cxx_library(gtest_main_no_exception "${cxx_no_exception}"
148      src/gtest-all.cc src/gtest_main.cc)
149  endif()
150  cxx_library(gtest_main_no_rtti "${cxx_no_rtti}"
151    src/gtest-all.cc src/gtest_main.cc)
152
153  cxx_test_with_flags(gtest-death-test_ex_nocatch_test
154    "${cxx_exception} -DGTEST_ENABLE_CATCH_EXCEPTIONS_=0"
155    gtest test/gtest-death-test_ex_test.cc)
156  cxx_test_with_flags(gtest-death-test_ex_catch_test
157    "${cxx_exception} -DGTEST_ENABLE_CATCH_EXCEPTIONS_=1"
158    gtest test/gtest-death-test_ex_test.cc)
159
160  cxx_test_with_flags(gtest_no_rtti_unittest "${cxx_no_rtti}"
161    gtest_main_no_rtti test/gtest_unittest.cc)
162
163  cxx_shared_library(gtest_dll "${cxx_default}"
164    src/gtest-all.cc src/gtest_main.cc)
165
166  cxx_executable_with_flags(gtest_dll_test_ "${cxx_default}"
167    gtest_dll test/gtest_all_test.cc)
168  set_target_properties(gtest_dll_test_
169                        PROPERTIES
170                        COMPILE_DEFINITIONS "GTEST_LINKED_AS_SHARED_LIBRARY=1")
171
172  if (NOT MSVC OR NOT MSVC_VERSION EQUAL 1600)
173    # The C++ Standard specifies tuple_element<int, class>.
174    # Yet MSVC 10's <utility> declares tuple_element<size_t, class>.
175    # That declaration conflicts with our own standard-conforming
176    # tuple implementation.  Therefore using our own tuple with
177    # MSVC 10 doesn't compile.
178    cxx_library(gtest_main_use_own_tuple "${cxx_use_own_tuple}"
179      src/gtest-all.cc src/gtest_main.cc)
180
181    cxx_test_with_flags(gtest-tuple_test "${cxx_use_own_tuple}"
182      gtest_main_use_own_tuple test/gtest-tuple_test.cc)
183
184    cxx_test_with_flags(gtest_use_own_tuple_test "${cxx_use_own_tuple}"
185      gtest_main_use_own_tuple
186      test/gtest-param-test_test.cc test/gtest-param-test2_test.cc)
187  endif()
188
189  ############################################################
190  # Python tests.
191
192  cxx_executable(gtest_break_on_failure_unittest_ test gtest)
193  py_test(gtest_break_on_failure_unittest)
194
195  # MSVC 7.1 does not support STL with exceptions disabled.
196  if (NOT MSVC OR MSVC_VERSION GREATER 1310)
197    cxx_executable_with_flags(
198      gtest_catch_exceptions_no_ex_test_
199      "${cxx_no_exception}"
200      gtest_main_no_exception
201      test/gtest_catch_exceptions_test_.cc)
202  endif()
203
204  cxx_executable_with_flags(
205    gtest_catch_exceptions_ex_test_
206    "${cxx_exception}"
207    gtest_main
208    test/gtest_catch_exceptions_test_.cc)
209  py_test(gtest_catch_exceptions_test)
210
211  cxx_executable(gtest_color_test_ test gtest)
212  py_test(gtest_color_test)
213
214  cxx_executable(gtest_env_var_test_ test gtest)
215  py_test(gtest_env_var_test)
216
217  cxx_executable(gtest_filter_unittest_ test gtest)
218  py_test(gtest_filter_unittest)
219
220  cxx_executable(gtest_help_test_ test gtest_main)
221  py_test(gtest_help_test)
222
223  cxx_executable(gtest_list_tests_unittest_ test gtest)
224  py_test(gtest_list_tests_unittest)
225
226  cxx_executable(gtest_output_test_ test gtest)
227  py_test(gtest_output_test)
228
229  cxx_executable(gtest_shuffle_test_ test gtest)
230  py_test(gtest_shuffle_test)
231
232  # MSVC 7.1 does not support STL with exceptions disabled.
233  if (NOT MSVC OR MSVC_VERSION GREATER 1310)
234    cxx_executable(gtest_throw_on_failure_test_ test gtest_no_exception)
235    set_target_properties(gtest_throw_on_failure_test_
236      PROPERTIES
237      COMPILE_FLAGS "${cxx_no_exception}")
238    py_test(gtest_throw_on_failure_test)
239  endif()
240
241  cxx_executable(gtest_uninitialized_test_ test gtest)
242  py_test(gtest_uninitialized_test)
243
244  cxx_executable(gtest_xml_outfile1_test_ test gtest_main)
245  cxx_executable(gtest_xml_outfile2_test_ test gtest_main)
246  py_test(gtest_xml_outfiles_test)
247
248  cxx_executable(gtest_xml_output_unittest_ test gtest)
249  py_test(gtest_xml_output_unittest)
250endif()
251