blob: c30ab1e0ef0f644f9450a721dc2a5ad5c254e91a [file] [log] [blame]
cparsons5d85e752018-06-26 13:47:28 -07001// Copyright 2018 The Bazel Authors. All rights reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15package com.google.devtools.build.skydoc.fakebuildapi;
16
cparsonsb68bf022018-10-17 10:33:40 -070017import com.google.common.collect.ImmutableList;
cparsons11c9f202018-07-11 10:30:02 -070018import com.google.common.collect.ImmutableMap;
cparsons5d85e752018-06-26 13:47:28 -070019import com.google.devtools.build.lib.cmdline.Label;
cparsons17710542018-07-13 15:23:46 -070020import com.google.devtools.build.lib.cmdline.LabelSyntaxException;
cparsons5d85e752018-06-26 13:47:28 -070021import com.google.devtools.build.lib.events.Location;
22import com.google.devtools.build.lib.skylarkbuildapi.FileApi;
cparsons5d85e752018-06-26 13:47:28 -070023import com.google.devtools.build.lib.skylarkbuildapi.ProviderApi;
24import com.google.devtools.build.lib.skylarkbuildapi.SkylarkAspectApi;
cparsons5d85e752018-06-26 13:47:28 -070025import com.google.devtools.build.lib.skylarkbuildapi.SkylarkRuleFunctionsApi;
cparsonsb380dc92018-12-05 13:57:39 -080026import com.google.devtools.build.lib.skylarkinterface.StarlarkContext;
cparsons5d85e752018-06-26 13:47:28 -070027import com.google.devtools.build.lib.syntax.BaseFunction;
28import com.google.devtools.build.lib.syntax.Environment;
29import com.google.devtools.build.lib.syntax.EvalException;
30import com.google.devtools.build.lib.syntax.FuncallExpression;
cparsonsd790ce42018-06-27 15:29:02 -070031import com.google.devtools.build.lib.syntax.Runtime;
cparsons5d85e752018-06-26 13:47:28 -070032import com.google.devtools.build.lib.syntax.SkylarkDict;
33import com.google.devtools.build.lib.syntax.SkylarkList;
cparsonsb68bf022018-10-17 10:33:40 -070034import com.google.devtools.build.lib.syntax.SkylarkType;
kendalllane3da15682019-07-24 16:02:17 -070035import com.google.devtools.build.skydoc.rendering.AspectInfoWrapper;
blossomsm25d27482019-06-11 10:47:18 -070036import com.google.devtools.build.skydoc.rendering.ProviderInfoWrapper;
kendalllanee536b142019-06-10 13:12:43 -070037import com.google.devtools.build.skydoc.rendering.RuleInfoWrapper;
kendalllane3da15682019-07-24 16:02:17 -070038import com.google.devtools.build.skydoc.rendering.proto.StardocOutputProtos.AspectInfo;
cparsons5ece6502019-04-17 10:19:41 -070039import com.google.devtools.build.skydoc.rendering.proto.StardocOutputProtos.AttributeInfo;
40import com.google.devtools.build.skydoc.rendering.proto.StardocOutputProtos.AttributeType;
blossomsmf644c1e2019-06-06 12:52:11 -070041import com.google.devtools.build.skydoc.rendering.proto.StardocOutputProtos.ProviderFieldInfo;
blossomsm25d27482019-06-11 10:47:18 -070042import com.google.devtools.build.skydoc.rendering.proto.StardocOutputProtos.ProviderInfo;
kendalllanee536b142019-06-10 13:12:43 -070043import com.google.devtools.build.skydoc.rendering.proto.StardocOutputProtos.RuleInfo;
kendalllane3da15682019-07-24 16:02:17 -070044import java.util.ArrayList;
blossomsm25d27482019-06-11 10:47:18 -070045import java.util.Collection;
cparsons11c9f202018-07-11 10:30:02 -070046import java.util.Comparator;
cparsons5d85e752018-06-26 13:47:28 -070047import java.util.List;
cparsonsb68bf022018-10-17 10:33:40 -070048import java.util.Map;
cparsonsfc0e52f2018-07-09 11:06:57 -070049import java.util.stream.Collectors;
nharmataaaff22c2019-08-02 11:59:26 -070050import javax.annotation.Nullable;
cparsons5d85e752018-06-26 13:47:28 -070051
52/**
53 * Fake implementation of {@link SkylarkRuleFunctionsApi}.
54 *
cparsonsd790ce42018-06-27 15:29:02 -070055 * <p>This fake hooks into the global {@code rule()} function, adding descriptors of calls of that
56 * function to a {@link List} given in the class constructor.</p>
cparsons5d85e752018-06-26 13:47:28 -070057 */
58public class FakeSkylarkRuleFunctionsApi implements SkylarkRuleFunctionsApi<FileApi> {
59
cparsons11c9f202018-07-11 10:30:02 -070060 private static final FakeDescriptor IMPLICIT_NAME_ATTRIBUTE_DESCRIPTOR =
cparsons5ece6502019-04-17 10:19:41 -070061 new FakeDescriptor(AttributeType.NAME, "A unique name for this target.", true);
kendalllanee536b142019-06-10 13:12:43 -070062 private final List<RuleInfoWrapper> ruleInfoList;
blossomsm25d27482019-06-11 10:47:18 -070063
64 private final List<ProviderInfoWrapper> providerInfoList;
cparsons5d85e752018-06-26 13:47:28 -070065
kendalllane3da15682019-07-24 16:02:17 -070066 private final List<AspectInfoWrapper> aspectInfoList;
67
cparsons5d85e752018-06-26 13:47:28 -070068 /**
69 * Constructor.
70 *
71 * @param ruleInfoList the list of {@link RuleInfo} objects to which rule() invocation information
72 * will be added
kendalllanee536b142019-06-10 13:12:43 -070073 * @param providerInfoList the list of {@link ProviderInfo} objects to which provider() invocation
74 * information will be added
kendalllane3da15682019-07-24 16:02:17 -070075 * @param aspectInfoList the list of {@link AspectInfo} objects to which aspect() invocation
76 * information will be added
cparsons5d85e752018-06-26 13:47:28 -070077 */
kendalllanee536b142019-06-10 13:12:43 -070078 public FakeSkylarkRuleFunctionsApi(
kendalllane3da15682019-07-24 16:02:17 -070079 List<RuleInfoWrapper> ruleInfoList,
80 List<ProviderInfoWrapper> providerInfoList,
81 List<AspectInfoWrapper> aspectInfoList) {
cparsons5d85e752018-06-26 13:47:28 -070082 this.ruleInfoList = ruleInfoList;
cparsonsb68bf022018-10-17 10:33:40 -070083 this.providerInfoList = providerInfoList;
kendalllane3da15682019-07-24 16:02:17 -070084 this.aspectInfoList = aspectInfoList;
cparsons5d85e752018-06-26 13:47:28 -070085 }
86
87 @Override
nharmatabbf64732019-08-02 09:16:57 -070088 public ProviderApi provider(String doc, Object fields, Location location) throws EvalException {
cparsonsb68bf022018-10-17 10:33:40 -070089 FakeProviderApi fakeProvider = new FakeProviderApi();
90 // Field documentation will be output preserving the order in which the fields are listed.
91 ImmutableList.Builder<ProviderFieldInfo> providerFieldInfos = ImmutableList.builder();
92 if (fields instanceof SkylarkList) {
93 @SuppressWarnings("unchecked")
94 SkylarkList<String> fieldNames = (SkylarkList<String>)
95 SkylarkType.cast(
96 fields,
97 SkylarkList.class, String.class, location,
98 "Expected list of strings or dictionary of string -> string for 'fields'");
99 for (String fieldName : fieldNames) {
blossomsmf644c1e2019-06-06 12:52:11 -0700100 providerFieldInfos.add(asProviderFieldInfo(fieldName, "(Undocumented)"));
cparsonsb68bf022018-10-17 10:33:40 -0700101 }
102 } else if (fields instanceof SkylarkDict) {
103 Map<String, String> dict = SkylarkType.castMap(
104 fields,
105 String.class, String.class,
106 "Expected list of strings or dictionary of string -> string for 'fields'");
107 for (Map.Entry<String, String> fieldEntry : dict.entrySet()) {
blossomsmf644c1e2019-06-06 12:52:11 -0700108 providerFieldInfos.add(asProviderFieldInfo(fieldEntry.getKey(), fieldEntry.getValue()));
cparsonsb68bf022018-10-17 10:33:40 -0700109 }
110 } else {
111 // fields is NONE, so there is no field information to add.
112 }
blossomsm25d27482019-06-11 10:47:18 -0700113 providerInfoList.add(forProviderInfo(fakeProvider, doc, providerFieldInfos.build()));
cparsonsb68bf022018-10-17 10:33:40 -0700114 return fakeProvider;
cparsons5d85e752018-06-26 13:47:28 -0700115 }
116
blossomsmf644c1e2019-06-06 12:52:11 -0700117 /** Constructor for ProviderFieldInfo. */
118 public ProviderFieldInfo asProviderFieldInfo(String name, String docString) {
119 return ProviderFieldInfo.newBuilder().setName(name).setDocString(docString).build();
120 }
121
blossomsm25d27482019-06-11 10:47:18 -0700122 /** Constructor for ProviderInfoWrapper. */
123 public ProviderInfoWrapper forProviderInfo(
124 BaseFunction identifier, String docString, Collection<ProviderFieldInfo> fieldInfos) {
125 return new ProviderInfoWrapper(identifier, docString, fieldInfos);
126 }
127
cparsons5d85e752018-06-26 13:47:28 -0700128 @Override
cparsons55781c92018-10-17 12:03:08 -0700129 public BaseFunction rule(
130 BaseFunction implementation,
131 Boolean test,
132 Object attrs,
133 Object implicitOutputs,
134 Boolean executable,
135 Boolean outputToGenfiles,
136 SkylarkList<?> fragments,
137 SkylarkList<?> hostFragments,
138 Boolean skylarkTestable,
139 SkylarkList<?> toolchains,
140 String doc,
141 SkylarkList<?> providesArg,
142 Boolean executionPlatformConstraintsAllowed,
143 SkylarkList<?> execCompatibleWith,
144 Object analysisTest,
juliexxia1f332e02018-10-31 14:20:55 -0700145 Object buildSetting,
juliexxiadeb028e2019-01-05 17:19:21 -0800146 Object cfg,
cparsons55781c92018-10-17 12:03:08 -0700147 FuncallExpression ast,
cparsonsb380dc92018-12-05 13:57:39 -0800148 Environment funcallEnv,
149 StarlarkContext context)
cparsons55781c92018-10-17 12:03:08 -0700150 throws EvalException {
cparsons11c9f202018-07-11 10:30:02 -0700151 ImmutableMap.Builder<String, FakeDescriptor> attrsMapBuilder = ImmutableMap.builder();
cparsonsd790ce42018-06-27 15:29:02 -0700152 if (attrs != null && attrs != Runtime.NONE) {
cparsons5d85e752018-06-26 13:47:28 -0700153 SkylarkDict<?, ?> attrsDict = (SkylarkDict<?, ?>) attrs;
cparsons11c9f202018-07-11 10:30:02 -0700154 attrsMapBuilder.putAll(attrsDict.getContents(String.class, FakeDescriptor.class, "attrs"));
cparsons5d85e752018-06-26 13:47:28 -0700155 }
156
cparsons11c9f202018-07-11 10:30:02 -0700157 attrsMapBuilder.put("name", IMPLICIT_NAME_ATTRIBUTE_DESCRIPTOR);
kendalllane3da15682019-07-24 16:02:17 -0700158 List<AttributeInfo> attrInfos =
cparsons5ece6502019-04-17 10:19:41 -0700159 attrsMapBuilder.build().entrySet().stream()
160 .filter(entry -> !entry.getKey().startsWith("_"))
161 .map(entry -> entry.getValue().asAttributeInfo(entry.getKey()))
162 .collect(Collectors.toList());
cparsons11c9f202018-07-11 10:30:02 -0700163 attrInfos.sort(new AttributeNameComparator());
164
cparsonsd790ce42018-06-27 15:29:02 -0700165 RuleDefinitionIdentifier functionIdentifier = new RuleDefinitionIdentifier();
cparsonsfc0e52f2018-07-09 11:06:57 -0700166
kendalllane26e94c92019-06-27 07:26:57 -0700167 // Only the Builder is passed to RuleInfoWrapper as the rule name is not yet available.
168 RuleInfo.Builder ruleInfo = RuleInfo.newBuilder().setDocString(doc).addAllAttribute(attrInfos);
kendalllanee536b142019-06-10 13:12:43 -0700169
170 ruleInfoList.add(new RuleInfoWrapper(functionIdentifier, ast.getLocation(), ruleInfo));
171
cparsonsd790ce42018-06-27 15:29:02 -0700172 return functionIdentifier;
cparsons5d85e752018-06-26 13:47:28 -0700173 }
174
175 @Override
dannarkfbfd2c02019-01-14 20:51:39 -0800176 public Label label(
177 String labelString,
178 Boolean relativeToCallerRepository,
179 Location loc,
180 Environment env,
181 StarlarkContext context)
182 throws EvalException {
cparsons17710542018-07-13 15:23:46 -0700183 try {
184 return Label.parseAbsolute(
185 labelString,
186 /* defaultToMain= */ false,
187 /* repositoryMapping= */ ImmutableMap.of());
188 } catch (LabelSyntaxException e) {
189 throw new EvalException(loc, "Illegal absolute label syntax: " + labelString);
190 }
cparsons5d85e752018-06-26 13:47:28 -0700191 }
192
193 @Override
cparsonsdac21352019-05-03 11:41:17 -0700194 public SkylarkAspectApi aspect(BaseFunction implementation, SkylarkList<?> attributeAspects,
195 Object attrs, SkylarkList<?> requiredAspectProvidersArg, SkylarkList<?> providesArg,
196 SkylarkList<?> fragments, SkylarkList<?> hostFragments, SkylarkList<?> toolchains, String doc,
197 FuncallExpression ast, Environment funcallEnv) throws EvalException {
kendalllane3da15682019-07-24 16:02:17 -0700198 FakeSkylarkAspect fakeAspect = new FakeSkylarkAspect();
199 ImmutableMap.Builder<String, FakeDescriptor> attrsMapBuilder = ImmutableMap.builder();
200 if (attrs != null && attrs != Runtime.NONE) {
201 SkylarkDict<?, ?> attrsDict = (SkylarkDict<?, ?>) attrs;
202 attrsMapBuilder.putAll(attrsDict.getContents(String.class, FakeDescriptor.class, "attrs"));
203 }
204
205 attrsMapBuilder.put("name", IMPLICIT_NAME_ATTRIBUTE_DESCRIPTOR);
206 List<AttributeInfo> attrInfos =
207 attrsMapBuilder.build().entrySet().stream()
208 .filter(entry -> !entry.getKey().startsWith("_"))
209 .map(entry -> entry.getValue().asAttributeInfo(entry.getKey()))
210 .collect(Collectors.toList());
211 attrInfos.sort(new AttributeNameComparator());
212
213 List<String> aspectAttrs = new ArrayList<>();
214 if (attributeAspects != null) {
215 aspectAttrs = attributeAspects.getContents(String.class, "aspectAttrs");
216 }
217
218 aspectAttrs =
219 aspectAttrs.stream().filter(entry -> !entry.startsWith("_")).collect(Collectors.toList());
220
221 // Only the Builder is passed to AspectInfoWrapper as the aspect name is not yet available.
222 AspectInfo.Builder aspectInfo =
223 AspectInfo.newBuilder()
224 .setDocString(doc)
225 .addAllAttribute(attrInfos)
226 .addAllAspectAttribute(aspectAttrs);
227
228 aspectInfoList.add(new AspectInfoWrapper(fakeAspect, ast.getLocation(), aspectInfo));
229
230 return fakeAspect;
cparsons5d85e752018-06-26 13:47:28 -0700231 }
cparsonsd790ce42018-06-27 15:29:02 -0700232
233 /**
234 * A fake {@link BaseFunction} implementation which serves as an identifier for a rule definition.
235 * A skylark invocation of 'rule()' should spawn a unique instance of this class and return it.
236 * Thus, skylark code such as 'foo = rule()' will result in 'foo' being assigned to a unique
237 * identifier, which can later be matched to a registered rule() invocation saved by the fake
238 * build API implementation.
239 */
240 private static class RuleDefinitionIdentifier extends BaseFunction {
241
cparsons93adb102018-07-11 11:44:17 -0700242 private static int idCounter = 0;
243
cparsonsd790ce42018-06-27 15:29:02 -0700244 public RuleDefinitionIdentifier() {
cparsons93adb102018-07-11 11:44:17 -0700245 super("RuleDefinitionIdentifier" + idCounter++);
cparsonsd790ce42018-06-27 15:29:02 -0700246 }
nharmataaaff22c2019-08-02 11:59:26 -0700247
248 @Override
249 public boolean equals(@Nullable Object other) {
250 // Use exact object matching.
251 return this == other;
252 }
cparsonsd790ce42018-06-27 15:29:02 -0700253 }
cparsons11c9f202018-07-11 10:30:02 -0700254
255 /**
256 * A comparator for {@link AttributeInfo} objects which sorts by attribute name alphabetically,
257 * except that any attribute named "name" is placed first.
258 */
Klaus Aehlig547aa482019-03-18 11:44:11 -0700259 public static class AttributeNameComparator implements Comparator<AttributeInfo> {
cparsons11c9f202018-07-11 10:30:02 -0700260
261 @Override
262 public int compare(AttributeInfo o1, AttributeInfo o2) {
263 if (o1.getName().equals("name")) {
264 return o2.getName().equals("name") ? 0 : -1;
265 } else if (o2.getName().equals("name")) {
266 return 1;
267 } else {
268 return o1.getName().compareTo(o2.getName());
269 }
270 }
271 }
cparsons5d85e752018-06-26 13:47:28 -0700272}