-
Notifications
You must be signed in to change notification settings - Fork 0
/
cxxplug_gen.cmake
235 lines (222 loc) · 7.95 KB
/
cxxplug_gen.cmake
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
# https://cmake.org/cmake/help/latest/command/cmake_parse_arguments.html
function (cxxplug_gen_integrated)
# Each entry of list names a list variable with source file names
set(arg_options "LISTS_INSIDE")
set(arg_values "INTERFACE_CONFIG" "INTERFACE_NAME" "TOOLS_PATH")
set(arg_multivalues "IMPLEMENTATIONS")
cmake_parse_arguments(
CXXPLUG "${arg_options}" "${arg_values}" "${arg_multivalues}" ${ARGN}
)
if (NOT DEFINED CXXPLUG_INTERFACE_CONFIG)
message(
FATAL_ERROR "Argument \"CXXPLUG_INTERFACE_CONFIG\" was not passed."
)
elseif (NOT EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/${CXXPLUG_INTERFACE_CONFIG}")
message(
FATAL_ERROR
"Interface file \"${CXXPLUG_INTERFACE_CONFIG}\" does not exist."
)
endif ()
set(cxxplug_gen_interface "cxxplug-gen-interface")
set(cxxplug_gen_proxy "cxxplug-gen-proxy")
set(cxxplug_hide_linked_symbols "cxxplug-hide-linked-symbols")
set(cxxplug_gen_collected_proxies "cxxplug-gen-collected-proxies")
if (DEFINED CXXPLUG_TOOLS_PATH)
set(
cxxplug_gen_interface
"${CXXPLUG_TOOLS_PATH}/${cxxplug_gen_interface}"
)
set(
cxxplug_gen_proxy
"${CXXPLUG_TOOLS_PATH}/${cxxplug_gen_proxy}"
)
set(
cxxplug_hide_linked_symbols
"${CXXPLUG_TOOLS_PATH}/${cxxplug_hide_linked_symbols}"
)
set(
cxxplug_gen_collected_proxies
"${CXXPLUG_TOOLS_PATH}/${cxxplug_gen_collected_proxies}"
)
endif ()
# Sources output directory generation
set(base_include_dir "${CMAKE_CURRENT_BINARY_DIR}/include")
set(generated_include_dir "${base_include_dir}/cxxplug_gen")
set(builtin_include_dir "${generated_include_dir}/builtin")
file(
MAKE_DIRECTORY "${builtin_include_dir}"
)
include_directories("${generated_include_dir}")
# Interface header
if (DEFINED CXXPLUG_INTERFACE_NAME)
set(interface_name "${CXXPLUG_INTERFACE_NAME}")
else ()
get_filename_component(
interface_name ${CXXPLUG_INTERFACE_CONFIG} NAME_WE
)
endif ()
set(
interface_header
"${generated_include_dir}/${interface_name}_interface.hpp"
)
add_custom_command(
OUTPUT "${interface_header}"
COMMAND "${cxxplug_gen_interface}"
ARGS
"${CMAKE_CURRENT_SOURCE_DIR}/${CXXPLUG_INTERFACE_CONFIG}"
"${interface_header}"
DEPENDS "${CMAKE_CURRENT_SOURCE_DIR}/${CXXPLUG_INTERFACE_CONFIG}"
)
add_custom_target(
"${interface_name}_header_target" DEPENDS "${interface_header}"
)
# Proxy generation for each passed implementation
unset(proxy_builtin_name_and_header_list)
unset(proxy_builtin_dependencies)
unset(proxy_builtin_libraries)
foreach (implementation ${CXXPLUG_IMPLEMENTATIONS})
if (CXXPLUG_LISTS_INSIDE)
set(implementation_sources ${${implementation}})
set(implementation_base_name ${implementation})
else ()
set(implementation_sources ${implementation})
get_filename_component(
implementation_base_name ${implementation} NAME_WE
)
endif ()
# Implementation
add_library(
"${implementation_base_name}" STATIC ${implementation_sources}
)
set_target_properties(
"${implementation_base_name}" PROPERTIES PREFIX ""
)
# Proxy
set(
proxy_hpp
"${CMAKE_CURRENT_BINARY_DIR}/${implementation_base_name}_proxy.hpp"
)
set(
proxy_cpp
"${CMAKE_CURRENT_BINARY_DIR}/${implementation_base_name}_proxy.cpp"
)
set(
proxy_sources
"${proxy_hpp}"
"${proxy_cpp}"
)
add_custom_command(
OUTPUT ${proxy_sources}
COMMAND "${cxxplug_gen_proxy}"
ARGS
"${CMAKE_CURRENT_SOURCE_DIR}/${CXXPLUG_INTERFACE_CONFIG}"
"${implementation_base_name}"
"${CMAKE_CURRENT_BINARY_DIR}/"
"${interface_header}"
DEPENDS
"${interface_header}"
"${CMAKE_CURRENT_SOURCE_DIR}/${CXXPLUG_INTERFACE_CONFIG}"
)
add_library(
"${implementation_base_name}_proxy_impl" STATIC ${proxy_sources}
)
set_target_properties(
"${implementation_base_name}_proxy_impl" PROPERTIES PREFIX ""
)
# Merged implementation&proxy (no symbols localized)
string(
CONCAT proxy_merged_output
"${CMAKE_CURRENT_BINARY_DIR}/"
"${implementation_base_name}_proxy_merged"
"${CMAKE_STATIC_LIBRARY_SUFFIX}"
)
add_custom_command(
OUTPUT
"${proxy_merged_output}"
COMMAND "${CMAKE_LINKER}"
ARGS
"--whole-archive" "--relocatable"
"$<TARGET_FILE:${implementation_base_name}_proxy_impl>"
"$<TARGET_FILE:${implementation_base_name}>"
"-o"
"${proxy_merged_output}"
DEPENDS
"$<TARGET_FILE:${implementation_base_name}_proxy_impl>"
"$<TARGET_FILE:${implementation_base_name}>"
)
add_custom_target(
"${implementation_base_name}_proxy_merged_target"
ALL DEPENDS
"${proxy_merged_output}"
)
# Proxy final
string(
CONCAT proxy_final_output
"${CMAKE_CURRENT_BINARY_DIR}/"
"${implementation_base_name}_proxy"
"${CMAKE_STATIC_LIBRARY_SUFFIX}"
)
add_custom_command(
OUTPUT "${proxy_final_output}"
COMMAND "${cxxplug_hide_linked_symbols}"
ARGS
"$<TARGET_FILE:${implementation_base_name}>"
"${proxy_merged_output}"
"${proxy_final_output}"
"${CMAKE_NM}"
"${CMAKE_OBJCOPY}"
DEPENDS
"${proxy_merged_output}"
"${implementation_base_name}"
)
add_custom_target(
"${implementation_base_name}_proxy_target"
ALL DEPENDS
"${proxy_final_output}"
)
# Prepare more data to generate collection after this loop
list(
APPEND proxy_builtin_name_and_header_list
"${implementation_base_name}" "${proxy_hpp}"
)
list(
APPEND proxy_builtin_dependencies
${proxy_sources}
)
list(
APPEND proxy_builtin_libraries
"${proxy_final_output}"
)
endforeach ()
set(
builtin_collection_sources
"${builtin_include_dir}/${interface_name}_builtin.hpp"
"${builtin_include_dir}/${interface_name}_builtin.cpp"
)
add_custom_command(
OUTPUT ${builtin_collection_sources}
COMMAND "${cxxplug_gen_collected_proxies}"
ARGS
"${CMAKE_CURRENT_SOURCE_DIR}/${CXXPLUG_INTERFACE_CONFIG}"
"${interface_name}"
"${builtin_include_dir}/"
"${interface_header}"
"SOURCES"
${proxy_builtin_name_and_header_list}
DEPENDS
"${interface_header}"
"${CMAKE_CURRENT_SOURCE_DIR}/${CXXPLUG_INTERFACE_CONFIG}"
${proxy_builtin_dependencies}
)
add_library(
"${interface_name}_builtin"
STATIC
${builtin_collection_sources}
)
target_link_libraries(
"${interface_name}_builtin" ${proxy_builtin_libraries}
)
# Return values
set(CXXPLUG_GENERATED_BUILTIN "${${interface_name}_builtin}" PARENT_SCOPE)
set(CXXPLUG_GENERATED_INCLUDE_DIR "${base_include_dir}" PARENT_SCOPE)
endfunction ()