Damien Martin-Guillerez | f88f4d8 | 2015-09-25 13:56:55 +0000 | [diff] [blame] | 1 | // Copyright 2015 The Bazel Authors. All rights reserved. |
Marian Lobur | 702cad7 | 2015-09-02 09:53:58 +0000 | [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. |
Dmitry Lomov | 6231d08 | 2015-11-02 17:17:20 +0000 | [diff] [blame] | 14 | package com.google.devtools.build.lib.packages; |
Marian Lobur | 702cad7 | 2015-09-02 09:53:58 +0000 | [diff] [blame] | 15 | |
seancurran | 128123f | 2021-06-17 19:06:18 -0700 | [diff] [blame^] | 16 | import com.github.benmanes.caffeine.cache.Caffeine; |
| 17 | import com.github.benmanes.caffeine.cache.LoadingCache; |
tomlu | a155b53 | 2017-11-08 20:12:47 +0100 | [diff] [blame] | 18 | import com.google.common.base.Preconditions; |
messa | f8c3408 | 2021-06-04 02:51:04 -0700 | [diff] [blame] | 19 | import com.google.common.collect.ImmutableSet; |
Dmitry Lomov | e804017 | 2016-04-06 14:53:43 +0000 | [diff] [blame] | 20 | import com.google.devtools.build.lib.concurrent.ThreadSafety.Immutable; |
janakr | ee5bf48 | 2018-03-26 10:55:59 -0700 | [diff] [blame] | 21 | import com.google.devtools.build.lib.skyframe.serialization.DeserializationContext; |
| 22 | import com.google.devtools.build.lib.skyframe.serialization.ObjectCodec; |
| 23 | import com.google.devtools.build.lib.skyframe.serialization.SerializationContext; |
| 24 | import com.google.devtools.build.lib.skyframe.serialization.SerializationException; |
| 25 | import com.google.protobuf.CodedInputStream; |
| 26 | import com.google.protobuf.CodedOutputStream; |
| 27 | import java.io.IOException; |
Marian Lobur | 702cad7 | 2015-09-02 09:53:58 +0000 | [diff] [blame] | 28 | |
Marian Lobur | 702cad7 | 2015-09-02 09:53:58 +0000 | [diff] [blame] | 29 | /** |
Dmitry Lomov | e804017 | 2016-04-06 14:53:43 +0000 | [diff] [blame] | 30 | * An instance of a given {@code AspectClass} with loaded definition and parameters. |
| 31 | * |
| 32 | * This is an aspect equivalent of {@link Rule} class for build rules. |
| 33 | * |
| 34 | * Note: this class does not have {@code equals()} and {@code hashCode()} redefined, so should |
| 35 | * not be used in SkyKeys. |
Marian Lobur | 702cad7 | 2015-09-02 09:53:58 +0000 | [diff] [blame] | 36 | */ |
Dmitry Lomov | e804017 | 2016-04-06 14:53:43 +0000 | [diff] [blame] | 37 | @Immutable |
Dmitry Lomov | 940ea07 | 2016-01-21 22:34:14 +0000 | [diff] [blame] | 38 | public final class Aspect implements DependencyFilter.AttributeInfoProvider { |
Carmi Grushko | 238bf4e | 2016-07-22 16:11:34 +0000 | [diff] [blame] | 39 | |
tomlu | 8306d43 | 2017-08-11 21:32:01 +0200 | [diff] [blame] | 40 | /** |
| 41 | * The aspect definition is a function of the aspect class + its parameters, so we can cache that. |
| 42 | * |
| 43 | * <p>The native aspects are loaded with blaze and are not stateful. Reference equality works fine |
| 44 | * in this case. |
| 45 | * |
gregce | 3377c11 | 2020-04-13 09:29:59 -0700 | [diff] [blame] | 46 | * <p>Caching of Starlark aspects is not yet implemented. |
tomlu | 8306d43 | 2017-08-11 21:32:01 +0200 | [diff] [blame] | 47 | */ |
nharmata | 4c8c152 | 2019-02-25 14:25:44 -0800 | [diff] [blame] | 48 | private static final LoadingCache< |
| 49 | NativeAspectClass, LoadingCache<AspectParameters, AspectDefinition>> |
| 50 | definitionCache = |
seancurran | 128123f | 2021-06-17 19:06:18 -0700 | [diff] [blame^] | 51 | Caffeine.newBuilder() |
nharmata | 4c8c152 | 2019-02-25 14:25:44 -0800 | [diff] [blame] | 52 | .build( |
seancurran | 128123f | 2021-06-17 19:06:18 -0700 | [diff] [blame^] | 53 | nativeAspectClass -> |
| 54 | Caffeine.newBuilder().build(nativeAspectClass::getDefinition)); |
tomlu | 8306d43 | 2017-08-11 21:32:01 +0200 | [diff] [blame] | 55 | |
Dmitry Lomov | 1575652 | 2016-12-16 16:52:37 +0000 | [diff] [blame] | 56 | private final AspectDescriptor aspectDescriptor; |
Dmitry Lomov | e804017 | 2016-04-06 14:53:43 +0000 | [diff] [blame] | 57 | private final AspectDefinition aspectDefinition; |
Marian Lobur | 702cad7 | 2015-09-02 09:53:58 +0000 | [diff] [blame] | 58 | |
Dmitry Lomov | e804017 | 2016-04-06 14:53:43 +0000 | [diff] [blame] | 59 | private Aspect( |
| 60 | AspectClass aspectClass, |
| 61 | AspectDefinition aspectDefinition, |
| 62 | AspectParameters parameters) { |
Dmitry Lomov | 1575652 | 2016-12-16 16:52:37 +0000 | [diff] [blame] | 63 | this.aspectDescriptor = new AspectDescriptor( |
| 64 | Preconditions.checkNotNull(aspectClass), |
| 65 | Preconditions.checkNotNull(parameters)); |
Dmitry Lomov | e804017 | 2016-04-06 14:53:43 +0000 | [diff] [blame] | 66 | this.aspectDefinition = Preconditions.checkNotNull(aspectDefinition); |
Marian Lobur | 702cad7 | 2015-09-02 09:53:58 +0000 | [diff] [blame] | 67 | } |
| 68 | |
messa | f8c3408 | 2021-06-04 02:51:04 -0700 | [diff] [blame] | 69 | private Aspect( |
| 70 | AspectClass aspectClass, |
| 71 | AspectDefinition aspectDefinition, |
| 72 | AspectParameters parameters, |
| 73 | RequiredProviders inheritedRequiredProviders, |
| 74 | ImmutableSet<String> inheritedAttributeAspects) { |
| 75 | this.aspectDescriptor = |
| 76 | new AspectDescriptor( |
| 77 | Preconditions.checkNotNull(aspectClass), |
| 78 | Preconditions.checkNotNull(parameters), |
messa | ed7ddfa | 2021-06-09 05:45:34 -0700 | [diff] [blame] | 79 | inheritedRequiredProviders, |
messa | f8c3408 | 2021-06-04 02:51:04 -0700 | [diff] [blame] | 80 | inheritedAttributeAspects); |
| 81 | this.aspectDefinition = Preconditions.checkNotNull(aspectDefinition); |
| 82 | } |
| 83 | |
| 84 | public static Aspect forNative( |
| 85 | NativeAspectClass nativeAspectClass, |
| 86 | AspectParameters parameters, |
| 87 | RequiredProviders inheritedRequiredProviders, |
| 88 | ImmutableSet<String> inheritedAttributeAspects) { |
seancurran | 128123f | 2021-06-17 19:06:18 -0700 | [diff] [blame^] | 89 | AspectDefinition definition = definitionCache.get(nativeAspectClass).get(parameters); |
messa | f8c3408 | 2021-06-04 02:51:04 -0700 | [diff] [blame] | 90 | return new Aspect( |
| 91 | nativeAspectClass, |
| 92 | definition, |
| 93 | parameters, |
| 94 | inheritedRequiredProviders, |
| 95 | inheritedAttributeAspects); |
| 96 | } |
| 97 | |
Dmitry Lomov | e804017 | 2016-04-06 14:53:43 +0000 | [diff] [blame] | 98 | public static Aspect forNative( |
Luis Fernando Pino Duque | e82713d | 2016-04-26 16:22:38 +0000 | [diff] [blame] | 99 | NativeAspectClass nativeAspectClass, AspectParameters parameters) { |
seancurran | 128123f | 2021-06-17 19:06:18 -0700 | [diff] [blame^] | 100 | AspectDefinition definition = definitionCache.get(nativeAspectClass).get(parameters); |
tomlu | 8306d43 | 2017-08-11 21:32:01 +0200 | [diff] [blame] | 101 | return new Aspect(nativeAspectClass, definition, parameters); |
Dmitry Lomov | e804017 | 2016-04-06 14:53:43 +0000 | [diff] [blame] | 102 | } |
| 103 | |
Luis Fernando Pino Duque | e82713d | 2016-04-26 16:22:38 +0000 | [diff] [blame] | 104 | public static Aspect forNative(NativeAspectClass nativeAspectClass) { |
Dmitry Lomov | e804017 | 2016-04-06 14:53:43 +0000 | [diff] [blame] | 105 | return forNative(nativeAspectClass, AspectParameters.EMPTY); |
| 106 | } |
| 107 | |
gregce | 18694cd | 2020-05-12 15:40:05 -0700 | [diff] [blame] | 108 | public static Aspect forStarlark( |
| 109 | StarlarkAspectClass starlarkAspectClass, |
tomlu | 8306d43 | 2017-08-11 21:32:01 +0200 | [diff] [blame] | 110 | AspectDefinition aspectDefinition, |
messa | f8c3408 | 2021-06-04 02:51:04 -0700 | [diff] [blame] | 111 | AspectParameters parameters, |
| 112 | RequiredProviders inheritedRequiredProviders, |
| 113 | ImmutableSet<String> inheritedAttributeAspects) { |
| 114 | return new Aspect( |
| 115 | starlarkAspectClass, |
| 116 | aspectDefinition, |
| 117 | parameters, |
| 118 | inheritedRequiredProviders, |
| 119 | inheritedAttributeAspects); |
Marian Lobur | 702cad7 | 2015-09-02 09:53:58 +0000 | [diff] [blame] | 120 | } |
| 121 | |
| 122 | /** |
Dmitry Lomov | 8ff0645 | 2015-10-21 19:16:30 +0000 | [diff] [blame] | 123 | * Returns the aspectClass required for building the aspect. |
Marian Lobur | 702cad7 | 2015-09-02 09:53:58 +0000 | [diff] [blame] | 124 | */ |
Dmitry Lomov | 8ff0645 | 2015-10-21 19:16:30 +0000 | [diff] [blame] | 125 | public AspectClass getAspectClass() { |
Dmitry Lomov | 1575652 | 2016-12-16 16:52:37 +0000 | [diff] [blame] | 126 | return aspectDescriptor.getAspectClass(); |
Marian Lobur | 702cad7 | 2015-09-02 09:53:58 +0000 | [diff] [blame] | 127 | } |
| 128 | |
| 129 | /** |
| 130 | * Returns parameters for evaluation of the aspect. |
| 131 | */ |
| 132 | public AspectParameters getParameters() { |
Dmitry Lomov | 1575652 | 2016-12-16 16:52:37 +0000 | [diff] [blame] | 133 | return aspectDescriptor.getParameters(); |
| 134 | } |
| 135 | |
| 136 | public AspectDescriptor getDescriptor() { |
| 137 | return aspectDescriptor; |
Marian Lobur | 702cad7 | 2015-09-02 09:53:58 +0000 | [diff] [blame] | 138 | } |
| 139 | |
| 140 | @Override |
Marian Lobur | 702cad7 | 2015-09-02 09:53:58 +0000 | [diff] [blame] | 141 | public String toString() { |
Dmitry Lomov | 1575652 | 2016-12-16 16:52:37 +0000 | [diff] [blame] | 142 | return String.format("Aspect %s", aspectDescriptor.toString()); |
Marian Lobur | 702cad7 | 2015-09-02 09:53:58 +0000 | [diff] [blame] | 143 | } |
Dmitry Lomov | 6231d08 | 2015-11-02 17:17:20 +0000 | [diff] [blame] | 144 | |
| 145 | public AspectDefinition getDefinition() { |
Dmitry Lomov | e804017 | 2016-04-06 14:53:43 +0000 | [diff] [blame] | 146 | return aspectDefinition; |
Dmitry Lomov | 6231d08 | 2015-11-02 17:17:20 +0000 | [diff] [blame] | 147 | } |
Dmitry Lomov | 940ea07 | 2016-01-21 22:34:14 +0000 | [diff] [blame] | 148 | |
| 149 | @Override |
| 150 | public boolean isAttributeValueExplicitlySpecified(Attribute attribute) { |
| 151 | // All aspect attributes are implicit. |
| 152 | return false; |
| 153 | } |
janakr | ee5bf48 | 2018-03-26 10:55:59 -0700 | [diff] [blame] | 154 | |
| 155 | /** {@link ObjectCodec} for {@link Aspect}. */ |
| 156 | static class AspectCodec implements ObjectCodec<Aspect> { |
| 157 | @Override |
| 158 | public Class<Aspect> getEncodedClass() { |
| 159 | return Aspect.class; |
| 160 | } |
| 161 | |
| 162 | @Override |
| 163 | public void serialize(SerializationContext context, Aspect obj, CodedOutputStream codedOut) |
| 164 | throws SerializationException, IOException { |
| 165 | context.serialize(obj.getDescriptor(), codedOut); |
| 166 | boolean nativeAspect = obj.getDescriptor().getAspectClass() instanceof NativeAspectClass; |
| 167 | codedOut.writeBoolNoTag(nativeAspect); |
| 168 | if (!nativeAspect) { |
| 169 | context.serialize(obj.getDefinition(), codedOut); |
| 170 | } |
| 171 | } |
| 172 | |
| 173 | @Override |
| 174 | public Aspect deserialize(DeserializationContext context, CodedInputStream codedIn) |
| 175 | throws SerializationException, IOException { |
| 176 | AspectDescriptor aspectDescriptor = context.deserialize(codedIn); |
| 177 | if (codedIn.readBool()) { |
| 178 | return forNative( |
| 179 | (NativeAspectClass) aspectDescriptor.getAspectClass(), |
| 180 | aspectDescriptor.getParameters()); |
| 181 | } else { |
| 182 | AspectDefinition aspectDefinition = context.deserialize(codedIn); |
gregce | 18694cd | 2020-05-12 15:40:05 -0700 | [diff] [blame] | 183 | return forStarlark( |
| 184 | (StarlarkAspectClass) aspectDescriptor.getAspectClass(), |
janakr | ee5bf48 | 2018-03-26 10:55:59 -0700 | [diff] [blame] | 185 | aspectDefinition, |
messa | f8c3408 | 2021-06-04 02:51:04 -0700 | [diff] [blame] | 186 | aspectDescriptor.getParameters(), |
| 187 | aspectDescriptor.getInheritedRequiredProviders(), |
| 188 | aspectDescriptor.getInheritedAttributeAspects()); |
janakr | ee5bf48 | 2018-03-26 10:55:59 -0700 | [diff] [blame] | 189 | } |
| 190 | } |
| 191 | } |
Marian Lobur | 702cad7 | 2015-09-02 09:53:58 +0000 | [diff] [blame] | 192 | } |