|
1 # This Source Code Form is subject to the terms of the Mozilla Public |
|
2 # License, v. 2.0. If a copy of the MPL was not distributed with this |
|
3 # file, You can obtain one at http://mozilla.org/MPL/2.0/. |
|
4 # |
|
5 # ToCAsciiArray and ToCArray are from V8's js2c.py. |
|
6 # |
|
7 # Copyright 2012 the V8 project authors. All rights reserved. |
|
8 # Redistribution and use in source and binary forms, with or without |
|
9 # modification, are permitted provided that the following conditions are |
|
10 # met: |
|
11 # |
|
12 # * Redistributions of source code must retain the above copyright |
|
13 # notice, this list of conditions and the following disclaimer. |
|
14 # * Redistributions in binary form must reproduce the above |
|
15 # copyright notice, this list of conditions and the following |
|
16 # disclaimer in the documentation and/or other materials provided |
|
17 # with the distribution. |
|
18 # * Neither the name of Google Inc. nor the names of its |
|
19 # contributors may be used to endorse or promote products derived |
|
20 # from this software without specific prior written permission. |
|
21 # |
|
22 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
|
23 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
|
24 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
|
25 # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
|
26 # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
|
27 # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
|
28 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
|
29 # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
|
30 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
|
31 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
|
32 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
|
33 |
|
34 # This utility converts JS files containing self-hosted builtins into a C |
|
35 # header file that can be embedded into SpiderMonkey. |
|
36 # |
|
37 # It uses the C preprocessor to process its inputs. |
|
38 |
|
39 from __future__ import with_statement |
|
40 import re, sys, os, fileinput, subprocess |
|
41 import shlex |
|
42 from optparse import OptionParser |
|
43 |
|
44 def ToCAsciiArray(lines): |
|
45 result = [] |
|
46 for chr in lines: |
|
47 value = ord(chr) |
|
48 assert value < 128 |
|
49 result.append(str(value)) |
|
50 return ", ".join(result) |
|
51 |
|
52 def ToCArray(lines): |
|
53 result = [] |
|
54 for chr in lines: |
|
55 result.append(str(ord(chr))) |
|
56 return ", ".join(result) |
|
57 |
|
58 HEADER_TEMPLATE = """\ |
|
59 /* This Source Code Form is subject to the terms of the Mozilla Public |
|
60 * License, v. 2.0. If a copy of the MPL was not distributed with this |
|
61 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
|
62 |
|
63 namespace js { |
|
64 namespace selfhosted { |
|
65 static const %(sources_type)s data[] = { %(sources_data)s }; |
|
66 |
|
67 static const %(sources_type)s * const %(sources_name)s = reinterpret_cast<const %(sources_type)s *>(data); |
|
68 |
|
69 uint32_t GetCompressedSize() { |
|
70 return %(compressed_total_length)i; |
|
71 } |
|
72 |
|
73 uint32_t GetRawScriptsSize() { |
|
74 return %(raw_total_length)i; |
|
75 } |
|
76 } // selfhosted |
|
77 } // js |
|
78 """ |
|
79 |
|
80 def embed(cpp, msgs, sources, c_out, js_out, env): |
|
81 # Clang seems to complain and not output anything if the extension of the |
|
82 # input is not something it recognizes, so just fake a .h here. |
|
83 tmp = 'selfhosted.js.h' |
|
84 with open(tmp, 'wb') as output: |
|
85 output.write('\n'.join([msgs] + ['#include "%(s)s"' % { 's': source } for source in sources])) |
|
86 cmdline = cpp + ['-D%(k)s=%(v)s' % { 'k': k, 'v': env[k] } for k in env] + [tmp] |
|
87 p = subprocess.Popen(cmdline, stdout=subprocess.PIPE) |
|
88 processed = '' |
|
89 for line in p.stdout: |
|
90 if not line.startswith('#'): |
|
91 processed += line |
|
92 os.remove(tmp) |
|
93 with open(js_out, 'w') as output: |
|
94 output.write(processed) |
|
95 with open(c_out, 'w') as output: |
|
96 if 'USE_ZLIB' in env: |
|
97 import zlib |
|
98 compressed = zlib.compress(processed) |
|
99 data = ToCArray(compressed) |
|
100 output.write(HEADER_TEMPLATE % { |
|
101 'sources_type': 'unsigned char', |
|
102 'sources_data': data, |
|
103 'sources_name': 'compressedSources', |
|
104 'compressed_total_length': len(compressed), |
|
105 'raw_total_length': len(processed) |
|
106 }) |
|
107 else: |
|
108 data = ToCAsciiArray(processed) |
|
109 output.write(HEADER_TEMPLATE % { |
|
110 'sources_type': 'char', |
|
111 'sources_data': data, |
|
112 'sources_name': 'rawSources', |
|
113 'compressed_total_length': 0, |
|
114 'raw_total_length': len(processed) |
|
115 }) |
|
116 |
|
117 def process_msgs(cpp, msgs): |
|
118 # Clang seems to complain and not output anything if the extension of the |
|
119 # input is not something it recognizes, so just fake a .h here. |
|
120 tmp = 'selfhosted.msg.h' |
|
121 with open(tmp, 'wb') as output: |
|
122 output.write("""\ |
|
123 #define hash # |
|
124 #define id(x) x |
|
125 #define hashify(x) id(hash)x |
|
126 #define MSG_DEF(name, id, argc, ex, msg) hashify(define) name id |
|
127 #include "%(msgs)s" |
|
128 """ % { 'msgs': msgs }) |
|
129 p = subprocess.Popen(cpp + [tmp], stdout=subprocess.PIPE) |
|
130 processed = p.communicate()[0] |
|
131 os.remove(tmp) |
|
132 return processed |
|
133 |
|
134 def main(): |
|
135 env = {} |
|
136 def define_env(option, opt, value, parser): |
|
137 pair = value.split('=', 1) |
|
138 if len(pair) == 1: |
|
139 pair.append(1) |
|
140 env[pair[0]] = pair[1] |
|
141 p = OptionParser(usage="%prog [options] file") |
|
142 p.add_option('-D', action='callback', callback=define_env, type="string", |
|
143 metavar='var=[val]', help='Define a variable') |
|
144 p.add_option('-m', type='string', metavar='jsmsg', default='../js.msg', |
|
145 help='js.msg file') |
|
146 p.add_option('-p', type='string', metavar='cpp', help='Path to C preprocessor') |
|
147 p.add_option('-o', type='string', metavar='filename', default='selfhosted.out.h', |
|
148 help='C array header file') |
|
149 p.add_option('-s', type='string', metavar='jsfilename', default='selfhosted.js', |
|
150 help='Combined postprocessed JS file') |
|
151 (options, sources) = p.parse_args() |
|
152 if not (options.p and sources): |
|
153 p.print_help() |
|
154 exit(1) |
|
155 cpp = shlex.split(options.p) |
|
156 embed(cpp, process_msgs(cpp, options.m), |
|
157 sources, options.o, options.s, env) |
|
158 |
|
159 if __name__ == "__main__": |
|
160 main() |