aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/main/java/com/google/devtools/build/lib/analysis/config/DynamicTransitionMapper.java
blob: e481fb1c4500d8e2f1236ea11440a8a3178459bf (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
// Copyright 2017 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.config;

import com.google.common.collect.ImmutableMap;
import com.google.devtools.build.lib.analysis.config.transitions.PatchTransition;
import com.google.devtools.build.lib.analysis.config.transitions.SplitTransition;
import com.google.devtools.build.lib.analysis.config.transitions.Transition;
import com.google.devtools.build.lib.packages.Attribute;
import com.google.devtools.build.lib.packages.RuleClass;

/**
 * Maps non-{@link PatchTransition} declarations to their implementable equivalents.
 *
 * <p>Blaze applies configuration transitions by executing {@link PatchTransition} instances. But
 * for legacy reasons, not every transition declaration is a {@link PatchTransition}. The most
 * prominent example is {@link ConfigurationTransitionProxy}, which defines its transitions as
 * enums. These transitions are used all over the place. So we need a way to continue to support
 * them.
 *
 * <p>Hence this class.
 *
 * <p>Going forward, we should eliminate the need for this class by eliminating
 * non-{@link PatchTransition} transitions. This is conceptually straightforward: replace
 * declarations of the form {@link RuleClass.Builder#cfg(Transition)} with
 * {@link RuleClass.Builder#cfg(PatchTransition)}. That way, transition declarations "just work",
 * with no extra fuss. But this is a migration that will take some time to complete.
 *
 * {@link ConfigurationTransitionProxy#DATA} provides the most complicated challenge. This is
 * C++/LIPO logic, and the implementation is in C++ rule code
 * ({@link com.google.devtools.build.lib.rules.cpp.transitions.DisableLipoTransition}). But the enum
 * is defined in {@link Attribute}, which is in {@code lib.packages}, which has access to neither
 * rule-specific nor configuration-specific code. Furthermore, many non-C++ rules declare this
 * transition. We ultimately need a cleaner way to inject this rules-specific logic into general
 * Blaze code.
  */
public final class DynamicTransitionMapper {
  /**
   * Use this to declare a no-op transition that keeps the input configuration.
   */
  public static final Transition SELF = new Transition() {};

  private final ImmutableMap<Transition, Transition> map;

  /**
   * Creates a new mapper with the given mapping. Any transition not in this mapping triggers
   * an {@link IllegalArgumentException}.
   */
  public DynamicTransitionMapper(ImmutableMap<Transition, Transition> map) {
    this.map = map;
  }

  /**
   * Given an input transition, returns the equivalent transition Blaze's implementation logic knows
   * how to apply.
   *
   * <p>When the input is a {@link PatchTransition}, this just returns the input. This is because
   * that's the kind of transition that Blaze natively applies. For this reason, all inputs should
   * ideally be {@link PatchTransition}s.
   *
   * <p>Non-{@link PatchTransition} inputs that aren't mapped here throw an
   * {@link IllegalArgumentException}.
   */
  public Transition map(Transition fromTransition) {
    if (fromTransition instanceof PatchTransition
        || fromTransition instanceof SplitTransition
        || fromTransition == null) {
      return fromTransition;
    }
    Transition toTransition = map.get(fromTransition);
    if (toTransition == SELF) {
      return fromTransition;
    } else if (toTransition != null) {
      return toTransition;
    }
    throw new IllegalArgumentException("No dynamic mapping for " + fromTransition.toString());
  }
}