Chromium Code Reviews| Index: pkg/front_end/lib/src/kernel_generator_impl.dart |
| diff --git a/pkg/front_end/lib/src/kernel_generator_impl.dart b/pkg/front_end/lib/src/kernel_generator_impl.dart |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..be1ea4947d7108353a22a434f60a73bc288a47b7 |
| --- /dev/null |
| +++ b/pkg/front_end/lib/src/kernel_generator_impl.dart |
| @@ -0,0 +1,149 @@ |
| +// Copyright (c) 2017, the Dart project authors. Please see the AUTHORS file |
| +// for details. All rights reserved. Use of this source code is governed by a |
| +// BSD-style license that can be found in the LICENSE file. |
| + |
| +/// Defines the front-end API for converting source code to Dart Kernel objects. |
| +library front_end.kernel_generator_impl; |
| + |
| +import 'dart:async' show Future; |
| +import 'dart:async'; |
| + |
| +import 'package:kernel/kernel.dart' show Program, CanonicalName; |
| + |
| +import 'base/processed_options.dart'; |
| +import 'fasta/dill/dill_target.dart' show DillTarget; |
| +import 'fasta/errors.dart' show InputError; |
| +import 'fasta/kernel/kernel_outline_shaker.dart'; |
| +import 'fasta/kernel/kernel_target.dart' show KernelTarget; |
| +import 'fasta/kernel/verifier.dart'; |
| +import 'fasta/kernel/utils.dart'; |
| +import 'fasta/translate_uri.dart' show TranslateUri; |
| + |
| +/// CODE REVIEW COMMENT: Should we expose this in the public API as well (it is |
| +/// used to implement all 3 APIs, but also used directly in patched_sdk) |
| + |
| +/// Implementation for the `package:front_end/kernel_generator.dart` and |
| +/// `package:front_end/summary_generator.dart` APIs. |
| +Future<CompilerResult> generateKernel(ProcessedOptions options, |
| + {bool buildSummary: false, |
| + bool buildProgram: true, |
| + bool trimDependencies: false}) async { |
|
ahe
2017/07/03 10:05:31
Need to enter a new zone here.
Siggi Cherem (dart-lang)
2017/07/05 03:39:30
I'm not sure if you saw these two changes on the l
|
| + var fs = options.fileSystem; |
| + options.ticker.logMs("Parsed arguments"); |
| + |
| + if (!await options.validateOptions()) return null; |
| + options.ticker.logMs("Validated arguments"); |
| + |
| + try { |
| + TranslateUri uriTranslator = await options.getUriTranslator(); |
| + |
| + var dillTarget = |
| + new DillTarget(options.ticker, uriTranslator, options.target); |
| + |
| + CanonicalName nameRoot = new CanonicalName.root(); |
| + Set<Uri> externalLibs(Program program) { |
| + return program.libraries |
| + .where((lib) => lib.isExternal) |
| + .map((lib) => lib.importUri) |
| + .toSet(); |
| + } |
| + |
| + var sdkSummary = await options.loadSdkSummary(nameRoot); |
| + if (sdkSummary != null) { |
| + var excluded = externalLibs(sdkSummary); |
| + dillTarget.loader |
| + .appendLibraries(sdkSummary, (uri) => !excluded.contains(uri)); |
| + } |
| + |
| + // TODO(sigmund): provide better error reporting if input summaries or |
| + // linked dependencies were listed out of order (or provide mechanism to |
| + // sort them). |
| + for (var inputSummary in await options.loadInputSummaries(nameRoot)) { |
| + var excluded = externalLibs(inputSummary); |
| + dillTarget.loader |
| + .appendLibraries(inputSummary, (uri) => !excluded.contains(uri)); |
| + } |
| + |
| + // All summaries are considered external and shouldn't include source-info. |
| + // TODO(sigmund): rather than clearing uriToSource here, it would be nice if |
| + // the serializer could filter uriToSource as well. |
| + dillTarget.loader.libraries.forEach((lib) => lib.isExternal = true); |
| + dillTarget.loader.uriToSource.clear(); |
| + |
| + // Linked dependencies are meant to be part of the program so they are not |
| + // marked external. |
| + for (var dependency in await options.loadLinkDependencies(nameRoot)) { |
| + var excluded = externalLibs(dependency); |
| + dillTarget.loader |
| + .appendLibraries(dependency, (uri) => !excluded.contains(uri)); |
| + } |
| + |
| + await dillTarget.buildOutlines(); |
| + |
| + var kernelTarget = new KernelTarget(fs, dillTarget, uriTranslator); |
| + options.inputs.forEach(kernelTarget.read); |
| + Program summaryProgram = |
| + await kernelTarget.buildOutlines(nameRoot: nameRoot); |
| + List<int> summary = null; |
| + if (buildSummary) { |
| + if (trimDependencies) { |
| + // TODO(sigmund): see if it is worth supporting this. Trimming the |
| + // program here will affect how we build the program later. To support |
| + // both we'd have to clone the program first. |
| + assert(!buildProgram); |
| + var excluded = |
| + dillTarget.loader.libraries.map((lib) => lib.importUri).toSet(); |
| + trimProgram(summaryProgram, (uri) => !excluded.contains(uri)); |
| + } |
| + if (options.verify) { |
| + verifyProgram(summaryProgram).forEach((e) => options.reportError('$e')); |
| + } |
| + if (kernelTarget.errors.isEmpty) { |
| + summary = serializeProgram(summaryProgram, excludeUriToSource: true); |
| + } |
| + options.ticker.logMs("Generated outline"); |
| + } |
| + |
| + Program program; |
| + if (buildProgram && kernelTarget.errors.isEmpty) { |
| + program = await kernelTarget.buildProgram(verify: options.verify); |
| + if (trimDependencies) { |
| + var excluded = |
| + dillTarget.loader.libraries.map((lib) => lib.importUri).toSet(); |
| + trimProgram(program, (uri) => !excluded.contains(uri)); |
| + } |
| + if (options.debugDump) printProgramText(program); |
| + options.ticker.logMs("Generated program"); |
| + } |
| + |
| + if (kernelTarget.errors.isNotEmpty) { |
| + kernelTarget.errors.forEach(options.reportError); |
| + return null; |
| + } |
| + |
| + return new CompilerResult( |
| + summary: summary, |
| + program: program, |
| + deps: kernelTarget.loader.getDependencies()); |
| + } on InputError catch (e) { |
| + options.reportError(e.format()); |
| + return null; |
| + } |
|
ahe
2017/07/03 10:05:31
Add crash reporting here.
Siggi Cherem (dart-lang)
2017/07/05 03:39:30
Ditto, I believe this is done.
|
| +} |
| + |
| +/// Result object of [generateKernel]. |
| +class CompilerResult { |
| + /// The generated summary bytes, if it was requested. |
| + final List<int> summary; |
| + |
| + /// The generated program, if it was requested. |
| + final Program program; |
| + |
| + /// Dependencies traversed by the compiler. Used only for generating |
| + /// dependency .GN files in the dart-sdk build system. |
| + /// Note this might be removed when we switch to compute depencencies without |
| + /// using the compiler itself. |
| + final List<Uri> deps; |
| + |
| + CompilerResult({this.summary, this.program, this.deps}); |
| +} |