forked from bazelbuild/bazel
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathRuleClass.java
More file actions
2824 lines (2540 loc) · 114 KB
/
Copy pathRuleClass.java
File metadata and controls
2824 lines (2540 loc) · 114 KB
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
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// 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.Attribute.attr;
import static com.google.devtools.build.lib.packages.BuildType.LABEL_LIST;
import static com.google.devtools.build.lib.packages.Type.BOOLEAN;
import static com.google.devtools.build.lib.packages.Type.STRING_NO_INTERN;
import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Function;
import com.google.common.base.Functions;
import com.google.common.base.Joiner;
import com.google.common.base.Preconditions;
import com.google.common.base.Predicate;
import com.google.common.base.Predicates;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.ImmutableTable;
import com.google.common.collect.Interner;
import com.google.common.collect.Iterables;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.google.devtools.build.lib.analysis.config.Fragment;
import com.google.devtools.build.lib.analysis.config.ToolchainTypeRequirement;
import com.google.devtools.build.lib.analysis.config.transitions.NoTransition;
import com.google.devtools.build.lib.analysis.config.transitions.TransitionFactory;
import com.google.devtools.build.lib.analysis.config.transitions.TransitionFactory.TransitionType;
import com.google.devtools.build.lib.cmdline.Label;
import com.google.devtools.build.lib.cmdline.LabelSyntaxException;
import com.google.devtools.build.lib.cmdline.RepositoryName;
import com.google.devtools.build.lib.events.EventHandler;
import com.google.devtools.build.lib.packages.Attribute.ComputedDefault;
import com.google.devtools.build.lib.packages.Attribute.StarlarkComputedDefaultTemplate;
import com.google.devtools.build.lib.packages.Attribute.StarlarkComputedDefaultTemplate.CannotPrecomputeDefaultsException;
import com.google.devtools.build.lib.packages.BuildType.SelectorList;
import com.google.devtools.build.lib.packages.ConfigurationFragmentPolicy.MissingFragmentPolicy;
import com.google.devtools.build.lib.packages.ImplicitOutputsFunction.SafeImplicitOutputsFunction;
import com.google.devtools.build.lib.packages.RuleClass.Builder.RuleClassType;
import com.google.devtools.build.lib.packages.RuleFactory.AttributeValues;
import com.google.devtools.build.lib.packages.Type.ConversionException;
import com.google.devtools.build.lib.packages.Type.LabelClass;
import com.google.devtools.build.lib.skyframe.serialization.VisibleForSerialization;
import com.google.devtools.build.lib.skyframe.serialization.autocodec.AutoCodec;
import com.google.devtools.build.lib.skyframe.serialization.autocodec.SerializationConstant;
import com.google.devtools.build.lib.starlarkbuildapi.StarlarkSubruleApi;
import com.google.devtools.build.lib.util.HashCodes;
import com.google.devtools.build.lib.util.StringUtil;
import com.google.devtools.build.lib.vfs.PathFragment;
import com.google.errorprone.annotations.CanIgnoreReturnValue;
import com.google.errorprone.annotations.FormatMethod;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.BitSet;
import java.util.Collection;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.regex.Pattern;
import javax.annotation.Nullable;
import javax.annotation.concurrent.Immutable;
import net.starlark.java.eval.EvalException;
import net.starlark.java.eval.Starlark;
import net.starlark.java.eval.StarlarkCallable;
import net.starlark.java.eval.StarlarkFunction;
import net.starlark.java.eval.StarlarkThread;
import net.starlark.java.spelling.SpellChecker;
/**
* Instances of RuleClass encapsulate the set of attributes of a given "class" of rule, such as
* <code>cc_binary</code>.
*
* <p>This is an instance of the "meta-class" pattern for Rules: we achieve using <i>values</i> what
* subclasses achieve using <i>types</i>. (The "Design Patterns" book doesn't include this pattern,
* so think of it as something like a cross between a Flyweight and a State pattern. Like Flyweight,
* we avoid repeatedly storing data that belongs to many instances. Like State, we delegate from
* Rule to RuleClass for the specific behavior of that rule (though unlike state, a Rule object
* never changes its RuleClass). This avoids the need to declare one Java class per class of Rule,
* yet achieves the same behavior.)
*
* <p>The use of a metaclass also allows us to compute a mapping from Attributes to small integers
* and share this between all rules of the same metaclass. This means we can save the attribute
* dictionary for each rule instance using an array, which is much more compact than a hashtable.
*
* <p>Rule classes whose names start with "$" are considered "abstract"; since they are not valid
* identifiers, they cannot be named in the build language. However, they are useful for grouping
* related attributes which are inherited.
*
* <p>The exact values in this class are important. In particular:
*
* <ul>
* <li>Changing an attribute from MANDATORY to OPTIONAL creates the potential for null-pointer
* exceptions in code that expects a value.
* <li>Attributes whose names are preceded by a "$" or a ":" are "hidden", and cannot be redefined
* in a BUILD file. They are a useful way of adding a special dependency. By convention,
* attributes starting with "$" are implicit dependencies, and those starting with a ":" are
* late-bound implicit dependencies, i.e. dependencies that can only be resolved when the
* configuration is known.
* <li>Attributes should not be introduced into the hierarchy higher then necessary.
* <li>The 'deps' and 'data' attributes are treated specially by the code that builds the runfiles
* tree. All targets appearing in these attributes appears beneath the ".runfiles" tree; in
* addition, "deps" may have rule-specific semantics.
* </ul>
*
* TODO(bazel-team): Consider breaking up this class in more manageable subclasses.
*/
// Non-final only for mocking in tests. Do not subclass!
@Immutable
public class RuleClass implements RuleClassData {
/** The name attribute, present for all rules at index 0. Also defined for all symbolic macros. */
public static final Attribute NAME_ATTRIBUTE =
attr("name", STRING_NO_INTERN)
.nonconfigurable("All rules have a non-customizable \"name\" attribute")
.mandatory()
.build();
/**
* Maximum attributes per RuleClass. Current value was chosen to be high enough to be considered a
* non-breaking change for reasonable use. It was also chosen to be low enough to give significant
* headroom before hitting limits imposed by the compact attribute value storage strategy in
* {@link Rule}.
*/
private static final int MAX_ATTRIBUTES = 200;
/**
* Maximum attribute name length. Chosen to accommodate existing and prevent extreme outliers from
* forming - extreme values create bloat, both in memory usage and various outputs, including but
* not limited to, query output.
*/
private static final int MAX_ATTRIBUTE_NAME_LENGTH = 128;
@SerializationConstant
static final Function<? super Rule, Set<String>> NO_OPTION_REFERENCE =
Functions.constant(ImmutableSet.of());
public static final PathFragment THIRD_PARTY_PREFIX = PathFragment.create("third_party");
public static final PathFragment EXPERIMENTAL_PREFIX = PathFragment.create("experimental");
/** The attribute that declares the set of metadata labels which apply to this target. */
public static final String APPLICABLE_METADATA_ATTR = "package_metadata";
public static final String APPLICABLE_METADATA_ATTR_ALT = "applicable_licenses";
/** Interface for determining whether a rule needs toolchain resolution or not. */
@FunctionalInterface
public interface ToolchainResolutionMode extends Serializable {
boolean useToolchainResolution(Rule rule);
ToolchainResolutionMode ENABLED = (unused) -> true;
ToolchainResolutionMode DISABLED = (unused) -> false;
}
/** Enum to determine whether a rule class uses auto exec groups. */
public enum AutoExecGroupsMode {
/** The rule class does not support auto exec groups. */
DISABLED,
/** The rule class uses auto exec groups regardless of other settings in the configuration. */
ENABLED,
/**
* The rule class uses auto exec groups if configured using the {@code _use_auto_exec_groups}
* attribute and {@code --incompatible_auto_exec_groups} flag.
*/
DYNAMIC;
public boolean isEnabled(AttributeMap attributes, boolean isAllowedByConfiguration) {
return switch (this) {
case DISABLED -> false;
case ENABLED -> true;
case DYNAMIC -> {
if (attributes.has("$use_auto_exec_groups")) {
yield attributes.get("$use_auto_exec_groups", Type.BOOLEAN);
} else {
yield isAllowedByConfiguration;
}
}
};
}
}
/** A factory or builder class for rule implementations. */
public interface ConfiguredTargetFactory<
ConfiguredTargetT, ContextT, ActionConflictExceptionT extends Throwable> {
/**
* Returns a fully initialized configured target instance using the given context, or {@code
* null} on certain rule errors (typically if {@code ruleContext.hasErrors()} becomes {@code
* true} while trying to create the target).
*
* @throws RuleErrorException if configured target creation could not be completed due to rule
* errors
* @throws ActionConflictExceptionT if there were conflicts during action registration
*/
@Nullable
ConfiguredTargetT create(ContextT ruleContext)
throws InterruptedException, RuleErrorException, ActionConflictExceptionT;
/**
* Exception indicating that configured target creation could not be completed. General error
* messaging should be done via {@link
* com.google.devtools.build.lib.analysis.RuleErrorConsumer}; this exception only interrupts
* configured target creation in cases where it can no longer continue.
*/
final class RuleErrorException extends Exception {
public RuleErrorException() {
super();
}
public RuleErrorException(String message) {
super(message);
}
public RuleErrorException(Throwable cause) {
super(cause);
}
public RuleErrorException(String message, Throwable cause) {
super(message, cause);
}
}
}
/**
* For Bazel's constraint system: the attribute that declares the set of environments a rule
* supports, overriding the defaults for their respective groups.
*/
public static final String RESTRICTED_ENVIRONMENT_ATTR = "restricted_to";
/**
* For Bazel's constraint system: the attribute that declares the set of environments a rule
* supports, appending them to the defaults for their respective groups.
*/
public static final String COMPATIBLE_ENVIRONMENT_ATTR = "compatible_with";
/**
* For Bazel's constraint system: the attribute that declares the list of constraints that the
* target platform must satisfy to be considered compatible.
*/
public static final String TARGET_COMPATIBLE_WITH_ATTR = "target_compatible_with";
/**
* For Bazel's constraint system: the attribute that declares the list of constraints that the
* execution platform must satisfy to be considered compatible.
*/
public static final String EXEC_COMPATIBLE_WITH_ATTR = "exec_compatible_with";
/**
* The attribute that declares execution properties that should be added to actions created by
* this target.
*/
public static final String EXEC_PROPERTIES_ATTR = "exec_properties";
/**
* For Bazel's constraint system: the implicit attribute used to store rule class restriction
* defaults as specified by {@link Builder#restrictedTo}.
*/
public static final String DEFAULT_RESTRICTED_ENVIRONMENT_ATTR =
"$" + RESTRICTED_ENVIRONMENT_ATTR;
/**
* For Bazel's constraint system: the implicit attribute used to store rule class compatibility
* defaults as specified by {@link Builder#compatibleWith}.
*/
public static final String DEFAULT_COMPATIBLE_ENVIRONMENT_ATTR =
"$" + COMPATIBLE_ENVIRONMENT_ATTR;
/**
* Name of the attribute that stores all {@link
* com.google.devtools.build.lib.rules.config.ConfigRuleClasses} labels this rule references (i.e.
* select() keys). This is specially populated in {@link #populateRuleAttributeValues}.
*
* <p>This isn't technically necessary for builds: select() keys are evaluated in {@link
* com.google.devtools.build.lib.skyframe.PrerequisiteProducer#computeConfigConditions} instead of
* normal dependency resolution because they're needed to determine other dependencies. So there's
* no intrinsic reason why we need an extra attribute to store them.
*
* <p>There are four reasons why we still create this attribute:
*
* <ol>
* <li>Collecting them once in {@link #populateRuleAttributeValues} instead of multiple times in
* ConfiguredTargetFunction saves extra looping over the rule's attributes.
* <li>Query's dependency resolution has no equivalent of {@link
* com.google.devtools.build.lib.skyframe.PrerequisiteProducer#computeConfigConditions} and
* we need to make sure its coverage remains complete.
* <li>Manual configuration trimming uses the normal dependency resolution process to work
* correctly and config_setting keys are subject to this trimming.
* <li>{@link Rule#useToolchainResolution(Rule) supports conditional toolchain resolution for
* targets with non-empty select()s. This requirement would go away if platform info was
* prepared for all rules regardless of toolchain needs.
* </ol>
*
* <p>It should be possible to clean up these issues if we decide we don't want an artificial
* attribute dependency. But care has to be taken to do that safely.
*/
public static final String CONFIG_SETTING_DEPS_ATTRIBUTE = "$config_dependencies";
/**
* A support class to make it easier to create {@code RuleClass} instances. This class follows the
* 'fluent builder' pattern.
*
* <p>The {@link #addAttribute} method will throw an exception if an attribute of that name
* already exists. Use {@link #overrideAttribute} in that case.
*/
public static final class Builder {
private static final Pattern RULE_NAME_PATTERN = Pattern.compile("[A-Za-z_][A-Za-z0-9_]*");
/** The type of the rule class, which determines valid names and required attributes. */
public enum RuleClassType {
/**
* Abstract rules are intended for rule classes that are just used to factor out common
* attributes, and for rule classes that are used only internally. These rules cannot be
* instantiated by a BUILD file.
*
* <p>The rule name must contain a '$' and {@link TargetUtils#isTestRuleName} must return
* false for the name.
*/
ABSTRACT {
@Override
public void checkName(String name) {
Preconditions.checkArgument(
(name.contains("$") && !TargetUtils.isTestRuleName(name)) || name.isEmpty());
}
@Override
public void checkAttributes(Map<String, Attribute> attributes) {
// No required attributes.
}
},
/**
* Invisible rule classes should contain a dollar sign so that they cannot be instantiated by
* the user. They are different from abstract rules in that they can be instantiated at will.
*/
INVISIBLE {
@Override
public void checkName(String name) {
Preconditions.checkArgument(name.contains("$"));
}
@Override
public void checkAttributes(Map<String, Attribute> attributes) {
// No required attributes.
}
},
/**
* Normal rules are instantiable by BUILD files, possibly via a macro (symbolic or legacy), in
* which case the rule's symbol is namespaced under {@code native}. Normal rule names must
* therefore obey the rules for identifiers in the BUILD language. In addition, {@link
* TargetUtils#isTestRuleName} must return false for the name.
*/
NORMAL {
@Override
public void checkName(String name) {
Preconditions.checkArgument(
!TargetUtils.isTestRuleName(name) && RULE_NAME_PATTERN.matcher(name).matches(),
"Invalid rule name: %s",
name);
}
@Override
public void checkAttributes(Map<String, Attribute> attributes) {
for (Attribute attribute : REQUIRED_ATTRIBUTES_FOR_NORMAL_RULES) {
Attribute presentAttribute = attributes.get(attribute.getName());
Preconditions.checkState(
presentAttribute != null,
"Missing mandatory '%s' attribute in normal rule class.",
attribute.getName());
Preconditions.checkState(
presentAttribute.getType().equals(attribute.getType()),
"Mandatory attribute '%s' in normal rule class has incorrect type (expected"
+ " %s).",
attribute.getName(),
attribute.getType());
}
}
},
/**
* Normal rules with the additional restriction that they can only be instantiated by BUILD
* files or legacy macros - but not symbolic macros.
*/
BUILD_ONLY {
@Override
public void checkName(String name) {
NORMAL.checkName(name);
}
@Override
public void checkAttributes(Map<String, Attribute> attributes) {
NORMAL.checkAttributes(attributes);
}
},
/**
* Workspace rules can only be instantiated from a WORKSPACE file. Their names obey the rule
* for identifiers.
*/
WORKSPACE {
@Override
public void checkName(String name) {
Preconditions.checkArgument(RULE_NAME_PATTERN.matcher(name).matches());
}
@Override
public void checkAttributes(Map<String, Attribute> attributes) {
// No required attributes.
}
},
/**
* Test rules are instantiable by BUILD files and are handled specially when run with the
* 'test' command. Their names must obey the rules for identifiers in the BUILD language and
* {@link TargetUtils#isTestRuleName} must return true for the name.
*
* <p>In addition, test rules must contain certain attributes. See {@link
* Builder#REQUIRED_ATTRIBUTES_FOR_TESTS}.
*/
TEST {
@Override
public void checkName(String name) {
Preconditions.checkArgument(
TargetUtils.isTestRuleName(name) && RULE_NAME_PATTERN.matcher(name).matches());
}
@Override
public void checkAttributes(Map<String, Attribute> attributes) {
for (Attribute attribute : REQUIRED_ATTRIBUTES_FOR_TESTS) {
Attribute presentAttribute = attributes.get(attribute.getName());
Preconditions.checkState(
presentAttribute != null,
"Missing mandatory '%s' attribute in test rule class.",
attribute.getName());
Preconditions.checkState(
presentAttribute.getType().equals(attribute.getType()),
"Mandatory attribute '%s' in test rule class has incorrect type (expected %s).",
attribute.getName(),
attribute.getType());
}
}
},
/**
* Placeholder rules are only instantiated when packages which refer to non-native rule
* classes are deserialized. At this time, non-native rule classes can't be serialized. To
* prevent crashes on deserialization, when a package containing a rule with a non-native rule
* class is deserialized, the rule is assigned a placeholder rule class. This is compatible
* with our limited set of package serialization use cases.
*
* <p>Placeholder rule class names obey the rule for identifiers.
*/
PLACEHOLDER {
@Override
public void checkName(String name) {
Preconditions.checkArgument(RULE_NAME_PATTERN.matcher(name).matches(), name);
}
@Override
public void checkAttributes(Map<String, Attribute> attributes) {
// No required attributes; this rule class cannot have the wrong set of attributes now
// because, if it did, the rule class would have failed to build before the package
// referring to it was serialized.
}
};
/**
* Checks whether the given name is valid for the current rule class type.
*
* @throws IllegalArgumentException if the name is not valid
*/
public abstract void checkName(String name);
/**
* Checks whether the given set of attributes contains all the required attributes for the
* current rule class type.
*
* @throws IllegalArgumentException if a required attribute is missing
*/
protected abstract void checkAttributes(Map<String, Attribute> attributes);
}
/**
* A predicate that filters rule classes based on their names.
*
* <p>In {@link Rule}, {@code ruleClass} refers to the string name of the rule class while
* {@code ruleClassObject} refers to the actual instance of {@link RuleClass}. Here, {@code
* RuleClassName} emphasizes that the underlying logic of the predicate is based only on the
* {@code String} name. The public methods, {@link #asPredicateOfRuleClass} and {@link
* #asPredicateOfRuleClassObject} revert to the common convention used in {@link Rule}.
*/
@AutoCodec
public static class RuleClassNamePredicate {
private static final RuleClassNamePredicate UNSPECIFIED_INSTANCE =
new RuleClassNamePredicate(ImmutableSet.of(), PredicateType.UNSPECIFIED, null);
private final ImmutableSet<String> ruleClassNames;
private final PredicateType predicateType;
private final Predicate<String> ruleClassNamePredicate;
private final Predicate<RuleClass> ruleClassPredicate;
// if non-null, used ONLY for checking overlap
@Nullable private final Set<?> overlappable;
@VisibleForSerialization
enum PredicateType {
ONLY,
All_EXCEPT,
UNSPECIFIED
}
@VisibleForSerialization
RuleClassNamePredicate(
ImmutableSet<String> ruleClassNames, PredicateType predicateType, Set<?> overlappable) {
this.ruleClassNames = ruleClassNames;
this.predicateType = predicateType;
this.overlappable = overlappable;
switch (predicateType) {
case All_EXCEPT -> {
Predicate<String> containing = only(ruleClassNames).asPredicateOfRuleClass();
ruleClassNamePredicate =
new DescribedPredicate<>(Predicates.not(containing), "all but " + containing);
ruleClassPredicate =
new DescribedPredicate<>(
Predicates.compose(ruleClassNamePredicate, RuleClass::getName),
ruleClassNamePredicate.toString());
}
case ONLY -> {
ruleClassNamePredicate =
new DescribedPredicate<>(
Predicates.in(ruleClassNames), StringUtil.joinEnglishList(ruleClassNames));
ruleClassPredicate =
new DescribedPredicate<>(
Predicates.compose(ruleClassNamePredicate, RuleClass::getName),
ruleClassNamePredicate.toString());
}
case UNSPECIFIED -> {
ruleClassNamePredicate = Predicates.alwaysTrue();
ruleClassPredicate = Predicates.alwaysTrue();
}
default ->
// This shouldn't happen normally since the constructor is private and within this
// file.
throw new IllegalArgumentException(
"Predicate type was not specified when constructing a RuleClassNamePredicate.");
}
}
public static RuleClassNamePredicate only(Iterable<String> ruleClassNamesAsIterable) {
ImmutableSet<String> ruleClassNames = ImmutableSet.copyOf(ruleClassNamesAsIterable);
return new RuleClassNamePredicate(ruleClassNames, PredicateType.ONLY, ruleClassNames);
}
public static RuleClassNamePredicate only(String... ruleClasses) {
return only(Arrays.asList(ruleClasses));
}
public static RuleClassNamePredicate allExcept(String... ruleClasses) {
ImmutableSet<String> ruleClassNames = ImmutableSet.copyOf(ruleClasses);
Preconditions.checkState(!ruleClassNames.isEmpty(), "Use unspecified() instead");
return new RuleClassNamePredicate(ruleClassNames, PredicateType.All_EXCEPT, null);
}
/**
* This is a special sentinel value which represents a "default" {@link
* RuleClassNamePredicate} which is unspecified. Note that a call to its {@link
* RuleClassNamePredicate#asPredicateOfRuleClass} produces {@code
* Predicates.<RuleClass>alwaysTrue()}, which is a sentinel value for other parts of bazel.
*/
public static RuleClassNamePredicate unspecified() {
return UNSPECIFIED_INSTANCE;
}
final Predicate<String> asPredicateOfRuleClass() {
return ruleClassNamePredicate;
}
final Predicate<RuleClass> asPredicateOfRuleClassObject() {
return ruleClassPredicate;
}
/**
* Determines whether two {@code RuleClassNamePredicate}s should be considered incompatible as
* rule class predicate and rule class warning predicate.
*
* <p>Specifically, if both list sets of explicit rule class names to permit, those two sets
* must be disjoint, so the restriction only applies when both predicates have been created by
* {@link #only}.
*/
boolean consideredOverlapping(RuleClassNamePredicate that) {
return this.overlappable != null
&& that.overlappable != null
&& !Collections.disjoint(this.overlappable, that.overlappable);
}
@Override
public int hashCode() {
return HashCodes.hashObjects(ruleClassNames, predicateType);
}
@Override
public boolean equals(Object obj) {
// NOTE: Specifically not checking equality of ruleClassPredicate.
// By construction, if the name predicates are equals, the rule class predicates are, too.
return obj instanceof RuleClassNamePredicate
&& ruleClassNames.equals(((RuleClassNamePredicate) obj).ruleClassNames)
&& predicateType.equals(((RuleClassNamePredicate) obj).predicateType);
}
@Override
public String toString() {
return ruleClassNamePredicate.toString();
}
/** A pass-through predicate, except that an explicit {@link #toString()} is provided. */
private static class DescribedPredicate<T> implements Predicate<T> {
private final Predicate<T> delegate; // the actual predicate
private final String description;
private DescribedPredicate(Predicate<T> delegate, String description) {
this.delegate = delegate;
this.description = description;
}
@Override
public boolean apply(T input) {
return delegate.apply(input);
}
@Override
public int hashCode() {
return delegate.hashCode();
}
@Override
public boolean equals(Object obj) {
return obj instanceof DescribedPredicate
&& delegate.equals(((DescribedPredicate<?>) obj).delegate);
}
@Override
public String toString() {
return description;
}
}
}
/**
* Name of default attribute implicitly added to all Starlark RuleClasses that are {@code
* build_setting}s.
*/
public static final String STARLARK_BUILD_SETTING_DEFAULT_ATTR_NAME = "build_setting_default";
static final String STARLARK_BUILD_SETTING_HELP_ATTR_NAME = "help";
static final String BUILD_SETTING_DEFAULT_NONCONFIGURABLE =
"Build setting defaults are referenced during analysis.";
/** List of required attributes for normal rules, name and type. */
static final ImmutableList<Attribute> REQUIRED_ATTRIBUTES_FOR_NORMAL_RULES =
ImmutableList.of(attr("tags", Types.STRING_LIST).build());
/** List of required attributes for test rules, name and type. */
static final ImmutableList<Attribute> REQUIRED_ATTRIBUTES_FOR_TESTS =
ImmutableList.of(
attr("tags", Types.STRING_LIST).build(),
attr("size", Type.STRING).build(),
attr("timeout", Type.STRING).build(),
attr("flaky", Type.FLAKY_TEST_RETRIES).build(),
attr("shard_count", Type.INTEGER).build(),
attr("local", Type.BOOLEAN).build());
private final String name;
private ImmutableList<StarlarkThread.CallStackEntry> callstack = ImmutableList.of();
private final RuleClassType type;
@Nullable private RuleClass starlarkParent = null;
@Nullable private StarlarkFunction initializer = null;
@Nullable private LabelConverter labelConverterForInitializer = null;
// The extendable may take 3 value, null means that the default allowlist should be use when
// rule is extendable in practice.
@Nullable private Boolean extendable = null;
@Nullable private Label extendableAllowlist = null;
@Nullable private Label defaultExtendableAllowlist = null;
private final boolean starlark;
private boolean starlarkTestable = false;
private boolean documented;
private boolean outputsToBindir = true;
private boolean workspaceOnly = false;
private boolean dependencyResolutionRule = false;
private boolean isExecutableStarlark = false;
private boolean isAnalysisTest = false;
private boolean hasAnalysisTestTransition = false;
private final ImmutableList.Builder<AllowlistChecker> allowlistCheckers =
ImmutableList.builder();
private boolean ignoreLicenses = false;
private ImplicitOutputsFunction implicitOutputsFunction = SafeImplicitOutputsFunction.NONE;
private TransitionFactory<RuleTransitionData> transitionFactory = NoTransition.getFactory();
private ConfiguredTargetFactory<?, ?, ?> configuredTargetFactory = null;
private final AdvertisedProviderSet.Builder advertisedProviders =
AdvertisedProviderSet.builder();
private StarlarkCallable configuredTargetFunction = null;
private BuildSetting buildSetting = null;
private ImmutableList<? extends StarlarkSubruleApi> subrules = ImmutableList.of();
private Function<? super Rule, ? extends Set<String>> optionReferenceFunction =
NO_OPTION_REFERENCE;
/** The following 3 fields are null iff the rule is native. */
@Nullable private Label ruleDefinitionEnvironmentLabel;
@Nullable private byte[] ruleDefinitionEnvironmentDigest = null;
// TODO(b/366027483): in theory, ruleDefinitionEnvironmentLabel ought to equal
// starlarkExtensionLabel, and we ought to get rid of one of them.
@Nullable private Label starlarkExtensionLabel = null;
// May be non-null only if the rule is Starlark-defined.
@Nullable private String starlarkDocumentation = null;
/** This field is non-null iff the rule is a Starlark repo rule. */
@Nullable
private ImmutableTable<RepositoryName, String, RepositoryName>
ruleDefinitionEnvironmentRepoMappingEntries;
private final ConfigurationFragmentPolicy.Builder configurationFragmentPolicy =
new ConfigurationFragmentPolicy.Builder();
private boolean supportsConstraintChecking = true;
private final Map<String, Attribute> attributes = new LinkedHashMap<>();
private final Set<ToolchainTypeRequirement> toolchainTypes = new LinkedHashSet<>();
private ToolchainResolutionMode toolchainResolutionMode = ToolchainResolutionMode.ENABLED;
private final Set<Label> executionPlatformConstraints = new LinkedHashSet<>();
private OutputFile.Kind outputFileKind = OutputFile.Kind.FILE;
private final Map<String, ExecGroup> execGroups = new LinkedHashMap<>();
private AutoExecGroupsMode autoExecGroupsMode = AutoExecGroupsMode.DYNAMIC;
/**
* Constructs a new {@link RuleClass.Builder} using all attributes from all parent rule classes.
* An attribute cannot exist in more than one parent.
*
* <p>The rule type affects the allowed names and the required attributes (see {@link
* RuleClassType}).
*
* @param parents There may be either multiple native {@code RuleClassType.ABSTRACT} rules or a
* single Starlark rule.
* @throws IllegalArgumentException if an attribute with the same name exists in more than one
* parent
*/
public Builder(String name, RuleClassType type, boolean starlark, RuleClass... parents) {
Preconditions.checkArgument(
(parents.length == 1 && parents[0].isStarlark())
|| Arrays.stream(parents).allMatch(rule -> !rule.isStarlark()));
this.name = name;
this.starlark = starlark;
this.type = type;
Preconditions.checkState(starlark || type != RuleClassType.PLACEHOLDER, name);
this.documented = type != RuleClassType.ABSTRACT;
addAttribute(NAME_ATTRIBUTE);
if (parents.length == 1
&& parents[0].isStarlark()
&& parents[0].getRuleClassType() != RuleClassType.ABSTRACT) {
// the condition removes {@link StarlarkRuleClassFunctions.baseRule} and binaryBaseRule,
// which are marked as Starlark (because of Stardoc) && abstract at the same time
starlarkParent = parents[0];
Preconditions.checkArgument(starlarkParent.isExtendable());
}
for (RuleClass parent : parents) {
if (parent.dependencyResolutionRule) {
dependencyResolutionRule = true;
} else if (dependencyResolutionRule) {
throw new IllegalArgumentException(
"Inconsistent value of dependencyResolutionRule among parents");
}
configurationFragmentPolicy.includeConfigurationFragmentsFrom(
parent.getConfigurationFragmentPolicy());
supportsConstraintChecking = parent.supportsConstraintChecking;
addToolchainTypes(parent.getToolchainTypes());
addExecutionPlatformConstraints(parent.getExecutionPlatformConstraints());
try {
addExecGroups(parent.getExecGroups());
} catch (DuplicateExecGroupError e) {
throw new IllegalArgumentException(
String.format(
"An execution group named '%s' is inherited multiple times with different"
+ " requirements in %s ruleclass",
e.getDuplicateGroup(), name));
}
this.autoExecGroupsMode = parent.getAutoExecGroupsMode();
for (Attribute attribute : parent.getAttributes()) {
String attrName = attribute.getName();
Preconditions.checkArgument(
!attributes.containsKey(attrName) || attributes.get(attrName).equals(attribute),
"Attribute %s is inherited multiple times in %s ruleclass",
attrName,
name);
attributes.put(attrName, attribute);
}
allowlistCheckers.addAll(parent.getAllowlistCheckers());
advertisedProviders.addParent(parent.getAdvertisedProviders());
if (parent.getDefaultImplicitOutputsFunction() != SafeImplicitOutputsFunction.NONE) {
if (implicitOutputsFunction != SafeImplicitOutputsFunction.NONE) {
throw new IllegalArgumentException("Only a single parent may set implicit outputs");
}
implicitOutputsFunction = parent.getDefaultImplicitOutputsFunction();
}
}
// TODO(bazel-team): move this testonly attribute setting to somewhere else
// preferably to some base RuleClass implementation.
if (this.type.equals(RuleClassType.TEST)) {
Attribute.Builder<Boolean> testOnlyAttr =
attr("testonly", BOOLEAN)
.value(true)
.nonconfigurable("policy decision: this shouldn't depend on the configuration");
if (attributes.containsKey("testonly")) {
override(testOnlyAttr);
} else {
add(testOnlyAttr);
}
}
}
/**
* Same as {@link #build}, except for a Starlark-defined rule class; the rule class's key will
* be derived from the Starlark file label (falling back to the rule definition environment
* label if null) and the name.
*
* @param name rule class name; if the builder was initialized with an empty name, this value
* will override it.
* @param starlarkExtensionLabel the label of the Starlark file where the rule class was
* exported. Permitted to be null only as a workaround for unexported repository rules in
* legacy code - see https://github.com/bazelbuild/bazel/issues/10441 and b/111199163.
*/
public RuleClass buildStarlark(String name, @Nullable Label starlarkExtensionLabel) {
Preconditions.checkState(starlark);
this.starlarkExtensionLabel = starlarkExtensionLabel;
Label keyLabel =
this.starlarkExtensionLabel != null
? this.starlarkExtensionLabel
: ruleDefinitionEnvironmentLabel;
return build(name, keyLabel + "%" + name);
}
/**
* For a native rule, checks that required attributes for test rules are present, creates the
* {@link RuleClass} object and returns it.
*
* @throws IllegalStateException if any of the required attributes is missing
*/
public RuleClass build() {
// For built-ins, name == key
return build(name, name);
}
/** Same as {@link #build} except with setting the name and key parameters. */
private RuleClass build(String name, String key) {
Preconditions.checkArgument(this.name.isEmpty() || this.name.equals(name));
type.checkName(name);
checkAttributes(name);
Preconditions.checkState(
(type == RuleClassType.ABSTRACT)
== (configuredTargetFactory == null && configuredTargetFunction == null),
"Bad combo for %s: %s %s %s",
name,
type,
configuredTargetFactory,
configuredTargetFunction);
if (!workspaceOnly && starlark) {
assertStarlarkRuleClassHasImplementationFunction();
assertStarlarkRuleClassHasEnvironmentLabel();
}
if (type == RuleClassType.PLACEHOLDER) {
Preconditions.checkNotNull(ruleDefinitionEnvironmentDigest, this.name);
}
if (buildSetting != null) {
Type<?> type = buildSetting.getType();
Attribute.Builder<?> defaultAttrBuilder =
attr(STARLARK_BUILD_SETTING_DEFAULT_ATTR_NAME, type)
.nonconfigurable(BUILD_SETTING_DEFAULT_NONCONFIGURABLE)
.mandatory();
this.add(defaultAttrBuilder);
this.add(
attr(STARLARK_BUILD_SETTING_HELP_ATTR_NAME, Type.STRING)
.nonconfigurable(BUILD_SETTING_DEFAULT_NONCONFIGURABLE));
// Build setting rules should opt out of toolchain resolution, since they form part of the
// configuration.
this.toolchainResolutionMode(ToolchainResolutionMode.DISABLED);
}
if (starlark
&& (type == RuleClassType.NORMAL || type == RuleClassType.TEST)
&& outputsToBindir
&& !starlarkTestable
&& !isAnalysisTest
&& buildSetting == null) {
if (extendable == null) { // The rule can be extended, use fallback
extendable = true;
extendableAllowlist = defaultExtendableAllowlist;
}
} else {
// This kind of rule can't be extended
if (Boolean.TRUE.equals(extendable) || extendableAllowlist != null) {
throw new IllegalArgumentException("The rule cannot be extended");
}
extendable = false;
}
return new RuleClass(
name,
callstack,
key,
type,
starlarkParent,
initializer,
labelConverterForInitializer,
starlark,
starlarkExtensionLabel,
starlarkDocumentation,
extendable,
extendableAllowlist,
starlarkTestable,
documented,
outputsToBindir,
workspaceOnly,
dependencyResolutionRule,
isExecutableStarlark,
isAnalysisTest,
hasAnalysisTestTransition,
allowlistCheckers.build(),
ignoreLicenses,
implicitOutputsFunction,
transitionFactory,
configuredTargetFactory,
advertisedProviders.build(),
configuredTargetFunction,
optionReferenceFunction,
ruleDefinitionEnvironmentLabel,
ruleDefinitionEnvironmentDigest,
ruleDefinitionEnvironmentRepoMappingEntries,
configurationFragmentPolicy.build(),
supportsConstraintChecking,
toolchainTypes,
toolchainResolutionMode,
executionPlatformConstraints,
execGroups,
autoExecGroupsMode,
outputFileKind,
ImmutableList.copyOf(attributes.values()),
buildSetting,
subrules);
}
private void checkAttributes(String ruleClassName) {
Preconditions.checkArgument(
attributes.size() <= MAX_ATTRIBUTES,
"Rule class %s declared too many attributes (%s > %s)",
ruleClassName,
attributes.size(),
MAX_ATTRIBUTES);
ImmutableList.Builder<String> attributesNotForDependencyResolutionBuilder =
ImmutableList.builder();
for (var entry : attributes.entrySet()) {
String attributeName = entry.getKey();
Attribute attribute = entry.getValue();
int attributeNameLength =
StarlarkSubruleApi.getUserDefinedNameIfSubruleAttr(subrules, attributeName)