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; |
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 BytesBuilder, Directory, File, exitCode; |
12 | 12 |
| 13 import 'package:front_end/file_system.dart'; |
13 import 'package:front_end/physical_file_system.dart'; | 14 import 'package:front_end/physical_file_system.dart'; |
14 import 'package:front_end/src/fasta/kernel/utils.dart'; | 15 import 'package:front_end/src/fasta/kernel/utils.dart'; |
| 16 import 'package:kernel/binary/ast_to_binary.dart' |
| 17 show LibraryFilteringBinaryPrinter; |
15 | 18 |
16 import 'package:kernel/kernel.dart' show Program, loadProgramFromBytes; | 19 import 'package:kernel/kernel.dart' show Library, Program, loadProgramFromBytes; |
| 20 |
| 21 import 'package:kernel/target/targets.dart' show Target; |
17 | 22 |
18 import 'compiler_command_line.dart' show CompilerCommandLine; | 23 import 'compiler_command_line.dart' show CompilerCommandLine; |
19 | 24 |
20 import 'compiler_context.dart' show CompilerContext; | 25 import 'compiler_context.dart' show CompilerContext; |
21 | 26 |
22 import 'errors.dart' show InputError, inputError; | 27 import 'errors.dart' show InputError, formatUnexpected, inputError, reportCrash; |
23 | 28 |
24 import 'kernel/kernel_target.dart' show KernelTarget; | 29 import 'kernel/kernel_target.dart' show KernelTarget; |
25 | 30 |
26 import 'dill/dill_target.dart' show DillTarget; | 31 import 'dill/dill_target.dart' show DillTarget; |
27 | 32 |
28 import 'compile_platform.dart' show compilePlatformInternal; | 33 import 'compile_platform.dart' show compilePlatformInternal; |
29 | 34 |
30 import 'ticker.dart' show Ticker; | 35 import 'ticker.dart' show Ticker; |
31 | 36 |
32 import 'translate_uri.dart' show TranslateUri; | 37 import 'translate_uri.dart' show TranslateUri; |
33 | 38 |
| 39 import 'vm.dart' show CompilationResult; |
| 40 |
34 const bool summary = const bool.fromEnvironment("summary", defaultValue: false); | 41 const bool summary = const bool.fromEnvironment("summary", defaultValue: false); |
35 const int iterations = const int.fromEnvironment("iterations", defaultValue: 1); | 42 const int iterations = const int.fromEnvironment("iterations", defaultValue: 1); |
36 | 43 |
37 compileEntryPoint(List<String> arguments) async { | 44 compileEntryPoint(List<String> arguments) async { |
38 // Timing results for each iteration | 45 // Timing results for each iteration |
39 List<double> elapsedTimes = <double>[]; | 46 List<double> elapsedTimes = <double>[]; |
40 | 47 |
41 for (int i = 0; i < iterations; i++) { | 48 for (int i = 0; i < iterations; i++) { |
42 if (i > 0) { | 49 if (i > 0) { |
43 print("\n\n=== Iteration ${i+1} of $iterations"); | 50 print("\n\n=== Iteration ${i+1} of $iterations"); |
(...skipping 113 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
157 var program = await kernelTarget.buildProgram(verify: c.options.verify); | 164 var program = await kernelTarget.buildProgram(verify: c.options.verify); |
158 if (c.options.dumpIr) { | 165 if (c.options.dumpIr) { |
159 printProgramText(program, libraryFilter: kernelTarget.isSourceLibrary); | 166 printProgramText(program, libraryFilter: kernelTarget.isSourceLibrary); |
160 } | 167 } |
161 await writeProgramToFile(program, uri); | 168 await writeProgramToFile(program, uri); |
162 ticker.logMs("Wrote program to ${uri.toFilePath()}"); | 169 ticker.logMs("Wrote program to ${uri.toFilePath()}"); |
163 return uri; | 170 return uri; |
164 } | 171 } |
165 } | 172 } |
166 | 173 |
| 174 Future<CompilationResult> parseScript( |
| 175 Uri fileName, Uri packages, Uri patchedSdk, Target backendTarget, |
| 176 {bool verbose: false}) async { |
| 177 return parseScriptInFileSystem(fileName, PhysicalFileSystem.instance, |
| 178 packages, patchedSdk, backendTarget, |
| 179 verbose: verbose); |
| 180 } |
| 181 |
| 182 Future<CompilationResult> parseScriptInFileSystem(Uri fileName, |
| 183 FileSystem fileSystem, Uri packages, Uri patchedSdk, Target backendTarget, |
| 184 {bool verbose: false}) async { |
| 185 try { |
| 186 if (!await fileSystem.entityForUri(fileName).exists()) { |
| 187 return new CompilationResult.error( |
| 188 formatUnexpected(fileName, -1, "No such file.")); |
| 189 } |
| 190 if (!await new Directory.fromUri(patchedSdk).exists()) { |
| 191 return new CompilationResult.error( |
| 192 formatUnexpected(patchedSdk, -1, "Patched sdk directory not found.")); |
| 193 } |
| 194 |
| 195 Program program; |
| 196 try { |
| 197 TranslateUri uriTranslator = |
| 198 await TranslateUri.parse(fileSystem, patchedSdk, packages: packages); |
| 199 final Ticker ticker = new Ticker(isVerbose: verbose); |
| 200 final DillTarget dillTarget = |
| 201 new DillTarget(ticker, uriTranslator, backendTarget); |
| 202 _appendDillForUri(dillTarget, patchedSdk.resolve('platform.dill')); |
| 203 final KernelTarget kernelTarget = |
| 204 new KernelTarget(fileSystem, dillTarget, uriTranslator); |
| 205 kernelTarget.read(fileName); |
| 206 await dillTarget.buildOutlines(); |
| 207 await kernelTarget.buildOutlines(); |
| 208 program = await kernelTarget.buildProgram(); |
| 209 if (kernelTarget.errors.isNotEmpty) { |
| 210 return new CompilationResult.errors(kernelTarget.errors); |
| 211 } |
| 212 } on InputError catch (e) { |
| 213 return new CompilationResult.error(e.format()); |
| 214 } |
| 215 |
| 216 if (program.mainMethod == null) { |
| 217 return new CompilationResult.error("No 'main' method found."); |
| 218 } |
| 219 |
| 220 // Write the program to a list of bytes and return it. Do not include |
| 221 // libraries that have a dart: import URI. |
| 222 // |
| 223 // TODO(kmillikin): This is intended to exclude platform libraries that are |
| 224 // included in the Kernel binary platform platform.dill. It does not |
| 225 // necessarily exclude exactly the platform libraries. Use a better |
| 226 // predicate that knows what is included in platform.dill. |
| 227 var sink = new ByteSink(); |
| 228 bool predicate(Library library) => !library.importUri.isScheme('dart'); |
| 229 new LibraryFilteringBinaryPrinter(sink, predicate) |
| 230 .writeProgramFile(program); |
| 231 return new CompilationResult.ok(sink.builder.takeBytes()); |
| 232 } catch (e, s) { |
| 233 return reportCrash(e, s, fileName); |
| 234 } |
| 235 } |
| 236 |
167 Future compilePlatform(Uri patchedSdk, Uri fullOutput, | 237 Future compilePlatform(Uri patchedSdk, Uri fullOutput, |
168 {Uri outlineOutput, | 238 {Uri outlineOutput, |
169 Uri packages, | 239 Uri packages, |
170 bool verbose: false, | 240 bool verbose: false, |
171 String backendTarget}) async { | 241 String backendTarget}) async { |
172 backendTarget ??= "vm_fasta"; | 242 backendTarget ??= "vm_fasta"; |
173 Ticker ticker = new Ticker(isVerbose: verbose); | 243 Ticker ticker = new Ticker(isVerbose: verbose); |
174 await CompilerCommandLine.withGlobalOptions("", [""], (CompilerContext c) { | 244 await CompilerCommandLine.withGlobalOptions("", [""], (CompilerContext c) { |
175 c.options.options["--target"] = backendTarget; | 245 c.options.options["--target"] = backendTarget; |
176 c.options.options["--packages"] = packages; | 246 c.options.options["--packages"] = packages; |
177 if (verbose) { | 247 if (verbose) { |
178 c.options.options["--verbose"] = true; | 248 c.options.options["--verbose"] = true; |
179 } | 249 } |
180 c.options.validate(); | 250 c.options.validate(); |
181 return compilePlatformInternal( | 251 return compilePlatformInternal( |
182 c, ticker, patchedSdk, fullOutput, outlineOutput); | 252 c, ticker, patchedSdk, fullOutput, outlineOutput); |
183 }); | 253 }); |
184 } | 254 } |
185 | 255 |
186 // TODO(sigmund): reimplement this API using the directive listener intead. | 256 Future writeDepsFile(Uri script, Uri depsFile, Uri output, |
187 Future<List<Uri>> getDependencies(Uri script, | |
188 {Uri sdk, | 257 {Uri sdk, |
189 Uri packages, | 258 Uri packages, |
190 Uri platform, | 259 Uri platform, |
| 260 Iterable<Uri> extraDependencies, |
191 bool verbose: false, | 261 bool verbose: false, |
192 String backendTarget}) async { | 262 String backendTarget}) async { |
193 backendTarget ??= "vm_fasta"; | 263 backendTarget ??= "vm_fasta"; |
194 Ticker ticker = new Ticker(isVerbose: verbose); | 264 Ticker ticker = new Ticker(isVerbose: verbose); |
195 return await CompilerCommandLine.withGlobalOptions("", [""], | 265 await CompilerCommandLine.withGlobalOptions("", [""], |
196 (CompilerContext c) async { | 266 (CompilerContext c) async { |
197 c.options.options["--target"] = backendTarget; | 267 c.options.options["--target"] = backendTarget; |
198 c.options.options["--strong-mode"] = false; | 268 c.options.options["--strong-mode"] = false; |
199 c.options.options["--packages"] = packages; | 269 c.options.options["--packages"] = packages; |
200 if (verbose) { | 270 if (verbose) { |
201 c.options.options["--verbose"] = true; | 271 c.options.options["--verbose"] = true; |
202 } | 272 } |
203 c.options.validate(); | 273 c.options.validate(); |
204 sdk ??= c.options.sdk; | 274 sdk ??= c.options.sdk; |
205 | 275 |
206 TranslateUri uriTranslator = await TranslateUri.parse(c.fileSystem, sdk, | 276 TranslateUri uriTranslator = await TranslateUri.parse(c.fileSystem, sdk, |
207 packages: c.options.packages); | 277 packages: c.options.packages); |
208 ticker.logMs("Read packages file"); | 278 ticker.logMs("Read packages file"); |
209 DillTarget dillTarget = | 279 DillTarget dillTarget = |
210 new DillTarget(ticker, uriTranslator, c.options.target); | 280 new DillTarget(ticker, uriTranslator, c.options.target); |
211 if (platform != null) _appendDillForUri(dillTarget, platform); | 281 _appendDillForUri(dillTarget, platform); |
212 KernelTarget kernelTarget = new KernelTarget( | 282 KernelTarget kernelTarget = new KernelTarget( |
213 PhysicalFileSystem.instance, dillTarget, uriTranslator, c.uriToSource); | 283 PhysicalFileSystem.instance, dillTarget, uriTranslator, c.uriToSource); |
214 | 284 |
215 kernelTarget.read(script); | 285 kernelTarget.read(script); |
216 await dillTarget.buildOutlines(); | 286 await dillTarget.buildOutlines(); |
217 await kernelTarget.loader.buildOutlines(); | 287 await kernelTarget.loader.buildOutlines(); |
218 return await kernelTarget.loader.getDependencies(); | 288 await kernelTarget.writeDepsFile(output, depsFile, |
| 289 extraDependencies: extraDependencies); |
219 }); | 290 }); |
220 } | 291 } |
221 | 292 |
222 /// Load the [Program] from the given [uri] and append its libraries | 293 /// Load the [Program] from the given [uri] and append its libraries |
223 /// to the [dillTarget]. | 294 /// to the [dillTarget]. |
224 void _appendDillForUri(DillTarget dillTarget, Uri uri) { | 295 void _appendDillForUri(DillTarget dillTarget, Uri uri) { |
225 var bytes = new File.fromUri(uri).readAsBytesSync(); | 296 var bytes = new File.fromUri(uri).readAsBytesSync(); |
226 var platformProgram = loadProgramFromBytes(bytes); | 297 var platformProgram = loadProgramFromBytes(bytes); |
227 platformProgram.unbindCanonicalNames(); | 298 platformProgram.unbindCanonicalNames(); |
228 dillTarget.loader.appendLibraries(platformProgram); | 299 dillTarget.loader.appendLibraries(platformProgram); |
229 } | 300 } |
230 | 301 |
231 // TODO(ahe): https://github.com/dart-lang/sdk/issues/28316 | 302 // TODO(ahe): https://github.com/dart-lang/sdk/issues/28316 |
232 class ByteSink implements Sink<List<int>> { | 303 class ByteSink implements Sink<List<int>> { |
233 final BytesBuilder builder = new BytesBuilder(); | 304 final BytesBuilder builder = new BytesBuilder(); |
234 | 305 |
235 void add(List<int> data) { | 306 void add(List<int> data) { |
236 builder.add(data); | 307 builder.add(data); |
237 } | 308 } |
238 | 309 |
239 void close() { | 310 void close() { |
240 // Nothing to do. | 311 // Nothing to do. |
241 } | 312 } |
242 } | 313 } |
OLD | NEW |