-
Notifications
You must be signed in to change notification settings - Fork 4k
/
RuleConfiguredTarget.java
294 lines (267 loc) · 11.9 KB
/
RuleConfiguredTarget.java
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
// Copyright 2014 The Bazel Authors. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package com.google.devtools.build.lib.analysis.configuredtargets;
import com.google.common.base.Joiner;
import com.google.common.base.Preconditions;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Interner;
import com.google.devtools.build.lib.actions.ActionAnalysisMetadata;
import com.google.devtools.build.lib.actions.Artifact;
import com.google.devtools.build.lib.analysis.AnalysisUtils;
import com.google.devtools.build.lib.analysis.FileProvider;
import com.google.devtools.build.lib.analysis.FilesToRunProvider;
import com.google.devtools.build.lib.analysis.IncompatiblePlatformProvider;
import com.google.devtools.build.lib.analysis.RuleContext;
import com.google.devtools.build.lib.analysis.RunfilesProvider;
import com.google.devtools.build.lib.analysis.TransitiveInfoProvider;
import com.google.devtools.build.lib.analysis.TransitiveInfoProviderMap;
import com.google.devtools.build.lib.analysis.TransitiveInfoProviderMapBuilder;
import com.google.devtools.build.lib.analysis.Util;
import com.google.devtools.build.lib.analysis.config.ConfigMatchingProvider;
import com.google.devtools.build.lib.analysis.config.RunUnder;
import com.google.devtools.build.lib.analysis.starlark.StarlarkApiProvider;
import com.google.devtools.build.lib.cmdline.Label;
import com.google.devtools.build.lib.collect.nestedset.NestedSet;
import com.google.devtools.build.lib.concurrent.BlazeInterners;
import com.google.devtools.build.lib.concurrent.ThreadSafety.Immutable;
import com.google.devtools.build.lib.packages.Info;
import com.google.devtools.build.lib.packages.OutputFile;
import com.google.devtools.build.lib.packages.PackageSpecification.PackageGroupContents;
import com.google.devtools.build.lib.packages.Provider;
import com.google.devtools.build.lib.skyframe.BuildConfigurationKey;
import com.google.devtools.build.lib.skyframe.ConfiguredTargetKey;
import com.google.devtools.build.lib.skyframe.serialization.autocodec.AutoCodec;
import com.google.devtools.build.lib.skyframe.serialization.autocodec.AutoCodec.Instantiator;
import com.google.devtools.build.lib.skyframe.serialization.autocodec.AutoCodec.VisibleForSerialization;
import com.google.devtools.build.lib.starlarkbuildapi.ActionApi;
import java.util.function.Consumer;
import javax.annotation.Nullable;
import net.starlark.java.eval.Dict;
import net.starlark.java.eval.Printer;
import net.starlark.java.eval.Starlark;
import net.starlark.java.eval.StarlarkSemantics;
/**
* A {@link com.google.devtools.build.lib.analysis.ConfiguredTarget} that is produced by a rule.
*
* <p>Created by {@link com.google.devtools.build.lib.analysis.RuleConfiguredTargetBuilder}. There
* is an instance of this class for every analyzed rule. For more information about how analysis
* works, see {@link com.google.devtools.build.lib.analysis.RuleConfiguredTargetFactory}.
*/
@AutoCodec(checkClassExplicitlyAllowed = true)
@Immutable
public final class RuleConfiguredTarget extends AbstractConfiguredTarget {
/** A set of this target's implicitDeps. */
private final ImmutableSet<ConfiguredTargetKey> implicitDeps;
/*
* An interner for the implicitDeps set. {@link Util.findImplicitDeps} is called upon every
* construction of a RuleConfiguredTarget and we expect many of these targets to contain the same
* set of implicit deps so this reduces the memory load per build.
*/
private static final Interner<ImmutableSet<ConfiguredTargetKey>> IMPLICIT_DEPS_INTERNER =
BlazeInterners.newWeakInterner();
private final TransitiveInfoProviderMap providers;
private final ImmutableMap<Label, ConfigMatchingProvider> configConditions;
private final String ruleClassString;
private final ImmutableList<ActionAnalysisMetadata> actions;
// TODO(b/133160730): can we only populate this map for outputs that have labels?
private final ImmutableMap<Label, Artifact> artifactsByOutputLabel;
@Instantiator
@VisibleForSerialization
RuleConfiguredTarget(
Label label,
BuildConfigurationKey configurationKey,
NestedSet<PackageGroupContents> visibility,
TransitiveInfoProviderMap providers,
ImmutableMap<Label, ConfigMatchingProvider> configConditions,
ImmutableSet<ConfiguredTargetKey> implicitDeps,
String ruleClassString,
ImmutableList<ActionAnalysisMetadata> actions,
ImmutableMap<Label, Artifact> artifactsByOutputLabel) {
super(label, configurationKey, visibility);
this.artifactsByOutputLabel = artifactsByOutputLabel;
// We don't use ImmutableMap.Builder here to allow augmenting the initial list of 'default'
// providers by passing them in.
TransitiveInfoProviderMapBuilder providerBuilder =
new TransitiveInfoProviderMapBuilder().addAll(providers);
Preconditions.checkState(providerBuilder.contains(RunfilesProvider.class), label);
Preconditions.checkState(providerBuilder.contains(FileProvider.class), label);
Preconditions.checkState(providerBuilder.contains(FilesToRunProvider.class), label);
// Initialize every StarlarkApiProvider
for (int i = 0; i < providers.getProviderCount(); i++) {
Object obj = providers.getProviderInstanceAt(i);
if (obj instanceof StarlarkApiProvider) {
((StarlarkApiProvider) obj).init(this);
}
}
this.providers = providerBuilder.build();
this.configConditions = configConditions;
this.implicitDeps = IMPLICIT_DEPS_INTERNER.intern(implicitDeps);
this.ruleClassString = ruleClassString;
this.actions = actions;
}
public RuleConfiguredTarget(
RuleContext ruleContext,
TransitiveInfoProviderMap providers,
ImmutableList<ActionAnalysisMetadata> actions,
ImmutableMap<Label, Artifact> artifactsByOutputLabel) {
this(
ruleContext.getLabel(),
ruleContext.getConfigurationKey(),
ruleContext.getVisibility(),
providers,
ruleContext.getConfigConditions(),
Util.findImplicitDeps(ruleContext),
ruleContext.getRule().getRuleClass(),
actions,
artifactsByOutputLabel);
// If this rule is the run_under target, then check that we have an executable; note that
// run_under is only set in the target configuration, and the target must also be analyzed for
// the target configuration.
RunUnder runUnder = ruleContext.getConfiguration().getRunUnder();
if (runUnder != null && getLabel().equals(runUnder.getLabel())) {
if (getProvider(FilesToRunProvider.class).getExecutable() == null) {
ruleContext.ruleError("run_under target " + runUnder.getLabel() + " is not executable");
}
}
// Make sure that all declared output files are also created as artifacts. The
// CachingAnalysisEnvironment makes sure that they all have generating actions.
if (!ruleContext.hasErrors()) {
for (OutputFile out : ruleContext.getRule().getOutputFiles()) {
ruleContext.createOutputArtifact(out);
}
}
}
/** Use this constructor for creating incompatible ConfiguredTarget instances. */
public RuleConfiguredTarget(
Label label,
BuildConfigurationKey configurationKey,
NestedSet<PackageGroupContents> visibility,
TransitiveInfoProviderMap providers,
ImmutableMap<Label, ConfigMatchingProvider> configConditions,
String ruleClassString) {
this(
label,
configurationKey,
visibility,
providers,
configConditions,
ImmutableSet.<ConfiguredTargetKey>of(),
ruleClassString,
ImmutableList.<ActionAnalysisMetadata>of(),
ImmutableMap.<Label, Artifact>of());
Preconditions.checkState(providers.get(IncompatiblePlatformProvider.PROVIDER) != null, label);
}
/** The configuration conditions that trigger this rule's configurable attributes. */
@Override
public ImmutableMap<Label, ConfigMatchingProvider> getConfigConditions() {
return configConditions;
}
public ImmutableSet<ConfiguredTargetKey> getImplicitDeps() {
return implicitDeps;
}
@Override
public String getRuleClassString() {
return ruleClassString;
}
@Nullable
@Override
public <P extends TransitiveInfoProvider> P getProvider(Class<P> providerClass) {
// TODO(bazel-team): Should aspects be allowed to override providers on the configured target
// class?
AnalysisUtils.checkProvider(providerClass);
final P provider = providers.getProvider(providerClass);
if (provider != null) {
return provider;
}
if (providerClass.isAssignableFrom(getClass())) {
return providerClass.cast(this);
}
return null;
}
@Override
public String getErrorMessageForUnknownField(String name) {
return String.format(
"%s (rule '%s') doesn't have provider '%s'",
Starlark.repr(this), getRuleClassString(), name);
}
@Override
protected void addExtraStarlarkKeys(Consumer<String> result) {
for (int i = 0; i < providers.getProviderCount(); i++) {
Object classAt = providers.getProviderKeyAt(i);
if (classAt instanceof String) {
result.accept((String) classAt);
}
}
result.accept(ACTIONS_FIELD_NAME);
}
@Override
protected Info rawGetStarlarkProvider(Provider.Key providerKey) {
return providers.get(providerKey);
}
@Override
protected Object rawGetStarlarkProvider(String providerKey) {
if (providerKey.equals(ACTIONS_FIELD_NAME)) {
// Only expose actions which are legitimate Starlark values, otherwise they will later
// cause a Bazel crash.
// TODO(cparsons): Expose all actions to Starlark.
return actions.stream()
.filter(action -> action instanceof ActionApi)
.collect(ImmutableList.toImmutableList());
}
return providers.get(providerKey);
}
@Override
public void repr(Printer printer) {
printer.append("<target " + getLabel() + ">");
}
@Override
public void debugPrint(Printer printer, StarlarkSemantics semantics) {
// Show the names of the provider keys that this target propagates.
// Provider key names might potentially be *private* information, and thus a comprehensive
// list of provider keys should not be exposed in any way other than for debug information.
printer.append("<target " + getLabel() + ", keys:[");
ImmutableList.Builder<String> starlarkProviderKeyStrings = ImmutableList.builder();
for (int providerIndex = 0; providerIndex < providers.getProviderCount(); providerIndex++) {
Object providerKey = providers.getProviderKeyAt(providerIndex);
if (providerKey instanceof Provider.Key) {
starlarkProviderKeyStrings.add(providerKey.toString());
}
}
printer.append(Joiner.on(", ").join(starlarkProviderKeyStrings.build()));
printer.append("]>");
}
/** Returns a list of actions that this configured target generated. */
public ImmutableList<ActionAnalysisMetadata> getActions() {
return actions;
}
/**
* Provides an artifact by its corresponding output label, for use by output file configured
* targets.
*/
public Artifact getArtifactByOutputLabel(Label outputLabel) {
return Preconditions.checkNotNull(
artifactsByOutputLabel.get(outputLabel),
"%s %s %s %s",
outputLabel,
this,
this.artifactsByOutputLabel,
this.actions);
}
@Override
public Dict<String, Object> getProvidersDict() {
return ConfiguredTargetsUtil.getProvidersDict(this, providers);
}
}