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 | |
| 15 | package com.google.devtools.build.lib.rules; |
| 16 | |
| 17 | import static com.google.devtools.build.lib.packages.Attribute.ConfigurationTransition.DATA; |
| 18 | import static com.google.devtools.build.lib.packages.Attribute.ConfigurationTransition.HOST; |
| 19 | import static com.google.devtools.build.lib.packages.Attribute.attr; |
Lukacs Berki | ffa73ad | 2015-09-18 11:40:12 +0000 | [diff] [blame] | 20 | import static com.google.devtools.build.lib.packages.BuildType.LABEL; |
| 21 | import static com.google.devtools.build.lib.packages.BuildType.LABEL_LIST; |
| 22 | import static com.google.devtools.build.lib.packages.BuildType.LICENSE; |
Francois-Rene Rideau | 48670f8 | 2015-03-19 17:23:35 +0000 | [diff] [blame] | 23 | import static com.google.devtools.build.lib.syntax.SkylarkType.castMap; |
Lukacs Berki | ffa73ad | 2015-09-18 11:40:12 +0000 | [diff] [blame] | 24 | import static com.google.devtools.build.lib.syntax.Type.BOOLEAN; |
| 25 | import static com.google.devtools.build.lib.syntax.Type.INTEGER; |
| 26 | import static com.google.devtools.build.lib.syntax.Type.STRING; |
| 27 | import static com.google.devtools.build.lib.syntax.Type.STRING_LIST; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 28 | |
| 29 | import com.google.common.annotations.VisibleForTesting; |
| 30 | import com.google.common.cache.CacheBuilder; |
| 31 | import com.google.common.cache.CacheLoader; |
| 32 | import com.google.common.cache.LoadingCache; |
| 33 | import com.google.common.collect.ImmutableList; |
Lukacs Berki | 57892bd | 2015-04-13 14:42:28 +0000 | [diff] [blame] | 34 | import com.google.common.collect.ImmutableMap; |
Michael Staib | cd48cd5 | 2016-01-15 20:11:11 +0000 | [diff] [blame] | 35 | import com.google.common.collect.ImmutableSet; |
Lukacs Berki | 3fbcc61 | 2015-10-05 12:30:56 +0000 | [diff] [blame] | 36 | import com.google.devtools.build.lib.Constants; |
Dmitry Lomov | 4be5893 | 2015-12-23 13:21:32 +0000 | [diff] [blame] | 37 | import com.google.devtools.build.lib.actions.Artifact; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 38 | import com.google.devtools.build.lib.analysis.BaseRuleClasses; |
Dmitry Lomov | 4be5893 | 2015-12-23 13:21:32 +0000 | [diff] [blame] | 39 | import com.google.devtools.build.lib.analysis.OutputGroupProvider; |
| 40 | import com.google.devtools.build.lib.analysis.TransitiveInfoCollection; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 41 | import com.google.devtools.build.lib.analysis.config.BuildConfiguration; |
| 42 | import com.google.devtools.build.lib.analysis.config.RunUnder; |
Lukacs Berki | 6e91eb9 | 2015-09-21 09:12:37 +0000 | [diff] [blame] | 43 | import com.google.devtools.build.lib.cmdline.Label; |
Lukacs Berki | a643436 | 2015-09-15 13:56:14 +0000 | [diff] [blame] | 44 | import com.google.devtools.build.lib.cmdline.LabelSyntaxException; |
Dmitry Lomov | 4be5893 | 2015-12-23 13:21:32 +0000 | [diff] [blame] | 45 | import com.google.devtools.build.lib.collect.nestedset.NestedSet; |
| 46 | import com.google.devtools.build.lib.collect.nestedset.NestedSetBuilder; |
| 47 | import com.google.devtools.build.lib.collect.nestedset.Order; |
Dmitry Lomov | c15ba2e | 2015-10-30 15:50:01 +0000 | [diff] [blame] | 48 | import com.google.devtools.build.lib.concurrent.ThreadSafety.Immutable; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 49 | import com.google.devtools.build.lib.events.Location; |
Dmitry Lomov | c15ba2e | 2015-10-30 15:50:01 +0000 | [diff] [blame] | 50 | import com.google.devtools.build.lib.packages.AspectDefinition; |
Dmitry Lomov | 6231d08 | 2015-11-02 17:17:20 +0000 | [diff] [blame] | 51 | import com.google.devtools.build.lib.packages.AspectParameters; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 52 | import com.google.devtools.build.lib.packages.Attribute; |
| 53 | import com.google.devtools.build.lib.packages.Attribute.ConfigurationTransition; |
| 54 | import com.google.devtools.build.lib.packages.Attribute.LateBoundLabel; |
| 55 | import com.google.devtools.build.lib.packages.AttributeMap; |
| 56 | import com.google.devtools.build.lib.packages.ImplicitOutputsFunction.SkylarkImplicitOutputsFunctionWithCallback; |
| 57 | import com.google.devtools.build.lib.packages.ImplicitOutputsFunction.SkylarkImplicitOutputsFunctionWithMap; |
| 58 | import com.google.devtools.build.lib.packages.Package.NameConflictException; |
| 59 | import com.google.devtools.build.lib.packages.PackageFactory; |
| 60 | import com.google.devtools.build.lib.packages.PackageFactory.PackageContext; |
| 61 | import com.google.devtools.build.lib.packages.Rule; |
| 62 | import com.google.devtools.build.lib.packages.RuleClass; |
| 63 | import com.google.devtools.build.lib.packages.RuleClass.Builder; |
| 64 | import com.google.devtools.build.lib.packages.RuleClass.Builder.RuleClassType; |
| 65 | import com.google.devtools.build.lib.packages.RuleFactory; |
Mark Schaller | ee62445 | 2016-01-13 18:41:24 +0000 | [diff] [blame] | 66 | import com.google.devtools.build.lib.packages.RuleFactory.BuildLangTypedAttributeValuesMap; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 67 | import com.google.devtools.build.lib.packages.RuleFactory.InvalidRuleException; |
Dmitry Lomov | 82e0377 | 2015-11-30 12:13:22 +0000 | [diff] [blame] | 68 | import com.google.devtools.build.lib.packages.SkylarkAspectClass; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 69 | import com.google.devtools.build.lib.packages.TargetUtils; |
| 70 | import com.google.devtools.build.lib.packages.TestSize; |
Dmitry Lomov | ace678e | 2015-12-16 15:10:20 +0000 | [diff] [blame] | 71 | import com.google.devtools.build.lib.rules.SkylarkAttr.Descriptor; |
John Field | 585d1a0 | 2015-12-16 16:03:52 +0000 | [diff] [blame] | 72 | import com.google.devtools.build.lib.skylarkinterface.SkylarkSignature; |
| 73 | import com.google.devtools.build.lib.skylarkinterface.SkylarkSignature.Param; |
| 74 | import com.google.devtools.build.lib.skylarkinterface.SkylarkValue; |
Francois-Rene Rideau | a3ac202 | 2015-04-20 18:35:05 +0000 | [diff] [blame] | 75 | import com.google.devtools.build.lib.syntax.BaseFunction; |
Francois-Rene Rideau | 537a90b | 2015-04-22 06:47:31 +0000 | [diff] [blame] | 76 | import com.google.devtools.build.lib.syntax.BuiltinFunction; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 77 | import com.google.devtools.build.lib.syntax.ClassObject; |
| 78 | import com.google.devtools.build.lib.syntax.ClassObject.SkylarkClassObject; |
| 79 | import com.google.devtools.build.lib.syntax.Environment; |
| 80 | import com.google.devtools.build.lib.syntax.Environment.NoSuchVariableException; |
| 81 | import com.google.devtools.build.lib.syntax.EvalException; |
| 82 | import com.google.devtools.build.lib.syntax.EvalUtils; |
| 83 | import com.google.devtools.build.lib.syntax.FuncallExpression; |
Francois-Rene Rideau | 537a90b | 2015-04-22 06:47:31 +0000 | [diff] [blame] | 84 | import com.google.devtools.build.lib.syntax.FunctionSignature; |
Dmitry Lomov | 0b832ce | 2015-10-20 10:03:14 +0000 | [diff] [blame] | 85 | import com.google.devtools.build.lib.syntax.Printer; |
Francois-Rene Rideau | 0f7ba34 | 2015-08-31 16:16:21 +0000 | [diff] [blame] | 86 | import com.google.devtools.build.lib.syntax.Runtime; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 87 | import com.google.devtools.build.lib.syntax.SkylarkCallbackFunction; |
Francois-Rene Rideau | c0a8c58 | 2016-01-28 18:36:22 +0000 | [diff] [blame^] | 88 | import com.google.devtools.build.lib.syntax.SkylarkDict; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 89 | import com.google.devtools.build.lib.syntax.SkylarkList; |
Dmitry Lomov | 4be5893 | 2015-12-23 13:21:32 +0000 | [diff] [blame] | 90 | import com.google.devtools.build.lib.syntax.SkylarkNestedSet; |
Francois-Rene Rideau | 537a90b | 2015-04-22 06:47:31 +0000 | [diff] [blame] | 91 | import com.google.devtools.build.lib.syntax.SkylarkSignatureProcessor; |
Lukacs Berki | ffa73ad | 2015-09-18 11:40:12 +0000 | [diff] [blame] | 92 | import com.google.devtools.build.lib.syntax.Type; |
| 93 | import com.google.devtools.build.lib.syntax.Type.ConversionException; |
Dmitry Lomov | 7b59945 | 2015-11-26 10:07:32 +0000 | [diff] [blame] | 94 | import com.google.devtools.build.lib.util.Pair; |
Mark Schaller | 6df8179 | 2015-12-10 18:47:47 +0000 | [diff] [blame] | 95 | import com.google.devtools.build.lib.util.Preconditions; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 96 | |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 97 | import java.util.Map; |
Dmitry Lomov | 5a8f1c0 | 2015-11-26 10:49:16 +0000 | [diff] [blame] | 98 | import java.util.Set; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 99 | import java.util.concurrent.ExecutionException; |
| 100 | |
| 101 | /** |
| 102 | * A helper class to provide an easier API for Skylark rule definitions. |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 103 | */ |
| 104 | public class SkylarkRuleClassFunctions { |
| 105 | |
| 106 | //TODO(bazel-team): proper enum support |
Francois-Rene Rideau | a3ac202 | 2015-04-20 18:35:05 +0000 | [diff] [blame] | 107 | @SkylarkSignature(name = "DATA_CFG", returnType = ConfigurationTransition.class, |
Laurent Le Brun | d90db7f | 2015-04-01 14:16:30 +0000 | [diff] [blame] | 108 | doc = "Experimental. Specifies a transition to the data configuration.") |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 109 | private static final Object dataTransition = ConfigurationTransition.DATA; |
| 110 | |
Francois-Rene Rideau | a3ac202 | 2015-04-20 18:35:05 +0000 | [diff] [blame] | 111 | @SkylarkSignature(name = "HOST_CFG", returnType = ConfigurationTransition.class, |
Laurent Le Brun | d90db7f | 2015-04-01 14:16:30 +0000 | [diff] [blame] | 112 | doc = "Specifies a transition to the host configuration.") |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 113 | private static final Object hostTransition = ConfigurationTransition.HOST; |
| 114 | |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 115 | private static final LateBoundLabel<BuildConfiguration> RUN_UNDER = |
| 116 | new LateBoundLabel<BuildConfiguration>() { |
| 117 | @Override |
| 118 | public Label getDefault(Rule rule, BuildConfiguration configuration) { |
| 119 | RunUnder runUnder = configuration.getRunUnder(); |
| 120 | return runUnder == null ? null : runUnder.getLabel(); |
| 121 | } |
| 122 | }; |
| 123 | |
| 124 | // TODO(bazel-team): Copied from ConfiguredRuleClassProvider for the transition from built-in |
| 125 | // rules to skylark extensions. Using the same instance would require a large refactoring. |
| 126 | // If we don't want to support old built-in rules and Skylark simultaneously |
| 127 | // (except for transition phase) it's probably OK. |
| 128 | private static LoadingCache<String, Label> labelCache = |
| 129 | CacheBuilder.newBuilder().build(new CacheLoader<String, Label>() { |
| 130 | @Override |
| 131 | public Label load(String from) throws Exception { |
| 132 | try { |
| 133 | return Label.parseAbsolute(from); |
Lukacs Berki | a643436 | 2015-09-15 13:56:14 +0000 | [diff] [blame] | 134 | } catch (LabelSyntaxException e) { |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 135 | throw new Exception(from); |
| 136 | } |
| 137 | } |
| 138 | }); |
| 139 | |
| 140 | // TODO(bazel-team): Remove the code duplication (BaseRuleClasses and this class). |
Florian Weikert | 70265ec | 2015-08-31 14:37:27 +0000 | [diff] [blame] | 141 | /** Parent rule class for non-executable non-test Skylark rules. */ |
Mark Schaller | 4fa83ac | 2015-07-10 16:59:37 +0000 | [diff] [blame] | 142 | public static final RuleClass baseRule = |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 143 | BaseRuleClasses.commonCoreAndSkylarkAttributes( |
| 144 | new RuleClass.Builder("$base_rule", RuleClassType.ABSTRACT, true)) |
| 145 | .add(attr("expect_failure", STRING)) |
| 146 | .build(); |
| 147 | |
Florian Weikert | 70265ec | 2015-08-31 14:37:27 +0000 | [diff] [blame] | 148 | /** Parent rule class for executable non-test Skylark rules. */ |
| 149 | public static final RuleClass binaryBaseRule = |
| 150 | new RuleClass.Builder("$binary_base_rule", RuleClassType.ABSTRACT, true, baseRule) |
| 151 | .add( |
| 152 | attr("args", STRING_LIST) |
| 153 | .nonconfigurable("policy decision: should be consistent across configurations")) |
| 154 | .add(attr("output_licenses", LICENSE)) |
| 155 | .build(); |
| 156 | |
Mark Schaller | 4fa83ac | 2015-07-10 16:59:37 +0000 | [diff] [blame] | 157 | /** Parent rule class for test Skylark rules. */ |
| 158 | public static final RuleClass testBaseRule = |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 159 | new RuleClass.Builder("$test_base_rule", RuleClassType.ABSTRACT, true, baseRule) |
| 160 | .add(attr("size", STRING).value("medium").taggable() |
| 161 | .nonconfigurable("used in loading phase rule validation logic")) |
| 162 | .add(attr("timeout", STRING).taggable() |
| 163 | .nonconfigurable("used in loading phase rule validation logic").value( |
| 164 | new Attribute.ComputedDefault() { |
| 165 | @Override |
| 166 | public Object getDefault(AttributeMap rule) { |
| 167 | TestSize size = TestSize.getTestSize(rule.get("size", Type.STRING)); |
| 168 | if (size != null) { |
| 169 | String timeout = size.getDefaultTimeout().toString(); |
| 170 | if (timeout != null) { |
| 171 | return timeout; |
| 172 | } |
| 173 | } |
| 174 | return "illegal"; |
| 175 | } |
| 176 | })) |
| 177 | .add(attr("flaky", BOOLEAN).value(false).taggable() |
| 178 | .nonconfigurable("taggable - called in Rule.getRuleTags")) |
| 179 | .add(attr("shard_count", INTEGER).value(-1)) |
| 180 | .add(attr("local", BOOLEAN).value(false).taggable() |
| 181 | .nonconfigurable("policy decision: this should be consistent across configurations")) |
Laurent Le Brun | 8d4ffc4 | 2015-06-24 15:21:50 +0000 | [diff] [blame] | 182 | .add(attr("args", STRING_LIST) |
| 183 | .nonconfigurable("policy decision: should be consistent across configurations")) |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 184 | .add(attr("$test_runtime", LABEL_LIST).cfg(HOST).value(ImmutableList.of( |
Lukacs Berki | 3fbcc61 | 2015-10-05 12:30:56 +0000 | [diff] [blame] | 185 | labelCache.getUnchecked(Constants.TOOLS_REPOSITORY + "//tools/test:runtime")))) |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 186 | .add(attr(":run_under", LABEL).cfg(DATA).value(RUN_UNDER)) |
| 187 | .build(); |
| 188 | |
| 189 | /** |
Han-Wen Nienhuys | d39e1e1 | 2015-08-20 13:52:19 +0000 | [diff] [blame] | 190 | * In native code, private values start with $. |
| 191 | * In Skylark, private values start with _, because of the grammar. |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 192 | */ |
| 193 | private static String attributeToNative(String oldName, Location loc, boolean isLateBound) |
| 194 | throws EvalException { |
| 195 | if (oldName.isEmpty()) { |
| 196 | throw new EvalException(loc, "Attribute name cannot be empty"); |
| 197 | } |
| 198 | if (isLateBound) { |
| 199 | if (oldName.charAt(0) != '_') { |
| 200 | throw new EvalException(loc, "When an attribute value is a function, " |
| 201 | + "the attribute must be private (start with '_')"); |
| 202 | } |
| 203 | return ":" + oldName.substring(1); |
| 204 | } |
| 205 | if (oldName.charAt(0) == '_') { |
| 206 | return "$" + oldName.substring(1); |
| 207 | } |
| 208 | return oldName; |
| 209 | } |
| 210 | |
| 211 | // TODO(bazel-team): implement attribute copy and other rule properties |
Francois-Rene Rideau | a3ac202 | 2015-04-20 18:35:05 +0000 | [diff] [blame] | 212 | @SkylarkSignature(name = "rule", doc = |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 213 | "Creates a new rule. Store it in a global value, so that it can be loaded and called " |
| 214 | + "from BUILD files.", |
Francois-Rene Rideau | 95b0d0c | 2015-04-22 16:52:13 +0000 | [diff] [blame] | 215 | returnType = BaseFunction.class, |
Francois-Rene Rideau | a3ac202 | 2015-04-20 18:35:05 +0000 | [diff] [blame] | 216 | mandatoryPositionals = { |
Francois-Rene Rideau | 95b0d0c | 2015-04-22 16:52:13 +0000 | [diff] [blame] | 217 | @Param(name = "implementation", type = BaseFunction.class, |
Laurent Le Brun | faf7841 | 2015-07-28 16:13:00 +0000 | [diff] [blame] | 218 | doc = "the function implementing this rule, must have exactly one parameter: " |
David Chen | 24f2d99 | 2015-08-17 17:25:46 +0000 | [diff] [blame] | 219 | + "<a href=\"ctx.html\">ctx</a>. The function is called during the analysis phase " |
Laurent Le Brun | faf7841 | 2015-07-28 16:13:00 +0000 | [diff] [blame] | 220 | + "for each instance of the rule. It can access the attributes provided by the user. " |
Francois-Rene Rideau | a3ac202 | 2015-04-20 18:35:05 +0000 | [diff] [blame] | 221 | + "It must create actions to generate all the declared outputs.") |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 222 | }, |
Francois-Rene Rideau | a3ac202 | 2015-04-20 18:35:05 +0000 | [diff] [blame] | 223 | optionalPositionals = { |
| 224 | @Param(name = "test", type = Boolean.class, defaultValue = "False", |
| 225 | doc = "Whether this rule is a test rule. " |
Laurent Le Brun | faf7841 | 2015-07-28 16:13:00 +0000 | [diff] [blame] | 226 | + "If True, the rule must end with <code>_test</code> (otherwise it must not), " |
Laurent Le Brun | 9178bc2 | 2015-06-19 16:00:34 +0000 | [diff] [blame] | 227 | + "and there must be an action that generates <code>ctx.outputs.executable</code>."), |
Francois-Rene Rideau | c0a8c58 | 2016-01-28 18:36:22 +0000 | [diff] [blame^] | 228 | @Param(name = "attrs", type = SkylarkDict.class, noneable = true, defaultValue = "None", |
| 229 | doc = |
Francois-Rene Rideau | a3ac202 | 2015-04-20 18:35:05 +0000 | [diff] [blame] | 230 | "dictionary to declare all the attributes of the rule. It maps from an attribute name " |
David Chen | 24f2d99 | 2015-08-17 17:25:46 +0000 | [diff] [blame] | 231 | + "to an attribute object (see <a href=\"attr.html\">attr</a> module). " |
Francois-Rene Rideau | a3ac202 | 2015-04-20 18:35:05 +0000 | [diff] [blame] | 232 | + "Attributes starting with <code>_</code> are private, and can be used to add " |
Laurent Le Brun | 9ba067d | 2015-05-22 13:55:23 +0000 | [diff] [blame] | 233 | + "an implicit dependency on a label. The attribute <code>name</code> is implicitly " |
| 234 | + "added and must not be specified. Attributes <code>visibility</code>, " |
| 235 | + "<code>deprecation</code>, <code>tags</code>, <code>testonly</code>, and " |
| 236 | + "<code>features</code> are implicitly added and might be overriden."), |
| 237 | // TODO(bazel-team): need to give the types of these builtin attributes |
Francois-Rene Rideau | c0a8c58 | 2016-01-28 18:36:22 +0000 | [diff] [blame^] | 238 | @Param(name = "outputs", type = SkylarkDict.class, callbackEnabled = true, noneable = true, |
Francois-Rene Rideau | a3ac202 | 2015-04-20 18:35:05 +0000 | [diff] [blame] | 239 | defaultValue = "None", doc = "outputs of this rule. " |
| 240 | + "It is a dictionary mapping from string to a template name. " |
Brian Silverman | e0adfc6 | 2015-08-03 09:04:31 +0000 | [diff] [blame] | 241 | + "For example: <code>{\"ext\": \"%{name}.ext\"}</code>. <br>" |
Laurent Le Brun | faf7841 | 2015-07-28 16:13:00 +0000 | [diff] [blame] | 242 | + "The dictionary key becomes an attribute in <code>ctx.outputs</code>. " |
Francois-Rene Rideau | a3ac202 | 2015-04-20 18:35:05 +0000 | [diff] [blame] | 243 | // TODO(bazel-team): Make doc more clear, wrt late-bound attributes. |
| 244 | + "It may also be a function (which receives <code>ctx.attr</code> as argument) " |
| 245 | + "returning such a dictionary."), |
| 246 | @Param(name = "executable", type = Boolean.class, defaultValue = "False", |
Laurent Le Brun | faf7841 | 2015-07-28 16:13:00 +0000 | [diff] [blame] | 247 | doc = "whether this rule is marked as executable or not. If True, " |
| 248 | + "there must be an action that generates <code>ctx.outputs.executable</code>."), |
Francois-Rene Rideau | a3ac202 | 2015-04-20 18:35:05 +0000 | [diff] [blame] | 249 | @Param(name = "output_to_genfiles", type = Boolean.class, defaultValue = "False", |
| 250 | doc = "If true, the files will be generated in the genfiles directory instead of the " |
Laurent Le Brun | 9c8b370 | 2015-09-23 13:53:59 +0000 | [diff] [blame] | 251 | + "bin directory. Unless you need it for compatibility with existing rules " |
| 252 | + "(e.g. when generating header files for C++), do not set this flag."), |
Dmitry Lomov | ace678e | 2015-12-16 15:10:20 +0000 | [diff] [blame] | 253 | @Param(name = "fragments", type = SkylarkList.class, generic1 = String.class, |
| 254 | defaultValue = "[]", |
| 255 | doc = "List of names of configuration fragments that the rule requires " |
| 256 | + "in target configuration."), |
| 257 | @Param(name = "host_fragments", type = SkylarkList.class, generic1 = String.class, |
| 258 | defaultValue = "[]", |
| 259 | doc = "List of names of configuration fragments that the rule requires " |
| 260 | + "in host configuration.")}, |
Francois-Rene Rideau | a3ac202 | 2015-04-20 18:35:05 +0000 | [diff] [blame] | 261 | useAst = true, useEnvironment = true) |
Francois-Rene Rideau | 537a90b | 2015-04-22 06:47:31 +0000 | [diff] [blame] | 262 | private static final BuiltinFunction rule = new BuiltinFunction("rule") { |
Florian Weikert | 3f8aac9 | 2015-09-07 12:06:02 +0000 | [diff] [blame] | 263 | @SuppressWarnings({"rawtypes", "unchecked"}) // castMap produces |
| 264 | // an Attribute.Builder instead of a Attribute.Builder<?> but it's OK. |
| 265 | public BaseFunction invoke(BaseFunction implementation, Boolean test, Object attrs, |
| 266 | Object implicitOutputs, Boolean executable, Boolean outputToGenfiles, SkylarkList fragments, |
| 267 | SkylarkList hostFragments, FuncallExpression ast, Environment funcallEnv) |
| 268 | throws EvalException, ConversionException { |
| 269 | funcallEnv.checkLoadingPhase("rule", ast.getLocation()); |
| 270 | RuleClassType type = test ? RuleClassType.TEST : RuleClassType.NORMAL; |
| 271 | RuleClass parent = test ? testBaseRule : (executable ? binaryBaseRule : baseRule); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 272 | |
Florian Weikert | 3f8aac9 | 2015-09-07 12:06:02 +0000 | [diff] [blame] | 273 | // We'll set the name later, pass the empty string for now. |
| 274 | RuleClass.Builder builder = new RuleClass.Builder("", type, true, parent); |
Dmitry Lomov | ace678e | 2015-12-16 15:10:20 +0000 | [diff] [blame] | 275 | ImmutableList<Pair<String, SkylarkAttr.Descriptor>> attributes = |
| 276 | attrObjectToAttributesList(attrs, ast); |
Florian Weikert | 3f8aac9 | 2015-09-07 12:06:02 +0000 | [diff] [blame] | 277 | if (executable || test) { |
Florian Weikert | e96b0b8 | 2015-09-25 11:35:11 +0000 | [diff] [blame] | 278 | addAttribute( |
Dmitry Lomov | 7b59945 | 2015-11-26 10:07:32 +0000 | [diff] [blame] | 279 | ast.getLocation(), |
Florian Weikert | e96b0b8 | 2015-09-25 11:35:11 +0000 | [diff] [blame] | 280 | builder, |
Florian Weikert | 3f8aac9 | 2015-09-07 12:06:02 +0000 | [diff] [blame] | 281 | attr("$is_executable", BOOLEAN) |
| 282 | .value(true) |
| 283 | .nonconfigurable("Called from RunCommand.isExecutable, which takes a Target") |
| 284 | .build()); |
| 285 | builder.setOutputsDefaultExecutable(); |
| 286 | } |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 287 | |
Florian Weikert | 3f8aac9 | 2015-09-07 12:06:02 +0000 | [diff] [blame] | 288 | if (implicitOutputs != Runtime.NONE) { |
| 289 | if (implicitOutputs instanceof BaseFunction) { |
| 290 | BaseFunction func = (BaseFunction) implicitOutputs; |
Dmitry Lomov | ace678e | 2015-12-16 15:10:20 +0000 | [diff] [blame] | 291 | SkylarkCallbackFunction callback = new SkylarkCallbackFunction(func, ast, funcallEnv); |
Florian Weikert | 3f8aac9 | 2015-09-07 12:06:02 +0000 | [diff] [blame] | 292 | builder.setImplicitOutputsFunction( |
| 293 | new SkylarkImplicitOutputsFunctionWithCallback(callback, ast.getLocation())); |
| 294 | } else { |
| 295 | builder.setImplicitOutputsFunction( |
Francois-Rene Rideau | 89312fb | 2015-09-10 18:53:03 +0000 | [diff] [blame] | 296 | new SkylarkImplicitOutputsFunctionWithMap( |
Dmitry Lomov | ace678e | 2015-12-16 15:10:20 +0000 | [diff] [blame] | 297 | ImmutableMap.copyOf( |
| 298 | castMap( |
| 299 | implicitOutputs, |
| 300 | String.class, |
| 301 | String.class, |
| 302 | "implicit outputs of the rule class")))); |
Han-Wen Nienhuys | d39e1e1 | 2015-08-20 13:52:19 +0000 | [diff] [blame] | 303 | } |
Florian Weikert | 3f8aac9 | 2015-09-07 12:06:02 +0000 | [diff] [blame] | 304 | } |
Laurent Le Brun | c2efe45 | 2015-04-08 16:27:21 +0000 | [diff] [blame] | 305 | |
Florian Weikert | 3f8aac9 | 2015-09-07 12:06:02 +0000 | [diff] [blame] | 306 | if (outputToGenfiles) { |
| 307 | builder.setOutputToGenfiles(); |
| 308 | } |
Florian Weikert | 3f53fbb | 2015-08-07 22:25:50 +0000 | [diff] [blame] | 309 | |
Michael Staib | ff66c19 | 2016-01-14 22:40:37 +0000 | [diff] [blame] | 310 | builder.requiresConfigurationFragmentsBySkylarkModuleName( |
| 311 | fragments.getContents(String.class, "fragments")); |
| 312 | builder.requiresHostConfigurationFragmentsBySkylarkModuleName( |
| 313 | hostFragments.getContents(String.class, "host_fragments")); |
Florian Weikert | 3f8aac9 | 2015-09-07 12:06:02 +0000 | [diff] [blame] | 314 | builder.setConfiguredTargetFunction(implementation); |
Francois-Rene Rideau | 89312fb | 2015-09-10 18:53:03 +0000 | [diff] [blame] | 315 | builder.setRuleDefinitionEnvironment(funcallEnv); |
Dmitry Lomov | ace678e | 2015-12-16 15:10:20 +0000 | [diff] [blame] | 316 | return new RuleFunction(builder, type, attributes, ast.getLocation()); |
Florian Weikert | 3f8aac9 | 2015-09-07 12:06:02 +0000 | [diff] [blame] | 317 | } |
Dmitry Lomov | ace678e | 2015-12-16 15:10:20 +0000 | [diff] [blame] | 318 | }; |
Florian Weikert | 3f8aac9 | 2015-09-07 12:06:02 +0000 | [diff] [blame] | 319 | |
Dmitry Lomov | ace678e | 2015-12-16 15:10:20 +0000 | [diff] [blame] | 320 | protected static ImmutableList<Pair<String, Descriptor>> attrObjectToAttributesList( |
| 321 | Object attrs, FuncallExpression ast) throws EvalException { |
| 322 | ImmutableList.Builder<Pair<String, Descriptor>> attributes = ImmutableList.builder(); |
| 323 | |
| 324 | if (attrs != Runtime.NONE) { |
| 325 | for (Map.Entry<String, Descriptor> attr : |
| 326 | castMap(attrs, String.class, Descriptor.class, "attrs").entrySet()) { |
| 327 | Descriptor attrDescriptor = attr.getValue(); |
| 328 | String attrName = |
| 329 | attributeToNative( |
| 330 | attr.getKey(), |
| 331 | ast.getLocation(), |
| 332 | attrDescriptor.getAttributeBuilder().hasLateBoundValue()); |
| 333 | attributes.add(Pair.of(attrName, attrDescriptor)); |
Florian Weikert | 3f8aac9 | 2015-09-07 12:06:02 +0000 | [diff] [blame] | 334 | } |
| 335 | } |
Dmitry Lomov | ace678e | 2015-12-16 15:10:20 +0000 | [diff] [blame] | 336 | return attributes.build(); |
| 337 | } |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 338 | |
Dmitry Lomov | 7b59945 | 2015-11-26 10:07:32 +0000 | [diff] [blame] | 339 | private static void addAttribute( |
| 340 | Location location, RuleClass.Builder builder, Attribute attribute) throws EvalException { |
| 341 | try { |
| 342 | builder.addOrOverrideAttribute(attribute); |
| 343 | } catch (IllegalArgumentException ex) { |
| 344 | throw new EvalException(location, ex); |
| 345 | } |
| 346 | } |
| 347 | |
| 348 | |
Dmitry Lomov | 99f4372 | 2015-12-22 13:04:46 +0000 | [diff] [blame] | 349 | @SkylarkSignature(name = "aspect", doc = |
| 350 | "Creates a new aspect. The result of this fucntion must be stored in a global value.", |
Dmitry Lomov | 0b832ce | 2015-10-20 10:03:14 +0000 | [diff] [blame] | 351 | returnType = SkylarkAspect.class, |
Dmitry Lomov | 99f4372 | 2015-12-22 13:04:46 +0000 | [diff] [blame] | 352 | mandatoryPositionals = { |
| 353 | @Param(name = "implementation", type = BaseFunction.class, |
| 354 | doc = "the function implementing this aspect. Must have two parameters: " |
| 355 | + "<a href=\"Target.html\">Target</a> (the target to which the aspect is applied) and" |
| 356 | + "<a href=\"ctx.html\">ctx</a>. Attributes of the target are available via ctx.rule " |
| 357 | + " field. The function is called during the analysis phase for each application of " |
| 358 | + "an aspect to a target." |
| 359 | ), |
| 360 | }, |
Dmitry Lomov | c15ba2e | 2015-10-30 15:50:01 +0000 | [diff] [blame] | 361 | optionalPositionals = { |
Dmitry Lomov | 99f4372 | 2015-12-22 13:04:46 +0000 | [diff] [blame] | 362 | @Param(name = "attr_aspects", type = SkylarkList.class, generic1 = String.class, |
| 363 | defaultValue = "[]", |
| 364 | doc = "List of attribute names. The aspect propagates along dependencies specified by " |
| 365 | + " attributes of a target with this name" |
Dmitry Lomov | c1b1eed | 2015-11-26 11:18:12 +0000 | [diff] [blame] | 366 | ), |
Francois-Rene Rideau | c0a8c58 | 2016-01-28 18:36:22 +0000 | [diff] [blame^] | 367 | @Param(name = "attrs", type = SkylarkDict.class, noneable = true, defaultValue = "None", |
Dmitry Lomov | 99f4372 | 2015-12-22 13:04:46 +0000 | [diff] [blame] | 368 | doc = "dictionary to declare all the attributes of the aspect. " |
| 369 | + "It maps from an attribute name to an attribute object " |
| 370 | + "(see <a href=\"attr.html\">attr</a> module). " |
| 371 | + "Aspect attributes are available to implementation function as fields of ctx parameter. " |
| 372 | + "All aspect attributes must be private, so their names must start with <code>_</code>. " |
| 373 | + "All aspect attributes must be have default values, and be of type " |
| 374 | + "<code>label</code> or <code>label_list</code>" |
Michael Staib | cd48cd5 | 2016-01-15 20:11:11 +0000 | [diff] [blame] | 375 | ), |
| 376 | @Param( |
| 377 | name = "fragments", |
| 378 | type = SkylarkList.class, |
| 379 | generic1 = String.class, |
| 380 | defaultValue = "[]", |
| 381 | doc = |
| 382 | "List of names of configuration fragments that the aspect requires " |
| 383 | + "in target configuration." |
| 384 | ), |
| 385 | @Param( |
| 386 | name = "host_fragments", |
| 387 | type = SkylarkList.class, |
| 388 | generic1 = String.class, |
| 389 | defaultValue = "[]", |
| 390 | doc = |
| 391 | "List of names of configuration fragments that the aspect requires " |
| 392 | + "in host configuration." |
Dmitry Lomov | 99f4372 | 2015-12-22 13:04:46 +0000 | [diff] [blame] | 393 | ) |
Dmitry Lomov | c15ba2e | 2015-10-30 15:50:01 +0000 | [diff] [blame] | 394 | }, |
Dmitry Lomov | c1b1eed | 2015-11-26 11:18:12 +0000 | [diff] [blame] | 395 | useEnvironment = true, |
| 396 | useAst = true |
Dmitry Lomov | 0b832ce | 2015-10-20 10:03:14 +0000 | [diff] [blame] | 397 | ) |
| 398 | private static final BuiltinFunction aspect = |
| 399 | new BuiltinFunction("aspect") { |
Dmitry Lomov | c15ba2e | 2015-10-30 15:50:01 +0000 | [diff] [blame] | 400 | public SkylarkAspect invoke( |
Dmitry Lomov | c1b1eed | 2015-11-26 11:18:12 +0000 | [diff] [blame] | 401 | BaseFunction implementation, |
| 402 | SkylarkList attributeAspects, |
Dmitry Lomov | ace678e | 2015-12-16 15:10:20 +0000 | [diff] [blame] | 403 | Object attrs, |
Michael Staib | cd48cd5 | 2016-01-15 20:11:11 +0000 | [diff] [blame] | 404 | SkylarkList fragments, |
| 405 | SkylarkList hostFragments, |
Dmitry Lomov | c1b1eed | 2015-11-26 11:18:12 +0000 | [diff] [blame] | 406 | FuncallExpression ast, |
Dmitry Lomov | ace678e | 2015-12-16 15:10:20 +0000 | [diff] [blame] | 407 | Environment funcallEnv) |
| 408 | throws EvalException { |
| 409 | ImmutableList.Builder<String> attrAspects = ImmutableList.builder(); |
Dmitry Lomov | c15ba2e | 2015-10-30 15:50:01 +0000 | [diff] [blame] | 410 | for (Object attributeAspect : attributeAspects) { |
Dmitry Lomov | ace678e | 2015-12-16 15:10:20 +0000 | [diff] [blame] | 411 | attrAspects.add(STRING.convert(attributeAspect, "attr_aspects")); |
Dmitry Lomov | c1b1eed | 2015-11-26 11:18:12 +0000 | [diff] [blame] | 412 | } |
| 413 | |
Dmitry Lomov | ace678e | 2015-12-16 15:10:20 +0000 | [diff] [blame] | 414 | ImmutableList<Pair<String, SkylarkAttr.Descriptor>> attributes = |
| 415 | attrObjectToAttributesList(attrs, ast); |
| 416 | for (Pair<String, Descriptor> attribute : attributes) { |
| 417 | String nativeName = attribute.getFirst(); |
| 418 | if (!Attribute.isImplicit(nativeName)) { |
| 419 | throw new EvalException( |
| 420 | ast.getLocation(), |
| 421 | String.format( |
| 422 | "Aspect attribute '%s' must be implicit (its name should start with '_').", |
| 423 | nativeName)); |
| 424 | } |
| 425 | |
| 426 | String skylarkName = "_" + nativeName.substring(1); |
| 427 | |
| 428 | if (!attribute.getSecond().getAttributeBuilder().isValueSet()) { |
| 429 | throw new EvalException( |
| 430 | ast.getLocation(), |
| 431 | String.format("Aspect attribute '%s' has no default value.", skylarkName)); |
| 432 | } |
| 433 | } |
| 434 | |
Michael Staib | cd48cd5 | 2016-01-15 20:11:11 +0000 | [diff] [blame] | 435 | return new SkylarkAspect( |
| 436 | implementation, |
| 437 | attrAspects.build(), |
| 438 | attributes, |
| 439 | ImmutableSet.copyOf(fragments.getContents(String.class, "fragments")), |
| 440 | ImmutableSet.copyOf(hostFragments.getContents(String.class, "host_fragments")), |
| 441 | funcallEnv); |
Dmitry Lomov | 0b832ce | 2015-10-20 10:03:14 +0000 | [diff] [blame] | 442 | } |
| 443 | }; |
| 444 | |
| 445 | |
Francois-Rene Rideau | 93ed7f1 | 2015-10-20 15:39:33 +0000 | [diff] [blame] | 446 | /** The implementation for the magic function "rule" that creates Skylark rule classes */ |
Han-Wen Nienhuys | ceae8c5 | 2015-09-22 16:24:45 +0000 | [diff] [blame] | 447 | public static final class RuleFunction extends BaseFunction { |
Dmitry Lomov | 7b59945 | 2015-11-26 10:07:32 +0000 | [diff] [blame] | 448 | private RuleClass.Builder builder; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 449 | |
Dmitry Lomov | 7b59945 | 2015-11-26 10:07:32 +0000 | [diff] [blame] | 450 | private RuleClass ruleClass; |
| 451 | private final RuleClassType type; |
| 452 | private ImmutableList<Pair<String, SkylarkAttr.Descriptor>> attributes; |
| 453 | private final Location definitionLocation; |
| 454 | private Label skylarkLabel; |
| 455 | |
| 456 | public RuleFunction(Builder builder, RuleClassType type, |
| 457 | ImmutableList<Pair<String, SkylarkAttr.Descriptor>> attributes, |
| 458 | Location definitionLocation) { |
Francois-Rene Rideau | 537a90b | 2015-04-22 06:47:31 +0000 | [diff] [blame] | 459 | super("rule", FunctionSignature.KWARGS); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 460 | this.builder = builder; |
| 461 | this.type = type; |
Dmitry Lomov | 7b59945 | 2015-11-26 10:07:32 +0000 | [diff] [blame] | 462 | this.attributes = attributes; |
| 463 | this.definitionLocation = definitionLocation; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 464 | } |
| 465 | |
Francois-Rene Rideau | 537a90b | 2015-04-22 06:47:31 +0000 | [diff] [blame] | 466 | @Override |
Francois-Rene Rideau | 76023b9 | 2015-04-17 15:31:59 +0000 | [diff] [blame] | 467 | @SuppressWarnings("unchecked") // the magic hidden $pkg_context variable is guaranteed |
| 468 | // to be a PackageContext |
Francois-Rene Rideau | 537a90b | 2015-04-22 06:47:31 +0000 | [diff] [blame] | 469 | public Object call(Object[] args, FuncallExpression ast, Environment env) |
| 470 | throws EvalException, InterruptedException, ConversionException { |
Francois-Rene Rideau | 3a8ddcc | 2015-08-26 22:30:38 +0000 | [diff] [blame] | 471 | env.checkLoadingPhase(getName(), ast.getLocation()); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 472 | try { |
Dmitry Lomov | 7b59945 | 2015-11-26 10:07:32 +0000 | [diff] [blame] | 473 | if (ruleClass == null) { |
Francois-Rene Rideau | a8352e9 | 2015-06-30 16:59:42 +0000 | [diff] [blame] | 474 | throw new EvalException(ast.getLocation(), |
| 475 | "Invalid rule class hasn't been exported by a Skylark file"); |
| 476 | } |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 477 | PackageContext pkgContext = (PackageContext) env.lookup(PackageFactory.PKG_CONTEXT); |
Mark Schaller | ee62445 | 2016-01-13 18:41:24 +0000 | [diff] [blame] | 478 | BuildLangTypedAttributeValuesMap attributeValues = |
| 479 | new BuildLangTypedAttributeValuesMap((Map<String, Object>) args[0]); |
| 480 | return RuleFactory.createAndAddRule(pkgContext, ruleClass, attributeValues, ast, env); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 481 | } catch (InvalidRuleException | NameConflictException | NoSuchVariableException e) { |
| 482 | throw new EvalException(ast.getLocation(), e.getMessage()); |
| 483 | } |
| 484 | } |
| 485 | |
Francois-Rene Rideau | a8352e9 | 2015-06-30 16:59:42 +0000 | [diff] [blame] | 486 | /** |
| 487 | * Export a RuleFunction from a Skylark file with a given name. |
| 488 | */ |
Dmitry Lomov | 7b59945 | 2015-11-26 10:07:32 +0000 | [diff] [blame] | 489 | void export(Label skylarkLabel, String ruleClassName) throws EvalException { |
| 490 | Preconditions.checkState(ruleClass == null && builder != null); |
John Field | a97e17f | 2015-11-13 02:19:52 +0000 | [diff] [blame] | 491 | this.skylarkLabel = skylarkLabel; |
Dmitry Lomov | 7b59945 | 2015-11-26 10:07:32 +0000 | [diff] [blame] | 492 | if (type == RuleClassType.TEST != TargetUtils.isTestRuleName(ruleClassName)) { |
| 493 | throw new EvalException(definitionLocation, "Invalid rule class name '" + ruleClassName |
| 494 | + "', test rule class names must end with '_test' and other rule classes must not"); |
| 495 | } |
| 496 | for (Pair<String, SkylarkAttr.Descriptor> attribute : attributes) { |
Dmitry Lomov | 5a8f1c0 | 2015-11-26 10:49:16 +0000 | [diff] [blame] | 497 | SkylarkAttr.Descriptor descriptor = attribute.getSecond(); |
| 498 | Attribute.Builder<?> attributeBuilder = descriptor.getAttributeBuilder(); |
| 499 | for (SkylarkAspect skylarkAspect : descriptor.getAspects()) { |
| 500 | if (!skylarkAspect.isExported()) { |
| 501 | throw new EvalException(definitionLocation, |
| 502 | "All aspects applied to rule dependencies must be top-level values"); |
| 503 | } |
Dmitry Lomov | 82e0377 | 2015-11-30 12:13:22 +0000 | [diff] [blame] | 504 | attributeBuilder.aspect(skylarkAspect.getAspectClass()); |
Dmitry Lomov | 5a8f1c0 | 2015-11-26 10:49:16 +0000 | [diff] [blame] | 505 | } |
Dmitry Lomov | c1b1eed | 2015-11-26 11:18:12 +0000 | [diff] [blame] | 506 | |
Dmitry Lomov | 7b59945 | 2015-11-26 10:07:32 +0000 | [diff] [blame] | 507 | addAttribute(definitionLocation, builder, |
Dmitry Lomov | 5a8f1c0 | 2015-11-26 10:49:16 +0000 | [diff] [blame] | 508 | descriptor.getAttributeBuilder().build(attribute.getFirst())); |
Dmitry Lomov | 7b59945 | 2015-11-26 10:07:32 +0000 | [diff] [blame] | 509 | } |
| 510 | this.ruleClass = builder.build(ruleClassName); |
| 511 | |
| 512 | this.builder = null; |
| 513 | this.attributes = null; |
Francois-Rene Rideau | a8352e9 | 2015-06-30 16:59:42 +0000 | [diff] [blame] | 514 | } |
| 515 | |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 516 | @VisibleForTesting |
Dmitry Lomov | 7b59945 | 2015-11-26 10:07:32 +0000 | [diff] [blame] | 517 | public RuleClass getRuleClass() { |
| 518 | Preconditions.checkState(ruleClass != null && builder == null); |
| 519 | return ruleClass; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 520 | } |
| 521 | } |
| 522 | |
Dmitry Lomov | 7b59945 | 2015-11-26 10:07:32 +0000 | [diff] [blame] | 523 | public static void exportRuleFunctionsAndAspects(Environment env, Label skylarkLabel) |
| 524 | throws EvalException { |
Dmitry Lomov | 5a8f1c0 | 2015-11-26 10:49:16 +0000 | [diff] [blame] | 525 | Set<String> globalNames = env.getGlobals().getDirectVariableNames(); |
| 526 | |
| 527 | // Export aspects first since rules can depend on aspects. |
| 528 | for (String name : globalNames) { |
| 529 | Object value; |
| 530 | try { |
| 531 | value = env.lookup(name); |
| 532 | } catch (NoSuchVariableException e) { |
| 533 | throw new AssertionError(e); |
| 534 | } |
| 535 | if (value instanceof SkylarkAspect) { |
| 536 | SkylarkAspect skylarkAspect = (SkylarkAspect) value; |
| 537 | if (!skylarkAspect.isExported()) { |
| 538 | skylarkAspect.export(skylarkLabel, name); |
| 539 | } |
| 540 | } |
| 541 | } |
| 542 | |
| 543 | for (String name : globalNames) { |
Francois-Rene Rideau | a8352e9 | 2015-06-30 16:59:42 +0000 | [diff] [blame] | 544 | try { |
| 545 | Object value = env.lookup(name); |
| 546 | if (value instanceof RuleFunction) { |
| 547 | RuleFunction function = (RuleFunction) value; |
John Field | a97e17f | 2015-11-13 02:19:52 +0000 | [diff] [blame] | 548 | if (function.skylarkLabel == null) { |
| 549 | function.export(skylarkLabel, name); |
Dmitry Lomov | c15ba2e | 2015-10-30 15:50:01 +0000 | [diff] [blame] | 550 | } |
| 551 | } |
Francois-Rene Rideau | a8352e9 | 2015-06-30 16:59:42 +0000 | [diff] [blame] | 552 | } catch (NoSuchVariableException e) { |
| 553 | throw new AssertionError(e); |
| 554 | } |
| 555 | } |
| 556 | } |
| 557 | |
Francois-Rene Rideau | a3ac202 | 2015-04-20 18:35:05 +0000 | [diff] [blame] | 558 | @SkylarkSignature(name = "Label", doc = "Creates a Label referring to a BUILD target. Use " |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 559 | + "this function only when you want to give a default value for the label attributes. " |
Laurent Le Brun | faf7841 | 2015-07-28 16:13:00 +0000 | [diff] [blame] | 560 | + "The argument must refer to an absolute label. " |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 561 | + "Example: <br><pre class=language-python>Label(\"//tools:default\")</pre>", |
| 562 | returnType = Label.class, |
Francois-Rene Rideau | a3ac202 | 2015-04-20 18:35:05 +0000 | [diff] [blame] | 563 | mandatoryPositionals = {@Param(name = "label_string", type = String.class, |
| 564 | doc = "the label string")}, |
| 565 | useLocation = true) |
Francois-Rene Rideau | 537a90b | 2015-04-22 06:47:31 +0000 | [diff] [blame] | 566 | private static final BuiltinFunction label = new BuiltinFunction("Label") { |
| 567 | public Label invoke(String labelString, |
| 568 | Location loc) throws EvalException, ConversionException { |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 569 | try { |
| 570 | return labelCache.get(labelString); |
| 571 | } catch (ExecutionException e) { |
| 572 | throw new EvalException(loc, "Illegal absolute label syntax: " + labelString); |
| 573 | } |
Francois-Rene Rideau | 537a90b | 2015-04-22 06:47:31 +0000 | [diff] [blame] | 574 | } |
| 575 | }; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 576 | |
Francois-Rene Rideau | a3ac202 | 2015-04-20 18:35:05 +0000 | [diff] [blame] | 577 | @SkylarkSignature(name = "FileType", |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 578 | doc = "Creates a file filter from a list of strings. For example, to match files ending " |
| 579 | + "with .cc or .cpp, use: <pre class=language-python>FileType([\".cc\", \".cpp\"])</pre>", |
| 580 | returnType = SkylarkFileType.class, |
Francois-Rene Rideau | a3ac202 | 2015-04-20 18:35:05 +0000 | [diff] [blame] | 581 | mandatoryPositionals = { |
Francois-Rene Rideau | 537a90b | 2015-04-22 06:47:31 +0000 | [diff] [blame] | 582 | @Param(name = "types", type = SkylarkList.class, generic1 = String.class, defaultValue = "[]", |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 583 | doc = "a list of the accepted file extensions")}) |
Francois-Rene Rideau | 537a90b | 2015-04-22 06:47:31 +0000 | [diff] [blame] | 584 | private static final BuiltinFunction fileType = new BuiltinFunction("FileType") { |
Francois-Rene Rideau | 93ed7f1 | 2015-10-20 15:39:33 +0000 | [diff] [blame] | 585 | public SkylarkFileType invoke(SkylarkList types) throws EvalException { |
| 586 | return SkylarkFileType.of(types.getContents(String.class, "types")); |
Francois-Rene Rideau | 537a90b | 2015-04-22 06:47:31 +0000 | [diff] [blame] | 587 | } |
| 588 | }; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 589 | |
Francois-Rene Rideau | a3ac202 | 2015-04-20 18:35:05 +0000 | [diff] [blame] | 590 | @SkylarkSignature(name = "to_proto", |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 591 | doc = "Creates a text message from the struct parameter. This method only works if all " |
| 592 | + "struct elements (recursively) are strings, ints, booleans, other structs or a " |
| 593 | + "list of these types. Quotes and new lines in strings are escaped. " |
| 594 | + "Examples:<br><pre class=language-python>" |
| 595 | + "struct(key=123).to_proto()\n# key: 123\n\n" |
| 596 | + "struct(key=True).to_proto()\n# key: true\n\n" |
| 597 | + "struct(key=[1, 2, 3]).to_proto()\n# key: 1\n# key: 2\n# key: 3\n\n" |
| 598 | + "struct(key='text').to_proto()\n# key: \"text\"\n\n" |
| 599 | + "struct(key=struct(inner_key='text')).to_proto()\n" |
| 600 | + "# key {\n# inner_key: \"text\"\n# }\n\n" |
| 601 | + "struct(key=[struct(inner_key=1), struct(inner_key=2)]).to_proto()\n" |
| 602 | + "# key {\n# inner_key: 1\n# }\n# key {\n# inner_key: 2\n# }\n\n" |
| 603 | + "struct(key=struct(inner_key=struct(inner_inner_key='text'))).to_proto()\n" |
| 604 | + "# key {\n# inner_key {\n# inner_inner_key: \"text\"\n# }\n# }\n</pre>", |
Francois-Rene Rideau | a3ac202 | 2015-04-20 18:35:05 +0000 | [diff] [blame] | 605 | objectType = SkylarkClassObject.class, returnType = String.class, |
| 606 | mandatoryPositionals = { |
| 607 | // TODO(bazel-team): shouldn't we accept any ClassObject? |
| 608 | @Param(name = "self", type = SkylarkClassObject.class, |
| 609 | doc = "this struct")}, |
| 610 | useLocation = true) |
Francois-Rene Rideau | 537a90b | 2015-04-22 06:47:31 +0000 | [diff] [blame] | 611 | private static final BuiltinFunction toProto = new BuiltinFunction("to_proto") { |
| 612 | public String invoke(SkylarkClassObject self, Location loc) throws EvalException { |
| 613 | StringBuilder sb = new StringBuilder(); |
| 614 | printTextMessage(self, sb, 0, loc); |
| 615 | return sb.toString(); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 616 | } |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 617 | |
Francois-Rene Rideau | 537a90b | 2015-04-22 06:47:31 +0000 | [diff] [blame] | 618 | private void printTextMessage(ClassObject object, StringBuilder sb, |
| 619 | int indent, Location loc) throws EvalException { |
| 620 | for (String key : object.getKeys()) { |
| 621 | printTextMessage(key, object.getValue(key), sb, indent, loc); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 622 | } |
Francois-Rene Rideau | 537a90b | 2015-04-22 06:47:31 +0000 | [diff] [blame] | 623 | } |
| 624 | |
| 625 | private void printSimpleTextMessage(String key, Object value, StringBuilder sb, |
| 626 | int indent, Location loc, String container) throws EvalException { |
| 627 | if (value instanceof ClassObject) { |
| 628 | print(sb, key + " {", indent); |
| 629 | printTextMessage((ClassObject) value, sb, indent + 1, loc); |
| 630 | print(sb, "}", indent); |
| 631 | } else if (value instanceof String) { |
| 632 | print(sb, key + ": \"" + escape((String) value) + "\"", indent); |
| 633 | } else if (value instanceof Integer) { |
| 634 | print(sb, key + ": " + value, indent); |
| 635 | } else if (value instanceof Boolean) { |
| 636 | // We're relying on the fact that Java converts Booleans to Strings in the same way |
| 637 | // as the protocol buffers do. |
| 638 | print(sb, key + ": " + value, indent); |
| 639 | } else { |
| 640 | throw new EvalException(loc, |
| 641 | "Invalid text format, expected a struct, a string, a bool, or an int but got a " |
| 642 | + EvalUtils.getDataTypeName(value) + " for " + container + " '" + key + "'"); |
| 643 | } |
| 644 | } |
| 645 | |
| 646 | private void printTextMessage(String key, Object value, StringBuilder sb, |
| 647 | int indent, Location loc) throws EvalException { |
| 648 | if (value instanceof SkylarkList) { |
| 649 | for (Object item : ((SkylarkList) value)) { |
| 650 | // TODO(bazel-team): There should be some constraint on the fields of the structs |
| 651 | // in the same list but we ignore that for now. |
| 652 | printSimpleTextMessage(key, item, sb, indent, loc, "list element in struct field"); |
| 653 | } |
| 654 | } else { |
Francois-Rene Rideau | 93ed7f1 | 2015-10-20 15:39:33 +0000 | [diff] [blame] | 655 | printSimpleTextMessage(key, value, sb, indent, loc, "struct field"); |
Francois-Rene Rideau | 537a90b | 2015-04-22 06:47:31 +0000 | [diff] [blame] | 656 | } |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 657 | } |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 658 | |
Francois-Rene Rideau | 537a90b | 2015-04-22 06:47:31 +0000 | [diff] [blame] | 659 | private String escape(String string) { |
| 660 | // TODO(bazel-team): use guava's SourceCodeEscapers when it's released. |
| 661 | return string.replace("\"", "\\\"").replace("\n", "\\n"); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 662 | } |
Francois-Rene Rideau | 537a90b | 2015-04-22 06:47:31 +0000 | [diff] [blame] | 663 | |
| 664 | private void print(StringBuilder sb, String text, int indent) { |
| 665 | for (int i = 0; i < indent; i++) { |
| 666 | sb.append(" "); |
| 667 | } |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 668 | sb.append(text); |
| 669 | sb.append("\n"); |
Francois-Rene Rideau | 537a90b | 2015-04-22 06:47:31 +0000 | [diff] [blame] | 670 | } |
| 671 | }; |
| 672 | |
Dmitry Lomov | 4be5893 | 2015-12-23 13:21:32 +0000 | [diff] [blame] | 673 | @SkylarkSignature(name = "output_group", |
| 674 | documented = false, // TODO(dslomov): document. |
| 675 | objectType = TransitiveInfoCollection.class, |
| 676 | returnType = SkylarkNestedSet.class, |
| 677 | mandatoryPositionals = { |
| 678 | @Param(name = "self", type = TransitiveInfoCollection.class, doc = |
| 679 | "this target" |
| 680 | ), |
| 681 | @Param(name = "group_name", type = String.class, doc = |
| 682 | "Output group name" |
| 683 | ) |
| 684 | } |
| 685 | ) |
| 686 | public static final BuiltinFunction output_group = new BuiltinFunction("output_group") { |
| 687 | public SkylarkNestedSet invoke(TransitiveInfoCollection self, String group) { |
| 688 | OutputGroupProvider provider = self.getProvider(OutputGroupProvider.class); |
| 689 | NestedSet<Artifact> result = provider != null |
| 690 | ? provider.getOutputGroup(group) |
| 691 | : NestedSetBuilder.<Artifact>emptySet(Order.STABLE_ORDER); |
| 692 | return SkylarkNestedSet.of(Artifact.class, result); |
| 693 | } |
| 694 | }; |
| 695 | |
Francois-Rene Rideau | 537a90b | 2015-04-22 06:47:31 +0000 | [diff] [blame] | 696 | static { |
| 697 | SkylarkSignatureProcessor.configureSkylarkFunctions(SkylarkRuleClassFunctions.class); |
| 698 | } |
Dmitry Lomov | 0b832ce | 2015-10-20 10:03:14 +0000 | [diff] [blame] | 699 | |
| 700 | /** |
| 701 | * A Skylark value that is a result of 'aspect(..)' function call. |
| 702 | */ |
Dmitry Lomov | 82e0377 | 2015-11-30 12:13:22 +0000 | [diff] [blame] | 703 | public static final class SkylarkAspect implements SkylarkValue { |
Dmitry Lomov | 0b832ce | 2015-10-20 10:03:14 +0000 | [diff] [blame] | 704 | private final BaseFunction implementation; |
Dmitry Lomov | c15ba2e | 2015-10-30 15:50:01 +0000 | [diff] [blame] | 705 | private final ImmutableList<String> attributeAspects; |
Dmitry Lomov | ace678e | 2015-12-16 15:10:20 +0000 | [diff] [blame] | 706 | private final ImmutableList<Pair<String, Descriptor>> attributes; |
Michael Staib | cd48cd5 | 2016-01-15 20:11:11 +0000 | [diff] [blame] | 707 | private final ImmutableSet<String> fragments; |
| 708 | private final ImmutableSet<String> hostFragments; |
Dmitry Lomov | 0b832ce | 2015-10-20 10:03:14 +0000 | [diff] [blame] | 709 | private final Environment funcallEnv; |
Dmitry Lomov | c15ba2e | 2015-10-30 15:50:01 +0000 | [diff] [blame] | 710 | private Exported exported; |
Dmitry Lomov | 0b832ce | 2015-10-20 10:03:14 +0000 | [diff] [blame] | 711 | |
Dmitry Lomov | c15ba2e | 2015-10-30 15:50:01 +0000 | [diff] [blame] | 712 | public SkylarkAspect( |
| 713 | BaseFunction implementation, |
| 714 | ImmutableList<String> attributeAspects, |
Dmitry Lomov | ace678e | 2015-12-16 15:10:20 +0000 | [diff] [blame] | 715 | ImmutableList<Pair<String, Descriptor>> attributes, |
Michael Staib | cd48cd5 | 2016-01-15 20:11:11 +0000 | [diff] [blame] | 716 | ImmutableSet<String> fragments, |
| 717 | ImmutableSet<String> hostFragments, |
Dmitry Lomov | ace678e | 2015-12-16 15:10:20 +0000 | [diff] [blame] | 718 | Environment funcallEnv) { |
Dmitry Lomov | 0b832ce | 2015-10-20 10:03:14 +0000 | [diff] [blame] | 719 | this.implementation = implementation; |
Dmitry Lomov | c15ba2e | 2015-10-30 15:50:01 +0000 | [diff] [blame] | 720 | this.attributeAspects = attributeAspects; |
Dmitry Lomov | ace678e | 2015-12-16 15:10:20 +0000 | [diff] [blame] | 721 | this.attributes = attributes; |
Michael Staib | cd48cd5 | 2016-01-15 20:11:11 +0000 | [diff] [blame] | 722 | this.fragments = fragments; |
| 723 | this.hostFragments = hostFragments; |
Dmitry Lomov | 0b832ce | 2015-10-20 10:03:14 +0000 | [diff] [blame] | 724 | this.funcallEnv = funcallEnv; |
| 725 | } |
| 726 | |
| 727 | public BaseFunction getImplementation() { |
| 728 | return implementation; |
| 729 | } |
| 730 | |
Dmitry Lomov | c15ba2e | 2015-10-30 15:50:01 +0000 | [diff] [blame] | 731 | public ImmutableList<String> getAttributeAspects() { |
| 732 | return attributeAspects; |
| 733 | } |
| 734 | |
Dmitry Lomov | 0b832ce | 2015-10-20 10:03:14 +0000 | [diff] [blame] | 735 | public Environment getFuncallEnv() { |
| 736 | return funcallEnv; |
| 737 | } |
| 738 | |
Dmitry Lomov | ace678e | 2015-12-16 15:10:20 +0000 | [diff] [blame] | 739 | public ImmutableList<Pair<String, Descriptor>> getAttributes() { |
| 740 | return attributes; |
Dmitry Lomov | c1b1eed | 2015-11-26 11:18:12 +0000 | [diff] [blame] | 741 | } |
| 742 | |
Michael Staib | cd48cd5 | 2016-01-15 20:11:11 +0000 | [diff] [blame] | 743 | /** |
| 744 | * Gets the set of configuration fragment names needed in the target configuration. |
| 745 | */ |
| 746 | public ImmutableSet<String> getFragments() { |
| 747 | return fragments; |
| 748 | } |
| 749 | |
| 750 | /** |
| 751 | * Gets the set of configuration fragment names needed in the host configuration. |
| 752 | */ |
| 753 | public ImmutableSet<String> getHostFragments() { |
| 754 | return hostFragments; |
| 755 | } |
| 756 | |
Dmitry Lomov | 0b832ce | 2015-10-20 10:03:14 +0000 | [diff] [blame] | 757 | @Override |
| 758 | public boolean isImmutable() { |
| 759 | return implementation.isImmutable(); |
| 760 | } |
| 761 | |
| 762 | @Override |
| 763 | public void write(Appendable buffer, char quotationMark) { |
| 764 | Printer.append(buffer, "Aspect:"); |
| 765 | implementation.write(buffer, quotationMark); |
| 766 | } |
Dmitry Lomov | c15ba2e | 2015-10-30 15:50:01 +0000 | [diff] [blame] | 767 | |
| 768 | public String getName() { |
Dmitry Lomov | 82e0377 | 2015-11-30 12:13:22 +0000 | [diff] [blame] | 769 | return getAspectClass().getName(); |
| 770 | } |
| 771 | |
| 772 | public SkylarkAspectClass getAspectClass() { |
| 773 | Preconditions.checkState(isExported()); |
| 774 | return new SkylarkAspectClassImpl(this); |
Dmitry Lomov | c15ba2e | 2015-10-30 15:50:01 +0000 | [diff] [blame] | 775 | } |
| 776 | |
John Field | a97e17f | 2015-11-13 02:19:52 +0000 | [diff] [blame] | 777 | void export(Label extensionLabel, String name) { |
| 778 | this.exported = new Exported(extensionLabel, name); |
Dmitry Lomov | c15ba2e | 2015-10-30 15:50:01 +0000 | [diff] [blame] | 779 | } |
| 780 | |
| 781 | public boolean isExported() { |
| 782 | return exported != null; |
| 783 | } |
| 784 | |
John Field | a97e17f | 2015-11-13 02:19:52 +0000 | [diff] [blame] | 785 | private Label getExtensionLabel() { |
Dmitry Lomov | c15ba2e | 2015-10-30 15:50:01 +0000 | [diff] [blame] | 786 | Preconditions.checkArgument(isExported()); |
John Field | a97e17f | 2015-11-13 02:19:52 +0000 | [diff] [blame] | 787 | return exported.extensionLabel; |
Dmitry Lomov | c15ba2e | 2015-10-30 15:50:01 +0000 | [diff] [blame] | 788 | } |
| 789 | |
| 790 | private String getExportedName() { |
| 791 | Preconditions.checkArgument(isExported()); |
| 792 | return exported.name; |
| 793 | } |
| 794 | |
| 795 | @Immutable |
| 796 | private static class Exported { |
John Field | a97e17f | 2015-11-13 02:19:52 +0000 | [diff] [blame] | 797 | private final Label extensionLabel; |
Dmitry Lomov | c15ba2e | 2015-10-30 15:50:01 +0000 | [diff] [blame] | 798 | private final String name; |
| 799 | |
John Field | a97e17f | 2015-11-13 02:19:52 +0000 | [diff] [blame] | 800 | public Exported(Label extensionLabel, String name) { |
| 801 | this.extensionLabel = extensionLabel; |
Dmitry Lomov | c15ba2e | 2015-10-30 15:50:01 +0000 | [diff] [blame] | 802 | this.name = name; |
| 803 | } |
| 804 | |
John Field | a97e17f | 2015-11-13 02:19:52 +0000 | [diff] [blame] | 805 | @Override |
Dmitry Lomov | c15ba2e | 2015-10-30 15:50:01 +0000 | [diff] [blame] | 806 | public String toString() { |
John Field | a97e17f | 2015-11-13 02:19:52 +0000 | [diff] [blame] | 807 | return extensionLabel.toString() + "%" + name; |
Dmitry Lomov | c15ba2e | 2015-10-30 15:50:01 +0000 | [diff] [blame] | 808 | } |
| 809 | } |
| 810 | } |
| 811 | |
| 812 | /** |
| 813 | * Implementation of an aspect class defined in Skylark. |
| 814 | */ |
| 815 | @Immutable |
Dmitry Lomov | 82e0377 | 2015-11-30 12:13:22 +0000 | [diff] [blame] | 816 | private static final class SkylarkAspectClassImpl extends SkylarkAspectClass { |
Dmitry Lomov | c15ba2e | 2015-10-30 15:50:01 +0000 | [diff] [blame] | 817 | private final AspectDefinition aspectDefinition; |
John Field | a97e17f | 2015-11-13 02:19:52 +0000 | [diff] [blame] | 818 | private final Label extensionLabel; |
Dmitry Lomov | c15ba2e | 2015-10-30 15:50:01 +0000 | [diff] [blame] | 819 | private final String exportedName; |
| 820 | |
Dmitry Lomov | 82e0377 | 2015-11-30 12:13:22 +0000 | [diff] [blame] | 821 | public SkylarkAspectClassImpl(SkylarkAspect skylarkAspect) { |
Dmitry Lomov | c15ba2e | 2015-10-30 15:50:01 +0000 | [diff] [blame] | 822 | Preconditions.checkArgument(skylarkAspect.isExported(), "Skylark aspects must be exported"); |
Dmitry Lomov | 82e0377 | 2015-11-30 12:13:22 +0000 | [diff] [blame] | 823 | this.extensionLabel = skylarkAspect.getExtensionLabel(); |
| 824 | this.exportedName = skylarkAspect.getExportedName(); |
| 825 | |
| 826 | AspectDefinition.Builder builder = new AspectDefinition.Builder(getName()); |
Dmitry Lomov | c15ba2e | 2015-10-30 15:50:01 +0000 | [diff] [blame] | 827 | for (String attributeAspect : skylarkAspect.getAttributeAspects()) { |
| 828 | builder.attributeAspect(attributeAspect, this); |
| 829 | } |
Dmitry Lomov | ace678e | 2015-12-16 15:10:20 +0000 | [diff] [blame] | 830 | ImmutableList<Pair<String, Descriptor>> attributes = skylarkAspect.getAttributes(); |
| 831 | for (Pair<String, Descriptor> attribute : attributes) { |
| 832 | builder.add(attribute.second.getAttributeBuilder().build(attribute.first)); |
| 833 | } |
Michael Staib | cd48cd5 | 2016-01-15 20:11:11 +0000 | [diff] [blame] | 834 | builder.requiresConfigurationFragmentsBySkylarkModuleName(skylarkAspect.getFragments()); |
| 835 | builder.requiresHostConfigurationFragmentsBySkylarkModuleName( |
| 836 | skylarkAspect.getHostFragments()); |
Dmitry Lomov | c15ba2e | 2015-10-30 15:50:01 +0000 | [diff] [blame] | 837 | this.aspectDefinition = builder.build(); |
Dmitry Lomov | c15ba2e | 2015-10-30 15:50:01 +0000 | [diff] [blame] | 838 | } |
| 839 | |
| 840 | @Override |
Dmitry Lomov | 6231d08 | 2015-11-02 17:17:20 +0000 | [diff] [blame] | 841 | public AspectDefinition getDefinition(AspectParameters aspectParameters) { |
Dmitry Lomov | c15ba2e | 2015-10-30 15:50:01 +0000 | [diff] [blame] | 842 | return aspectDefinition; |
| 843 | } |
| 844 | |
Francois-Rene Rideau | c0a8c58 | 2016-01-28 18:36:22 +0000 | [diff] [blame^] | 845 | @Override |
John Field | a97e17f | 2015-11-13 02:19:52 +0000 | [diff] [blame] | 846 | public Label getExtensionLabel() { |
| 847 | return extensionLabel; |
Dmitry Lomov | c15ba2e | 2015-10-30 15:50:01 +0000 | [diff] [blame] | 848 | } |
| 849 | |
Francois-Rene Rideau | c0a8c58 | 2016-01-28 18:36:22 +0000 | [diff] [blame^] | 850 | @Override |
Dmitry Lomov | c15ba2e | 2015-10-30 15:50:01 +0000 | [diff] [blame] | 851 | public String getExportedName() { |
| 852 | return exportedName; |
| 853 | } |
| 854 | |
Dmitry Lomov | 0b832ce | 2015-10-20 10:03:14 +0000 | [diff] [blame] | 855 | } |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 856 | } |