incbin.h 11 KB

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