Damien Martin-Guillerez | f88f4d8 | 2015-09-25 13:56:55 +0000 | [diff] [blame] | 1 | // Copyright 2014 The Bazel Authors. All rights reserved. |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 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 | package com.google.devtools.build.docgen; |
| 15 | |
| 16 | import com.google.common.base.Preconditions; |
| 17 | import com.google.common.collect.ImmutableMap; |
| 18 | import com.google.common.collect.ImmutableSet; |
Luis Fernando Pino Duque | 2b0b5cc | 2016-04-26 09:31:27 +0000 | [diff] [blame] | 19 | import com.google.devtools.build.lib.analysis.ConfiguredRuleClassProvider; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 20 | import com.google.devtools.build.lib.analysis.RuleDefinition; |
Lukacs Berki | 6e91eb9 | 2015-09-21 09:12:37 +0000 | [diff] [blame] | 21 | import com.google.devtools.build.lib.cmdline.Label; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 22 | import com.google.devtools.build.lib.packages.Attribute; |
Lukacs Berki | ffa73ad | 2015-09-18 11:40:12 +0000 | [diff] [blame] | 23 | import com.google.devtools.build.lib.packages.BuildType; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 24 | import com.google.devtools.build.lib.packages.TriState; |
Lukacs Berki | ffa73ad | 2015-09-18 11:40:12 +0000 | [diff] [blame] | 25 | import com.google.devtools.build.lib.syntax.Type; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 26 | import java.util.HashMap; |
| 27 | import java.util.LinkedList; |
| 28 | import java.util.Map; |
| 29 | import java.util.Set; |
| 30 | |
| 31 | /** |
twerth | 756380c | 2018-11-14 00:23:06 -0800 | [diff] [blame] | 32 | * A class storing a rule attribute documentation along with some meta information. The class |
| 33 | * provides functionality to compute the ancestry level of this attribute's generator rule |
| 34 | * definition class compared to other rule definition classes. |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 35 | * |
| 36 | * <p>Warning, two RuleDocumentationAttribute objects are equal based on only the attributeName. |
| 37 | */ |
twerth | 756380c | 2018-11-14 00:23:06 -0800 | [diff] [blame] | 38 | public class RuleDocumentationAttribute |
| 39 | implements Comparable<RuleDocumentationAttribute>, Cloneable { |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 40 | |
Googler | cffe735 | 2017-03-02 16:01:13 +0000 | [diff] [blame] | 41 | private static final ImmutableMap<Type<?>, String> TYPE_DESC = |
| 42 | ImmutableMap.<Type<?>, String>builder() |
| 43 | .put(Type.BOOLEAN, "Boolean") |
| 44 | .put(Type.INTEGER, "Integer") |
| 45 | .put(Type.INTEGER_LIST, "List of integers") |
| 46 | .put(Type.STRING, "String") |
gregce | d5cfbfe | 2017-07-14 22:21:36 +0200 | [diff] [blame] | 47 | .put(Type.STRING_DICT, "Dictionary: String -> String") |
Googler | cffe735 | 2017-03-02 16:01:13 +0000 | [diff] [blame] | 48 | .put(Type.STRING_LIST, "List of strings") |
| 49 | .put(BuildType.TRISTATE, "Integer") |
| 50 | .put(BuildType.LABEL, "<a href=\"../build-ref.html#labels\">Label</a>") |
| 51 | .put(BuildType.LABEL_LIST, "List of <a href=\"../build-ref.html#labels\">labels</a>") |
| 52 | .put( |
| 53 | BuildType.LABEL_DICT_UNARY, |
| 54 | "Dictionary mapping strings to <a href=\"../build-ref.html#labels\">labels</a>") |
gregce | d5cfbfe | 2017-07-14 22:21:36 +0200 | [diff] [blame] | 55 | .put(BuildType.LICENSE, "Licence type") |
Googler | cffe735 | 2017-03-02 16:01:13 +0000 | [diff] [blame] | 56 | .put(BuildType.NODEP_LABEL, "<a href=\"../build-ref.html#name\">Name</a>") |
| 57 | .put(BuildType.NODEP_LABEL_LIST, "List of <a href=\"../build-ref.html#name\">names</a>") |
| 58 | .put(BuildType.OUTPUT, "<a href=\"../build-ref.html#filename\">Filename</a>") |
| 59 | .put( |
| 60 | BuildType.OUTPUT_LIST, "List of <a href=\"../build-ref.html#filename\">filenames</a>") |
| 61 | .build(); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 62 | |
| 63 | private final Class<? extends RuleDefinition> definitionClass; |
| 64 | private final String attributeName; |
| 65 | private final String htmlDocumentation; |
| 66 | private final String commonType; |
David Chen | ac13e22 | 2016-02-24 22:17:42 +0000 | [diff] [blame] | 67 | // Used to expand rule link references in the attribute documentation. |
| 68 | private RuleLinkExpander linkExpander; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 69 | private int startLineCnt; |
David Chen | ac13e22 | 2016-02-24 22:17:42 +0000 | [diff] [blame] | 70 | private String fileName; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 71 | private Set<String> flags; |
David Chen | 31fce14 | 2015-08-18 08:39:51 +0000 | [diff] [blame] | 72 | private Attribute attribute; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 73 | |
twerth | 756380c | 2018-11-14 00:23:06 -0800 | [diff] [blame] | 74 | |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 75 | /** |
| 76 | * Creates common RuleDocumentationAttribute such as deps or data. |
| 77 | * These attribute docs have no definitionClass or htmlDocumentation (it's in the BE header). |
| 78 | */ |
| 79 | static RuleDocumentationAttribute create( |
| 80 | String attributeName, String commonType, String htmlDocumentation) { |
| 81 | RuleDocumentationAttribute docAttribute = new RuleDocumentationAttribute( |
David Chen | ac13e22 | 2016-02-24 22:17:42 +0000 | [diff] [blame] | 82 | null, attributeName, htmlDocumentation, 0, "", ImmutableSet.<String>of(), commonType); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 83 | return docAttribute; |
| 84 | } |
| 85 | |
| 86 | /** |
| 87 | * Creates a RuleDocumentationAttribute with all the necessary fields for explicitly |
| 88 | * defined rule attributes. |
| 89 | */ |
| 90 | static RuleDocumentationAttribute create(Class<? extends RuleDefinition> definitionClass, |
David Chen | ac13e22 | 2016-02-24 22:17:42 +0000 | [diff] [blame] | 91 | String attributeName, String htmlDocumentation, int startLineCnt, String fileName, |
| 92 | Set<String> flags) { |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 93 | return new RuleDocumentationAttribute(definitionClass, attributeName, htmlDocumentation, |
David Chen | ac13e22 | 2016-02-24 22:17:42 +0000 | [diff] [blame] | 94 | startLineCnt, fileName, flags, null); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | private RuleDocumentationAttribute(Class<? extends RuleDefinition> definitionClass, |
David Chen | ac13e22 | 2016-02-24 22:17:42 +0000 | [diff] [blame] | 98 | String attributeName, String htmlDocumentation, int startLineCnt, String fileName, |
| 99 | Set<String> flags, String commonType) { |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 100 | Preconditions.checkNotNull(attributeName, "AttributeName must not be null."); |
| 101 | this.definitionClass = definitionClass; |
| 102 | this.attributeName = attributeName; |
| 103 | this.htmlDocumentation = htmlDocumentation; |
| 104 | this.startLineCnt = startLineCnt; |
| 105 | this.flags = flags; |
| 106 | this.commonType = commonType; |
twerth | 756380c | 2018-11-14 00:23:06 -0800 | [diff] [blame] | 107 | this.fileName = fileName; |
| 108 | } |
| 109 | |
| 110 | @Override |
| 111 | protected Object clone() throws CloneNotSupportedException { |
| 112 | return super.clone(); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 113 | } |
| 114 | |
| 115 | /** |
David Chen | 31fce14 | 2015-08-18 08:39:51 +0000 | [diff] [blame] | 116 | * Sets the Attribute object that this documents. |
| 117 | */ |
| 118 | void setAttribute(Attribute attribute) { |
| 119 | this.attribute = attribute; |
| 120 | } |
| 121 | |
| 122 | /** |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 123 | * Returns the name of the rule attribute. |
| 124 | */ |
David Chen | 31fce14 | 2015-08-18 08:39:51 +0000 | [diff] [blame] | 125 | public String getAttributeName() { |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 126 | return attributeName; |
| 127 | } |
| 128 | |
twerth | 756380c | 2018-11-14 00:23:06 -0800 | [diff] [blame] | 129 | /** Returns the file name where the rule attribute is defined. */ |
| 130 | public String getFileName() { |
| 131 | return fileName; |
| 132 | } |
| 133 | |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 134 | /** |
David Chen | 31fce14 | 2015-08-18 08:39:51 +0000 | [diff] [blame] | 135 | * Returns whether this attribute is marked as deprecated. |
| 136 | */ |
| 137 | public boolean isDeprecated() { |
| 138 | return hasFlag(DocgenConsts.FLAG_DEPRECATED); |
| 139 | } |
| 140 | |
| 141 | /** |
David Chen | ac13e22 | 2016-02-24 22:17:42 +0000 | [diff] [blame] | 142 | * Sets the {@link RuleLinkExpander} to be used to expand links in the HTML documentation. |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 143 | */ |
David Chen | ac13e22 | 2016-02-24 22:17:42 +0000 | [diff] [blame] | 144 | public void setRuleLinkExpander(RuleLinkExpander linkExpander) { |
| 145 | this.linkExpander = linkExpander; |
| 146 | } |
| 147 | |
| 148 | /** |
| 149 | * Returns the html documentation of the rule attribute. |
| 150 | */ |
| 151 | public String getHtmlDocumentation() throws BuildEncyclopediaDocException { |
| 152 | String expandedHtmlDoc = htmlDocumentation; |
| 153 | if (linkExpander != null) { |
| 154 | try { |
| 155 | expandedHtmlDoc = linkExpander.expand(expandedHtmlDoc); |
| 156 | } catch (IllegalArgumentException e) { |
| 157 | throw new BuildEncyclopediaDocException(fileName, startLineCnt, e.getMessage()); |
| 158 | } |
| 159 | } |
| 160 | return expandedHtmlDoc; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 161 | } |
| 162 | |
Googler | c68c27d | 2018-09-13 05:12:58 -0700 | [diff] [blame] | 163 | /** Returns whether the param is required or optional. */ |
| 164 | public boolean isMandatory() { |
| 165 | if (attribute == null) { |
| 166 | return false; |
| 167 | } |
| 168 | return attribute.isMandatory(); |
| 169 | } |
| 170 | |
David Chen | 31fce14 | 2015-08-18 08:39:51 +0000 | [diff] [blame] | 171 | private String getDefaultValue() { |
| 172 | if (attribute == null) { |
| 173 | return ""; |
| 174 | } |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 175 | String prefix = "; default is "; |
juliexxia | 84d1a66 | 2018-12-26 14:07:04 -0800 | [diff] [blame] | 176 | Object value = attribute.getDefaultValueUnchecked(); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 177 | if (value instanceof Boolean) { |
hlopko | f23d6cc | 2018-12-13 09:01:41 -0800 | [diff] [blame] | 178 | return prefix + ((Boolean) value ? "True" : "False"); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 179 | } else if (value instanceof Integer) { |
| 180 | return prefix + String.valueOf(value); |
Googler | 2411ab9 | 2019-01-08 03:13:12 -0800 | [diff] [blame] | 181 | } else if (value instanceof String && !((String) value).isEmpty()) { |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 182 | return prefix + "\"" + value + "\""; |
| 183 | } else if (value instanceof TriState) { |
| 184 | switch((TriState) value) { |
| 185 | case AUTO: |
| 186 | return prefix + "-1"; |
| 187 | case NO: |
| 188 | return prefix + "0"; |
| 189 | case YES: |
| 190 | return prefix + "1"; |
| 191 | } |
| 192 | } else if (value instanceof Label) { |
| 193 | return prefix + "<code>" + value + "</code>"; |
| 194 | } |
| 195 | return ""; |
| 196 | } |
| 197 | |
| 198 | /** |
David Chen | 31fce14 | 2015-08-18 08:39:51 +0000 | [diff] [blame] | 199 | * Returns a string containing the synopsis for this attribute. |
| 200 | */ |
| 201 | public String getSynopsis() { |
| 202 | if (attribute == null) { |
| 203 | return ""; |
| 204 | } |
| 205 | StringBuilder sb = new StringBuilder() |
| 206 | .append(TYPE_DESC.get(attribute.getType())) |
| 207 | .append("; " + (attribute.isMandatory() ? "required" : "optional")) |
gregce | 27a91eb | 2017-04-04 22:30:25 +0000 | [diff] [blame] | 208 | .append(!attribute.isConfigurable() |
| 209 | ? String.format("; <a href=\"%s#configurable-attributes\">nonconfigurable</a>", |
| 210 | RuleDocumentation.COMMON_DEFINITIONS_PAGE) |
| 211 | : "") |
David Chen | 31fce14 | 2015-08-18 08:39:51 +0000 | [diff] [blame] | 212 | .append(getDefaultValue()); |
| 213 | return sb.toString(); |
| 214 | } |
| 215 | |
| 216 | /** |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 217 | * Returns the number of first line of the attribute documentation in its declaration file. |
| 218 | */ |
| 219 | int getStartLineCnt() { |
| 220 | return startLineCnt; |
| 221 | } |
| 222 | |
| 223 | /** |
| 224 | * Returns true if the attribute doc is of a common attribute type. |
| 225 | */ |
David Chen | 31fce14 | 2015-08-18 08:39:51 +0000 | [diff] [blame] | 226 | public boolean isCommonType() { |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 227 | return commonType != null; |
| 228 | } |
| 229 | |
| 230 | /** |
| 231 | * Returns the common attribute type if this attribute doc is of a common type |
| 232 | * otherwise actualRule. |
| 233 | */ |
| 234 | String getGeneratedInRule(String actualRule) { |
| 235 | return isCommonType() ? commonType : actualRule; |
| 236 | } |
| 237 | |
| 238 | /** |
| 239 | * Returns true if this attribute documentation has the parameter flag. |
| 240 | */ |
| 241 | boolean hasFlag(String flag) { |
| 242 | return flags.contains(flag); |
| 243 | } |
| 244 | |
| 245 | /** |
| 246 | * Returns the length of a shortest path from usingClass to the definitionClass of this |
| 247 | * RuleDocumentationAttribute in the rule definition ancestry graph. Returns -1 |
| 248 | * if definitionClass is not the ancestor (transitively) of usingClass. |
| 249 | */ |
Luis Fernando Pino Duque | 2b0b5cc | 2016-04-26 09:31:27 +0000 | [diff] [blame] | 250 | int getDefinitionClassAncestryLevel(Class<? extends RuleDefinition> usingClass, |
| 251 | ConfiguredRuleClassProvider ruleClassProvider) { |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 252 | if (usingClass.equals(definitionClass)) { |
| 253 | return 0; |
| 254 | } |
| 255 | // Storing nodes (rule class definitions) with the length of the shortest path from usingClass |
| 256 | Map<Class<? extends RuleDefinition>, Integer> visited = new HashMap<>(); |
| 257 | LinkedList<Class<? extends RuleDefinition>> toVisit = new LinkedList<>(); |
| 258 | visited.put(usingClass, 0); |
| 259 | toVisit.add(usingClass); |
| 260 | // Searching the shortest path from usingClass to this.definitionClass using BFS |
| 261 | do { |
| 262 | Class<? extends RuleDefinition> ancestor = toVisit.removeFirst(); |
Luis Fernando Pino Duque | 2b0b5cc | 2016-04-26 09:31:27 +0000 | [diff] [blame] | 263 | visitAncestor(ancestor, visited, toVisit, ruleClassProvider); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 264 | if (ancestor.equals(definitionClass)) { |
| 265 | return visited.get(ancestor); |
| 266 | } |
| 267 | } while (!toVisit.isEmpty()); |
| 268 | return -1; |
| 269 | } |
| 270 | |
| 271 | private void visitAncestor( |
| 272 | Class<? extends RuleDefinition> usingClass, |
| 273 | Map<Class<? extends RuleDefinition>, Integer> visited, |
Luis Fernando Pino Duque | 2b0b5cc | 2016-04-26 09:31:27 +0000 | [diff] [blame] | 274 | LinkedList<Class<? extends RuleDefinition>> toVisit, |
| 275 | ConfiguredRuleClassProvider ruleClassProvider) { |
| 276 | RuleDefinition instance = getRuleDefinition(usingClass, ruleClassProvider); |
Googler | 5850503 | 2015-03-19 16:12:34 +0000 | [diff] [blame] | 277 | for (Class<? extends RuleDefinition> ancestor : instance.getMetadata().ancestors()) { |
| 278 | if (!visited.containsKey(ancestor)) { |
| 279 | toVisit.addLast(ancestor); |
| 280 | visited.put(ancestor, visited.get(usingClass) + 1); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 281 | } |
| 282 | } |
| 283 | } |
| 284 | |
Luis Fernando Pino Duque | 2b0b5cc | 2016-04-26 09:31:27 +0000 | [diff] [blame] | 285 | private RuleDefinition getRuleDefinition(Class<? extends RuleDefinition> usingClass, |
| 286 | ConfiguredRuleClassProvider ruleClassProvider) { |
| 287 | if (ruleClassProvider == null) { |
| 288 | try { |
Ulf Adams | 653a16a | 2016-08-11 12:39:46 +0000 | [diff] [blame] | 289 | return usingClass.getConstructor().newInstance(); |
| 290 | } catch (ReflectiveOperationException | IllegalArgumentException e) { |
Luis Fernando Pino Duque | 2b0b5cc | 2016-04-26 09:31:27 +0000 | [diff] [blame] | 291 | throw new IllegalStateException(e); |
| 292 | } |
| 293 | } |
| 294 | return ruleClassProvider.getRuleClassDefinition(usingClass.getName()); |
| 295 | } |
| 296 | |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 297 | private int getAttributeOrderingPriority(RuleDocumentationAttribute attribute) { |
| 298 | if (DocgenConsts.ATTRIBUTE_ORDERING.containsKey(attribute.attributeName)) { |
| 299 | return DocgenConsts.ATTRIBUTE_ORDERING.get(attribute.attributeName); |
| 300 | } else { |
| 301 | return 0; |
| 302 | } |
| 303 | } |
| 304 | |
| 305 | @Override |
| 306 | public int compareTo(RuleDocumentationAttribute o) { |
| 307 | int thisPriority = getAttributeOrderingPriority(this); |
| 308 | int otherPriority = getAttributeOrderingPriority(o); |
| 309 | if (thisPriority > otherPriority) { |
| 310 | return 1; |
| 311 | } else if (thisPriority < otherPriority) { |
| 312 | return -1; |
| 313 | } else { |
| 314 | return this.attributeName.compareTo(o.attributeName); |
| 315 | } |
| 316 | } |
| 317 | |
| 318 | @Override |
| 319 | public boolean equals(Object obj) { |
| 320 | if (this == obj) { |
| 321 | return true; |
| 322 | } |
| 323 | if (!(obj instanceof RuleDocumentationAttribute)) { |
| 324 | return false; |
| 325 | } |
| 326 | return attributeName.equals(((RuleDocumentationAttribute) obj).attributeName); |
| 327 | } |
| 328 | |
| 329 | @Override |
| 330 | public int hashCode() { |
| 331 | return attributeName.hashCode(); |
| 332 | } |
| 333 | } |