Skip to content

Commit d446ed0

Browse files
committed
Merge branch 'gtest_update' of https://github.com/kimkulling/cppcore into gtest_update
2 parents 99f435f + 4f563ac commit d446ed0

File tree

6 files changed

+439
-114
lines changed

6 files changed

+439
-114
lines changed

.gitignore

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ build/bin/Debug/cppcore.dll
1818
*.check_cache
1919
*.rule
2020
build/CMakeFiles/2.8.12.2/CompilerIdC/CMakeCCompilerId.c
21-
*.cmake
2221
*.bin
2322
build/CMakeFiles/TargetDirectories.txt
2423
build/CMakeFiles/generate.stamp.list
Lines changed: 358 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,358 @@
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([email protected]): Add -RTCs and -RTCu to debug builds.
75+
set(cxx_base_flags "-GS -W4 -WX -wd4251 -wd4275 -nologo -J -Zi")
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+
elseif (CMAKE_CXX_COMPILER_ID STREQUAL "Clang")
85+
set(cxx_base_flags "-Wall -Wshadow -Werror -Wconversion")
86+
set(cxx_exception_flags "-fexceptions")
87+
set(cxx_no_exception_flags "-fno-exceptions")
88+
set(cxx_strict_flags "-W -Wpointer-arith -Wreturn-type -Wcast-qual -Wwrite-strings -Wswitch -Wunused-parameter -Wcast-align -Wchar-subscripts -Winline -Wredundant-decls")
89+
set(cxx_no_rtti_flags "-fno-rtti")
90+
elseif (CMAKE_COMPILER_IS_GNUCXX)
91+
set(cxx_base_flags "-Wall -Wshadow -Werror")
92+
if(NOT CMAKE_CXX_COMPILER_VERSION VERSION_LESS 7.0.0)
93+
set(cxx_base_flags "${cxx_base_flags} -Wno-error=dangling-else")
94+
endif()
95+
set(cxx_exception_flags "-fexceptions")
96+
set(cxx_no_exception_flags "-fno-exceptions")
97+
# Until version 4.3.2, GCC doesn't define a macro to indicate
98+
# whether RTTI is enabled. Therefore we define GTEST_HAS_RTTI
99+
# explicitly.
100+
set(cxx_no_rtti_flags "-fno-rtti -DGTEST_HAS_RTTI=0")
101+
set(cxx_strict_flags
102+
"-Wextra -Wno-unused-parameter -Wno-missing-field-initializers")
103+
elseif (CMAKE_CXX_COMPILER_ID STREQUAL "SunPro")
104+
set(cxx_exception_flags "-features=except")
105+
# Sun Pro doesn't provide macros to indicate whether exceptions and
106+
# RTTI are enabled, so we define GTEST_HAS_* explicitly.
107+
set(cxx_no_exception_flags "-features=no%except -DGTEST_HAS_EXCEPTIONS=0")
108+
set(cxx_no_rtti_flags "-features=no%rtti -DGTEST_HAS_RTTI=0")
109+
elseif (CMAKE_CXX_COMPILER_ID STREQUAL "VisualAge" OR
110+
CMAKE_CXX_COMPILER_ID STREQUAL "XL")
111+
# CMake 2.8 changes Visual Age's compiler ID to "XL".
112+
set(cxx_exception_flags "-qeh")
113+
set(cxx_no_exception_flags "-qnoeh")
114+
# Until version 9.0, Visual Age doesn't define a macro to indicate
115+
# whether RTTI is enabled. Therefore we define GTEST_HAS_RTTI
116+
# explicitly.
117+
set(cxx_no_rtti_flags "-qnortti -DGTEST_HAS_RTTI=0")
118+
elseif (CMAKE_CXX_COMPILER_ID STREQUAL "HP")
119+
set(cxx_base_flags "-AA -mt")
120+
set(cxx_exception_flags "-DGTEST_HAS_EXCEPTIONS=1")
121+
set(cxx_no_exception_flags "+noeh -DGTEST_HAS_EXCEPTIONS=0")
122+
# RTTI can not be disabled in HP aCC compiler.
123+
set(cxx_no_rtti_flags "")
124+
endif()
125+
126+
# The pthreads library is available and allowed?
127+
if (DEFINED GTEST_HAS_PTHREAD)
128+
set(GTEST_HAS_PTHREAD_MACRO "-DGTEST_HAS_PTHREAD=1")
129+
else()
130+
set(GTEST_HAS_PTHREAD_MACRO "-DGTEST_HAS_PTHREAD=0")
131+
endif()
132+
set(cxx_base_flags "${cxx_base_flags} ${GTEST_HAS_PTHREAD_MACRO}")
133+
134+
# For building gtest's own tests and samples.
135+
set(cxx_exception "${cxx_base_flags} ${cxx_exception_flags}")
136+
set(cxx_no_exception
137+
"${CMAKE_CXX_FLAGS} ${cxx_base_flags} ${cxx_no_exception_flags}")
138+
set(cxx_default "${cxx_exception}")
139+
set(cxx_no_rtti "${cxx_default} ${cxx_no_rtti_flags}")
140+
141+
# For building the gtest libraries.
142+
set(cxx_strict "${cxx_default} ${cxx_strict_flags}")
143+
endmacro()
144+
145+
# Defines the gtest & gtest_main libraries. User tests should link
146+
# with one of them.
147+
function(cxx_library_with_type name type cxx_flags)
148+
# type can be either STATIC or SHARED to denote a static or shared library.
149+
# ARGN refers to additional arguments after 'cxx_flags'.
150+
add_library(${name} ${type} ${ARGN})
151+
set_target_properties(${name}
152+
PROPERTIES
153+
COMPILE_FLAGS "${cxx_flags}")
154+
# Generate debug library name with a postfix.
155+
set_target_properties(${name}
156+
PROPERTIES
157+
DEBUG_POSTFIX "d")
158+
# Set the output directory for build artifacts
159+
set_target_properties(${name}
160+
PROPERTIES
161+
RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/bin"
162+
LIBRARY_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/lib"
163+
ARCHIVE_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/lib"
164+
PDB_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/bin")
165+
# make PDBs match library name
166+
get_target_property(pdb_debug_postfix ${name} DEBUG_POSTFIX)
167+
set_target_properties(${name}
168+
PROPERTIES
169+
PDB_NAME "${name}"
170+
PDB_NAME_DEBUG "${name}${pdb_debug_postfix}"
171+
COMPILE_PDB_NAME "${name}"
172+
COMPILE_PDB_NAME_DEBUG "${name}${pdb_debug_postfix}")
173+
174+
if (BUILD_SHARED_LIBS OR type STREQUAL "SHARED")
175+
set_target_properties(${name}
176+
PROPERTIES
177+
COMPILE_DEFINITIONS "GTEST_CREATE_SHARED_LIBRARY=1")
178+
if (NOT "${CMAKE_VERSION}" VERSION_LESS "2.8.11")
179+
target_compile_definitions(${name} INTERFACE
180+
$<INSTALL_INTERFACE:GTEST_LINKED_AS_SHARED_LIBRARY=1>)
181+
endif()
182+
endif()
183+
if (DEFINED GTEST_HAS_PTHREAD)
184+
if ("${CMAKE_VERSION}" VERSION_LESS "3.1.0")
185+
set(threads_spec ${CMAKE_THREAD_LIBS_INIT})
186+
else()
187+
set(threads_spec Threads::Threads)
188+
endif()
189+
target_link_libraries(${name} PUBLIC ${threads_spec})
190+
endif()
191+
endfunction()
192+
193+
########################################################################
194+
#
195+
# Helper functions for creating build targets.
196+
197+
function(cxx_shared_library name cxx_flags)
198+
cxx_library_with_type(${name} SHARED "${cxx_flags}" ${ARGN})
199+
endfunction()
200+
201+
function(cxx_library name cxx_flags)
202+
cxx_library_with_type(${name} "" "${cxx_flags}" ${ARGN})
203+
endfunction()
204+
205+
# cxx_executable_with_flags(name cxx_flags libs srcs...)
206+
#
207+
# creates a named C++ executable that depends on the given libraries and
208+
# is built from the given source files with the given compiler flags.
209+
function(cxx_executable_with_flags name cxx_flags libs)
210+
add_executable(${name} ${ARGN})
211+
if (MSVC)
212+
# BigObj required for tests.
213+
set(cxx_flags "${cxx_flags} -bigobj")
214+
endif()
215+
if (cxx_flags)
216+
set_target_properties(${name}
217+
PROPERTIES
218+
COMPILE_FLAGS "${cxx_flags}")
219+
endif()
220+
if (BUILD_SHARED_LIBS)
221+
set_target_properties(${name}
222+
PROPERTIES
223+
COMPILE_DEFINITIONS "GTEST_LINKED_AS_SHARED_LIBRARY=1")
224+
endif()
225+
# To support mixing linking in static and dynamic libraries, link each
226+
# library in with an extra call to target_link_libraries.
227+
foreach (lib "${libs}")
228+
target_link_libraries(${name} ${lib})
229+
endforeach()
230+
endfunction()
231+
232+
# cxx_executable(name dir lib srcs...)
233+
#
234+
# creates a named target that depends on the given libs and is built
235+
# from the given source files. dir/name.cc is implicitly included in
236+
# the source file list.
237+
function(cxx_executable name dir libs)
238+
cxx_executable_with_flags(
239+
${name} "${cxx_default}" "${libs}" "${dir}/${name}.cc" ${ARGN})
240+
endfunction()
241+
242+
# Sets PYTHONINTERP_FOUND and PYTHON_EXECUTABLE.
243+
find_package(PythonInterp)
244+
245+
# cxx_test_with_flags(name cxx_flags libs srcs...)
246+
#
247+
# creates a named C++ test that depends on the given libs and is built
248+
# from the given source files with the given compiler flags.
249+
function(cxx_test_with_flags name cxx_flags libs)
250+
cxx_executable_with_flags(${name} "${cxx_flags}" "${libs}" ${ARGN})
251+
if (WIN32 OR MINGW)
252+
add_test(NAME ${name}
253+
COMMAND "powershell" "-Command" "${CMAKE_CURRENT_BINARY_DIR}/$<CONFIG>/RunTest.ps1" "$<TARGET_FILE:${name}>")
254+
else()
255+
add_test(NAME ${name}
256+
COMMAND "$<TARGET_FILE:${name}>")
257+
endif()
258+
endfunction()
259+
260+
# cxx_test(name libs srcs...)
261+
#
262+
# creates a named test target that depends on the given libs and is
263+
# built from the given source files. Unlike cxx_test_with_flags,
264+
# test/name.cc is already implicitly included in the source file list.
265+
function(cxx_test name libs)
266+
cxx_test_with_flags("${name}" "${cxx_default}" "${libs}"
267+
"test/${name}.cc" ${ARGN})
268+
endfunction()
269+
270+
# py_test(name)
271+
#
272+
# creates a Python test with the given name whose main module is in
273+
# test/name.py. It does nothing if Python is not installed.
274+
function(py_test name)
275+
if (PYTHONINTERP_FOUND)
276+
if ("${CMAKE_MAJOR_VERSION}.${CMAKE_MINOR_VERSION}" VERSION_GREATER 3.1)
277+
if (CMAKE_CONFIGURATION_TYPES)
278+
# Multi-configuration build generators as for Visual Studio save
279+
# output in a subdirectory of CMAKE_CURRENT_BINARY_DIR (Debug,
280+
# Release etc.), so we have to provide it here.
281+
if (WIN32 OR MINGW)
282+
add_test(NAME ${name}
283+
COMMAND powershell -Command ${CMAKE_CURRENT_BINARY_DIR}/$<CONFIG>/RunTest.ps1
284+
${PYTHON_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/test/${name}.py
285+
--build_dir=${CMAKE_CURRENT_BINARY_DIR}/$<CONFIG> ${ARGN})
286+
else()
287+
add_test(NAME ${name}
288+
COMMAND ${PYTHON_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/test/${name}.py
289+
--build_dir=${CMAKE_CURRENT_BINARY_DIR}/$<CONFIG> ${ARGN})
290+
endif()
291+
else (CMAKE_CONFIGURATION_TYPES)
292+
# Single-configuration build generators like Makefile generators
293+
# don't have subdirs below CMAKE_CURRENT_BINARY_DIR.
294+
if (WIN32 OR MINGW)
295+
add_test(NAME ${name}
296+
COMMAND powershell -Command ${CMAKE_CURRENT_BINARY_DIR}/RunTest.ps1
297+
${PYTHON_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/test/${name}.py
298+
--build_dir=${CMAKE_CURRENT_BINARY_DIR} ${ARGN})
299+
else()
300+
add_test(NAME ${name}
301+
COMMAND ${PYTHON_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/test/${name}.py
302+
--build_dir=${CMAKE_CURRENT_BINARY_DIR} ${ARGN})
303+
endif()
304+
endif (CMAKE_CONFIGURATION_TYPES)
305+
else()
306+
# ${CMAKE_CURRENT_BINARY_DIR} is known at configuration time, so we can
307+
# directly bind it from cmake. ${CTEST_CONFIGURATION_TYPE} is known
308+
# only at ctest runtime (by calling ctest -c <Configuration>), so
309+
# we have to escape $ to delay variable substitution here.
310+
if (WIN32 OR MINGW)
311+
add_test(NAME ${name}
312+
COMMAND powershell -Command ${CMAKE_CURRENT_BINARY_DIR}/RunTest.ps1
313+
${PYTHON_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/test/${name}.py
314+
--build_dir=${CMAKE_CURRENT_BINARY_DIR}/\${CTEST_CONFIGURATION_TYPE} ${ARGN})
315+
else()
316+
add_test(NAME ${name}
317+
COMMAND ${PYTHON_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/test/${name}.py
318+
--build_dir=${CMAKE_CURRENT_BINARY_DIR}/\${CTEST_CONFIGURATION_TYPE} ${ARGN})
319+
endif()
320+
endif()
321+
endif(PYTHONINTERP_FOUND)
322+
endfunction()
323+
324+
# install_project(targets...)
325+
#
326+
# Installs the specified targets and configures the associated pkgconfig files.
327+
function(install_project)
328+
if(INSTALL_GTEST)
329+
install(DIRECTORY "${PROJECT_SOURCE_DIR}/include/"
330+
DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}")
331+
# Install the project targets.
332+
install(TARGETS ${ARGN}
333+
EXPORT ${targets_export_name}
334+
RUNTIME DESTINATION "${CMAKE_INSTALL_BINDIR}"
335+
ARCHIVE DESTINATION "${CMAKE_INSTALL_LIBDIR}"
336+
LIBRARY DESTINATION "${CMAKE_INSTALL_LIBDIR}")
337+
if(CMAKE_CXX_COMPILER_ID MATCHES "MSVC")
338+
# Install PDBs
339+
foreach(t ${ARGN})
340+
get_target_property(t_pdb_name ${t} COMPILE_PDB_NAME)
341+
get_target_property(t_pdb_name_debug ${t} COMPILE_PDB_NAME_DEBUG)
342+
get_target_property(t_pdb_output_directory ${t} PDB_OUTPUT_DIRECTORY)
343+
install(FILES
344+
"${t_pdb_output_directory}/\${CMAKE_INSTALL_CONFIG_NAME}/$<$<CONFIG:Debug>:${t_pdb_name_debug}>$<$<NOT:$<CONFIG:Debug>>:${t_pdb_name}>.pdb"
345+
DESTINATION ${CMAKE_INSTALL_LIBDIR}
346+
OPTIONAL)
347+
endforeach()
348+
endif()
349+
# Configure and install pkgconfig files.
350+
foreach(t ${ARGN})
351+
set(configured_pc "${generated_dir}/${t}.pc")
352+
configure_file("${PROJECT_SOURCE_DIR}/cmake/${t}.pc.in"
353+
"${configured_pc}" @ONLY)
354+
install(FILES "${configured_pc}"
355+
DESTINATION "${CMAKE_INSTALL_LIBDIR}/pkgconfig")
356+
endforeach()
357+
endif()
358+
endfunction()

0 commit comments

Comments
 (0)