OLD | NEW |
(Empty) | |
| 1 #!/usr/bin/python |
| 2 # |
| 3 # Copyright (C) 2011 Google Inc. All rights reserved. |
| 4 # |
| 5 # Redistribution and use in source and binary forms, with or without |
| 6 # modification, are permitted provided that the following conditions are |
| 7 # met: |
| 8 # |
| 9 # * Redistributions of source code must retain the above copyright |
| 10 # notice, this list of conditions and the following disclaimer. |
| 11 # * Redistributions in binary form must reproduce the above |
| 12 # copyright notice, this list of conditions and the following disclaimer |
| 13 # in the documentation and/or other materials provided with the |
| 14 # distribution. |
| 15 # * Neither the name of Google Inc. nor the names of its |
| 16 # contributors may be used to endorse or promote products derived from |
| 17 # this software without specific prior written permission. |
| 18 # |
| 19 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 20 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 21 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 22 # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 23 # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 24 # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 25 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 26 # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 27 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 28 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 29 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 30 # |
| 31 # Copyright (c) 2011 The Chromium Authors. All rights reserved. |
| 32 # Use of this source code is governed by a BSD-style license that can be |
| 33 # found in the LICENSE file. |
| 34 |
| 35 # build_dart_snapshot.py generates two C++ files: DartSnapshot.cpp |
| 36 # with a constant which is a snapshot of major DOM libs an |
| 37 # DartResolver.cpp which is a resolver for dart:html library. |
| 38 |
| 39 import os.path |
| 40 import subprocess |
| 41 import sys |
| 42 |
| 43 |
| 44 def main(args): |
| 45 assert(len(args) >= 4) |
| 46 dartPath = args[1] |
| 47 dartSnapshotTemplateFile = args[2] |
| 48 outputFilePath = args[3] |
| 49 genSnapshotBinPath = args[4] |
| 50 snapshottedLibPaths = args[5:] |
| 51 |
| 52 def path(*components): |
| 53 return os.path.abspath(os.path.join(*components)) |
| 54 |
| 55 def dartName(path): |
| 56 # Translates <dirs>/foo_dartium.dart into foo. |
| 57 return (os.path.splitext(os.path.split(path)[1])[0] |
| 58 .replace('_dartium', '')) |
| 59 |
| 60 snapshottedLibs = [(dartName(p), path(p)) |
| 61 for p in snapshottedLibPaths] |
| 62 |
| 63 # Generate a Dart script to build the snapshot from. |
| 64 snapshotScriptName = os.path.join(outputFilePath, 'snapshotScript.dart') |
| 65 with file(snapshotScriptName, 'w') as snapshotScript: |
| 66 snapshotScript.write('library snapshot;\n') |
| 67 for name, _ in snapshottedLibs: |
| 68 # Skip internal libraries - they should be indirectly imported via t
he public ones. |
| 69 if not name.startswith('_'): |
| 70 snapshotScript.write('import \'dart:%(name)s\' as %(name)s;\n' %
{'name': name}) |
| 71 |
| 72 binarySnapshotFile = path(outputFilePath, 'DartSnapshot.bin') |
| 73 |
| 74 # Build a command to generate the snapshot bin file. |
| 75 command = [ |
| 76 'python', |
| 77 path(dartPath, 'runtime', 'tools', 'create_snapshot_bin.py'), |
| 78 '--executable=%s' % path(genSnapshotBinPath), |
| 79 '--output_bin=%s' % binarySnapshotFile, |
| 80 '--script=%s' % snapshotScriptName, |
| 81 ] |
| 82 command.extend(['--url_mapping=dart:%s,%s' % lib for lib in snapshottedLibs]
) |
| 83 |
| 84 pipe = subprocess.Popen(command, |
| 85 stdout=subprocess.PIPE, |
| 86 stderr=subprocess.PIPE) |
| 87 out, error = pipe.communicate() |
| 88 if (pipe.returncode != 0): |
| 89 raise Exception('Snapshot bin generation failed: %s/%s' % (out, error)) |
| 90 |
| 91 # Build a command to generate the snapshot file. |
| 92 command = [ |
| 93 'python', |
| 94 path(dartPath, 'runtime', 'tools', 'create_snapshot_file.py'), |
| 95 '--input_cc=%s' % dartSnapshotTemplateFile, |
| 96 '--input_bin=%s' % binarySnapshotFile, |
| 97 '--output=%s' % path(outputFilePath, 'DartSnapshot.bytes'), |
| 98 ] |
| 99 |
| 100 pipe = subprocess.Popen(command, |
| 101 stdout=subprocess.PIPE, |
| 102 stderr=subprocess.PIPE) |
| 103 out, error = pipe.communicate() |
| 104 if (pipe.returncode != 0): |
| 105 raise Exception('Snapshot file generation failed: %s/%s' % (out, error)) |
| 106 |
| 107 snapshotSizeInBytes = os.path.getsize(binarySnapshotFile) |
| 108 productDir = os.path.dirname(genSnapshotBinPath) |
| 109 snapshotSizeOutputPath = os.path.join(productDir, 'snapshot-size.txt') |
| 110 with file(snapshotSizeOutputPath, 'w') as snapshotSizeFile: |
| 111 snapshotSizeFile.write('%d\n' % snapshotSizeInBytes) |
| 112 |
| 113 return 0 |
| 114 |
| 115 if __name__ == '__main__': |
| 116 sys.exit(main(sys.argv)) |
OLD | NEW |