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