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