1 | /**
|
---|
2 | * @file incbin.h
|
---|
3 | * @author Dale Weiler
|
---|
4 | * @brief Utility for including binary files
|
---|
5 | *
|
---|
6 | * Facilities for including binary files into the current translation unit and
|
---|
7 | * making use from them externally in other translation units.
|
---|
8 | */
|
---|
9 | #ifndef INCBIN_HDR
|
---|
10 | #define INCBIN_HDR
|
---|
11 | #include <limits.h>
|
---|
12 |
|
---|
13 | # define INCBIN_ALIGNMENT_INDEX 8
|
---|
14 |
|
---|
15 | /* Lookup table of (1 << n) where `n' is `INCBIN_ALIGNMENT_INDEX' */
|
---|
16 | #define INCBIN_ALIGN_SHIFT_0 1
|
---|
17 | #define INCBIN_ALIGN_SHIFT_1 2
|
---|
18 | #define INCBIN_ALIGN_SHIFT_2 4
|
---|
19 | #define INCBIN_ALIGN_SHIFT_3 8
|
---|
20 | #define INCBIN_ALIGN_SHIFT_4 16
|
---|
21 | #define INCBIN_ALIGN_SHIFT_5 32
|
---|
22 | #define INCBIN_ALIGN_SHIFT_6 64
|
---|
23 | #define INCBIN_ALIGN_SHIFT_7 128
|
---|
24 | #define INCBIN_ALIGN_SHIFT_8 256
|
---|
25 |
|
---|
26 | /* Actual alignment value */
|
---|
27 | #define INCBIN_ALIGNMENT \
|
---|
28 | INCBIN_CONCATENATE( \
|
---|
29 | INCBIN_CONCATENATE(INCBIN_ALIGN_SHIFT, _), \
|
---|
30 | INCBIN_ALIGNMENT_INDEX)
|
---|
31 |
|
---|
32 | /* Stringize */
|
---|
33 | #define INCBIN_STR(X) \
|
---|
34 | #X
|
---|
35 | #define INCBIN_STRINGIZE(X) \
|
---|
36 | INCBIN_STR(X)
|
---|
37 | /* Concatenate */
|
---|
38 | #define INCBIN_CAT(X, Y) \
|
---|
39 | X ## Y
|
---|
40 | #define INCBIN_CONCATENATE(X, Y) \
|
---|
41 | INCBIN_CAT(X, Y)
|
---|
42 | /* Deferred macro expansion */
|
---|
43 | #define INCBIN_EVAL(X) \
|
---|
44 | X
|
---|
45 | #define INCBIN_INVOKE(N, ...) \
|
---|
46 | INCBIN_EVAL(N(__VA_ARGS__))
|
---|
47 |
|
---|
48 | /* Green Hills uses a different directive for including binary data */
|
---|
49 | #if defined(__ghs__)
|
---|
50 | # define INCBIN_MACRO "\tINCBIN"
|
---|
51 | #else
|
---|
52 | # define INCBIN_MACRO ".incbin"
|
---|
53 | #endif
|
---|
54 |
|
---|
55 | #ifndef _MSC_VER
|
---|
56 | # define INCBIN_ALIGN \
|
---|
57 | __attribute__((aligned(INCBIN_ALIGNMENT)))
|
---|
58 | #else
|
---|
59 | # define INCBIN_ALIGN __declspec(align(INCBIN_ALIGNMENT))
|
---|
60 | #endif
|
---|
61 |
|
---|
62 | #if defined(__arm__) || /* GNU C and RealView */ \
|
---|
63 | defined(__arm) || /* Diab */ \
|
---|
64 | defined(_ARM) /* ImageCraft */
|
---|
65 | # define INCBIN_ARM
|
---|
66 | #endif
|
---|
67 |
|
---|
68 | #ifdef __GNUC__
|
---|
69 | /* Utilize .balign where supported */
|
---|
70 | # define INCBIN_ALIGN_HOST ".balign " INCBIN_STRINGIZE(INCBIN_ALIGNMENT) "\n"
|
---|
71 | # define INCBIN_ALIGN_BYTE ".balign 1\n"
|
---|
72 | #elif defined(INCBIN_ARM)
|
---|
73 | /*
|
---|
74 | * On arm assemblers, the alignment value is calculated as (1 << n) where `n' is
|
---|
75 | * the shift count. This is the value passed to `.align'
|
---|
76 | */
|
---|
77 | # define INCBIN_ALIGN_HOST ".align" INCBIN_STRINGIZE(INCBIN_ALIGNMENT_INDEX) "\n"
|
---|
78 | # define INCBIN_ALIGN_BYTE ".align 0\n"
|
---|
79 | #else
|
---|
80 | /* We assume other inline assembler's treat `.align' as `.balign' */
|
---|
81 | # define INCBIN_ALIGN_HOST ".align" INCBIN_STRINGIZE(INCBIN_ALIGNMENT) "\n"
|
---|
82 | # define INCBIN_ALIGN_BYTE ".align 1\n"
|
---|
83 | #endif
|
---|
84 |
|
---|
85 | /* INCBIN_CONST is used by incbin.c generated files */
|
---|
86 | #if defined(__cplusplus)
|
---|
87 | # define INCBIN_EXTERNAL extern "C"
|
---|
88 | # define INCBIN_CONST extern const
|
---|
89 | #else
|
---|
90 | # define INCBIN_EXTERNAL extern
|
---|
91 | # define INCBIN_CONST const
|
---|
92 | #endif
|
---|
93 |
|
---|
94 | #if defined(__APPLE__)
|
---|
95 | /* The directives are different for Apple branded compilers */
|
---|
96 | # define INCBIN_SECTION ".rodata\n"
|
---|
97 | # define INCBIN_GLOBAL(NAME) ".globl " INCBIN_STRINGIZE(INCBIN_PREFIX) #NAME "\n"
|
---|
98 | # define INCBIN_INT ".long "
|
---|
99 | # define INCBIN_MANGLE "_"
|
---|
100 | # define INCBIN_BYTE ".byte "
|
---|
101 | # define INCBIN_TYPE(...)
|
---|
102 | #else
|
---|
103 | # define INCBIN_SECTION ".section .rodata\n"
|
---|
104 | # define INCBIN_GLOBAL(NAME) ".global " INCBIN_STRINGIZE(INCBIN_PREFIX) #NAME "\n"
|
---|
105 | # define INCBIN_INT ".int "
|
---|
106 | # if defined(__USER_LABEL_PREFIX__)
|
---|
107 | # define INCBIN_MANGLE INCBIN_STRINGIZE(__USER_LABEL_PREFIX__)
|
---|
108 | # else
|
---|
109 | # define INCBIN_MANGLE ""
|
---|
110 | # endif
|
---|
111 | # if defined(INCBIN_ARM)
|
---|
112 | /* On arm assemblers, `@' is used as a line comment token */
|
---|
113 | # define INCBIN_TYPE(NAME) ".type " INCBIN_STRINGIZE(INCBIN_PREFIX) #NAME ", %object\n"
|
---|
114 | # elif defined(__MINGW32__) || defined(__MINGW64__)
|
---|
115 | /* Mingw doesn't support this directive either */
|
---|
116 | # define INCBIN_TYPE(NAME)
|
---|
117 | # else
|
---|
118 | /* It's safe to use `@' on other architectures */
|
---|
119 | # define INCBIN_TYPE(NAME) ".type " INCBIN_STRINGIZE(INCBIN_PREFIX) #NAME ", @object\n"
|
---|
120 | # endif
|
---|
121 | # define INCBIN_BYTE ".byte "
|
---|
122 | #endif
|
---|
123 |
|
---|
124 | /* List of style types used for symbol names */
|
---|
125 | #define INCBIN_STYLE_CAMEL 0
|
---|
126 | #define INCBIN_STYLE_SNAKE 1
|
---|
127 |
|
---|
128 | /**
|
---|
129 | * @brief Specify the prefix to use for symbol names.
|
---|
130 | *
|
---|
131 | * By default this is `g', producing symbols of the form:
|
---|
132 | * @code
|
---|
133 | * #include "incbin.h"
|
---|
134 | * INCBIN(Foo, "foo.txt");
|
---|
135 | *
|
---|
136 | * // Now you have the following symbols:
|
---|
137 | * // const unsigned char gFooData[];
|
---|
138 | * // const unsigned char gFooEnd;
|
---|
139 | * // const unsigned int gFooSize;
|
---|
140 | * @endcode
|
---|
141 | *
|
---|
142 | * If however you specify a prefix before including: e.g:
|
---|
143 | * @code
|
---|
144 | * #define INCBIN_PREFIX incbin
|
---|
145 | * #include "incbin.h"
|
---|
146 | * INCBIN(Foo, "foo.txt");
|
---|
147 | *
|
---|
148 | * // Now you have the following symbols instead:
|
---|
149 | * // const unsigned char incbinFooData[];
|
---|
150 | * // const unsigned char incbinFooEnd;
|
---|
151 | * // const unsigned int incbinFooSize;
|
---|
152 | * @endcode
|
---|
153 | */
|
---|
154 | #if !defined(INCBIN_PREFIX)
|
---|
155 | # define INCBIN_PREFIX g
|
---|
156 | #endif
|
---|
157 |
|
---|
158 | /**
|
---|
159 | * @brief Specify the style used for symbol names.
|
---|
160 | *
|
---|
161 | * Possible options are
|
---|
162 | * - INCBIN_STYLE_CAMEL "CamelCase"
|
---|
163 | * - INCBIN_STYLE_SNAKE "snake_case"
|
---|
164 | *
|
---|
165 | * Default option is *INCBIN_STYLE_CAMEL* producing symbols of the form:
|
---|
166 | * @code
|
---|
167 | * #include "incbin.h"
|
---|
168 | * INCBIN(Foo, "foo.txt");
|
---|
169 | *
|
---|
170 | * // Now you have the following symbols:
|
---|
171 | * // const unsigned char <prefix>FooData[];
|
---|
172 | * // const unsigned char <prefix>FooEnd;
|
---|
173 | * // const unsigned int <prefix>FooSize;
|
---|
174 | * @endcode
|
---|
175 | *
|
---|
176 | * If however you specify a style before including: e.g:
|
---|
177 | * @code
|
---|
178 | * #define INCBIN_STYLE INCBIN_STYLE_SNAKE
|
---|
179 | * #include "incbin.h"
|
---|
180 | * INCBIN(foo, "foo.txt");
|
---|
181 | *
|
---|
182 | * // Now you have the following symbols:
|
---|
183 | * // const unsigned char <prefix>foo_data[];
|
---|
184 | * // const unsigned char <prefix>foo_end;
|
---|
185 | * // const unsigned int <prefix>foo_size;
|
---|
186 | * @endcode
|
---|
187 | */
|
---|
188 | #if !defined(INCBIN_STYLE)
|
---|
189 | # define INCBIN_STYLE INCBIN_STYLE_CAMEL
|
---|
190 | #endif
|
---|
191 |
|
---|
192 | /* Style lookup tables */
|
---|
193 | #define INCBIN_STYLE_0_DATA Data
|
---|
194 | #define INCBIN_STYLE_0_END End
|
---|
195 | #define INCBIN_STYLE_0_SIZE Size
|
---|
196 | #define INCBIN_STYLE_1_DATA _data
|
---|
197 | #define INCBIN_STYLE_1_END _end
|
---|
198 | #define INCBIN_STYLE_1_SIZE _size
|
---|
199 |
|
---|
200 | /* Style lookup: returning identifier */
|
---|
201 | #define INCBIN_STYLE_IDENT(TYPE) \
|
---|
202 | INCBIN_CONCATENATE( \
|
---|
203 | INCBIN_STYLE_, \
|
---|
204 | INCBIN_CONCATENATE( \
|
---|
205 | INCBIN_EVAL(INCBIN_STYLE), \
|
---|
206 | INCBIN_CONCATENATE(_, TYPE)))
|
---|
207 |
|
---|
208 | /* Style lookup: returning string literal */
|
---|
209 | #define INCBIN_STYLE_STRING(TYPE) \
|
---|
210 | INCBIN_STRINGIZE( \
|
---|
211 | INCBIN_STYLE_IDENT(TYPE)) \
|
---|
212 |
|
---|
213 | /* Generate the global labels by indirectly invoking the macro with our style
|
---|
214 | * type and concatenating the name against them. */
|
---|
215 | #define INCBIN_GLOBAL_LABELS(NAME, TYPE) \
|
---|
216 | INCBIN_INVOKE( \
|
---|
217 | INCBIN_GLOBAL, \
|
---|
218 | INCBIN_CONCATENATE( \
|
---|
219 | NAME, \
|
---|
220 | INCBIN_INVOKE( \
|
---|
221 | INCBIN_STYLE_IDENT, \
|
---|
222 | TYPE))) \
|
---|
223 | INCBIN_INVOKE( \
|
---|
224 | INCBIN_TYPE, \
|
---|
225 | INCBIN_CONCATENATE( \
|
---|
226 | NAME, \
|
---|
227 | INCBIN_INVOKE( \
|
---|
228 | INCBIN_STYLE_IDENT, \
|
---|
229 | TYPE)))
|
---|
230 |
|
---|
231 | /**
|
---|
232 | * @brief Externally reference binary data included in another translation unit.
|
---|
233 | *
|
---|
234 | * Produces three external symbols that reference the binary data included in
|
---|
235 | * another translation unit.
|
---|
236 | *
|
---|
237 | * The symbol names are a concatenation of `INCBIN_PREFIX' before *NAME*; with
|
---|
238 | * "Data", as well as "End" and "Size" after. An example is provided below.
|
---|
239 | *
|
---|
240 | * @param NAME The name given for the binary data
|
---|
241 | *
|
---|
242 | * @code
|
---|
243 | * INCBIN_EXTERN(Foo);
|
---|
244 | *
|
---|
245 | * // Now you have the following symbols:
|
---|
246 | * // extern const unsigned char <prefix>FooData[];
|
---|
247 | * // extern const unsigned char <prefix>FooEnd;
|
---|
248 | * // extern const unsigned int <prefix>FooSize;
|
---|
249 | * @endcode
|
---|
250 | */
|
---|
251 | #define INCBIN_EXTERN(NAME) \
|
---|
252 | INCBIN_EXTERNAL INCBIN_ALIGN unsigned char \
|
---|
253 | INCBIN_CONCATENATE( \
|
---|
254 | INCBIN_CONCATENATE(INCBIN_PREFIX, NAME), \
|
---|
255 | INCBIN_STYLE_IDENT(DATA))[]; \
|
---|
256 | INCBIN_EXTERNAL INCBIN_ALIGN unsigned char * \
|
---|
257 | INCBIN_CONCATENATE( \
|
---|
258 | INCBIN_CONCATENATE(INCBIN_PREFIX, NAME), \
|
---|
259 | INCBIN_STYLE_IDENT(END)); \
|
---|
260 | INCBIN_EXTERNAL unsigned int \
|
---|
261 | INCBIN_CONCATENATE( \
|
---|
262 | INCBIN_CONCATENATE(INCBIN_PREFIX, NAME), \
|
---|
263 | INCBIN_STYLE_IDENT(SIZE))
|
---|
264 |
|
---|
265 | /**
|
---|
266 | * @brief Include a binary file into the current translation unit.
|
---|
267 | *
|
---|
268 | * Includes a binary file into the current translation unit, producing three symbols
|
---|
269 | * for objects that encode the data and size respectively.
|
---|
270 | *
|
---|
271 | * The symbol names are a concatenation of `INCBIN_PREFIX' before *NAME*; with
|
---|
272 | * "Data", as well as "End" and "Size" after. An example is provided below.
|
---|
273 | *
|
---|
274 | * @param NAME The name to associate with this binary data (as an identifier.)
|
---|
275 | * @param FILENAME The file to include (as a string literal.)
|
---|
276 | *
|
---|
277 | * @code
|
---|
278 | * INCBIN(Icon, "icon.png");
|
---|
279 | *
|
---|
280 | * // Now you have the following symbols:
|
---|
281 | * // const unsigned char <prefix>IconData[];
|
---|
282 | * // const unsigned char <prefix>IconEnd;
|
---|
283 | * // const unsigned int <prefix>IconSize;
|
---|
284 | * @endcode
|
---|
285 | *
|
---|
286 | * @warning This must be used in global scope
|
---|
287 | * @warning The identifiers may be different if INCBIN_STYLE is not default
|
---|
288 | *
|
---|
289 | * To externally reference the data included by this in another translation unit
|
---|
290 | * please @see INCBIN_EXTERN.
|
---|
291 | */
|
---|
292 | #ifdef _MSC_VER
|
---|
293 | #define INCBIN(NAME, FILENAME) \
|
---|
294 | INCBIN_EXTERN(NAME)
|
---|
295 | #else
|
---|
296 | #define INCBIN(NAME, FILENAME) \
|
---|
297 | __asm__(INCBIN_SECTION \
|
---|
298 | INCBIN_GLOBAL_LABELS(NAME, DATA) \
|
---|
299 | INCBIN_ALIGN_HOST \
|
---|
300 | INCBIN_MANGLE INCBIN_STRINGIZE(INCBIN_PREFIX) #NAME INCBIN_STYLE_STRING(DATA) ":\n" \
|
---|
301 | INCBIN_MACRO " \"" FILENAME "\"\n" \
|
---|
302 | INCBIN_GLOBAL_LABELS(NAME, END) \
|
---|
303 | INCBIN_ALIGN_BYTE \
|
---|
304 | INCBIN_MANGLE INCBIN_STRINGIZE(INCBIN_PREFIX) #NAME INCBIN_STYLE_STRING(END) ":\n" \
|
---|
305 | INCBIN_BYTE "1\n" \
|
---|
306 | INCBIN_GLOBAL_LABELS(NAME, SIZE) \
|
---|
307 | INCBIN_ALIGN_HOST \
|
---|
308 | INCBIN_MANGLE INCBIN_STRINGIZE(INCBIN_PREFIX) #NAME INCBIN_STYLE_STRING(SIZE) ":\n" \
|
---|
309 | INCBIN_INT INCBIN_MANGLE INCBIN_STRINGIZE(INCBIN_PREFIX) #NAME INCBIN_STYLE_STRING(END) " - " \
|
---|
310 | INCBIN_MANGLE INCBIN_STRINGIZE(INCBIN_PREFIX) #NAME INCBIN_STYLE_STRING(DATA) "\n" \
|
---|
311 | ); \
|
---|
312 | INCBIN_EXTERN(NAME)
|
---|
313 |
|
---|
314 | #endif
|
---|
315 | #endif
|
---|