Damien Martin-Guillerez | f88f4d8 | 2015-09-25 13:56:55 +0000 | [diff] [blame] | 1 | // Copyright 2015 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 | |
Kristina Chodorow | 73fa203 | 2015-08-28 17:57:46 +0000 | [diff] [blame] | 15 | package com.google.devtools.build.lib.cmdline; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 16 | |
tomlu | a155b53 | 2017-11-08 20:12:47 +0100 | [diff] [blame] | 17 | import com.google.common.base.Preconditions; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 18 | import com.google.common.collect.ComparisonChain; |
twerth | c366166 | 2018-07-04 05:56:27 -0700 | [diff] [blame] | 19 | import com.google.common.collect.ImmutableMap; |
Miguel Alcon Pinto | 2627b9f | 2015-10-01 16:02:53 +0000 | [diff] [blame] | 20 | import com.google.common.collect.Interner; |
Nathan Harmata | a16d9f1 | 2016-11-23 20:58:07 +0000 | [diff] [blame] | 21 | import com.google.devtools.build.lib.concurrent.BlazeInterners; |
shahan | 20f35b4 | 2018-02-28 15:57:33 -0800 | [diff] [blame] | 22 | import com.google.devtools.build.lib.skyframe.serialization.autocodec.AutoCodec; |
vladmos | f07773b | 2017-07-11 16:31:32 +0200 | [diff] [blame] | 23 | import com.google.devtools.build.lib.skylarkinterface.SkylarkPrinter; |
| 24 | import com.google.devtools.build.lib.skylarkinterface.SkylarkValue; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 25 | import com.google.devtools.build.lib.vfs.PathFragment; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 26 | import java.io.Serializable; |
Shreya Bhattarai | fed931d | 2016-05-26 13:46:39 +0000 | [diff] [blame] | 27 | import java.util.Objects; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 28 | import javax.annotation.concurrent.Immutable; |
| 29 | |
| 30 | /** |
| 31 | * Uniquely identifies a package, given a repository name and a package's path fragment. |
| 32 | * |
vladmos | f07773b | 2017-07-11 16:31:32 +0200 | [diff] [blame] | 33 | * <p>The repository the build is happening in is the <i>default workspace</i>, and is identified by |
| 34 | * the workspace name "". Other repositories can be named in the WORKSPACE file. These workspaces |
| 35 | * are prefixed by {@literal @}. |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 36 | */ |
shahan | 20f35b4 | 2018-02-28 15:57:33 -0800 | [diff] [blame] | 37 | @AutoCodec |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 38 | @Immutable |
vladmos | f07773b | 2017-07-11 16:31:32 +0200 | [diff] [blame] | 39 | public final class PackageIdentifier |
| 40 | implements Comparable<PackageIdentifier>, Serializable, SkylarkValue { |
Nathan Harmata | a16d9f1 | 2016-11-23 20:58:07 +0000 | [diff] [blame] | 41 | private static final Interner<PackageIdentifier> INTERNER = BlazeInterners.newWeakInterner(); |
Miguel Alcon Pinto | 2627b9f | 2015-10-01 16:02:53 +0000 | [diff] [blame] | 42 | |
| 43 | public static PackageIdentifier create(String repository, PathFragment pkgName) |
| 44 | throws LabelSyntaxException { |
| 45 | return create(RepositoryName.create(repository), pkgName); |
| 46 | } |
| 47 | |
shahan | 20f35b4 | 2018-02-28 15:57:33 -0800 | [diff] [blame] | 48 | @AutoCodec.Instantiator |
Miguel Alcon Pinto | 2627b9f | 2015-10-01 16:02:53 +0000 | [diff] [blame] | 49 | public static PackageIdentifier create(RepositoryName repository, PathFragment pkgName) { |
shreyax | 43a1dc7 | 2018-06-02 15:08:51 -0700 | [diff] [blame] | 50 | // Note: We rely on these being interned to fast-path Label#equals. |
Miguel Alcon Pinto | 2627b9f | 2015-10-01 16:02:53 +0000 | [diff] [blame] | 51 | return INTERNER.intern(new PackageIdentifier(repository, pkgName)); |
| 52 | } |
| 53 | |
Kristina Chodorow | a1a31ff | 2016-07-27 16:34:27 +0000 | [diff] [blame] | 54 | public static final PackageIdentifier EMPTY_PACKAGE_ID = createInMainRepo( |
| 55 | PathFragment.EMPTY_FRAGMENT); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 56 | |
Brian Silverman | d7d6d62 | 2016-03-17 09:53:39 +0000 | [diff] [blame] | 57 | public static PackageIdentifier createInMainRepo(String name) { |
nharmata | b4060b6 | 2017-04-04 17:11:39 +0000 | [diff] [blame] | 58 | return createInMainRepo(PathFragment.create(name)); |
Brian Silverman | d7d6d62 | 2016-03-17 09:53:39 +0000 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | public static PackageIdentifier createInMainRepo(PathFragment name) { |
Kristina Chodorow | a1a31ff | 2016-07-27 16:34:27 +0000 | [diff] [blame] | 62 | return create(RepositoryName.MAIN, name); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | /** |
Ulf Adams | a28b540 | 2017-02-24 09:28:44 +0000 | [diff] [blame] | 66 | * Tries to infer the package identifier from the given exec path. This method does not perform |
| 67 | * any I/O, but looks solely at the structure of the exec path. The resulting identifier may |
| 68 | * actually be a subdirectory of a package rather than a package, e.g.: |
jcater | 7b00f14 | 2017-08-30 23:10:11 +0200 | [diff] [blame] | 69 | * |
Ulf Adams | a28b540 | 2017-02-24 09:28:44 +0000 | [diff] [blame] | 70 | * <pre><code> |
| 71 | * + WORKSPACE |
| 72 | * + foo/BUILD |
| 73 | * + foo/bar/bar.java |
| 74 | * </code></pre> |
| 75 | * |
| 76 | * In this case, this method returns a package identifier for foo/bar, even though that is not a |
| 77 | * package. Callers need to look up the actual package if needed. |
| 78 | * |
jcater | 7b00f14 | 2017-08-30 23:10:11 +0200 | [diff] [blame] | 79 | * @throws LabelSyntaxException if the exec path seems to be for an external repository that does |
| 80 | * not have a valid repository name (see {@link RepositoryName#create}) |
Ulf Adams | a28b540 | 2017-02-24 09:28:44 +0000 | [diff] [blame] | 81 | */ |
| 82 | public static PackageIdentifier discoverFromExecPath(PathFragment execPath, boolean forFiles) |
| 83 | throws LabelSyntaxException { |
| 84 | Preconditions.checkArgument(!execPath.isAbsolute(), execPath); |
| 85 | PathFragment tofind = forFiles |
| 86 | ? Preconditions.checkNotNull( |
| 87 | execPath.getParentDirectory(), "Must pass in files, not root directory") |
| 88 | : execPath; |
dannark | be3cefc | 2018-12-13 11:52:45 -0800 | [diff] [blame^] | 89 | if (tofind.startsWith(LabelConstants.EXTERNAL_PATH_PREFIX)) { |
Ulf Adams | a28b540 | 2017-02-24 09:28:44 +0000 | [diff] [blame] | 90 | // TODO(ulfjack): Remove this when kchodorow@'s exec root rearrangement has been rolled out. |
| 91 | RepositoryName repository = RepositoryName.create("@" + tofind.getSegment(1)); |
tomlu | e7552c5 | 2018-01-19 10:25:19 -0800 | [diff] [blame] | 92 | return PackageIdentifier.create(repository, tofind.subFragment(2)); |
tomlu | a729b9b | 2018-02-08 15:32:00 -0800 | [diff] [blame] | 93 | } else if (tofind.containsUplevelReferences()) { |
Ulf Adams | a28b540 | 2017-02-24 09:28:44 +0000 | [diff] [blame] | 94 | RepositoryName repository = RepositoryName.create("@" + tofind.getSegment(1)); |
tomlu | e7552c5 | 2018-01-19 10:25:19 -0800 | [diff] [blame] | 95 | return PackageIdentifier.create(repository, tofind.subFragment(2)); |
Ulf Adams | a28b540 | 2017-02-24 09:28:44 +0000 | [diff] [blame] | 96 | } else { |
| 97 | return PackageIdentifier.createInMainRepo(tofind); |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | /** |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 102 | * The identifier for this repository. This is either "" or prefixed with an "@", |
| 103 | * e.g., "@myrepo". |
| 104 | */ |
| 105 | private final RepositoryName repository; |
| 106 | |
brandjon | f76ad07 | 2017-04-12 16:32:05 +0000 | [diff] [blame] | 107 | /** The name of the package. */ |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 108 | private final PathFragment pkgName; |
| 109 | |
brandjon | f76ad07 | 2017-04-12 16:32:05 +0000 | [diff] [blame] | 110 | /** |
| 111 | * Precomputed hash code. Hash/equality is based on repository and pkgName. Note that due to |
| 112 | * interning, x.equals(y) <=> x==y. |
| 113 | **/ |
Shreya Bhattarai | fed931d | 2016-05-26 13:46:39 +0000 | [diff] [blame] | 114 | private final int hashCode; |
| 115 | |
Miguel Alcon Pinto | 2627b9f | 2015-10-01 16:02:53 +0000 | [diff] [blame] | 116 | private PackageIdentifier(RepositoryName repository, PathFragment pkgName) { |
Eric Fellheimer | 913d458 | 2016-01-14 17:53:54 +0000 | [diff] [blame] | 117 | this.repository = Preconditions.checkNotNull(repository); |
shreyax | a1f90ce | 2018-02-15 12:05:13 -0800 | [diff] [blame] | 118 | this.pkgName = Preconditions.checkNotNull(pkgName); |
Shreya Bhattarai | fed931d | 2016-05-26 13:46:39 +0000 | [diff] [blame] | 119 | this.hashCode = Objects.hash(repository, pkgName); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 120 | } |
| 121 | |
Lukacs Berki | a643436 | 2015-09-15 13:56:14 +0000 | [diff] [blame] | 122 | public static PackageIdentifier parse(String input) throws LabelSyntaxException { |
twerth | c366166 | 2018-07-04 05:56:27 -0700 | [diff] [blame] | 123 | return parse(input, /* repo= */ null, /* repositoryMapping= */ null); |
| 124 | } |
| 125 | |
| 126 | public static PackageIdentifier parse( |
| 127 | String input, String repo, ImmutableMap<RepositoryName, RepositoryName> repositoryMapping) |
| 128 | throws LabelSyntaxException { |
Lukacs Berki | 33aa1e1 | 2015-07-08 08:11:30 +0000 | [diff] [blame] | 129 | String packageName; |
| 130 | int packageStartPos = input.indexOf("//"); |
twerth | c366166 | 2018-07-04 05:56:27 -0700 | [diff] [blame] | 131 | if (repo != null) { |
| 132 | packageName = input; |
| 133 | } else if (input.startsWith("@") && packageStartPos > 0) { |
Lukacs Berki | 33aa1e1 | 2015-07-08 08:11:30 +0000 | [diff] [blame] | 134 | repo = input.substring(0, packageStartPos); |
| 135 | packageName = input.substring(packageStartPos + 2); |
Lukacs Berki | a643436 | 2015-09-15 13:56:14 +0000 | [diff] [blame] | 136 | } else if (input.startsWith("@")) { |
Lukacs Berki | 485eb96 | 2016-01-13 10:47:29 +0000 | [diff] [blame] | 137 | throw new LabelSyntaxException("starts with a '@' but does not contain '//'"); |
Lukacs Berki | 33aa1e1 | 2015-07-08 08:11:30 +0000 | [diff] [blame] | 138 | } else if (packageStartPos == 0) { |
Kristina Chodorow | a1a31ff | 2016-07-27 16:34:27 +0000 | [diff] [blame] | 139 | repo = RepositoryName.DEFAULT_REPOSITORY; |
Lukacs Berki | 33aa1e1 | 2015-07-08 08:11:30 +0000 | [diff] [blame] | 140 | packageName = input.substring(2); |
| 141 | } else { |
Kristina Chodorow | a1a31ff | 2016-07-27 16:34:27 +0000 | [diff] [blame] | 142 | repo = RepositoryName.DEFAULT_REPOSITORY; |
Lukacs Berki | 33aa1e1 | 2015-07-08 08:11:30 +0000 | [diff] [blame] | 143 | packageName = input; |
| 144 | } |
| 145 | |
| 146 | String error = RepositoryName.validate(repo); |
| 147 | if (error != null) { |
Lukacs Berki | a643436 | 2015-09-15 13:56:14 +0000 | [diff] [blame] | 148 | throw new LabelSyntaxException(error); |
Lukacs Berki | 33aa1e1 | 2015-07-08 08:11:30 +0000 | [diff] [blame] | 149 | } |
| 150 | |
| 151 | error = LabelValidator.validatePackageName(packageName); |
| 152 | if (error != null) { |
Lukacs Berki | a643436 | 2015-09-15 13:56:14 +0000 | [diff] [blame] | 153 | throw new LabelSyntaxException(error); |
Lukacs Berki | 33aa1e1 | 2015-07-08 08:11:30 +0000 | [diff] [blame] | 154 | } |
| 155 | |
twerth | c366166 | 2018-07-04 05:56:27 -0700 | [diff] [blame] | 156 | if (repositoryMapping != null) { |
| 157 | RepositoryName repositoryName = RepositoryName.create(repo); |
| 158 | repositoryName = repositoryMapping.getOrDefault(repositoryName, repositoryName); |
| 159 | return create(repositoryName, PathFragment.create(packageName)); |
| 160 | } else { |
| 161 | return create(repo, PathFragment.create(packageName)); |
| 162 | } |
Lukacs Berki | 33aa1e1 | 2015-07-08 08:11:30 +0000 | [diff] [blame] | 163 | } |
| 164 | |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 165 | public RepositoryName getRepository() { |
| 166 | return repository; |
| 167 | } |
| 168 | |
| 169 | public PathFragment getPackageFragment() { |
| 170 | return pkgName; |
| 171 | } |
| 172 | |
| 173 | /** |
Kristina Chodorow | a1a31ff | 2016-07-27 16:34:27 +0000 | [diff] [blame] | 174 | * Returns a relative path to the source code for this package. Returns pkgName if this is in the |
| 175 | * main repository or external/[repository name]/[pkgName] if not. |
Kristina Chodorow | 9bf65cb | 2015-03-18 18:42:35 +0000 | [diff] [blame] | 176 | */ |
Kristina Chodorow | a1a31ff | 2016-07-27 16:34:27 +0000 | [diff] [blame] | 177 | public PathFragment getSourceRoot() { |
| 178 | return repository.getSourceRoot().getRelative(pkgName); |
Kristina Chodorow | 9bf65cb | 2015-03-18 18:42:35 +0000 | [diff] [blame] | 179 | } |
| 180 | |
Kristina Chodorow | bd016c9 | 2016-09-09 14:10:44 +0000 | [diff] [blame] | 181 | public PathFragment getPathUnderExecRoot() { |
Kristina Chodorow | f57730b | 2017-01-05 19:43:03 +0000 | [diff] [blame] | 182 | return repository.getPathUnderExecRoot().getRelative(pkgName); |
Laszlo Csomor | 8539a12 | 2016-09-20 15:40:42 +0000 | [diff] [blame] | 183 | } |
| 184 | |
Dmitry Lomov | e36a66c | 2017-02-17 14:48:48 +0000 | [diff] [blame] | 185 | /** |
| 186 | * Returns the runfiles/execRoot path for this repository (relative to the x.runfiles/main-repo/ |
| 187 | * directory). |
| 188 | */ |
| 189 | public PathFragment getRunfilesPath() { |
| 190 | return repository.getRunfilesPath().getRelative(pkgName); |
| 191 | } |
| 192 | |
Brian Silverman | d7d6d62 | 2016-03-17 09:53:39 +0000 | [diff] [blame] | 193 | public PackageIdentifier makeAbsolute() { |
| 194 | if (!repository.isDefault()) { |
| 195 | return this; |
| 196 | } |
| 197 | |
Kristina Chodorow | a1a31ff | 2016-07-27 16:34:27 +0000 | [diff] [blame] | 198 | return create(RepositoryName.MAIN, pkgName); |
Brian Silverman | d7d6d62 | 2016-03-17 09:53:39 +0000 | [diff] [blame] | 199 | } |
| 200 | |
Kristina Chodorow | 9bf65cb | 2015-03-18 18:42:35 +0000 | [diff] [blame] | 201 | /** |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 202 | * Returns the name of this package. |
| 203 | * |
| 204 | * <p>There are certain places that expect the path fragment as the package name ('foo/bar') as a |
| 205 | * package identifier. This isn't specific enough for packages in other repositories, so their |
| 206 | * stringified version is '@baz//foo/bar'.</p> |
| 207 | */ |
| 208 | @Override |
| 209 | public String toString() { |
Brian Silverman | d7d6d62 | 2016-03-17 09:53:39 +0000 | [diff] [blame] | 210 | return (repository.isDefault() || repository.isMain() ? "" : repository + "//") + pkgName; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 211 | } |
| 212 | |
| 213 | @Override |
| 214 | public boolean equals(Object object) { |
| 215 | if (this == object) { |
| 216 | return true; |
| 217 | } |
Ulf Adams | c5aeaa3 | 2015-02-06 14:05:30 +0000 | [diff] [blame] | 218 | if (!(object instanceof PackageIdentifier)) { |
| 219 | return false; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 220 | } |
Ulf Adams | c5aeaa3 | 2015-02-06 14:05:30 +0000 | [diff] [blame] | 221 | PackageIdentifier that = (PackageIdentifier) object; |
Shreya Bhattarai | fed931d | 2016-05-26 13:46:39 +0000 | [diff] [blame] | 222 | return this.hashCode == that.hashCode && pkgName.equals(that.pkgName) |
| 223 | && repository.equals(that.repository); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 224 | } |
| 225 | |
| 226 | @Override |
| 227 | public int hashCode() { |
Shreya Bhattarai | fed931d | 2016-05-26 13:46:39 +0000 | [diff] [blame] | 228 | return this.hashCode; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 229 | } |
| 230 | |
| 231 | @Override |
| 232 | public int compareTo(PackageIdentifier that) { |
| 233 | return ComparisonChain.start() |
| 234 | .compare(repository.toString(), that.repository.toString()) |
| 235 | .compare(pkgName, that.pkgName) |
| 236 | .result(); |
| 237 | } |
vladmos | f07773b | 2017-07-11 16:31:32 +0200 | [diff] [blame] | 238 | |
| 239 | @Override |
| 240 | public void repr(SkylarkPrinter printer) { |
| 241 | printer.repr(toString()); |
| 242 | } |
Janak Ramakrishnan | a71d470 | 2015-05-06 16:55:00 +0000 | [diff] [blame] | 243 | } |