blob: 2f6c4dc093913f5744150e0c81f8788000b53b91 [file] [log] [blame]
Damien Martin-Guillerezf88f4d82015-09-25 13:56:55 +00001// Copyright 2015 The Bazel Authors. All rights reserved.
Marian Loburbe3171e2015-06-26 16:03:14 +00002//
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.
14package com.google.devtools.build.lib.skyframe;
15
tomlua155b532017-11-08 20:12:47 +010016import com.google.common.base.Preconditions;
Marian Loburbe3171e2015-06-26 16:03:14 +000017import com.google.common.collect.Maps;
Lukacs Berki6e91eb92015-09-21 09:12:37 +000018import com.google.devtools.build.lib.cmdline.Label;
Kristina Chodorow73fa2032015-08-28 17:57:46 +000019import com.google.devtools.build.lib.cmdline.PackageIdentifier;
Marian Loburbe3171e2015-06-26 16:03:14 +000020import com.google.devtools.build.lib.cmdline.ResolvedTargets;
ulfjack53a0ff12019-05-21 07:08:40 -070021import com.google.devtools.build.lib.collect.compacthashset.CompactHashSet;
Marian Loburbe3171e2015-06-26 16:03:14 +000022import com.google.devtools.build.lib.packages.NoSuchTargetException;
23import com.google.devtools.build.lib.packages.Package;
Marian Loburbe3171e2015-06-26 16:03:14 +000024import com.google.devtools.build.lib.packages.Target;
Marian Loburbe3171e2015-06-26 16:03:14 +000025import com.google.devtools.build.skyframe.WalkableGraph;
ulfjack53a0ff12019-05-21 07:08:40 -070026import java.util.Collection;
Marian Loburbe3171e2015-06-26 16:03:14 +000027import java.util.HashSet;
28import java.util.Map;
29import java.util.Set;
30
31/**
32 * This class encapsulates logic behind computing final target set based on separate results from a
33 * list of target patterns (eg, //foo:all -//bar/... //foo:test).
34 */
ulfjackc96839f2019-05-15 04:49:40 -070035class TargetPatternsResultBuilder {
ulfjack53a0ff12019-05-21 07:08:40 -070036 private final Set<Label> resolvedLabelsBuilder = CompactHashSet.create();
ulfjackc96839f2019-05-15 04:49:40 -070037 private Map<PackageIdentifier, Package> packages;
Marian Loburbe3171e2015-06-26 16:03:14 +000038
Janak Ramakrishnan3c0adb22016-08-15 21:54:55 +000039 /** Returns final set of targets and sets error flag if required. */
ulfjack53a0ff12019-05-21 07:08:40 -070040 public Collection<Target> build(WalkableGraph walkableGraph) throws InterruptedException {
Marian Loburbe3171e2015-06-26 16:03:14 +000041 precomputePackages(walkableGraph);
ulfjack53a0ff12019-05-21 07:08:40 -070042 return transformLabelsIntoTargets(resolvedLabelsBuilder);
Marian Loburbe3171e2015-06-26 16:03:14 +000043 }
44
45 /**
46 * Transforms {@code ResolvedTargets<Label>} to {@code ResolvedTargets<Target>}. Note that this
47 * method is using information about packages, so {@link #precomputePackages} has to be called
48 * before this method.
49 */
ulfjack53a0ff12019-05-21 07:08:40 -070050 private Collection<Target> transformLabelsIntoTargets(Set<Label> resolvedLabels) {
Marian Loburbe3171e2015-06-26 16:03:14 +000051 // precomputePackages has to be called before this method.
ulfjack53a0ff12019-05-21 07:08:40 -070052 Set<Target> targets = CompactHashSet.create();
Marian Loburbe3171e2015-06-26 16:03:14 +000053 Preconditions.checkNotNull(packages);
ulfjack53a0ff12019-05-21 07:08:40 -070054 for (Label label : resolvedLabels) {
55 targets.add(getExistingTarget(label));
Marian Loburbe3171e2015-06-26 16:03:14 +000056 }
ulfjack53a0ff12019-05-21 07:08:40 -070057 return targets;
Marian Loburbe3171e2015-06-26 16:03:14 +000058 }
59
Janak Ramakrishnan3c0adb22016-08-15 21:54:55 +000060 private void precomputePackages(WalkableGraph walkableGraph) throws InterruptedException {
Marian Loburbe3171e2015-06-26 16:03:14 +000061 Set<PackageIdentifier> packagesToRequest = getPackagesIdentifiers();
62 packages = Maps.newHashMapWithExpectedSize(packagesToRequest.size());
63 for (PackageIdentifier pkgIdentifier : packagesToRequest) {
64 packages.put(pkgIdentifier, findPackageInGraph(pkgIdentifier, walkableGraph));
65 }
66 }
67
68 private Target getExistingTarget(Label label) {
69 Package pkg = Preconditions.checkNotNull(packages.get(label.getPackageIdentifier()), label);
70 try {
71 return pkg.getTarget(label.getName());
72 } catch (NoSuchTargetException e) {
73 // This exception should not raise, because we are processing it during TargetPatternValues
74 // evaluation in SkyframeTargetPatternEvaluator#parseTargetPatternKeys and values with errors
75 // are not added to final result.
76 throw new IllegalStateException(e);
77 }
78 }
79
80 private Set<PackageIdentifier> getPackagesIdentifiers() {
81 Set<PackageIdentifier> packagesIdentifiers = new HashSet<>();
ulfjack53a0ff12019-05-21 07:08:40 -070082 for (Label label : resolvedLabelsBuilder) {
Marian Loburbe3171e2015-06-26 16:03:14 +000083 packagesIdentifiers.add(label.getPackageIdentifier());
84 }
85 return packagesIdentifiers;
86 }
87
Janak Ramakrishnan3c0adb22016-08-15 21:54:55 +000088 private static Package findPackageInGraph(
89 PackageIdentifier pkgIdentifier, WalkableGraph walkableGraph) throws InterruptedException {
Janak Ramakrishnan0a4c6e42015-09-17 00:37:58 +000090 return Preconditions.checkNotNull(
91 ((PackageValue) walkableGraph.getValue(PackageValue.key(pkgIdentifier))), pkgIdentifier)
92 .getPackage();
Marian Loburbe3171e2015-06-26 16:03:14 +000093 }
94
ulfjackc96839f2019-05-15 04:49:40 -070095 /** Adds the result from expansion of negative target pattern (eg, "-//foo:all"). */
96 void addLabelsOfPositivePattern(ResolvedTargets<Label> labels) {
ulfjack53a0ff12019-05-21 07:08:40 -070097 Preconditions.checkArgument(labels.getFilteredTargets().isEmpty());
98 resolvedLabelsBuilder.addAll(labels.getTargets());
ulfjackc96839f2019-05-15 04:49:40 -070099 }
Marian Loburbe3171e2015-06-26 16:03:14 +0000100}