aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/main/java/com/google/devtools/build/lib/analysis/Dependency.java
blob: 755b991d86663b84cbef9d3393e82eb0130dece2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
// Copyright 2014 The Bazel Authors. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//    http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package com.google.devtools.build.lib.analysis;

import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
import com.google.devtools.build.lib.analysis.config.BuildConfiguration;
import com.google.devtools.build.lib.cmdline.Label;
import com.google.devtools.build.lib.packages.Attribute;
import com.google.devtools.build.lib.util.Preconditions;
import java.util.Map;
import java.util.Objects;
import javax.annotation.Nullable;

/**
 * A dependency of a configured target through a label.
 *
 * <p>For static configurations: includes the target and the configuration of the dependency
 * configured target and any aspects that may be required, as well as the configurations for
 * these aspects.
 *
 * <p>For dynamic configurations: includes the target and the desired configuration transitions
 * that should be applied to produce the dependency's configuration. It's the caller's
 * responsibility to construct an actual configuration out of that. A set of aspects is also
 * included; the caller must also construct configurations for each of these.
 *
 * <p>Note that the presence of an aspect here does not necessarily mean that it will be created.
 * They will be filtered based on the {@link TransitiveInfoProvider} instances their associated
 * configured targets have (we cannot do that here because the configured targets are not
 * available yet). No error or warning is reported in this case, because it is expected that rules
 * sometimes over-approximate the providers they supply in their definitions.
 */
public abstract class Dependency {

  /**
   * Creates a new {@link Dependency} with a null configuration, suitable for edges with no
   * configuration in static configuration builds.
   */
  public static Dependency withNullConfiguration(Label label) {
    return new NullConfigurationDependency(label);
  }

  /**
   * Creates a new {@link Dependency} with the given configuration, suitable for static
   * configuration builds.
   *
   * <p>The configuration must not be {@code null}.
   *
   * <p>A {@link Dependency} created this way will have no associated aspects.
   */
  public static Dependency withConfiguration(Label label, BuildConfiguration configuration) {
    return new StaticConfigurationDependency(
        label, configuration, ImmutableMap.<AspectDescriptor, BuildConfiguration>of());
  }

  /**
   * Creates a new {@link Dependency} with the given configuration and aspects, suitable for
   * static configuration builds. The configuration is also applied to all aspects.
   *
   * <p>The configuration and aspects must not be {@code null}.
   */
  public static Dependency withConfigurationAndAspects(
      Label label, BuildConfiguration configuration, Iterable<AspectDescriptor> aspects) {
    ImmutableMap.Builder<AspectDescriptor, BuildConfiguration> aspectBuilder =
        new ImmutableMap.Builder<>();
    for (AspectDescriptor aspect : aspects) {
      aspectBuilder.put(aspect, configuration);
    }
    return new StaticConfigurationDependency(label, configuration, aspectBuilder.build());
  }

  /**
   * Creates a new {@link Dependency} with the given configuration and aspects, suitable for
   * storing the output of a dynamic configuration trimming step. The aspects each have their own
   * configuration.
   *
   * <p>The aspects and configurations must not be {@code null}.
   */
  public static Dependency withConfiguredAspects(
      Label label, BuildConfiguration configuration,
      Map<AspectDescriptor, BuildConfiguration> aspectConfigurations) {
    return new StaticConfigurationDependency(
        label, configuration, ImmutableMap.copyOf(aspectConfigurations));
  }

  /**
   * Creates a new {@link Dependency} with the given transition and aspects, suitable for dynamic
   * configuration builds.
   */
  public static Dependency withTransitionAndAspects(
      Label label, Attribute.Transition transition, Iterable<AspectDescriptor> aspects) {
    return new DynamicConfigurationDependency(label, transition, ImmutableSet.copyOf(aspects));
  }

  protected final Label label;

  /**
   * Only the implementations below should extend this class. Use the factory methods above to
   * create new Dependencies.
   */
  private Dependency(Label label) {
    this.label = Preconditions.checkNotNull(label);
  }

  /** Returns the label of the target this dependency points to. */
  public Label getLabel() {
    return label;
  }

  /**
   * Returns true if this dependency specifies a static configuration, false if it specifies
   * a dynamic transition.
   */
  public abstract boolean hasStaticConfiguration();

  /**
   * Returns the static configuration for the target this dependency points to.
   *
   * @throws IllegalStateException if {@link #hasStaticConfiguration} returns false.
   */
  @Nullable
  public abstract BuildConfiguration getConfiguration();

  /**
   * Returns the dynamic transition to be applied to reach the target this dependency points to.
   *
   * @throws IllegalStateException if {@link #hasStaticConfiguration} returns true.
   */
  public abstract Attribute.Transition getTransition();

  /**
   * Returns the set of aspects which should be evaluated and combined with the configured target
   * pointed to by this dependency.
   *
   * @see #getAspectConfigurations()
   */
  public abstract ImmutableSet<AspectDescriptor> getAspects();

  /**
   * Returns the mapping from aspects to the static configurations they should be evaluated with.
   *
   * <p>The {@link Map#keySet()} of this map is equal to that returned by {@link #getAspects()}.
   *
   * @throws IllegalStateException if {@link #hasStaticConfiguration()} returns false.
   */
  public abstract ImmutableMap<AspectDescriptor, BuildConfiguration> getAspectConfigurations();

  /**
   * Implementation of a dependency with no configuration, suitable for static configuration
   * builds of edges to source files or e.g. for visibility.
   */
  private static final class NullConfigurationDependency extends Dependency {
    public NullConfigurationDependency(Label label) {
      super(label);
    }

    @Override
    public boolean hasStaticConfiguration() {
      return true;
    }

    @Nullable
    @Override
    public BuildConfiguration getConfiguration() {
      return null;
    }

    @Override
    public Attribute.Transition getTransition() {
      throw new IllegalStateException(
          "A dependency with a static configuration does not have a transition.");
    }

    @Override
    public ImmutableSet<AspectDescriptor> getAspects() {
      return ImmutableSet.of();
    }

    @Override
    public ImmutableMap<AspectDescriptor, BuildConfiguration> getAspectConfigurations() {
      return ImmutableMap.of();
    }

    @Override
    public int hashCode() {
      return label.hashCode();
    }

    @Override
    public boolean equals(Object other) {
      if (!(other instanceof NullConfigurationDependency)) {
        return false;
      }
      NullConfigurationDependency otherDep = (NullConfigurationDependency) other;
      return label.equals(otherDep.label);
    }

    @Override
    public String toString() {
      return String.format("NullConfigurationDependency{label=%s}", label);
    }
  }

  /**
   * Implementation of a dependency with static configurations, suitable for static configuration
   * builds.
   */
  private static final class StaticConfigurationDependency extends Dependency {
    private final BuildConfiguration configuration;
    private final ImmutableMap<AspectDescriptor, BuildConfiguration> aspectConfigurations;

    public StaticConfigurationDependency(
        Label label, BuildConfiguration configuration,
        ImmutableMap<AspectDescriptor, BuildConfiguration> aspects) {
      super(label);
      this.configuration = Preconditions.checkNotNull(configuration);
      this.aspectConfigurations = Preconditions.checkNotNull(aspects);
    }

    @Override
    public boolean hasStaticConfiguration() {
      return true;
    }

    @Override
    public BuildConfiguration getConfiguration() {
      return configuration;
    }

    @Override
    public Attribute.Transition getTransition() {
      throw new IllegalStateException(
          "A dependency with a static configuration does not have a transition.");
    }

    @Override
    public ImmutableSet<AspectDescriptor> getAspects() {
      return aspectConfigurations.keySet();
    }

    @Override
    public ImmutableMap<AspectDescriptor, BuildConfiguration> getAspectConfigurations() {
      return aspectConfigurations;
    }

    @Override
    public int hashCode() {
      return Objects.hash(label, configuration, aspectConfigurations);
    }

    @Override
    public boolean equals(Object other) {
      if (!(other instanceof StaticConfigurationDependency)) {
        return false;
      }
      StaticConfigurationDependency otherDep = (StaticConfigurationDependency) other;
      return label.equals(otherDep.label)
          && configuration.equals(otherDep.configuration)
          && aspectConfigurations.equals(otherDep.aspectConfigurations);
    }

    @Override
    public String toString() {
      return String.format(
          "StaticConfigurationDependency{label=%s, configuration=%s, aspectConfigurations=%s}",
          label, configuration, aspectConfigurations);
    }
  }

  /**
   * Implementation of a dependency with a given configuration transition, suitable for dynamic
   * configuration builds.
   */
  private static final class DynamicConfigurationDependency extends Dependency {
    private final Attribute.Transition transition;
    private final ImmutableSet<AspectDescriptor> aspects;

    public DynamicConfigurationDependency(
        Label label, Attribute.Transition transition, ImmutableSet<AspectDescriptor> aspects) {
      super(label);
      this.transition = Preconditions.checkNotNull(transition);
      this.aspects = Preconditions.checkNotNull(aspects);
    }

    @Override
    public boolean hasStaticConfiguration() {
      return false;
    }

    @Override
    public BuildConfiguration getConfiguration() {
      throw new IllegalStateException(
          "A dependency with a dynamic configuration transition does not have a configuration.");
    }

    @Override
    public Attribute.Transition getTransition() {
      return transition;
    }

    @Override
    public ImmutableSet<AspectDescriptor> getAspects() {
      return aspects;
    }

    @Override
    public ImmutableMap<AspectDescriptor, BuildConfiguration> getAspectConfigurations() {
      throw new IllegalStateException(
          "A dependency with a dynamic configuration transition does not have aspect "
          + "configurations.");
    }

    @Override
    public int hashCode() {
      return Objects.hash(label, transition, aspects);
    }

    @Override
    public boolean equals(Object other) {
      if (!(other instanceof DynamicConfigurationDependency)) {
        return false;
      }
      DynamicConfigurationDependency otherDep = (DynamicConfigurationDependency) other;
      return label.equals(otherDep.label)
          && transition.equals(otherDep.transition)
          && aspects.equals(otherDep.aspects);
    }

    @Override
    public String toString() {
      return String.format(
          "DynamicConfigurationDependency{label=%s, transition=%s, aspects=%s}",
          label, transition, aspects);
    }
  }
}