functional-dag
Loading...
Searching...
No Matches
incbin_util.h
1#pragma once
2
3// This is based off of the wonderful example found at:
4// https://gist.github.com/mmozeiko/ed9655cf50341553d282 that demonstrates how
5// to embed resource files into c in a cross platform way.
6
7#define STR2(x) #x
8#define STR(x) STR2(x)
9
10#ifdef __APPLE__
11#define USTR(x) "_" STR(x)
12#else
13#define USTR(x) STR(x)
14#endif
15
16#ifdef _WIN32
17#define INCBIN_SECTION ".rdata, \"dr\""
18#elif defined __APPLE__
19#define INCBIN_SECTION "__TEXT,__const"
20#else
21#define INCBIN_SECTION ".rodata"
22#endif
23
24// extern const char FooBin[], _sizeof_FooBin[];
25// this aligns start address to 16 and terminates byte array with explict
26// 0 which is not really needed, feel free to change it to whatever you
27// want/need
28#define INCBIN(prefix, name, file) \
29 __asm__(".section " INCBIN_SECTION "\n" \
30 ".balign 1\n" \
31 ".byte 0\n" \
32 ".global " USTR(prefix) "_" STR(name) "_start\n" \
33 ".balign 16\n" \
34 USTR(prefix) "_" STR(name) "_start:\n" \
35 ".incbin \"" file "\"\n" \
36 \
37 ".global " STR(prefix) "_" STR(name) "_end\n" \
38 ".balign 1\n" \
39 USTR(prefix) "_" STR(name) "_end:\n" \
40 ".byte 0\n" \
41 ); \
42 extern __attribute__((aligned(16))) const char prefix##_##name##_start[]; \
43 extern const char prefix##_##name##_end[];