OLD | NEW |
(Empty) | |
| 1 # Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file |
| 2 # for details. All rights reserved. Use of this source code is governed by a |
| 3 # BSD-style license that can be found in the LICENSE file. |
| 4 # |
| 5 # This python script creates string literals in a C++ source file from a C++ |
| 6 # source template and one or more resource files. |
| 7 |
| 8 import os |
| 9 import sys |
| 10 from os.path import join |
| 11 import time |
| 12 from optparse import OptionParser |
| 13 import re |
| 14 from datetime import date |
| 15 |
| 16 |
| 17 def makeResources(root_dir, input_files): |
| 18 result = '' |
| 19 resources = [] |
| 20 |
| 21 # Write each file's contents as a byte string constant. |
| 22 for resource_file in input_files: |
| 23 if root_dir and resource_file.startswith(root_dir): |
| 24 resource_file_name = resource_file[len(root_dir):] |
| 25 else: |
| 26 resource_file_name = resource_file |
| 27 resource_url = '/%s' % resource_file_name |
| 28 result += '// %s\n' % resource_file |
| 29 result += 'const char ' |
| 30 resource_name = re.sub(r'(/|\.|-)', '_', resource_file_name) + '_' |
| 31 result += resource_name |
| 32 result += '[] = {\n ' |
| 33 fileHandle = open(resource_file, 'rb') |
| 34 lineCounter = 0 |
| 35 for byte in fileHandle.read(): |
| 36 result += r" '\x%02x'," % ord(byte) |
| 37 lineCounter += 1 |
| 38 if lineCounter == 10: |
| 39 result += '\n ' |
| 40 lineCounter = 0 |
| 41 if lineCounter != 0: |
| 42 result += '\n ' |
| 43 result += ' 0\n};\n\n' |
| 44 resources.append( |
| 45 (resource_url, resource_name, os.stat(resource_file).st_size)) |
| 46 |
| 47 # Write the resource table. |
| 48 result += 'Resources::resource_map_entry Resources::builtin_resources_[] = ' |
| 49 result += '{\n' |
| 50 for res in resources: |
| 51 result += ' { "%s", %s, %d },\n' % res |
| 52 result += '};\n\n' |
| 53 result += 'const intptr_t Resources::builtin_resources_count_ ' |
| 54 result += '= %d;\n' % len(resources) |
| 55 return result |
| 56 |
| 57 |
| 58 def makeFile(output_file, root_dir, input_files): |
| 59 cc_text = ''' |
| 60 // Copyright (c) %d, the Dart project authors. Please see the AUTHORS file |
| 61 // for details. All rights reserved. Use of this source code is governed by a |
| 62 // BSD-style license that can be found in the LICENSE file. |
| 63 |
| 64 ''' % date.today().year |
| 65 cc_text += '#if defined(new)\n' |
| 66 cc_text += '#undef new\n' |
| 67 cc_text += '#endif\n\n' |
| 68 cc_text += '#if defined(delete)\n' |
| 69 cc_text += '#undef delete\n' |
| 70 cc_text += '#endif\n\n' |
| 71 cc_text += '#ifndef NDEBUG\n' |
| 72 cc_text += '#define DEBUG\n' |
| 73 cc_text += '#endif\n' |
| 74 cc_text += '#include "bin/resources.h"\n\n' |
| 75 cc_text += 'namespace dart {\n' |
| 76 cc_text += 'namespace bin {\n' |
| 77 cc_text += makeResources(root_dir, input_files) |
| 78 cc_text += '} // namespace bin\n} // namespace dart\n' |
| 79 open(output_file, 'w').write(cc_text) |
| 80 return True |
| 81 |
| 82 |
| 83 def main(args): |
| 84 try: |
| 85 # Parse input. |
| 86 parser = OptionParser() |
| 87 parser.add_option("--output", |
| 88 action="store", type="string", |
| 89 help="output file name") |
| 90 parser.add_option("--root_prefix", |
| 91 action="store", type="string", |
| 92 help="root directory for resources") |
| 93 (options, args) = parser.parse_args() |
| 94 if not options.output: |
| 95 sys.stderr.write('--output not specified\n') |
| 96 return -1 |
| 97 if len(args) == 0: |
| 98 sys.stderr.write('No input files specified\n') |
| 99 return -1 |
| 100 |
| 101 files = [] |
| 102 for arg in args: |
| 103 files.append(arg) |
| 104 |
| 105 if not makeFile(options.output, options.root_prefix, files): |
| 106 return -1 |
| 107 |
| 108 return 0 |
| 109 except Exception, inst: |
| 110 sys.stderr.write('create_resources.py exception\n') |
| 111 sys.stderr.write(str(inst)) |
| 112 sys.stderr.write('\n') |
| 113 return -1 |
| 114 |
| 115 if __name__ == '__main__': |
| 116 sys.exit(main(sys.argv)) |
OLD | NEW |