diff options
author | 2018-04-17 14:09:54 -0700 | |
---|---|---|
committer | 2018-04-17 14:11:38 -0700 | |
commit | 6df5ec3a1ab47a1cde39b5f7dafc1788248efb56 (patch) | |
tree | e97343eef5c97954e508feb7bab9d8bff1543b87 /src/main/java | |
parent | d7aebdbc567a6b015b6fc73835100f4c1725d691 (diff) |
Migrate configuration_field() to SkylarkCallable.
Previously this was a member of SkylarkAttr, which didn't make much sense, as the function was not under the attr namespace. This moves configuration_field to a new spot for global functions, "SkylarkBuildApiGlobals".
RELNOTES: None.
PiperOrigin-RevId: 193253435
Diffstat (limited to 'src/main/java')
3 files changed, 73 insertions, 50 deletions
diff --git a/src/main/java/com/google/devtools/build/lib/analysis/skylark/SkylarkAttr.java b/src/main/java/com/google/devtools/build/lib/analysis/skylark/SkylarkAttr.java index 4afe8c29d6..423fab890a 100644 --- a/src/main/java/com/google/devtools/build/lib/analysis/skylark/SkylarkAttr.java +++ b/src/main/java/com/google/devtools/build/lib/analysis/skylark/SkylarkAttr.java @@ -41,9 +41,7 @@ import com.google.devtools.build.lib.skylarkinterface.SkylarkCallable; import com.google.devtools.build.lib.skylarkinterface.SkylarkModule; import com.google.devtools.build.lib.skylarkinterface.SkylarkModuleCategory; import com.google.devtools.build.lib.skylarkinterface.SkylarkPrinter; -import com.google.devtools.build.lib.skylarkinterface.SkylarkSignature; import com.google.devtools.build.lib.skylarkinterface.SkylarkValue; -import com.google.devtools.build.lib.syntax.BuiltinFunction; import com.google.devtools.build.lib.syntax.Environment; import com.google.devtools.build.lib.syntax.EvalException; import com.google.devtools.build.lib.syntax.EvalUtils; @@ -52,7 +50,6 @@ import com.google.devtools.build.lib.syntax.Runtime; import com.google.devtools.build.lib.syntax.SkylarkCallbackFunction; import com.google.devtools.build.lib.syntax.SkylarkDict; import com.google.devtools.build.lib.syntax.SkylarkList; -import com.google.devtools.build.lib.syntax.SkylarkSignatureProcessor; import com.google.devtools.build.lib.syntax.SkylarkType; import com.google.devtools.build.lib.syntax.SkylarkUtils; import com.google.devtools.build.lib.syntax.Type; @@ -537,49 +534,6 @@ public final class SkylarkAttr implements SkylarkValue { env); } - @SkylarkSignature( - name = "configuration_field", - returnType = SkylarkLateBoundDefault.class, - // TODO(cparsons): Provide a link to documentation for available SkylarkConfigurationFields. - doc = "References a late-bound default value for an attribute of type " - + "<a href=\"attr.html#label\">label</a>. A value is 'late-bound' if it requires " - + "the configuration to be built before determining the value. Any attribute using this " - + "as a value must <a href=\"../rules.html#private-attributes\">be private</a>.", - parameters = { - @Param( - name = "fragment", - type = String.class, - doc = "The name of a configuration fragment which contains the late-bound value." - ), - @Param( - name = "name", - type = String.class, - doc = "The name of the value to obtain from the configuration fragment."), - }, - useLocation = true, - useEnvironment = true - ) - private static final BuiltinFunction configurationField = - new BuiltinFunction("configuration_field") { - public SkylarkLateBoundDefault<?> invoke( - String fragment, String name, Location loc, Environment env) - throws EvalException { - Class<?> fragmentClass = SkylarkUtils.getFragmentMap(env).get(fragment); - - if (fragmentClass == null) { - throw new EvalException( - loc, - String.format("invalid configuration fragment name '%s'", fragment)); - } - try { - return SkylarkLateBoundDefault.forConfigurationField( - fragmentClass, name, SkylarkUtils.getToolsRepository(env)); - } catch (SkylarkLateBoundDefault.InvalidConfigurationFieldException exception) { - throw new EvalException(loc, exception); - } - } - }; - @SkylarkCallable( name = "string", doc = "Creates an attribute of type <a href=\"string.html\">string</a>.", @@ -1708,8 +1662,4 @@ public final class SkylarkAttr implements SkylarkValue { printer.append("<attr." + name + ">"); } } - - static { - SkylarkSignatureProcessor.configureSkylarkFunctions(SkylarkAttr.class); - } } diff --git a/src/main/java/com/google/devtools/build/lib/analysis/skylark/SkylarkBuildApiGlobals.java b/src/main/java/com/google/devtools/build/lib/analysis/skylark/SkylarkBuildApiGlobals.java new file mode 100644 index 0000000000..a8851c92c4 --- /dev/null +++ b/src/main/java/com/google/devtools/build/lib/analysis/skylark/SkylarkBuildApiGlobals.java @@ -0,0 +1,72 @@ +// Copyright 2018 The Bazel Authors. All rights reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package com.google.devtools.build.lib.analysis.skylark; + + +import com.google.devtools.build.lib.events.Location; +import com.google.devtools.build.lib.skylarkinterface.Param; +import com.google.devtools.build.lib.skylarkinterface.SkylarkCallable; +import com.google.devtools.build.lib.skylarkinterface.SkylarkGlobalLibrary; +import com.google.devtools.build.lib.syntax.Environment; +import com.google.devtools.build.lib.syntax.EvalException; +import com.google.devtools.build.lib.syntax.SkylarkUtils; + +/** + * A collection of global skylark build API functions that belong in the global namespace. + */ +@SkylarkGlobalLibrary +public class SkylarkBuildApiGlobals { + + @SkylarkCallable( + name = "configuration_field", + // TODO(cparsons): Provide a link to documentation for available SkylarkConfigurationFields. + doc = "References a late-bound default value for an attribute of type " + + "<a href=\"attr.html#label\">label</a>. A value is 'late-bound' if it requires " + + "the configuration to be built before determining the value. Any attribute using this " + + "as a value must <a href=\"../rules.html#private-attributes\">be private</a>.", + parameters = { + @Param( + name = "fragment", + type = String.class, + named = true, + doc = "The name of a configuration fragment which contains the late-bound value." + ), + @Param( + name = "name", + type = String.class, + named = true, + doc = "The name of the value to obtain from the configuration fragment."), + }, + useLocation = true, + useEnvironment = true + ) + public SkylarkLateBoundDefault<?> configurationField( + String fragment, String name, Location loc, Environment env) + throws EvalException { + Class<?> fragmentClass = SkylarkUtils.getFragmentMap(env).get(fragment); + + if (fragmentClass == null) { + throw new EvalException( + loc, + String.format("invalid configuration fragment name '%s'", fragment)); + } + try { + return SkylarkLateBoundDefault.forConfigurationField( + fragmentClass, name, SkylarkUtils.getToolsRepository(env)); + } catch (SkylarkLateBoundDefault.InvalidConfigurationFieldException exception) { + throw new EvalException(loc, exception); + } + } +} diff --git a/src/main/java/com/google/devtools/build/lib/analysis/skylark/SkylarkModules.java b/src/main/java/com/google/devtools/build/lib/analysis/skylark/SkylarkModules.java index 3ed8e1003d..17129ee6f4 100644 --- a/src/main/java/com/google/devtools/build/lib/analysis/skylark/SkylarkModules.java +++ b/src/main/java/com/google/devtools/build/lib/analysis/skylark/SkylarkModules.java @@ -39,6 +39,7 @@ public final class SkylarkModules { public static final ImmutableList<Class<?>> MODULES = ImmutableList.of( SkylarkAttr.class, + SkylarkBuildApiGlobals.class, SkylarkCommandLine.class, SkylarkNativeModule.class, SkylarkRuleClassFunctions.class); |