29 parser = argparse.ArgumentParser(
30 formatter_class=argparse.RawDescriptionHelpFormatter,
31 description='Convert resource files to embedded read only data.',
32 epilog='''The output (when compiled and linked) can be used as:
33struct SkEmbeddedResource {const uint8_t* data; const size_t size;};
34struct SkEmbeddedHeader {const SkEmbeddedResource* entries; const int count;};
35extern "C" SkEmbeddedHeader const NAME;''')
36 parser.add_argument('--align', default=1, type=int,
37 help='minimum alignment (in bytes) of resource data')
38 parser.add_argument('--name', default='_resource', type=str,
39 help='the name of the c identifier to export')
40 parser.add_argument('--input', required=True, type=argparse.FileType('rb'),
41 nargs='+', help='list of resource files to embed')
42 parser.add_argument('--output', required=True, type=argparse.FileType('w'),
43 help='the name of the cpp file to output')
44 args = parser.parse_args()
45
46 out = args.output.write;
47 out(
'#include <stddef.h>\n')
48 out(
'#include <stdint.h>\n')
49
50
51 index = 0
52 for f in args.input:
53 out(
'alignas({1:d}) static const uint8_t resource{0:d}[] = {{\n'
54 .
format(index, args.align))
55 bytes_written = 0
56 bytes_on_line = 0
59 bytes_written += 1
60 bytes_on_line += 1
61 if bytes_on_line >= 32:
63 bytes_on_line = 0
65 out(
'static const size_t resource{0:d}_size = {1:d};\n'
66 .
format(index, bytes_written))
67 index += 1
68
69
70 out(
'struct SkEmbeddedResource { const uint8_t* d; const size_t s; };\n')
71 out(
'static const SkEmbeddedResource header[] = {\n')
72 index = 0
73 for f in args.input:
74 out(
' {{ resource{0:d}, resource{0:d}_size }},\n'.
format(index))
75 index += 1
77 out(
'static const int header_count = {0:d};\n'.
format(index))
78
79
80 out(
'struct SkEmbeddedHeader {const SkEmbeddedResource* e; const int c;};\n')
81 out(
'extern "C" const SkEmbeddedHeader {0:s} = {{ header, header_count }};\n'
83
84
uint32_t uint32_t * format