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 | |
Lukacs Berki | ffa73ad | 2015-09-18 11:40:12 +0000 | [diff] [blame] | 15 | package com.google.devtools.build.lib.packages; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 16 | |
Michajlo Matijkiw | 586f80f | 2016-06-09 18:51:18 +0000 | [diff] [blame] | 17 | import com.google.common.base.Objects; |
tomlu | a155b53 | 2017-11-08 20:12:47 +0100 | [diff] [blame] | 18 | import com.google.common.base.Preconditions; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 19 | import com.google.common.collect.ImmutableList; |
| 20 | import com.google.common.collect.ImmutableSet; |
Lukacs Berki | 6e91eb9 | 2015-09-21 09:12:37 +0000 | [diff] [blame] | 21 | import com.google.common.collect.Lists; |
| 22 | import com.google.devtools.build.lib.cmdline.Label; |
Laszlo Csomor | b136315c | 2016-01-19 14:03:29 +0000 | [diff] [blame] | 23 | import com.google.devtools.build.lib.concurrent.ThreadSafety.Immutable; |
| 24 | import com.google.devtools.build.lib.concurrent.ThreadSafety.ThreadSafe; |
gregce | 59f5cba | 2020-07-22 12:18:43 -0700 | [diff] [blame] | 25 | import com.google.devtools.build.lib.starlarkbuildapi.FilesetEntryApi; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 26 | import com.google.devtools.build.lib.vfs.PathFragment; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 27 | import java.util.Collection; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 28 | import java.util.LinkedHashSet; |
| 29 | import java.util.List; |
Jingwen Chen | 91b867f | 2018-05-11 08:56:06 -0700 | [diff] [blame] | 30 | import java.util.Locale; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 31 | import java.util.Set; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 32 | import javax.annotation.Nullable; |
ilist | 5645424 | 2021-09-24 01:08:05 -0700 | [diff] [blame] | 33 | import net.starlark.java.annot.StarlarkMethod; |
| 34 | import net.starlark.java.eval.EvalException; |
adonovan | 450c7ad | 2020-09-14 13:00:21 -0700 | [diff] [blame] | 35 | import net.starlark.java.eval.Printer; |
ilist | 5645424 | 2021-09-24 01:08:05 -0700 | [diff] [blame] | 36 | import net.starlark.java.eval.StarlarkThread; |
adonovan | 450c7ad | 2020-09-14 13:00:21 -0700 | [diff] [blame] | 37 | import net.starlark.java.eval.StarlarkValue; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 38 | |
| 39 | /** |
| 40 | * FilesetEntry is a value object used to represent a "FilesetEntry" inside a "Fileset" BUILD rule. |
| 41 | */ |
Laszlo Csomor | b136315c | 2016-01-19 14:03:29 +0000 | [diff] [blame] | 42 | @Immutable |
| 43 | @ThreadSafe |
Googler | 34f7058 | 2019-11-25 12:27:34 -0800 | [diff] [blame] | 44 | public final class FilesetEntry implements StarlarkValue, FilesetEntryApi { |
Lukacs Berki | de27f9e | 2015-09-17 12:18:45 +0000 | [diff] [blame] | 45 | |
jhorvitz | 9302ebd | 2021-06-28 11:09:06 -0700 | [diff] [blame] | 46 | private static final SymlinkBehavior DEFAULT_SYMLINK_BEHAVIOR = SymlinkBehavior.COPY; |
Laszlo Csomor | b136315c | 2016-01-19 14:03:29 +0000 | [diff] [blame] | 47 | public static final String DEFAULT_STRIP_PREFIX = "."; |
Laszlo Csomor | 6979c8e | 2016-02-04 10:43:11 +0000 | [diff] [blame] | 48 | public static final String STRIP_PREFIX_WORKSPACE = "%workspace%"; |
Laszlo Csomor | b136315c | 2016-01-19 14:03:29 +0000 | [diff] [blame] | 49 | |
Lukacs Berki | de27f9e | 2015-09-17 12:18:45 +0000 | [diff] [blame] | 50 | @Override |
| 51 | public boolean isImmutable() { |
adonovan | 121224e | 2020-05-18 08:39:45 -0700 | [diff] [blame] | 52 | return true; |
Lukacs Berki | de27f9e | 2015-09-17 12:18:45 +0000 | [diff] [blame] | 53 | } |
| 54 | |
Lukacs Berki | de27f9e | 2015-09-17 12:18:45 +0000 | [diff] [blame] | 55 | @Override |
Googler | 34f7058 | 2019-11-25 12:27:34 -0800 | [diff] [blame] | 56 | public void repr(Printer printer) { |
vladmos | 4690793 | 2017-06-30 14:01:45 +0200 | [diff] [blame] | 57 | printer.append("FilesetEntry(srcdir = "); |
jhorvitz | 9302ebd | 2021-06-28 11:09:06 -0700 | [diff] [blame] | 58 | printer.repr(srcLabel.toString()); |
vladmos | 4690793 | 2017-06-30 14:01:45 +0200 | [diff] [blame] | 59 | printer.append(", files = "); |
jhorvitz | 9302ebd | 2021-06-28 11:09:06 -0700 | [diff] [blame] | 60 | printer.repr(files == null ? ImmutableList.of() : Lists.transform(files, Label::toString)); |
vladmos | 4690793 | 2017-06-30 14:01:45 +0200 | [diff] [blame] | 61 | printer.append(", excludes = "); |
jhorvitz | 9302ebd | 2021-06-28 11:09:06 -0700 | [diff] [blame] | 62 | printer.repr(excludes == null ? ImmutableList.of() : excludes.asList()); |
vladmos | 4690793 | 2017-06-30 14:01:45 +0200 | [diff] [blame] | 63 | printer.append(", destdir = "); |
jhorvitz | 9302ebd | 2021-06-28 11:09:06 -0700 | [diff] [blame] | 64 | printer.repr(destDir.getPathString()); |
vladmos | 4690793 | 2017-06-30 14:01:45 +0200 | [diff] [blame] | 65 | printer.append(", strip_prefix = "); |
jhorvitz | 9302ebd | 2021-06-28 11:09:06 -0700 | [diff] [blame] | 66 | printer.repr(stripPrefix); |
vladmos | 4690793 | 2017-06-30 14:01:45 +0200 | [diff] [blame] | 67 | printer.append(", symlinks = "); |
jhorvitz | 9302ebd | 2021-06-28 11:09:06 -0700 | [diff] [blame] | 68 | printer.repr(symlinkBehavior.toString()); |
vladmos | 4690793 | 2017-06-30 14:01:45 +0200 | [diff] [blame] | 69 | printer.append(")"); |
Lukacs Berki | de27f9e | 2015-09-17 12:18:45 +0000 | [diff] [blame] | 70 | } |
| 71 | |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 72 | /** SymlinkBehavior decides what to do when a source file of a FilesetEntry is a symlink. */ |
Laszlo Csomor | b136315c | 2016-01-19 14:03:29 +0000 | [diff] [blame] | 73 | @Immutable |
| 74 | @ThreadSafe |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 75 | public enum SymlinkBehavior { |
| 76 | /** Just copies the symlink as-is. May result in dangling links. */ |
| 77 | COPY, |
| 78 | /** Follow the link and make the destination point to the absolute path of the final target. */ |
| 79 | DEREFERENCE; |
| 80 | |
| 81 | public static SymlinkBehavior parse(String value) throws IllegalArgumentException { |
Jingwen Chen | 91b867f | 2018-05-11 08:56:06 -0700 | [diff] [blame] | 82 | return valueOf(value.toUpperCase(Locale.ENGLISH)); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | @Override |
| 86 | public String toString() { |
| 87 | return super.toString().toLowerCase(); |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | private final Label srcLabel; |
| 92 | @Nullable private final ImmutableList<Label> files; |
| 93 | @Nullable private final ImmutableSet<String> excludes; |
| 94 | private final PathFragment destDir; |
| 95 | private final SymlinkBehavior symlinkBehavior; |
| 96 | private final String stripPrefix; |
| 97 | |
| 98 | /** |
| 99 | * Constructs a FilesetEntry with the given values. |
| 100 | * |
| 101 | * @param srcLabel the label of the source directory. Must be non-null. |
| 102 | * @param files The explicit files to include. May be null. |
| 103 | * @param excludes The files to exclude. Man be null. May only be non-null if files is null. |
| 104 | * @param destDir The target-relative output directory. |
| 105 | * @param symlinkBehavior how to treat symlinks on the input. See |
| 106 | * {@link FilesetEntry.SymlinkBehavior}. |
| 107 | * @param stripPrefix the prefix to strip from the package-relative path. If ".", keep only the |
| 108 | * basename. |
| 109 | */ |
Laszlo Csomor | b136315c | 2016-01-19 14:03:29 +0000 | [diff] [blame] | 110 | public FilesetEntry( |
| 111 | Label srcLabel, |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 112 | @Nullable List<Label> files, |
Laszlo Csomor | b136315c | 2016-01-19 14:03:29 +0000 | [diff] [blame] | 113 | @Nullable Collection<String> excludes, |
| 114 | @Nullable String destDir, |
| 115 | @Nullable SymlinkBehavior symlinkBehavior, |
| 116 | @Nullable String stripPrefix) { |
| 117 | this.srcLabel = Preconditions.checkNotNull(srcLabel); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 118 | this.files = files == null ? null : ImmutableList.copyOf(files); |
| 119 | this.excludes = (excludes == null || excludes.isEmpty()) ? null : ImmutableSet.copyOf(excludes); |
nharmata | b4060b6 | 2017-04-04 17:11:39 +0000 | [diff] [blame] | 120 | this.destDir = PathFragment.create((destDir == null) ? "" : destDir); |
Laszlo Csomor | b136315c | 2016-01-19 14:03:29 +0000 | [diff] [blame] | 121 | this.symlinkBehavior = symlinkBehavior == null ? DEFAULT_SYMLINK_BEHAVIOR : symlinkBehavior; |
| 122 | this.stripPrefix = stripPrefix == null ? DEFAULT_STRIP_PREFIX : stripPrefix; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | /** |
| 126 | * @return the source label. |
| 127 | */ |
| 128 | public Label getSrcLabel() { |
| 129 | return srcLabel; |
| 130 | } |
| 131 | |
ilist | 5645424 | 2021-09-24 01:08:05 -0700 | [diff] [blame] | 132 | /** Returns the source label. */ |
| 133 | @StarlarkMethod(name = "srcdir", documented = false, useStarlarkThread = true) |
| 134 | public Label getSrcStarlark(StarlarkThread thread) throws EvalException { |
| 135 | BuiltinRestriction.throwIfNotBuiltinUsage(thread); |
| 136 | return getSrcLabel(); |
| 137 | } |
| 138 | |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 139 | /** |
| 140 | * @return the destDir. Non null. |
| 141 | */ |
| 142 | public PathFragment getDestDir() { |
| 143 | return destDir; |
| 144 | } |
| 145 | |
ilist | 5645424 | 2021-09-24 01:08:05 -0700 | [diff] [blame] | 146 | @StarlarkMethod(name = "destdir", documented = false, useStarlarkThread = true) |
| 147 | public String getStarlarkDestDir(StarlarkThread thread) throws EvalException { |
| 148 | BuiltinRestriction.throwIfNotBuiltinUsage(thread); |
| 149 | return getDestDir().toString(); |
| 150 | } |
| 151 | |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 152 | /** |
| 153 | * @return how symlinks should be handled. |
| 154 | */ |
| 155 | public SymlinkBehavior getSymlinkBehavior() { |
| 156 | return symlinkBehavior; |
| 157 | } |
| 158 | |
ilist | 5645424 | 2021-09-24 01:08:05 -0700 | [diff] [blame] | 159 | @StarlarkMethod(name = "symlinks", documented = false, useStarlarkThread = true) |
| 160 | public String getSymlinkBehaviorStarlark(StarlarkThread thread) throws EvalException { |
| 161 | BuiltinRestriction.throwIfNotBuiltinUsage(thread); |
| 162 | return getSymlinkBehavior().toString(); |
| 163 | } |
| 164 | |
| 165 | /** Returns an immutable set of excludes. Null if none specified. */ |
| 166 | @StarlarkMethod( |
| 167 | name = "excludes", |
| 168 | documented = false, |
| 169 | useStarlarkThread = true, |
| 170 | allowReturnNones = true) |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 171 | @Nullable |
ilist | 5645424 | 2021-09-24 01:08:05 -0700 | [diff] [blame] | 172 | public ImmutableList<String> getExcludesStarlark(StarlarkThread thread) throws EvalException { |
| 173 | BuiltinRestriction.throwIfNotBuiltinUsage(thread); |
| 174 | return getExcludes() == null ? null : getExcludes().asList(); |
| 175 | } |
| 176 | |
| 177 | /** Returns an immutable set of excludes. Null if none specified. */ |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 178 | public ImmutableSet<String> getExcludes() { |
| 179 | return excludes; |
| 180 | } |
| 181 | |
ilist | 5645424 | 2021-09-24 01:08:05 -0700 | [diff] [blame] | 182 | /** Returns an immutable list of file labels. Null if none specified. */ |
| 183 | @StarlarkMethod( |
| 184 | name = "files", |
| 185 | documented = false, |
| 186 | useStarlarkThread = true, |
| 187 | allowReturnNones = true) |
| 188 | @Nullable |
| 189 | public ImmutableList<Label> getFilesStarlark(StarlarkThread thread) throws EvalException { |
| 190 | BuiltinRestriction.throwIfNotBuiltinUsage(thread); |
| 191 | return getFiles(); |
| 192 | } |
| 193 | |
| 194 | /** Returns an immutable list of file labels. Null if none specified. */ |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 195 | @Nullable |
| 196 | public ImmutableList<Label> getFiles() { |
| 197 | return files; |
| 198 | } |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 199 | /** |
| 200 | * @return true if this Fileset should get files from the source directory. |
| 201 | */ |
| 202 | public boolean isSourceFileset() { |
| 203 | return "BUILD".equals(srcLabel.getName()); |
| 204 | } |
| 205 | |
| 206 | /** |
| 207 | * @return all prerequisite labels in the FilesetEntry. |
| 208 | */ |
| 209 | public Collection<Label> getLabels() { |
| 210 | Set<Label> labels = new LinkedHashSet<>(); |
| 211 | if (files != null) { |
| 212 | labels.addAll(files); |
| 213 | } else { |
| 214 | labels.add(srcLabel); |
| 215 | } |
| 216 | return labels; |
| 217 | } |
| 218 | |
ilist | 5645424 | 2021-09-24 01:08:05 -0700 | [diff] [blame] | 219 | /** Returns the prefix that should be stripped from package-relative path names. */ |
| 220 | @StarlarkMethod(name = "strip_prefix", documented = false, useStarlarkThread = true) |
| 221 | public String getStripPrefixStarlark(StarlarkThread thread) throws EvalException { |
| 222 | BuiltinRestriction.throwIfNotBuiltinUsage(thread); |
| 223 | return getStripPrefix(); |
| 224 | } |
| 225 | |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 226 | public String getStripPrefix() { |
| 227 | return stripPrefix; |
| 228 | } |
| 229 | |
| 230 | /** |
| 231 | * @return null if the entry is valid, and a human-readable error message otherwise. |
| 232 | */ |
| 233 | @Nullable |
| 234 | public String validate() { |
| 235 | if (excludes != null && files != null) { |
| 236 | return "Cannot specify both 'files' and 'excludes' in a FilesetEntry"; |
| 237 | } else if (files != null && !isSourceFileset()) { |
| 238 | return "Cannot specify files with Fileset label '" + srcLabel + "'"; |
| 239 | } else if (destDir.isAbsolute()) { |
| 240 | return "Cannot specify absolute destdir '" + destDir + "'"; |
Laszlo Csomor | b136315c | 2016-01-19 14:03:29 +0000 | [diff] [blame] | 241 | } else if (!stripPrefix.equals(DEFAULT_STRIP_PREFIX) && files == null) { |
| 242 | return "If the strip prefix is not \"" + DEFAULT_STRIP_PREFIX + "\", files must be specified"; |
Laszlo Csomor | 6979c8e | 2016-02-04 10:43:11 +0000 | [diff] [blame] | 243 | } else if (stripPrefix.startsWith("/")) { |
| 244 | return "Cannot specify absolute strip prefix; perhaps you need to use \"" |
| 245 | + STRIP_PREFIX_WORKSPACE + "\""; |
nharmata | b4060b6 | 2017-04-04 17:11:39 +0000 | [diff] [blame] | 246 | } else if (PathFragment.create(stripPrefix).containsUplevelReferences()) { |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 247 | return "Strip prefix must not contain uplevel references"; |
Laszlo Csomor | 6979c8e | 2016-02-04 10:43:11 +0000 | [diff] [blame] | 248 | } else if (stripPrefix.startsWith("%") && !stripPrefix.startsWith(STRIP_PREFIX_WORKSPACE)) { |
| 249 | return "If the strip_prefix starts with \"%\" then it must start with \"" |
| 250 | + STRIP_PREFIX_WORKSPACE + "\""; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 251 | } else { |
| 252 | return null; |
| 253 | } |
| 254 | } |
| 255 | |
| 256 | @Override |
| 257 | public String toString() { |
Laszlo Csomor | b136315c | 2016-01-19 14:03:29 +0000 | [diff] [blame] | 258 | return String.format( |
| 259 | "FilesetEntry(srcdir=%s, destdir=%s, strip_prefix=%s, symlinks=%s, " |
| 260 | + "%d file(s) and %d excluded)", |
| 261 | srcLabel, |
| 262 | destDir, |
| 263 | stripPrefix, |
| 264 | symlinkBehavior, |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 265 | files != null ? files.size() : 0, |
| 266 | excludes != null ? excludes.size() : 0); |
| 267 | } |
Michajlo Matijkiw | 586f80f | 2016-06-09 18:51:18 +0000 | [diff] [blame] | 268 | |
| 269 | @Override |
| 270 | public int hashCode() { |
| 271 | return Objects.hashCode(srcLabel, files, excludes, destDir, symlinkBehavior, stripPrefix); |
| 272 | } |
| 273 | |
| 274 | @Override |
| 275 | public boolean equals(Object other) { |
| 276 | if (this == other) { |
| 277 | return true; |
| 278 | } |
| 279 | |
| 280 | if (!(other instanceof FilesetEntry)) { |
| 281 | return false; |
| 282 | } |
| 283 | |
| 284 | FilesetEntry that = (FilesetEntry) other; |
| 285 | return Objects.equal(srcLabel, that.srcLabel) |
| 286 | && Objects.equal(files, that.files) |
| 287 | && Objects.equal(excludes, that.excludes) |
| 288 | && Objects.equal(destDir, that.destDir) |
| 289 | && Objects.equal(symlinkBehavior, that.symlinkBehavior) |
| 290 | && Objects.equal(stripPrefix, that.stripPrefix); |
| 291 | } |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 292 | } |