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..6a06891ec6a868c37ad1d33815c8e49d1ad14086 |
| --- /dev/null |
| +++ b/pkg/front_end/lib/src/kernel_generator_impl.dart |
| @@ -0,0 +1,189 @@ |
| +// 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 '../compiler_options.dart'; |
| +import '../file_system.dart'; |
| +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; |
| +import 'simple_error.dart'; |
| +import 'fasta/errors.dart'; |
| + |
| +/// 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( |
| + Iterable<Uri> sources, CompilerOptions options, |
| + {bool buildSummary: false, |
| + bool buildProgram: true, |
| + bool trimDependencies: false}) async { |
| + report(String msg) { |
| + options.onError(new SimpleError(msg)); |
|
ahe
2017/07/05 13:29:40
What is a SimpleError?
Siggi Cherem (dart-lang)
2017/07/05 18:42:10
An error object that needs a better name? ;-)
The
|
| + return null; |
| + } |
| + |
| + var fs = options.fileSystem; |
| + |
| + for (var source in sources) { |
| + if (source.scheme == 'file' && !await fs.entityForUri(source).exists()) { |
| + return report("Entry-point file not found: $source"); |
| + } |
| + } |
| + |
| + if (!options.chaseDependencies) { |
| + fs = new HermeticFileSystem(sources.toSet(), fs); |
|
Paul Berry
2017/06/29 18:51:07
It would be cool if we moved this logic into Proce
Siggi Cherem (dart-lang)
2017/06/30 04:12:02
Done.
I initially had discarded this idea because
Paul Berry
2017/07/04 15:12:14
I like it! Thanks :)
|
| + } |
| + |
| + var pOptions = new ProcessedOptions(options); |
| + pOptions.ticker.logMs("Parsed arguments"); |
|
ahe
2017/07/05 13:29:40
This will always report that parsing arguments too
Siggi Cherem (dart-lang)
2017/07/05 18:42:11
Done - now ticker is created eagerly when allocati
|
| + |
| + if (!await pOptions.validateOptions()) return null; |
| + pOptions.ticker.logMs("Validated arguments"); |
| + |
| + try { |
| + TranslateUri uriTranslator = await pOptions.getUriTranslator(); |
| + |
| + var dillTarget = |
| + new DillTarget(pOptions.ticker, uriTranslator, pOptions.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 pOptions.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 pOptions.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(); |
|
ahe
2017/07/05 13:29:40
Why do we clear this? Have you made sure that stac
Siggi Cherem (dart-lang)
2017/07/05 18:42:10
Deleted this, but for a different reason :)
The b
|
| + |
| + // Linked dependencies are meant to be part of the program so they are not |
| + // marked external. |
| + for (var dependency in await pOptions.loadLinkDependencies(nameRoot)) { |
| + var excluded = externalLibs(dependency); |
| + dillTarget.loader |
| + .appendLibraries(dependency, (uri) => !excluded.contains(uri)); |
| + } |
| + |
| + await dillTarget.buildOutlines(); |
| + |
| + var kernelTarget = new KernelTarget(fs, dillTarget, uriTranslator); |
| + sources.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. |
|
ahe
2017/07/05 13:29:40
To support both what?
Siggi Cherem (dart-lang)
2017/07/05 18:42:10
buildSummary & buildProgram at once + trimming dep
|
| + assert(!buildProgram); |
| + var excluded = |
| + dillTarget.loader.libraries.map((lib) => lib.importUri).toSet(); |
| + trimProgram(summaryProgram, (uri) => !excluded.contains(uri)); |
| + } |
| + if (options.verify) verifyProgram(summaryProgram); |
| + if (kernelTarget.errors.isEmpty) { |
| + summary = serializeProgram(summaryProgram, excludeUriToSource: true); |
| + } |
| + pOptions.ticker.logMs("Generated outline"); |
| + } |
| + |
| + Program program; |
| + if (buildProgram && kernelTarget.errors.isEmpty) { |
| + program = await kernelTarget.buildProgram(verify: pOptions.verify); |
| + if (trimDependencies) { |
| + var excluded = |
| + dillTarget.loader.libraries.map((lib) => lib.importUri).toSet(); |
| + trimProgram(program, (uri) => !excluded.contains(uri)); |
| + } |
| + if (pOptions.debugDump) printProgramText(program); |
|
ahe
2017/07/05 13:29:40
libraryFilter option missing here.
Siggi Cherem (dart-lang)
2017/07/05 18:42:10
Done.
|
| + pOptions.ticker.logMs("Generated program"); |
| + } |
| + |
| + if (kernelTarget.errors.isNotEmpty) { |
| + kernelTarget.errors.forEach(report); |
| + return null; |
| + } |
| + |
| + return new CompilerResult( |
| + summary: summary, |
| + program: program, |
| + deps: kernelTarget.loader.getDependencies()); |
| + } on InputError catch (e) { |
| + report(e.format()); |
| + return null; |
| + } |
| +} |
| + |
| +/// 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}); |
| +} |
| + |
| +/// A [FileSystem] that only allows access to files that have been explicitly |
| +/// whitelisted. |
| +class HermeticFileSystem implements FileSystem { |
| + final Set<Uri> includedFiles; |
| + final FileSystem _realFileSystem; |
| + |
| + HermeticFileSystem(this.includedFiles, this._realFileSystem); |
| + |
| + FileSystemEntity entityForUri(Uri uri) { |
| + if (includedFiles.contains(uri)) return _realFileSystem.entityForUri(uri); |
| + return inputError( |
|
ahe
2017/07/05 13:29:40
Throw FileSystemException instead, then Fasta can
Siggi Cherem (dart-lang)
2017/07/05 18:42:11
Done. I like it.
|
| + null, |
|
ahe
2017/07/05 13:29:40
uri
Siggi Cherem (dart-lang)
2017/07/05 18:42:11
n/a now that I use FileSystemException.
However,
|
| + -1, |
| + 'Invalid access to $uri: ' |
| + 'the file is accessed in a modular hermetic build ' |
| + '(where chaseDependencies is false), but it was not ' |
| + 'explicitly listed as an input.'); |
| + } |
| +} |