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