cparsons | 6df5ec3 | 2018-04-17 14:09:54 -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 | |
cparsons | 981f65b | 2018-04-23 12:04:09 -0700 | [diff] [blame] | 15 | package com.google.devtools.build.lib.skylarkbuildapi; |
cparsons | 6df5ec3 | 2018-04-17 14:09:54 -0700 | [diff] [blame] | 16 | |
cparsons | 6df5ec3 | 2018-04-17 14:09:54 -0700 | [diff] [blame] | 17 | import com.google.devtools.build.lib.syntax.EvalException; |
Googler | a3421e2 | 2019-09-26 06:48:32 -0700 | [diff] [blame] | 18 | import com.google.devtools.build.lib.syntax.StarlarkThread; |
adonovan | b017468 | 2020-05-18 16:01:53 -0700 | [diff] [blame] | 19 | import net.starlark.java.annot.Param; |
| 20 | import net.starlark.java.annot.StarlarkGlobalLibrary; |
| 21 | import net.starlark.java.annot.StarlarkMethod; |
cparsons | 6df5ec3 | 2018-04-17 14:09:54 -0700 | [diff] [blame] | 22 | |
gregce | ad752cc | 2020-04-10 10:32:59 -0700 | [diff] [blame] | 23 | /** A collection of global Starlark build API functions that belong in the global namespace. */ |
gregce | 8345b79 | 2020-05-15 13:23:29 -0700 | [diff] [blame] | 24 | @StarlarkGlobalLibrary |
gregce | 5c8a5f5 | 2020-05-13 10:35:36 -0700 | [diff] [blame] | 25 | public interface StarlarkBuildApiGlobals { |
cparsons | 6df5ec3 | 2018-04-17 14:09:54 -0700 | [diff] [blame] | 26 | |
gregce | 8345b79 | 2020-05-15 13:23:29 -0700 | [diff] [blame] | 27 | @StarlarkMethod( |
cparsons | b380dc9 | 2018-12-05 13:57:39 -0800 | [diff] [blame] | 28 | name = "configuration_field", |
gregce | 5c8a5f5 | 2020-05-13 10:35:36 -0700 | [diff] [blame] | 29 | // TODO(cparsons): Provide a link to documentation for available StarlarkConfigurationFields. |
cparsons | b380dc9 | 2018-12-05 13:57:39 -0800 | [diff] [blame] | 30 | doc = |
| 31 | "References a late-bound default value for an attribute of type " |
| 32 | + "<a href=\"attr.html#label\">label</a>. A value is 'late-bound' if it requires " |
| 33 | + "the configuration to be built before determining the value. Any attribute using " |
| 34 | + "this as a value must <a href=\"../rules.html#private-attributes\">be private</a>. " |
| 35 | + "<p>Example usage: " |
| 36 | + "<p>Defining a rule attribute: <br><pre class=language-python>" |
| 37 | + "'_foo': attr.label(default=configuration_field(fragment='java', " |
| 38 | + "name='toolchain'))</pre>" |
| 39 | + "<p>Accessing in rule implementation: <br><pre class=language-python>" |
| 40 | + " def _rule_impl(ctx):\n" |
| 41 | + " foo_info = ctx.attr._foo\n" |
| 42 | + " ...</pre>", |
| 43 | parameters = { |
cparsons | 6df5ec3 | 2018-04-17 14:09:54 -0700 | [diff] [blame] | 44 | @Param( |
| 45 | name = "fragment", |
| 46 | type = String.class, |
| 47 | named = true, |
cparsons | b380dc9 | 2018-12-05 13:57:39 -0800 | [diff] [blame] | 48 | doc = "The name of a configuration fragment which contains the late-bound value."), |
cparsons | 6df5ec3 | 2018-04-17 14:09:54 -0700 | [diff] [blame] | 49 | @Param( |
| 50 | name = "name", |
| 51 | type = String.class, |
| 52 | named = true, |
| 53 | doc = "The name of the value to obtain from the configuration fragment."), |
cparsons | b380dc9 | 2018-12-05 13:57:39 -0800 | [diff] [blame] | 54 | }, |
Googler | a3421e2 | 2019-09-26 06:48:32 -0700 | [diff] [blame] | 55 | useStarlarkThread = true) |
adonovan | 7891d3b | 2020-01-22 12:40:50 -0800 | [diff] [blame] | 56 | LateBoundDefaultApi configurationField(String fragment, String name, StarlarkThread thread) |
| 57 | throws EvalException; |
cparsons | 6df5ec3 | 2018-04-17 14:09:54 -0700 | [diff] [blame] | 58 | } |