Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
xxd.py
Go to the documentation of this file.
1# Copyright 2013 The Flutter Authors. All rights reserved.
2# Use of this source code is governed by a BSD-style license that can be
3# found in the LICENSE file.
4
5import argparse
6import errno
7import os
8
9
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.
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
85if __name__ == '__main__':
86 main()
Definition main.py:1
main()
Definition xxd.py:22
make_directories(path)
Definition xxd.py:10