-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAddMultipleDefinitionsTest.cmake
More file actions
83 lines (77 loc) · 2.58 KB
/
Copy pathAddMultipleDefinitionsTest.cmake
File metadata and controls
83 lines (77 loc) · 2.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# Eggs.Stacktrace
#
# Copyright (c) 2026 Agustin Berge
#
# Distributed under the Boost Software License, Version 1.0. (See accompanying
# file LICENSE.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
include_guard()
# add_multiple_definitions_test(<name> <target>...)
#
# Generates two TUs that each include all headers from every <target>'s
# header sets, then compiles and links them together as a test. A
# non-inline definition in any header causes a linker error, catching ODR
# violations at test time.
#
# Creates an executable target and a CTest test, both named <name>.
function(add_multiple_definitions_test name)
set(_targets ${ARGN})
set(_headers)
set(_include_dirs)
foreach(_target IN LISTS _targets)
list(
APPEND _include_dirs
"$<TARGET_PROPERTY:${_target},INCLUDE_DIRECTORIES>"
)
get_target_property(_set_headers ${_target} HEADER_SET)
if(_set_headers)
list(APPEND _headers ${_set_headers})
endif()
get_target_property(_header_sets ${_target} HEADER_SETS)
foreach(_set IN LISTS _header_sets)
get_target_property(_set_headers ${_target} HEADER_SET_${_set})
if(_set_headers)
list(APPEND _headers ${_set_headers})
endif()
endforeach()
endforeach()
if(NOT _headers)
message(
FATAL_ERROR
"add_multiple_definitions_test: no headers found for '${_targets}'"
)
endif()
list(REMOVE_DUPLICATES _headers)
set(_includes "")
foreach(_header IN LISTS _headers)
cmake_path(
RELATIVE_PATH _header
BASE_DIRECTORY "${CMAKE_SOURCE_DIR}"
OUTPUT_VARIABLE _rel
)
string(APPEND _includes "#include <${_rel}>\n")
endforeach()
file(
WRITE "${CMAKE_CURRENT_BINARY_DIR}/verify_definitions.${name}.a.cpp"
"// Generated by CMake - do not edit.\n${_includes}int main() {}\n"
)
file(
WRITE "${CMAKE_CURRENT_BINARY_DIR}/verify_definitions.${name}.b.cpp"
"// Generated by CMake - do not edit.\n${_includes}"
)
add_executable(
${name}
EXCLUDE_FROM_ALL
"${CMAKE_CURRENT_BINARY_DIR}/verify_definitions.${name}.a.cpp"
"${CMAKE_CURRENT_BINARY_DIR}/verify_definitions.${name}.b.cpp"
)
target_include_directories(
${name}
PRIVATE "${CMAKE_SOURCE_DIR}" ${_include_dirs}
)
add_test(
NAME ${name}
COMMAND
${CMAKE_COMMAND} --build ${CMAKE_BINARY_DIR} --target ${name}
--config $<CONFIG>
)
endfunction()