Damien Martin-Guillerez | f88f4d8 | 2015-09-25 13:56:55 +0000 | [diff] [blame] | 1 | // Copyright 2015 The Bazel Authors. All rights reserved. |
Marian Lobur | be3171e | 2015-06-26 16:03:14 +0000 | [diff] [blame] | 2 | // |
| 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. |
| 14 | package com.google.devtools.build.lib.skyframe; |
| 15 | |
tomlu | a155b53 | 2017-11-08 20:12:47 +0100 | [diff] [blame] | 16 | import com.google.common.base.Preconditions; |
Marian Lobur | be3171e | 2015-06-26 16:03:14 +0000 | [diff] [blame] | 17 | import com.google.common.collect.Maps; |
Lukacs Berki | 6e91eb9 | 2015-09-21 09:12:37 +0000 | [diff] [blame] | 18 | import com.google.devtools.build.lib.cmdline.Label; |
Kristina Chodorow | 73fa203 | 2015-08-28 17:57:46 +0000 | [diff] [blame] | 19 | import com.google.devtools.build.lib.cmdline.PackageIdentifier; |
Marian Lobur | be3171e | 2015-06-26 16:03:14 +0000 | [diff] [blame] | 20 | import com.google.devtools.build.lib.cmdline.ResolvedTargets; |
ulfjack | 53a0ff1 | 2019-05-21 07:08:40 -0700 | [diff] [blame] | 21 | import com.google.devtools.build.lib.collect.compacthashset.CompactHashSet; |
Marian Lobur | be3171e | 2015-06-26 16:03:14 +0000 | [diff] [blame] | 22 | import com.google.devtools.build.lib.packages.NoSuchTargetException; |
| 23 | import com.google.devtools.build.lib.packages.Package; |
Marian Lobur | be3171e | 2015-06-26 16:03:14 +0000 | [diff] [blame] | 24 | import com.google.devtools.build.lib.packages.Target; |
Marian Lobur | be3171e | 2015-06-26 16:03:14 +0000 | [diff] [blame] | 25 | import com.google.devtools.build.skyframe.WalkableGraph; |
ulfjack | 53a0ff1 | 2019-05-21 07:08:40 -0700 | [diff] [blame] | 26 | import java.util.Collection; |
Marian Lobur | be3171e | 2015-06-26 16:03:14 +0000 | [diff] [blame] | 27 | import java.util.HashSet; |
| 28 | import java.util.Map; |
| 29 | import 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 | */ |
ulfjack | c96839f | 2019-05-15 04:49:40 -0700 | [diff] [blame] | 35 | class TargetPatternsResultBuilder { |
ulfjack | 53a0ff1 | 2019-05-21 07:08:40 -0700 | [diff] [blame] | 36 | private final Set<Label> resolvedLabelsBuilder = CompactHashSet.create(); |
ulfjack | c96839f | 2019-05-15 04:49:40 -0700 | [diff] [blame] | 37 | private Map<PackageIdentifier, Package> packages; |
Marian Lobur | be3171e | 2015-06-26 16:03:14 +0000 | [diff] [blame] | 38 | |
Janak Ramakrishnan | 3c0adb2 | 2016-08-15 21:54:55 +0000 | [diff] [blame] | 39 | /** Returns final set of targets and sets error flag if required. */ |
ulfjack | 53a0ff1 | 2019-05-21 07:08:40 -0700 | [diff] [blame] | 40 | public Collection<Target> build(WalkableGraph walkableGraph) throws InterruptedException { |
Marian Lobur | be3171e | 2015-06-26 16:03:14 +0000 | [diff] [blame] | 41 | precomputePackages(walkableGraph); |
ulfjack | 53a0ff1 | 2019-05-21 07:08:40 -0700 | [diff] [blame] | 42 | return transformLabelsIntoTargets(resolvedLabelsBuilder); |
Marian Lobur | be3171e | 2015-06-26 16:03:14 +0000 | [diff] [blame] | 43 | } |
| 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 | */ |
ulfjack | 53a0ff1 | 2019-05-21 07:08:40 -0700 | [diff] [blame] | 50 | private Collection<Target> transformLabelsIntoTargets(Set<Label> resolvedLabels) { |
Marian Lobur | be3171e | 2015-06-26 16:03:14 +0000 | [diff] [blame] | 51 | // precomputePackages has to be called before this method. |
ulfjack | 53a0ff1 | 2019-05-21 07:08:40 -0700 | [diff] [blame] | 52 | Set<Target> targets = CompactHashSet.create(); |
Marian Lobur | be3171e | 2015-06-26 16:03:14 +0000 | [diff] [blame] | 53 | Preconditions.checkNotNull(packages); |
ulfjack | 53a0ff1 | 2019-05-21 07:08:40 -0700 | [diff] [blame] | 54 | for (Label label : resolvedLabels) { |
| 55 | targets.add(getExistingTarget(label)); |
Marian Lobur | be3171e | 2015-06-26 16:03:14 +0000 | [diff] [blame] | 56 | } |
ulfjack | 53a0ff1 | 2019-05-21 07:08:40 -0700 | [diff] [blame] | 57 | return targets; |
Marian Lobur | be3171e | 2015-06-26 16:03:14 +0000 | [diff] [blame] | 58 | } |
| 59 | |
Janak Ramakrishnan | 3c0adb2 | 2016-08-15 21:54:55 +0000 | [diff] [blame] | 60 | private void precomputePackages(WalkableGraph walkableGraph) throws InterruptedException { |
Marian Lobur | be3171e | 2015-06-26 16:03:14 +0000 | [diff] [blame] | 61 | 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<>(); |
ulfjack | 53a0ff1 | 2019-05-21 07:08:40 -0700 | [diff] [blame] | 82 | for (Label label : resolvedLabelsBuilder) { |
Marian Lobur | be3171e | 2015-06-26 16:03:14 +0000 | [diff] [blame] | 83 | packagesIdentifiers.add(label.getPackageIdentifier()); |
| 84 | } |
| 85 | return packagesIdentifiers; |
| 86 | } |
| 87 | |
Janak Ramakrishnan | 3c0adb2 | 2016-08-15 21:54:55 +0000 | [diff] [blame] | 88 | private static Package findPackageInGraph( |
| 89 | PackageIdentifier pkgIdentifier, WalkableGraph walkableGraph) throws InterruptedException { |
Janak Ramakrishnan | 0a4c6e4 | 2015-09-17 00:37:58 +0000 | [diff] [blame] | 90 | return Preconditions.checkNotNull( |
| 91 | ((PackageValue) walkableGraph.getValue(PackageValue.key(pkgIdentifier))), pkgIdentifier) |
| 92 | .getPackage(); |
Marian Lobur | be3171e | 2015-06-26 16:03:14 +0000 | [diff] [blame] | 93 | } |
| 94 | |
ulfjack | c96839f | 2019-05-15 04:49:40 -0700 | [diff] [blame] | 95 | /** Adds the result from expansion of negative target pattern (eg, "-//foo:all"). */ |
| 96 | void addLabelsOfPositivePattern(ResolvedTargets<Label> labels) { |
ulfjack | 53a0ff1 | 2019-05-21 07:08:40 -0700 | [diff] [blame] | 97 | Preconditions.checkArgument(labels.getFilteredTargets().isEmpty()); |
| 98 | resolvedLabelsBuilder.addAll(labels.getTargets()); |
ulfjack | c96839f | 2019-05-15 04:49:40 -0700 | [diff] [blame] | 99 | } |
Marian Lobur | be3171e | 2015-06-26 16:03:14 +0000 | [diff] [blame] | 100 | } |