/
bazel_worker.dart
189 lines (155 loc) · 6.62 KB
/
bazel_worker.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
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.
/// API needed by `utils/front_end/summary_worker.dart`, a tool used to compute
/// summaries in build systems like bazel, pub-build, and package-build.
import 'dart:async' show Future;
import 'package:_fe_analyzer_shared/src/messages/diagnostic_message.dart'
show DiagnosticMessageHandler;
import 'package:front_end/src/api_prototype/compiler_options.dart';
import 'package:kernel/kernel.dart' show Component, Library;
import 'package:kernel/target/targets.dart' show Target;
import '../api_prototype/compiler_options.dart'
show CompilerOptions, parseExperimentalFlags;
import '../api_prototype/experimental_flags.dart' show ExperimentalFlag;
import '../api_prototype/file_system.dart' show FileSystem;
import '../api_prototype/front_end.dart' show CompilerResult;
import '../base/nnbd_mode.dart' show NnbdMode;
import '../base/processed_options.dart' show ProcessedOptions;
import '../kernel_generator_impl.dart' show generateKernel;
import 'compiler_state.dart' show InitializedCompilerState;
import 'modular_incremental_compilation.dart' as modular
show initializeIncrementalCompiler;
export 'package:_fe_analyzer_shared/src/messages/diagnostic_message.dart'
show DiagnosticMessage;
export 'package:_fe_analyzer_shared/src/messages/severity.dart' show Severity;
export '../api_prototype/compiler_options.dart'
show parseExperimentalFlags, parseExperimentalArguments;
export '../api_prototype/experimental_flags.dart'
show ExperimentalFlag, parseExperimentalFlag;
export '../api_prototype/standard_file_system.dart' show StandardFileSystem;
export '../api_prototype/terminal_color_support.dart'
show printDiagnosticMessage;
export '../base/nnbd_mode.dart' show NnbdMode;
export '../fasta/kernel/utils.dart' show serializeComponent;
export 'compiler_state.dart' show InitializedCompilerState;
/// Initializes the compiler for a modular build.
///
/// Re-uses cached components from [oldState.workerInputCache], and reloads them
/// as necessary based on [workerInputDigests].
Future<InitializedCompilerState> initializeIncrementalCompiler(
InitializedCompilerState oldState,
Set<String> tags,
Uri sdkSummary,
Uri packagesFile,
Uri librariesSpecificationUri,
List<Uri> additionalDills,
Map<Uri, List<int>> workerInputDigests,
Target target,
FileSystem fileSystem,
Iterable<String> experiments,
bool outlineOnly,
Map<String, String> environmentDefines,
{bool trackNeededDillLibraries: false,
bool verbose: false,
NnbdMode nnbdMode: NnbdMode.Weak}) async {
List<Component> outputLoadedAdditionalDills =
new List<Component>(additionalDills.length);
Map<ExperimentalFlag, bool> experimentalFlags = parseExperimentalFlags(
parseExperimentalArguments(experiments),
onError: (e) => throw e);
return modular.initializeIncrementalCompiler(
oldState,
tags,
outputLoadedAdditionalDills,
sdkSummary,
packagesFile,
librariesSpecificationUri,
additionalDills,
workerInputDigests,
target,
fileSystem: fileSystem,
experimentalFlags: experimentalFlags,
outlineOnly: outlineOnly,
omitPlatform: true,
trackNeededDillLibraries: trackNeededDillLibraries,
environmentDefines: environmentDefines,
verbose: verbose,
nnbdMode: nnbdMode);
}
Future<InitializedCompilerState> initializeCompiler(
InitializedCompilerState oldState,
Uri sdkSummary,
Uri librariesSpecificationUri,
Uri packagesFile,
List<Uri> additionalDills,
Target target,
FileSystem fileSystem,
Iterable<String> experiments,
Map<String, String> environmentDefines, {
bool verbose: false,
NnbdMode nnbdMode: NnbdMode.Weak,
}) async {
// TODO(sigmund): use incremental compiler when it supports our use case.
// Note: it is common for the summary worker to invoke the compiler with the
// same input summary URIs, but with different contents, so we'd need to be
// able to track shas or modification time-stamps to be able to invalidate the
// old state appropriately.
CompilerOptions options = new CompilerOptions()
..sdkSummary = sdkSummary
..packagesFileUri = packagesFile
..librariesSpecificationUri = librariesSpecificationUri
..additionalDills = additionalDills
..target = target
..fileSystem = fileSystem
..environmentDefines = environmentDefines
..experimentalFlags = parseExperimentalFlags(
parseExperimentalArguments(experiments),
onError: (e) => throw e)
..verbose = verbose
..nnbdMode = nnbdMode;
ProcessedOptions processedOpts = new ProcessedOptions(options: options);
return new InitializedCompilerState(options, processedOpts);
}
Future<CompilerResult> _compile(InitializedCompilerState compilerState,
List<Uri> inputs, DiagnosticMessageHandler diagnosticMessageHandler,
{bool summaryOnly, bool includeOffsets: true}) {
summaryOnly ??= true;
CompilerOptions options = compilerState.options;
options..onDiagnostic = diagnosticMessageHandler;
ProcessedOptions processedOpts = compilerState.processedOpts;
processedOpts.inputs.clear();
processedOpts.inputs.addAll(inputs);
return generateKernel(processedOpts,
buildSummary: summaryOnly,
buildComponent: !summaryOnly,
includeOffsets: includeOffsets);
}
Future<List<int>> compileSummary(InitializedCompilerState compilerState,
List<Uri> inputs, DiagnosticMessageHandler diagnosticMessageHandler,
{bool includeOffsets: false}) async {
CompilerResult result = await _compile(
compilerState, inputs, diagnosticMessageHandler,
summaryOnly: true, includeOffsets: includeOffsets);
return result?.summary;
}
Future<Component> compileComponent(InitializedCompilerState compilerState,
List<Uri> inputs, DiagnosticMessageHandler diagnosticMessageHandler) async {
CompilerResult result = await _compile(
compilerState, inputs, diagnosticMessageHandler,
summaryOnly: false);
Component component = result?.component;
if (component != null) {
for (Library lib in component.libraries) {
if (!inputs.contains(lib.importUri)) {
// Excluding the library also means that their canonical names will not
// be computed as part of serialization, so we need to do that
// preemptively here to avoid errors when serializing references to
// elements of these libraries.
component.root.getChildFromUri(lib.importUri).bindTo(lib.reference);
lib.computeCanonicalNames();
}
}
}
return component;
}