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 | package com.google.devtools.build.lib.packages; |
| 15 | |
| 16 | import com.google.common.collect.ImmutableList; |
dannark | 90e2b4b | 2018-06-27 13:35:04 -0700 | [diff] [blame] | 17 | import com.google.common.collect.ImmutableMap; |
Lukacs Berki | 6e91eb9 | 2015-09-21 09:12:37 +0000 | [diff] [blame] | 18 | import com.google.devtools.build.lib.cmdline.Label; |
Lukacs Berki | a643436 | 2015-09-15 13:56:14 +0000 | [diff] [blame] | 19 | import com.google.devtools.build.lib.cmdline.LabelSyntaxException; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 20 | import com.google.devtools.build.lib.concurrent.ThreadSafety.Immutable; |
| 21 | import com.google.devtools.build.lib.concurrent.ThreadSafety.ThreadSafe; |
janakr | 73d3310 | 2018-05-29 08:21:29 -0700 | [diff] [blame] | 22 | import com.google.devtools.build.lib.skyframe.serialization.autocodec.AutoCodec; |
| 23 | import com.google.devtools.build.lib.syntax.EvalException; |
adonovan | 889bc0b | 2020-08-07 18:35:24 -0700 | [diff] [blame] | 24 | import com.google.devtools.build.lib.syntax.Starlark; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 25 | import java.io.Serializable; |
| 26 | import java.util.Collections; |
| 27 | import 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 |
| 34 | public class ConstantRuleVisibility implements RuleVisibility, Serializable { |
janakr | 73d3310 | 2018-05-29 08:21:29 -0700 | [diff] [blame] | 35 | @AutoCodec |
| 36 | static final Label LEGACY_PUBLIC_LABEL; // same as "public"; used for automated depot cleanup |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 37 | |
janakr | 73d3310 | 2018-05-29 08:21:29 -0700 | [diff] [blame] | 38 | @AutoCodec @AutoCodec.VisibleForSerialization static final Label PUBLIC_LABEL; |
| 39 | @AutoCodec @AutoCodec.VisibleForSerialization static final Label PRIVATE_LABEL; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 40 | |
janakr | 73d3310 | 2018-05-29 08:21:29 -0700 | [diff] [blame] | 41 | @AutoCodec public static final ConstantRuleVisibility PUBLIC = new ConstantRuleVisibility(true); |
| 42 | |
| 43 | @AutoCodec public static final ConstantRuleVisibility PRIVATE = new ConstantRuleVisibility(false); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 44 | |
| 45 | static { |
| 46 | try { |
dannark | 90e2b4b | 2018-06-27 13:35:04 -0700 | [diff] [blame] | 47 | 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 Berki | a643436 | 2015-09-15 13:56:14 +0000 | [diff] [blame] | 50 | } catch (LabelSyntaxException e) { |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 51 | 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 Gan | 36a2657 | 2016-05-25 11:48:10 +0000 | [diff] [blame] | 82 | public static ConstantRuleVisibility tryParse(List<Label> labels) throws EvalException { |
| 83 | if (labels.size() == 1) { |
| 84 | return tryParse(labels.get(0)); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 85 | } |
Yue Gan | 36a2657 | 2016-05-25 11:48:10 +0000 | [diff] [blame] | 86 | ConstantRuleVisibility visibility; |
| 87 | for (Label label : labels) { |
| 88 | visibility = tryParse(label); |
| 89 | if (visibility != null) { |
adonovan | 889bc0b | 2020-08-07 18:35:24 -0700 | [diff] [blame] | 90 | throw Starlark.errorf( |
adonovan | a628736 | 2020-08-07 07:15:30 -0700 | [diff] [blame] | 91 | "Public or private visibility labels (e.g. //visibility:public or" |
| 92 | + " //visibility:private) cannot be used in combination with other labels"); |
Yue Gan | 36a2657 | 2016-05-25 11:48:10 +0000 | [diff] [blame] | 93 | } |
| 94 | } |
| 95 | return null; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 96 | } |
| 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 | } |