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(utils.GuessOS(), mode, arch) |
| 40 if component == 'frog': |
| 41 return os.path.join(build_root, 'frog', 'bin', 'frog') |
| 42 else: |
| 43 suffix = '' |
| 44 if utils.IsWindows(): |
| 45 suffix = '.exe' |
| 46 return os.path.join(build_root, 'dart') + suffix |
| 47 |
| 48 |
36 def GetDart(): | 49 def GetDart(): |
37 # Get the release version. | 50 # Get the release version. |
38 return utils.GetDartRunner('release', 'ia32', 'vm') | 51 return GetDartRunner('release', 'ia32', 'vm') |
39 | 52 |
40 def GetD8(): | 53 def GetD8(): |
41 return join(dirname(GetDart()), 'd8') | 54 return join(dirname(GetDart()), 'd8') |
42 | 55 |
43 D8 = GetD8() | 56 D8 = GetD8() |
44 | 57 |
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): | 58 def execute(cmd): |
51 """Execute a command in a subprocess. """ | 59 """Execute a command in a subprocess. """ |
52 try: | 60 try: |
53 proc = subprocess.Popen(cmd, stdout=sys.stdout, stderr=sys.stderr, | 61 proc = subprocess.Popen(cmd, stdout=sys.stdout, stderr=sys.stderr, |
54 env=os.environ) | 62 env=os.environ) |
55 return proc.wait() | 63 return proc.wait() |
56 except Exception as e: | 64 except Exception as e: |
57 print 'Exception when executing: ' + ' '.join(cmd) | 65 print 'Exception when executing: ' + ' '.join(cmd) |
58 print e | 66 print e |
59 return 1 | 67 return 1 |
(...skipping 18 matching lines...) Expand all Loading... |
78 | 86 |
79 optionParser.add_option('--js_cmd', | 87 optionParser.add_option('--js_cmd', |
80 default = '%s --crankshaft' % D8, | 88 default = '%s --crankshaft' % D8, |
81 metavar='FILE', help='The shell cmd to use to run output JS code.') | 89 metavar='FILE', help='The shell cmd to use to run output JS code.') |
82 | 90 |
83 optionParser.add_option('--keep_files', | 91 optionParser.add_option('--keep_files', |
84 action='store_true', help='Do not remove temporary files.') | 92 action='store_true', help='Do not remove temporary files.') |
85 | 93 |
86 # TODO(vsm): Hook in HtmlConverter. | 94 # TODO(vsm): Hook in HtmlConverter. |
87 optionParser.add_option('--html', | 95 optionParser.add_option('--html', |
88 action='store_true', help='Invoke this in the browser instead of node/d8.'
) | 96 action='store_true', help='Invoke this in the browser instead of d8.') |
89 optionParser.add_option('--browser', | 97 optionParser.add_option('--browser', |
90 default = None, | 98 default = None, |
91 metavar='FILE', help='The browser to use to run output HTML.') | 99 metavar='FILE', help='The browser to use to run output HTML.') |
92 | 100 |
93 optionParser.add_option('--verbose', | 101 optionParser.add_option('--verbose', |
94 help='Verbose output', default=False, action='store_true') | 102 help='Verbose output', default=False, action='store_true') |
95 | 103 |
96 optionParser.set_usage("frog.py <dart-script-file> [<dart-options>]") | 104 optionParser.set_usage("frog.py <dart-script-file> [<dart-options>]") |
97 return optionParser.parse_args(args) | 105 return optionParser.parse_args(args) |
98 | 106 |
(...skipping 20 matching lines...) Expand all Loading... |
119 if not exists(dart): | 127 if not exists(dart): |
120 print "Dart VM not built. Please run the following command:" | 128 print "Dart VM not built. Please run the following command:" |
121 build_file = relpath(join(HOME, os.pardir, 'tools', 'build.py')) | 129 build_file = relpath(join(HOME, os.pardir, 'tools', 'build.py')) |
122 print ' ' + build_file + ' -m release' | 130 print ' ' + build_file + ' -m release' |
123 return 1 | 131 return 1 |
124 | 132 |
125 return compileAndRun(options, dartArgs, dart) | 133 return compileAndRun(options, dartArgs, dart) |
126 | 134 |
127 | 135 |
128 def ensureJsEngine(options): | 136 def ensureJsEngine(options): |
129 proc = subprocess.Popen("node --help", | 137 if not exists(D8): |
130 stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True) | 138 print "No engine available for running JS code." |
131 if proc.wait() != 0: | 139 print "See frog/README.txt for instructions." |
132 if not exists(D8): | 140 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 | 141 return 0 |
139 | 142 |
140 def compileAndRun(options, args, dart): | 143 def compileAndRun(options, args, dart): |
141 nodeArgs = [] | 144 jsArgs = [] |
142 for i in range(len(args)): | 145 for i in range(len(args)): |
143 if args[i].endswith('.dart'): | 146 if args[i].endswith('.dart'): |
144 nodeArgs = args[i+1:] | 147 jsArgs = args[i+1:] |
145 args = args[:i+1] | 148 args = args[:i+1] |
146 break | 149 break |
147 | 150 |
148 outfile_given = False | 151 outfile_given = False |
149 execute_output = True | 152 execute_output = True |
150 for i in range(len(args)): | 153 for i in range(len(args)): |
151 if args[i].startswith('--out'): | 154 if args[i].startswith('--out'): |
152 outfile_given = True | 155 outfile_given = True |
153 outfile = args[i][6:] | 156 outfile = args[i][6:] |
154 execute_output = False | 157 execute_output = False |
155 break; | 158 break; |
156 if args[i] == '--compile-only': | 159 if args[i] == '--compile-only': |
157 execute_output = False | 160 execute_output = False |
158 break; | 161 break; |
159 | 162 |
160 if options.verbose: print "nodeArgs %s" % ' '.join(nodeArgs); | 163 if options.verbose: print "jsArgs %s" % ' '.join(jsArgs); |
161 | 164 |
162 workdir = options.workdir | 165 workdir = options.workdir |
163 cleanup = False | 166 cleanup = False |
164 if not workdir: | 167 if not workdir: |
165 if not options.html: | 168 if not options.html: |
166 workdir = tempfile.mkdtemp() | 169 workdir = tempfile.mkdtemp() |
167 if not options.keep_files: | 170 if not options.keep_files: |
168 cleanup = True | 171 cleanup = True |
169 else: | 172 else: |
170 # A persistent location for the browser to load. | 173 # 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. | 206 # TODO(ahe): Using 253 to signal a crash to test.dart. |
204 return 253 | 207 return 253 |
205 return exit_code | 208 return exit_code |
206 | 209 |
207 result = 0 | 210 result = 0 |
208 if execute_output: | 211 if execute_output: |
209 if not options.html: | 212 if not options.html: |
210 if ensureJsEngine(options) != 0: | 213 if ensureJsEngine(options) != 0: |
211 return 1 | 214 return 1 |
212 js_cmd = options.js_cmd | 215 js_cmd = options.js_cmd |
213 result = execute(js_cmd.split(' ') + [outfile] + nodeArgs) | 216 result = execute(js_cmd.split(' ') + [outfile] + jsArgs) |
214 else: | 217 else: |
215 f = open(outhtml, 'w') | 218 f = open(outhtml, 'w') |
216 f.write(HTML) | 219 f.write(HTML) |
217 f.close() | 220 f.close() |
218 result = execute([browser, outhtml]) | 221 result = execute([browser, outhtml]) |
219 elif outfile_given: | 222 elif outfile_given: |
220 print 'Compilation succeded. Code generated in: %s' % outfile | 223 print 'Compilation succeded. Code generated in: %s' % outfile |
221 else: | 224 else: |
222 print 'Compilation succeded.' | 225 print 'Compilation succeded.' |
223 | 226 |
224 if cleanup: shutil.rmtree(workdir) | 227 if cleanup: shutil.rmtree(workdir) |
225 if result != 0: | 228 if result != 0: |
226 return 1 | 229 return 1 |
227 return 0 | 230 return 0 |
228 | 231 |
229 if __name__ == '__main__': | 232 if __name__ == '__main__': |
230 sys.exit(main(sys.argv)) | 233 sys.exit(main(sys.argv)) |
OLD | NEW |