Michael Staib | 5e573fd | 2016-01-27 00:33:29 +0000 | [diff] [blame] | 1 | // Copyright 2014 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 | package com.google.devtools.build.lib.analysis; |
| 15 | |
John Cater | 1f6758f | 2020-05-21 08:41:53 -0700 | [diff] [blame] | 16 | import com.google.auto.value.AutoValue; |
tomlu | a155b53 | 2017-11-08 20:12:47 +0100 | [diff] [blame] | 17 | import com.google.common.base.Preconditions; |
Googler | 3cc6cc3 | 2020-04-29 10:50:38 -0700 | [diff] [blame] | 18 | import com.google.common.collect.ImmutableList; |
Michael Staib | 5e573fd | 2016-01-27 00:33:29 +0000 | [diff] [blame] | 19 | import com.google.devtools.build.lib.analysis.config.BuildConfiguration; |
gregce | be55e11 | 2018-01-30 11:04:53 -0800 | [diff] [blame] | 20 | import com.google.devtools.build.lib.analysis.config.transitions.ConfigurationTransition; |
Michael Staib | 5e573fd | 2016-01-27 00:33:29 +0000 | [diff] [blame] | 21 | import com.google.devtools.build.lib.cmdline.Label; |
Dmitry Lomov | 1575652 | 2016-12-16 16:52:37 +0000 | [diff] [blame] | 22 | import com.google.devtools.build.lib.packages.AspectDescriptor; |
John Cater | 5fa85c8 | 2020-05-26 10:52:30 -0700 | [diff] [blame] | 23 | import com.google.devtools.build.lib.skyframe.ConfiguredTargetKey; |
Michael Staib | 5e573fd | 2016-01-27 00:33:29 +0000 | [diff] [blame] | 24 | import javax.annotation.Nullable; |
| 25 | |
| 26 | /** |
| 27 | * A dependency of a configured target through a label. |
| 28 | * |
John Cater | 2a54659 | 2020-05-19 04:48:52 -0700 | [diff] [blame] | 29 | * <p>All instances have an explicit configuration, which includes the target and the configuration |
| 30 | * of the dependency configured target and any aspects that may be required, as well as the |
| 31 | * configurations for these aspects and transition keys. {@link Dependency#getTransitionKeys} |
| 32 | * provides some more context on transition keys. |
Michael Staib | 5e573fd | 2016-01-27 00:33:29 +0000 | [diff] [blame] | 33 | * |
| 34 | * <p>Note that the presence of an aspect here does not necessarily mean that it will be created. |
| 35 | * They will be filtered based on the {@link TransitiveInfoProvider} instances their associated |
Googler | 46b285a | 2020-03-06 13:33:24 -0800 | [diff] [blame] | 36 | * configured targets have (we cannot do that here because the configured targets are not available |
| 37 | * yet). No error or warning is reported in this case, because it is expected that rules sometimes |
| 38 | * over-approximate the providers they supply in their definitions. |
Michael Staib | 5e573fd | 2016-01-27 00:33:29 +0000 | [diff] [blame] | 39 | */ |
John Cater | 1f6758f | 2020-05-21 08:41:53 -0700 | [diff] [blame] | 40 | @AutoValue |
Michael Staib | 5e573fd | 2016-01-27 00:33:29 +0000 | [diff] [blame] | 41 | public abstract class Dependency { |
John Cater | 696e302 | 2020-05-19 10:19:09 -0700 | [diff] [blame] | 42 | /** Builder to assist in creating dependency instances. */ |
John Cater | 1f6758f | 2020-05-21 08:41:53 -0700 | [diff] [blame] | 43 | @AutoValue.Builder |
| 44 | public abstract static class Builder { |
| 45 | /** Sets the label of the target this dependency points to. */ |
| 46 | public abstract Builder setLabel(Label label); |
Michael Staib | 5e573fd | 2016-01-27 00:33:29 +0000 | [diff] [blame] | 47 | |
John Cater | 1f6758f | 2020-05-21 08:41:53 -0700 | [diff] [blame] | 48 | /** Sets the configuration intended for this dependency. */ |
| 49 | public abstract Builder setConfiguration(BuildConfiguration configuration); |
John Cater | 696e302 | 2020-05-19 10:19:09 -0700 | [diff] [blame] | 50 | |
| 51 | /** Explicitly set the configuration for this dependency to null. */ |
| 52 | public Builder withNullConfiguration() { |
| 53 | return setConfiguration(null); |
| 54 | } |
| 55 | |
| 56 | /** Add aspects to this Dependency. The same configuration is applied to all aspects. */ |
John Cater | 1f6758f | 2020-05-21 08:41:53 -0700 | [diff] [blame] | 57 | public abstract Builder setAspects(AspectCollection aspects); |
| 58 | |
| 59 | /** Sets the keys of a configuration transition. */ |
| 60 | public Builder setTransitionKey(String key) { |
jcater | bd864ef | 2020-06-05 07:34:16 -0700 | [diff] [blame] | 61 | if (key.isEmpty()) { |
| 62 | // Ignore empty keys. |
| 63 | return this; |
| 64 | } |
John Cater | 1f6758f | 2020-05-21 08:41:53 -0700 | [diff] [blame] | 65 | return setTransitionKeys(ImmutableList.of(key)); |
John Cater | 696e302 | 2020-05-19 10:19:09 -0700 | [diff] [blame] | 66 | } |
| 67 | |
John Cater | 1f6758f | 2020-05-21 08:41:53 -0700 | [diff] [blame] | 68 | /** Sets the keys of a configuration transition. */ |
| 69 | public abstract Builder setTransitionKeys(ImmutableList<String> keys); |
John Cater | 696e302 | 2020-05-19 10:19:09 -0700 | [diff] [blame] | 70 | |
John Cater | e9137b6 | 2020-06-10 14:16:21 -0700 | [diff] [blame] | 71 | /** |
twigg | 7978f36 | 2021-10-20 14:29:46 -0700 | [diff] [blame^] | 72 | * Sets the execution platform {@link Label} that this dependency should use as an override for |
| 73 | * toolchain resolution. |
John Cater | e9137b6 | 2020-06-10 14:16:21 -0700 | [diff] [blame] | 74 | */ |
twigg | 7978f36 | 2021-10-20 14:29:46 -0700 | [diff] [blame^] | 75 | public abstract Builder setExecutionPlatformLabel(@Nullable Label executionPlatformLabel); |
John Cater | e9137b6 | 2020-06-10 14:16:21 -0700 | [diff] [blame] | 76 | |
John Cater | 1f6758f | 2020-05-21 08:41:53 -0700 | [diff] [blame] | 77 | // Not public. |
| 78 | abstract Dependency autoBuild(); |
John Cater | 696e302 | 2020-05-19 10:19:09 -0700 | [diff] [blame] | 79 | |
| 80 | /** Returns the full Dependency instance. */ |
| 81 | public Dependency build() { |
John Cater | 1f6758f | 2020-05-21 08:41:53 -0700 | [diff] [blame] | 82 | Dependency dependency = autoBuild(); |
| 83 | if (dependency.getConfiguration() == null) { |
| 84 | Preconditions.checkState( |
| 85 | dependency.getAspects().equals(AspectCollection.EMPTY), |
| 86 | "Dependency with null Configuration cannot have aspects"); |
John Cater | 696e302 | 2020-05-19 10:19:09 -0700 | [diff] [blame] | 87 | } |
John Cater | 1f6758f | 2020-05-21 08:41:53 -0700 | [diff] [blame] | 88 | return dependency; |
John Cater | 696e302 | 2020-05-19 10:19:09 -0700 | [diff] [blame] | 89 | } |
jcater | bd864ef | 2020-06-05 07:34:16 -0700 | [diff] [blame] | 90 | |
| 91 | // Added to enable copy, below. Should not be accessible to other classes. |
| 92 | protected abstract Label getLabel(); |
| 93 | |
| 94 | @Nullable |
| 95 | protected abstract BuildConfiguration getConfiguration(); |
| 96 | |
| 97 | protected abstract AspectCollection getAspects(); |
| 98 | |
| 99 | protected abstract ImmutableList<String> getTransitionKeys(); |
| 100 | |
| 101 | /** Returns a copy of this Builder, with the values the same. */ |
| 102 | public Builder copy() { |
| 103 | return Dependency.builder() |
| 104 | .setLabel(getLabel()) |
| 105 | .setConfiguration(getConfiguration()) |
| 106 | .setAspects(getAspects()) |
| 107 | .setTransitionKeys(getTransitionKeys()); |
| 108 | } |
Michael Staib | 5e573fd | 2016-01-27 00:33:29 +0000 | [diff] [blame] | 109 | } |
| 110 | |
John Cater | 696e302 | 2020-05-19 10:19:09 -0700 | [diff] [blame] | 111 | /** Returns a new {@link Builder} to create {@link Dependency} instances. */ |
| 112 | public static Builder builder() { |
John Cater | 1f6758f | 2020-05-21 08:41:53 -0700 | [diff] [blame] | 113 | return new AutoValue_Dependency.Builder() |
| 114 | .setAspects(AspectCollection.EMPTY) |
| 115 | .setTransitionKeys(ImmutableList.of()); |
Michael Staib | 5e573fd | 2016-01-27 00:33:29 +0000 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | /** Returns the label of the target this dependency points to. */ |
John Cater | 1f6758f | 2020-05-21 08:41:53 -0700 | [diff] [blame] | 119 | public abstract Label getLabel(); |
Michael Staib | 5e573fd | 2016-01-27 00:33:29 +0000 | [diff] [blame] | 120 | |
John Cater | 1f6758f | 2020-05-21 08:41:53 -0700 | [diff] [blame] | 121 | /** Returns the explicit configuration intended for this dependency. */ |
Michael Staib | 5e573fd | 2016-01-27 00:33:29 +0000 | [diff] [blame] | 122 | @Nullable |
| 123 | public abstract BuildConfiguration getConfiguration(); |
| 124 | |
| 125 | /** |
Michael Staib | 5e573fd | 2016-01-27 00:33:29 +0000 | [diff] [blame] | 126 | * Returns the set of aspects which should be evaluated and combined with the configured target |
| 127 | * pointed to by this dependency. |
| 128 | * |
Dmitry Lomov | e851fe2 | 2017-02-14 23:11:23 +0000 | [diff] [blame] | 129 | * @see #getAspectConfiguration(AspectDescriptor) |
Michael Staib | 5e573fd | 2016-01-27 00:33:29 +0000 | [diff] [blame] | 130 | */ |
Dmitry Lomov | e851fe2 | 2017-02-14 23:11:23 +0000 | [diff] [blame] | 131 | public abstract AspectCollection getAspects(); |
Michael Staib | 5e573fd | 2016-01-27 00:33:29 +0000 | [diff] [blame] | 132 | |
John Cater | 696e302 | 2020-05-19 10:19:09 -0700 | [diff] [blame] | 133 | /** Returns the configuration an aspect should be evaluated with. */ |
John Cater | 1f6758f | 2020-05-21 08:41:53 -0700 | [diff] [blame] | 134 | @Nullable |
| 135 | public BuildConfiguration getAspectConfiguration(AspectDescriptor aspect) { |
| 136 | return getConfiguration(); |
| 137 | } |
Michael Staib | 5e573fd | 2016-01-27 00:33:29 +0000 | [diff] [blame] | 138 | |
| 139 | /** |
John Cater | 1f6758f | 2020-05-21 08:41:53 -0700 | [diff] [blame] | 140 | * Returns the keys of a configuration transition, if any exist, associated with this dependency. |
| 141 | * See {@link ConfigurationTransition#apply} for more details. Normally, this returns an empty |
| 142 | * list, when there was no configuration transition in effect, or one with a single entry, when |
| 143 | * there was a specific configuration transition result that led to this. It may also return a |
| 144 | * list with multiple entries if the dependency has a null configuration, yet the outgoing edge |
| 145 | * has a split transition. In such cases all transition keys returned by the transition are tagged |
| 146 | * to the dependency. |
Googler | 46b285a | 2020-03-06 13:33:24 -0800 | [diff] [blame] | 147 | */ |
Googler | 3cc6cc3 | 2020-04-29 10:50:38 -0700 | [diff] [blame] | 148 | public abstract ImmutableList<String> getTransitionKeys(); |
John Cater | 5fa85c8 | 2020-05-26 10:52:30 -0700 | [diff] [blame] | 149 | |
John Cater | e9137b6 | 2020-06-10 14:16:21 -0700 | [diff] [blame] | 150 | /** |
twigg | 7978f36 | 2021-10-20 14:29:46 -0700 | [diff] [blame^] | 151 | * Returns the execution platform {@link Label} that this dependency should use as an override for |
| 152 | * toolchain resolution. |
John Cater | e9137b6 | 2020-06-10 14:16:21 -0700 | [diff] [blame] | 153 | */ |
| 154 | @Nullable |
twigg | 7978f36 | 2021-10-20 14:29:46 -0700 | [diff] [blame^] | 155 | public abstract Label getExecutionPlatformLabel(); |
John Cater | e9137b6 | 2020-06-10 14:16:21 -0700 | [diff] [blame] | 156 | |
John Cater | 5fa85c8 | 2020-05-26 10:52:30 -0700 | [diff] [blame] | 157 | /** Returns the ConfiguredTargetKey needed to fetch this dependency. */ |
| 158 | public ConfiguredTargetKey getConfiguredTargetKey() { |
John Cater | e9137b6 | 2020-06-10 14:16:21 -0700 | [diff] [blame] | 159 | ConfiguredTargetKey.Builder configuredTargetKeyBuilder = |
| 160 | ConfiguredTargetKey.builder().setLabel(getLabel()).setConfiguration(getConfiguration()); |
twigg | 7978f36 | 2021-10-20 14:29:46 -0700 | [diff] [blame^] | 161 | if (getExecutionPlatformLabel() != null) { |
| 162 | configuredTargetKeyBuilder.setExecutionPlatformLabel(getExecutionPlatformLabel()); |
John Cater | e9137b6 | 2020-06-10 14:16:21 -0700 | [diff] [blame] | 163 | } |
| 164 | return configuredTargetKeyBuilder.build(); |
John Cater | 5fa85c8 | 2020-05-26 10:52:30 -0700 | [diff] [blame] | 165 | } |
Michael Staib | 5e573fd | 2016-01-27 00:33:29 +0000 | [diff] [blame] | 166 | } |