forked from bazelbuild/bazel
-
Notifications
You must be signed in to change notification settings - Fork 0
/
BazelBuildApiGlobals.java
149 lines (140 loc) · 6.84 KB
/
BazelBuildApiGlobals.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
// Copyright 2018 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.starlark;
import com.google.common.collect.ImmutableList;
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.packages.BazelStarlarkContext;
import com.google.devtools.build.lib.packages.BzlInitThreadContext;
import com.google.devtools.build.lib.packages.BzlVisibility;
import com.google.devtools.build.lib.packages.PackageSpecification;
import com.google.devtools.build.lib.packages.semantics.BuildLanguageOptions;
import com.google.devtools.build.lib.starlarkbuildapi.StarlarkBuildApiGlobals;
import java.util.List;
import net.starlark.java.eval.EvalException;
import net.starlark.java.eval.Sequence;
import net.starlark.java.eval.Starlark;
import net.starlark.java.eval.StarlarkList;
import net.starlark.java.eval.StarlarkThread;
/**
* Bazel implementation of {@link StarlarkBuildApiGlobals}: a collection of global Starlark build
* API functions that belong in the global namespace.
*/
// TODO(brandjon): This should probably be refactored into a StarlarkLibrary#BZL field, analogous to
// StarlarkLibrary#COMMON and StarlarkLibrary#BUILD.
public class BazelBuildApiGlobals implements StarlarkBuildApiGlobals {
@Override
public void visibility(Object value, StarlarkThread thread) throws EvalException {
// Manually check the experimental flag because enableOnlyWithFlag doesn't work for top-level
// builtins.
if (!thread.getSemantics().getBool(BuildLanguageOptions.EXPERIMENTAL_BZL_VISIBILITY)) {
throw Starlark.errorf("Use of `visibility()` requires --experimental_bzl_visibility");
}
// Fail if we're not initializing a .bzl module, or if that .bzl module isn't on the
// experimental allowlist, or if visibility is already set.
BzlInitThreadContext context = BzlInitThreadContext.fromOrFailFunction(thread, "visibility");
PackageIdentifier pkgId = context.getBzlFile().getPackageIdentifier();
List<String> allowlist =
thread.getSemantics().get(BuildLanguageOptions.EXPERIMENTAL_BZL_VISIBILITY_ALLOWLIST);
checkVisibilityAllowlist(pkgId, allowlist);
if (context.getBzlVisibility() != null) {
throw Starlark.errorf(".bzl visibility may not be set more than once");
}
RepositoryName repo = context.getBzlFile().getRepository();
ImmutableList<PackageSpecification> specs;
if (value instanceof String) {
// `visibility("public")`, `visibility("private")`, visibility("//pkg")
specs =
ImmutableList.of(PackageSpecification.fromStringForBzlVisibility(repo, (String) value));
} else if (value instanceof StarlarkList) {
// `visibility(["//pkg1", "//pkg2", ...])`
List<String> specStrings = Sequence.cast(value, String.class, "visibility list");
ImmutableList.Builder<PackageSpecification> specsBuilder =
ImmutableList.builderWithExpectedSize(specStrings.size());
for (String specString : specStrings) {
PackageSpecification spec =
PackageSpecification.fromStringForBzlVisibility(repo, specString);
specsBuilder.add(spec);
}
specs = specsBuilder.build();
} else {
throw Starlark.errorf(
"Invalid bzl-visibility: got '%s', want string or list of strings", Starlark.type(value));
}
context.setBzlVisibility(BzlVisibility.of(specs));
}
private void checkVisibilityAllowlist(PackageIdentifier pkgId, List<String> allowlist)
throws EvalException {
// The allowlist is represented as a list of strings because BuildLanguageOptions isn't allowed
// to depend on Label, PackageIdentifier, etc. For simplicity we just convert the strings to
// PackageIdentifiers here, at linear cost and redundantly for each call to `visibility()`. This
// is ok because the allowlist is not intended to stay permanent, it is expected to remain
// small, and calls to visibility() are relatively infrequent.
boolean foundMatch = false;
for (String allowedPkgString : allowlist) {
// Special constant to disable allowlisting. For migration to enable the feature globally.
if (allowedPkgString.equals("everyone")) {
foundMatch = true;
break;
}
// The wildcard syntax /... is not valid for PackageIdentifiers, so we extract it first.
boolean allBeneath = allowedPkgString.endsWith("/...");
if (allBeneath) {
allowedPkgString = allowedPkgString.substring(0, allowedPkgString.length() - 4);
if (allowedPkgString.equals("/")) {
// was "//..."
allowedPkgString = "//";
}
}
PackageIdentifier allowedPkgId;
try {
// TODO(b/22193153): This seems incorrect since parse doesn't take into account any
// repository map. (This shouldn't matter within Google's monorepo, which doesn't use a repo
// map.)
allowedPkgId = PackageIdentifier.parse(allowedPkgString);
} catch (LabelSyntaxException ex) {
throw Starlark.errorf("Invalid bzl-visibility allowlist: %s", ex.getMessage());
}
if (pkgId.equals(allowedPkgId)
|| (allBeneath
// Again, we're erroneously ignoring repo.
&& pkgId.getPackageFragment().startsWith(allowedPkgId.getPackageFragment()))) {
foundMatch = true;
break;
}
}
if (!foundMatch) {
throw Starlark.errorf(
"`visibility() is not enabled for package %s; consider adding it to "
+ "--experimental_bzl_visibility_allowlist",
pkgId.getCanonicalForm());
}
}
@Override
public StarlarkLateBoundDefault<?> configurationField(
String fragment, String name, StarlarkThread thread) throws EvalException {
BazelStarlarkContext context = BazelStarlarkContext.from(thread);
Class<?> fragmentClass = context.getFragmentNameToClass().get(fragment);
if (fragmentClass == null) {
throw Starlark.errorf("invalid configuration fragment name '%s'", fragment);
}
try {
return StarlarkLateBoundDefault.forConfigurationField(
fragmentClass, name, context.getToolsRepository());
} catch (StarlarkLateBoundDefault.InvalidConfigurationFieldException exception) {
throw new EvalException(exception);
}
}
}