1# Defines functions and macros useful for building Google Test and
2# Google Mock.
3#
4# Note:
5#
6# - This file will be run twice when building Google Mock (once via
7#   Google Test's CMakeLists.txt, and once via Google Mock's).
8#   Therefore it shouldn't have any side effects other than defining
9#   the functions and macros.
10#
11# - The functions/macros defined in this file may depend on Google
12#   Test and Google Mock's option() definitions, and thus must be
13#   called *after* the options have been defined.
14
15# Tweaks CMake's default compiler/linker settings to suit Google Test's needs.
16#
17# This must be a macro(), as inside a function string() can only
18# update variables in the function scope.
19macro(fix_default_compiler_settings_)
20  if (MSVC)
21    # For MSVC, CMake sets certain flags to defaults we want to override.
22    # This replacement code is taken from sample in the CMake Wiki at
23    # http://www.cmake.org/Wiki/CMake_FAQ#Dynamic_Replace.
24    foreach (flag_var
25             CMAKE_CXX_FLAGS CMAKE_CXX_FLAGS_DEBUG CMAKE_CXX_FLAGS_RELEASE
26             CMAKE_CXX_FLAGS_MINSIZEREL CMAKE_CXX_FLAGS_RELWITHDEBINFO)
27      if (NOT BUILD_SHARED_LIBS AND NOT gtest_force_shared_crt)
28        # When Google Test is built as a shared library, it should also use
29        # shared runtime libraries.  Otherwise, it may end up with multiple
30        # copies of runtime library data in different modules, resulting in
31        # hard-to-find crashes. When it is built as a static library, it is
32        # preferable to use CRT as static libraries, as we don't have to rely
33        # on CRT DLLs being available. CMake always defaults to using shared
34        # CRT libraries, so we override that default here.
35        string(REPLACE "/MD" "-MT" ${flag_var} "${${flag_var}}")
36      endif()
37
38      # We prefer more strict warning checking for building Google Test.
39      # Replaces /W3 with /W4 in defaults.
40      string(REPLACE "/W3" "/W4" ${flag_var} "${${flag_var}}")
41    endforeach()
42  endif()
43endmacro()
44
45# Defines the compiler/linker flags used to build Google Test and
46# Google Mock.  You can tweak these definitions to suit your need.  A
47# variable's value is empty before it's explicitly assigned to.
48macro(config_compiler_and_linker)
49  if (NOT gtest_disable_pthreads)
50    # Defines CMAKE_USE_PTHREADS_INIT and CMAKE_THREAD_LIBS_INIT.
51    find_package(Threads)
52  endif()
53
54  fix_default_compiler_settings_()
55  if (MSVC)
56    # Newlines inside flags variables break CMake's NMake generator.
57    # TODO(vladl@google.com): Add -RTCs and -RTCu to debug builds.
58    set(cxx_base_flags "-GS -W4 -WX -wd4251 -wd4275 -nologo -J -Zi")
59    if (MSVC_VERSION LESS 1400)  # 1400 is Visual Studio 2005
60      # Suppress spurious warnings MSVC 7.1 sometimes issues.
61      # Forcing value to bool.
62      set(cxx_base_flags "${cxx_base_flags} -wd4800")
63      # Copy constructor and assignment operator could not be generated.
64      set(cxx_base_flags "${cxx_base_flags} -wd4511 -wd4512")
65      # Compatibility warnings not applicable to Google Test.
66      # Resolved overload was found by argument-dependent lookup.
67      set(cxx_base_flags "${cxx_base_flags} -wd4675")
68    endif()
69    if (MSVC_VERSION LESS 1500)  # 1500 is Visual Studio 2008
70      # Conditional expression is constant.
71      # When compiling with /W4, we get several instances of C4127
72      # (Conditional expression is constant). In our code, we disable that
73      # warning on a case-by-case basis. However, on Visual Studio 2005,
74      # the warning fires on std::list. Therefore on that compiler and earlier,
75      # we disable the warning project-wide.
76      set(cxx_base_flags "${cxx_base_flags} -wd4127")
77    endif()
78    if (NOT (MSVC_VERSION LESS 1700))  # 1700 is Visual Studio 2012.
79      # Suppress "unreachable code" warning on VS 2012 and later.
80      # http://stackoverflow.com/questions/3232669 explains the issue.
81      set(cxx_base_flags "${cxx_base_flags} -wd4702")
82    endif()
83
84    set(cxx_base_flags "${cxx_base_flags} -D_UNICODE -DUNICODE -DWIN32 -D_WIN32")
85    set(cxx_base_flags "${cxx_base_flags} -DSTRICT -DWIN32_LEAN_AND_MEAN")
86    set(cxx_exception_flags "-EHsc -D_HAS_EXCEPTIONS=1")
87    set(cxx_no_exception_flags "-D_HAS_EXCEPTIONS=0")
88    set(cxx_no_rtti_flags "-GR-")
89  elseif (CMAKE_COMPILER_IS_GNUCXX)
90    set(cxx_base_flags "-Wall -Wshadow")
91    set(cxx_exception_flags "-fexceptions")
92    set(cxx_no_exception_flags "-fno-exceptions")
93    # Until version 4.3.2, GCC 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 "-fno-rtti -DGTEST_HAS_RTTI=0")
97    set(cxx_strict_flags
98      "-Wextra -Wno-unused-parameter -Wno-missing-field-initializers")
99  elseif (CMAKE_CXX_COMPILER_ID STREQUAL "SunPro")
100    set(cxx_exception_flags "-features=except")
101    # Sun Pro doesn't provide macros to indicate whether exceptions and
102    # RTTI are enabled, so we define GTEST_HAS_* explicitly.
103    set(cxx_no_exception_flags "-features=no%except -DGTEST_HAS_EXCEPTIONS=0")
104    set(cxx_no_rtti_flags "-features=no%rtti -DGTEST_HAS_RTTI=0")
105  elseif (CMAKE_CXX_COMPILER_ID STREQUAL "VisualAge" OR
106      CMAKE_CXX_COMPILER_ID STREQUAL "XL")
107    # CMake 2.8 changes Visual Age's compiler ID to "XL".
108    set(cxx_exception_flags "-qeh")
109    set(cxx_no_exception_flags "-qnoeh")
110    # Until version 9.0, Visual Age doesn't define a macro to indicate
111    # whether RTTI is enabled.  Therefore we define GTEST_HAS_RTTI
112    # explicitly.
113    set(cxx_no_rtti_flags "-qnortti -DGTEST_HAS_RTTI=0")
114  elseif (CMAKE_CXX_COMPILER_ID STREQUAL "HP")
115    set(cxx_base_flags "-AA -mt")
116    set(cxx_exception_flags "-DGTEST_HAS_EXCEPTIONS=1")
117    set(cxx_no_exception_flags "+noeh -DGTEST_HAS_EXCEPTIONS=0")
118    # RTTI can not be disabled in HP aCC compiler.
119    set(cxx_no_rtti_flags "")
120  endif()
121
122  if (CMAKE_USE_PTHREADS_INIT)  # The pthreads library is available and allowed.
123    set(cxx_base_flags "${cxx_base_flags} -DGTEST_HAS_PTHREAD=1")
124  else()
125    set(cxx_base_flags "${cxx_base_flags} -DGTEST_HAS_PTHREAD=0")
126  endif()
127
128  # For building gtest's own tests and samples.
129  set(cxx_exception "${CMAKE_CXX_FLAGS} ${cxx_base_flags} ${cxx_exception_flags}")
130  set(cxx_no_exception
131    "${CMAKE_CXX_FLAGS} ${cxx_base_flags} ${cxx_no_exception_flags}")
132  set(cxx_default "${cxx_exception}")
133  set(cxx_no_rtti "${cxx_default} ${cxx_no_rtti_flags}")
134  set(cxx_use_own_tuple "${cxx_default} -DGTEST_USE_OWN_TR1_TUPLE=1")
135
136  # For building the gtest libraries.
137  set(cxx_strict "${cxx_default} ${cxx_strict_flags}")
138endmacro()
139
140# Defines the gtest & gtest_main libraries.  User tests should link
141# with one of them.
142function(cxx_library_with_type name type cxx_flags)
143  # type can be either STATIC or SHARED to denote a static or shared library.
144  # ARGN refers to additional arguments after 'cxx_flags'.
145  add_library(${name} ${type} ${ARGN})
146  set_target_properties(${name}
147    PROPERTIES
148    COMPILE_FLAGS "${cxx_flags}")
149  if (BUILD_SHARED_LIBS OR type STREQUAL "SHARED")
150    set_target_properties(${name}
151      PROPERTIES
152      COMPILE_DEFINITIONS "GTEST_CREATE_SHARED_LIBRARY=1")
153  endif()
154  if (CMAKE_USE_PTHREADS_INIT)
155    target_link_libraries(${name} ${CMAKE_THREAD_LIBS_INIT})
156  endif()
157endfunction()
158
159########################################################################
160#
161# Helper functions for creating build targets.
162
163function(cxx_shared_library name cxx_flags)
164  cxx_library_with_type(${name} SHARED "${cxx_flags}" ${ARGN})
165endfunction()
166
167function(cxx_library name cxx_flags)
168  cxx_library_with_type(${name} "" "${cxx_flags}" ${ARGN})
169endfunction()
170
171# cxx_executable_with_flags(name cxx_flags libs srcs...)
172#
173# creates a named C++ executable that depends on the given libraries and
174# is built from the given source files with the given compiler flags.
175function(cxx_executable_with_flags name cxx_flags libs)
176  add_executable(${name} ${ARGN})
177  if (cxx_flags)
178    set_target_properties(${name}
179      PROPERTIES
180      COMPILE_FLAGS "${cxx_flags}")
181  endif()
182  if (BUILD_SHARED_LIBS)
183    set_target_properties(${name}
184      PROPERTIES
185      COMPILE_DEFINITIONS "GTEST_LINKED_AS_SHARED_LIBRARY=1")
186  endif()
187  # To support mixing linking in static and dynamic libraries, link each
188  # library in with an extra call to target_link_libraries.
189  foreach (lib "${libs}")
190    target_link_libraries(${name} ${lib})
191  endforeach()
192endfunction()
193
194# cxx_executable(name dir lib srcs...)
195#
196# creates a named target that depends on the given libs and is built
197# from the given source files.  dir/name.cc is implicitly included in
198# the source file list.
199function(cxx_executable name dir libs)
200  cxx_executable_with_flags(
201    ${name} "${cxx_default}" "${libs}" "${dir}/${name}.cc" ${ARGN})
202endfunction()
203
204# Sets PYTHONINTERP_FOUND and PYTHON_EXECUTABLE.
205find_package(PythonInterp)
206
207# cxx_test_with_flags(name cxx_flags libs srcs...)
208#
209# creates a named C++ test that depends on the given libs and is built
210# from the given source files with the given compiler flags.
211function(cxx_test_with_flags name cxx_flags libs)
212  cxx_executable_with_flags(${name} "${cxx_flags}" "${libs}" ${ARGN})
213  add_test(${name} ${name})
214endfunction()
215
216# cxx_test(name libs srcs...)
217#
218# creates a named test target that depends on the given libs and is
219# built from the given source files.  Unlike cxx_test_with_flags,
220# test/name.cc is already implicitly included in the source file list.
221function(cxx_test name libs)
222  cxx_test_with_flags("${name}" "${cxx_default}" "${libs}"
223    "test/${name}.cc" ${ARGN})
224endfunction()
225
226# py_test(name)
227#
228# creates a Python test with the given name whose main module is in
229# test/name.py.  It does nothing if Python is not installed.
230function(py_test name)
231  # We are not supporting Python tests on Linux yet as they consider
232  # all Linux environments to be google3 and try to use google3 features.
233  if (PYTHONINTERP_FOUND)
234    # ${CMAKE_BINARY_DIR} is known at configuration time, so we can
235    # directly bind it from cmake. ${CTEST_CONFIGURATION_TYPE} is known
236    # only at ctest runtime (by calling ctest -c <Configuration>), so
237    # we have to escape $ to delay variable substitution here.
238    add_test(${name}
239      ${PYTHON_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/test/${name}.py
240          --build_dir=${CMAKE_CURRENT_BINARY_DIR}/\${CTEST_CONFIGURATION_TYPE})
241  endif()
242endfunction()
243