-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcreate_resource.cmake
executable file
·362 lines (292 loc) · 9.92 KB
/
create_resource.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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
if (MSVC)
set(rcid 1 CACHE INTERNAL "rcid")
function(create_resource root input_paths lib)
set(rc-file-contents "")
set(emplace-statements "")
set(offset ${rcid})
foreach(p ${input_paths})
file(RELATIVE_PATH rel-path ${root} ${p})
string(APPEND emplace-statements " m.emplace(\"${rel-path}\", ${rcid});\n")
string(APPEND create-statements " create_resource(${rcid}),\n")
string(APPEND rc-file-contents "${rcid} RCDATA \"${p}\"\n")
math(EXPR rcid "${rcid}+1")
set(rcid ${rcid} CACHE INTERNAL "rcid")
endforeach(p input_paths)
file(WRITE ${CMAKE_CURRENT_BINARY_DIR}/${lib}/src/resource.rc ${rc-file-contents})
file(WRITE ${CMAKE_CURRENT_BINARY_DIR}/${lib}/include/${lib}.h "\
#pragma once
#include <cstddef>
#include <map>
#include <string>
namespace ${lib} {
struct resource {
std::size_t size_{0U};
void const* ptr_{nullptr};
};
resource get_resource(std::string const&);
int get_resource_id(std::string const&);
resource make_resource(int id);
std::map<std::string, int> const& get_resource_ids();
} // namespace ${lib}
")
file(WRITE ${CMAKE_CURRENT_BINARY_DIR}/${lib}/src/${lib}.cc "\
#include \"${lib}.h\"
#include <map>
#include <string>
#include \"windows.h\"
namespace ${lib} {
static auto resource_ids = [] {
std::map<std::string, int> m;
${emplace-statements}\
return m;
}();
resource create_resource(int id) {
auto const a = FindResource(nullptr, MAKEINTRESOURCEA(id), RT_RCDATA);
auto const mem = LoadResource(nullptr, a);
auto const size = SizeofResource(nullptr, a);
auto const ptr = LockResource(mem);
return resource{size, ptr};
}
resource make_resource(int id) {
resource res[] = {
${create-statements}\
};
return res[id - ${offset}];
}
int get_resource_id(std::string const& s) {
return resource_ids.at(s);
}
resource get_resource(std::string const& s) {
return make_resource(get_resource_id(s));
}
std::map<std::string, int> const& get_resource_ids() {
return resource_ids;
}
} // namespace ${lib}
")
add_library(${lib}-res EXCLUDE_FROM_ALL OBJECT ${CMAKE_CURRENT_BINARY_DIR}/${lib}/src/resource.rc)
add_library(${lib} EXCLUDE_FROM_ALL ${CMAKE_CURRENT_BINARY_DIR}/${lib}/src/${lib}.cc)
target_compile_features(${lib} PUBLIC cxx_std_17)
target_include_directories(${lib} PUBLIC ${CMAKE_CURRENT_BINARY_DIR}/${lib}/include)
endfunction(create_resource)
elseif(APPLE)
function(create_resource root input_paths lib)
# Write an empty dummy object file because the ld call needs this.
file(WRITE ${CMAKE_CURRENT_BINARY_DIR}/${lib}/src/${lib}-stub.c "")
add_custom_command(
OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/${lib}/obj/stub.o
COMMAND
${CMAKE_C_COMPILER}
-o ${CMAKE_CURRENT_BINARY_DIR}/${lib}/obj/stub.o
-c ${CMAKE_CURRENT_BINARY_DIR}/${lib}/src/${lib}-stub.c
)
add_custom_command(
OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/${lib}/all_res.bin
COMMAND cat ${input_paths} > ${CMAKE_CURRENT_BINARY_DIR}/${lib}/all_res.bin
COMMENT Concatenate ${lib} resource file with all resources.
DEPENDS ${input_paths}
)
if (DEFINED ARGV3)
add_custom_command(
OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/${lib}/${lib}-res.cc
COMMAND embedfiles ${CMAKE_CURRENT_BINARY_DIR}/${lib}/${lib}-res.cc ${lib} ${input_paths}
DEPENDS embedfiles
)
add_library(${lib}-res ${CMAKE_CURRENT_BINARY_DIR}/${lib}/${lib}-res.cc)
endif()
set(id 0)
set(res-offset 0)
set(res-total-size 0)
foreach(p ${input_paths})
file(SIZE "${p}" res-input-length)
math(EXPR res-total-size "${res-total-size}+${res-input-length}")
math(EXPR res-size "${res-total-size}-${res-offset}")
string(APPEND resource-statements " create_resource(${res-offset}, ${res-size}),\n")
file(RELATIVE_PATH rel-path ${root} ${p})
string(APPEND emplace-statements " m.emplace(\"${rel-path}\", ${id});\r\n")
math(EXPR res-offset "${res-offset}+${res-size}")
math(EXPR id "${id}+1")
endforeach(p input_paths)
string(SUBSTRING ${lib} 0 15 short-lib)
if ("${CMAKE_OSX_ARCHITECTURES}" STREQUAL "")
message(STATUS "res arch is CMAKE_SYSTEM_PROCESSOR=${CMAKE_SYSTEM_PROCESSOR}")
else()
set(arch-flag -arch ${CMAKE_OSX_ARCHITECTURES})
message(STATUS "res arch is CMAKE_OSX_ARCHITECTURES=${CMAKE_OSX_ARCHITECTURES}")
endif()
add_custom_command(
OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/${lib}/obj/${lib}-res.o
COMMAND
ld
${arch-flag}
-r -o ${CMAKE_CURRENT_BINARY_DIR}/${lib}/obj/${lib}-res.o
-sectcreate binary ${short-lib} ${CMAKE_CURRENT_BINARY_DIR}/${lib}/all_res.bin
${CMAKE_CURRENT_BINARY_DIR}/${lib}/obj/stub.o
DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/${lib}/obj/stub.o
DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/${lib}/all_res.bin
WORKING_DIRECTORY ${root}
COMMENT "Generating resource ${CMAKE_CURRENT_BINARY_DIR}/${lib}/obj/${lib}-res.o"
VERBATIM
)
file(WRITE ${CMAKE_CURRENT_BINARY_DIR}/${lib}/include/${lib}.h "\
#pragma once
#include <cstddef>
#include <map>
#include <string>
namespace ${lib} {
struct resource {
std::size_t size_{0U};
void const* ptr_{nullptr};
};
resource get_resource(std::string const&);
int get_resource_id(std::string const&);
resource make_resource(int id);
std::map<std::string, int> const& get_resource_ids();
} // namespace ${lib}
")
if (DEFINED ARGV3)
set(res-${lib}-access "\
namespace ${lib} {
extern uint32_t size;
extern uint8_t const* const base;")
else()
set(res-${lib}-access "\
#include <mach-o/getsect.h>
extern const struct mach_header_64 _mh_execute_header;
namespace ${lib} {
static auto size = size_t{};
static uint8_t const* const base = getsectiondata(
&_mh_execute_header, \"binary\", \"${short-lib}\", &size);")
endif()
file(WRITE ${CMAKE_CURRENT_BINARY_DIR}/${lib}/src/${lib}.cc "\
#include \"${lib}.h\"
#include <map>
#include <string>
${res-${lib}-access}
static auto resource_ids = [] {
std::map<std::string, int> m;
${emplace-statements}\
return m;
}();
resource create_resource(size_t const offset, size_t const size) {
return resource{size, base + offset};
}
resource make_resource(int id) {
static resource resources[] = {
${resource-statements}\
};
return resources[id];
}
int get_resource_id(std::string const& s) {
return resource_ids.at(s);
}
resource get_resource(std::string const& s) {
return make_resource(get_resource_id(s));
}
std::map<std::string, int> const& get_resource_ids() {
return resource_ids;
}
} // namespace ${lib}
")
if (NOT DEFINED ARGV3)
set_source_files_properties(${lib}-res.o
PROPERTIES
EXTERNAL_OBJECT true
GENERATED true)
add_library(${lib}-res OBJECT IMPORTED GLOBAL)
set_target_properties(${lib}-res
PROPERTIES
IMPORTED_OBJECTS
${CMAKE_CURRENT_BINARY_DIR}/${lib}/obj/${lib}-res.o)
endif()
add_library(${lib} EXCLUDE_FROM_ALL STATIC ${CMAKE_CURRENT_BINARY_DIR}/${lib}/src/${lib}.cc)
target_compile_features(${lib} PUBLIC cxx_std_17)
target_include_directories(${lib} PUBLIC ${CMAKE_CURRENT_BINARY_DIR}/${lib}/include)
target_link_libraries(${lib} ${lib}-res)
endfunction()
else()
function(create_resource root input_paths lib)
if (CMAKE_CROSSCOMPILING)
set(resource-linker ${CMAKE_LINKER})
else()
set(resource-linker ld)
endif()
if (${CMAKE_SYSTEM_PROCESSOR} MATCHES "i686")
set(flags "-melf_i386")
endif()
set(id 0)
foreach(p ${input_paths})
file(RELATIVE_PATH rel-path ${root} ${p})
string(REGEX REPLACE "[^0-9a-zA-Z]" "_" mangled-path ${rel-path})
string(APPEND extern-definitions "extern const char _binary_${mangled-path}_start, _binary_${mangled-path}_end;\n")
string(APPEND resource-statements " resource{\
static_cast<std::size_t>(&_binary_${mangled-path}_end - &_binary_${mangled-path}_start),\
reinterpret_cast<void const*>(&_binary_${mangled-path}_start)},\n")
string(APPEND emplace-statements " m.emplace(\"${rel-path}\", ${id});\r\n")
file(MAKE_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/${lib}/obj)
add_custom_command(OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/${lib}/obj/res_${id}.o
COMMAND ${resource-linker} ${flags} -z noexecstack -r -b binary -o ${CMAKE_CURRENT_BINARY_DIR}/${lib}/obj/res_${id}.o ${rel-path}
DEPENDS ${p}
WORKING_DIRECTORY ${root}
COMMENT "Generating resource ${rel-path} (${CMAKE_CURRENT_BINARY_DIR}/${lib}/obj/res_${id}.o)"
VERBATIM
)
list(APPEND o-files ${CMAKE_CURRENT_BINARY_DIR}/${lib}/obj/res_${id}.o)
math(EXPR id "${id}+1")
endforeach(p input_paths)
file(WRITE ${CMAKE_CURRENT_BINARY_DIR}/${lib}/include/${lib}.h "\
#pragma once
#include <cstddef>
#include <map>
#include <string>
namespace ${lib} {
struct resource {
std::size_t size_{0U};
void const* ptr_{nullptr};
};
resource get_resource(std::string const&);
int get_resource_id(std::string const&);
resource make_resource(int id);
std::map<std::string, int> const& get_resource_ids();
} // namespace ${lib}
")
file(WRITE ${CMAKE_CURRENT_BINARY_DIR}/${lib}/src/${lib}.cc "\
#include \"${lib}.h\"
#include <map>
#include <string>
${extern-definitions}
namespace ${lib} {
static auto resource_ids = [] {
std::map<std::string, int> m;
${emplace-statements}\
return m;
}();
resource resources[] = {
${resource-statements}\
};
resource make_resource(int id) {
return resources[id];
}
int get_resource_id(std::string const& s) {
return resource_ids.at(s);
}
resource get_resource(std::string const& s) {
return make_resource(get_resource_id(s));
}
std::map<std::string, int> const& get_resource_ids() {
return resource_ids;
}
} // namespace ${lib}
")
set_source_files_properties(${o-files} PROPERTIES
EXTERNAL_OBJECT true
GENERATED true
)
add_library(${lib}-res OBJECT IMPORTED GLOBAL)
set_target_properties(${lib}-res PROPERTIES IMPORTED_OBJECTS "${o-files}")
add_library(${lib} EXCLUDE_FROM_ALL STATIC ${CMAKE_CURRENT_BINARY_DIR}/${lib}/src/${lib}.cc)
target_link_libraries(${lib} ${lib}-res)
target_compile_features(${lib} PUBLIC cxx_std_17)
target_include_directories(${lib} PUBLIC ${CMAKE_CURRENT_BINARY_DIR}/${lib}/include)
endfunction()
endif()