blob: c833f144d02ddf8a7ce5396cac95bb1eba126336 [file] [log] [blame]
Dmitry Lomov8ff06452015-10-21 19:16:30 +00001// 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
15package com.google.devtools.build.lib.packages;
16
17/**
janakr64d9a4d2018-02-02 12:38:58 -080018 * A class of aspects.
Lukacs Berki2300cd62016-05-19 11:06:37 +000019 *
janakr64d9a4d2018-02-02 12:38:58 -080020 * <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 Berki2300cd62016-05-19 11:06:37 +000025 *
janakr64d9a4d2018-02-02 12:38:58 -080026 * <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 Berki2300cd62016-05-19 11:06:37 +000032 *
janakr64d9a4d2018-02-02 12:38:58 -080033 * <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 Berki2300cd62016-05-19 11:06:37 +000039 *
janakr64d9a4d2018-02-02 12:38:58 -080040 * <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 Berki2300cd62016-05-19 11:06:37 +000046 *
gregcead752cc2020-04-10 10:32:59 -070047 * <p>Aspects can be defined natively, in Java ({@link NativeAspectClass}) or in Starlark ({@link
gregce18694cd2020-05-12 15:40:05 -070048 * StarlarkAspectClass}).
Dmitry Lomov8ff06452015-10-21 19:16:30 +000049 *
janakr64d9a4d2018-02-02 12:38:58 -080050 * <p>Bazel propagates aspects through a multistage process. The general pipeline is as follows:
Dmitry Lomovc15ba2e2015-10-30 15:50:01 +000051 *
janakr64d9a4d2018-02-02 12:38:58 -080052 * <pre>
Dmitry Lomove8040172016-04-06 14:53:43 +000053 * {@link AspectClass}
54 * |
55 * V
messaa4063f62021-09-09 10:14:45 -070056 * {@code AspectDescriptor} <- {@link AspectParameters}
Dmitry Lomove8040172016-04-06 14:53:43 +000057 * \
58 * V
gregcead752cc2020-04-10 10:32:59 -070059 * {@link Aspect} <- {@link AspectDefinition} (might require loading Starlark files)
Dmitry Lomove8040172016-04-06 14:53:43 +000060 * |
61 * V
62 * {@code ConfiguredAspect} <- {@code ConfiguredTarget}
63 * </pre>
64 *
janakr64d9a4d2018-02-02 12:38:58 -080065 * <ul>
66 * <li>{@link AspectClass} is a moniker for "user" definition of the aspect, be it a native aspect
gregce3377c112020-04-13 09:29:59 -070067 * 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.
janakr64d9a4d2018-02-02 12:38:58 -080069 * 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
messaa4063f62021-09-09 10:14:45 -070072 * <li>{@link AspectDescriptor} is a pair of {@code AspectClass} and {@link AspectParameters}. It
73 * uniquely identifies the aspect and can be used in SkyKeys.
janakr64d9a4d2018-02-02 12:38:58 -080074 * <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
gregce3377c112020-04-13 09:29:59 -070077 * {@code Aspect} from {@code AspectDescriptor} for Starlark aspects requires adding a
78 * Skyframe dependency.
janakr64d9a4d2018-02-02 12:38:58 -080079 * <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 Lomove8040172016-04-06 14:53:43 +000083 *
janakr64d9a4d2018-02-02 12:38:58 -080084 * {@link AspectDescriptor}, or in general, a tuple of ({@link AspectClass}, {@link
messaa4063f62021-09-09 10:14:45 -070085 * 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 Berki2300cd62016-05-19 11:06:37 +000088 *
janakr64d9a4d2018-02-02 12:38:58 -080089 * @see com.google.devtools.build.lib.analysis.RuleConfiguredTargetFactory
90 * @see com.google.devtools.build.lib.skyframe.AspectFunction
Dmitry Lomov8ff06452015-10-21 19:16:30 +000091 */
92public interface AspectClass {
Dmitry Lomov8ff06452015-10-21 19:16:30 +000093 /**
94 * Returns an aspect name.
95 */
96 String getName();
tomlu72642a22017-10-18 06:23:14 +020097
98 default String getKey() {
99 return getName();
100 }
Dmitry Lomov8ff06452015-10-21 19:16:30 +0000101}