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

Unified Diff: runtime/tools/create_snapshot_bin.py

Issue 10823209: Add support for building the Dart VM for Android OS. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Address cshapiro's review comments, pass 2 Created 8 years, 4 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 side-by-side diff with in-line comments
Download patch
Index: runtime/tools/create_snapshot_bin.py
diff --git a/runtime/tools/create_snapshot_bin.py b/runtime/tools/create_snapshot_bin.py
new file mode 100755
index 0000000000000000000000000000000000000000..37413eff282b56cc96b5e9016922e2522e229986
--- /dev/null
+++ b/runtime/tools/create_snapshot_bin.py
@@ -0,0 +1,192 @@
+#!/usr/bin/env python
+#
+# Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file
+# for details. All rights reserved. Use of this source code is governed by a
+# BSD-style license that can be found in the LICENSE file.
+
+# Script to create snapshot bin file.
+
+import getopt
+import optparse
+import os
+from os.path import abspath, basename, dirname, join
+import string
+import subprocess
+import sys
+import tempfile
+import utils
+
+
+HOST_OS = utils.GuessOS()
+HOST_CPUS = utils.GuessCpus()
+
+
+def BuildOptions():
+ result = optparse.OptionParser()
+ result.add_option("--executable",
+ action="store", type="string",
+ help="path to snapshot generator executable")
+ result.add_option("--output_bin",
+ action="store", type="string",
+ help="output file name into which snapshot in binary form is generated")
+ result.add_option("--script",
+ action="store", type="string",
+ help="Dart script for which snapshot is to be generated")
+ result.add_option("--url_mapping",
+ default=[],
+ action="append",
+ help="mapping from url to file name, used when generating snapshots")
+ result.add_option("-v", "--verbose",
+ help='Verbose output.',
+ default=False, action="store_true")
+ result.add_option("--target_os",
+ action="store", type="string",
+ help="Which os to run the executable on")
+ return result
+
+
Emily Fortuna 2012/08/08 01:28:37 extra newline here and a few other places?
Emily Fortuna 2012/08/08 01:33:47 edit: I see you're following the format from creat
jackpal 2012/08/08 23:06:45 Done.
+def ProcessOptions(options):
+ if not options.executable:
+ sys.stderr.write('--executable not specified\n')
+ return False
+ if not options.output_bin:
+ sys.stderr.write('--output_bin not specified\n')
+ return False
+ return True
+
+
+def makeString(input_file):
+ result = ' '
+ fileHandle = open(input_file, 'rb')
+ lineCounter = 0
+ for byte in fileHandle.read():
+ result += ' %d,' % ord(byte)
+ lineCounter += 1
+ if lineCounter == 10:
+ result += '\n '
+ lineCounter = 0
+ if lineCounter != 0:
+ result += '\n '
+ return result
+
+def runHost(command):
+ print "command %s" % command
+ pipe = subprocess.Popen(args=command,
+ shell=True,
+ stdout=subprocess.PIPE,
+ stderr=subprocess.PIPE)
+ out, error = pipe.communicate()
+ if (pipe.returncode != 0):
+ print out, error
+ print "command failed"
+ print "(Command was: '", ' '.join(command), "')"
+ raise Exception("Failed")
+
+def runTarget(command):
+ runHost("adb shell %s" % command)
+
+def runOnAndroid(options):
+ outputBin = options.output_bin
+
+ android_workspace = os.getenv("ANDROID_DART", "/data/local/dart")
+ android_outputBin = join(android_workspace, basename(outputBin))
+
+ executable = options.executable
+ android_executable = join(android_workspace, basename(executable))
+
+ filesToPush = [] # (src, dest)
+ filesToPull = [] # (src, dest)
+
+ # Setup arguments to the snapshot generator binary.
+
Emily Fortuna 2012/08/08 01:33:47 okay this seems like random newlines...?
jackpal 2012/08/08 23:06:45 Done.
+
+ script_args = [android_executable]
+
+ # First setup the snapshot output filename.
+ filesToPull.append((android_outputBin, outputBin))
+ script_args.append(''.join([ "--snapshot=", android_outputBin]))
+
+ # Next setup all url mapping options specified.
+ for url_arg in options.url_mapping:
+ android_url_arg = join(android_workspace, basename(url_arg))
+ filesToPush.push((url_arg, android_url_arg))
+ url_mapping_argument = ''.join(["--url_mapping=", url_arg ])
+ script_args.append(url_mapping_argument)
+
+ # Finally append the script name if one is specified.
+ if options.script:
+ android_script_name = join(android_workspace, basename(options.script))
+ filesToPush.append((options.script, android_script_name))
+ script_args.append(android_script_name)
+
+ filesToPush.append((executable, android_executable))
+
+ command = ' '.join(script_args)
+
+ runHost("adb shell mkdir %s" % android_workspace)
+ try:
+ for src, dest in filesToPush:
+ runHost("adb push '%s' '%s'" % (src, dest))
+ runTarget(command)
+ for src, dest in filesToPull:
+ runHost("adb pull '%s' '%s'" % (src, dest))
+ finally:
+ for src, dest in filesToPush:
+ runHost("adb shell rm '%s'" % dest)
+ for src, dest in filesToPull:
+ runHost("adb shell rm '%s'" % src)
+
+def Main():
+ # Parse options.
+ parser = BuildOptions()
+ (options, args) = parser.parse_args()
+ if not ProcessOptions(options):
+ parser.print_help()
+ return 1
+
+ # If there are additional arguments, report error and exit.
+ if args:
+ parser.print_help()
+ return 1
+
+ # Setup arguments to the snapshot generator binary.
+ script_args = []
+
+ # First setup the snapshot output filename.
+ script_args.append(''.join([ "--snapshot=", options.output_bin ]))
+
+ # Next setup all url mapping options specified.
+ for url_arg in options.url_mapping:
+ url_mapping_argument = ''.join(["--url_mapping=", url_arg ])
+ script_args.append(url_mapping_argument)
+
+ # Finally append the script name if one is specified.
+ if options.script:
+ script_args.append(options.script)
+
+ # Construct command line to execute the snapshot generator binary and invoke.
+ command = [ options.executable ] + script_args
+ if options.verbose:
+ print ' '.join(command)
+
+ if options.target_os == 'android':
+ try:
+ runOnAndroid(options)
+ except Exception as e:
+ print "Could not run on Android: %s" % e
+ return -1
+ else:
+ pipe = subprocess.Popen(command,
+ stdout=subprocess.PIPE,
+ stderr=subprocess.PIPE)
+ out, error = pipe.communicate()
+ if (pipe.returncode != 0):
+ print out, error
+ print "Snapshot generation failed"
+ print "(Command was: '", ' '.join(command), "')"
+ return -1
+
+ return 0
+
+if __name__ == '__main__':
+ sys.exit(Main())

Powered by Google App Engine
This is Rietveld 408576698