Dmitry Lomov | 8ff0645 | 2015-10-21 19:16:30 +0000 | [diff] [blame] | 1 | // Copyright 2015 The Bazel Authors. All rights reserved. |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | package com.google.devtools.build.lib.packages; |
| 16 | |
| 17 | /** |
janakr | 64d9a4d | 2018-02-02 12:38:58 -0800 | [diff] [blame] | 18 | * A class of aspects. |
Lukacs Berki | 2300cd6 | 2016-05-19 11:06:37 +0000 | [diff] [blame] | 19 | * |
janakr | 64d9a4d | 2018-02-02 12:38:58 -0800 | [diff] [blame] | 20 | * <p>An aspect allows a rule to create actions in its dependencies, without their knowledge. It can |
| 21 | * be viewed as the ability to attach shadow targets to transitive dependencies or a way to run |
| 22 | * visitations of certain parts of the transitive closure of a rule in such a way that can be cached |
| 23 | * (even partially) and reused between different configured targets requiring the same aspect. Some |
| 24 | * examples where aspects are useful: |
Lukacs Berki | 2300cd6 | 2016-05-19 11:06:37 +0000 | [diff] [blame] | 25 | * |
janakr | 64d9a4d | 2018-02-02 12:38:58 -0800 | [diff] [blame] | 26 | * <ul> |
| 27 | * <li>Converting the .jar files in the transitive closure of an Android binary to dexes |
| 28 | * <li>Emitting Java sources for a <code>proto_library</code> and the messages it depends on |
| 29 | * <li>Collecting all the dependencies of a rule to make sure that it does not contain a forbidden |
| 30 | * one |
| 31 | * </ul> |
Lukacs Berki | 2300cd6 | 2016-05-19 11:06:37 +0000 | [diff] [blame] | 32 | * |
janakr | 64d9a4d | 2018-02-02 12:38:58 -0800 | [diff] [blame] | 33 | * <p>When a configured target requests that an aspect be attached to one of its dependencies, the |
| 34 | * {@link com.google.devtools.build.lib.analysis.TransitiveInfoProvider}s generated by that aspects |
| 35 | * are merged with those of the actual dependency, that is, {@link |
| 36 | * com.google.devtools.build.lib.analysis.RuleContext#getPrerequisite( String, |
| 37 | * RuleConfiguredTarget.Mode)} will contain the transitive info providers produced both by the |
| 38 | * dependency and the aspects that are attached to it. |
Lukacs Berki | 2300cd6 | 2016-05-19 11:06:37 +0000 | [diff] [blame] | 39 | * |
janakr | 64d9a4d | 2018-02-02 12:38:58 -0800 | [diff] [blame] | 40 | * <p>Configured targets can specify which aspects should be attached to some of their dependencies |
| 41 | * by specifying this in their {@link com.google.devtools.build.lib.analysis.RuleDefinition}: each |
| 42 | * attribute can have a list of aspects to be applied to the rules in that attribute and each aspect |
| 43 | * can specify which {@link com.google.devtools.build.lib.analysis.TransitiveInfoProvider}s it needs |
| 44 | * on a rule so that it can do meaningful work (for example, dexing only makes sense for configured |
| 45 | * targets that produce Java code). |
Lukacs Berki | 2300cd6 | 2016-05-19 11:06:37 +0000 | [diff] [blame] | 46 | * |
gregce | ad752cc | 2020-04-10 10:32:59 -0700 | [diff] [blame] | 47 | * <p>Aspects can be defined natively, in Java ({@link NativeAspectClass}) or in Starlark ({@link |
gregce | 18694cd | 2020-05-12 15:40:05 -0700 | [diff] [blame] | 48 | * StarlarkAspectClass}). |
Dmitry Lomov | 8ff0645 | 2015-10-21 19:16:30 +0000 | [diff] [blame] | 49 | * |
janakr | 64d9a4d | 2018-02-02 12:38:58 -0800 | [diff] [blame] | 50 | * <p>Bazel propagates aspects through a multistage process. The general pipeline is as follows: |
Dmitry Lomov | c15ba2e | 2015-10-30 15:50:01 +0000 | [diff] [blame] | 51 | * |
janakr | 64d9a4d | 2018-02-02 12:38:58 -0800 | [diff] [blame] | 52 | * <pre> |
Dmitry Lomov | e804017 | 2016-04-06 14:53:43 +0000 | [diff] [blame] | 53 | * {@link AspectClass} |
| 54 | * | |
| 55 | * V |
messa | a4063f6 | 2021-09-09 10:14:45 -0700 | [diff] [blame] | 56 | * {@code AspectDescriptor} <- {@link AspectParameters} |
Dmitry Lomov | e804017 | 2016-04-06 14:53:43 +0000 | [diff] [blame] | 57 | * \ |
| 58 | * V |
gregce | ad752cc | 2020-04-10 10:32:59 -0700 | [diff] [blame] | 59 | * {@link Aspect} <- {@link AspectDefinition} (might require loading Starlark files) |
Dmitry Lomov | e804017 | 2016-04-06 14:53:43 +0000 | [diff] [blame] | 60 | * | |
| 61 | * V |
| 62 | * {@code ConfiguredAspect} <- {@code ConfiguredTarget} |
| 63 | * </pre> |
| 64 | * |
janakr | 64d9a4d | 2018-02-02 12:38:58 -0800 | [diff] [blame] | 65 | * <ul> |
| 66 | * <li>{@link AspectClass} is a moniker for "user" definition of the aspect, be it a native aspect |
gregce | 3377c11 | 2020-04-13 09:29:59 -0700 | [diff] [blame] | 67 | * or a Starlark aspect. It contains either a reference to the native class implementing the |
| 68 | * aspect or the location of the Starlark definition of the aspect in the source tree, i.e. |
janakr | 64d9a4d | 2018-02-02 12:38:58 -0800 | [diff] [blame] | 69 | * label of .bzl file + symbol name. |
| 70 | * <li>{@link AspectParameters} is a (key,value) pair list that can be used to parameterize aspect |
| 71 | * classes |
messa | a4063f6 | 2021-09-09 10:14:45 -0700 | [diff] [blame] | 72 | * <li>{@link AspectDescriptor} is a pair of {@code AspectClass} and {@link AspectParameters}. It |
| 73 | * uniquely identifies the aspect and can be used in SkyKeys. |
janakr | 64d9a4d | 2018-02-02 12:38:58 -0800 | [diff] [blame] | 74 | * <li>{@link AspectDefinition} is a class encapsulating the aspect definition (what attributes |
| 75 | * aspoect has, and along which dependencies does it propagate. |
| 76 | * <li>{@link Aspect} is a fully instantiated instance of an Aspect after it is loaded. Getting an |
gregce | 3377c11 | 2020-04-13 09:29:59 -0700 | [diff] [blame] | 77 | * {@code Aspect} from {@code AspectDescriptor} for Starlark aspects requires adding a |
| 78 | * Skyframe dependency. |
janakr | 64d9a4d | 2018-02-02 12:38:58 -0800 | [diff] [blame] | 79 | * <li>{@link com.google.devtools.build.lib.analysis.ConfiguredAspect} represents a result of |
| 80 | * application of an {@link Aspect} to a given {@link |
| 81 | * com.google.devtools.build.lib.analysis.ConfiguredTarget}. |
| 82 | * </ul> |
Dmitry Lomov | e804017 | 2016-04-06 14:53:43 +0000 | [diff] [blame] | 83 | * |
janakr | 64d9a4d | 2018-02-02 12:38:58 -0800 | [diff] [blame] | 84 | * {@link AspectDescriptor}, or in general, a tuple of ({@link AspectClass}, {@link |
messa | a4063f6 | 2021-09-09 10:14:45 -0700 | [diff] [blame] | 85 | * AspectParameters}) is an identifier that should be used in SkyKeys or in other contexts that need |
| 86 | * equality for aspects. See also {@link com.google.devtools.build.lib.skyframe.AspectFunction} for |
| 87 | * details on Skyframe treatment of Aspects. |
Lukacs Berki | 2300cd6 | 2016-05-19 11:06:37 +0000 | [diff] [blame] | 88 | * |
janakr | 64d9a4d | 2018-02-02 12:38:58 -0800 | [diff] [blame] | 89 | * @see com.google.devtools.build.lib.analysis.RuleConfiguredTargetFactory |
| 90 | * @see com.google.devtools.build.lib.skyframe.AspectFunction |
Dmitry Lomov | 8ff0645 | 2015-10-21 19:16:30 +0000 | [diff] [blame] | 91 | */ |
| 92 | public interface AspectClass { |
Dmitry Lomov | 8ff0645 | 2015-10-21 19:16:30 +0000 | [diff] [blame] | 93 | /** |
| 94 | * Returns an aspect name. |
| 95 | */ |
| 96 | String getName(); |
tomlu | 72642a2 | 2017-10-18 06:23:14 +0200 | [diff] [blame] | 97 | |
| 98 | default String getKey() { |
| 99 | return getName(); |
| 100 | } |
Dmitry Lomov | 8ff0645 | 2015-10-21 19:16:30 +0000 | [diff] [blame] | 101 | } |