blob: bffd92941dd7d63fd05c2ec5acea01ebde6f5398 [file] [log] [blame]
Damien Martin-Guillerezf88f4d82015-09-25 13:56:55 +00001// Copyright 2014 The Bazel Authors. All rights reserved.
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +01002//
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
15package com.google.devtools.build.lib.packages;
16
17import com.google.common.collect.ImmutableList;
18import com.google.common.collect.Lists;
Lukacs Berki6e91eb92015-09-21 09:12:37 +000019import com.google.devtools.build.lib.cmdline.Label;
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010020import com.google.devtools.build.lib.events.Event;
21import com.google.devtools.build.lib.events.EventHandler;
22import com.google.devtools.build.lib.events.Location;
23import com.google.devtools.build.lib.packages.License.DistributionType;
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010024import java.util.Collection;
25import java.util.Collections;
26import java.util.List;
27import java.util.Set;
28
29/**
Mark Schaller0312f912016-07-22 18:45:02 +000030 * 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 Nienhuysd08b27f2015-02-25 16:45:20 +010033 */
34public 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 Schaller0312f912016-07-22 18:45:02 +000042 public PackageGroup(
43 Label label,
44 Package pkg,
45 Collection<String> packageSpecifications,
46 Collection<Label> includes,
47 EventHandler eventHandler,
48 Location location) {
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010049 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 Schaller0312f912016-07-22 18:45:02 +000055 for (String packageSpecification : packageSpecifications) {
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010056 PackageSpecification specification = null;
57 try {
Mark Schaller0312f912016-07-22 18:45:02 +000058 specification =
59 PackageSpecification.fromString(
60 label.getPackageIdentifier().getRepository(), packageSpecification);
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010061 } 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 Berki485eb962016-01-13 10:47:29 +000083 if (specification.containsPackage(pkg.getPackageIdentifier())) {
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010084 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 Estrena6c88962015-09-28 19:35:18 +0000155 @Override
156 public boolean isConfigurable() {
157 return false;
158 }
159
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100160 public static String targetKind() {
161 return "package group";
162 }
163}