cparsons | 5d85e75 | 2018-06-26 13:47:28 -0700 | [diff] [blame] | 1 | // 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 | |
| 15 | package com.google.devtools.build.skydoc.fakebuildapi; |
| 16 | |
cparsons | b68bf02 | 2018-10-17 10:33:40 -0700 | [diff] [blame] | 17 | import com.google.common.collect.ImmutableList; |
cparsons | 11c9f20 | 2018-07-11 10:30:02 -0700 | [diff] [blame] | 18 | import com.google.common.collect.ImmutableMap; |
cparsons | 5d85e75 | 2018-06-26 13:47:28 -0700 | [diff] [blame] | 19 | import com.google.devtools.build.lib.cmdline.Label; |
cparsons | 1771054 | 2018-07-13 15:23:46 -0700 | [diff] [blame] | 20 | import com.google.devtools.build.lib.cmdline.LabelSyntaxException; |
cparsons | 5d85e75 | 2018-06-26 13:47:28 -0700 | [diff] [blame] | 21 | import com.google.devtools.build.lib.events.Location; |
| 22 | import com.google.devtools.build.lib.skylarkbuildapi.FileApi; |
cparsons | 5d85e75 | 2018-06-26 13:47:28 -0700 | [diff] [blame] | 23 | import com.google.devtools.build.lib.skylarkbuildapi.ProviderApi; |
| 24 | import com.google.devtools.build.lib.skylarkbuildapi.SkylarkAspectApi; |
cparsons | 5d85e75 | 2018-06-26 13:47:28 -0700 | [diff] [blame] | 25 | import com.google.devtools.build.lib.skylarkbuildapi.SkylarkRuleFunctionsApi; |
cparsons | b380dc9 | 2018-12-05 13:57:39 -0800 | [diff] [blame] | 26 | import com.google.devtools.build.lib.skylarkinterface.StarlarkContext; |
cparsons | 5d85e75 | 2018-06-26 13:47:28 -0700 | [diff] [blame] | 27 | import com.google.devtools.build.lib.syntax.BaseFunction; |
| 28 | import com.google.devtools.build.lib.syntax.Environment; |
| 29 | import com.google.devtools.build.lib.syntax.EvalException; |
| 30 | import com.google.devtools.build.lib.syntax.FuncallExpression; |
cparsons | d790ce4 | 2018-06-27 15:29:02 -0700 | [diff] [blame] | 31 | import com.google.devtools.build.lib.syntax.Runtime; |
cparsons | 5d85e75 | 2018-06-26 13:47:28 -0700 | [diff] [blame] | 32 | import com.google.devtools.build.lib.syntax.SkylarkDict; |
| 33 | import com.google.devtools.build.lib.syntax.SkylarkList; |
cparsons | b68bf02 | 2018-10-17 10:33:40 -0700 | [diff] [blame] | 34 | import com.google.devtools.build.lib.syntax.SkylarkType; |
kendalllane | 3da1568 | 2019-07-24 16:02:17 -0700 | [diff] [blame] | 35 | import com.google.devtools.build.skydoc.rendering.AspectInfoWrapper; |
blossomsm | 25d2748 | 2019-06-11 10:47:18 -0700 | [diff] [blame] | 36 | import com.google.devtools.build.skydoc.rendering.ProviderInfoWrapper; |
kendalllane | e536b14 | 2019-06-10 13:12:43 -0700 | [diff] [blame] | 37 | import com.google.devtools.build.skydoc.rendering.RuleInfoWrapper; |
kendalllane | 3da1568 | 2019-07-24 16:02:17 -0700 | [diff] [blame] | 38 | import com.google.devtools.build.skydoc.rendering.proto.StardocOutputProtos.AspectInfo; |
cparsons | 5ece650 | 2019-04-17 10:19:41 -0700 | [diff] [blame] | 39 | import com.google.devtools.build.skydoc.rendering.proto.StardocOutputProtos.AttributeInfo; |
| 40 | import com.google.devtools.build.skydoc.rendering.proto.StardocOutputProtos.AttributeType; |
blossomsm | f644c1e | 2019-06-06 12:52:11 -0700 | [diff] [blame] | 41 | import com.google.devtools.build.skydoc.rendering.proto.StardocOutputProtos.ProviderFieldInfo; |
blossomsm | 25d2748 | 2019-06-11 10:47:18 -0700 | [diff] [blame] | 42 | import com.google.devtools.build.skydoc.rendering.proto.StardocOutputProtos.ProviderInfo; |
kendalllane | e536b14 | 2019-06-10 13:12:43 -0700 | [diff] [blame] | 43 | import com.google.devtools.build.skydoc.rendering.proto.StardocOutputProtos.RuleInfo; |
kendalllane | 3da1568 | 2019-07-24 16:02:17 -0700 | [diff] [blame] | 44 | import java.util.ArrayList; |
blossomsm | 25d2748 | 2019-06-11 10:47:18 -0700 | [diff] [blame] | 45 | import java.util.Collection; |
cparsons | 11c9f20 | 2018-07-11 10:30:02 -0700 | [diff] [blame] | 46 | import java.util.Comparator; |
cparsons | 5d85e75 | 2018-06-26 13:47:28 -0700 | [diff] [blame] | 47 | import java.util.List; |
cparsons | b68bf02 | 2018-10-17 10:33:40 -0700 | [diff] [blame] | 48 | import java.util.Map; |
cparsons | fc0e52f | 2018-07-09 11:06:57 -0700 | [diff] [blame] | 49 | import java.util.stream.Collectors; |
nharmata | aaff22c | 2019-08-02 11:59:26 -0700 | [diff] [blame] | 50 | import javax.annotation.Nullable; |
cparsons | 5d85e75 | 2018-06-26 13:47:28 -0700 | [diff] [blame] | 51 | |
| 52 | /** |
| 53 | * Fake implementation of {@link SkylarkRuleFunctionsApi}. |
| 54 | * |
cparsons | d790ce4 | 2018-06-27 15:29:02 -0700 | [diff] [blame] | 55 | * <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> |
cparsons | 5d85e75 | 2018-06-26 13:47:28 -0700 | [diff] [blame] | 57 | */ |
| 58 | public class FakeSkylarkRuleFunctionsApi implements SkylarkRuleFunctionsApi<FileApi> { |
| 59 | |
cparsons | 11c9f20 | 2018-07-11 10:30:02 -0700 | [diff] [blame] | 60 | private static final FakeDescriptor IMPLICIT_NAME_ATTRIBUTE_DESCRIPTOR = |
cparsons | 5ece650 | 2019-04-17 10:19:41 -0700 | [diff] [blame] | 61 | new FakeDescriptor(AttributeType.NAME, "A unique name for this target.", true); |
kendalllane | e536b14 | 2019-06-10 13:12:43 -0700 | [diff] [blame] | 62 | private final List<RuleInfoWrapper> ruleInfoList; |
blossomsm | 25d2748 | 2019-06-11 10:47:18 -0700 | [diff] [blame] | 63 | |
| 64 | private final List<ProviderInfoWrapper> providerInfoList; |
cparsons | 5d85e75 | 2018-06-26 13:47:28 -0700 | [diff] [blame] | 65 | |
kendalllane | 3da1568 | 2019-07-24 16:02:17 -0700 | [diff] [blame] | 66 | private final List<AspectInfoWrapper> aspectInfoList; |
| 67 | |
cparsons | 5d85e75 | 2018-06-26 13:47:28 -0700 | [diff] [blame] | 68 | /** |
| 69 | * Constructor. |
| 70 | * |
| 71 | * @param ruleInfoList the list of {@link RuleInfo} objects to which rule() invocation information |
| 72 | * will be added |
kendalllane | e536b14 | 2019-06-10 13:12:43 -0700 | [diff] [blame] | 73 | * @param providerInfoList the list of {@link ProviderInfo} objects to which provider() invocation |
| 74 | * information will be added |
kendalllane | 3da1568 | 2019-07-24 16:02:17 -0700 | [diff] [blame] | 75 | * @param aspectInfoList the list of {@link AspectInfo} objects to which aspect() invocation |
| 76 | * information will be added |
cparsons | 5d85e75 | 2018-06-26 13:47:28 -0700 | [diff] [blame] | 77 | */ |
kendalllane | e536b14 | 2019-06-10 13:12:43 -0700 | [diff] [blame] | 78 | public FakeSkylarkRuleFunctionsApi( |
kendalllane | 3da1568 | 2019-07-24 16:02:17 -0700 | [diff] [blame] | 79 | List<RuleInfoWrapper> ruleInfoList, |
| 80 | List<ProviderInfoWrapper> providerInfoList, |
| 81 | List<AspectInfoWrapper> aspectInfoList) { |
cparsons | 5d85e75 | 2018-06-26 13:47:28 -0700 | [diff] [blame] | 82 | this.ruleInfoList = ruleInfoList; |
cparsons | b68bf02 | 2018-10-17 10:33:40 -0700 | [diff] [blame] | 83 | this.providerInfoList = providerInfoList; |
kendalllane | 3da1568 | 2019-07-24 16:02:17 -0700 | [diff] [blame] | 84 | this.aspectInfoList = aspectInfoList; |
cparsons | 5d85e75 | 2018-06-26 13:47:28 -0700 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | @Override |
nharmata | bbf6473 | 2019-08-02 09:16:57 -0700 | [diff] [blame] | 88 | public ProviderApi provider(String doc, Object fields, Location location) throws EvalException { |
cparsons | b68bf02 | 2018-10-17 10:33:40 -0700 | [diff] [blame] | 89 | 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) { |
blossomsm | f644c1e | 2019-06-06 12:52:11 -0700 | [diff] [blame] | 100 | providerFieldInfos.add(asProviderFieldInfo(fieldName, "(Undocumented)")); |
cparsons | b68bf02 | 2018-10-17 10:33:40 -0700 | [diff] [blame] | 101 | } |
| 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()) { |
blossomsm | f644c1e | 2019-06-06 12:52:11 -0700 | [diff] [blame] | 108 | providerFieldInfos.add(asProviderFieldInfo(fieldEntry.getKey(), fieldEntry.getValue())); |
cparsons | b68bf02 | 2018-10-17 10:33:40 -0700 | [diff] [blame] | 109 | } |
| 110 | } else { |
| 111 | // fields is NONE, so there is no field information to add. |
| 112 | } |
blossomsm | 25d2748 | 2019-06-11 10:47:18 -0700 | [diff] [blame] | 113 | providerInfoList.add(forProviderInfo(fakeProvider, doc, providerFieldInfos.build())); |
cparsons | b68bf02 | 2018-10-17 10:33:40 -0700 | [diff] [blame] | 114 | return fakeProvider; |
cparsons | 5d85e75 | 2018-06-26 13:47:28 -0700 | [diff] [blame] | 115 | } |
| 116 | |
blossomsm | f644c1e | 2019-06-06 12:52:11 -0700 | [diff] [blame] | 117 | /** Constructor for ProviderFieldInfo. */ |
| 118 | public ProviderFieldInfo asProviderFieldInfo(String name, String docString) { |
| 119 | return ProviderFieldInfo.newBuilder().setName(name).setDocString(docString).build(); |
| 120 | } |
| 121 | |
blossomsm | 25d2748 | 2019-06-11 10:47:18 -0700 | [diff] [blame] | 122 | /** Constructor for ProviderInfoWrapper. */ |
| 123 | public ProviderInfoWrapper forProviderInfo( |
| 124 | BaseFunction identifier, String docString, Collection<ProviderFieldInfo> fieldInfos) { |
| 125 | return new ProviderInfoWrapper(identifier, docString, fieldInfos); |
| 126 | } |
| 127 | |
cparsons | 5d85e75 | 2018-06-26 13:47:28 -0700 | [diff] [blame] | 128 | @Override |
cparsons | 55781c9 | 2018-10-17 12:03:08 -0700 | [diff] [blame] | 129 | 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, |
juliexxia | 1f332e0 | 2018-10-31 14:20:55 -0700 | [diff] [blame] | 145 | Object buildSetting, |
juliexxia | deb028e | 2019-01-05 17:19:21 -0800 | [diff] [blame] | 146 | Object cfg, |
cparsons | 55781c9 | 2018-10-17 12:03:08 -0700 | [diff] [blame] | 147 | FuncallExpression ast, |
cparsons | b380dc9 | 2018-12-05 13:57:39 -0800 | [diff] [blame] | 148 | Environment funcallEnv, |
| 149 | StarlarkContext context) |
cparsons | 55781c9 | 2018-10-17 12:03:08 -0700 | [diff] [blame] | 150 | throws EvalException { |
cparsons | 11c9f20 | 2018-07-11 10:30:02 -0700 | [diff] [blame] | 151 | ImmutableMap.Builder<String, FakeDescriptor> attrsMapBuilder = ImmutableMap.builder(); |
cparsons | d790ce4 | 2018-06-27 15:29:02 -0700 | [diff] [blame] | 152 | if (attrs != null && attrs != Runtime.NONE) { |
cparsons | 5d85e75 | 2018-06-26 13:47:28 -0700 | [diff] [blame] | 153 | SkylarkDict<?, ?> attrsDict = (SkylarkDict<?, ?>) attrs; |
cparsons | 11c9f20 | 2018-07-11 10:30:02 -0700 | [diff] [blame] | 154 | attrsMapBuilder.putAll(attrsDict.getContents(String.class, FakeDescriptor.class, "attrs")); |
cparsons | 5d85e75 | 2018-06-26 13:47:28 -0700 | [diff] [blame] | 155 | } |
| 156 | |
cparsons | 11c9f20 | 2018-07-11 10:30:02 -0700 | [diff] [blame] | 157 | attrsMapBuilder.put("name", IMPLICIT_NAME_ATTRIBUTE_DESCRIPTOR); |
kendalllane | 3da1568 | 2019-07-24 16:02:17 -0700 | [diff] [blame] | 158 | List<AttributeInfo> attrInfos = |
cparsons | 5ece650 | 2019-04-17 10:19:41 -0700 | [diff] [blame] | 159 | attrsMapBuilder.build().entrySet().stream() |
| 160 | .filter(entry -> !entry.getKey().startsWith("_")) |
| 161 | .map(entry -> entry.getValue().asAttributeInfo(entry.getKey())) |
| 162 | .collect(Collectors.toList()); |
cparsons | 11c9f20 | 2018-07-11 10:30:02 -0700 | [diff] [blame] | 163 | attrInfos.sort(new AttributeNameComparator()); |
| 164 | |
cparsons | d790ce4 | 2018-06-27 15:29:02 -0700 | [diff] [blame] | 165 | RuleDefinitionIdentifier functionIdentifier = new RuleDefinitionIdentifier(); |
cparsons | fc0e52f | 2018-07-09 11:06:57 -0700 | [diff] [blame] | 166 | |
kendalllane | 26e94c9 | 2019-06-27 07:26:57 -0700 | [diff] [blame] | 167 | // 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); |
kendalllane | e536b14 | 2019-06-10 13:12:43 -0700 | [diff] [blame] | 169 | |
| 170 | ruleInfoList.add(new RuleInfoWrapper(functionIdentifier, ast.getLocation(), ruleInfo)); |
| 171 | |
cparsons | d790ce4 | 2018-06-27 15:29:02 -0700 | [diff] [blame] | 172 | return functionIdentifier; |
cparsons | 5d85e75 | 2018-06-26 13:47:28 -0700 | [diff] [blame] | 173 | } |
| 174 | |
| 175 | @Override |
dannark | fbfd2c0 | 2019-01-14 20:51:39 -0800 | [diff] [blame] | 176 | public Label label( |
| 177 | String labelString, |
| 178 | Boolean relativeToCallerRepository, |
| 179 | Location loc, |
| 180 | Environment env, |
| 181 | StarlarkContext context) |
| 182 | throws EvalException { |
cparsons | 1771054 | 2018-07-13 15:23:46 -0700 | [diff] [blame] | 183 | 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 | } |
cparsons | 5d85e75 | 2018-06-26 13:47:28 -0700 | [diff] [blame] | 191 | } |
| 192 | |
| 193 | @Override |
cparsons | dac2135 | 2019-05-03 11:41:17 -0700 | [diff] [blame] | 194 | 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 { |
kendalllane | 3da1568 | 2019-07-24 16:02:17 -0700 | [diff] [blame] | 198 | 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; |
cparsons | 5d85e75 | 2018-06-26 13:47:28 -0700 | [diff] [blame] | 231 | } |
cparsons | d790ce4 | 2018-06-27 15:29:02 -0700 | [diff] [blame] | 232 | |
| 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 | |
cparsons | 93adb10 | 2018-07-11 11:44:17 -0700 | [diff] [blame] | 242 | private static int idCounter = 0; |
| 243 | |
cparsons | d790ce4 | 2018-06-27 15:29:02 -0700 | [diff] [blame] | 244 | public RuleDefinitionIdentifier() { |
cparsons | 93adb10 | 2018-07-11 11:44:17 -0700 | [diff] [blame] | 245 | super("RuleDefinitionIdentifier" + idCounter++); |
cparsons | d790ce4 | 2018-06-27 15:29:02 -0700 | [diff] [blame] | 246 | } |
nharmata | aaff22c | 2019-08-02 11:59:26 -0700 | [diff] [blame] | 247 | |
| 248 | @Override |
| 249 | public boolean equals(@Nullable Object other) { |
| 250 | // Use exact object matching. |
| 251 | return this == other; |
| 252 | } |
cparsons | d790ce4 | 2018-06-27 15:29:02 -0700 | [diff] [blame] | 253 | } |
cparsons | 11c9f20 | 2018-07-11 10:30:02 -0700 | [diff] [blame] | 254 | |
| 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 Aehlig | 547aa48 | 2019-03-18 11:44:11 -0700 | [diff] [blame] | 259 | public static class AttributeNameComparator implements Comparator<AttributeInfo> { |
cparsons | 11c9f20 | 2018-07-11 10:30:02 -0700 | [diff] [blame] | 260 | |
| 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 | } |
cparsons | 5d85e75 | 2018-06-26 13:47:28 -0700 | [diff] [blame] | 272 | } |