Debian Codegraph @

Modules/ThirdParty/GoogleTest/src/itkgoogletest/googletest/cmake/internal_utils.cmake

raw
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 (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()
59 endmacro()
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.
64 macro(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([email protected]): 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}")
152 endmacro()
153
154 # Defines the gtest & gtest_main libraries. User tests should link
155 # with one of them.
156 function(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}/${ITK_INSTALL_RUNTIME_DIR}"
168 LIBRARY_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/${ITK_INSTALL_LIBRARY_DIR}"
169 ARCHIVE_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/${ITK_INSTALL_ARCHIVE_DIR}"
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()
202 endfunction()
203
204 ########################################################################
205 #
206 # Helper functions for creating build targets.
207
208 function(cxx_shared_library name cxx_flags)
209 cxx_library_with_type(${name} SHARED "${cxx_flags}" ${ARGN})
210 endfunction()
211
212 function(cxx_library name cxx_flags)
213 cxx_library_with_type(${name} "" "${cxx_flags}" ${ARGN})
214 endfunction()
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.
220 function(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()
241 endfunction()
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.
248 function(cxx_executable name dir libs)
249 cxx_executable_with_flags(
250 ${name} "${cxx_default}" "${libs}" "${dir}/${name}.cc" ${ARGN})
251 endfunction()
252
253 # cxx_test_with_flags(name cxx_flags libs srcs...)
254 #
255 # creates a named C++ test that depends on the given libs and is built
256 # from the given source files with the given compiler flags.
257 function(cxx_test_with_flags name cxx_flags libs)
258 cxx_executable_with_flags(${name} "${cxx_flags}" "${libs}" ${ARGN})
259 add_test(NAME ${name} COMMAND "$<TARGET_FILE:${name}>")
260 endfunction()
261
262 # cxx_test(name libs srcs...)
263 #
264 # creates a named test target that depends on the given libs and is
265 # built from the given source files. Unlike cxx_test_with_flags,
266 # test/name.cc is already implicitly included in the source file list.
267 function(cxx_test name libs)
268 cxx_test_with_flags("${name}" "${cxx_default}" "${libs}"
269 "test/${name}.cc" ${ARGN})
270 endfunction()
271
272 # ITK py_test function is not used for ITK, and searching for the python interpreter conflicts with ITK
273 # ITK Remove the py_test function.
274
275 # install_project(targets...)
276 #
277 # Installs the specified targets and configures the associated pkgconfig files.
278 function(install_project)
279 if(INSTALL_GTEST)
280 install(DIRECTORY "${PROJECT_SOURCE_DIR}/include/"
281 DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}")
282 # Install the project targets.
283 install(TARGETS ${ARGN}
284 EXPORT ${targets_export_name}
285 RUNTIME DESTINATION "${CMAKE_INSTALL_BINDIR}"
286 ARCHIVE DESTINATION "${CMAKE_INSTALL_LIBDIR}"
287 LIBRARY DESTINATION "${CMAKE_INSTALL_LIBDIR}")
288 if(CMAKE_CXX_COMPILER_ID MATCHES "MSVC")
289 # Install PDBs
290 foreach(t ${ARGN})
291 get_target_property(t_pdb_name ${t} COMPILE_PDB_NAME)
292 get_target_property(t_pdb_name_debug ${t} COMPILE_PDB_NAME_DEBUG)
293 get_target_property(t_pdb_output_directory ${t} PDB_OUTPUT_DIRECTORY)
294 install(FILES
295 "${t_pdb_output_directory}/\${CMAKE_INSTALL_CONFIG_NAME}/$<$<CONFIG:Debug>:${t_pdb_name_debug}>$<$<NOT:$<CONFIG:Debug>>:${t_pdb_name}>.pdb"
296 DESTINATION ${CMAKE_INSTALL_LIBDIR}
297 OPTIONAL)
298 endforeach()
299 endif()
300 # Configure and install pkgconfig files.
301 foreach(t ${ARGN})
302 set(configured_pc "${generated_dir}/${t}.pc")
303 configure_file("${PROJECT_SOURCE_DIR}/cmake/${t}.pc.in"
304 "${configured_pc}" @ONLY)
305 install(FILES "${configured_pc}"
306 DESTINATION "${CMAKE_INSTALL_LIBDIR}/pkgconfig")
307 endforeach()
308 endif()
309 endfunction()