Flutter Engine
The Flutter Engine
Functions
xxd Namespace Reference

Functions

def make_directories (path)
 
def main ()
 

Function Documentation

◆ main()

def xxd.main ( )

Definition at line 22 of file xxd.py.

22def main():
23 parser = argparse.ArgumentParser()
24 parser.add_argument(
25 '--symbol-name', type=str, required=True, help='The name of the symbol referencing the data.'
26 )
27 parser.add_argument(
28 '--output-header',
29 type=str,
30 required=True,
31 help='The header file containing the symbol reference.'
32 )
33 parser.add_argument(
34 '--output-source', type=str, required=True, help='The source file containing the file bytes.'
35 )
36 parser.add_argument(
37 '--source',
38 type=str,
39 required=True,
40 help='The source file whose contents to embed in the output source file.'
41 )
42
43 args = parser.parse_args()
44
45 assert os.path.exists(args.source)
46
47 output_header = os.path.abspath(args.output_header)
48 output_source = os.path.abspath(args.output_source)
49 output_header_basename = output_header[output_header.rfind('/') + 1:]
50
51 make_directories(os.path.dirname(output_header))
52 make_directories(os.path.dirname(output_source))
53
54 with open(args.source, 'rb') as source, open(output_source, 'w') as output:
55 data_len = 0
56 output.write(f'#include "{output_header_basename}"\n')
57 output.write('#include <cstddef>\n')
58 output.write(
59 f'alignas(std::max_align_t) const unsigned char impeller_{args.symbol_name}_data[] =\n'
60 )
61 output.write('{\n')
62 while True:
63 byte = source.read(1)
64 if not byte:
65 break
66 data_len += 1
67 output.write(f'{ord(byte)},')
68 output.write('};\n')
69 output.write(f'const unsigned long impeller_{args.symbol_name}_length = {data_len};\n')
70
71 with open(output_header, 'w') as output:
72 output.write('#pragma once\n')
73 output.write('#ifdef __cplusplus\n')
74 output.write('extern "C" {\n')
75 output.write('#endif\n\n')
76
77 output.write(f'extern const unsigned char impeller_{args.symbol_name}_data[];\n')
78 output.write(f'extern const unsigned long impeller_{args.symbol_name}_length;\n\n')
79
80 output.write('#ifdef __cplusplus\n')
81 output.write('}\n')
82 output.write('#endif\n')
83
84
def make_directories(path)
Definition: xxd.py:10
def main()
Definition: xxd.py:22

◆ make_directories()

def xxd.make_directories (   path)

Definition at line 10 of file xxd.py.

10def make_directories(path):
11 try:
12 os.makedirs(path)
13 except OSError as exc:
14 if exc.errno == errno.EEXIST and os.path.isdir(path):
15 pass
16 else:
17 raise
18
19
20# Dump the bytes of file into a C translation unit.
21# This can be used to embed the file contents into a binary.