summaryrefslogtreecommitdiffstats
path: root/source/base/base_embed.h
blob: 08ea840d854fddc7e127b78e16fffab90ec70dbc (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#ifndef BASE_EMBED_H
#define BASE_EMBED_H

// NOTE: bake a file into the binary (fonts, help.txt) so the release build is one file. .incbin
// works in gcc and clang on elf, coff and mach-o; the path is relative to the directory the
// compiler runs in, which is the repo root for compile.sh, build.bat and build_macos.sh.
// name points at the bytes, name##_end one past them; a 0 byte follows so text is terminated.

#if PLATFORM_APPLE
#define EMBED_SECTION ".section __TEXT,__const\n"
#define EMBED_SYMBOL(name) "_" #name
#elif PLATFORM_WINDOWS
#define EMBED_SECTION ".section .rdata,\"dr\"\n"
#define EMBED_SYMBOL(name) #name
#else
#define EMBED_SECTION ".section .rodata\n"
#define EMBED_SYMBOL(name) #name
#endif

#define EMBED_FILE(name, path)                                          \
	extern "C" const u8 name[];                                         \
	extern "C" const u8 name##_end[];                                   \
	__asm__(EMBED_SECTION                                               \
	        ".balign 16\n"                                              \
	        ".globl " EMBED_SYMBOL(name) "\n"                           \
	        EMBED_SYMBOL(name) ":\n"                                    \
	        ".incbin \"" path "\"\n"                                    \
	        ".globl " EMBED_SYMBOL(name##_end) "\n"                     \
	        EMBED_SYMBOL(name##_end) ":\n"                              \
	        ".byte 0\n"                                                 \
	        ".text\n")

#define EmbeddedString8(name) ToString8Size((u8 *)(name), (u64)((name##_end) - (name)))

#endif // BASE_EMBED_H