/
RuleClass.java
2642 lines (2379 loc) · 104 KB
/
RuleClass.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
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.ANY_RULE;
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 com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Function;
import com.google.common.base.Functions;
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.Interner;
import com.google.common.collect.Iterables;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.google.common.collect.Ordering;
import com.google.devtools.build.lib.analysis.config.transitions.ConfigurationTransition;
import com.google.devtools.build.lib.analysis.config.transitions.PatchTransition;
import com.google.devtools.build.lib.analysis.config.transitions.SplitTransition;
import com.google.devtools.build.lib.analysis.config.transitions.TransitionFactory;
import com.google.devtools.build.lib.cmdline.Label;
import com.google.devtools.build.lib.cmdline.LabelSyntaxException;
import com.google.devtools.build.lib.cmdline.PackageIdentifier;
import com.google.devtools.build.lib.cmdline.RepositoryName;
import com.google.devtools.build.lib.events.EventHandler;
import com.google.devtools.build.lib.events.NullEventHandler;
import com.google.devtools.build.lib.packages.Attribute.ComputedDefault;
import com.google.devtools.build.lib.packages.Attribute.SkylarkComputedDefaultTemplate;
import com.google.devtools.build.lib.packages.Attribute.SkylarkComputedDefaultTemplate.CannotPrecomputeDefaultsException;
import com.google.devtools.build.lib.packages.BuildType.LabelConversionContext;
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.RuleClass.Builder.RuleClassType;
import com.google.devtools.build.lib.packages.RuleClass.Builder.ThirdPartyLicenseExistencePolicy;
import com.google.devtools.build.lib.packages.RuleFactory.AttributeValues;
import com.google.devtools.build.lib.packages.Type.ConversionException;
import com.google.devtools.build.lib.skyframe.serialization.autocodec.AutoCodec;
import com.google.devtools.build.lib.skyframe.serialization.autocodec.AutoCodec.VisibleForSerialization;
import com.google.devtools.build.lib.syntax.EvalException;
import com.google.devtools.build.lib.syntax.Location;
import com.google.devtools.build.lib.syntax.Sequence;
import com.google.devtools.build.lib.syntax.Starlark;
import com.google.devtools.build.lib.syntax.StarlarkCallable;
import com.google.devtools.build.lib.syntax.StarlarkThread;
import com.google.devtools.build.lib.util.FileTypeSet;
import com.google.devtools.build.lib.util.StringUtil;
import com.google.devtools.build.lib.vfs.PathFragment;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.BitSet;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.regex.Pattern;
import javax.annotation.Nullable;
import javax.annotation.concurrent.Immutable;
/**
* 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
@AutoCodec
public class RuleClass {
/**
* 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 {@link AttributeContainer}'s limits.
*/
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;
@AutoCodec
static final Function<? super Rule, Map<String, Label>> NO_EXTERNAL_BINDINGS =
Functions.<Map<String, Label>>constant(ImmutableMap.<String, Label>of());
@AutoCodec
static final Function<? super Rule, Set<String>> NO_OPTION_REFERENCE =
Functions.<Set<String>>constant(ImmutableSet.<String>of());
public static final PathFragment THIRD_PARTY_PREFIX = PathFragment.create("third_party");
public static final PathFragment EXPERIMENTAL_PREFIX = PathFragment.create("experimental");
public static final String EXEC_COMPATIBLE_WITH_ATTR = "exec_compatible_with";
public static final String EXEC_PROPERTIES = "exec_properties";
/*
* The attribute that declares the set of license labels which apply to this target.
*/
public static final String APPLICABLE_LICENSES_ATTR = "applicable_licenses";
/**
* A constraint for the package name of the Rule instances.
*/
public static class PackageNameConstraint implements PredicateWithMessage<Rule> {
public static final int ANY_SEGMENT = 0;
private final int pathSegment;
private final Set<String> values;
/**
* The pathSegment-th segment of the package must be one of the specified values.
* The path segment indexing starts from 1.
*/
public PackageNameConstraint(int pathSegment, String... values) {
this.values = ImmutableSet.copyOf(values);
this.pathSegment = pathSegment;
}
@Override
public boolean apply(Rule input) {
PathFragment path = input.getLabel().getPackageFragment();
if (pathSegment == ANY_SEGMENT) {
return path.getFirstSegment(values) != PathFragment.INVALID_SEGMENT;
} else {
return path.segmentCount() >= pathSegment
&& values.contains(path.getSegment(pathSegment - 1));
}
}
@Override
public String getErrorReason(Rule param) {
if (pathSegment == ANY_SEGMENT) {
return param.getRuleClass() + " rules have to be under a "
+ StringUtil.joinEnglishList(values, "or", "'") + " directory";
} else if (pathSegment == 1) {
return param.getRuleClass() + " rules are only allowed in "
+ StringUtil.joinEnglishList(StringUtil.append(values, "//", ""), "or");
} else {
return param.getRuleClass() + " rules are only allowed in packages which "
+ StringUtil.ordinal(pathSegment) + " is " + StringUtil.joinEnglishList(values, "or");
}
}
@VisibleForTesting
public int getPathSegment() {
return pathSegment;
}
@VisibleForTesting
public Collection<String> getValues() {
return values;
}
}
/** A factory or builder class for rule implementations. */
public interface ConfiguredTargetFactory<
TConfiguredTarget, TContext, TActionConflictException extends Throwable> {
/**
* Returns a fully initialized configured target instance using the given context.
*
* @throws RuleErrorException if configured target creation could not be completed due to rule
* errors
* @throws TActionConflictException if there were conflicts during action registration
*/
TConfiguredTarget create(TContext ruleContext)
throws InterruptedException, RuleErrorException, TActionConflictException;
/**
* Exception indicating that configured target creation could not be completed. General error
* messaging should be done via {@link RuleErrorConsumer}; this exception only interrupts
* configured target creation in cases where it can no longer continue.
*/
final class RuleErrorException extends Exception {
RuleErrorException() {
super();
}
RuleErrorException(String message) {
super(message);
}
RuleErrorException(Throwable cause) {
super(cause);
}
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 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.ConfiguredTargetFunction#getConfigConditions} 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 three 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.ConfiguredTargetFunction#getConfigConditions} 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.
* </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. Their 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());
}
}
},
/**
* 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.
*
* 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
*/
public abstract void checkAttributes(Map<String, Attribute> attributes);
}
/** A predicate that filters rule classes based on their names. */
@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).asPredicateOfRuleClassName();
ruleClassNamePredicate =
new DescribedPredicate<>(
Predicates.not(containing), "all but " + containing.toString());
ruleClassPredicate =
new DescribedPredicate<>(
Predicates.compose(ruleClassNamePredicate, RuleClass::getName),
ruleClassNamePredicate.toString());
break;
case ONLY:
ruleClassNamePredicate =
new DescribedPredicate<>(
Predicates.in(ruleClassNames), StringUtil.joinEnglishList(ruleClassNames));
ruleClassPredicate =
new DescribedPredicate<>(
Predicates.compose(ruleClassNamePredicate, RuleClass::getName),
ruleClassNamePredicate.toString());
break;
case UNSPECIFIED:
ruleClassNamePredicate = Predicates.alwaysTrue();
ruleClassPredicate = Predicates.alwaysTrue();
break;
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;
}
public final Predicate<String> asPredicateOfRuleClassName() {
return ruleClassNamePredicate;
}
public final Predicate<RuleClass> asPredicateOfRuleClass() {
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 Objects.hash(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";
public static final String BUILD_SETTING_DEFAULT_NONCONFIGURABLE =
"Build setting defaults are referenced during analysis.";
/** List of required attributes for normal rules, name and type. */
public static final ImmutableList<Attribute> REQUIRED_ATTRIBUTES_FOR_NORMAL_RULES =
ImmutableList.of(attr("tags", Type.STRING_LIST).build());
/** List of required attributes for test rules, name and type. */
public static final ImmutableList<Attribute> REQUIRED_ATTRIBUTES_FOR_TESTS =
ImmutableList.of(
attr("tags", Type.STRING_LIST).build(),
attr("size", Type.STRING).build(),
attr("timeout", Type.STRING).build(),
attr("flaky", Type.BOOLEAN).build(),
attr("shard_count", Type.INTEGER).build(),
attr("local", Type.BOOLEAN).build());
private String name;
private ImmutableList<StarlarkThread.CallStackEntry> callstack = ImmutableList.of();
private final RuleClassType type;
private final boolean starlark;
private boolean starlarkTestable = false;
private boolean documented;
private boolean publicByDefault = false;
private boolean binaryOutput = true;
private boolean workspaceOnly = false;
private boolean isExecutableStarlark = false;
private boolean isAnalysisTest = false;
private boolean hasAnalysisTestTransition = false;
private boolean hasFunctionTransitionWhitelist = false;
private boolean hasStarlarkRuleTransition = false;
private boolean ignoreLicenses = false;
private ImplicitOutputsFunction implicitOutputsFunction = ImplicitOutputsFunction.NONE;
private TransitionFactory<Rule> transitionFactory;
private ConfiguredTargetFactory<?, ?, ?> configuredTargetFactory = null;
private PredicateWithMessage<Rule> validityPredicate =
PredicatesWithMessage.<Rule>alwaysTrue();
private Predicate<String> preferredDependencyPredicate = Predicates.alwaysFalse();
private AdvertisedProviderSet.Builder advertisedProviders = AdvertisedProviderSet.builder();
private StarlarkCallable configuredTargetFunction = null;
private BuildSetting buildSetting = null;
private Function<? super Rule, Map<String, Label>> externalBindingsFunction =
NO_EXTERNAL_BINDINGS;
private Function<? super Rule, ? extends Set<String>> optionReferenceFunction =
NO_OPTION_REFERENCE;
/** This field and the next are null iff the rule is native. */
@Nullable private Label ruleDefinitionEnvironmentLabel;
@Nullable private byte[] ruleDefinitionEnvironmentDigest = null;
private ConfigurationFragmentPolicy.Builder configurationFragmentPolicy =
new ConfigurationFragmentPolicy.Builder();
private boolean supportsConstraintChecking = true;
/**
* The policy on whether Bazel should enforce that third_party rules declare <code>licenses().
* </code>. This is only intended for the migration of <a
* href="https://github.com/bazelbuild/bazel/issues/7444">GitHub #7444</a>. Our final end state
* is to have no license-related logic whatsoever. But that's going to take some time.
*/
public enum ThirdPartyLicenseExistencePolicy {
/**
* Always do this check, overriding whatever {@link
* StarlarkSemanticsOptions#incompatibleDisableThirdPartyLicenseChecking} says.
*/
ALWAYS_CHECK,
/**
* Never do this check, overriding whatever {@link
* StarlarkSemanticsOptions#incompatibleDisableThirdPartyLicenseChecking} says.
*/
NEVER_CHECK,
/**
* Do whatever {@link StarlarkSemanticsOptions#incompatibleDisableThirdPartyLicenseChecking}
* says.
*/
USER_CONTROLLABLE
}
private ThirdPartyLicenseExistencePolicy thirdPartyLicenseExistencePolicy;
private final Map<String, Attribute> attributes = new LinkedHashMap<>();
private final Set<Label> requiredToolchains = new HashSet<>();
private boolean useToolchainResolution = true;
private Set<Label> executionPlatformConstraints = new HashSet<>();
private OutputFile.Kind outputFileKind = OutputFile.Kind.FILE;
private final Map<String, ExecGroup> execGroups = new HashMap<>();
/**
* Constructs a new {@code RuleClassBuilder} 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}).
*
* @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) {
this.name = name;
this.starlark = starlark;
this.type = type;
Preconditions.checkState(starlark || type != RuleClassType.PLACEHOLDER, name);
this.documented = type != RuleClassType.ABSTRACT;
for (RuleClass parent : parents) {
if (parent.getValidityPredicate() != PredicatesWithMessage.<Rule>alwaysTrue()) {
setValidityPredicate(parent.getValidityPredicate());
}
if (parent.preferredDependencyPredicate != Predicates.<String>alwaysFalse()) {
setPreferredDependencyPredicate(parent.preferredDependencyPredicate);
}
configurationFragmentPolicy
.includeConfigurationFragmentsFrom(parent.getConfigurationFragmentPolicy());
configurationFragmentPolicy.setMissingFragmentPolicy(
parent.getConfigurationFragmentPolicy().getMissingFragmentPolicy());
supportsConstraintChecking = parent.supportsConstraintChecking;
addRequiredToolchains(parent.getRequiredToolchains());
useToolchainResolution = parent.useToolchainResolution;
addExecutionPlatformConstraints(parent.getExecutionPlatformConstraints());
try {
addExecGroups(parent.getExecGroups());
} catch (DuplicateExecGroupError e) {
throw new IllegalArgumentException(
String.format(
"An execution group named '%s' is inherited multiple times in %s ruleclass",
e.getDuplicateGroup(), name));
}
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);
}
advertisedProviders.addParent(parent.getAdvertisedProviders());
}
// 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);
}
}
}
/**
* 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. */
public RuleClass build(String name, String key) {
Preconditions.checkArgument(this.name.isEmpty() || this.name.equals(name));
type.checkName(name);
checkAttributes(name, type, attributes);
Preconditions.checkState(
(type == RuleClassType.ABSTRACT)
== (configuredTargetFactory == null && configuredTargetFunction == null),
"Bad combo for %s: %s %s %s",
name,
type,
configuredTargetFactory,
configuredTargetFunction);
if (!workspaceOnly) {
if (starlark) {
assertStarlarkRuleClassHasImplementationFunction();
assertStarlarkRuleClassHasEnvironmentLabel();
}
Preconditions.checkState(externalBindingsFunction == NO_EXTERNAL_BINDINGS);
}
if (type == RuleClassType.PLACEHOLDER) {
Preconditions.checkNotNull(ruleDefinitionEnvironmentDigest, this.name);
}
if (buildSetting != null) {
Type<?> type = buildSetting.getType();
Attribute.Builder<?> attrBuilder =
attr(STARLARK_BUILD_SETTING_DEFAULT_ATTR_NAME, type)
.nonconfigurable(BUILD_SETTING_DEFAULT_NONCONFIGURABLE)
.mandatory();
if (BuildType.isLabelType(type)) {
attrBuilder.allowedFileTypes(FileTypeSet.ANY_FILE);
attrBuilder.allowedRuleClasses(ANY_RULE);
}
this.add(attrBuilder);
// Build setting rules should opt out of toolchain resolution, since they form part of the
// configuration.
this.useToolchainResolution(false);
}
return new RuleClass(
name,
callstack,
key,
type,
starlark,
starlarkTestable,
documented,
publicByDefault,
binaryOutput,
workspaceOnly,
isExecutableStarlark,
isAnalysisTest,
hasAnalysisTestTransition,
hasFunctionTransitionWhitelist,
ignoreLicenses,
implicitOutputsFunction,
transitionFactory,
configuredTargetFactory,
validityPredicate,
preferredDependencyPredicate,
advertisedProviders.build(),
configuredTargetFunction,
externalBindingsFunction,
optionReferenceFunction,
ruleDefinitionEnvironmentLabel,
ruleDefinitionEnvironmentDigest,
configurationFragmentPolicy.build(),
supportsConstraintChecking,
thirdPartyLicenseExistencePolicy,
requiredToolchains,
useToolchainResolution,
executionPlatformConstraints,
execGroups,
outputFileKind,
attributes.values(),
buildSetting);
}
private static void checkAttributes(
String ruleClassName, RuleClassType ruleClassType, Map<String, Attribute> attributes) {
Preconditions.checkArgument(
attributes.size() <= MAX_ATTRIBUTES,
"Rule class %s declared too many attributes (%s > %s)",
ruleClassName,
attributes.size(),
MAX_ATTRIBUTES);
for (String attributeName : attributes.keySet()) {
// TODO(b/151171037): This check would make more sense at Attribute creation time, but the
// use of unchecked exceptions in these APIs makes it brittle.
Preconditions.checkArgument(
attributeName.length() <= MAX_ATTRIBUTE_NAME_LENGTH,
"Attribute %s.%s's name is too long (%s > %s)",
ruleClassName,
attributeName,
attributeName.length(),
MAX_ATTRIBUTE_NAME_LENGTH);
}
ruleClassType.checkAttributes(attributes);
}
private void assertStarlarkRuleClassHasImplementationFunction() {
Preconditions.checkState(
(type == RuleClassType.NORMAL || type == RuleClassType.TEST)
== (configuredTargetFunction != null),
"%s %s",
type,
configuredTargetFunction);
}
private void assertStarlarkRuleClassHasEnvironmentLabel() {
Preconditions.checkState(
(type == RuleClassType.NORMAL
|| type == RuleClassType.TEST
|| type == RuleClassType.PLACEHOLDER)
== (ruleDefinitionEnvironmentLabel != null),
"Concrete Starlark rule classes can't have null labels: %s %s",
ruleDefinitionEnvironmentLabel,
type);
}
/**
* Declares that the implementation of the associated rule class requires the given fragments to
* be present in this rule's host and target configurations.
*
* <p>The value is inherited by subclasses.
*/
public Builder requiresConfigurationFragments(Class<?>... configurationFragments) {
configurationFragmentPolicy.requiresConfigurationFragments(
ImmutableSet.<Class<?>>copyOf(configurationFragments));
return this;
}
/**
* Declares that the implementation of the associated rule class requires the given
* fragments to be present in the given configuration that isn't the rule's configuration but
* is also readable by the rule.
*
* <p>You probably don't want to use this, because rules generally shouldn't read configurations
* other than their own. If you want to declare host config fragments, see
* {@link com.google.devtools.build.lib.analysis.config.ConfigAwareRuleClassBuilder}.
*
* <p>The value is inherited by subclasses.
*/
public Builder requiresConfigurationFragments(ConfigurationTransition transition,
Class<?>... configurationFragments) {
configurationFragmentPolicy.requiresConfigurationFragments(
transition,
ImmutableSet.<Class<?>>copyOf(configurationFragments));
return this;
}
/**
* Declares the configuration fragments that are required by this rule for the target
* configuration.
*
* <p>In contrast to {@link #requiresConfigurationFragments(Class...)}, this method takes the
* Starlark module names of fragments instead of their classes.
*/
public Builder requiresConfigurationFragmentsByStarlarkModuleName(
Collection<String> configurationFragmentNames) {
configurationFragmentPolicy.requiresConfigurationFragmentsByStarlarkBuiltinName(
configurationFragmentNames);
return this;
}
/**
* Declares the configuration fragments that are required by this rule for the host
* configuration.
*/
/**
* Declares that the implementation of the associated rule class requires the given fragments to
* be present in the given configuration that isn't the rule's configuration but is also
* readable by the rule.
*
* <p>In contrast to {@link #requiresConfigurationFragments(ConfigurationTransition, Class...)},
* this method takes Starlark module names of fragments instead of their classes. *
*
* <p>You probably don't want to use this, because rules generally shouldn't read configurations
* other than their own. If you want to declare host config fragments, see {@link
* com.google.devtools.build.lib.analysis.config.ConfigAwareRuleClassBuilder}.
*
* <p>The value is inherited by subclasses.
*/
public Builder requiresConfigurationFragmentsByStarlarkModuleName(
ConfigurationTransition transition, Collection<String> configurationFragmentNames) {
configurationFragmentPolicy.requiresConfigurationFragmentsByStarlarkBuiltinName(
transition, configurationFragmentNames);
return this;
}
/** Sets the Starlark call stack associated with this rule class's creation. */
public Builder setCallStack(ImmutableList<StarlarkThread.CallStackEntry> callstack) {
this.callstack = callstack;
return this;
}