• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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
15if (POLICY CMP0054)
16  cmake_policy(SET CMP0054 NEW)
17endif (POLICY CMP0054)
18
19# Tweaks CMake's default compiler/linker settings to suit Google Test's needs.
20#
21# This must be a macro(), as inside a function string() can only
22# update variables in the function scope.
23macro(fix_default_compiler_settings_)
24  if (CMAKE_CXX_COMPILER_ID MATCHES "MSVC|Clang")
25    # For MSVC and Clang, CMake sets certain flags to defaults we want to
26    # override.
27    # This replacement code is taken from sample in the CMake Wiki at
28    # https://gitlab.kitware.com/cmake/community/wikis/FAQ#dynamic-replace.
29    foreach (flag_var
30             CMAKE_C_FLAGS CMAKE_C_FLAGS_DEBUG CMAKE_C_FLAGS_RELEASE
31             CMAKE_C_FLAGS_MINSIZEREL CMAKE_C_FLAGS_RELWITHDEBINFO
32             CMAKE_CXX_FLAGS CMAKE_CXX_FLAGS_DEBUG CMAKE_CXX_FLAGS_RELEASE
33             CMAKE_CXX_FLAGS_MINSIZEREL CMAKE_CXX_FLAGS_RELWITHDEBINFO)
34      if (NOT BUILD_SHARED_LIBS AND NOT gtest_force_shared_crt)
35        # When Google Test is built as a shared library, it should also use
36        # shared runtime libraries.  Otherwise, it may end up with multiple
37        # copies of runtime library data in different modules, resulting in
38        # hard-to-find crashes. When it is built as a static library, it is
39        # preferable to use CRT as static libraries, as we don't have to rely
40        # on CRT DLLs being available. CMake always defaults to using shared
41        # CRT libraries, so we override that default here.
42        string(REPLACE "/MD" "-MT" ${flag_var} "${${flag_var}}")
43
44        # When using Ninja with Clang, static builds pass -D_DLL on Windows.
45        # This is incorrect and should not happen, so we fix that here.
46        string(REPLACE "-D_DLL" "" ${flag_var} "${${flag_var}}")
47      endif()
48
49      # We prefer more strict warning checking for building Google Test.
50      # Replaces /W3 with /W4 in defaults.
51      string(REPLACE "/W3" "/W4" ${flag_var} "${${flag_var}}")
52
53      # Prevent D9025 warning for targets that have exception handling
54      # turned off (/EHs-c- flag). Where required, exceptions are explicitly
55      # re-enabled using the cxx_exception_flags variable.
56      string(REPLACE "/EHsc" "" ${flag_var} "${${flag_var}}")
57    endforeach()
58  endif()
59endmacro()
60
61# Defines the compiler/linker flags used to build Google Test and
62# Google Mock.  You can tweak these definitions to suit your need.  A
63# variable's value is empty before it's explicitly assigned to.
64macro(config_compiler_and_linker)
65  # Note: pthreads on MinGW is not supported, even if available
66  # instead, we use windows threading primitives
67  unset(GTEST_HAS_PTHREAD)
68  if (NOT gtest_disable_pthreads AND NOT MINGW)
69    # Defines CMAKE_USE_PTHREADS_INIT and CMAKE_THREAD_LIBS_INIT.
70    find_package(Threads)
71    if (CMAKE_USE_PTHREADS_INIT)
72      set(GTEST_HAS_PTHREAD ON)
73    endif()
74  endif()
75
76  fix_default_compiler_settings_()
77  if (MSVC)
78    # Newlines inside flags variables break CMake's NMake generator.
79    # TODO(vladl@google.com): Add -RTCs and -RTCu to debug builds.
80    set(cxx_base_flags "-GS -W4 -WX -wd4251 -wd4275 -nologo -J")
81    set(cxx_base_flags "${cxx_base_flags} -D_UNICODE -DUNICODE -DWIN32 -D_WIN32")
82    set(cxx_base_flags "${cxx_base_flags} -DSTRICT -DWIN32_LEAN_AND_MEAN")
83    set(cxx_exception_flags "-EHsc -D_HAS_EXCEPTIONS=1")
84    set(cxx_no_exception_flags "-EHs-c- -D_HAS_EXCEPTIONS=0")
85    set(cxx_no_rtti_flags "-GR-")
86    # Suppress "unreachable code" warning
87    # http://stackoverflow.com/questions/3232669 explains the issue.
88    set(cxx_base_flags "${cxx_base_flags} -wd4702")
89    # Ensure MSVC treats source files as UTF-8 encoded.
90    if(CMAKE_CXX_COMPILER_ID STREQUAL "MSVC")
91      set(cxx_base_flags "${cxx_base_flags} -utf-8")
92    endif()
93  elseif (CMAKE_CXX_COMPILER_ID STREQUAL "Clang")
94    set(cxx_base_flags "-Wall -Wshadow -Wconversion")
95    set(cxx_exception_flags "-fexceptions")
96    set(cxx_no_exception_flags "-fno-exceptions")
97    set(cxx_strict_flags "-W -Wpointer-arith -Wreturn-type -Wcast-qual -Wwrite-strings -Wswitch -Wunused-parameter -Wcast-align -Wchar-subscripts -Winline -Wredundant-decls")
98    set(cxx_no_rtti_flags "-fno-rtti")
99  elseif (CMAKE_COMPILER_IS_GNUCXX)
100    set(cxx_base_flags "-Wall -Wshadow")
101    if(NOT CMAKE_CXX_COMPILER_VERSION VERSION_LESS 7.0.0)
102      set(cxx_base_flags "${cxx_base_flags} -Wno-error=dangling-else")
103    endif()
104    set(cxx_exception_flags "-fexceptions")
105    set(cxx_no_exception_flags "-fno-exceptions")
106    # Until version 4.3.2, GCC doesn't define a macro to indicate
107    # whether RTTI is enabled.  Therefore we define GTEST_HAS_RTTI
108    # explicitly.
109    set(cxx_no_rtti_flags "-fno-rtti -DGTEST_HAS_RTTI=0")
110    set(cxx_strict_flags
111      "-Wextra -Wno-unused-parameter -Wno-missing-field-initializers")
112  elseif (CMAKE_CXX_COMPILER_ID STREQUAL "SunPro")
113    set(cxx_exception_flags "-features=except")
114    # Sun Pro doesn't provide macros to indicate whether exceptions and
115    # RTTI are enabled, so we define GTEST_HAS_* explicitly.
116    set(cxx_no_exception_flags "-features=no%except -DGTEST_HAS_EXCEPTIONS=0")
117    set(cxx_no_rtti_flags "-features=no%rtti -DGTEST_HAS_RTTI=0")
118  elseif (CMAKE_CXX_COMPILER_ID STREQUAL "VisualAge" OR
119      CMAKE_CXX_COMPILER_ID STREQUAL "XL")
120    # CMake 2.8 changes Visual Age's compiler ID to "XL".
121    set(cxx_exception_flags "-qeh")
122    set(cxx_no_exception_flags "-qnoeh")
123    # Until version 9.0, Visual Age doesn't define a macro to indicate
124    # whether RTTI is enabled.  Therefore we define GTEST_HAS_RTTI
125    # explicitly.
126    set(cxx_no_rtti_flags "-qnortti -DGTEST_HAS_RTTI=0")
127  elseif (CMAKE_CXX_COMPILER_ID STREQUAL "HP")
128    set(cxx_base_flags "-AA -mt")
129    set(cxx_exception_flags "-DGTEST_HAS_EXCEPTIONS=1")
130    set(cxx_no_exception_flags "+noeh -DGTEST_HAS_EXCEPTIONS=0")
131    # RTTI can not be disabled in HP aCC compiler.
132    set(cxx_no_rtti_flags "")
133  endif()
134
135  # The pthreads library is available and allowed?
136  if (DEFINED GTEST_HAS_PTHREAD)
137    set(GTEST_HAS_PTHREAD_MACRO "-DGTEST_HAS_PTHREAD=1")
138  else()
139    set(GTEST_HAS_PTHREAD_MACRO "-DGTEST_HAS_PTHREAD=0")
140  endif()
141  set(cxx_base_flags "${cxx_base_flags} ${GTEST_HAS_PTHREAD_MACRO}")
142
143  # For building gtest's own tests and samples.
144  set(cxx_exception "${cxx_base_flags} ${cxx_exception_flags}")
145  set(cxx_no_exception
146    "${CMAKE_CXX_FLAGS} ${cxx_base_flags} ${cxx_no_exception_flags}")
147  set(cxx_default "${cxx_exception}")
148  set(cxx_no_rtti "${cxx_default} ${cxx_no_rtti_flags}")
149
150  # For building the gtest libraries.
151  set(cxx_strict "${cxx_default} ${cxx_strict_flags}")
152endmacro()
153
154# Defines the gtest & gtest_main libraries.  User tests should link
155# with one of them.
156function(cxx_library_with_type name type cxx_flags)
157  # type can be either STATIC or SHARED to denote a static or shared library.
158  # ARGN refers to additional arguments after 'cxx_flags'.
159  add_library(${name} ${type} ${ARGN})
160  add_library(${cmake_package_name}::${name} ALIAS ${name})
161  set_target_properties(${name}
162    PROPERTIES
163    COMPILE_FLAGS "${cxx_flags}")
164  # Set the output directory for build artifacts
165  set_target_properties(${name}
166    PROPERTIES
167    RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/bin"
168    LIBRARY_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/lib"
169    ARCHIVE_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/lib"
170    PDB_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/bin"
171    COMPILE_PDB_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/lib")
172  # make PDBs match library name
173  get_target_property(pdb_debug_postfix ${name} DEBUG_POSTFIX)
174  set_target_properties(${name}
175    PROPERTIES
176    PDB_NAME "${name}"
177    PDB_NAME_DEBUG "${name}${pdb_debug_postfix}"
178    COMPILE_PDB_NAME "${name}"
179    COMPILE_PDB_NAME_DEBUG "${name}${pdb_debug_postfix}")
180
181  if (BUILD_SHARED_LIBS OR type STREQUAL "SHARED")
182    set_target_properties(${name}
183      PROPERTIES
184      COMPILE_DEFINITIONS "GTEST_CREATE_SHARED_LIBRARY=1")
185    if (NOT "${CMAKE_VERSION}" VERSION_LESS "2.8.11")
186      target_compile_definitions(${name} INTERFACE
187        $<INSTALL_INTERFACE:GTEST_LINKED_AS_SHARED_LIBRARY=1>)
188    endif()
189  endif()
190  if (DEFINED GTEST_HAS_PTHREAD)
191    if ("${CMAKE_VERSION}" VERSION_LESS "3.1.0")
192      set(threads_spec ${CMAKE_THREAD_LIBS_INIT})
193    else()
194      set(threads_spec Threads::Threads)
195    endif()
196    target_link_libraries(${name} PUBLIC ${threads_spec})
197  endif()
198
199  if (NOT "${CMAKE_VERSION}" VERSION_LESS "3.8")
200    target_compile_features(${name} PUBLIC cxx_std_14)
201  endif()
202endfunction()
203
204########################################################################
205#
206# Helper functions for creating build targets.
207
208function(cxx_shared_library name cxx_flags)
209  cxx_library_with_type(${name} SHARED "${cxx_flags}" ${ARGN})
210endfunction()
211
212function(cxx_library name cxx_flags)
213  cxx_library_with_type(${name} "" "${cxx_flags}" ${ARGN})
214endfunction()
215
216# cxx_executable_with_flags(name cxx_flags libs srcs...)
217#
218# creates a named C++ executable that depends on the given libraries and
219# is built from the given source files with the given compiler flags.
220function(cxx_executable_with_flags name cxx_flags libs)
221  add_executable(${name} ${ARGN})
222  if (MSVC)
223    # BigObj required for tests.
224    set(cxx_flags "${cxx_flags} -bigobj")
225  endif()
226  if (cxx_flags)
227    set_target_properties(${name}
228      PROPERTIES
229      COMPILE_FLAGS "${cxx_flags}")
230  endif()
231  if (BUILD_SHARED_LIBS)
232    set_target_properties(${name}
233      PROPERTIES
234      COMPILE_DEFINITIONS "GTEST_LINKED_AS_SHARED_LIBRARY=1")
235  endif()
236  # To support mixing linking in static and dynamic libraries, link each
237  # library in with an extra call to target_link_libraries.
238  foreach (lib "${libs}")
239    target_link_libraries(${name} ${lib})
240  endforeach()
241endfunction()
242
243# cxx_executable(name dir lib srcs...)
244#
245# creates a named target that depends on the given libs and is built
246# from the given source files.  dir/name.cc is implicitly included in
247# the source file list.
248function(cxx_executable name dir libs)
249  cxx_executable_with_flags(
250    ${name} "${cxx_default}" "${libs}" "${dir}/${name}.cc" ${ARGN})
251endfunction()
252
253# CMP0094 policy enables finding a Python executable in the LOCATION order, as
254# specified by the PATH environment variable.
255if (POLICY CMP0094)
256  cmake_policy(SET CMP0094 NEW)
257endif()
258
259# Sets PYTHONINTERP_FOUND and PYTHON_EXECUTABLE.
260if ("${CMAKE_VERSION}" VERSION_LESS "3.12.0")
261  find_package(PythonInterp)
262else()
263  find_package(Python COMPONENTS Interpreter)
264  set(PYTHONINTERP_FOUND ${Python_Interpreter_FOUND})
265  set(PYTHON_EXECUTABLE ${Python_EXECUTABLE})
266endif()
267
268# cxx_test_with_flags(name cxx_flags libs srcs...)
269#
270# creates a named C++ test that depends on the given libs and is built
271# from the given source files with the given compiler flags.
272function(cxx_test_with_flags name cxx_flags libs)
273  cxx_executable_with_flags(${name} "${cxx_flags}" "${libs}" ${ARGN})
274    add_test(NAME ${name} COMMAND "$<TARGET_FILE:${name}>")
275endfunction()
276
277# cxx_test(name libs srcs...)
278#
279# creates a named test target that depends on the given libs and is
280# built from the given source files.  Unlike cxx_test_with_flags,
281# test/name.cc is already implicitly included in the source file list.
282function(cxx_test name libs)
283  cxx_test_with_flags("${name}" "${cxx_default}" "${libs}"
284    "test/${name}.cc" ${ARGN})
285endfunction()
286
287# py_test(name)
288#
289# creates a Python test with the given name whose main module is in
290# test/name.py.  It does nothing if Python is not installed.
291function(py_test name)
292  if (PYTHONINTERP_FOUND)
293    if ("${CMAKE_MAJOR_VERSION}.${CMAKE_MINOR_VERSION}" VERSION_GREATER 3.1)
294      if (CMAKE_CONFIGURATION_TYPES)
295        # Multi-configuration build generators as for Visual Studio save
296        # output in a subdirectory of CMAKE_CURRENT_BINARY_DIR (Debug,
297        # Release etc.), so we have to provide it here.
298        add_test(NAME ${name}
299          COMMAND ${PYTHON_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/test/${name}.py
300              --build_dir=${CMAKE_CURRENT_BINARY_DIR}/$<CONFIG> ${ARGN})
301      else (CMAKE_CONFIGURATION_TYPES)
302        # Single-configuration build generators like Makefile generators
303        # don't have subdirs below CMAKE_CURRENT_BINARY_DIR.
304        add_test(NAME ${name}
305          COMMAND ${PYTHON_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/test/${name}.py
306            --build_dir=${CMAKE_CURRENT_BINARY_DIR} ${ARGN})
307      endif (CMAKE_CONFIGURATION_TYPES)
308    else()
309      # ${CMAKE_CURRENT_BINARY_DIR} is known at configuration time, so we can
310      # directly bind it from cmake. ${CTEST_CONFIGURATION_TYPE} is known
311      # only at ctest runtime (by calling ctest -c <Configuration>), so
312      # we have to escape $ to delay variable substitution here.
313      add_test(NAME ${name}
314        COMMAND ${PYTHON_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/test/${name}.py
315          --build_dir=${CMAKE_CURRENT_BINARY_DIR}/\${CTEST_CONFIGURATION_TYPE} ${ARGN})
316    endif()
317    # Make the Python import path consistent between Bazel and CMake.
318    set_tests_properties(${name} PROPERTIES ENVIRONMENT PYTHONPATH=${CMAKE_SOURCE_DIR})
319  endif(PYTHONINTERP_FOUND)
320endfunction()
321
322# install_project(targets...)
323#
324# Installs the specified targets and configures the associated pkgconfig files.
325function(install_project)
326  if(INSTALL_GTEST)
327    install(DIRECTORY "${PROJECT_SOURCE_DIR}/include/"
328      DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}")
329    # Install the project targets.
330    install(TARGETS ${ARGN}
331      EXPORT ${targets_export_name}
332      RUNTIME DESTINATION "${CMAKE_INSTALL_BINDIR}"
333      ARCHIVE DESTINATION "${CMAKE_INSTALL_LIBDIR}"
334      LIBRARY DESTINATION "${CMAKE_INSTALL_LIBDIR}")
335    if(CMAKE_CXX_COMPILER_ID MATCHES "MSVC")
336      # Install PDBs
337      foreach(t ${ARGN})
338        get_target_property(t_pdb_name ${t} COMPILE_PDB_NAME)
339        get_target_property(t_pdb_name_debug ${t} COMPILE_PDB_NAME_DEBUG)
340        get_target_property(t_pdb_output_directory ${t} PDB_OUTPUT_DIRECTORY)
341        install(FILES
342          "${t_pdb_output_directory}/\${CMAKE_INSTALL_CONFIG_NAME}/$<$<CONFIG:Debug>:${t_pdb_name_debug}>$<$<NOT:$<CONFIG:Debug>>:${t_pdb_name}>.pdb"
343          DESTINATION ${CMAKE_INSTALL_LIBDIR}
344          OPTIONAL)
345      endforeach()
346    endif()
347    # Configure and install pkgconfig files.
348    foreach(t ${ARGN})
349      set(configured_pc "${generated_dir}/${t}.pc")
350      configure_file("${PROJECT_SOURCE_DIR}/cmake/${t}.pc.in"
351        "${configured_pc}" @ONLY)
352      install(FILES "${configured_pc}"
353        DESTINATION "${CMAKE_INSTALL_LIBDIR}/pkgconfig")
354    endforeach()
355  endif()
356endfunction()
357