Update Incbin Library

No functional change
This commit is contained in:
Disservin
2024-12-13 16:59:34 +01:00
parent b822fdf2f2
commit f414d490bc
2 changed files with 170 additions and 60 deletions
+167 -59
View File
@@ -3,8 +3,8 @@
* @author Dale Weiler * @author Dale Weiler
* @brief Utility for including binary files * @brief Utility for including binary files
* *
* Facilities for including binary files into the current translation unit * Facilities for including binary files into the current translation unit and
* and making use of them externally in other translation units. * making use from them externally in other translation units.
*/ */
#ifndef INCBIN_HDR #ifndef INCBIN_HDR
#define INCBIN_HDR #define INCBIN_HDR
@@ -26,7 +26,9 @@
defined(__SSSE3__) || \ defined(__SSSE3__) || \
defined(__SSE4_1__) || \ defined(__SSE4_1__) || \
defined(__SSE4_2__) || \ defined(__SSE4_2__) || \
defined(__neon__) defined(__neon__) || \
defined(__ARM_NEON) || \
defined(__ALTIVEC__)
# define INCBIN_ALIGNMENT_INDEX 4 # define INCBIN_ALIGNMENT_INDEX 4
#elif ULONG_MAX != 0xffffffffu #elif ULONG_MAX != 0xffffffffu
# define INCBIN_ALIGNMENT_INDEX 3 # define INCBIN_ALIGNMENT_INDEX 3
@@ -64,6 +66,9 @@
X X
#define INCBIN_INVOKE(N, ...) \ #define INCBIN_INVOKE(N, ...) \
INCBIN_EVAL(N(__VA_ARGS__)) INCBIN_EVAL(N(__VA_ARGS__))
/* Variable argument count for overloading by arity */
#define INCBIN_VA_ARG_COUNTER(_1, _2, _3, N, ...) N
#define INCBIN_VA_ARGC(...) INCBIN_VA_ARG_COUNTER(__VA_ARGS__, 3, 2, 1, 0)
/* Green Hills uses a different directive for including binary data */ /* Green Hills uses a different directive for including binary data */
#if defined(__ghs__) #if defined(__ghs__)
@@ -117,29 +122,50 @@
#endif #endif
/** /**
* @brief Optionally override the linker section into which data is emitted. * @brief Optionally override the linker section into which size and data is
* * emitted.
* @warning If you use this facility, you'll have to deal with platform-specific linker output *
* section naming on your own * @warning If you use this facility, you might have to deal with
* * platform-specific linker output section naming on your own.
* Overriding the default linker output section, e.g for esp8266/Arduino:
* @code
* #define INCBIN_OUTPUT_SECTION ".irom.text"
* #include "incbin.h"
* INCBIN(Foo, "foo.txt");
* // Data is emitted into program memory that never gets copied to RAM
* @endcode
*/ */
#if !defined(INCBIN_OUTPUT_SECTION) #if !defined(INCBIN_OUTPUT_SECTION)
# if defined(__APPLE__) # if defined(__APPLE__)
# define INCBIN_OUTPUT_SECTION ".const_data" # define INCBIN_OUTPUT_SECTION ".const_data"
# else # else
# define INCBIN_OUTPUT_SECTION ".rodata" # define INCBIN_OUTPUT_SECTION ".rodata"
# endif # endif
#endif #endif
/**
* @brief Optionally override the linker section into which data is emitted.
*
* @warning If you use this facility, you might have to deal with
* platform-specific linker output section naming on your own.
*/
#if !defined(INCBIN_OUTPUT_DATA_SECTION)
# define INCBIN_OUTPUT_DATA_SECTION INCBIN_OUTPUT_SECTION
#endif
/**
* @brief Optionally override the linker section into which size is emitted.
*
* @warning If you use this facility, you might have to deal with
* platform-specific linker output section naming on your own.
*
* @note This is useful for Harvard architectures where program memory cannot
* be directly read from the program without special instructions. With this you
* can chose to put the size variable in RAM rather than ROM.
*/
#if !defined(INCBIN_OUTPUT_SIZE_SECTION)
# define INCBIN_OUTPUT_SIZE_SECTION INCBIN_OUTPUT_SECTION
#endif
#if defined(__APPLE__) #if defined(__APPLE__)
/* The directives are different for Apple-branded compilers */ # include "TargetConditionals.h"
# if defined(TARGET_OS_IPHONE) && !defined(INCBIN_SILENCE_BITCODE_WARNING)
# warning "incbin is incompatible with bitcode. Using the library will break upload to App Store if you have bitcode enabled. Add `#define INCBIN_SILENCE_BITCODE_WARNING` before including this header to silence this warning."
# endif
/* The directives are different for Apple branded compilers */
# define INCBIN_SECTION INCBIN_OUTPUT_SECTION "\n" # define INCBIN_SECTION INCBIN_OUTPUT_SECTION "\n"
# define INCBIN_GLOBAL(NAME) ".globl " INCBIN_MANGLE INCBIN_STRINGIZE(INCBIN_PREFIX) #NAME "\n" # define INCBIN_GLOBAL(NAME) ".globl " INCBIN_MANGLE INCBIN_STRINGIZE(INCBIN_PREFIX) #NAME "\n"
# define INCBIN_INT ".long " # define INCBIN_INT ".long "
@@ -179,27 +205,17 @@
/** /**
* @brief Specify the prefix to use for symbol names. * @brief Specify the prefix to use for symbol names.
* *
* By default this is `g', producing symbols of the form: * @note By default this is "g".
* @code
* #include "incbin.h"
* INCBIN(Foo, "foo.txt");
* *
* // Now you have the following symbols:
* // const unsigned char gFooData[];
* // const unsigned char *const gFooEnd;
* // const unsigned int gFooSize;
* @endcode
*
* If however you specify a prefix before including: e.g:
* @code * @code
* #define INCBIN_PREFIX incbin * #define INCBIN_PREFIX incbin
* #include "incbin.h" * #include "incbin.h"
* INCBIN(Foo, "foo.txt"); * INCBIN(Foo, "foo.txt");
* *
* // Now you have the following symbols instead: * // Now you have the following symbols instead:
* // const unsigned char incbinFooData[]; * // const unsigned char incbinFoo<data>[];
* // const unsigned char *const incbinFooEnd; * // const unsigned char *const incbinFoo<end>;
* // const unsigned int incbinFooSize; * // const unsigned int incbinFoo<size>;
* @endcode * @endcode
*/ */
#if !defined(INCBIN_PREFIX) #if !defined(INCBIN_PREFIX)
@@ -213,18 +229,8 @@
* - INCBIN_STYLE_CAMEL "CamelCase" * - INCBIN_STYLE_CAMEL "CamelCase"
* - INCBIN_STYLE_SNAKE "snake_case" * - INCBIN_STYLE_SNAKE "snake_case"
* *
* Default option is *INCBIN_STYLE_CAMEL* producing symbols of the form: * @note By default this is INCBIN_STYLE_CAMEL
* @code
* #include "incbin.h"
* INCBIN(Foo, "foo.txt");
* *
* // Now you have the following symbols:
* // const unsigned char <prefix>FooData[];
* // const unsigned char *const <prefix>FooEnd;
* // const unsigned int <prefix>FooSize;
* @endcode
*
* If however you specify a style before including: e.g:
* @code * @code
* #define INCBIN_STYLE INCBIN_STYLE_SNAKE * #define INCBIN_STYLE INCBIN_STYLE_SNAKE
* #include "incbin.h" * #include "incbin.h"
@@ -261,8 +267,8 @@
INCBIN_STRINGIZE( \ INCBIN_STRINGIZE( \
INCBIN_STYLE_IDENT(TYPE)) \ INCBIN_STYLE_IDENT(TYPE)) \
/* Generate the global labels by indirectly invoking the macro /* Generate the global labels by indirectly invoking the macro with our style
* with our style type and concatenate the name against them. */ * type and concatenating the name against them. */
#define INCBIN_GLOBAL_LABELS(NAME, TYPE) \ #define INCBIN_GLOBAL_LABELS(NAME, TYPE) \
INCBIN_INVOKE( \ INCBIN_INVOKE( \
INCBIN_GLOBAL, \ INCBIN_GLOBAL, \
@@ -288,23 +294,38 @@
* The symbol names are a concatenation of `INCBIN_PREFIX' before *NAME*; with * The symbol names are a concatenation of `INCBIN_PREFIX' before *NAME*; with
* "Data", as well as "End" and "Size" after. An example is provided below. * "Data", as well as "End" and "Size" after. An example is provided below.
* *
* @param TYPE Optional array type. Omitting this picks a default of `unsigned char`.
* @param NAME The name given for the binary data * @param NAME The name given for the binary data
* *
* @code * @code
* INCBIN_EXTERN(Foo); * INCBIN_EXTERN(Foo);
* *
* // Now you have the following symbols: * // Now you have the following symbols:
* // extern const unsigned char <prefix>FooData[]; * // extern const unsigned char <prefix>Foo<data>[];
* // extern const unsigned char *const <prefix>FooEnd; * // extern const unsigned char *const <prefix>Foo<end>;
* // extern const unsigned int <prefix>FooSize; * // extern const unsigned int <prefix>Foo<size>;
* @endcode
*
* You may specify a custom optional data type as well as the first argument.
* @code
* INCBIN_EXTERN(custom_type, Foo);
*
* // Now you have the following symbols:
* // extern const custom_type <prefix>Foo<data>[];
* // extern const custom_type *const <prefix>Foo<end>;
* // extern const unsigned int <prefix>Foo<size>;
* @endcode * @endcode
*/ */
#define INCBIN_EXTERN(NAME) \ #define INCBIN_EXTERN(...) \
INCBIN_EXTERNAL const INCBIN_ALIGN unsigned char \ INCBIN_CONCATENATE(INCBIN_EXTERN_, INCBIN_VA_ARGC(__VA_ARGS__))(__VA_ARGS__)
#define INCBIN_EXTERN_1(NAME, ...) \
INCBIN_EXTERN_2(unsigned char, NAME)
#define INCBIN_EXTERN_2(TYPE, NAME) \
INCBIN_EXTERNAL const INCBIN_ALIGN TYPE \
INCBIN_CONCATENATE( \ INCBIN_CONCATENATE( \
INCBIN_CONCATENATE(INCBIN_PREFIX, NAME), \ INCBIN_CONCATENATE(INCBIN_PREFIX, NAME), \
INCBIN_STYLE_IDENT(DATA))[]; \ INCBIN_STYLE_IDENT(DATA))[]; \
INCBIN_EXTERNAL const INCBIN_ALIGN unsigned char *const \ INCBIN_EXTERNAL const INCBIN_ALIGN TYPE *const \
INCBIN_CONCATENATE( \ INCBIN_CONCATENATE( \
INCBIN_CONCATENATE(INCBIN_PREFIX, NAME), \ INCBIN_CONCATENATE(INCBIN_PREFIX, NAME), \
INCBIN_STYLE_IDENT(END)); \ INCBIN_STYLE_IDENT(END)); \
@@ -313,6 +334,29 @@
INCBIN_CONCATENATE(INCBIN_PREFIX, NAME), \ INCBIN_CONCATENATE(INCBIN_PREFIX, NAME), \
INCBIN_STYLE_IDENT(SIZE)) INCBIN_STYLE_IDENT(SIZE))
/**
* @brief Externally reference textual data included in another translation unit.
*
* Produces three external symbols that reference the textual data included in
* another translation unit.
*
* The symbol names are a concatenation of `INCBIN_PREFIX' before *NAME*; with
* "Data", as well as "End" and "Size" after. An example is provided below.
*
* @param NAME The name given for the textual data
*
* @code
* INCBIN_EXTERN(Foo);
*
* // Now you have the following symbols:
* // extern const char <prefix>Foo<data>[];
* // extern const char *const <prefix>Foo<end>;
* // extern const unsigned int <prefix>Foo<size>;
* @endcode
*/
#define INCTXT_EXTERN(NAME) \
INCBIN_EXTERN_2(char, NAME)
/** /**
* @brief Include a binary file into the current translation unit. * @brief Include a binary file into the current translation unit.
* *
@@ -322,6 +366,7 @@
* The symbol names are a concatenation of `INCBIN_PREFIX' before *NAME*; with * The symbol names are a concatenation of `INCBIN_PREFIX' before *NAME*; with
* "Data", as well as "End" and "Size" after. An example is provided below. * "Data", as well as "End" and "Size" after. An example is provided below.
* *
* @param TYPE Optional array type. Omitting this picks a default of `unsigned char`.
* @param NAME The name to associate with this binary data (as an identifier.) * @param NAME The name to associate with this binary data (as an identifier.)
* @param FILENAME The file to include (as a string literal.) * @param FILENAME The file to include (as a string literal.)
* *
@@ -329,9 +374,20 @@
* INCBIN(Icon, "icon.png"); * INCBIN(Icon, "icon.png");
* *
* // Now you have the following symbols: * // Now you have the following symbols:
* // const unsigned char <prefix>IconData[]; * // const unsigned char <prefix>Icon<data>[];
* // const unsigned char *const <prefix>IconEnd; * // const unsigned char *const <prefix>Icon<end>;
* // const unsigned int <prefix>IconSize; * // const unsigned int <prefix>Icon<size>;
* @endcode
*
* You may specify a custom optional data type as well as the first argument.
* These macros are specialized by arity.
* @code
* INCBIN(custom_type, Icon, "icon.png");
*
* // Now you have the following symbols:
* // const custom_type <prefix>Icon<data>[];
* // const custom_type *const <prefix>Icon<end>;
* // const unsigned int <prefix>Icon<size>;
* @endcode * @endcode
* *
* @warning This must be used in global scope * @warning This must be used in global scope
@@ -341,15 +397,28 @@
* please @see INCBIN_EXTERN. * please @see INCBIN_EXTERN.
*/ */
#ifdef _MSC_VER #ifdef _MSC_VER
#define INCBIN(NAME, FILENAME) \ # define INCBIN(NAME, FILENAME) \
INCBIN_EXTERN(NAME) INCBIN_EXTERN(NAME)
#else #else
#define INCBIN(NAME, FILENAME) \ # define INCBIN(...) \
INCBIN_CONCATENATE(INCBIN_, INCBIN_VA_ARGC(__VA_ARGS__))(__VA_ARGS__)
# if defined(__GNUC__)
# define INCBIN_1(...) _Pragma("GCC error \"Single argument INCBIN not allowed\"")
# elif defined(__clang__)
# define INCBIN_1(...) _Pragma("clang error \"Single argument INCBIN not allowed\"")
# else
# define INCBIN_1(...) /* Cannot do anything here */
# endif
# define INCBIN_2(NAME, FILENAME) \
INCBIN_3(unsigned char, NAME, FILENAME)
# define INCBIN_3(TYPE, NAME, FILENAME) INCBIN_COMMON(TYPE, NAME, FILENAME, /* No terminator for binary data */)
# define INCBIN_COMMON(TYPE, NAME, FILENAME, TERMINATOR) \
__asm__(INCBIN_SECTION \ __asm__(INCBIN_SECTION \
INCBIN_GLOBAL_LABELS(NAME, DATA) \ INCBIN_GLOBAL_LABELS(NAME, DATA) \
INCBIN_ALIGN_HOST \ INCBIN_ALIGN_HOST \
INCBIN_MANGLE INCBIN_STRINGIZE(INCBIN_PREFIX) #NAME INCBIN_STYLE_STRING(DATA) ":\n" \ INCBIN_MANGLE INCBIN_STRINGIZE(INCBIN_PREFIX) #NAME INCBIN_STYLE_STRING(DATA) ":\n" \
INCBIN_MACRO " \"" FILENAME "\"\n" \ INCBIN_MACRO " \"" FILENAME "\"\n" \
TERMINATOR \
INCBIN_GLOBAL_LABELS(NAME, END) \ INCBIN_GLOBAL_LABELS(NAME, END) \
INCBIN_ALIGN_BYTE \ INCBIN_ALIGN_BYTE \
INCBIN_MANGLE INCBIN_STRINGIZE(INCBIN_PREFIX) #NAME INCBIN_STYLE_STRING(END) ":\n" \ INCBIN_MANGLE INCBIN_STRINGIZE(INCBIN_PREFIX) #NAME INCBIN_STYLE_STRING(END) ":\n" \
@@ -362,7 +431,46 @@
INCBIN_ALIGN_HOST \ INCBIN_ALIGN_HOST \
".text\n" \ ".text\n" \
); \ ); \
INCBIN_EXTERN(NAME) INCBIN_EXTERN(TYPE, NAME)
#endif
/**
* @brief Include a textual file into the current translation unit.
*
* This behaves the same as INCBIN except it produces char compatible arrays
* and implicitly adds a null-terminator byte, thus the size of data included
* by this is one byte larger than that of INCBIN.
*
* Includes a textual file into the current translation unit, producing three
* symbols for objects that encode the data and size respectively.
*
* The symbol names are a concatenation of `INCBIN_PREFIX' before *NAME*; with
* "Data", as well as "End" and "Size" after. An example is provided below.
*
* @param NAME The name to associate with this binary data (as an identifier.)
* @param FILENAME The file to include (as a string literal.)
*
* @code
* INCTXT(Readme, "readme.txt");
*
* // Now you have the following symbols:
* // const char <prefix>Readme<data>[];
* // const char *const <prefix>Readme<end>;
* // const unsigned int <prefix>Readme<size>;
* @endcode
*
* @warning This must be used in global scope
* @warning The identifiers may be different if INCBIN_STYLE is not default
*
* To externally reference the data included by this in another translation unit
* please @see INCBIN_EXTERN.
*/
#if defined(_MSC_VER)
# define INCTXT(NAME, FILENAME) \
INCBIN_EXTERN(NAME)
#else
# define INCTXT(NAME, FILENAME) \
INCBIN_COMMON(char, NAME, FILENAME, INCBIN_BYTE "0\n")
#endif #endif
#endif
#endif
+3 -1
View File
@@ -26,8 +26,10 @@
#include <type_traits> #include <type_traits>
#include <vector> #include <vector>
#include "../evaluate.h" #define INCBIN_SILENCE_BITCODE_WARNING
#include "../incbin/incbin.h" #include "../incbin/incbin.h"
#include "../evaluate.h"
#include "../memory.h" #include "../memory.h"
#include "../misc.h" #include "../misc.h"
#include "../position.h" #include "../position.h"