OLD | NEW |
---|---|
1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
2 # Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file | 2 # Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file |
3 # for details. All rights reserved. Use of this source code is governed by a | 3 # for details. All rights reserved. Use of this source code is governed by a |
4 # BSD-style license that can be found in the LICENSE file. | 4 # BSD-style license that can be found in the LICENSE file. |
5 | 5 |
6 """Command line wrapper to run the frog compiler under the Dart VM""" | 6 """Command line wrapper to run the frog compiler under the Dart VM""" |
7 | 7 |
8 # TODO(jimhug): This is a temporary hack to enable running on the VM. We need | 8 # TODO(jimhug): This is a temporary hack to enable running on the VM. We need |
9 # the ability to get command-line arguments, to return exit codes, and to | 9 # the ability to get command-line arguments, to return exit codes, and to |
10 # communicate with spawned processes in the VM for this to go away. | 10 # communicate with spawned processes in the VM for this to go away. |
(...skipping 15 matching lines...) Expand all Loading... | |
26 import utils | 26 import utils |
27 | 27 |
28 HTML = '''<html> | 28 HTML = '''<html> |
29 <head><title>Frog</title><head> | 29 <head><title>Frog</title><head> |
30 <body> | 30 <body> |
31 <script type="application/javascript" src="out.js"></script> | 31 <script type="application/javascript" src="out.js"></script> |
32 </body> | 32 </body> |
33 </html> | 33 </html> |
34 ''' | 34 ''' |
35 | 35 |
36 | |
37 # Returns the path to the Dart test runner (executes the .dart file). | |
38 def GetDartRunner(mode, arch, component): | |
39 build_root = utils.GetBuildRoot(GuessOS(), mode, arch) | |
40 if component == 'dartc': | |
kasperl
2012/04/20 12:53:09
Does this still exist (dartc_test)?
ahe
2012/04/23 13:14:09
Done.
| |
41 return os.path.join(build_root, 'compiler', 'bin', 'dartc_test') | |
42 elif component == 'frog': | |
43 return os.path.join(build_root, 'frog', 'bin', 'frog') | |
44 else: | |
45 suffix = '' | |
46 if utils.IsWindows(): | |
47 suffix = '.exe' | |
48 return os.path.join(build_root, 'dart') + suffix | |
49 | |
50 | |
36 def GetDart(): | 51 def GetDart(): |
37 # Get the release version. | 52 # Get the release version. |
38 return utils.GetDartRunner('release', 'ia32', 'vm') | 53 return utils.GetDartRunner('release', 'ia32', 'vm') |
39 | 54 |
40 def GetD8(): | 55 def GetD8(): |
41 return join(dirname(GetDart()), 'd8') | 56 return join(dirname(GetDart()), 'd8') |
42 | 57 |
43 D8 = GetD8() | 58 D8 = GetD8() |
44 | 59 |
45 # The following environment variable is needed for isolate tests to work. | |
46 # TODO(sigmund): delete this and the 'env' parameter in 'execute' when we remove | |
47 # dependencies to nodejs | |
48 os.environ['NODE_MODULE_CONTEXTS'] = '1' | |
49 | |
50 def execute(cmd): | 60 def execute(cmd): |
51 """Execute a command in a subprocess. """ | 61 """Execute a command in a subprocess. """ |
52 try: | 62 try: |
53 proc = subprocess.Popen(cmd, stdout=sys.stdout, stderr=sys.stderr, | 63 proc = subprocess.Popen(cmd, stdout=sys.stdout, stderr=sys.stderr, |
54 env=os.environ) | 64 env=os.environ) |
55 return proc.wait() | 65 return proc.wait() |
56 except Exception as e: | 66 except Exception as e: |
57 print 'Exception when executing: ' + ' '.join(cmd) | 67 print 'Exception when executing: ' + ' '.join(cmd) |
58 print e | 68 print e |
59 return 1 | 69 return 1 |
(...skipping 18 matching lines...) Expand all Loading... | |
78 | 88 |
79 optionParser.add_option('--js_cmd', | 89 optionParser.add_option('--js_cmd', |
80 default = '%s --crankshaft' % D8, | 90 default = '%s --crankshaft' % D8, |
81 metavar='FILE', help='The shell cmd to use to run output JS code.') | 91 metavar='FILE', help='The shell cmd to use to run output JS code.') |
82 | 92 |
83 optionParser.add_option('--keep_files', | 93 optionParser.add_option('--keep_files', |
84 action='store_true', help='Do not remove temporary files.') | 94 action='store_true', help='Do not remove temporary files.') |
85 | 95 |
86 # TODO(vsm): Hook in HtmlConverter. | 96 # TODO(vsm): Hook in HtmlConverter. |
87 optionParser.add_option('--html', | 97 optionParser.add_option('--html', |
88 action='store_true', help='Invoke this in the browser instead of node/d8.' ) | 98 action='store_true', help='Invoke this in the browser instead of d8.') |
89 optionParser.add_option('--browser', | 99 optionParser.add_option('--browser', |
90 default = None, | 100 default = None, |
91 metavar='FILE', help='The browser to use to run output HTML.') | 101 metavar='FILE', help='The browser to use to run output HTML.') |
92 | 102 |
93 optionParser.add_option('--verbose', | 103 optionParser.add_option('--verbose', |
94 help='Verbose output', default=False, action='store_true') | 104 help='Verbose output', default=False, action='store_true') |
95 | 105 |
96 optionParser.set_usage("frog.py <dart-script-file> [<dart-options>]") | 106 optionParser.set_usage("frog.py <dart-script-file> [<dart-options>]") |
97 return optionParser.parse_args(args) | 107 return optionParser.parse_args(args) |
98 | 108 |
(...skipping 20 matching lines...) Expand all Loading... | |
119 if not exists(dart): | 129 if not exists(dart): |
120 print "Dart VM not built. Please run the following command:" | 130 print "Dart VM not built. Please run the following command:" |
121 build_file = relpath(join(HOME, os.pardir, 'tools', 'build.py')) | 131 build_file = relpath(join(HOME, os.pardir, 'tools', 'build.py')) |
122 print ' ' + build_file + ' -m release' | 132 print ' ' + build_file + ' -m release' |
123 return 1 | 133 return 1 |
124 | 134 |
125 return compileAndRun(options, dartArgs, dart) | 135 return compileAndRun(options, dartArgs, dart) |
126 | 136 |
127 | 137 |
128 def ensureJsEngine(options): | 138 def ensureJsEngine(options): |
129 proc = subprocess.Popen("node --help", | 139 if not exists(D8): |
130 stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True) | 140 print "No engine available for running JS code." |
131 if proc.wait() != 0: | 141 print "See frog/README.txt for instructions." |
132 if not exists(D8): | 142 return 1 |
133 print "No engine available for running JS code." | |
134 print "See frog/README.txt for instructions." | |
135 return 1 | |
136 elif 'node' in options.js_cmd: | |
137 options.js_cmd = D8 | |
138 return 0 | 143 return 0 |
139 | 144 |
140 def compileAndRun(options, args, dart): | 145 def compileAndRun(options, args, dart): |
141 nodeArgs = [] | 146 jsArgs = [] |
142 for i in range(len(args)): | 147 for i in range(len(args)): |
143 if args[i].endswith('.dart'): | 148 if args[i].endswith('.dart'): |
144 nodeArgs = args[i+1:] | 149 jsArgs = args[i+1:] |
145 args = args[:i+1] | 150 args = args[:i+1] |
146 break | 151 break |
147 | 152 |
148 outfile_given = False | 153 outfile_given = False |
149 execute_output = True | 154 execute_output = True |
150 for i in range(len(args)): | 155 for i in range(len(args)): |
151 if args[i].startswith('--out'): | 156 if args[i].startswith('--out'): |
152 outfile_given = True | 157 outfile_given = True |
153 outfile = args[i][6:] | 158 outfile = args[i][6:] |
154 execute_output = False | 159 execute_output = False |
155 break; | 160 break; |
156 if args[i] == '--compile-only': | 161 if args[i] == '--compile-only': |
157 execute_output = False | 162 execute_output = False |
158 break; | 163 break; |
159 | 164 |
160 if options.verbose: print "nodeArgs %s" % ' '.join(nodeArgs); | 165 if options.verbose: print "jsArgs %s" % ' '.join(jsArgs); |
161 | 166 |
162 workdir = options.workdir | 167 workdir = options.workdir |
163 cleanup = False | 168 cleanup = False |
164 if not workdir: | 169 if not workdir: |
165 if not options.html: | 170 if not options.html: |
166 workdir = tempfile.mkdtemp() | 171 workdir = tempfile.mkdtemp() |
167 if not options.keep_files: | 172 if not options.keep_files: |
168 cleanup = True | 173 cleanup = True |
169 else: | 174 else: |
170 # A persistent location for the browser to load. | 175 # A persistent location for the browser to load. |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
203 # TODO(ahe): Using 253 to signal a crash to test.dart. | 208 # TODO(ahe): Using 253 to signal a crash to test.dart. |
204 return 253 | 209 return 253 |
205 return exit_code | 210 return exit_code |
206 | 211 |
207 result = 0 | 212 result = 0 |
208 if execute_output: | 213 if execute_output: |
209 if not options.html: | 214 if not options.html: |
210 if ensureJsEngine(options) != 0: | 215 if ensureJsEngine(options) != 0: |
211 return 1 | 216 return 1 |
212 js_cmd = options.js_cmd | 217 js_cmd = options.js_cmd |
213 result = execute(js_cmd.split(' ') + [outfile] + nodeArgs) | 218 result = execute(js_cmd.split(' ') + [outfile] + jsArgs) |
214 else: | 219 else: |
215 f = open(outhtml, 'w') | 220 f = open(outhtml, 'w') |
216 f.write(HTML) | 221 f.write(HTML) |
217 f.close() | 222 f.close() |
218 result = execute([browser, outhtml]) | 223 result = execute([browser, outhtml]) |
219 elif outfile_given: | 224 elif outfile_given: |
220 print 'Compilation succeded. Code generated in: %s' % outfile | 225 print 'Compilation succeded. Code generated in: %s' % outfile |
221 else: | 226 else: |
222 print 'Compilation succeded.' | 227 print 'Compilation succeded.' |
223 | 228 |
224 if cleanup: shutil.rmtree(workdir) | 229 if cleanup: shutil.rmtree(workdir) |
225 if result != 0: | 230 if result != 0: |
226 return 1 | 231 return 1 |
227 return 0 | 232 return 0 |
228 | 233 |
229 if __name__ == '__main__': | 234 if __name__ == '__main__': |
230 sys.exit(main(sys.argv)) | 235 sys.exit(main(sys.argv)) |
OLD | NEW |