Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(226)

Side by Side Diff: runtime/tools/create_resources.py

Issue 1151743002: Decrease Dart binary size by 1.7MB (Closed) Base URL: git@github.com:dart-lang/sdk.git@master
Patch Set: Created 5 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « runtime/bin/vmservice_impl.cc ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 # Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file 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 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. 3 # BSD-style license that can be found in the LICENSE file.
4 # 4 #
5 # This python script creates string literals in a C++ source file from a C++ 5 # This python script creates string literals in a C++ source file from a C++
6 # source template and one or more resource files. 6 # source template and one or more resource files.
7 7
8 import os 8 import os
9 import sys 9 import sys
10 from os.path import join 10 from os.path import join, splitext
11 import time 11 import time
12 from optparse import OptionParser 12 from optparse import OptionParser
13 import re 13 import re
14 from datetime import date 14 from datetime import date
15 import zlib
15 16
16 def makeResources(root_dir, client_dir, input_files, table_name): 17 def makeResources(root_dir, client_dir, input_files, table_name, compress, no_co mpress_extensions):
17 result = '' 18 result = ''
18 resources = [] 19 resources = []
19 20
20 # Write each file's contents as a byte string constant. 21 # Write each file's contents as a byte string constant.
21 for resource_file in input_files: 22 for resource_file in input_files:
22 if root_dir and resource_file.startswith(root_dir): 23 if root_dir and resource_file.startswith(root_dir):
23 resource_file_name = resource_file[ len(root_dir) : ] 24 resource_file_name = resource_file[ len(root_dir) : ]
24 elif client_dir and resource_file.startswith(client_dir): 25 elif client_dir and resource_file.startswith(client_dir):
25 resource_file_name = resource_file[ len(client_dir) : ] 26 resource_file_name = resource_file[ len(client_dir) : ]
26 else: 27 else:
27 resource_file_name = resource_file 28 resource_file_name = resource_file
29 _, ext = os.path.splitext(resource_file)
30 if ext in no_compress_extensions:
31 # Force no compression for files of this extension.
32 compress = None
28 resource_url = '/%s' % resource_file_name 33 resource_url = '/%s' % resource_file_name
29 result += '// %s\n' % resource_file 34 result += '// %s\n' % resource_file
30 result += 'const char ' 35 result += 'const char '
31 resource_name = re.sub(r'(/|\.|-|\\)', '_', resource_file_name) + '_' 36 resource_name = re.sub(r'(/|\.|-|\\)', '_', resource_file_name) + '_'
32 result += resource_name 37 result += resource_name
33 result += '[] = {\n ' 38 result += '[] = {\n '
34 fileHandle = open(resource_file, 'rb') 39 fileHandle = open(resource_file, 'rb')
35 lineCounter = 0 40 lineCounter = 0
36 for byte in fileHandle.read(): 41 file_contents = fileHandle.read()
42 if compress:
43 file_contents = zlib.compress(file_contents)
44 for byte in file_contents:
37 result += r" '\x%02x'," % ord(byte) 45 result += r" '\x%02x'," % ord(byte)
38 lineCounter += 1 46 lineCounter += 1
39 if lineCounter == 10: 47 if lineCounter == 10:
40 result += '\n ' 48 result += '\n '
41 lineCounter = 0 49 lineCounter = 0
42 if lineCounter != 0: 50 if lineCounter != 0:
43 result += '\n ' 51 result += '\n '
44 result += ' 0\n};\n\n' 52 result += ' 0\n};\n\n'
45 resource_url_scrubbed = re.sub(r'\\', '/', resource_url) 53 resource_url_scrubbed = re.sub(r'\\', '/', resource_url)
46 resources.append( 54 resources.append(
47 (resource_url_scrubbed, resource_name, os.stat(resource_file).st_size)); 55 (resource_url_scrubbed, resource_name, len(file_contents)));
48 56
49 # Write the resource table. 57 # Write the resource table.
50 result += 'ResourcesEntry __%s_resources_[] = ' % table_name 58 result += 'ResourcesEntry __%s_resources_[] = ' % table_name
51 result += '{\n' 59 result += '{\n'
52 for res in resources: 60 for res in resources:
53 result += ' { "%s", %s, %d },\n' % res 61 result += ' { "%s", %s, %d },\n' % res
54 result += ' { 0, 0, 0 },\n' 62 result += ' { 0, 0, 0 },\n'
55 result += '};\n\n' 63 result += '};\n\n'
56 return result 64 return result
57 65
58 66
59 def makeFile(output_file, root_dir, client_dir, input_files, outer_namespace, 67 def makeFile(output_file, root_dir, client_dir, input_files, outer_namespace,
60 inner_namespace, table_name): 68 inner_namespace, table_name, compress, no_compress_extensions):
61 cc_text = ''' 69 cc_text = '''
62 // Copyright (c) %d, the Dart project authors. Please see the AUTHORS file 70 // Copyright (c) %d, the Dart project authors. Please see the AUTHORS file
63 // for details. All rights reserved. Use of this source code is governed by a 71 // for details. All rights reserved. Use of this source code is governed by a
64 // BSD-style license that can be found in the LICENSE file. 72 // BSD-style license that can be found in the LICENSE file.
65 73
66 ''' % date.today().year 74 ''' % date.today().year
67 cc_text += 'namespace %s {\n' % outer_namespace 75 cc_text += 'namespace %s {\n' % outer_namespace
68 if inner_namespace != None: 76 if inner_namespace != None:
69 cc_text += 'namespace %s {\n' % inner_namespace 77 cc_text += 'namespace %s {\n' % inner_namespace
70 cc_text += ''' 78 cc_text += '''
71 struct ResourcesEntry { 79 struct ResourcesEntry {
72 const char* path_; 80 const char* path_;
73 const char* resource_; 81 const char* resource_;
74 int length_; 82 int length_;
75 }; 83 };
76 84
77 ''' 85 '''
78 cc_text += makeResources(root_dir, client_dir, input_files, table_name) 86 cc_text += makeResources(root_dir, client_dir, input_files, table_name,
87 compress, no_compress_extensions)
79 cc_text += '\n' 88 cc_text += '\n'
80 if inner_namespace != None: 89 if inner_namespace != None:
81 cc_text += '} // namespace %s\n' % inner_namespace 90 cc_text += '} // namespace %s\n' % inner_namespace
82 cc_text += '} // namespace %s\n' % outer_namespace 91 cc_text += '} // namespace %s\n' % outer_namespace
83 open(output_file, 'w').write(cc_text) 92 open(output_file, 'w').write(cc_text)
84 return True 93 return True
85 94
86 95
87 def main(args): 96 def main(args):
88 try: 97 try:
(...skipping 11 matching lines...) Expand all
100 default="dart") 109 default="dart")
101 parser.add_option("--inner_namespace", 110 parser.add_option("--inner_namespace",
102 action="store", type="string", 111 action="store", type="string",
103 help="inner C++ namespace") 112 help="inner C++ namespace")
104 parser.add_option("--table_name", 113 parser.add_option("--table_name",
105 action="store", type="string", 114 action="store", type="string",
106 help="name of table") 115 help="name of table")
107 parser.add_option("--client_root", 116 parser.add_option("--client_root",
108 action="store", type="string", 117 action="store", type="string",
109 help="root directory client resources") 118 help="root directory client resources")
119 parser.add_option("--compress",
120 action="store_true",
121 help="zlib compress resources")
122 parser.add_option("--no_compress_extensions",
123 action="append",
124 default=['.dart'],
125 help="file extensions that should not be compressed.")
126
110 (options, args) = parser.parse_args() 127 (options, args) = parser.parse_args()
111 if not options.output: 128 if not options.output:
112 sys.stderr.write('--output not specified\n') 129 sys.stderr.write('--output not specified\n')
113 return -1 130 return -1
114 if not options.table_name: 131 if not options.table_name:
115 sys.stderr.write('--table_name not specified\n') 132 sys.stderr.write('--table_name not specified\n')
116 return -1 133 return -1
117 if len(args) == 0: 134 if len(args) == 0:
118 sys.stderr.write('No input files specified\n') 135 sys.stderr.write('No input files specified\n')
119 return -1 136 return -1
(...skipping 12 matching lines...) Expand all
132 # Skip devtools version 149 # Skip devtools version
133 if (src_path.find("index_devtools") != -1): 150 if (src_path.find("index_devtools") != -1):
134 continue 151 continue
135 files.append(src_path) 152 files.append(src_path)
136 153
137 for arg in args: 154 for arg in args:
138 files.append(arg) 155 files.append(arg)
139 156
140 if not makeFile(options.output, options.root_prefix, options.client_root, 157 if not makeFile(options.output, options.root_prefix, options.client_root,
141 files, options.outer_namespace, options.inner_namespace, 158 files, options.outer_namespace, options.inner_namespace,
142 options.table_name): 159 options.table_name, options.compress,
160 options.no_compress_extensions):
143 return -1 161 return -1
144 162
145 return 0 163 return 0
146 except Exception, inst: 164 except Exception, inst:
147 sys.stderr.write('create_resources.py exception\n') 165 sys.stderr.write('create_resources.py exception\n')
148 sys.stderr.write(str(inst)) 166 sys.stderr.write(str(inst))
149 sys.stderr.write('\n') 167 sys.stderr.write('\n')
150 return -1 168 return -1
151 169
152 if __name__ == '__main__': 170 if __name__ == '__main__':
153 sys.exit(main(sys.argv)) 171 sys.exit(main(sys.argv))
OLDNEW
« no previous file with comments | « runtime/bin/vmservice_impl.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698