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.skyframe; |
| 15 | |
janakr | 6cf00a0 | 2018-11-09 12:26:30 -0800 | [diff] [blame] | 16 | import com.google.common.base.MoreObjects; |
tomlu | a155b53 | 2017-11-08 20:12:47 +0100 | [diff] [blame] | 17 | import com.google.common.base.Preconditions; |
janakr | 5fb2a48 | 2018-03-02 17:48:57 -0800 | [diff] [blame] | 18 | import com.google.common.collect.Interner; |
nharmata | d86b509 | 2018-10-16 15:50:21 -0700 | [diff] [blame] | 19 | import com.google.devtools.build.lib.cmdline.Label; |
Kristina Chodorow | 73fa203 | 2015-08-28 17:57:46 +0000 | [diff] [blame] | 20 | import com.google.devtools.build.lib.cmdline.PackageIdentifier; |
janakr | 5fb2a48 | 2018-03-02 17:48:57 -0800 | [diff] [blame] | 21 | import com.google.devtools.build.lib.concurrent.BlazeInterners; |
shahan | bbbf3dc | 2018-02-23 15:41:52 -0800 | [diff] [blame] | 22 | import com.google.devtools.build.lib.skyframe.serialization.autocodec.AutoCodec; |
nharmata | d86b509 | 2018-10-16 15:50:21 -0700 | [diff] [blame] | 23 | import com.google.devtools.build.lib.vfs.PathFragment; |
tomlu | ee6a686 | 2018-01-17 14:36:26 -0800 | [diff] [blame] | 24 | import com.google.devtools.build.lib.vfs.Root; |
janakr | 5fb2a48 | 2018-03-02 17:48:57 -0800 | [diff] [blame] | 25 | import com.google.devtools.build.skyframe.AbstractSkyKey; |
| 26 | import com.google.devtools.build.skyframe.SkyFunctionName; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 27 | import com.google.devtools.build.skyframe.SkyValue; |
Googler | 06eb1bb | 2019-02-26 15:33:15 -0800 | [diff] [blame] | 28 | import javax.annotation.Nonnull; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 29 | |
| 30 | /** |
| 31 | * A value that represents the result of looking for the existence of a package that owns a |
| 32 | * specific directory path. Compare with {@link PackageLookupValue}, which deals with existence of |
| 33 | * a specific package. |
| 34 | */ |
| 35 | public abstract class ContainingPackageLookupValue implements SkyValue { |
Michajlo Matijkiw | 1118459 | 2015-10-16 19:00:45 +0000 | [diff] [blame] | 36 | |
shahan | 1fe2312 | 2018-02-28 09:18:36 -0800 | [diff] [blame] | 37 | @AutoCodec public static final NoContainingPackage NONE = new NoContainingPackage(); |
Michajlo Matijkiw | 1118459 | 2015-10-16 19:00:45 +0000 | [diff] [blame] | 38 | |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 39 | /** Returns whether there is a containing package. */ |
| 40 | public abstract boolean hasContainingPackage(); |
| 41 | |
| 42 | /** If there is a containing package, returns its name. */ |
Janak Ramakrishnan | 29c5ab4 | 2015-05-14 19:38:12 +0000 | [diff] [blame] | 43 | public abstract PackageIdentifier getContainingPackageName(); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 44 | |
| 45 | /** If there is a containing package, returns its package root */ |
tomlu | ee6a686 | 2018-01-17 14:36:26 -0800 | [diff] [blame] | 46 | public abstract Root getContainingPackageRoot(); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 47 | |
Googler | 06eb1bb | 2019-02-26 15:33:15 -0800 | [diff] [blame] | 48 | /** |
| 49 | * If there is not a containing package, returns a reason why (this is usually the reason the |
| 50 | * outer-most directory isn't a package). |
| 51 | */ |
| 52 | public String getReasonForNoContainingPackage() { |
| 53 | throw new IllegalStateException(); |
| 54 | } |
| 55 | |
janakr | 5fb2a48 | 2018-03-02 17:48:57 -0800 | [diff] [blame] | 56 | public static Key key(PackageIdentifier id) { |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 57 | Preconditions.checkArgument(!id.getPackageFragment().isAbsolute(), id); |
Brian Silverman | d7d6d62 | 2016-03-17 09:53:39 +0000 | [diff] [blame] | 58 | Preconditions.checkArgument(!id.getRepository().isDefault(), id); |
janakr | 5fb2a48 | 2018-03-02 17:48:57 -0800 | [diff] [blame] | 59 | return Key.create(id); |
| 60 | } |
| 61 | |
nharmata | d86b509 | 2018-10-16 15:50:21 -0700 | [diff] [blame] | 62 | static String getErrorMessageForLabelCrossingPackageBoundary( |
| 63 | Root pkgRoot, |
| 64 | Label label, |
| 65 | ContainingPackageLookupValue containingPkgLookupValue) { |
| 66 | PackageIdentifier containingPkg = containingPkgLookupValue.getContainingPackageName(); |
| 67 | boolean crossesPackageBoundaryBelow = |
| 68 | containingPkg.getSourceRoot().startsWith(label.getPackageIdentifier().getSourceRoot()); |
| 69 | PathFragment labelNameFragment = PathFragment.create(label.getName()); |
laurentlb | af489f2 | 2019-07-29 08:19:03 -0700 | [diff] [blame] | 70 | String message; |
| 71 | if (crossesPackageBoundaryBelow) { |
| 72 | message = |
| 73 | String.format("Label '%s' is invalid because '%s' is a subpackage", label, containingPkg); |
| 74 | } else { |
| 75 | message = |
| 76 | String.format( |
| 77 | "Label '%s' is invalid because '%s' is not a package", label, label.getPackageName()); |
| 78 | } |
| 79 | |
nharmata | d86b509 | 2018-10-16 15:50:21 -0700 | [diff] [blame] | 80 | Root containingRoot = containingPkgLookupValue.getContainingPackageRoot(); |
| 81 | if (pkgRoot.equals(containingRoot)) { |
| 82 | PathFragment containingPkgFragment = containingPkg.getPackageFragment(); |
| 83 | PathFragment labelNameInContainingPackage = |
| 84 | crossesPackageBoundaryBelow |
| 85 | ? labelNameFragment.subFragment( |
| 86 | containingPkgFragment.segmentCount() |
| 87 | - label.getPackageFragment().segmentCount(), |
| 88 | labelNameFragment.segmentCount()) |
| 89 | : label.toPathFragment().relativeTo(containingPkgFragment); |
laurentlb | af489f2 | 2019-07-29 08:19:03 -0700 | [diff] [blame] | 90 | message += "; perhaps you meant to put the colon here: '"; |
nharmata | d86b509 | 2018-10-16 15:50:21 -0700 | [diff] [blame] | 91 | if (containingPkg.getRepository().isDefault() || containingPkg.getRepository().isMain()) { |
| 92 | message += "//"; |
| 93 | } |
laurentlb | af489f2 | 2019-07-29 08:19:03 -0700 | [diff] [blame] | 94 | message += containingPkg + ":" + labelNameInContainingPackage + "'?"; |
nharmata | d86b509 | 2018-10-16 15:50:21 -0700 | [diff] [blame] | 95 | } else { |
laurentlb | af489f2 | 2019-07-29 08:19:03 -0700 | [diff] [blame] | 96 | message += |
| 97 | "; have you deleted " |
| 98 | + containingPkg |
| 99 | + "/BUILD? " |
| 100 | + "If so, use the --deleted_packages=" |
| 101 | + containingPkg |
| 102 | + " option"; |
nharmata | d86b509 | 2018-10-16 15:50:21 -0700 | [diff] [blame] | 103 | } |
| 104 | return message; |
| 105 | } |
| 106 | |
jcater | c52cfa4 | 2020-05-18 06:16:02 -0700 | [diff] [blame] | 107 | /** {@link com.google.devtools.build.skyframe.SkyKey} for {@code ContainingPackageLookupValue}. */ |
janakr | 5fb2a48 | 2018-03-02 17:48:57 -0800 | [diff] [blame] | 108 | @AutoCodec |
janakr | 43c642d | 2018-10-18 09:05:08 -0700 | [diff] [blame] | 109 | public static class Key extends AbstractSkyKey<PackageIdentifier> { |
janakr | 5fb2a48 | 2018-03-02 17:48:57 -0800 | [diff] [blame] | 110 | private static final Interner<Key> interner = BlazeInterners.newWeakInterner(); |
| 111 | |
| 112 | private Key(PackageIdentifier arg) { |
| 113 | super(arg); |
| 114 | } |
| 115 | |
| 116 | @AutoCodec.VisibleForSerialization |
| 117 | @AutoCodec.Instantiator |
| 118 | static Key create(PackageIdentifier arg) { |
| 119 | return interner.intern(new Key(arg)); |
| 120 | } |
| 121 | |
| 122 | @Override |
| 123 | public SkyFunctionName functionName() { |
| 124 | return SkyFunctions.CONTAINING_PACKAGE_LOOKUP; |
| 125 | } |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 126 | } |
| 127 | |
tomlu | ee6a686 | 2018-01-17 14:36:26 -0800 | [diff] [blame] | 128 | public static ContainingPackage withContainingPackage(PackageIdentifier pkgId, Root root) { |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 129 | return new ContainingPackage(pkgId, root); |
| 130 | } |
| 131 | |
Michajlo Matijkiw | 1118459 | 2015-10-16 19:00:45 +0000 | [diff] [blame] | 132 | /** Value indicating there is no containing package. */ |
| 133 | public static class NoContainingPackage extends ContainingPackageLookupValue { |
Googler | 06eb1bb | 2019-02-26 15:33:15 -0800 | [diff] [blame] | 134 | private final String reason; |
| 135 | |
| 136 | private NoContainingPackage() { |
| 137 | this.reason = null; |
| 138 | } |
| 139 | |
| 140 | NoContainingPackage(@Nonnull String reason) { |
| 141 | this.reason = reason; |
| 142 | } |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 143 | |
| 144 | @Override |
| 145 | public boolean hasContainingPackage() { |
| 146 | return false; |
| 147 | } |
| 148 | |
| 149 | @Override |
| 150 | public PackageIdentifier getContainingPackageName() { |
| 151 | throw new IllegalStateException(); |
| 152 | } |
| 153 | |
| 154 | @Override |
tomlu | ee6a686 | 2018-01-17 14:36:26 -0800 | [diff] [blame] | 155 | public Root getContainingPackageRoot() { |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 156 | throw new IllegalStateException(); |
| 157 | } |
janakr | 6cf00a0 | 2018-11-09 12:26:30 -0800 | [diff] [blame] | 158 | |
| 159 | @Override |
| 160 | public String toString() { |
| 161 | return getClass().getName(); |
| 162 | } |
Googler | 06eb1bb | 2019-02-26 15:33:15 -0800 | [diff] [blame] | 163 | |
| 164 | @Override |
| 165 | public String getReasonForNoContainingPackage() { |
| 166 | return reason; |
| 167 | } |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 168 | } |
| 169 | |
Michajlo Matijkiw | 1118459 | 2015-10-16 19:00:45 +0000 | [diff] [blame] | 170 | /** A successful lookup value. */ |
shahan | bbbf3dc | 2018-02-23 15:41:52 -0800 | [diff] [blame] | 171 | @AutoCodec |
Michajlo Matijkiw | 1118459 | 2015-10-16 19:00:45 +0000 | [diff] [blame] | 172 | public static class ContainingPackage extends ContainingPackageLookupValue { |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 173 | private final PackageIdentifier containingPackage; |
tomlu | ee6a686 | 2018-01-17 14:36:26 -0800 | [diff] [blame] | 174 | private final Root containingPackageRoot; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 175 | |
shahan | bbbf3dc | 2018-02-23 15:41:52 -0800 | [diff] [blame] | 176 | @AutoCodec.Instantiator |
| 177 | @AutoCodec.VisibleForSerialization |
| 178 | ContainingPackage(PackageIdentifier containingPackage, Root containingPackageRoot) { |
| 179 | this.containingPackage = containingPackage; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 180 | this.containingPackageRoot = containingPackageRoot; |
| 181 | } |
| 182 | |
| 183 | @Override |
| 184 | public boolean hasContainingPackage() { |
| 185 | return true; |
| 186 | } |
| 187 | |
| 188 | @Override |
| 189 | public PackageIdentifier getContainingPackageName() { |
| 190 | return containingPackage; |
| 191 | } |
| 192 | |
| 193 | @Override |
tomlu | ee6a686 | 2018-01-17 14:36:26 -0800 | [diff] [blame] | 194 | public Root getContainingPackageRoot() { |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 195 | return containingPackageRoot; |
| 196 | } |
| 197 | |
| 198 | @Override |
| 199 | public boolean equals(Object obj) { |
| 200 | if (this == obj) { |
| 201 | return true; |
| 202 | } |
| 203 | if (!(obj instanceof ContainingPackage)) { |
| 204 | return false; |
| 205 | } |
| 206 | ContainingPackage other = (ContainingPackage) obj; |
| 207 | return containingPackage.equals(other.containingPackage) |
| 208 | && containingPackageRoot.equals(other.containingPackageRoot); |
| 209 | } |
| 210 | |
| 211 | @Override |
| 212 | public int hashCode() { |
| 213 | return containingPackage.hashCode(); |
| 214 | } |
janakr | 6cf00a0 | 2018-11-09 12:26:30 -0800 | [diff] [blame] | 215 | |
| 216 | @Override |
| 217 | public String toString() { |
| 218 | return MoreObjects.toStringHelper(this) |
| 219 | .add("containingPackage", containingPackage) |
| 220 | .add("containingPackageRoot", containingPackageRoot) |
| 221 | .toString(); |
| 222 | } |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 223 | } |
| 224 | } |