blob: 935d48d20220014d55b69c38c738b13df35424d5 [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.
14package com.google.devtools.build.lib.packages;
15
16import com.google.common.collect.ImmutableList;
dannark90e2b4b2018-06-27 13:35:04 -070017import com.google.common.collect.ImmutableMap;
Lukacs Berki6e91eb92015-09-21 09:12:37 +000018import com.google.devtools.build.lib.cmdline.Label;
Lukacs Berkia6434362015-09-15 13:56:14 +000019import com.google.devtools.build.lib.cmdline.LabelSyntaxException;
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010020import com.google.devtools.build.lib.concurrent.ThreadSafety.Immutable;
21import com.google.devtools.build.lib.concurrent.ThreadSafety.ThreadSafe;
janakr73d33102018-05-29 08:21:29 -070022import com.google.devtools.build.lib.skyframe.serialization.autocodec.AutoCodec;
23import com.google.devtools.build.lib.syntax.EvalException;
adonovan889bc0b2020-08-07 18:35:24 -070024import com.google.devtools.build.lib.syntax.Starlark;
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010025import java.io.Serializable;
26import java.util.Collections;
27import java.util.List;
28
29/**
30 * A rule visibility that simply says yes or no. It corresponds to public,
31 * legacy_public and private visibilities.
32 */
33@Immutable @ThreadSafe
34public class ConstantRuleVisibility implements RuleVisibility, Serializable {
janakr73d33102018-05-29 08:21:29 -070035 @AutoCodec
36 static final Label LEGACY_PUBLIC_LABEL; // same as "public"; used for automated depot cleanup
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010037
janakr73d33102018-05-29 08:21:29 -070038 @AutoCodec @AutoCodec.VisibleForSerialization static final Label PUBLIC_LABEL;
39 @AutoCodec @AutoCodec.VisibleForSerialization static final Label PRIVATE_LABEL;
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010040
janakr73d33102018-05-29 08:21:29 -070041 @AutoCodec public static final ConstantRuleVisibility PUBLIC = new ConstantRuleVisibility(true);
42
43 @AutoCodec public static final ConstantRuleVisibility PRIVATE = new ConstantRuleVisibility(false);
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010044
45 static {
46 try {
dannark90e2b4b2018-06-27 13:35:04 -070047 PUBLIC_LABEL = Label.parseAbsolute("//visibility:public", ImmutableMap.of());
48 LEGACY_PUBLIC_LABEL = Label.parseAbsolute("//visibility:legacy_public", ImmutableMap.of());
49 PRIVATE_LABEL = Label.parseAbsolute("//visibility:private", ImmutableMap.of());
Lukacs Berkia6434362015-09-15 13:56:14 +000050 } catch (LabelSyntaxException e) {
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010051 throw new IllegalStateException();
52 }
53 }
54
55 private final boolean result;
56
57 public ConstantRuleVisibility(boolean result) {
58 this.result = result;
59 }
60
61 public boolean isPubliclyVisible() {
62 return result;
63 }
64
65 @Override
66 public List<Label> getDependencyLabels() {
67 return Collections.emptyList();
68 }
69
70 @Override
71 public List<Label> getDeclaredLabels() {
72 return ImmutableList.of(result ? PUBLIC_LABEL : PRIVATE_LABEL);
73 }
74
75 /**
76 * Tries to parse a list of labels into a {@link ConstantRuleVisibility}.
77 *
78 * @param labels the list of labels to parse
79 * @return The resulting visibility object, or null if the list of labels
80 * could not be parsed.
81 */
Yue Gan36a26572016-05-25 11:48:10 +000082 public static ConstantRuleVisibility tryParse(List<Label> labels) throws EvalException {
83 if (labels.size() == 1) {
84 return tryParse(labels.get(0));
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010085 }
Yue Gan36a26572016-05-25 11:48:10 +000086 ConstantRuleVisibility visibility;
87 for (Label label : labels) {
88 visibility = tryParse(label);
89 if (visibility != null) {
adonovan889bc0b2020-08-07 18:35:24 -070090 throw Starlark.errorf(
adonovana6287362020-08-07 07:15:30 -070091 "Public or private visibility labels (e.g. //visibility:public or"
92 + " //visibility:private) cannot be used in combination with other labels");
Yue Gan36a26572016-05-25 11:48:10 +000093 }
94 }
95 return null;
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010096 }
97
98 public static ConstantRuleVisibility tryParse(Label label) {
99 if (PUBLIC_LABEL.equals(label) || LEGACY_PUBLIC_LABEL.equals(label)) {
100 return PUBLIC;
101 } else if (PRIVATE_LABEL.equals(label)) {
102 return PRIVATE;
103 } else {
104 return null;
105 }
106 }
107}