| 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, File, exitCode; |
| 12 | 12 |
| 13 import 'package:front_end/compiler_options.dart'; |
| 14 import 'package:front_end/src/base/processed_options.dart'; |
| 13 import 'package:front_end/physical_file_system.dart'; | 15 import 'package:front_end/physical_file_system.dart'; |
| 14 import 'package:front_end/src/fasta/kernel/utils.dart'; | 16 import 'package:front_end/src/fasta/kernel/utils.dart'; |
| 15 import 'package:front_end/src/fasta/uri_translator_impl.dart'; | |
| 16 | 17 |
| 17 import 'package:kernel/kernel.dart' show Program, loadProgramFromBytes; | 18 import 'package:kernel/kernel.dart' show Program, loadProgramFromBytes; |
| 18 | 19 |
| 19 import 'compiler_command_line.dart' show CompilerCommandLine; | 20 import 'compiler_command_line.dart' show CompilerCommandLine; |
| 20 | 21 |
| 21 import 'compiler_context.dart' show CompilerContext; | 22 import 'compiler_context.dart' show CompilerContext; |
| 22 | 23 |
| 23 import 'deprecated_problems.dart' | 24 import 'deprecated_problems.dart' |
| 24 show deprecated_InputError, deprecated_inputError; | 25 show deprecated_InputError, deprecated_inputError; |
| 25 | 26 |
| 26 import 'kernel/kernel_target.dart' show KernelTarget; | 27 import 'kernel/kernel_target.dart' show KernelTarget; |
| 27 | 28 |
| 29 import 'package:kernel/target/targets.dart' show Target; |
| 30 |
| 28 import 'dill/dill_target.dart' show DillTarget; | 31 import 'dill/dill_target.dart' show DillTarget; |
| 29 | 32 |
| 30 import 'compile_platform.dart' show compilePlatformInternal; | |
| 31 | |
| 32 import 'severity.dart' show Severity; | 33 import 'severity.dart' show Severity; |
| 33 | 34 |
| 34 import 'ticker.dart' show Ticker; | 35 import 'ticker.dart' show Ticker; |
| 35 | 36 |
| 36 import 'uri_translator.dart' show UriTranslator; | 37 import 'uri_translator.dart' show UriTranslator; |
| 37 | 38 |
| 38 const bool summary = const bool.fromEnvironment("summary", defaultValue: false); | 39 const bool summary = const bool.fromEnvironment("summary", defaultValue: false); |
| 39 const int iterations = const int.fromEnvironment("iterations", defaultValue: 1); | 40 const int iterations = const int.fromEnvironment("iterations", defaultValue: 1); |
| 40 | 41 |
| 41 compileEntryPoint(List<String> arguments) async { | 42 compileEntryPoint(List<String> arguments) async { |
| (...skipping 21 matching lines...) Expand all Loading... |
| 63 for (int i = 0; i < iterations; i++) { | 64 for (int i = 0; i < iterations; i++) { |
| 64 if (i > 0) { | 65 if (i > 0) { |
| 65 print("\n"); | 66 print("\n"); |
| 66 } | 67 } |
| 67 await outline(arguments); | 68 await outline(arguments); |
| 68 } | 69 } |
| 69 } | 70 } |
| 70 | 71 |
| 71 Future<KernelTarget> outline(List<String> arguments) async { | 72 Future<KernelTarget> outline(List<String> arguments) async { |
| 72 try { | 73 try { |
| 73 return await CompilerCommandLine.withGlobalOptions("outline", arguments, | 74 return await CompilerCommandLine.withGlobalOptions( |
| 74 (CompilerContext c) async { | 75 "outline", arguments, true, (CompilerContext c, _) async { |
| 75 if (c.options.verbose) { | 76 if (c.options.verbose) { |
| 76 print("Building outlines for ${arguments.join(' ')}"); | 77 print("Building outlines for ${arguments.join(' ')}"); |
| 77 } | 78 } |
| 78 CompileTask task = | 79 CompileTask task = |
| 79 new CompileTask(c, new Ticker(isVerbose: c.options.verbose)); | 80 new CompileTask(c, new Ticker(isVerbose: c.options.verbose)); |
| 80 return await task.buildOutline(c.options.output); | 81 return await task.buildOutline(c.options.output); |
| 81 }); | 82 }); |
| 82 } on deprecated_InputError catch (e) { | 83 } on deprecated_InputError catch (e) { |
| 83 exitCode = 1; | 84 exitCode = 1; |
| 84 CompilerCommandLine.deprecated_withDefaultOptions(() => CompilerContext | 85 CompilerContext.runWithDefaultOptions( |
| 85 .current | 86 (c) => c.report(deprecated_InputError.toMessage(e), Severity.error)); |
| 86 .report(deprecated_InputError.toMessage(e), Severity.error)); | |
| 87 return null; | 87 return null; |
| 88 } | 88 } |
| 89 } | 89 } |
| 90 | 90 |
| 91 Future<Uri> compile(List<String> arguments) async { | 91 Future<Uri> compile(List<String> arguments) async { |
| 92 try { | 92 try { |
| 93 return await CompilerCommandLine.withGlobalOptions("compile", arguments, | 93 return await CompilerCommandLine.withGlobalOptions( |
| 94 (CompilerContext c) async { | 94 "compile", arguments, true, (CompilerContext c, _) async { |
| 95 if (c.options.verbose) { | 95 if (c.options.verbose) { |
| 96 print("Compiling directly to Kernel: ${arguments.join(' ')}"); | 96 print("Compiling directly to Kernel: ${arguments.join(' ')}"); |
| 97 } | 97 } |
| 98 CompileTask task = | 98 CompileTask task = |
| 99 new CompileTask(c, new Ticker(isVerbose: c.options.verbose)); | 99 new CompileTask(c, new Ticker(isVerbose: c.options.verbose)); |
| 100 return await task.compile(); | 100 return await task.compile(); |
| 101 }); | 101 }); |
| 102 } on deprecated_InputError catch (e) { | 102 } on deprecated_InputError catch (e) { |
| 103 exitCode = 1; | 103 exitCode = 1; |
| 104 CompilerCommandLine.deprecated_withDefaultOptions(() => CompilerContext | 104 CompilerContext.runWithDefaultOptions( |
| 105 .current | 105 (c) => c.report(deprecated_InputError.toMessage(e), Severity.error)); |
| 106 .report(deprecated_InputError.toMessage(e), Severity.error)); | |
| 107 return null; | 106 return null; |
| 108 } | 107 } |
| 109 } | 108 } |
| 110 | 109 |
| 111 class CompileTask { | 110 class CompileTask { |
| 112 final CompilerContext c; | 111 final CompilerContext c; |
| 113 final Ticker ticker; | 112 final Ticker ticker; |
| 114 | 113 |
| 115 CompileTask(this.c, this.ticker); | 114 CompileTask(this.c, this.ticker); |
| 116 | 115 |
| 117 DillTarget createDillTarget(UriTranslator uriTranslator) { | 116 DillTarget createDillTarget(UriTranslator uriTranslator) { |
| 118 return new DillTarget(ticker, uriTranslator, c.options.target); | 117 return new DillTarget(ticker, uriTranslator, c.options.target); |
| 119 } | 118 } |
| 120 | 119 |
| 121 KernelTarget createKernelTarget( | 120 KernelTarget createKernelTarget( |
| 122 DillTarget dillTarget, UriTranslator uriTranslator, bool strongMode) { | 121 DillTarget dillTarget, UriTranslator uriTranslator, bool strongMode) { |
| 123 return new KernelTarget( | 122 return new KernelTarget( |
| 124 c.fileSystem, false, dillTarget, uriTranslator, c.uriToSource); | 123 c.fileSystem, false, dillTarget, uriTranslator, c.uriToSource); |
| 125 } | 124 } |
| 126 | 125 |
| 127 Future<KernelTarget> buildOutline([Uri output]) async { | 126 Future<KernelTarget> buildOutline([Uri output]) async { |
| 128 UriTranslator uriTranslator = await UriTranslatorImpl | 127 UriTranslator uriTranslator = await c.options.getUriTranslator(); |
| 129 .parse(c.fileSystem, c.options.sdk, packages: c.options.packages); | |
| 130 ticker.logMs("Read packages file"); | 128 ticker.logMs("Read packages file"); |
| 131 DillTarget dillTarget = createDillTarget(uriTranslator); | 129 DillTarget dillTarget = createDillTarget(uriTranslator); |
| 132 KernelTarget kernelTarget = | 130 KernelTarget kernelTarget = |
| 133 createKernelTarget(dillTarget, uriTranslator, c.options.strongMode); | 131 createKernelTarget(dillTarget, uriTranslator, c.options.strongMode); |
| 134 if (c.options.strongMode) { | 132 if (c.options.strongMode) { |
| 135 print("Note: strong mode support is preliminary and may not work."); | 133 print("Note: strong mode support is preliminary and may not work."); |
| 136 } | 134 } |
| 137 Uri platform = c.options.platform; | 135 Uri platform = c.options.sdkSummary; |
| 138 if (platform != null) { | 136 if (platform != null) { |
| 139 _appendDillForUri(dillTarget, platform); | 137 _appendDillForUri(dillTarget, platform); |
| 140 } | 138 } |
| 141 String argument = c.options.arguments.first; | 139 Uri uri = c.options.inputs.first; |
| 142 Uri uri = Uri.base.resolve(argument); | 140 String path = uriTranslator.translate(uri)?.path ?? uri.path; |
| 143 String path = uriTranslator.translate(uri)?.path ?? argument; | |
| 144 if (path.endsWith(".dart")) { | 141 if (path.endsWith(".dart")) { |
| 145 kernelTarget.read(uri); | 142 kernelTarget.read(uri); |
| 146 } else { | 143 } else { |
| 147 deprecated_inputError(uri, -1, "Unexpected input: $uri"); | 144 deprecated_inputError(uri, -1, "Unexpected input: $uri"); |
| 148 } | 145 } |
| 149 await dillTarget.buildOutlines(); | 146 await dillTarget.buildOutlines(); |
| 150 var outline = await kernelTarget.buildOutlines(); | 147 var outline = await kernelTarget.buildOutlines(); |
| 151 if (c.options.dumpIr && output != null) { | 148 if (c.options.debugDump && output != null) { |
| 152 printProgramText(outline, libraryFilter: kernelTarget.isSourceLibrary); | 149 printProgramText(outline, libraryFilter: kernelTarget.isSourceLibrary); |
| 153 } | 150 } |
| 154 if (output != null) { | 151 if (output != null) { |
| 155 await writeProgramToFile(outline, output); | 152 await writeProgramToFile(outline, output); |
| 156 ticker.logMs("Wrote outline to ${output.toFilePath()}"); | 153 ticker.logMs("Wrote outline to ${output.toFilePath()}"); |
| 157 } | 154 } |
| 158 return kernelTarget; | 155 return kernelTarget; |
| 159 } | 156 } |
| 160 | 157 |
| 161 Future<Uri> compile() async { | 158 Future<Uri> compile() async { |
| 162 KernelTarget kernelTarget = await buildOutline(); | 159 KernelTarget kernelTarget = await buildOutline(); |
| 163 if (exitCode != 0) return null; | 160 if (exitCode != 0) return null; |
| 164 Uri uri = c.options.output; | 161 Uri uri = c.options.output; |
| 165 var program = await kernelTarget.buildProgram(verify: c.options.verify); | 162 var program = await kernelTarget.buildProgram(verify: c.options.verify); |
| 166 if (c.options.dumpIr) { | 163 if (c.options.debugDump) { |
| 167 printProgramText(program, libraryFilter: kernelTarget.isSourceLibrary); | 164 printProgramText(program, libraryFilter: kernelTarget.isSourceLibrary); |
| 168 } | 165 } |
| 169 await writeProgramToFile(program, uri); | 166 await writeProgramToFile(program, uri); |
| 170 ticker.logMs("Wrote program to ${uri.toFilePath()}"); | 167 ticker.logMs("Wrote program to ${uri.toFilePath()}"); |
| 171 return uri; | 168 return uri; |
| 172 } | 169 } |
| 173 } | 170 } |
| 174 | 171 |
| 175 Future compilePlatform(Uri patchedSdk, Uri fullOutput, | |
| 176 {Uri outlineOutput, | |
| 177 Uri packages, | |
| 178 bool verbose: false, | |
| 179 String backendTarget}) async { | |
| 180 backendTarget ??= "vm_fasta"; | |
| 181 Ticker ticker = new Ticker(isVerbose: verbose); | |
| 182 await CompilerCommandLine.withGlobalOptions("", [""], (CompilerContext c) { | |
| 183 c.options.options["--target"] = backendTarget; | |
| 184 c.options.options["--packages"] = packages; | |
| 185 if (verbose) { | |
| 186 c.options.options["--verbose"] = true; | |
| 187 } | |
| 188 c.options.validate(); | |
| 189 return compilePlatformInternal( | |
| 190 c, ticker, patchedSdk, fullOutput, outlineOutput); | |
| 191 }); | |
| 192 } | |
| 193 | |
| 194 // TODO(sigmund): reimplement this API using the directive listener intead. | 172 // TODO(sigmund): reimplement this API using the directive listener intead. |
| 195 Future<List<Uri>> getDependencies(Uri script, | 173 Future<List<Uri>> getDependencies(Uri script, |
| 196 {Uri sdk, | 174 {Uri sdk, |
| 197 Uri packages, | 175 Uri packages, |
| 198 Uri platform, | 176 Uri platform, |
| 199 bool verbose: false, | 177 bool verbose: false, |
| 200 String backendTarget}) async { | 178 Target target}) async { |
| 201 backendTarget ??= "vm_fasta"; | 179 var options = new CompilerOptions() |
| 202 Ticker ticker = new Ticker(isVerbose: verbose); | 180 ..target = target |
| 203 return await CompilerCommandLine.withGlobalOptions("", [""], | 181 ..verbose = verbose |
| 182 ..packagesFileUri = packages |
| 183 ..sdkSummary = platform |
| 184 ..sdkRoot = sdk; |
| 185 var pOptions = new ProcessedOptions(options); |
| 186 return await CompilerContext.runWithOptions(pOptions, |
| 204 (CompilerContext c) async { | 187 (CompilerContext c) async { |
| 205 c.options.options["--target"] = backendTarget; | 188 UriTranslator uriTranslator = await c.options.getUriTranslator(); |
| 206 c.options.options["--strong-mode"] = false; | 189 c.options.ticker.logMs("Read packages file"); |
| 207 c.options.options["--packages"] = packages; | |
| 208 if (verbose) { | |
| 209 c.options.options["--verbose"] = true; | |
| 210 } | |
| 211 c.options.validate(); | |
| 212 sdk ??= c.options.sdk; | |
| 213 | |
| 214 UriTranslator uriTranslator = await UriTranslatorImpl | |
| 215 .parse(c.fileSystem, sdk, packages: c.options.packages); | |
| 216 ticker.logMs("Read packages file"); | |
| 217 DillTarget dillTarget = | 190 DillTarget dillTarget = |
| 218 new DillTarget(ticker, uriTranslator, c.options.target); | 191 new DillTarget(c.options.ticker, uriTranslator, c.options.target); |
| 219 if (platform != null) _appendDillForUri(dillTarget, platform); | 192 if (platform != null) _appendDillForUri(dillTarget, platform); |
| 220 KernelTarget kernelTarget = new KernelTarget(PhysicalFileSystem.instance, | 193 KernelTarget kernelTarget = new KernelTarget(PhysicalFileSystem.instance, |
| 221 false, dillTarget, uriTranslator, c.uriToSource); | 194 false, dillTarget, uriTranslator, c.uriToSource); |
| 222 | 195 |
| 223 kernelTarget.read(script); | 196 kernelTarget.read(script); |
| 224 await dillTarget.buildOutlines(); | 197 await dillTarget.buildOutlines(); |
| 225 await kernelTarget.loader.buildOutlines(); | 198 await kernelTarget.loader.buildOutlines(); |
| 226 return await kernelTarget.loader.getDependencies(); | 199 return await kernelTarget.loader.getDependencies(); |
| 227 }); | 200 }); |
| 228 } | 201 } |
| (...skipping 12 matching lines...) Expand all Loading... |
| 241 final BytesBuilder builder = new BytesBuilder(); | 214 final BytesBuilder builder = new BytesBuilder(); |
| 242 | 215 |
| 243 void add(List<int> data) { | 216 void add(List<int> data) { |
| 244 builder.add(data); | 217 builder.add(data); |
| 245 } | 218 } |
| 246 | 219 |
| 247 void close() { | 220 void close() { |
| 248 // Nothing to do. | 221 // Nothing to do. |
| 249 } | 222 } |
| 250 } | 223 } |
| OLD | NEW |