forked from bazelbuild/bazel
-
Notifications
You must be signed in to change notification settings - Fork 0
/
FakeStarlarkRuleFunctionsApi.java
287 lines (257 loc) · 11.1 KB
/
FakeStarlarkRuleFunctionsApi.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
// 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.skydoc.fakebuildapi;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.devtools.build.lib.cmdline.Label;
import com.google.devtools.build.lib.cmdline.LabelSyntaxException;
import com.google.devtools.build.lib.starlarkbuildapi.ExecGroupApi;
import com.google.devtools.build.lib.starlarkbuildapi.FileApi;
import com.google.devtools.build.lib.starlarkbuildapi.StarlarkAspectApi;
import com.google.devtools.build.lib.starlarkbuildapi.StarlarkRuleFunctionsApi;
import com.google.devtools.build.lib.starlarkbuildapi.core.ProviderApi;
import com.google.devtools.build.skydoc.rendering.AspectInfoWrapper;
import com.google.devtools.build.skydoc.rendering.ProviderInfoWrapper;
import com.google.devtools.build.skydoc.rendering.RuleInfoWrapper;
import com.google.devtools.build.skydoc.rendering.proto.StardocOutputProtos.AspectInfo;
import com.google.devtools.build.skydoc.rendering.proto.StardocOutputProtos.AttributeInfo;
import com.google.devtools.build.skydoc.rendering.proto.StardocOutputProtos.AttributeType;
import com.google.devtools.build.skydoc.rendering.proto.StardocOutputProtos.ProviderFieldInfo;
import com.google.devtools.build.skydoc.rendering.proto.StardocOutputProtos.ProviderInfo;
import com.google.devtools.build.skydoc.rendering.proto.StardocOutputProtos.RuleInfo;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import net.starlark.java.eval.Dict;
import net.starlark.java.eval.EvalException;
import net.starlark.java.eval.Sequence;
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.syntax.Location;
/**
* Fake implementation of {@link StarlarkRuleFunctionsApi}.
*
* <p>This fake hooks into the global {@code rule()} function, adding descriptors of calls of that
* function to a {@link List} given in the class constructor.</p>
*/
public class FakeStarlarkRuleFunctionsApi implements StarlarkRuleFunctionsApi<FileApi> {
private static final FakeDescriptor IMPLICIT_NAME_ATTRIBUTE_DESCRIPTOR =
new FakeDescriptor(
AttributeType.NAME, "A unique name for this target.", true, ImmutableList.of(), "");
private final List<RuleInfoWrapper> ruleInfoList;
private final List<ProviderInfoWrapper> providerInfoList;
private final List<AspectInfoWrapper> aspectInfoList;
/**
* Constructor.
*
* @param ruleInfoList the list of {@link RuleInfo} objects to which rule() invocation information
* will be added
* @param providerInfoList the list of {@link ProviderInfo} objects to which provider() invocation
* information will be added
* @param aspectInfoList the list of {@link AspectInfo} objects to which aspect() invocation
* information will be added
*/
public FakeStarlarkRuleFunctionsApi(
List<RuleInfoWrapper> ruleInfoList,
List<ProviderInfoWrapper> providerInfoList,
List<AspectInfoWrapper> aspectInfoList) {
this.ruleInfoList = ruleInfoList;
this.providerInfoList = providerInfoList;
this.aspectInfoList = aspectInfoList;
}
@Override
public ProviderApi provider(String doc, Object fields, StarlarkThread thread)
throws EvalException {
FakeProviderApi fakeProvider = new FakeProviderApi(null);
// Field documentation will be output preserving the order in which the fields are listed.
ImmutableList.Builder<ProviderFieldInfo> providerFieldInfos = ImmutableList.builder();
if (fields instanceof Sequence) {
for (String name : Sequence.cast(fields, String.class, "fields")) {
providerFieldInfos.add(asProviderFieldInfo(name, "(Undocumented)"));
}
} else if (fields instanceof Dict) {
for (Map.Entry<String, String> e :
Dict.cast(fields, String.class, String.class, "fields").entrySet()) {
providerFieldInfos.add(asProviderFieldInfo(e.getKey(), e.getValue()));
}
} else {
// fields is NONE, so there is no field information to add.
}
providerInfoList.add(forProviderInfo(fakeProvider, doc, providerFieldInfos.build()));
return fakeProvider;
}
/** Constructor for ProviderFieldInfo. */
public ProviderFieldInfo asProviderFieldInfo(String name, String docString) {
return ProviderFieldInfo.newBuilder().setName(name).setDocString(docString).build();
}
/** Constructor for ProviderInfoWrapper. */
public ProviderInfoWrapper forProviderInfo(
StarlarkCallable identifier, String docString, Collection<ProviderFieldInfo> fieldInfos) {
return new ProviderInfoWrapper(identifier, docString, fieldInfos);
}
@Override
public StarlarkCallable rule(
StarlarkFunction implementation,
Boolean test,
Object attrs,
Object implicitOutputs,
Boolean executable,
Boolean outputToGenfiles,
Sequence<?> fragments,
Sequence<?> hostFragments,
Boolean starlarkTestable,
Sequence<?> toolchains,
boolean useToolchainTransition,
String doc,
Sequence<?> providesArg,
Sequence<?> execCompatibleWith,
Object analysisTest,
Object buildSetting,
Object cfg,
Object execGroups,
Object compileOneFiletype,
Object name,
StarlarkThread thread)
throws EvalException {
ImmutableMap.Builder<String, FakeDescriptor> attrsMapBuilder = ImmutableMap.builder();
if (attrs != null && attrs != Starlark.NONE) {
attrsMapBuilder.putAll(Dict.cast(attrs, String.class, FakeDescriptor.class, "attrs"));
}
attrsMapBuilder.put("name", IMPLICIT_NAME_ATTRIBUTE_DESCRIPTOR);
List<AttributeInfo> attrInfos =
attrsMapBuilder.build().entrySet().stream()
.filter(entry -> !entry.getKey().startsWith("_"))
.map(entry -> entry.getValue().asAttributeInfo(entry.getKey()))
.collect(Collectors.toList());
attrInfos.sort(new AttributeNameComparator());
RuleDefinitionIdentifier functionIdentifier = new RuleDefinitionIdentifier();
// Only the Builder is passed to RuleInfoWrapper as the rule name may not be available yet.
RuleInfo.Builder ruleInfo = RuleInfo.newBuilder().setDocString(doc).addAllAttribute(attrInfos);
if (name != Starlark.NONE) {
ruleInfo.setRuleName((String) name);
}
Location loc = thread.getCallerLocation();
ruleInfoList.add(new RuleInfoWrapper(functionIdentifier, loc, ruleInfo));
return functionIdentifier;
}
@Override
public void analysisTest(
String name,
StarlarkFunction implementation,
Object attrs,
Sequence<?> fragments,
Sequence<?> toolchains,
Object argsValue,
StarlarkThread thread)
throws EvalException, InterruptedException {}
@Override
public Label label(String labelString, StarlarkThread thread) throws EvalException {
try {
return Label.parseAbsolute(
labelString,
/* defaultToMain= */ false,
/* repositoryMapping= */ ImmutableMap.of());
} catch (LabelSyntaxException e) {
throw Starlark.errorf("Illegal absolute label syntax: %s", labelString);
}
}
@Override
public StarlarkAspectApi aspect(
StarlarkFunction implementation,
Sequence<?> attributeAspects,
Object attrs,
Sequence<?> requiredProvidersArg,
Sequence<?> requiredAspectProvidersArg,
Sequence<?> providesArg,
Sequence<?> requiredAspects,
Sequence<?> fragments,
Sequence<?> hostFragments,
Sequence<?> toolchains,
boolean useToolchainTransition,
String doc,
Boolean applyToFiles,
StarlarkThread thread)
throws EvalException {
FakeStarlarkAspect fakeAspect = new FakeStarlarkAspect();
ImmutableMap.Builder<String, FakeDescriptor> attrsMapBuilder = ImmutableMap.builder();
if (attrs != null && attrs != Starlark.NONE) {
attrsMapBuilder.putAll(Dict.cast(attrs, String.class, FakeDescriptor.class, "attrs"));
}
attrsMapBuilder.put("name", IMPLICIT_NAME_ATTRIBUTE_DESCRIPTOR);
List<AttributeInfo> attrInfos =
attrsMapBuilder.build().entrySet().stream()
.filter(entry -> !entry.getKey().startsWith("_"))
.map(entry -> entry.getValue().asAttributeInfo(entry.getKey()))
.collect(Collectors.toList());
attrInfos.sort(new AttributeNameComparator());
List<String> aspectAttrs =
attributeAspects != null
? Sequence.cast(attributeAspects, String.class, "aspectAttrs")
: new ArrayList<>();
aspectAttrs =
aspectAttrs.stream().filter(entry -> !entry.startsWith("_")).collect(Collectors.toList());
// Only the Builder is passed to AspectInfoWrapper as the aspect name is not yet available.
AspectInfo.Builder aspectInfo =
AspectInfo.newBuilder()
.setDocString(doc)
.addAllAttribute(attrInfos)
.addAllAspectAttribute(aspectAttrs);
aspectInfoList.add(new AspectInfoWrapper(fakeAspect, thread.getCallerLocation(), aspectInfo));
return fakeAspect;
}
@Override
public ExecGroupApi execGroup(
Sequence<?> execCompatibleWith,
Sequence<?> toolchains,
Boolean copyFromRule,
StarlarkThread thread) {
return new FakeExecGroup();
}
/**
* A fake {@link StarlarkCallable} implementation which serves as an identifier for a rule
* definition. A Starlark invocation of 'rule()' should spawn a unique instance of this class and
* return it. Thus, Starlark code such as 'foo = rule()' will result in 'foo' being assigned to a
* unique identifier, which can later be matched to a registered rule() invocation saved by the
* fake build API implementation.
*/
private static class RuleDefinitionIdentifier implements StarlarkCallable {
private static int idCounter = 0;
private final String name = "RuleDefinitionIdentifier" + idCounter++;
@Override
public String getName() {
return name;
}
}
/**
* A comparator for {@link AttributeInfo} objects which sorts by attribute name alphabetically,
* except that any attribute named "name" is placed first.
*/
public static class AttributeNameComparator implements Comparator<AttributeInfo> {
@Override
public int compare(AttributeInfo o1, AttributeInfo o2) {
if (o1.getName().equals("name")) {
return o2.getName().equals("name") ? 0 : -1;
} else if (o2.getName().equals("name")) {
return 1;
} else {
return o1.getName().compareTo(o2.getName());
}
}
}
}