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 /// Defines the front-end API for converting source code to Dart Kernel objects. | 5 /// Defines the front-end API for converting source code to Dart Kernel objects. |
6 library front_end.kernel_generator; | 6 library front_end.kernel_generator; |
7 | 7 |
8 import 'compiler_options.dart'; | 8 import 'compiler_options.dart'; |
9 import 'dart:async' show Future; | 9 import 'dart:async' show Future; |
10 import 'dart:async'; | 10 import 'dart:async'; |
11 import 'package:front_end/src/base/processed_options.dart'; | |
12 import 'src/fasta/dill/dill_target.dart' show DillTarget; | |
13 import 'src/fasta/errors.dart' show InputError; | |
14 import 'src/fasta/kernel/kernel_target.dart' show KernelTarget; | |
15 import 'package:kernel/kernel.dart' show Program; | 11 import 'package:kernel/kernel.dart' show Program; |
16 import 'package:kernel/target/targets.dart' show TargetFlags; | |
17 import 'package:kernel/target/vm_fasta.dart' show VmFastaTarget; | |
18 import 'src/fasta/ticker.dart' show Ticker; | |
19 import 'src/fasta/translate_uri.dart' show TranslateUri; | |
20 import 'src/simple_error.dart'; | 12 import 'src/simple_error.dart'; |
13 import 'src/kernel_generator_impl.dart'; | |
21 | 14 |
22 /// Generates a kernel representation of the program whose main library is in | 15 /// Generates a kernel representation of the program whose main library is in |
23 /// the given [source]. | 16 /// the given [source]. |
24 /// | 17 /// |
25 /// Intended for whole program (non-modular) compilation. | 18 /// Intended for whole program (non-modular) compilation. |
26 /// | 19 /// |
27 /// Given the Uri of a file containing a program's `main` method, this function | 20 /// Given the Uri of a file containing a program's `main` method, this function |
28 /// follows `import`, `export`, and `part` declarations to discover the whole | 21 /// follows `import`, `export`, and `part` declarations to discover the whole |
29 /// program, and converts the result to Dart Kernel format. | 22 /// program, and converts the result to Dart Kernel format. |
30 /// | 23 /// |
31 /// If `compileSdk` in [options] is true, the generated program will include | 24 /// If `compileSdk` in [options] is true, the generated program will include |
32 /// code for the SDK. | 25 /// code for the SDK. |
33 /// | 26 /// |
34 /// If summaries are provided in [options], they will be used to speed up | 27 /// If summaries are provided in [options], the compiler will use them instead |
35 /// the process. If in addition `compileSdk` is false, then the resulting | 28 /// of compiling the libraries contained in those summaries. This is useful, for |
36 /// program will not contain the sdk contents. This is useful when building apps | 29 /// example, when compiling for platforms that already embed those sources (like |
37 /// for platforms that already embed the sdk (e.g. the VM), so there is no need | 30 /// the sdk in the standalone VM). |
38 /// to spend time and space rebuilding it. | 31 /// |
32 /// The input [source] is expected to be a script with a main method, otherwise | |
33 /// an error is reported. | |
34 // TODO(sigmund): rename to kernelForScript? | |
39 Future<Program> kernelForProgram(Uri source, CompilerOptions options) async { | 35 Future<Program> kernelForProgram(Uri source, CompilerOptions options) async { |
40 var fs = options.fileSystem; | 36 options = new CompilerOptions.from(options)..chaseDependencies = true; |
Paul Berry
2017/06/29 18:51:07
This concerns me a lot. If someone calls this API
Siggi Cherem (dart-lang)
2017/06/30 04:12:02
Done. I like your suggestion, it required that I a
Paul Berry
2017/07/04 15:12:14
I don't have strong feelings about the name--if "h
Siggi Cherem (dart-lang)
2017/07/05 18:42:07
Sounds good - I'll track this separately. Your com
| |
41 report(String msg) { | 37 var program = (await generateKernel([source], options))?.program; |
Paul Berry
2017/06/29 18:51:07
I would feel a lot safer if we could create a Proc
Siggi Cherem (dart-lang)
2017/06/30 04:12:02
Done.
| |
42 options.onError(new SimpleError(msg)); | 38 if (program == null) return null; |
39 | |
40 if (program.mainMethod == null) { | |
41 options.onError(new SimpleError("No 'main' method found.")); | |
43 return null; | 42 return null; |
44 } | 43 } |
45 | 44 |
46 if (!await fs.entityForUri(source).exists()) { | 45 return program; |
47 return report("Entry-point file not found: $source"); | |
48 } | |
49 | |
50 var pOptions = new ProcessedOptions(options); | |
51 | |
52 if (!await pOptions.validateOptions()) return null; | |
53 | |
54 try { | |
55 TranslateUri uriTranslator = await pOptions.getUriTranslator(); | |
56 | |
57 var dillTarget = new DillTarget(new Ticker(isVerbose: false), uriTranslator, | |
58 new VmFastaTarget(new TargetFlags(strongMode: options.strongMode))); | |
59 var summary = await pOptions.sdkSummaryProgram; | |
60 if (summary != null) { | |
61 dillTarget.loader.appendLibraries(summary); | |
62 } | |
63 | |
64 var kernelTarget = | |
65 new KernelTarget(options.fileSystem, dillTarget, uriTranslator); | |
66 kernelTarget.read(source); | |
67 | |
68 await dillTarget.buildOutlines(); | |
69 await kernelTarget.buildOutlines(); | |
70 Program program = await kernelTarget.buildProgram(trimDependencies: true); | |
71 | |
72 if (kernelTarget.errors.isNotEmpty) { | |
73 kernelTarget.errors.forEach(report); | |
74 return null; | |
75 } | |
76 | |
77 if (program.mainMethod == null) { | |
78 return report("No 'main' method found."); | |
79 } | |
80 | |
81 if (!options.compileSdk) { | |
82 // TODO(sigmund): ensure that the result is not including | |
83 // sources for the sdk, only external references. | |
84 } | |
85 return program; | |
86 } on InputError catch (e) { | |
87 options.onError(new SimpleError(e.format())); | |
88 return null; | |
89 } | |
90 } | 46 } |
91 | 47 |
92 /// Generates a kernel representation for a build unit. | 48 /// Generates a kernel representation for a build unit. |
93 /// | 49 /// |
94 /// Intended for modular compilation. | 50 /// Intended for modular compilation. |
95 /// | 51 /// |
96 /// The build unit by default contains only the source files in [sources] | 52 /// The build unit by default contains only the source files in [sources] |
ahe
2017/07/05 13:29:39
The term "build unit" isn't clearly defined.
Siggi Cherem (dart-lang)
2017/07/05 18:42:07
Done.
| |
97 /// (including library and part files), but if | 53 /// (including library and part files), but if |
98 /// [CompilerOptions.chaseDependencies] is true, it may include some additional | 54 /// [CompilerOptions.chaseDependencies] is true, it may include some additional |
ahe
2017/07/05 13:29:39
Only document the behavior of CompilerOptions.chas
Siggi Cherem (dart-lang)
2017/07/05 18:42:07
Done.
| |
99 /// source files. All of the library files are transformed into Dart Kernel | 55 /// source files. All of the library files are transformed into Dart Kernel |
100 /// Library objects. | 56 /// Library objects. |
101 /// | 57 /// |
102 /// By default, the compilation process is hermetic, meaning that the only files | 58 /// By default, the compilation process is hermetic, meaning that the only files |
103 /// which will be read are those listed in [sources], | 59 /// which will be read are those listed in [sources], |
104 /// [CompilerOptions.inputSummaries], and [CompilerOptions.sdkSummary]. If a | 60 /// [CompilerOptions.inputSummaries], and [CompilerOptions.sdkSummary]. If a |
105 /// source file attempts to refer to a file which is not obtainable from these | 61 /// source file attempts to refer to a file which is not obtainable from these |
106 /// URIs, that will result in an error, even if the file exists on the | 62 /// URIs, that will result in an error, even if the file exists on the |
107 /// filesystem. | 63 /// filesystem. |
108 /// | 64 /// |
109 /// When [CompilerOptions.chaseDependencies] is true, this default behavior | 65 /// When [CompilerOptions.chaseDependencies] is true, this default behavior |
110 /// changes, and any dependency of [sources] that is not listed in | 66 /// changes, and any dependency of [sources] that is not listed in |
111 /// [CompilerOptions.inputSummaries] and [CompilerOptions.sdkSummary] is treated | 67 /// [CompilerOptions.inputSummaries] and [CompilerOptions.sdkSummary] is treated |
112 /// as an additional source file for the build unit. | 68 /// as an additional source file for the build unit. |
113 /// | 69 /// |
114 /// Any `part` declarations found in [sources] must refer to part files which | 70 /// Any `part` declarations found in [sources] must refer to part files which |
115 /// are also listed in the build unit sources, otherwise an error results. (It | 71 /// are also listed in the build unit sources, otherwise an error results. (It |
116 /// is not permitted to refer to a part file declared in another build unit). | 72 /// is not permitted to refer to a part file declared in another build unit). |
117 /// | 73 /// |
118 /// The return value is a [Program] object with no main method set. | 74 /// The return value is a [Program] object with no main method set. |
119 /// TODO(paulberry): would it be better to define a data type in kernel to | |
120 /// represent a bundle of all the libraries in a given build unit? | |
121 /// | 75 /// |
122 /// TODO(paulberry): does additional information need to be output to allow the | 76 /// TODO(paulberry): does additional information need to be output to allow the |
123 /// caller to match up referenced elements to the summary files they were | 77 /// caller to match up referenced elements to the summary files they were |
124 /// obtained from? | 78 /// obtained from? |
125 Future<Program> kernelForBuildUnit( | 79 Future<Program> kernelForBuildUnit( |
126 List<Uri> sources, CompilerOptions options) async { | 80 List<Uri> sources, CompilerOptions options) async { |
127 var fs = options.fileSystem; | 81 return (await generateKernel(sources, options))?.program; |
Paul Berry
2017/06/29 18:51:07
Similar issue here.
Siggi Cherem (dart-lang)
2017/06/30 04:12:02
Done.
| |
128 report(String msg) { | |
129 options.onError(new SimpleError(msg)); | |
130 return null; | |
131 } | |
132 | |
133 if (!options.chaseDependencies) { | |
134 // TODO(sigmund): add support, most likely we can do so by adding a wrapper | |
135 // on top of filesystem that restricts reads to a set of known files. | |
136 report("hermetic mode (chaseDependencies = false) is not implemented"); | |
137 return null; | |
138 } | |
139 | |
140 for (var source in sources) { | |
141 if (!await fs.entityForUri(source).exists()) { | |
142 return report("Entry-point file not found: $source"); | |
143 } | |
144 } | |
145 | |
146 var pOptions = new ProcessedOptions(options); | |
147 | |
148 if (!await pOptions.validateOptions()) return null; | |
149 | |
150 try { | |
151 TranslateUri uriTranslator = await pOptions.getUriTranslator(); | |
152 | |
153 var dillTarget = new DillTarget(new Ticker(isVerbose: false), uriTranslator, | |
154 new VmFastaTarget(new TargetFlags(strongMode: options.strongMode))); | |
155 var summary = await pOptions.sdkSummaryProgram; | |
156 if (summary != null) { | |
157 dillTarget.loader.appendLibraries(summary); | |
158 } | |
159 | |
160 // TODO(sigmund): this is likely not going to work if done naively: if | |
161 // summaries contain external references we need to ensure they are loaded | |
162 // in a specific order. | |
163 for (var inputSummary in await pOptions.inputSummariesPrograms) { | |
164 dillTarget.loader.appendLibraries(inputSummary); | |
165 } | |
166 | |
167 await dillTarget.buildOutlines(); | |
168 | |
169 var kernelTarget = | |
170 new KernelTarget(options.fileSystem, dillTarget, uriTranslator); | |
171 sources.forEach(kernelTarget.read); | |
172 await kernelTarget.buildOutlines(); | |
173 | |
174 Program program = await kernelTarget.buildProgram(trimDependencies: true); | |
175 | |
176 if (kernelTarget.errors.isNotEmpty) { | |
177 kernelTarget.errors.forEach(report); | |
178 return null; | |
179 } | |
180 | |
181 return program; | |
182 } on InputError catch (e) { | |
183 options.onError(new SimpleError(e.format())); | |
184 return null; | |
185 } | |
186 } | 82 } |
OLD | NEW |