forked from bazelbuild/bazel
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTargetUtils.java
455 lines (412 loc) · 17.5 KB
/
TargetUtils.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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
// 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.packages;
import static com.google.devtools.build.lib.packages.BuildType.TRISTATE;
import static com.google.devtools.build.lib.packages.Type.BOOLEAN;
import com.google.common.base.Preconditions;
import com.google.common.base.Predicate;
import com.google.common.base.Strings;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSortedMap;
import com.google.common.collect.Iterables;
import com.google.common.collect.Maps;
import com.google.devtools.build.lib.actions.ExecutionRequirements;
import com.google.devtools.build.lib.cmdline.Label;
import com.google.devtools.build.lib.util.Pair;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.annotation.Nullable;
import net.starlark.java.eval.Dict;
import net.starlark.java.eval.EvalException;
import net.starlark.java.syntax.Location;
/**
* Utility functions over Targets that don't really belong in the base {@link
* Target} interface.
*/
public final class TargetUtils {
// *_test / test_suite attribute that used to specify constraint keywords.
private static final String CONSTRAINTS_ATTR = "tags";
// We don't want to pollute the execution info with random things, and we also need to reserve
// some internal tags that we don't allow to be set on targets. We also don't want to
// exhaustively enumerate all the legal values here. Right now, only a ~small set of tags is
// recognized by Bazel.
private static boolean legalExecInfoKeys(String tag) {
return tag.startsWith("block-")
|| tag.startsWith("requires-")
|| tag.startsWith("no-")
|| tag.startsWith("supports-")
|| tag.startsWith("disable-")
|| tag.startsWith("cpu:")
|| tag.equals(ExecutionRequirements.LOCAL)
|| tag.equals(ExecutionRequirements.WORKER_KEY_MNEMONIC)
|| tag.startsWith("resources:");
}
private TargetUtils() {} // Uninstantiable.
public static boolean isTestRuleName(String name) {
return name.endsWith("_test");
}
public static boolean isTestSuiteRuleName(String name) {
return name.equals("test_suite");
}
/**
* Returns true iff {@code target} is a {@code *_test} rule; excludes {@code
* test_suite}.
*/
public static boolean isTestRule(Target target) {
return (target instanceof Rule) && isTestRuleName(((Rule) target).getRuleClass());
}
/**
* Returns true iff {@code target} is a {@code test_suite} rule.
*/
public static boolean isTestSuiteRule(Target target) {
return target instanceof Rule && isTestSuiteRuleName(((Rule) target).getRuleClass());
}
/**
* Returns true iff {@code target} is a {@code *_test} or {@code test_suite}.
*/
public static boolean isTestOrTestSuiteRule(Target target) {
return isTestRule (target) || isTestSuiteRule(target);
}
/**
* Returns true if {@code target} has "manual" in the tags attribute and thus should be ignored by
* command-line wildcards or by test_suite $implicit_tests attribute.
*/
public static boolean hasManualTag(Target target) {
return (target instanceof Rule) && hasConstraint((Rule) target, "manual");
}
/**
* Returns true if test marked as "exclusive" by the appropriate keyword
* in the tags attribute.
*
* Method assumes that passed target is a test rule, so usually it should be
* used only after isTestRule() or isTestOrTestSuiteRule(). Behavior is
* undefined otherwise.
*/
public static boolean isExclusiveTestRule(Rule rule) {
return hasConstraint(rule, "exclusive");
}
/**
* Returns true if test marked as "exclusive-if-local" by the appropriate keyword in the tags
* attribute.
*
* <p>Method assumes that passed target is a test rule, so usually it should be used only after
* isTestRule() or isTestOrTestSuiteRule(). Behavior is undefined otherwise.
*/
public static boolean isExclusiveIfLocalTestRule(Rule rule) {
return hasConstraint(rule, "exclusive-if-local");
}
/**
* Returns true if test marked as "local" by the appropriate keyword
* in the tags attribute.
*
* Method assumes that passed target is a test rule, so usually it should be
* used only after isTestRule() or isTestOrTestSuiteRule(). Behavior is
* undefined otherwise.
*/
public static boolean isLocalTestRule(Rule rule) {
return hasConstraint(rule, "local")
|| NonconfigurableAttributeMapper.of(rule).get("local", Type.BOOLEAN);
}
/**
* Returns true if test marked as "external" by the appropriate keyword
* in the tags attribute.
*
* Method assumes that passed target is a test rule, so usually it should be
* used only after isTestRule() or isTestOrTestSuiteRule(). Behavior is
* undefined otherwise.
*/
public static boolean isExternalTestRule(Rule rule) {
return hasConstraint(rule, "external");
}
/**
* Returns true if test marked as "no-testloasd" by the appropriate keyword in the tags attribute.
*
* <p>Method assumes that passed target is a test rule, so usually it should be used only after
* isTestRule() or isTestOrTestSuiteRule(). Behavior is undefined otherwise.
*/
public static boolean isNoTestloasdTestRule(Rule rule) {
return hasConstraint(rule, "no-testloasd");
}
public static List<String> getStringListAttr(Target target, String attrName) {
Preconditions.checkArgument(target instanceof Rule);
return NonconfigurableAttributeMapper.of((Rule) target).get(attrName, Types.STRING_LIST);
}
public static String getStringAttr(Target target, String attrName) {
Preconditions.checkArgument(target instanceof Rule);
return NonconfigurableAttributeMapper.of((Rule) target).get(attrName, Type.STRING);
}
public static Iterable<String> getAttrAsString(Target target, String attrName) {
Preconditions.checkArgument(target instanceof Rule);
List<String> values = new ArrayList<>(); // May hold null values.
Attribute attribute = ((Rule) target).getAttributeDefinition(attrName);
if (attribute != null) {
Type<?> attributeType = attribute.getType();
for (Object attrValue :
AggregatingAttributeMapper.of((Rule) target)
.visitAttribute(attribute.getName(), attributeType)) {
values.add(convertAttributeValue(attributeType, attrValue));
}
}
return values;
}
@Nullable
public static String convertAttributeValue(Type<?> attributeType, Object attrValue) {
// Ugly hack to maintain backward 'attr' query compatibility for BOOLEAN and TRISTATE
// attributes. These are internally stored as actual Boolean or TriState objects but were
// historically queried as integers. To maintain compatibility, we inspect their actual
// value and return the integer equivalent represented as a String. This code is the
// opposite of the code in BooleanType and TriStateType respectively.
if (attributeType == BOOLEAN) {
return Type.BOOLEAN.cast(attrValue) ? "1" : "0";
} else if (attributeType == TRISTATE) {
return switch (BuildType.TRISTATE.cast(attrValue)) {
case AUTO -> "-1";
case NO -> "0";
case YES -> "1";
};
} else {
return attrValue == null ? null : attrValue.toString();
}
}
/**
* If the given target is a rule, returns its <code>deprecation<code/> value, or null if unset.
*/
@Nullable
public static String getDeprecation(Target target) {
if (!(target instanceof Rule)) {
return null;
}
Rule rule = (Rule) target;
return rule.isAttrDefined("deprecation", Type.STRING)
? NonconfigurableAttributeMapper.of(rule).get("deprecation", Type.STRING)
: null;
}
/**
* Checks whether specified constraint keyword is present in the
* tags attribute of the test or test suite rule.
*
* Method assumes that provided rule is a test or a test suite. Behavior is
* undefined otherwise.
*/
private static boolean hasConstraint(Rule rule, String keyword) {
return NonconfigurableAttributeMapper.of(rule)
.get(CONSTRAINTS_ATTR, Types.STRING_LIST)
.contains(keyword);
}
/**
* Returns the execution info from the tags declared on the target. These include only some tags
* {@link #legalExecInfoKeys} as keys with empty values.
*/
public static Map<String, String> getExecutionInfo(Rule rule) {
// tags may contain duplicate values.
Map<String, String> map = new HashMap<>();
for (String tag :
NonconfigurableAttributeMapper.of(rule).get(CONSTRAINTS_ATTR, Types.STRING_LIST)) {
if (legalExecInfoKeys(tag)) {
map.put(tag, "");
}
}
return ImmutableMap.copyOf(map);
}
/**
* Returns the execution info from the tags declared on the target. These include only some tags
* {@link #legalExecInfoKeys} as keys with empty values.
*
* @param rule a rule instance to get tags from
* @param allowTagsPropagation if set to true, tags will be propagated from a target to the
* actions' execution requirements, for more details {@see
* BuildLanguageOptions#experimentalAllowTagsPropagation}
*/
public static ImmutableMap<String, String> getExecutionInfo(
Rule rule, boolean allowTagsPropagation) {
if (allowTagsPropagation) {
return ImmutableMap.copyOf(getExecutionInfo(rule));
} else {
return ImmutableMap.of();
}
}
/**
* Returns the execution info, obtained from the rule's tags and the execution requirements
* provided. Only supported tags are included into the execution info, see {@link
* #legalExecInfoKeys}.
*
* @param executionRequirementsUnchecked execution_requirements of a rule, expected to be of a
* {@code Dict<String, String>} type, null or Starlark None.
* @param rule a rule instance to get tags from
* @param allowTagsPropagation if set to true, tags will be propagated from a target to the
* actions' execution requirements, for more details {@see
* StarlarkSematicOptions#experimentalAllowTagsPropagation}
*/
public static ImmutableSortedMap<String, String> getFilteredExecutionInfo(
@Nullable Object executionRequirementsUnchecked, Rule rule, boolean allowTagsPropagation)
throws EvalException {
Map<String, String> executionInfo =
executionRequirementsUnchecked == null
? ImmutableMap.of()
: TargetUtils.filter(
Dict.noneableCast(
executionRequirementsUnchecked,
String.class,
String.class,
"execution_requirements"));
if (allowTagsPropagation) {
executionInfo = new HashMap<>(executionInfo); // Make mutable.
Map<String, String> checkedTags = getExecutionInfo(rule);
// merging filtered tags to the execution info map avoiding duplicates
checkedTags.forEach(executionInfo::putIfAbsent);
}
return ImmutableSortedMap.copyOf(executionInfo);
}
/**
* Returns the execution info. These include execution requirement tags ('block-*', 'requires-*',
* 'no-*', 'supports-*', 'disable-*', 'local', and 'cpu:*') as keys with empty values.
*/
private static Map<String, String> filter(Map<String, String> executionInfo) {
return Maps.filterKeys(executionInfo, TargetUtils::legalExecInfoKeys);
}
/**
* Returns the language part of the rule name (e.g. "foo" for foo_test or foo_binary).
*
* <p>In practice this is the part before the "_", if any, otherwise the entire rule class name.
*
* <p>Precondition: isTestRule(target) || isRunnableNonTestRule(target).
*/
public static String getRuleLanguage(Target target) {
return getRuleLanguage(((Rule) target).getRuleClass());
}
/**
* Returns the language part of the rule name (e.g. "foo" for foo_test or foo_binary).
*
* <p>In practice this is the part before the "_", if any, otherwise the entire rule class name.
*/
public static String getRuleLanguage(String ruleClass) {
int index = ruleClass.lastIndexOf('_');
// Chop off "_binary" or "_test".
return index != -1 ? ruleClass.substring(0, index) : ruleClass;
}
private static boolean isExplicitDependency(Rule rule, Label label) {
if (Iterables.contains(rule.getVisibilityDependencyLabels(), label)) {
return true;
}
AggregatingAttributeMapper mapper = AggregatingAttributeMapper.of(rule);
try {
mapper.visitLabels(
DependencyFilter.NO_IMPLICIT_DEPS,
(Label depLabel, Attribute attribute) -> {
if (label.equals(depLabel)) {
throw StopIteration.INSTANCE;
}
});
} catch (StopIteration e) {
return true;
}
return false;
}
private static final class StopIteration extends RuntimeException {
private static final StopIteration INSTANCE = new StopIteration();
}
/**
* Returns a predicate to be used for test tag filtering, i.e., that only accepts tests that match
* all of the required tags and none of the excluded tags.
*/
public static Predicate<Target> tagFilter(List<String> tagFilterList) {
Pair<Collection<String>, Collection<String>> tagLists =
TestTargetUtils.sortTagsBySense(tagFilterList);
final Collection<String> requiredTags = tagLists.first;
final Collection<String> excludedTags = tagLists.second;
return input -> {
if (requiredTags.isEmpty() && excludedTags.isEmpty()) {
return true;
}
if (!(input instanceof Rule)) {
return requiredTags.isEmpty();
}
// Note that test_tags are those originating from the XX_test rule, whereas the requiredTags
// and excludedTags originate from the command line or test_suite rule.
// TODO(ulfjack): getRuleTags is inconsistent with TestFunction and other places that use
// tags + size, but consistent with TestSuite.
return TestTargetUtils.testMatchesFilters(
((Rule) input).getRuleTags(), requiredTags, excludedTags, false);
};
}
/**
* Returns a predicate to be used for test rule name filtering, i.e., that only accepts tests that match
* a required rule name and not an excluded rule name.
*/
public static Predicate<Target> ruleFilter(List<String> ruleFilterList) {
Pair<Collection<String>, Collection<String>> ruleLists =
TestTargetUtils.sortTagsBySense(ruleFilterList);
final Collection<String> requiredRules = ruleLists.first;
final Collection<String> excludedRules = ruleLists.second;
return input -> {
if (requiredRules.isEmpty() && excludedRules.isEmpty()) {
return true;
}
if (!(input instanceof Rule)) {
return requiredRules.isEmpty();
}
return TestTargetUtils.testMatchesRuleFilters(
((Rule) input).getRuleClass(), requiredRules, excludedRules);
};
}
/** Return {@link Location} for {@link Target} target, if it should not be null. */
@Nullable
public static Location getLocationMaybe(Target target) {
return (target instanceof Rule) || (target instanceof InputFile) ? target.getLocation() : null;
}
/**
* Return nicely formatted error message that {@link Label} label that was pointed to by {@link
* Target} target did not exist, due to {@link NoSuchThingException} e.
*/
public static String formatMissingEdge(
@Nullable Target target, Label label, NoSuchThingException e, @Nullable Attribute attr) {
// instanceof returns false if target is null (which is exploited here)
if (target instanceof Rule rule) {
if (isExplicitDependency(rule, label)) {
return String.format("%s and referenced by '%s'", e.getMessage(), target.getLabel());
} else {
String additionalInfo = "";
if (attr != null && !Strings.isNullOrEmpty(attr.getDoc())) {
additionalInfo =
String.format(
"\nDocumentation for implicit attribute %s of rules of type %s:\n%s",
attr.getPublicName(), rule.getRuleClass(), attr.getDoc());
}
// N.B. If you see this error message in one of our integration tests during development of
// a change that adds a new implicit dependency when running Blaze, maybe you forgot to add
// a new mock target to the integration test's setup.
return String.format(
"every rule of type %s implicitly depends upon the target '%s', but "
+ "this target could not be found because of: %s%s",
rule.getRuleClass(), label, e.getMessage(), additionalInfo);
}
} else if (target instanceof InputFile) {
return e.getMessage() + " (this is usually caused by a missing package group in the"
+ " package-level visibility declaration)";
} else {
if (target != null) {
return String.format("in target '%s', no such label '%s': %s", target.getLabel(), label,
e.getMessage());
}
return e.getMessage();
}
}
public static String formatMissingEdge(
@Nullable Target target, Label label, NoSuchThingException e) {
return formatMissingEdge(target, label, e, null);
}
}