| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // BSD-style license that can be found in the LICENSE file. | |
| 4 | |
| 5 library fasta; | |
| 6 | |
| 7 import 'dart:async' show Future; | |
| 8 | |
| 9 import 'dart:convert' show JSON; | |
| 10 | |
| 11 import 'dart:io' show BytesBuilder, File, exitCode; | |
| 12 | |
| 13 import 'package:front_end/compiler_options.dart'; | |
| 14 import 'package:front_end/src/base/processed_options.dart'; | |
| 15 import 'package:front_end/physical_file_system.dart'; | |
| 16 import 'package:front_end/src/fasta/kernel/utils.dart'; | |
| 17 | |
| 18 import 'package:kernel/kernel.dart' show Program, loadProgramFromBytes; | |
| 19 | |
| 20 import 'compiler_command_line.dart' show CompilerCommandLine; | |
| 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 | |
| 39 const bool summary = const bool.fromEnvironment("summary", defaultValue: false); | |
| 40 const int iterations = const int.fromEnvironment("iterations", defaultValue: 1); | |
| 41 | |
| 42 compileEntryPoint(List<String> arguments) async { | |
| 43 // Timing results for each iteration | |
| 44 List<double> elapsedTimes = <double>[]; | |
| 45 | |
| 46 for (int i = 0; i < iterations; i++) { | |
| 47 if (i > 0) { | |
| 48 print("\n\n=== Iteration ${i+1} of $iterations"); | |
| 49 } | |
| 50 var stopwatch = new Stopwatch()..start(); | |
| 51 await compile(arguments); | |
| 52 stopwatch.stop(); | |
| 53 | |
| 54 elapsedTimes.add(stopwatch.elapsedMilliseconds.toDouble()); | |
| 55 } | |
| 56 | |
| 57 if (summary) { | |
| 58 var json = JSON.encode(<String, dynamic>{'elapsedTimes': elapsedTimes}); | |
| 59 print('\nSummary: $json'); | |
| 60 } | |
| 61 } | |
| 62 | |
| 63 outlineEntryPoint(List<String> arguments) async { | |
| 64 for (int i = 0; i < iterations; i++) { | |
| 65 if (i > 0) { | |
| 66 print("\n"); | |
| 67 } | |
| 68 await outline(arguments); | |
| 69 } | |
| 70 } | |
| 71 | |
| 72 Future<KernelTarget> outline(List<String> arguments) async { | |
| 73 try { | |
| 74 return await CompilerCommandLine.withGlobalOptions( | |
| 75 "outline", arguments, true, (CompilerContext c, _) async { | |
| 76 if (c.options.verbose) { | |
| 77 print("Building outlines for ${arguments.join(' ')}"); | |
| 78 } | |
| 79 CompileTask task = | |
| 80 new CompileTask(c, new Ticker(isVerbose: c.options.verbose)); | |
| 81 return await task.buildOutline(c.options.output); | |
| 82 }); | |
| 83 } on deprecated_InputError catch (e) { | |
| 84 exitCode = 1; | |
| 85 CompilerContext.runWithDefaultOptions( | |
| 86 (c) => c.report(deprecated_InputError.toMessage(e), Severity.error)); | |
| 87 return null; | |
| 88 } | |
| 89 } | |
| 90 | |
| 91 Future<Uri> compile(List<String> arguments) async { | |
| 92 try { | |
| 93 return await CompilerCommandLine.withGlobalOptions( | |
| 94 "compile", arguments, true, (CompilerContext c, _) async { | |
| 95 if (c.options.verbose) { | |
| 96 print("Compiling directly to Kernel: ${arguments.join(' ')}"); | |
| 97 } | |
| 98 CompileTask task = | |
| 99 new CompileTask(c, new Ticker(isVerbose: c.options.verbose)); | |
| 100 return await task.compile(); | |
| 101 }); | |
| 102 } on deprecated_InputError catch (e) { | |
| 103 exitCode = 1; | |
| 104 CompilerContext.runWithDefaultOptions( | |
| 105 (c) => c.report(deprecated_InputError.toMessage(e), Severity.error)); | |
| 106 return null; | |
| 107 } | |
| 108 } | |
| 109 | |
| 110 class CompileTask { | |
| 111 final CompilerContext c; | |
| 112 final Ticker ticker; | |
| 113 | |
| 114 CompileTask(this.c, this.ticker); | |
| 115 | |
| 116 DillTarget createDillTarget(UriTranslator uriTranslator) { | |
| 117 return new DillTarget(ticker, uriTranslator, c.options.target); | |
| 118 } | |
| 119 | |
| 120 KernelTarget createKernelTarget( | |
| 121 DillTarget dillTarget, UriTranslator uriTranslator, bool strongMode) { | |
| 122 return new KernelTarget( | |
| 123 c.fileSystem, false, dillTarget, uriTranslator, c.uriToSource); | |
| 124 } | |
| 125 | |
| 126 Future<KernelTarget> buildOutline([Uri output]) async { | |
| 127 UriTranslator uriTranslator = await c.options.getUriTranslator(); | |
| 128 ticker.logMs("Read packages file"); | |
| 129 DillTarget dillTarget = createDillTarget(uriTranslator); | |
| 130 KernelTarget kernelTarget = | |
| 131 createKernelTarget(dillTarget, uriTranslator, c.options.strongMode); | |
| 132 if (c.options.strongMode) { | |
| 133 print("Note: strong mode support is preliminary and may not work."); | |
| 134 } | |
| 135 Uri platform = c.options.sdkSummary; | |
| 136 if (platform != null) { | |
| 137 _appendDillForUri(dillTarget, platform); | |
| 138 } | |
| 139 Uri uri = c.options.inputs.first; | |
| 140 String path = uriTranslator.translate(uri)?.path ?? uri.path; | |
| 141 if (path.endsWith(".dart")) { | |
| 142 kernelTarget.read(uri); | |
| 143 } else { | |
| 144 deprecated_inputError(uri, -1, "Unexpected input: $uri"); | |
| 145 } | |
| 146 await dillTarget.buildOutlines(); | |
| 147 var outline = await kernelTarget.buildOutlines(); | |
| 148 if (c.options.debugDump && output != null) { | |
| 149 printProgramText(outline, libraryFilter: kernelTarget.isSourceLibrary); | |
| 150 } | |
| 151 if (output != null) { | |
| 152 await writeProgramToFile(outline, output); | |
| 153 ticker.logMs("Wrote outline to ${output.toFilePath()}"); | |
| 154 } | |
| 155 return kernelTarget; | |
| 156 } | |
| 157 | |
| 158 Future<Uri> compile() async { | |
| 159 KernelTarget kernelTarget = await buildOutline(); | |
| 160 if (exitCode != 0) return null; | |
| 161 Uri uri = c.options.output; | |
| 162 var program = await kernelTarget.buildProgram(verify: c.options.verify); | |
| 163 if (c.options.debugDump) { | |
| 164 printProgramText(program, libraryFilter: kernelTarget.isSourceLibrary); | |
| 165 } | |
| 166 await writeProgramToFile(program, uri); | |
| 167 ticker.logMs("Wrote program to ${uri.toFilePath()}"); | |
| 168 return uri; | |
| 169 } | |
| 170 } | |
| 171 | |
| 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 | |
| 204 /// to the [dillTarget]. | |
| 205 void _appendDillForUri(DillTarget dillTarget, Uri uri) { | |
| 206 var bytes = new File.fromUri(uri).readAsBytesSync(); | |
| 207 var platformProgram = loadProgramFromBytes(bytes); | |
| 208 platformProgram.unbindCanonicalNames(); | |
| 209 dillTarget.loader.appendLibraries(platformProgram); | |
| 210 } | |
| 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 |