aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/main/java/com/google/devtools/build/lib/analysis/config/ConfigurationEnvironment.java
blob: 8b5e37a8ffa7f5b9d5656cfe8233d264bebf9a32 (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
// 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.config;

import com.google.devtools.build.lib.analysis.BlazeDirectories;
import com.google.devtools.build.lib.analysis.config.BuildConfiguration.Fragment;
import com.google.devtools.build.lib.cmdline.Label;
import com.google.devtools.build.lib.events.ExtendedEventHandler;
import com.google.devtools.build.lib.packages.NoSuchPackageException;
import com.google.devtools.build.lib.packages.NoSuchTargetException;
import com.google.devtools.build.lib.packages.Package;
import com.google.devtools.build.lib.packages.Target;
import com.google.devtools.build.lib.pkgcache.LoadedPackageProvider;
import com.google.devtools.build.lib.pkgcache.PackageProvider;
import com.google.devtools.build.lib.vfs.Path;
import javax.annotation.Nullable;

/**
 * An environment to support creating BuildConfiguration instances in a hermetic fashion; all
 * accesses to packages or the file system <b>must</b> go through this interface, so that they can
 * be recorded for correct caching.
 */
public interface ConfigurationEnvironment {

  /**
   * Returns an event handler to report errors to. Note that reporting an error does not cause the
   * computation to abort - you also need to throw an exception.
   */
  ExtendedEventHandler getEventHandler();

  /**
   * Returns a target for the given label, loading it if necessary, and throwing an exception if it
   * does not exist.
   *
   * @deprecated Do not use this method. Configuration fragments should be fairly dumb key-value
   * maps so that they are cheap and easy to create.
   *
   * <p>If you feel the need to use contents of BUILD files in your
   * {@link ConfigurationFragmentFactory}, add an implicit dependency to your rules that use your
   * configuration fragment that point to a rule of a new rule class, and do the computation during
   * the analysis of said rule. The only uses of this method are those we haven't gotten around
   * migrating yet.
   */
  @Deprecated
  Target getTarget(Label label)
      throws NoSuchPackageException, NoSuchTargetException, InterruptedException;

  /**
   * Returns a path for the given file within the given package.
   *
   * @deprecated Do not use this method. Configuration fragments should be fairly dumb key-value
   * maps so that they are cheap and easy to create. If you feel the need to read contents of files
   * in your {@link ConfigurationFragmentFactory}, you have the following options:
   * <ul>
   *   <li>
   *     Add an implicit dependency to the rules that need this configuration fragment, put the
   *     information you need in BUILD files and use it during the analysis of the implicit
   *     dependency
   *   </li>
   *   <li>
   *     Read the file during the execution phase (then it won't be able to affect analysis)
   *   </li>
   *   <li>
   *     Contact the developers of Bazel and we'll figure something out.
   *   </li>
   * </ul>
   */
  @Deprecated
  Path getPath(Package pkg, String fileName) throws InterruptedException;

  /** Returns fragment based on fragment class and build options. */
  <T extends Fragment> T getFragment(BuildOptions buildOptions, Class<T> fragmentType)
      throws InvalidConfigurationException, InterruptedException;

  /**
   * Returns global value of BlazeDirectories.
   *
   * @deprecated Do not use this method. Configuration fragments should be fairly dumb key-value
   * pairs so that they are cheap and easy to create.
   */
  @Nullable
  @Deprecated
  BlazeDirectories getBlazeDirectories() throws InterruptedException;

  /**
   * An implementation backed by a {@link PackageProvider} instance.
   */
  public static final class TargetProviderEnvironment implements ConfigurationEnvironment {
    private final LoadedPackageProvider packageProvider;
    private final BlazeDirectories blazeDirectories;

    public TargetProviderEnvironment(
        PackageProvider packageProvider,
        ExtendedEventHandler eventHandler,
        BlazeDirectories blazeDirectories) {
      this.packageProvider = new LoadedPackageProvider(packageProvider, eventHandler);
      this.blazeDirectories = blazeDirectories;
    }

    public TargetProviderEnvironment(
        PackageProvider packageProvider, ExtendedEventHandler eventHandler) {
      this(packageProvider, eventHandler, null);
    }

    @Override
    public ExtendedEventHandler getEventHandler() {
      return packageProvider.getEventHandler();
    }

    @Override
    public Target getTarget(final Label label)
        throws NoSuchPackageException, NoSuchTargetException, InterruptedException {
      return packageProvider.getLoadedTarget(label);
    }

    @Override
    public Path getPath(Package pkg, String fileName) {
      return pkg.getPackageDirectory().getRelative(fileName);
    }

    @Override
    public <T extends Fragment> T getFragment(BuildOptions buildOptions, Class<T> fragmentType) {
      throw new UnsupportedOperationException();
    }

    @Override
    public BlazeDirectories getBlazeDirectories() {
      return blazeDirectories;
    }
  }
}