OLD | NEW |
---|---|
(Empty) | |
1 // Copyright (c) 2017, 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 /// Defines the front-end API for converting source code to Dart Kernel objects. | |
6 library front_end.kernel_generator_impl; | |
7 | |
8 import 'dart:async' show Future; | |
9 import 'dart:async'; | |
10 | |
11 import 'package:kernel/kernel.dart' show Program, CanonicalName; | |
12 | |
13 import 'base/processed_options.dart'; | |
14 import 'fasta/dill/dill_target.dart' show DillTarget; | |
15 import 'fasta/errors.dart' show InputError; | |
16 import 'fasta/kernel/kernel_outline_shaker.dart'; | |
17 import 'fasta/kernel/kernel_target.dart' show KernelTarget; | |
18 import 'fasta/kernel/verifier.dart'; | |
19 import 'fasta/kernel/utils.dart'; | |
20 import 'fasta/translate_uri.dart' show TranslateUri; | |
21 | |
22 /// CODE REVIEW COMMENT: Should we expose this in the public API as well (it is | |
23 /// used to implement all 3 APIs, but also used directly in patched_sdk) | |
24 | |
25 /// Implementation for the `package:front_end/kernel_generator.dart` and | |
26 /// `package:front_end/summary_generator.dart` APIs. | |
27 Future<CompilerResult> generateKernel(ProcessedOptions options, | |
28 {bool buildSummary: false, | |
29 bool buildProgram: true, | |
30 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
| |
31 var fs = options.fileSystem; | |
32 options.ticker.logMs("Parsed arguments"); | |
33 | |
34 if (!await options.validateOptions()) return null; | |
35 options.ticker.logMs("Validated arguments"); | |
36 | |
37 try { | |
38 TranslateUri uriTranslator = await options.getUriTranslator(); | |
39 | |
40 var dillTarget = | |
41 new DillTarget(options.ticker, uriTranslator, options.target); | |
42 | |
43 CanonicalName nameRoot = new CanonicalName.root(); | |
44 Set<Uri> externalLibs(Program program) { | |
45 return program.libraries | |
46 .where((lib) => lib.isExternal) | |
47 .map((lib) => lib.importUri) | |
48 .toSet(); | |
49 } | |
50 | |
51 var sdkSummary = await options.loadSdkSummary(nameRoot); | |
52 if (sdkSummary != null) { | |
53 var excluded = externalLibs(sdkSummary); | |
54 dillTarget.loader | |
55 .appendLibraries(sdkSummary, (uri) => !excluded.contains(uri)); | |
56 } | |
57 | |
58 // TODO(sigmund): provide better error reporting if input summaries or | |
59 // linked dependencies were listed out of order (or provide mechanism to | |
60 // sort them). | |
61 for (var inputSummary in await options.loadInputSummaries(nameRoot)) { | |
62 var excluded = externalLibs(inputSummary); | |
63 dillTarget.loader | |
64 .appendLibraries(inputSummary, (uri) => !excluded.contains(uri)); | |
65 } | |
66 | |
67 // All summaries are considered external and shouldn't include source-info. | |
68 // TODO(sigmund): rather than clearing uriToSource here, it would be nice if | |
69 // the serializer could filter uriToSource as well. | |
70 dillTarget.loader.libraries.forEach((lib) => lib.isExternal = true); | |
71 dillTarget.loader.uriToSource.clear(); | |
72 | |
73 // Linked dependencies are meant to be part of the program so they are not | |
74 // marked external. | |
75 for (var dependency in await options.loadLinkDependencies(nameRoot)) { | |
76 var excluded = externalLibs(dependency); | |
77 dillTarget.loader | |
78 .appendLibraries(dependency, (uri) => !excluded.contains(uri)); | |
79 } | |
80 | |
81 await dillTarget.buildOutlines(); | |
82 | |
83 var kernelTarget = new KernelTarget(fs, dillTarget, uriTranslator); | |
84 options.inputs.forEach(kernelTarget.read); | |
85 Program summaryProgram = | |
86 await kernelTarget.buildOutlines(nameRoot: nameRoot); | |
87 List<int> summary = null; | |
88 if (buildSummary) { | |
89 if (trimDependencies) { | |
90 // TODO(sigmund): see if it is worth supporting this. Trimming the | |
91 // program here will affect how we build the program later. To support | |
92 // both we'd have to clone the program first. | |
93 assert(!buildProgram); | |
94 var excluded = | |
95 dillTarget.loader.libraries.map((lib) => lib.importUri).toSet(); | |
96 trimProgram(summaryProgram, (uri) => !excluded.contains(uri)); | |
97 } | |
98 if (options.verify) { | |
99 verifyProgram(summaryProgram).forEach((e) => options.reportError('$e')); | |
100 } | |
101 if (kernelTarget.errors.isEmpty) { | |
102 summary = serializeProgram(summaryProgram, excludeUriToSource: true); | |
103 } | |
104 options.ticker.logMs("Generated outline"); | |
105 } | |
106 | |
107 Program program; | |
108 if (buildProgram && kernelTarget.errors.isEmpty) { | |
109 program = await kernelTarget.buildProgram(verify: options.verify); | |
110 if (trimDependencies) { | |
111 var excluded = | |
112 dillTarget.loader.libraries.map((lib) => lib.importUri).toSet(); | |
113 trimProgram(program, (uri) => !excluded.contains(uri)); | |
114 } | |
115 if (options.debugDump) printProgramText(program); | |
116 options.ticker.logMs("Generated program"); | |
117 } | |
118 | |
119 if (kernelTarget.errors.isNotEmpty) { | |
120 kernelTarget.errors.forEach(options.reportError); | |
121 return null; | |
122 } | |
123 | |
124 return new CompilerResult( | |
125 summary: summary, | |
126 program: program, | |
127 deps: kernelTarget.loader.getDependencies()); | |
128 } on InputError catch (e) { | |
129 options.reportError(e.format()); | |
130 return null; | |
131 } | |
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.
| |
132 } | |
133 | |
134 /// Result object of [generateKernel]. | |
135 class CompilerResult { | |
136 /// The generated summary bytes, if it was requested. | |
137 final List<int> summary; | |
138 | |
139 /// The generated program, if it was requested. | |
140 final Program program; | |
141 | |
142 /// Dependencies traversed by the compiler. Used only for generating | |
143 /// dependency .GN files in the dart-sdk build system. | |
144 /// Note this might be removed when we switch to compute depencencies without | |
145 /// using the compiler itself. | |
146 final List<Uri> deps; | |
147 | |
148 CompilerResult({this.summary, this.program, this.deps}); | |
149 } | |
OLD | NEW |