OLD | NEW |
1 // Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file |
2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 library fasta; | 5 library fasta.tool.entry_points; |
6 | 6 |
7 import 'dart:async' show Future; | 7 import 'dart:async' show Future; |
8 | 8 |
9 import 'dart:convert' show JSON; | 9 import 'dart:convert' show JSON; |
10 | 10 |
11 import 'dart:io' show BytesBuilder, File, exitCode; | 11 import 'dart:io' show File, exitCode; |
12 | 12 |
13 import 'package:front_end/compiler_options.dart'; | 13 import 'package:front_end/src/fasta/compiler_context.dart' show CompilerContext; |
14 import 'package:front_end/src/base/processed_options.dart'; | 14 |
15 import 'package:front_end/physical_file_system.dart'; | 15 import 'package:front_end/src/fasta/deprecated_problems.dart' |
16 import 'package:front_end/src/fasta/kernel/utils.dart'; | 16 show deprecated_InputError, deprecated_inputError; |
| 17 |
| 18 import 'package:front_end/src/fasta/dill/dill_target.dart' show DillTarget; |
| 19 |
| 20 import 'package:front_end/src/fasta/kernel/kernel_target.dart' |
| 21 show KernelTarget; |
| 22 |
| 23 import 'package:front_end/src/fasta/kernel/utils.dart' |
| 24 show printProgramText, writeProgramToFile; |
| 25 |
| 26 import 'package:front_end/src/fasta/severity.dart' show Severity; |
| 27 |
| 28 import 'package:front_end/src/fasta/ticker.dart' show Ticker; |
| 29 |
| 30 import 'package:front_end/src/fasta/uri_translator.dart' show UriTranslator; |
17 | 31 |
18 import 'package:kernel/kernel.dart' show Program, loadProgramFromBytes; | 32 import 'package:kernel/kernel.dart' show Program, loadProgramFromBytes; |
19 | 33 |
20 import 'compiler_command_line.dart' show CompilerCommandLine; | 34 import 'command_line.dart' show withGlobalOptions; |
21 | |
22 import 'compiler_context.dart' show CompilerContext; | |
23 | |
24 import 'deprecated_problems.dart' | |
25 show deprecated_InputError, deprecated_inputError; | |
26 | |
27 import 'kernel/kernel_target.dart' show KernelTarget; | |
28 | |
29 import 'package:kernel/target/targets.dart' show Target; | |
30 | |
31 import 'dill/dill_target.dart' show DillTarget; | |
32 | |
33 import 'severity.dart' show Severity; | |
34 | |
35 import 'ticker.dart' show Ticker; | |
36 | |
37 import 'uri_translator.dart' show UriTranslator; | |
38 | 35 |
39 const bool summary = const bool.fromEnvironment("summary", defaultValue: false); | 36 const bool summary = const bool.fromEnvironment("summary", defaultValue: false); |
| 37 |
40 const int iterations = const int.fromEnvironment("iterations", defaultValue: 1); | 38 const int iterations = const int.fromEnvironment("iterations", defaultValue: 1); |
41 | 39 |
42 compileEntryPoint(List<String> arguments) async { | 40 compileEntryPoint(List<String> arguments) async { |
43 // Timing results for each iteration | 41 // Timing results for each iteration |
44 List<double> elapsedTimes = <double>[]; | 42 List<double> elapsedTimes = <double>[]; |
45 | 43 |
46 for (int i = 0; i < iterations; i++) { | 44 for (int i = 0; i < iterations; i++) { |
47 if (i > 0) { | 45 if (i > 0) { |
48 print("\n\n=== Iteration ${i+1} of $iterations"); | 46 print("\n\n=== Iteration ${i+1} of $iterations"); |
49 } | 47 } |
(...skipping 14 matching lines...) Expand all Loading... |
64 for (int i = 0; i < iterations; i++) { | 62 for (int i = 0; i < iterations; i++) { |
65 if (i > 0) { | 63 if (i > 0) { |
66 print("\n"); | 64 print("\n"); |
67 } | 65 } |
68 await outline(arguments); | 66 await outline(arguments); |
69 } | 67 } |
70 } | 68 } |
71 | 69 |
72 Future<KernelTarget> outline(List<String> arguments) async { | 70 Future<KernelTarget> outline(List<String> arguments) async { |
73 try { | 71 try { |
74 return await CompilerCommandLine.withGlobalOptions( | 72 return await withGlobalOptions("outline", arguments, true, |
75 "outline", arguments, true, (CompilerContext c, _) async { | 73 (CompilerContext c, _) async { |
76 if (c.options.verbose) { | 74 if (c.options.verbose) { |
77 print("Building outlines for ${arguments.join(' ')}"); | 75 print("Building outlines for ${arguments.join(' ')}"); |
78 } | 76 } |
79 CompileTask task = | 77 CompileTask task = |
80 new CompileTask(c, new Ticker(isVerbose: c.options.verbose)); | 78 new CompileTask(c, new Ticker(isVerbose: c.options.verbose)); |
81 return await task.buildOutline(c.options.output); | 79 return await task.buildOutline(c.options.output); |
82 }); | 80 }); |
83 } on deprecated_InputError catch (e) { | 81 } on deprecated_InputError catch (e) { |
84 exitCode = 1; | 82 exitCode = 1; |
85 CompilerContext.runWithDefaultOptions( | 83 CompilerContext.runWithDefaultOptions( |
86 (c) => c.report(deprecated_InputError.toMessage(e), Severity.error)); | 84 (c) => c.report(deprecated_InputError.toMessage(e), Severity.error)); |
87 return null; | 85 return null; |
88 } | 86 } |
89 } | 87 } |
90 | 88 |
91 Future<Uri> compile(List<String> arguments) async { | 89 Future<Uri> compile(List<String> arguments) async { |
92 try { | 90 try { |
93 return await CompilerCommandLine.withGlobalOptions( | 91 return await withGlobalOptions("compile", arguments, true, |
94 "compile", arguments, true, (CompilerContext c, _) async { | 92 (CompilerContext c, _) async { |
95 if (c.options.verbose) { | 93 if (c.options.verbose) { |
96 print("Compiling directly to Kernel: ${arguments.join(' ')}"); | 94 print("Compiling directly to Kernel: ${arguments.join(' ')}"); |
97 } | 95 } |
98 CompileTask task = | 96 CompileTask task = |
99 new CompileTask(c, new Ticker(isVerbose: c.options.verbose)); | 97 new CompileTask(c, new Ticker(isVerbose: c.options.verbose)); |
100 return await task.compile(); | 98 return await task.compile(); |
101 }); | 99 }); |
102 } on deprecated_InputError catch (e) { | 100 } on deprecated_InputError catch (e) { |
103 exitCode = 1; | 101 exitCode = 1; |
104 CompilerContext.runWithDefaultOptions( | 102 CompilerContext.runWithDefaultOptions( |
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
162 var program = await kernelTarget.buildProgram(verify: c.options.verify); | 160 var program = await kernelTarget.buildProgram(verify: c.options.verify); |
163 if (c.options.debugDump) { | 161 if (c.options.debugDump) { |
164 printProgramText(program, libraryFilter: kernelTarget.isSourceLibrary); | 162 printProgramText(program, libraryFilter: kernelTarget.isSourceLibrary); |
165 } | 163 } |
166 await writeProgramToFile(program, uri); | 164 await writeProgramToFile(program, uri); |
167 ticker.logMs("Wrote program to ${uri.toFilePath()}"); | 165 ticker.logMs("Wrote program to ${uri.toFilePath()}"); |
168 return uri; | 166 return uri; |
169 } | 167 } |
170 } | 168 } |
171 | 169 |
172 // TODO(sigmund): reimplement this API using the directive listener intead. | |
173 Future<List<Uri>> getDependencies(Uri script, | |
174 {Uri sdk, | |
175 Uri packages, | |
176 Uri platform, | |
177 bool verbose: false, | |
178 Target target}) async { | |
179 var options = new CompilerOptions() | |
180 ..target = target | |
181 ..verbose = verbose | |
182 ..packagesFileUri = packages | |
183 ..sdkSummary = platform | |
184 ..sdkRoot = sdk; | |
185 var pOptions = new ProcessedOptions(options); | |
186 return await CompilerContext.runWithOptions(pOptions, | |
187 (CompilerContext c) async { | |
188 UriTranslator uriTranslator = await c.options.getUriTranslator(); | |
189 c.options.ticker.logMs("Read packages file"); | |
190 DillTarget dillTarget = | |
191 new DillTarget(c.options.ticker, uriTranslator, c.options.target); | |
192 if (platform != null) _appendDillForUri(dillTarget, platform); | |
193 KernelTarget kernelTarget = new KernelTarget(PhysicalFileSystem.instance, | |
194 false, dillTarget, uriTranslator, c.uriToSource); | |
195 | |
196 kernelTarget.read(script); | |
197 await dillTarget.buildOutlines(); | |
198 await kernelTarget.loader.buildOutlines(); | |
199 return await kernelTarget.loader.getDependencies(); | |
200 }); | |
201 } | |
202 | |
203 /// Load the [Program] from the given [uri] and append its libraries | 170 /// Load the [Program] from the given [uri] and append its libraries |
204 /// to the [dillTarget]. | 171 /// to the [dillTarget]. |
205 void _appendDillForUri(DillTarget dillTarget, Uri uri) { | 172 void _appendDillForUri(DillTarget dillTarget, Uri uri) { |
206 var bytes = new File.fromUri(uri).readAsBytesSync(); | 173 var bytes = new File.fromUri(uri).readAsBytesSync(); |
207 var platformProgram = loadProgramFromBytes(bytes); | 174 var platformProgram = loadProgramFromBytes(bytes); |
208 platformProgram.unbindCanonicalNames(); | 175 platformProgram.unbindCanonicalNames(); |
209 dillTarget.loader.appendLibraries(platformProgram); | 176 dillTarget.loader.appendLibraries(platformProgram); |
210 } | 177 } |
211 | |
212 // TODO(ahe): https://github.com/dart-lang/sdk/issues/28316 | |
213 class ByteSink implements Sink<List<int>> { | |
214 final BytesBuilder builder = new BytesBuilder(); | |
215 | |
216 void add(List<int> data) { | |
217 builder.add(data); | |
218 } | |
219 | |
220 void close() { | |
221 // Nothing to do. | |
222 } | |
223 } | |
OLD | NEW |