OLD | NEW |
---|---|
(Empty) | |
1 #!/usr/bin/env python | |
2 # Copyright 2015 The Chromium Authors. All rights reserved. | |
3 # Use of this source code is governed by a BSD-style license that can be | |
4 # found in the LICENSE file. | |
5 | |
6 import argparse | |
7 import hashlib | |
8 import os | |
9 import sys | |
10 | |
11 def main(): | |
12 """Command line utility to embed file in a C executable.""" | |
13 parser = argparse.ArgumentParser( | |
14 description='Generate a source file to embed the content of a file') | |
15 | |
16 parser.add_argument('source') | |
17 parser.add_argument('out_dir') | |
18 parser.add_argument('variable') | |
19 | |
20 opts = parser.parse_args() | |
21 | |
22 if not os.path.exists(opts.out_dir): | |
23 os.makedirs(opts.out_dir) | |
24 | |
25 header = os.path.join(opts.out_dir, '%s.h' % opts.variable) | |
26 c_file = os.path.join(opts.out_dir, '%s.cc' % opts.variable) | |
27 | |
28 data = None | |
29 with open(opts.source, "rb") as f: | |
30 data = f.read() | |
31 | |
32 with open(header, "w") as f: | |
33 f.write('// Generated file. Do not modify.\n') | |
34 f.write('\n') | |
35 f.write('#include "mojo/tools/embed/data.h"\n') | |
36 f.write('\n') | |
37 f.write('namespace embed {\n') | |
38 f.write('extern const mojo::embed::Data %s;\n' % opts.variable); | |
39 f.write('} // namespace embed\n') | |
40 | |
41 sha1hash = hashlib.sha1(data).hexdigest() | |
42 values = ["0x%02x" % ord(c) for c in data] | |
43 lines = [] | |
44 chunk_size = 16 | |
45 for i in range(0, len(values), chunk_size): | |
46 lines.append(" " + ", ".join(values[i: i + chunk_size])) | |
47 | |
48 with open(c_file, "w") as f: | |
49 f.write('// Generated file. Do not modify.\n') | |
50 f.write('\n') | |
51 f.write('#include "mojo/tools/embed/data.h"\n') | |
52 f.write('\n') | |
53 f.write('namespace embed {\n') | |
abarth-chromium
2015/03/06 21:41:14
Seems odd to put this all into the same namespace.
| |
54 f.write('namespace {\n') | |
55 f.write("const char data[%d] = {\n" % len(data)) | |
56 f.write(",\n".join(lines)) | |
57 f.write("\n};\n") | |
58 f.write('} // namespace\n') | |
59 f.write('\n') | |
60 f.write('extern const mojo::embed::Data %s;\n' % opts.variable); | |
61 f.write('const mojo::embed::Data %s = {\n' % opts.variable); | |
62 f.write(' "%s",\n' % sha1hash) | |
63 f.write(' data,\n') | |
64 f.write(' sizeof(data)\n') | |
65 f.write('};\n'); | |
66 f.write('\n') | |
67 f.write('} // namespace embed\n') | |
68 | |
69 if __name__ == '__main__': | |
70 main() | |
OLD | NEW |