Damien Martin-Guillerez | f88f4d8 | 2015-09-25 13:56:55 +0000 | [diff] [blame] | 1 | // Copyright 2014 The Bazel Authors. All rights reserved. |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [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 | |
| 15 | package com.google.devtools.build.lib.packages; |
| 16 | |
| 17 | import com.google.common.collect.ImmutableList; |
| 18 | import com.google.common.collect.Lists; |
Lukacs Berki | 6e91eb9 | 2015-09-21 09:12:37 +0000 | [diff] [blame] | 19 | import com.google.devtools.build.lib.cmdline.Label; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 20 | import com.google.devtools.build.lib.events.Event; |
| 21 | import com.google.devtools.build.lib.events.EventHandler; |
| 22 | import com.google.devtools.build.lib.events.Location; |
| 23 | import com.google.devtools.build.lib.packages.License.DistributionType; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 24 | import java.util.Collection; |
| 25 | import java.util.Collections; |
| 26 | import java.util.List; |
| 27 | import java.util.Set; |
| 28 | |
| 29 | /** |
Mark Schaller | 0312f91 | 2016-07-22 18:45:02 +0000 | [diff] [blame] | 30 | * This class represents a package group BUILD target. It has a name, a list of {@link |
| 31 | * PackageSpecification}s, a list of {@link Label}s of other package groups this one includes, and |
| 32 | * can be asked if a specific package is included in it. |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 33 | */ |
| 34 | public class PackageGroup implements Target { |
| 35 | private boolean containsErrors; |
| 36 | private final Label label; |
| 37 | private final Location location; |
| 38 | private final Package containingPackage; |
| 39 | private final List<PackageSpecification> packageSpecifications; |
| 40 | private final List<Label> includes; |
| 41 | |
Mark Schaller | 0312f91 | 2016-07-22 18:45:02 +0000 | [diff] [blame] | 42 | public PackageGroup( |
| 43 | Label label, |
| 44 | Package pkg, |
| 45 | Collection<String> packageSpecifications, |
| 46 | Collection<Label> includes, |
| 47 | EventHandler eventHandler, |
| 48 | Location location) { |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 49 | this.label = label; |
| 50 | this.location = location; |
| 51 | this.containingPackage = pkg; |
| 52 | this.includes = ImmutableList.copyOf(includes); |
| 53 | |
| 54 | ImmutableList.Builder<PackageSpecification> packagesBuilder = ImmutableList.builder(); |
Mark Schaller | 0312f91 | 2016-07-22 18:45:02 +0000 | [diff] [blame] | 55 | for (String packageSpecification : packageSpecifications) { |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 56 | PackageSpecification specification = null; |
| 57 | try { |
Mark Schaller | 0312f91 | 2016-07-22 18:45:02 +0000 | [diff] [blame] | 58 | specification = |
| 59 | PackageSpecification.fromString( |
| 60 | label.getPackageIdentifier().getRepository(), packageSpecification); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 61 | } catch (PackageSpecification.InvalidPackageSpecificationException e) { |
| 62 | containsErrors = true; |
| 63 | eventHandler.handle(Event.error(location, e.getMessage())); |
| 64 | } |
| 65 | |
| 66 | if (specification != null) { |
| 67 | packagesBuilder.add(specification); |
| 68 | } |
| 69 | } |
| 70 | this.packageSpecifications = packagesBuilder.build(); |
| 71 | } |
| 72 | |
| 73 | public boolean containsErrors() { |
| 74 | return containsErrors; |
| 75 | } |
| 76 | |
| 77 | public Iterable<PackageSpecification> getPackageSpecifications() { |
| 78 | return packageSpecifications; |
| 79 | } |
| 80 | |
| 81 | public boolean contains(Package pkg) { |
| 82 | for (PackageSpecification specification : packageSpecifications) { |
Lukacs Berki | 485eb96 | 2016-01-13 10:47:29 +0000 | [diff] [blame] | 83 | if (specification.containsPackage(pkg.getPackageIdentifier())) { |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 84 | return true; |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | return false; |
| 89 | } |
| 90 | |
| 91 | public List<Label> getIncludes() { |
| 92 | return includes; |
| 93 | } |
| 94 | |
| 95 | public List<String> getContainedPackages() { |
| 96 | List<String> result = Lists.newArrayListWithCapacity(packageSpecifications.size()); |
| 97 | for (PackageSpecification specification : packageSpecifications) { |
| 98 | result.add(specification.toString()); |
| 99 | } |
| 100 | return result; |
| 101 | } |
| 102 | |
| 103 | @Override |
| 104 | public Rule getAssociatedRule() { |
| 105 | return null; |
| 106 | } |
| 107 | |
| 108 | @Override |
| 109 | public Set<DistributionType> getDistributions() { |
| 110 | return Collections.emptySet(); |
| 111 | } |
| 112 | |
| 113 | @Override |
| 114 | public Label getLabel() { |
| 115 | return label; |
| 116 | } |
| 117 | |
| 118 | @Override public String getName() { |
| 119 | return label.getName(); |
| 120 | } |
| 121 | |
| 122 | @Override |
| 123 | public License getLicense() { |
| 124 | return License.NO_LICENSE; |
| 125 | } |
| 126 | |
| 127 | @Override |
| 128 | public Package getPackage() { |
| 129 | return containingPackage; |
| 130 | } |
| 131 | |
| 132 | @Override |
| 133 | public String getTargetKind() { |
| 134 | return targetKind(); |
| 135 | } |
| 136 | |
| 137 | @Override |
| 138 | public Location getLocation() { |
| 139 | return location; |
| 140 | } |
| 141 | |
| 142 | @Override |
| 143 | public String toString() { |
| 144 | return targetKind() + " " + getLabel(); |
| 145 | } |
| 146 | |
| 147 | @Override |
| 148 | public RuleVisibility getVisibility() { |
| 149 | // Package groups are always public to avoid a PackageGroupConfiguredTarget |
| 150 | // needing itself for the visibility check. It may work, but I did not |
| 151 | // think it over completely. |
| 152 | return ConstantRuleVisibility.PUBLIC; |
| 153 | } |
| 154 | |
Greg Estren | a6c8896 | 2015-09-28 19:35:18 +0000 | [diff] [blame] | 155 | @Override |
| 156 | public boolean isConfigurable() { |
| 157 | return false; |
| 158 | } |
| 159 | |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 160 | public static String targetKind() { |
| 161 | return "package group"; |
| 162 | } |
| 163 | } |