aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/main/java/com/google/devtools/build/lib/skyframe/TargetPatternFunction.java
blob: 6e631ed7e28a107f474db3479a58b185c8455ae1 (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
// Copyright 2014 Google Inc. 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.skyframe;

import com.google.devtools.build.lib.cmdline.LabelValidator;
import com.google.devtools.build.lib.cmdline.ResolvedTargets;
import com.google.devtools.build.lib.cmdline.TargetParsingException;
import com.google.devtools.build.lib.cmdline.TargetPattern;
import com.google.devtools.build.lib.cmdline.TargetPatternResolver;
import com.google.devtools.build.lib.events.Event;
import com.google.devtools.build.lib.packages.NoSuchPackageException;
import com.google.devtools.build.lib.packages.NoSuchThingException;
import com.google.devtools.build.lib.packages.Package;
import com.google.devtools.build.lib.packages.PackageIdentifier;
import com.google.devtools.build.lib.packages.Target;
import com.google.devtools.build.lib.pkgcache.FilteringPolicies;
import com.google.devtools.build.lib.pkgcache.FilteringPolicy;
import com.google.devtools.build.lib.pkgcache.PathPackageLocator;
import com.google.devtools.build.lib.pkgcache.TargetPatternResolverUtil;
import com.google.devtools.build.lib.syntax.Label;
import com.google.devtools.build.lib.vfs.Path;
import com.google.devtools.build.lib.vfs.PathFragment;
import com.google.devtools.build.lib.vfs.RootedPath;
import com.google.devtools.build.skyframe.SkyFunction;
import com.google.devtools.build.skyframe.SkyFunctionException;
import com.google.devtools.build.skyframe.SkyKey;
import com.google.devtools.build.skyframe.SkyValue;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.atomic.AtomicReference;

import javax.annotation.Nullable;

/**
 * TargetPatternFunction translates a target pattern (eg, "foo/...") into a set of resolved
 * Targets.
 */
public class TargetPatternFunction implements SkyFunction {

  private final AtomicReference<PathPackageLocator> pkgPath;

  public TargetPatternFunction(AtomicReference<PathPackageLocator> pkgPath) {
    this.pkgPath = pkgPath;
  }

  @Override
  public SkyValue compute(SkyKey key, Environment env) throws TargetPatternFunctionException,
      InterruptedException {
    TargetPatternValue.TargetPattern patternKey =
        ((TargetPatternValue.TargetPattern) key.argument());

    TargetPattern.Parser parser = new TargetPattern.Parser(patternKey.getOffset());
    try {
      Resolver resolver = new Resolver(env, patternKey.getPolicy(), pkgPath);
      TargetPattern resolvedPattern = parser.parse(patternKey.getPattern());
      return new TargetPatternValue(resolvedPattern.eval(resolver));
    } catch (TargetParsingException e) {
      throw new TargetPatternFunctionException(e);
    } catch (TargetPatternResolver.MissingDepException e) {
      return null;
    }
  }

  @Nullable
  @Override
  public String extractTag(SkyKey skyKey) {
    return null;
  }

  private static class Resolver implements TargetPatternResolver<Target> {
    private final Environment env;
    private final FilteringPolicy policy;
    private final AtomicReference<PathPackageLocator> pkgPath;

    public Resolver(Environment env, FilteringPolicy policy,
                    AtomicReference<PathPackageLocator> pkgPath) {
      this.policy = policy;
      this.env = env;
      this.pkgPath = pkgPath;
    }

    @Override
    public void warn(String msg) {
      env.getListener().handle(Event.warn(msg));
    }

    /**
     * Gets a Package via the Skyframe env. May return a Package that has errors.
     */
    private Package getPackage(PackageIdentifier pkgIdentifier)
        throws MissingDepException, NoSuchThingException {
      SkyKey pkgKey = PackageValue.key(pkgIdentifier);
      Package pkg;
      try {
        PackageValue pkgValue =
            (PackageValue) env.getValueOrThrow(pkgKey, NoSuchThingException.class);
        if (pkgValue == null) {
          throw new MissingDepException();
        }
        pkg = pkgValue.getPackage();
      } catch (NoSuchPackageException e) {
        pkg = e.getPackage();
        if (pkg == null) {
          throw e;
        }
      }
      return pkg;
    }

    @Override
    public Target getTargetOrNull(String targetName) throws InterruptedException,
        MissingDepException {
      try {
        Label label = Label.parseAbsolute(targetName);
        if (!isPackage(label.getPackageName())) {
          return null;
        }
        Package pkg = getPackage(label.getPackageIdentifier());
        return pkg.getTarget(label.getName());
      } catch (Label.SyntaxException | NoSuchThingException e) {
        return null;
      }
    }

    @Override
    public ResolvedTargets<Target> getExplicitTarget(String targetName)
        throws TargetParsingException, InterruptedException, MissingDepException {
      Label label = TargetPatternResolverUtil.label(targetName);
      try {
        Package pkg = getPackage(label.getPackageIdentifier());
        Target target = pkg.getTarget(label.getName());
        return  policy.shouldRetain(target, true)
            ? ResolvedTargets.of(target)
            : ResolvedTargets.<Target>empty();
      } catch (NoSuchThingException e) {
        throw new TargetParsingException(e.getMessage(), e);
      }
    }

    @Override
    public ResolvedTargets<Target> getTargetsInPackage(String originalPattern, String packageName,
                                                       boolean rulesOnly)
        throws TargetParsingException, InterruptedException, MissingDepException {
      FilteringPolicy actualPolicy = rulesOnly
          ? FilteringPolicies.and(FilteringPolicies.RULES_ONLY, policy)
          : policy;
      return getTargetsInPackage(originalPattern, packageName, actualPolicy);
    }

    private ResolvedTargets<Target> getTargetsInPackage(String originalPattern, String packageName,
                                                        FilteringPolicy policy)
        throws TargetParsingException, MissingDepException {
      // Normalise, e.g "foo//bar" -> "foo/bar"; "foo/" -> "foo":
      PathFragment packageNameFragment = new PathFragment(packageName);
      packageName = packageNameFragment.toString();

      // It's possible for this check to pass, but for
      // Label.validatePackageNameFull to report an error because the
      // package name is illegal.  That's a little weird, but we can live with
      // that for now--see test case: testBadPackageNameButGoodEnoughForALabel.
      // (BTW I tried duplicating that validation logic in Label but it was
      // extremely tricky.)
      if (LabelValidator.validatePackageName(packageName) != null) {
        throw new TargetParsingException("'" + packageName + "' is not a valid package name");
      }
      if (!isPackage(packageName)) {
        throw new TargetParsingException(
            TargetPatternResolverUtil.getParsingErrorMessage(
                "no such package '" + packageName + "': BUILD file not found on package path",
                originalPattern));
      }

      try {
        Package pkg = getPackage(
            PackageIdentifier.createInDefaultRepo(packageNameFragment.toString()));
        return TargetPatternResolverUtil.resolvePackageTargets(pkg, policy);
      } catch (NoSuchThingException e) {
        String message = TargetPatternResolverUtil.getParsingErrorMessage(
            "package contains errors", originalPattern);
        throw new TargetParsingException(message, e);
      }
    }

    @Override
    public boolean isPackage(String packageName) throws MissingDepException {
      SkyKey packageLookupKey;
      packageLookupKey = PackageLookupValue.key(new PathFragment(packageName));
      PackageLookupValue packageLookupValue = (PackageLookupValue) env.getValue(packageLookupKey);
      if (packageLookupValue == null) {
        throw new MissingDepException();
      }
      return packageLookupValue.packageExists();
    }

    @Override
    public String getTargetKind(Target target) {
      return target.getTargetKind();
    }

    @Override
    public ResolvedTargets<Target> findTargetsBeneathDirectory(
        String originalPattern, String pathPrefix, boolean rulesOnly)
        throws TargetParsingException, MissingDepException {
      FilteringPolicy actualPolicy = rulesOnly
          ? FilteringPolicies.and(FilteringPolicies.RULES_ONLY, policy)
          : policy;

      PathFragment directory = new PathFragment(pathPrefix);
      if (directory.containsUplevelReferences()) {
        throw new TargetParsingException("up-level references are not permitted: '"
            + directory.getPathString() + "'");
      }
      if (!pathPrefix.isEmpty() && (LabelValidator.validatePackageName(pathPrefix) != null)) {
        throw new TargetParsingException("'" + pathPrefix + "' is not a valid package name");
      }

      ResolvedTargets.Builder<Target> builder = ResolvedTargets.builder();

      List<RecursivePkgValue> lookupValues = new ArrayList<>();
      for (Path root : pkgPath.get().getPathEntries()) {
        SkyKey key = RecursivePkgValue.key(RootedPath.toRootedPath(root, directory));
        RecursivePkgValue lookup = (RecursivePkgValue) env.getValue(key);
        if (lookup != null) {
          lookupValues.add(lookup);
        }
      }
      if (env.valuesMissing()) {
        throw new MissingDepException();
      }

      for (RecursivePkgValue value : lookupValues) {
        for (String pkg : value.getPackages()) {
          builder.merge(getTargetsInPackage(originalPattern, pkg, FilteringPolicies.NO_FILTER));
        }
      }

      if (builder.isEmpty()) {
        throw new TargetParsingException("no targets found beneath '" + directory + "'");
      }

      // Apply the transform after the check so we only return the
      // error if the tree really contains no targets.
      ResolvedTargets<Target> intermediateResult = builder.build();
      ResolvedTargets.Builder<Target> filteredBuilder = ResolvedTargets.builder();
      if (intermediateResult.hasError()) {
        filteredBuilder.setError();
      }
      for (Target target : intermediateResult.getTargets()) {
        if (actualPolicy.shouldRetain(target, false)) {
          filteredBuilder.add(target);
        }
      }
      return filteredBuilder.build();
    }
  }

  /**
   * Used to declare all the exception types that can be wrapped in the exception thrown by
   * {@link TargetPatternFunction#compute}.
   */
  private static final class TargetPatternFunctionException extends SkyFunctionException {
    public TargetPatternFunctionException(TargetParsingException e) {
      super(e, Transience.PERSISTENT);
    }
  }
}