aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/main/java/com/google/devtools/build/lib/analysis/whitelisting/Whitelist.java
blob: 0da6e4d3621483db14d78e4c478903b3c55404b6 (plain)
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
// Copyright 2017 The Bazel Authors. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//    http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package com.google.devtools.build.lib.analysis.whitelisting;

import static com.google.devtools.build.lib.packages.Attribute.ConfigurationTransition.HOST;
import static com.google.devtools.build.lib.packages.Attribute.attr;
import static com.google.devtools.build.lib.packages.BuildType.LABEL;

import com.google.common.collect.ImmutableList;
import com.google.devtools.build.lib.analysis.PackageSpecificationProvider;
import com.google.devtools.build.lib.analysis.RuleConfiguredTarget.Mode;
import com.google.devtools.build.lib.analysis.RuleContext;
import com.google.devtools.build.lib.analysis.TransitiveInfoCollection;
import com.google.devtools.build.lib.cmdline.Label;
import com.google.devtools.build.lib.packages.Attribute;
import com.google.devtools.build.lib.util.Preconditions;

/**
 * Class used for implementing whitelists using package groups.
 *
 * <p>To use add an attribute {@link getAttributeFromWhitelistName(String,Label) to the rule class
 * which needs the whitelisting mechanism and use {@link isAvailable(RuleContext,String)} to check
 * during analysis if a rule is present
 */
public final class Whitelist {

  private Whitelist() {}

  /**
   * Returns an Attribute.Builder that can be used to add an implicit attribute to a rule containing
   * a package group whitelist.
   *
   * @param whitelistName The name of the whitelist. This has to comply with attribute naming
   *     standards and will be used as a suffix for the attribute name.
   * @param packageGroupWhitelist Label for the package group with the whitelist.
   */
  public static Attribute.Builder<Label> getAttributeFromWhitelistName(
      String whitelistName, Label packageGroupWhitelist) {
    String attributeName = getAttributeNameFromWhitelistName(whitelistName);
    return attr(attributeName, LABEL)
        .value(packageGroupWhitelist)
        .cfg(HOST)
        .mandatoryNativeProviders(ImmutableList.of(PackageSpecificationProvider.class));
  }

  /**
   * Returns whether the rule in the given RuleContext is in a whitelist.
   *
   * @param ruleContext The context in which this check is being executed.
   * @param whitelistName The name of the whitelist being used.
   */
  public static boolean isAvailable(RuleContext ruleContext, String whitelistName) {
    String attributeName = getAttributeNameFromWhitelistName(whitelistName);
    Preconditions.checkArgument(ruleContext.isAttrDefined(attributeName, LABEL));
    TransitiveInfoCollection packageGroup = ruleContext.getPrerequisite(attributeName, Mode.HOST);
    Label label = ruleContext.getLabel();
    return packageGroup
        .getProvider(PackageSpecificationProvider.class)
        .getPackageSpecifications()
        .toList()
        .stream()
        .anyMatch(p -> p.containsPackage(label.getPackageIdentifier()));
  }

  private static String getAttributeNameFromWhitelistName(String whitelistName) {
    return String.format("$whitelist_%s", whitelistName);
  }
}