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('namespace') |
| 19 parser.add_argument('variable') |
| 20 |
| 21 opts = parser.parse_args() |
| 22 |
| 23 if not os.path.exists(opts.out_dir): |
| 24 os.makedirs(opts.out_dir) |
| 25 |
| 26 header = os.path.join(opts.out_dir, '%s.h' % opts.variable) |
| 27 c_file = os.path.join(opts.out_dir, '%s.cc' % opts.variable) |
| 28 namespaces = opts.namespace.split('::') |
| 29 |
| 30 data = None |
| 31 with open(opts.source, "rb") as f: |
| 32 data = f.read() |
| 33 |
| 34 with open(header, "w") as f: |
| 35 f.write('// Generated file. Do not modify.\n') |
| 36 f.write('\n') |
| 37 f.write('#include "mojo/tools/embed/data.h"\n') |
| 38 f.write('\n') |
| 39 for n in namespaces: |
| 40 f.write('namespace %s {\n' % n) |
| 41 f.write('extern const mojo::embed::Data %s;\n' % opts.variable); |
| 42 for n in reversed(namespaces): |
| 43 f.write('} // namespace %s\n' % n) |
| 44 |
| 45 sha1hash = hashlib.sha1(data).hexdigest() |
| 46 values = ["0x%02x" % ord(c) for c in data] |
| 47 lines = [] |
| 48 chunk_size = 16 |
| 49 for i in range(0, len(values), chunk_size): |
| 50 lines.append(" " + ", ".join(values[i: i + chunk_size])) |
| 51 |
| 52 with open(c_file, "w") as f: |
| 53 f.write('// Generated file. Do not modify.\n') |
| 54 f.write('\n') |
| 55 f.write('#include "mojo/tools/embed/data.h"\n') |
| 56 f.write('\n') |
| 57 for n in namespaces: |
| 58 f.write('namespace %s {\n' % n) |
| 59 f.write('namespace {\n') |
| 60 f.write("const char data[%d] = {\n" % len(data)) |
| 61 f.write(",\n".join(lines)) |
| 62 f.write("\n};\n") |
| 63 f.write('} // namespace\n') |
| 64 f.write('\n') |
| 65 f.write('extern const mojo::embed::Data %s;\n' % opts.variable); |
| 66 f.write('const mojo::embed::Data %s = {\n' % opts.variable); |
| 67 f.write(' "%s",\n' % sha1hash) |
| 68 f.write(' data,\n') |
| 69 f.write(' sizeof(data)\n') |
| 70 f.write('};\n'); |
| 71 f.write('\n') |
| 72 for n in reversed(namespaces): |
| 73 f.write('} // namespace %s\n' % n) |
| 74 |
| 75 if __name__ == '__main__': |
| 76 main() |
OLD | NEW |