cparsons | 90f70b6 | 2018-05-01 12:45:02 -0700 | [diff] [blame] | 1 | // Copyright 2018 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 | |
| 15 | package com.google.devtools.build.lib.analysis; |
| 16 | |
| 17 | import com.google.common.annotations.VisibleForTesting; |
| 18 | import com.google.common.base.Function; |
tomlu | a155b53 | 2017-11-08 20:12:47 +0100 | [diff] [blame] | 19 | import com.google.common.base.Preconditions; |
Michajlo Matijkiw | ac879b9 | 2015-04-15 21:28:33 +0000 | [diff] [blame] | 20 | import com.google.common.collect.ImmutableList; |
Benjamin Peterson | ba2f391 | 2018-06-18 08:54:36 -0700 | [diff] [blame] | 21 | import com.google.common.collect.ImmutableSet; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 22 | import com.google.common.collect.Iterables; |
Benjamin Peterson | ba2f391 | 2018-06-18 08:54:36 -0700 | [diff] [blame] | 23 | import com.google.common.collect.Streams; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 24 | import com.google.devtools.build.lib.actions.Artifact; |
felly | 56fd4fe | 2018-09-05 10:54:54 -0700 | [diff] [blame] | 25 | import com.google.devtools.build.lib.actions.ArtifactPathResolver; |
gregce | 593f7f9 | 2017-09-19 02:02:21 +0200 | [diff] [blame] | 26 | import com.google.devtools.build.lib.analysis.configuredtargets.RuleConfiguredTarget.Mode; |
dannark | be3cefc | 2018-12-13 11:52:45 -0800 | [diff] [blame] | 27 | import com.google.devtools.build.lib.cmdline.LabelConstants; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 28 | import com.google.devtools.build.lib.collect.nestedset.NestedSet; |
| 29 | import com.google.devtools.build.lib.collect.nestedset.NestedSetBuilder; |
| 30 | import com.google.devtools.build.lib.collect.nestedset.Order; |
Brian Silverman | 13891f6 | 2015-06-22 16:46:40 +0000 | [diff] [blame] | 31 | import com.google.devtools.build.lib.concurrent.ThreadSafety.Immutable; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 32 | import com.google.devtools.build.lib.events.Event; |
| 33 | import com.google.devtools.build.lib.events.EventHandler; |
Googler | 3ed13c8 | 2016-03-07 21:53:29 +0000 | [diff] [blame] | 34 | import com.google.devtools.build.lib.events.EventKind; |
Lukacs Berki | ffa73ad | 2015-09-18 11:40:12 +0000 | [diff] [blame] | 35 | import com.google.devtools.build.lib.packages.BuildType; |
cpeyser | 27458be | 2018-02-14 12:49:17 -0800 | [diff] [blame] | 36 | import com.google.devtools.build.lib.skyframe.serialization.autocodec.AutoCodec; |
cpeyser | 27458be | 2018-02-14 12:49:17 -0800 | [diff] [blame] | 37 | import com.google.devtools.build.lib.skyframe.serialization.autocodec.AutoCodec.VisibleForSerialization; |
cparsons | 90f70b6 | 2018-05-01 12:45:02 -0700 | [diff] [blame] | 38 | import com.google.devtools.build.lib.skylarkbuildapi.RunfilesApi; |
Benjamin Peterson | eadc65a | 2018-11-07 09:22:40 -0800 | [diff] [blame] | 39 | import com.google.devtools.build.lib.skylarkbuildapi.SymlinkEntryApi; |
Googler | d21a0d1 | 2019-11-21 13:52:30 -0800 | [diff] [blame] | 40 | import com.google.devtools.build.lib.syntax.Depset; |
adonovan | f5262c5 | 2020-04-02 09:25:14 -0700 | [diff] [blame^] | 41 | import com.google.devtools.build.lib.syntax.Location; |
Googler | 34f7058 | 2019-11-25 12:27:34 -0800 | [diff] [blame] | 42 | import com.google.devtools.build.lib.syntax.Printer; |
Googler | fb631cf | 2019-11-21 11:19:32 -0800 | [diff] [blame] | 43 | import com.google.devtools.build.lib.syntax.SkylarkType; |
felly | c31c8c9 | 2018-08-29 15:07:59 -0700 | [diff] [blame] | 44 | import com.google.devtools.build.lib.util.Fingerprint; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 45 | import com.google.devtools.build.lib.vfs.PathFragment; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 46 | import java.io.BufferedReader; |
| 47 | import java.io.IOException; |
| 48 | import java.io.InputStreamReader; |
| 49 | import java.util.Collections; |
| 50 | import java.util.HashMap; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 51 | import java.util.LinkedHashMap; |
| 52 | import java.util.Map; |
Googler | 3ed13c8 | 2016-03-07 21:53:29 +0000 | [diff] [blame] | 53 | import java.util.Objects; |
Michajlo Matijkiw | ac879b9 | 2015-04-15 21:28:33 +0000 | [diff] [blame] | 54 | import java.util.Set; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 55 | import javax.annotation.Nullable; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 56 | |
| 57 | /** |
| 58 | * An object that encapsulates runfiles. Conceptually, the runfiles are a map of paths to files, |
| 59 | * forming a symlink tree. |
| 60 | * |
| 61 | * <p>In order to reduce memory consumption, this map is not explicitly stored here, but instead as |
| 62 | * a combination of four parts: artifacts placed at their root-relative paths, source tree symlinks, |
| 63 | * root symlinks (outside of the source tree), and artifacts included as parts of "pruning |
| 64 | * manifests" (see {@link PruningManifest}). |
| 65 | */ |
| 66 | @Immutable |
cpeyser | 27458be | 2018-02-14 12:49:17 -0800 | [diff] [blame] | 67 | @AutoCodec |
cparsons | 90f70b6 | 2018-05-01 12:45:02 -0700 | [diff] [blame] | 68 | public final class Runfiles implements RunfilesApi { |
Lukacs Berki | 5ab30d6 | 2015-03-11 22:06:04 +0000 | [diff] [blame] | 69 | private static final Function<SymlinkEntry, Artifact> TO_ARTIFACT = |
| 70 | new Function<SymlinkEntry, Artifact>() { |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 71 | @Override |
Lukacs Berki | 5ab30d6 | 2015-03-11 22:06:04 +0000 | [diff] [blame] | 72 | public Artifact apply(SymlinkEntry input) { |
| 73 | return input.getArtifact(); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 74 | } |
| 75 | }; |
| 76 | |
shahan | 20f35b4 | 2018-02-28 15:57:33 -0800 | [diff] [blame] | 77 | private static class DummyEmptyFilesSupplier implements EmptyFilesSupplier { |
| 78 | private DummyEmptyFilesSupplier() {} |
cpeyser | 27458be | 2018-02-14 12:49:17 -0800 | [diff] [blame] | 79 | |
| 80 | @Override |
| 81 | public Iterable<PathFragment> getExtraPaths(Set<PathFragment> manifestPaths) { |
| 82 | return ImmutableList.of(); |
| 83 | } |
| 84 | } |
| 85 | |
shahan | 20f35b4 | 2018-02-28 15:57:33 -0800 | [diff] [blame] | 86 | @AutoCodec @AutoCodec.VisibleForSerialization |
| 87 | static final EmptyFilesSupplier DUMMY_EMPTY_FILES_SUPPLIER = new DummyEmptyFilesSupplier(); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 88 | |
Lukacs Berki | 5ab30d6 | 2015-03-11 22:06:04 +0000 | [diff] [blame] | 89 | /** |
| 90 | * An entry in the runfiles map. |
Googler | c85af31 | 2016-03-10 17:55:17 +0000 | [diff] [blame] | 91 | * |
| 92 | * <p>build-runfiles.cc enforces the following constraints: The PathFragment must not be an |
Eric Dobson | 20bd274 | 2016-11-25 21:59:09 +0000 | [diff] [blame] | 93 | * absolute path, nor contain "..". Overlapping runfiles links are also refused. This is the case |
Googler | c85af31 | 2016-03-10 17:55:17 +0000 | [diff] [blame] | 94 | * where you ask to create a link to "foo" and also "foo/bar.txt". I.e. you're asking it to make |
| 95 | * "foo" both a file (symlink) and a directory. |
| 96 | * |
| 97 | * <p>Links to directories are heavily discouraged. |
Lukacs Berki | 5ab30d6 | 2015-03-11 22:06:04 +0000 | [diff] [blame] | 98 | */ |
| 99 | // |
| 100 | // O intrepid fixer or bugs and implementor of features, dare not to add a .equals() method |
| 101 | // to this class, lest you condemn yourself, or a fellow other developer to spending two |
| 102 | // delightful hours in a fancy hotel on a Chromebook that is utterly unsuitable for Java |
| 103 | // development to figure out what went wrong, just like I just did. |
| 104 | // |
| 105 | // The semantics of the symlinks nested set dictates that later entries overwrite earlier |
| 106 | // ones. However, the semantics of nested sets dictate that if there are duplicate entries, they |
| 107 | // are only returned once in the iterator. |
| 108 | // |
| 109 | // These two things, innocent when taken alone, result in the effect that when there are three |
| 110 | // entries for the same path, the first one and the last one the same, and the middle one |
| 111 | // different, the *middle* one will take effect: the middle one overrides the first one, and the |
| 112 | // first one prevents the last one from appearing on the iterator. |
| 113 | // |
| 114 | // The lack of a .equals() method prevents this by making the first entry in the above case not |
| 115 | // equals to the third one if they are not the same instance (which they almost never are) |
| 116 | // |
| 117 | // Goodnight, prince(ss)?, and sweet dreams. |
cpeyser | 27458be | 2018-02-14 12:49:17 -0800 | [diff] [blame] | 118 | @AutoCodec |
| 119 | @VisibleForSerialization |
Benjamin Peterson | eadc65a | 2018-11-07 09:22:40 -0800 | [diff] [blame] | 120 | static final class SymlinkEntry implements SymlinkEntryApi { |
Googler | 40c1123 | 2019-11-26 09:35:55 -0800 | [diff] [blame] | 121 | |
| 122 | static final SkylarkType TYPE = SkylarkType.of(SymlinkEntry.class); |
| 123 | |
Lukacs Berki | 5ab30d6 | 2015-03-11 22:06:04 +0000 | [diff] [blame] | 124 | private final PathFragment path; |
| 125 | private final Artifact artifact; |
| 126 | |
cpeyser | 27458be | 2018-02-14 12:49:17 -0800 | [diff] [blame] | 127 | @VisibleForSerialization |
| 128 | SymlinkEntry(PathFragment path, Artifact artifact) { |
Ulf Adams | 8594de8 | 2017-03-20 16:30:11 +0000 | [diff] [blame] | 129 | this.path = Preconditions.checkNotNull(path); |
| 130 | this.artifact = Preconditions.checkNotNull(artifact); |
Lukacs Berki | 5ab30d6 | 2015-03-11 22:06:04 +0000 | [diff] [blame] | 131 | } |
| 132 | |
Benjamin Peterson | eadc65a | 2018-11-07 09:22:40 -0800 | [diff] [blame] | 133 | @Override |
| 134 | public String getPathString() { |
| 135 | return getPath().getPathString(); |
| 136 | } |
| 137 | |
Lukacs Berki | 5ab30d6 | 2015-03-11 22:06:04 +0000 | [diff] [blame] | 138 | public PathFragment getPath() { |
| 139 | return path; |
| 140 | } |
| 141 | |
Benjamin Peterson | eadc65a | 2018-11-07 09:22:40 -0800 | [diff] [blame] | 142 | @Override |
Lukacs Berki | 5ab30d6 | 2015-03-11 22:06:04 +0000 | [diff] [blame] | 143 | public Artifact getArtifact() { |
| 144 | return artifact; |
| 145 | } |
Eric Dobson | 20bd274 | 2016-11-25 21:59:09 +0000 | [diff] [blame] | 146 | |
Ulf Adams | 8594de8 | 2017-03-20 16:30:11 +0000 | [diff] [blame] | 147 | @Override |
Eric Dobson | 20bd274 | 2016-11-25 21:59:09 +0000 | [diff] [blame] | 148 | public boolean isImmutable() { |
| 149 | return true; |
| 150 | } |
| 151 | |
Ulf Adams | 8594de8 | 2017-03-20 16:30:11 +0000 | [diff] [blame] | 152 | @Override |
Googler | 34f7058 | 2019-11-25 12:27:34 -0800 | [diff] [blame] | 153 | public void repr(Printer printer) { |
vladmos | 4690793 | 2017-06-30 14:01:45 +0200 | [diff] [blame] | 154 | printer.append("SymlinkEntry(path = "); |
Benjamin Peterson | eadc65a | 2018-11-07 09:22:40 -0800 | [diff] [blame] | 155 | printer.repr(getPathString()); |
| 156 | printer.append(", target_file = "); |
vladmos | 4690793 | 2017-06-30 14:01:45 +0200 | [diff] [blame] | 157 | getArtifact().repr(printer); |
| 158 | printer.append(")"); |
Eric Dobson | 20bd274 | 2016-11-25 21:59:09 +0000 | [diff] [blame] | 159 | } |
Lukacs Berki | 5ab30d6 | 2015-03-11 22:06:04 +0000 | [diff] [blame] | 160 | } |
| 161 | |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 162 | // It is important to declare this *after* the DUMMY_SYMLINK_EXPANDER to avoid NPEs |
| 163 | public static final Runfiles EMPTY = new Builder().build(); |
| 164 | |
Kristina Chodorow | b579b94 | 2015-03-02 15:51:58 +0000 | [diff] [blame] | 165 | /** |
| 166 | * The directory to put all runfiles under. |
| 167 | * |
| 168 | * <p>Using "foo" will put runfiles under <target>.runfiles/foo.</p> |
Googler | 3ed13c8 | 2016-03-07 21:53:29 +0000 | [diff] [blame] | 169 | * |
| 170 | * <p>This is either set to the workspace name, or is empty. |
Kristina Chodorow | b579b94 | 2015-03-02 15:51:58 +0000 | [diff] [blame] | 171 | */ |
Kristina Chodorow | 7ef0251 | 2016-04-22 16:14:12 +0000 | [diff] [blame] | 172 | private final PathFragment suffix; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 173 | |
| 174 | /** |
| 175 | * The artifacts that should *always* be present in the runfiles directory. These are |
| 176 | * differentiated from the artifacts that may or may not be included by a pruning manifest |
| 177 | * (see {@link PruningManifest} below). |
| 178 | * |
| 179 | * <p>This collection may not include any middlemen. These artifacts will be placed at a location |
| 180 | * that corresponds to the root-relative path of each artifact. It's possible for several |
| 181 | * artifacts to have the same root-relative path, in which case the last one will win. |
| 182 | */ |
| 183 | private final NestedSet<Artifact> unconditionalArtifacts; |
| 184 | |
| 185 | /** |
| 186 | * A map of symlinks that should be present in the runfiles directory. In general, the symlink can |
| 187 | * be determined from the artifact by using the root-relative path, so this should only be used |
| 188 | * for cases where that isn't possible. |
| 189 | * |
| 190 | * <p>This may include runfiles symlinks from the root of the runfiles tree. |
| 191 | */ |
Lukacs Berki | 5ab30d6 | 2015-03-11 22:06:04 +0000 | [diff] [blame] | 192 | private final NestedSet<SymlinkEntry> symlinks; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 193 | |
| 194 | /** |
| 195 | * A map of symlinks that should be present above the runfiles directory. These are useful for |
| 196 | * certain rule types like AppEngine apps which have root level config files outside of the |
| 197 | * regular source tree. |
| 198 | */ |
Lukacs Berki | 5ab30d6 | 2015-03-11 22:06:04 +0000 | [diff] [blame] | 199 | private final NestedSet<SymlinkEntry> rootSymlinks; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 200 | |
| 201 | /** |
Benjamin Peterson | dbbcbcb | 2017-10-26 11:38:13 +0200 | [diff] [blame] | 202 | * A set of middlemen artifacts. {@link RuleConfiguredTargetBuilder} adds these to the {@link |
| 203 | * FilesToRunProvider} of binaries that include this runfiles tree in their runfiles. |
| 204 | */ |
| 205 | private final NestedSet<Artifact> extraMiddlemen; |
| 206 | |
| 207 | /** |
Michajlo Matijkiw | ac879b9 | 2015-04-15 21:28:33 +0000 | [diff] [blame] | 208 | * Interface used for adding empty files to the runfiles at the last minute. Mainly to support |
| 209 | * python-related rules adding __init__.py files. |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 210 | */ |
Michajlo Matijkiw | ac879b9 | 2015-04-15 21:28:33 +0000 | [diff] [blame] | 211 | public interface EmptyFilesSupplier { |
| 212 | /** Calculate additional empty files to add based on the existing manifest paths. */ |
| 213 | Iterable<PathFragment> getExtraPaths(Set<PathFragment> manifestPaths); |
| 214 | } |
| 215 | |
| 216 | /** Generates extra (empty file) inputs. */ |
| 217 | private final EmptyFilesSupplier emptyFilesSupplier; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 218 | |
| 219 | /** |
Googler | 3ed13c8 | 2016-03-07 21:53:29 +0000 | [diff] [blame] | 220 | * Behavior upon finding a conflict between two runfile entries. A conflict means that two |
| 221 | * different artifacts have the same runfiles path specified. For example, adding artifact |
Googler | c85af31 | 2016-03-10 17:55:17 +0000 | [diff] [blame] | 222 | * "a.foo" at path "bar" when there is already an artifact "b.foo" at path "bar". The policies |
| 223 | * are ordered from least strict to most strict. |
Googler | 3ed13c8 | 2016-03-07 21:53:29 +0000 | [diff] [blame] | 224 | * |
| 225 | * <p>Note that conflicts are found relatively late, when the manifest file is created, not when |
| 226 | * the symlinks are added to runfiles. |
| 227 | * |
| 228 | * <p>If no EventHandler is available, all values are treated as IGNORE. |
| 229 | */ |
Kristina Chodorow | 7ef0251 | 2016-04-22 16:14:12 +0000 | [diff] [blame] | 230 | public enum ConflictPolicy { |
Googler | 3ed13c8 | 2016-03-07 21:53:29 +0000 | [diff] [blame] | 231 | IGNORE, |
| 232 | WARN, |
| 233 | ERROR, |
| 234 | } |
| 235 | |
| 236 | /** Policy for this Runfiles tree */ |
| 237 | private ConflictPolicy conflictPolicy = ConflictPolicy.IGNORE; |
| 238 | |
| 239 | /** |
cpeyser | 27458be | 2018-02-14 12:49:17 -0800 | [diff] [blame] | 240 | * Defines a set of artifacts that may or may not be included in the runfiles directory and a |
| 241 | * manifest file that makes that determination. These are applied on top of any artifacts |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 242 | * specified in {@link #unconditionalArtifacts}. |
| 243 | * |
cpeyser | 27458be | 2018-02-14 12:49:17 -0800 | [diff] [blame] | 244 | * <p>The incentive behind this is to enable execution-phase "pruning" of runfiles. Anything set |
| 245 | * in unconditionalArtifacts is hard-set in Blaze's analysis phase, and thus unchangeable in |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 246 | * response to execution phase results. This isn't always convenient. For example, say we have an |
| 247 | * action that consumes a set of "possible" runtime dependencies for a source file, parses that |
| 248 | * file for "import a.b.c" statements, and outputs a manifest of the actual dependencies that are |
| 249 | * referenced and thus really needed. This can reduce the size of the runfiles set, but we can't |
| 250 | * use this information until the manifest output is available. |
| 251 | * |
| 252 | * <p>Only artifacts present in the candidate set AND the manifest output make it into the |
| 253 | * runfiles tree. The candidate set requirement guarantees that analysis-time dependencies are a |
| 254 | * superset of the pruned dependencies, so undeclared inclusions (which can break build |
| 255 | * correctness) aren't possible. |
| 256 | */ |
cpeyser | 27458be | 2018-02-14 12:49:17 -0800 | [diff] [blame] | 257 | @AutoCodec |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 258 | public static class PruningManifest { |
| 259 | private final NestedSet<Artifact> candidateRunfiles; |
| 260 | private final Artifact manifestFile; |
| 261 | |
| 262 | /** |
| 263 | * Creates a new pruning manifest. |
| 264 | * |
| 265 | * @param candidateRunfiles set of possible artifacts that the manifest file may reference |
| 266 | * @param manifestFile the manifest file, expected to be a newline-separated list of |
| 267 | * source tree root-relative paths (i.e. "my/package/myfile.txt"). Anything that can't be |
| 268 | * resolved back to an entry in candidateRunfiles is ignored and will *not* make it into |
| 269 | * the runfiles tree. |
| 270 | */ |
| 271 | public PruningManifest(NestedSet<Artifact> candidateRunfiles, Artifact manifestFile) { |
| 272 | this.candidateRunfiles = candidateRunfiles; |
| 273 | this.manifestFile = manifestFile; |
| 274 | } |
| 275 | |
| 276 | public NestedSet<Artifact> getCandidateRunfiles() { |
| 277 | return candidateRunfiles; |
| 278 | } |
| 279 | |
| 280 | public Artifact getManifestFile() { |
| 281 | return manifestFile; |
| 282 | } |
| 283 | } |
| 284 | |
| 285 | /** |
| 286 | * The pruning manifests that should be applied to these runfiles. |
| 287 | */ |
| 288 | private final NestedSet<PruningManifest> pruningManifests; |
| 289 | |
Kristina Chodorow | 8dba2b2 | 2016-04-22 21:24:31 +0000 | [diff] [blame] | 290 | /** |
| 291 | * If external runfiles should be created under .runfiles/wsname/external/repo as well as |
| 292 | * .runfiles/repo. |
| 293 | */ |
| 294 | private final boolean legacyExternalRunfiles; |
| 295 | |
cpeyser | 27458be | 2018-02-14 12:49:17 -0800 | [diff] [blame] | 296 | @AutoCodec.Instantiator |
| 297 | @VisibleForSerialization |
| 298 | Runfiles( |
Kristina Chodorow | 8dba2b2 | 2016-04-22 21:24:31 +0000 | [diff] [blame] | 299 | PathFragment suffix, |
cpeyser | 27458be | 2018-02-14 12:49:17 -0800 | [diff] [blame] | 300 | NestedSet<Artifact> unconditionalArtifacts, |
Lukacs Berki | 5ab30d6 | 2015-03-11 22:06:04 +0000 | [diff] [blame] | 301 | NestedSet<SymlinkEntry> symlinks, |
| 302 | NestedSet<SymlinkEntry> rootSymlinks, |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 303 | NestedSet<PruningManifest> pruningManifests, |
Benjamin Peterson | dbbcbcb | 2017-10-26 11:38:13 +0200 | [diff] [blame] | 304 | NestedSet<Artifact> extraMiddlemen, |
Googler | c85af31 | 2016-03-10 17:55:17 +0000 | [diff] [blame] | 305 | EmptyFilesSupplier emptyFilesSupplier, |
Kristina Chodorow | 8dba2b2 | 2016-04-22 21:24:31 +0000 | [diff] [blame] | 306 | ConflictPolicy conflictPolicy, |
| 307 | boolean legacyExternalRunfiles) { |
Ulf Adams | 4226be2 | 2015-09-03 17:00:54 +0000 | [diff] [blame] | 308 | this.suffix = suffix; |
cpeyser | 27458be | 2018-02-14 12:49:17 -0800 | [diff] [blame] | 309 | this.unconditionalArtifacts = Preconditions.checkNotNull(unconditionalArtifacts); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 310 | this.symlinks = Preconditions.checkNotNull(symlinks); |
| 311 | this.rootSymlinks = Preconditions.checkNotNull(rootSymlinks); |
Benjamin Peterson | dbbcbcb | 2017-10-26 11:38:13 +0200 | [diff] [blame] | 312 | this.extraMiddlemen = Preconditions.checkNotNull(extraMiddlemen); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 313 | this.pruningManifests = Preconditions.checkNotNull(pruningManifests); |
Michajlo Matijkiw | ac879b9 | 2015-04-15 21:28:33 +0000 | [diff] [blame] | 314 | this.emptyFilesSupplier = Preconditions.checkNotNull(emptyFilesSupplier); |
Googler | c85af31 | 2016-03-10 17:55:17 +0000 | [diff] [blame] | 315 | this.conflictPolicy = conflictPolicy; |
Kristina Chodorow | 8dba2b2 | 2016-04-22 21:24:31 +0000 | [diff] [blame] | 316 | this.legacyExternalRunfiles = legacyExternalRunfiles; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 317 | } |
| 318 | |
| 319 | /** |
Kristina Chodorow | b579b94 | 2015-03-02 15:51:58 +0000 | [diff] [blame] | 320 | * Returns the runfiles' suffix. |
| 321 | */ |
Kristina Chodorow | 7ef0251 | 2016-04-22 16:14:12 +0000 | [diff] [blame] | 322 | public PathFragment getSuffix() { |
Kristina Chodorow | b579b94 | 2015-03-02 15:51:58 +0000 | [diff] [blame] | 323 | return suffix; |
| 324 | } |
| 325 | |
| 326 | /** |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 327 | * Returns the artifacts that are unconditionally included in the runfiles (as opposed to |
| 328 | * pruning manifest candidates, which may or may not be included). |
| 329 | */ |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 330 | public NestedSet<Artifact> getUnconditionalArtifacts() { |
| 331 | return unconditionalArtifacts; |
| 332 | } |
| 333 | |
Benjamin Peterson | dbbcbcb | 2017-10-26 11:38:13 +0200 | [diff] [blame] | 334 | public NestedSet<Artifact> getExtraMiddlemen() { |
| 335 | return extraMiddlemen; |
| 336 | } |
| 337 | |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 338 | /** |
John Cater | d792842 | 2017-01-03 20:06:59 +0000 | [diff] [blame] | 339 | * Returns the collection of runfiles as artifacts, including both unconditional artifacts and |
| 340 | * pruning manifest candidates. |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 341 | */ |
cparsons | 90f70b6 | 2018-05-01 12:45:02 -0700 | [diff] [blame] | 342 | @Override |
Googler | d21a0d1 | 2019-11-21 13:52:30 -0800 | [diff] [blame] | 343 | public Depset /*<Artifact>*/ getArtifactsForStarlark() { |
| 344 | return Depset.of(Artifact.TYPE, getArtifacts()); |
Googler | fb631cf | 2019-11-21 11:19:32 -0800 | [diff] [blame] | 345 | } |
| 346 | |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 347 | public NestedSet<Artifact> getArtifacts() { |
| 348 | NestedSetBuilder<Artifact> allArtifacts = NestedSetBuilder.stableOrder(); |
Benjamin Peterson | 899a3e7 | 2018-06-06 14:18:29 -0700 | [diff] [blame] | 349 | allArtifacts.addTransitive(unconditionalArtifacts); |
ulfjack | 7a05289 | 2019-12-17 01:21:54 -0800 | [diff] [blame] | 350 | for (PruningManifest manifest : getPruningManifests().toList()) { |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 351 | allArtifacts.addTransitive(manifest.getCandidateRunfiles()); |
| 352 | } |
| 353 | return allArtifacts.build(); |
| 354 | } |
| 355 | |
John Cater | d792842 | 2017-01-03 20:06:59 +0000 | [diff] [blame] | 356 | /** Returns the symlinks. */ |
cparsons | 90f70b6 | 2018-05-01 12:45:02 -0700 | [diff] [blame] | 357 | @Override |
Googler | d21a0d1 | 2019-11-21 13:52:30 -0800 | [diff] [blame] | 358 | public Depset /*<SymlinkEntry>*/ getSymlinksForStarlark() { |
Googler | 40c1123 | 2019-11-26 09:35:55 -0800 | [diff] [blame] | 359 | return Depset.of(SymlinkEntry.TYPE, symlinks); |
Googler | fb631cf | 2019-11-21 11:19:32 -0800 | [diff] [blame] | 360 | } |
| 361 | |
Lukacs Berki | 5ab30d6 | 2015-03-11 22:06:04 +0000 | [diff] [blame] | 362 | public NestedSet<SymlinkEntry> getSymlinks() { |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 363 | return symlinks; |
| 364 | } |
| 365 | |
cparsons | 90f70b6 | 2018-05-01 12:45:02 -0700 | [diff] [blame] | 366 | @Override |
Googler | d21a0d1 | 2019-11-21 13:52:30 -0800 | [diff] [blame] | 367 | public Depset /*<String>*/ getEmptyFilenamesForStarlark() { |
| 368 | return Depset.of(SkylarkType.STRING, getEmptyFilenames()); |
Googler | fb631cf | 2019-11-21 11:19:32 -0800 | [diff] [blame] | 369 | } |
| 370 | |
Brian Silverman | 4321b67 | 2016-04-15 12:16:04 +0000 | [diff] [blame] | 371 | public NestedSet<String> getEmptyFilenames() { |
Benjamin Peterson | ba2f391 | 2018-06-18 08:54:36 -0700 | [diff] [blame] | 372 | Set<PathFragment> manifestKeys = |
| 373 | Streams.concat( |
ulfjack | 7a05289 | 2019-12-17 01:21:54 -0800 | [diff] [blame] | 374 | symlinks.toList().stream().map(SymlinkEntry::getPath), |
| 375 | getArtifacts().toList().stream().map(Artifact::getRootRelativePath)) |
Benjamin Peterson | ba2f391 | 2018-06-18 08:54:36 -0700 | [diff] [blame] | 376 | .collect(ImmutableSet.toImmutableSet()); |
| 377 | Iterable<PathFragment> emptyKeys = emptyFilesSupplier.getExtraPaths(manifestKeys); |
| 378 | return NestedSetBuilder.<String>stableOrder() |
| 379 | .addAll( |
| 380 | Streams.stream(emptyKeys) |
| 381 | .map(PathFragment::toString) |
| 382 | .collect(ImmutableList.toImmutableList())) |
| 383 | .build(); |
Brian Silverman | 4321b67 | 2016-04-15 12:16:04 +0000 | [diff] [blame] | 384 | } |
| 385 | |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 386 | /** |
| 387 | * Returns the symlinks as a map from path fragment to artifact. |
Damien Martin-Guillerez | 9e4c78f | 2016-04-22 11:42:34 +0000 | [diff] [blame] | 388 | * |
| 389 | * @param checker If not null, check for conflicts using this checker. |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 390 | */ |
mstaib | 0e81f9a | 2018-09-17 14:30:28 -0700 | [diff] [blame] | 391 | public Map<PathFragment, Artifact> getSymlinksAsMap(@Nullable ConflictChecker checker) { |
Damien Martin-Guillerez | 9e4c78f | 2016-04-22 11:42:34 +0000 | [diff] [blame] | 392 | return entriesToMap(symlinks, checker); |
| 393 | } |
| 394 | |
| 395 | /** |
| 396 | * @param eventHandler Used for throwing an error if we have an obscuring runlink. |
| 397 | * May be null, in which case obscuring symlinks are silently discarded. |
| 398 | * @param location Location for reporter. Ignored if reporter is null. |
| 399 | * @param workingManifest Manifest to be checked for obscuring symlinks. |
| 400 | * @return map of source file names mapped to their location on disk. |
| 401 | */ |
| 402 | @VisibleForTesting |
| 403 | static Map<PathFragment, Artifact> filterListForObscuringSymlinks( |
| 404 | EventHandler eventHandler, Location location, Map<PathFragment, Artifact> workingManifest) { |
| 405 | Map<PathFragment, Artifact> newManifest = new HashMap<>(); |
| 406 | |
| 407 | outer: |
cushon | 2b73253 | 2020-01-16 15:40:13 -0800 | [diff] [blame] | 408 | for (Map.Entry<PathFragment, Artifact> entry : workingManifest.entrySet()) { |
Damien Martin-Guillerez | 9e4c78f | 2016-04-22 11:42:34 +0000 | [diff] [blame] | 409 | PathFragment source = entry.getKey(); |
| 410 | Artifact symlink = entry.getValue(); |
| 411 | // drop nested entries; warn if this changes anything |
| 412 | int n = source.segmentCount(); |
| 413 | for (int j = 1; j < n; ++j) { |
| 414 | PathFragment prefix = source.subFragment(0, n - j); |
| 415 | Artifact ancestor = workingManifest.get(prefix); |
| 416 | if (ancestor != null) { |
| 417 | // This is an obscuring symlink, so just drop it and move on if there's no reporter. |
| 418 | if (eventHandler == null) { |
| 419 | continue outer; |
| 420 | } |
| 421 | PathFragment suffix = source.subFragment(n - j, n); |
shahan | b3ba514 | 2018-04-06 19:17:12 -0700 | [diff] [blame] | 422 | PathFragment viaAncestor = ancestor.getExecPath().getRelative(suffix); |
| 423 | PathFragment expected = symlink.getExecPath(); |
Damien Martin-Guillerez | 9e4c78f | 2016-04-22 11:42:34 +0000 | [diff] [blame] | 424 | if (!viaAncestor.equals(expected)) { |
shahan | b3ba514 | 2018-04-06 19:17:12 -0700 | [diff] [blame] | 425 | eventHandler.handle( |
| 426 | Event.warn( |
| 427 | location, |
| 428 | "runfiles symlink " |
| 429 | + source |
| 430 | + " -> " |
| 431 | + expected |
| 432 | + " obscured by " |
| 433 | + prefix |
| 434 | + " -> " |
| 435 | + ancestor.getExecPath())); |
Damien Martin-Guillerez | 9e4c78f | 2016-04-22 11:42:34 +0000 | [diff] [blame] | 436 | } |
| 437 | continue outer; |
| 438 | } |
| 439 | } |
| 440 | newManifest.put(entry.getKey(), entry.getValue()); |
| 441 | } |
| 442 | return newManifest; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 443 | } |
| 444 | |
| 445 | /** |
Michajlo Matijkiw | 9c56957 | 2015-05-07 20:09:14 +0000 | [diff] [blame] | 446 | * Returns the symlinks as a map from PathFragment to Artifact. |
| 447 | * |
| 448 | * @param eventHandler Used for throwing an error if we have an obscuring runlink within the |
Yue Gan | 5e60e38 | 2017-03-20 10:58:11 +0000 | [diff] [blame] | 449 | * normal source tree entries, or runfile conflicts. May be null, in which case obscuring |
| 450 | * symlinks are silently discarded, and conflicts are overwritten. |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 451 | * @param location Location for eventHandler warnings. Ignored if eventHandler is null. |
felly | 56fd4fe | 2018-09-05 10:54:54 -0700 | [diff] [blame] | 452 | * @param resolver The {@link ArtifactPathResolver} to use for the pruning manifest, if present. |
Michajlo Matijkiw | 9c56957 | 2015-05-07 20:09:14 +0000 | [diff] [blame] | 453 | * @return Map<PathFragment, Artifact> path fragment to artifact, of normal source tree entries |
Yue Gan | 5e60e38 | 2017-03-20 10:58:11 +0000 | [diff] [blame] | 454 | * and elements that live outside the source tree. Null values represent empty input files. |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 455 | */ |
felly | 56fd4fe | 2018-09-05 10:54:54 -0700 | [diff] [blame] | 456 | public Map<PathFragment, Artifact> getRunfilesInputs(EventHandler eventHandler, Location location, |
| 457 | ArtifactPathResolver resolver) throws IOException { |
Damien Martin-Guillerez | 9e4c78f | 2016-04-22 11:42:34 +0000 | [diff] [blame] | 458 | ConflictChecker checker = new ConflictChecker(conflictPolicy, eventHandler, location); |
| 459 | Map<PathFragment, Artifact> manifest = getSymlinksAsMap(checker); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 460 | // Add unconditional artifacts (committed to inclusion on construction of runfiles). |
ulfjack | 7a05289 | 2019-12-17 01:21:54 -0800 | [diff] [blame] | 461 | for (Artifact artifact : getUnconditionalArtifacts().toList()) { |
Dmitry Lomov | e36a66c | 2017-02-17 14:48:48 +0000 | [diff] [blame] | 462 | checker.put(manifest, artifact.getRootRelativePath(), artifact); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 463 | } |
| 464 | |
| 465 | // Add conditional artifacts (only included if they appear in a pruning manifest). |
ulfjack | 7a05289 | 2019-12-17 01:21:54 -0800 | [diff] [blame] | 466 | for (Runfiles.PruningManifest pruningManifest : getPruningManifests().toList()) { |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 467 | // This map helps us convert from source tree root-relative paths back to artifacts. |
| 468 | Map<String, Artifact> allowedRunfiles = new HashMap<>(); |
ulfjack | 7a05289 | 2019-12-17 01:21:54 -0800 | [diff] [blame] | 469 | for (Artifact artifact : pruningManifest.getCandidateRunfiles().toList()) { |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 470 | allowedRunfiles.put(artifact.getRootRelativePath().getPathString(), artifact); |
| 471 | } |
Miguel Alcon Pinto | 53fb4d0 | 2015-11-04 17:36:02 +0000 | [diff] [blame] | 472 | try (BufferedReader reader = new BufferedReader( |
felly | 56fd4fe | 2018-09-05 10:54:54 -0700 | [diff] [blame] | 473 | new InputStreamReader(resolver.toPath(pruningManifest.getManifestFile()).getInputStream()))) { |
Miguel Alcon Pinto | 53fb4d0 | 2015-11-04 17:36:02 +0000 | [diff] [blame] | 474 | String line; |
| 475 | while ((line = reader.readLine()) != null) { |
| 476 | Artifact artifact = allowedRunfiles.get(line); |
| 477 | if (artifact != null) { |
Damien Martin-Guillerez | 9e4c78f | 2016-04-22 11:42:34 +0000 | [diff] [blame] | 478 | checker.put(manifest, artifact.getRootRelativePath(), artifact); |
Miguel Alcon Pinto | 53fb4d0 | 2015-11-04 17:36:02 +0000 | [diff] [blame] | 479 | } |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 480 | } |
| 481 | } |
| 482 | } |
Damien Martin-Guillerez | 9e4c78f | 2016-04-22 11:42:34 +0000 | [diff] [blame] | 483 | manifest = filterListForObscuringSymlinks(eventHandler, location, manifest); |
Michajlo Matijkiw | ac879b9 | 2015-04-15 21:28:33 +0000 | [diff] [blame] | 484 | |
| 485 | // TODO(bazel-team): Create /dev/null-like Artifact to avoid nulls? |
Damien Martin-Guillerez | 9e4c78f | 2016-04-22 11:42:34 +0000 | [diff] [blame] | 486 | for (PathFragment extraPath : emptyFilesSupplier.getExtraPaths(manifest.keySet())) { |
| 487 | checker.put(manifest, extraPath, null); |
| 488 | } |
| 489 | |
| 490 | // Copy manifest map to another manifest map, prepending the workspace name to every path. |
| 491 | // E.g. for workspace "myworkspace", the runfile entry "mylib.so"->"/path/to/mylib.so" becomes |
| 492 | // "myworkspace/mylib.so"->"/path/to/mylib.so". |
Kristina Chodorow | d912197 | 2016-04-25 17:45:55 +0000 | [diff] [blame] | 493 | ManifestBuilder builder = new ManifestBuilder(suffix, legacyExternalRunfiles); |
| 494 | builder.addUnderWorkspace(manifest, checker); |
Michajlo Matijkiw | 9c56957 | 2015-05-07 20:09:14 +0000 | [diff] [blame] | 495 | |
Googler | 3ed13c8 | 2016-03-07 21:53:29 +0000 | [diff] [blame] | 496 | // Finally add symlinks relative to the root of the runfiles tree, on top of everything else. |
| 497 | // This operation is always checked for conflicts, to match historical behavior. |
| 498 | if (conflictPolicy == ConflictPolicy.IGNORE) { |
Damien Martin-Guillerez | 9e4c78f | 2016-04-22 11:42:34 +0000 | [diff] [blame] | 499 | checker = new ConflictChecker(ConflictPolicy.WARN, eventHandler, location); |
Googler | 3ed13c8 | 2016-03-07 21:53:29 +0000 | [diff] [blame] | 500 | } |
Kristina Chodorow | d912197 | 2016-04-25 17:45:55 +0000 | [diff] [blame] | 501 | builder.add(getRootSymlinksAsMap(checker), checker); |
| 502 | return builder.build(); |
| 503 | } |
| 504 | |
| 505 | /** |
| 506 | * Helper class to handle munging the paths of external artifacts. |
| 507 | */ |
| 508 | @VisibleForTesting |
| 509 | static final class ManifestBuilder { |
| 510 | // Manifest of paths to artifacts. Path fragments are relative to the .runfiles directory. |
| 511 | private final Map<PathFragment, Artifact> manifest; |
| 512 | private final PathFragment workspaceName; |
| 513 | private final boolean legacyExternalRunfiles; |
| 514 | // Whether we saw the local workspace name in the runfiles. If legacyExternalRunfiles is true, |
| 515 | // then this is true, as anything under external/ will also have a runfile under the local |
| 516 | // workspace. |
| 517 | private boolean sawWorkspaceName; |
| 518 | |
Dmitry Lomov | e36a66c | 2017-02-17 14:48:48 +0000 | [diff] [blame] | 519 | public ManifestBuilder( |
Kristina Chodorow | d912197 | 2016-04-25 17:45:55 +0000 | [diff] [blame] | 520 | PathFragment workspaceName, boolean legacyExternalRunfiles) { |
| 521 | this.manifest = new HashMap<>(); |
| 522 | this.workspaceName = workspaceName; |
| 523 | this.legacyExternalRunfiles = legacyExternalRunfiles; |
| 524 | this.sawWorkspaceName = legacyExternalRunfiles; |
Damien Martin-Guillerez | 9e4c78f | 2016-04-22 11:42:34 +0000 | [diff] [blame] | 525 | } |
| 526 | |
mstaib | 0e81f9a | 2018-09-17 14:30:28 -0700 | [diff] [blame] | 527 | /** |
| 528 | * Adds a map under the workspaceName. |
| 529 | */ |
Dmitry Lomov | e36a66c | 2017-02-17 14:48:48 +0000 | [diff] [blame] | 530 | public void addUnderWorkspace( |
mstaib | 0e81f9a | 2018-09-17 14:30:28 -0700 | [diff] [blame] | 531 | Map<PathFragment, Artifact> inputManifest, ConflictChecker checker) { |
Kristina Chodorow | d912197 | 2016-04-25 17:45:55 +0000 | [diff] [blame] | 532 | for (Map.Entry<PathFragment, Artifact> entry : inputManifest.entrySet()) { |
| 533 | PathFragment path = entry.getKey(); |
Dmitry Lomov | e36a66c | 2017-02-17 14:48:48 +0000 | [diff] [blame] | 534 | if (isUnderWorkspace(path)) { |
Kristina Chodorow | d912197 | 2016-04-25 17:45:55 +0000 | [diff] [blame] | 535 | sawWorkspaceName = true; |
Dmitry Lomov | e36a66c | 2017-02-17 14:48:48 +0000 | [diff] [blame] | 536 | checker.put(manifest, workspaceName.getRelative(path), entry.getValue()); |
Laurent Le Brun | f3cf98f | 2016-06-17 13:36:24 +0000 | [diff] [blame] | 537 | } else { |
| 538 | if (legacyExternalRunfiles) { |
Dmitry Lomov | e36a66c | 2017-02-17 14:48:48 +0000 | [diff] [blame] | 539 | checker.put(manifest, workspaceName.getRelative(path), entry.getValue()); |
Laurent Le Brun | f3cf98f | 2016-06-17 13:36:24 +0000 | [diff] [blame] | 540 | } |
Dmitry Lomov | e36a66c | 2017-02-17 14:48:48 +0000 | [diff] [blame] | 541 | // Always add the non-legacy .runfiles/repo/whatever path. |
| 542 | checker.put(manifest, getExternalPath(path), entry.getValue()); |
Kristina Chodorow | d912197 | 2016-04-25 17:45:55 +0000 | [diff] [blame] | 543 | } |
| 544 | } |
| 545 | } |
| 546 | |
mstaib | 0e81f9a | 2018-09-17 14:30:28 -0700 | [diff] [blame] | 547 | /** |
| 548 | * Adds a map to the root directory. |
| 549 | */ |
| 550 | public void add(Map<PathFragment, Artifact> inputManifest, ConflictChecker checker) { |
Kristina Chodorow | d912197 | 2016-04-25 17:45:55 +0000 | [diff] [blame] | 551 | for (Map.Entry<PathFragment, Artifact> entry : inputManifest.entrySet()) { |
| 552 | checker.put(manifest, checkForWorkspace(entry.getKey()), entry.getValue()); |
| 553 | } |
| 554 | } |
| 555 | |
| 556 | /** |
| 557 | * Returns the manifest, adding the workspaceName directory if it is not already present. |
| 558 | */ |
| 559 | public Map<PathFragment, Artifact> build() { |
| 560 | if (!sawWorkspaceName) { |
| 561 | // If we haven't seen it and we have seen other files, add the workspace name directory. |
| 562 | // It might not be there if all of the runfiles are from other repos (and then running from |
| 563 | // x.runfiles/ws will fail, because ws won't exist). We can't tell Runfiles to create a |
| 564 | // directory, so instead this creates a hidden file inside the desired directory. |
| 565 | manifest.put(workspaceName.getRelative(".runfile"), null); |
| 566 | } |
| 567 | return manifest; |
| 568 | } |
| 569 | |
Dmitry Lomov | e36a66c | 2017-02-17 14:48:48 +0000 | [diff] [blame] | 570 | private PathFragment getExternalPath(PathFragment path) { |
dannark | be3cefc | 2018-12-13 11:52:45 -0800 | [diff] [blame] | 571 | return checkForWorkspace(path.relativeTo(LabelConstants.EXTERNAL_PACKAGE_NAME)); |
Dmitry Lomov | e36a66c | 2017-02-17 14:48:48 +0000 | [diff] [blame] | 572 | } |
| 573 | |
Kristina Chodorow | d912197 | 2016-04-25 17:45:55 +0000 | [diff] [blame] | 574 | private PathFragment checkForWorkspace(PathFragment path) { |
Lukacs Berki | 4475bf1 | 2017-03-20 14:35:46 +0000 | [diff] [blame] | 575 | sawWorkspaceName = sawWorkspaceName |
| 576 | || path.getSegment(0).equals(workspaceName.getPathString()); |
Kristina Chodorow | d912197 | 2016-04-25 17:45:55 +0000 | [diff] [blame] | 577 | return path; |
| 578 | } |
| 579 | |
Dmitry Lomov | e36a66c | 2017-02-17 14:48:48 +0000 | [diff] [blame] | 580 | private static boolean isUnderWorkspace(PathFragment path) { |
dannark | be3cefc | 2018-12-13 11:52:45 -0800 | [diff] [blame] | 581 | return !path.startsWith(LabelConstants.EXTERNAL_PACKAGE_NAME); |
Kristina Chodorow | d912197 | 2016-04-25 17:45:55 +0000 | [diff] [blame] | 582 | } |
| 583 | } |
| 584 | |
| 585 | boolean getLegacyExternalRunfiles() { |
| 586 | return legacyExternalRunfiles; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 587 | } |
| 588 | |
Benjamin Peterson | eadc65a | 2018-11-07 09:22:40 -0800 | [diff] [blame] | 589 | /** Returns the root symlinks. */ |
| 590 | @Override |
Googler | d21a0d1 | 2019-11-21 13:52:30 -0800 | [diff] [blame] | 591 | public Depset /*<SymlinkEntry>*/ getRootSymlinksForStarlark() { |
Googler | 40c1123 | 2019-11-26 09:35:55 -0800 | [diff] [blame] | 592 | return Depset.of(SymlinkEntry.TYPE, rootSymlinks); |
Googler | fb631cf | 2019-11-21 11:19:32 -0800 | [diff] [blame] | 593 | } |
| 594 | |
Lukacs Berki | 5ab30d6 | 2015-03-11 22:06:04 +0000 | [diff] [blame] | 595 | public NestedSet<SymlinkEntry> getRootSymlinks() { |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 596 | return rootSymlinks; |
| 597 | } |
| 598 | |
| 599 | /** |
Googler | 3ed13c8 | 2016-03-07 21:53:29 +0000 | [diff] [blame] | 600 | * Returns the root symlinks as a map from path fragment to artifact. |
| 601 | * |
| 602 | * @param checker If not null, check for conflicts using this checker. |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 603 | */ |
mstaib | 0e81f9a | 2018-09-17 14:30:28 -0700 | [diff] [blame] | 604 | public Map<PathFragment, Artifact> getRootSymlinksAsMap(@Nullable ConflictChecker checker) { |
Damien Martin-Guillerez | 9e4c78f | 2016-04-22 11:42:34 +0000 | [diff] [blame] | 605 | return entriesToMap(rootSymlinks, checker); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 606 | } |
| 607 | |
| 608 | /** |
| 609 | * Returns the unified map of path fragments to artifacts, taking both artifacts and symlinks into |
| 610 | * account. |
| 611 | */ |
| 612 | public Map<PathFragment, Artifact> asMapWithoutRootSymlinks() { |
mstaib | 0e81f9a | 2018-09-17 14:30:28 -0700 | [diff] [blame] | 613 | Map<PathFragment, Artifact> result = entriesToMap(symlinks, null); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 614 | // If multiple artifacts have the same root-relative path, the last one in the list will win. |
| 615 | // That is because the runfiles tree cannot contain the same artifact for different |
| 616 | // configurations, because it only uses root-relative paths. |
ulfjack | 7a05289 | 2019-12-17 01:21:54 -0800 | [diff] [blame] | 617 | for (Artifact artifact : unconditionalArtifacts.toList()) { |
Damien Martin-Guillerez | 9e4c78f | 2016-04-22 11:42:34 +0000 | [diff] [blame] | 618 | result.put(artifact.getRootRelativePath(), artifact); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 619 | } |
Damien Martin-Guillerez | 9e4c78f | 2016-04-22 11:42:34 +0000 | [diff] [blame] | 620 | return result; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 621 | } |
| 622 | |
| 623 | /** |
| 624 | * Returns the pruning manifests specified for this runfiles tree. |
| 625 | */ |
| 626 | public NestedSet<PruningManifest> getPruningManifests() { |
| 627 | return pruningManifests; |
| 628 | } |
| 629 | |
| 630 | /** |
Michajlo Matijkiw | ac879b9 | 2015-04-15 21:28:33 +0000 | [diff] [blame] | 631 | * Returns the manifest expander specified for this runfiles tree. |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 632 | */ |
Michajlo Matijkiw | ac879b9 | 2015-04-15 21:28:33 +0000 | [diff] [blame] | 633 | private EmptyFilesSupplier getEmptyFilesProvider() { |
| 634 | return emptyFilesSupplier; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 635 | } |
| 636 | |
| 637 | /** |
| 638 | * Returns the unified map of path fragments to artifacts, taking into account artifacts, |
| 639 | * symlinks, and pruning manifest candidates. The returned set is guaranteed to be a (not |
| 640 | * necessarily strict) superset of the actual runfiles tree created at execution time. |
| 641 | */ |
| 642 | public NestedSet<Artifact> getAllArtifacts() { |
| 643 | if (isEmpty()) { |
| 644 | return NestedSetBuilder.emptySet(Order.STABLE_ORDER); |
| 645 | } |
| 646 | NestedSetBuilder<Artifact> allArtifacts = NestedSetBuilder.stableOrder(); |
| 647 | allArtifacts |
| 648 | .addTransitive(unconditionalArtifacts) |
Ulf Adams | cd31d3b | 2019-12-16 00:50:03 -0800 | [diff] [blame] | 649 | .addAll(Iterables.transform(symlinks.toList(), TO_ARTIFACT)) |
| 650 | .addAll(Iterables.transform(rootSymlinks.toList(), TO_ARTIFACT)); |
| 651 | for (PruningManifest manifest : getPruningManifests().toList()) { |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 652 | allArtifacts.addTransitive(manifest.getCandidateRunfiles()); |
| 653 | } |
| 654 | return allArtifacts.build(); |
| 655 | } |
| 656 | |
| 657 | /** |
| 658 | * Returns if there are no runfiles. |
| 659 | */ |
| 660 | public boolean isEmpty() { |
Benjamin Peterson | dbbcbcb | 2017-10-26 11:38:13 +0200 | [diff] [blame] | 661 | return unconditionalArtifacts.isEmpty() |
| 662 | && symlinks.isEmpty() |
| 663 | && rootSymlinks.isEmpty() |
| 664 | && pruningManifests.isEmpty() |
| 665 | && extraMiddlemen.isEmpty(); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 666 | } |
| 667 | |
Damien Martin-Guillerez | 9e4c78f | 2016-04-22 11:42:34 +0000 | [diff] [blame] | 668 | /** |
| 669 | * Flatten a sequence of entries into a single map. |
| 670 | * |
| 671 | * @param entrySet Sequence of entries to add. |
| 672 | * @param checker If not null, check for conflicts with this checker, otherwise silently allow |
ulfjack | 7a05289 | 2019-12-17 01:21:54 -0800 | [diff] [blame] | 673 | * entries to overwrite previous entries. |
Damien Martin-Guillerez | 9e4c78f | 2016-04-22 11:42:34 +0000 | [diff] [blame] | 674 | * @return Map<PathFragment, Artifact> Map of runfile entries. |
| 675 | */ |
| 676 | private static Map<PathFragment, Artifact> entriesToMap( |
ulfjack | 7a05289 | 2019-12-17 01:21:54 -0800 | [diff] [blame] | 677 | NestedSet<SymlinkEntry> entrySet, @Nullable ConflictChecker checker) { |
Damien Martin-Guillerez | 9e4c78f | 2016-04-22 11:42:34 +0000 | [diff] [blame] | 678 | checker = (checker != null) ? checker : ConflictChecker.IGNORE_CHECKER; |
| 679 | Map<PathFragment, Artifact> map = new LinkedHashMap<>(); |
ulfjack | 7a05289 | 2019-12-17 01:21:54 -0800 | [diff] [blame] | 680 | for (SymlinkEntry entry : entrySet.toList()) { |
Damien Martin-Guillerez | 9e4c78f | 2016-04-22 11:42:34 +0000 | [diff] [blame] | 681 | checker.put(map, entry.getPath(), entry.getArtifact()); |
| 682 | } |
| 683 | return map; |
| 684 | } |
| 685 | |
Googler | c85af31 | 2016-03-10 17:55:17 +0000 | [diff] [blame] | 686 | /** Returns currently policy for conflicting symlink entries. */ |
| 687 | public ConflictPolicy getConflictPolicy() { |
| 688 | return this.conflictPolicy; |
| 689 | } |
| 690 | |
Googler | 3ed13c8 | 2016-03-07 21:53:29 +0000 | [diff] [blame] | 691 | /** Set whether we should warn about conflicting symlink entries. */ |
Googler | c85af31 | 2016-03-10 17:55:17 +0000 | [diff] [blame] | 692 | public Runfiles setConflictPolicy(ConflictPolicy conflictPolicy) { |
Googler | 3ed13c8 | 2016-03-07 21:53:29 +0000 | [diff] [blame] | 693 | this.conflictPolicy = conflictPolicy; |
Googler | c85af31 | 2016-03-10 17:55:17 +0000 | [diff] [blame] | 694 | return this; |
Googler | 3ed13c8 | 2016-03-07 21:53:29 +0000 | [diff] [blame] | 695 | } |
| 696 | |
| 697 | /** |
| 698 | * Checks for conflicts between entries in a runfiles tree while putting them in a map. |
| 699 | */ |
| 700 | public static final class ConflictChecker { |
| 701 | /** Prebuilt ConflictChecker with policy set to IGNORE */ |
| 702 | public static final ConflictChecker IGNORE_CHECKER = |
| 703 | new ConflictChecker(ConflictPolicy.IGNORE, null, null); |
| 704 | |
| 705 | /** Behavior when a conflict is found. */ |
| 706 | private final ConflictPolicy policy; |
| 707 | |
| 708 | /** Used for warning on conflicts. May be null, in which case conflicts are ignored. */ |
| 709 | private final EventHandler eventHandler; |
| 710 | |
| 711 | /** Location for eventHandler warnings. Ignored if eventHandler is null. */ |
| 712 | private final Location location; |
| 713 | |
mstaib | 0e81f9a | 2018-09-17 14:30:28 -0700 | [diff] [blame] | 714 | /** Type of event to emit */ |
| 715 | private final EventKind eventKind; |
| 716 | |
Googler | 3ed13c8 | 2016-03-07 21:53:29 +0000 | [diff] [blame] | 717 | /** Construct a ConflictChecker for the given reporter with the given behavior */ |
| 718 | public ConflictChecker(ConflictPolicy policy, EventHandler eventHandler, Location location) { |
| 719 | if (eventHandler == null) { |
| 720 | this.policy = ConflictPolicy.IGNORE; // Can't warn even if we wanted to |
| 721 | } else { |
| 722 | this.policy = policy; |
| 723 | } |
| 724 | this.eventHandler = eventHandler; |
| 725 | this.location = location; |
mstaib | 0e81f9a | 2018-09-17 14:30:28 -0700 | [diff] [blame] | 726 | this.eventKind = (policy == ConflictPolicy.ERROR) ? EventKind.ERROR : EventKind.WARNING; |
Googler | 3ed13c8 | 2016-03-07 21:53:29 +0000 | [diff] [blame] | 727 | } |
| 728 | |
| 729 | /** |
| 730 | * Add an entry to a Map of symlinks, optionally reporting conflicts. |
| 731 | * |
Damien Martin-Guillerez | 9e4c78f | 2016-04-22 11:42:34 +0000 | [diff] [blame] | 732 | * @param map Manifest of runfile entries. |
| 733 | * @param path Path fragment to use as key in map. |
Googler | 3ed13c8 | 2016-03-07 21:53:29 +0000 | [diff] [blame] | 734 | * @param artifact Artifact to store in map. This may be null to indicate an empty file. |
| 735 | */ |
mstaib | 0e81f9a | 2018-09-17 14:30:28 -0700 | [diff] [blame] | 736 | public void put(Map<PathFragment, Artifact> map, PathFragment path, Artifact artifact) { |
ulfjack | ff179a3 | 2018-02-06 04:16:34 -0800 | [diff] [blame] | 737 | Preconditions.checkArgument( |
| 738 | artifact == null || !artifact.isMiddlemanArtifact(), "%s", artifact); |
Googler | 3ed13c8 | 2016-03-07 21:53:29 +0000 | [diff] [blame] | 739 | if (policy != ConflictPolicy.IGNORE && map.containsKey(path)) { |
| 740 | // Previous and new entry might have value of null |
| 741 | Artifact previous = map.get(path); |
| 742 | if (!Objects.equals(previous, artifact)) { |
shahan | b3ba514 | 2018-04-06 19:17:12 -0700 | [diff] [blame] | 743 | String previousStr = |
| 744 | (previous == null) ? "empty file" : previous.getExecPath().toString(); |
| 745 | String artifactStr = |
| 746 | (artifact == null) ? "empty file" : artifact.getExecPath().toString(); |
mstaib | 0e81f9a | 2018-09-17 14:30:28 -0700 | [diff] [blame] | 747 | String message = |
| 748 | String.format( |
| 749 | "overwrote runfile %s, was symlink to %s, now symlink to %s", |
| 750 | path.getSafePathString(), |
| 751 | previousStr, |
| 752 | artifactStr); |
| 753 | eventHandler.handle(Event.of(eventKind, location, message)); |
Googler | 3ed13c8 | 2016-03-07 21:53:29 +0000 | [diff] [blame] | 754 | } |
| 755 | } |
Damien Martin-Guillerez | 9e4c78f | 2016-04-22 11:42:34 +0000 | [diff] [blame] | 756 | map.put(path, artifact); |
Googler | 3ed13c8 | 2016-03-07 21:53:29 +0000 | [diff] [blame] | 757 | } |
| 758 | } |
| 759 | |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 760 | /** |
| 761 | * Builder for Runfiles objects. |
| 762 | */ |
| 763 | public static final class Builder { |
Kristina Chodorow | b579b94 | 2015-03-02 15:51:58 +0000 | [diff] [blame] | 764 | |
Googler | 3ed13c8 | 2016-03-07 21:53:29 +0000 | [diff] [blame] | 765 | /** This is set to the workspace name */ |
Kristina Chodorow | 7ef0251 | 2016-04-22 16:14:12 +0000 | [diff] [blame] | 766 | private PathFragment suffix; |
Kristina Chodorow | b579b94 | 2015-03-02 15:51:58 +0000 | [diff] [blame] | 767 | |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 768 | /** |
| 769 | * This must be COMPILE_ORDER because {@link #asMapWithoutRootSymlinks} overwrites earlier |
| 770 | * entries with later ones, so we want a post-order iteration. |
| 771 | */ |
| 772 | private NestedSetBuilder<Artifact> artifactsBuilder = |
| 773 | NestedSetBuilder.compileOrder(); |
Lukacs Berki | 5ab30d6 | 2015-03-11 22:06:04 +0000 | [diff] [blame] | 774 | private NestedSetBuilder<SymlinkEntry> symlinksBuilder = |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 775 | NestedSetBuilder.stableOrder(); |
Lukacs Berki | 5ab30d6 | 2015-03-11 22:06:04 +0000 | [diff] [blame] | 776 | private NestedSetBuilder<SymlinkEntry> rootSymlinksBuilder = |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 777 | NestedSetBuilder.stableOrder(); |
| 778 | private NestedSetBuilder<PruningManifest> pruningManifestsBuilder = |
| 779 | NestedSetBuilder.stableOrder(); |
Benjamin Peterson | dbbcbcb | 2017-10-26 11:38:13 +0200 | [diff] [blame] | 780 | private NestedSetBuilder<Artifact> extraMiddlemenBuilder = NestedSetBuilder.stableOrder(); |
Michajlo Matijkiw | ac879b9 | 2015-04-15 21:28:33 +0000 | [diff] [blame] | 781 | private EmptyFilesSupplier emptyFilesSupplier = DUMMY_EMPTY_FILES_SUPPLIER; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 782 | |
Googler | c85af31 | 2016-03-10 17:55:17 +0000 | [diff] [blame] | 783 | /** Build the Runfiles object with this policy */ |
| 784 | private ConflictPolicy conflictPolicy = ConflictPolicy.IGNORE; |
| 785 | |
Kristina Chodorow | 8dba2b2 | 2016-04-22 21:24:31 +0000 | [diff] [blame] | 786 | private final boolean legacyExternalRunfiles; |
| 787 | |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 788 | /** |
Kristina Chodorow | 9d513ca | 2015-08-12 15:48:30 +0000 | [diff] [blame] | 789 | * Only used for Runfiles.EMPTY. |
| 790 | */ |
Ulf Adams | 4226be2 | 2015-09-03 17:00:54 +0000 | [diff] [blame] | 791 | private Builder() { |
Kristina Chodorow | 7ef0251 | 2016-04-22 16:14:12 +0000 | [diff] [blame] | 792 | this.suffix = PathFragment.EMPTY_FRAGMENT; |
Kristina Chodorow | 8dba2b2 | 2016-04-22 21:24:31 +0000 | [diff] [blame] | 793 | this.legacyExternalRunfiles = false; |
| 794 | } |
| 795 | |
| 796 | /** |
| 797 | * Creates a builder with the given suffix. Transitional constructor so that new rules don't |
| 798 | * accidentally depend on the legacy repository structure, until that option is removed. |
| 799 | * |
| 800 | * @param workspace is the string specified in workspace() in the WORKSPACE file. |
| 801 | */ |
| 802 | public Builder(String workspace) { |
| 803 | this(workspace, false); |
Kristina Chodorow | 9d513ca | 2015-08-12 15:48:30 +0000 | [diff] [blame] | 804 | } |
| 805 | |
Kristina Chodorow | cc8954a | 2015-10-27 17:23:32 +0000 | [diff] [blame] | 806 | /** |
| 807 | * Creates a builder with the given suffix. |
| 808 | * @param workspace is the string specified in workspace() in the WORKSPACE file. |
Kristina Chodorow | 8dba2b2 | 2016-04-22 21:24:31 +0000 | [diff] [blame] | 809 | * @param legacyExternalRunfiles if the wsname/external/repo symlinks should also be |
| 810 | * created. |
Kristina Chodorow | cc8954a | 2015-10-27 17:23:32 +0000 | [diff] [blame] | 811 | */ |
Kristina Chodorow | 8dba2b2 | 2016-04-22 21:24:31 +0000 | [diff] [blame] | 812 | public Builder(String workspace, boolean legacyExternalRunfiles) { |
nharmata | b4060b6 | 2017-04-04 17:11:39 +0000 | [diff] [blame] | 813 | this(PathFragment.create(workspace), legacyExternalRunfiles); |
Jon Brandvein | 8d10034 | 2016-12-28 17:04:37 +0000 | [diff] [blame] | 814 | } |
| 815 | |
| 816 | /** |
| 817 | * Creates a builder with the given suffix. |
| 818 | * @param suffix is the PathFragment wrapping the string specified in workspace() in the |
| 819 | * WORKSPACE file. |
| 820 | * @param legacyExternalRunfiles if the wsname/external/repo symlinks should also be |
| 821 | * created. |
| 822 | */ |
| 823 | private Builder(PathFragment suffix, boolean legacyExternalRunfiles) { |
| 824 | this.suffix = suffix; |
Kristina Chodorow | 8dba2b2 | 2016-04-22 21:24:31 +0000 | [diff] [blame] | 825 | this.legacyExternalRunfiles = legacyExternalRunfiles; |
Kristina Chodorow | 9d513ca | 2015-08-12 15:48:30 +0000 | [diff] [blame] | 826 | } |
| 827 | |
| 828 | /** |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 829 | * Builds a new Runfiles object. |
| 830 | */ |
| 831 | public Runfiles build() { |
Benjamin Peterson | dbbcbcb | 2017-10-26 11:38:13 +0200 | [diff] [blame] | 832 | return new Runfiles( |
| 833 | suffix, |
| 834 | artifactsBuilder.build(), |
| 835 | symlinksBuilder.build(), |
| 836 | rootSymlinksBuilder.build(), |
| 837 | pruningManifestsBuilder.build(), |
| 838 | extraMiddlemenBuilder.build(), |
| 839 | emptyFilesSupplier, |
| 840 | conflictPolicy, |
| 841 | legacyExternalRunfiles); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 842 | } |
| 843 | |
| 844 | /** |
| 845 | * Adds an artifact to the internal collection of artifacts. |
| 846 | */ |
| 847 | public Builder addArtifact(Artifact artifact) { |
| 848 | Preconditions.checkNotNull(artifact); |
cushon | 110cba0 | 2019-02-19 12:43:11 -0800 | [diff] [blame] | 849 | Preconditions.checkArgument( |
| 850 | !artifact.isMiddlemanArtifact(), "unexpected middleman artifact: %s", artifact); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 851 | artifactsBuilder.add(artifact); |
| 852 | return this; |
| 853 | } |
| 854 | |
| 855 | /** |
| 856 | * Adds several artifacts to the internal collection. |
| 857 | */ |
| 858 | public Builder addArtifacts(Iterable<Artifact> artifacts) { |
| 859 | for (Artifact artifact : artifacts) { |
| 860 | addArtifact(artifact); |
| 861 | } |
| 862 | return this; |
| 863 | } |
| 864 | |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 865 | /** |
Carmi Grushko | 7566563 | 2015-08-27 23:19:26 +0000 | [diff] [blame] | 866 | * @deprecated Use {@link #addTransitiveArtifacts} instead, to prevent increased memory use. |
jingwen | 68c57f0 | 2018-11-21 16:17:17 -0800 | [diff] [blame] | 867 | * <p>See also {@link Builder#addTransitiveArtifactsWrappedInStableOrder} |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 868 | */ |
| 869 | @Deprecated |
| 870 | public Builder addArtifacts(NestedSet<Artifact> artifacts) { |
| 871 | // Do not delete this method, or else addArtifacts(Iterable) calls with a NestedSet argument |
| 872 | // will not be flagged. |
ulfjack | 7a05289 | 2019-12-17 01:21:54 -0800 | [diff] [blame] | 873 | addArtifacts(artifacts.toList()); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 874 | return this; |
| 875 | } |
Carmi Grushko | 7566563 | 2015-08-27 23:19:26 +0000 | [diff] [blame] | 876 | |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 877 | /** |
| 878 | * Adds a nested set to the internal collection. |
| 879 | */ |
| 880 | public Builder addTransitiveArtifacts(NestedSet<Artifact> artifacts) { |
| 881 | artifactsBuilder.addTransitive(artifacts); |
| 882 | return this; |
| 883 | } |
| 884 | |
| 885 | /** |
tomlu | cd032d0 | 2017-09-19 15:50:28 +0200 | [diff] [blame] | 886 | * Adds a nested set to the internal collection. |
| 887 | * |
| 888 | * <p>The nested set will become wrapped in stable order. Only use this when the set of |
| 889 | * artifacts will not have conflicting root relative paths, or the wrong artifact will end up in |
| 890 | * the runfiles tree. |
| 891 | */ |
| 892 | public Builder addTransitiveArtifactsWrappedInStableOrder(NestedSet<Artifact> artifacts) { |
| 893 | NestedSet<Artifact> wrappedArtifacts = |
| 894 | NestedSetBuilder.<Artifact>stableOrder().addTransitive(artifacts).build(); |
| 895 | artifactsBuilder.addTransitive(wrappedArtifacts); |
| 896 | return this; |
| 897 | } |
| 898 | |
| 899 | /** |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 900 | * Adds a symlink. |
| 901 | */ |
| 902 | public Builder addSymlink(PathFragment link, Artifact target) { |
Lukacs Berki | 5ab30d6 | 2015-03-11 22:06:04 +0000 | [diff] [blame] | 903 | symlinksBuilder.add(new SymlinkEntry(link, target)); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 904 | return this; |
| 905 | } |
| 906 | |
Ulf Adams | 8594de8 | 2017-03-20 16:30:11 +0000 | [diff] [blame] | 907 | /** Adds several symlinks. Neither keys nor values may be null. */ |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 908 | public Builder addSymlinks(Map<PathFragment, Artifact> symlinks) { |
Lukacs Berki | 5ab30d6 | 2015-03-11 22:06:04 +0000 | [diff] [blame] | 909 | for (Map.Entry<PathFragment, Artifact> symlink : symlinks.entrySet()) { |
| 910 | symlinksBuilder.add(new SymlinkEntry(symlink.getKey(), symlink.getValue())); |
| 911 | } |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 912 | return this; |
| 913 | } |
| 914 | |
| 915 | /** |
| 916 | * Adds several symlinks as a NestedSet. |
| 917 | */ |
Lukacs Berki | 5ab30d6 | 2015-03-11 22:06:04 +0000 | [diff] [blame] | 918 | public Builder addSymlinks(NestedSet<SymlinkEntry> symlinks) { |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 919 | symlinksBuilder.addTransitive(symlinks); |
| 920 | return this; |
| 921 | } |
| 922 | |
| 923 | /** |
Googler | c85af31 | 2016-03-10 17:55:17 +0000 | [diff] [blame] | 924 | * Adds a root symlink. |
| 925 | */ |
| 926 | public Builder addRootSymlink(PathFragment link, Artifact target) { |
Googler | c85af31 | 2016-03-10 17:55:17 +0000 | [diff] [blame] | 927 | rootSymlinksBuilder.add(new SymlinkEntry(link, target)); |
| 928 | return this; |
| 929 | } |
| 930 | |
Ulf Adams | 8594de8 | 2017-03-20 16:30:11 +0000 | [diff] [blame] | 931 | /** Adds several root symlinks. Neither keys nor values may be null. */ |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 932 | public Builder addRootSymlinks(Map<PathFragment, Artifact> symlinks) { |
Lukacs Berki | 5ab30d6 | 2015-03-11 22:06:04 +0000 | [diff] [blame] | 933 | for (Map.Entry<PathFragment, Artifact> symlink : symlinks.entrySet()) { |
| 934 | rootSymlinksBuilder.add(new SymlinkEntry(symlink.getKey(), symlink.getValue())); |
| 935 | } |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 936 | return this; |
| 937 | } |
| 938 | |
| 939 | /** |
| 940 | * Adds several root symlinks as a NestedSet. |
| 941 | */ |
Lukacs Berki | 5ab30d6 | 2015-03-11 22:06:04 +0000 | [diff] [blame] | 942 | public Builder addRootSymlinks(NestedSet<SymlinkEntry> symlinks) { |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 943 | rootSymlinksBuilder.addTransitive(symlinks); |
| 944 | return this; |
| 945 | } |
| 946 | |
| 947 | /** |
| 948 | * Adds a pruning manifest. See {@link PruningManifest} for an explanation. |
| 949 | */ |
| 950 | public Builder addPruningManifest(PruningManifest manifest) { |
| 951 | pruningManifestsBuilder.add(manifest); |
| 952 | return this; |
| 953 | } |
| 954 | |
| 955 | /** |
| 956 | * Adds several pruning manifests as a NestedSet. See {@link PruningManifest} for an |
| 957 | * explanation. |
| 958 | */ |
| 959 | public Builder addPruningManifests(NestedSet<PruningManifest> manifests) { |
| 960 | pruningManifestsBuilder.addTransitive(manifests); |
| 961 | return this; |
| 962 | } |
| 963 | |
| 964 | /** |
Michajlo Matijkiw | ac879b9 | 2015-04-15 21:28:33 +0000 | [diff] [blame] | 965 | * Specify a function that can create additional manifest entries based on the input entries, |
| 966 | * see {@link EmptyFilesSupplier} for more details. |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 967 | */ |
Michajlo Matijkiw | ac879b9 | 2015-04-15 21:28:33 +0000 | [diff] [blame] | 968 | public Builder setEmptyFilesSupplier(EmptyFilesSupplier supplier) { |
| 969 | emptyFilesSupplier = Preconditions.checkNotNull(supplier); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 970 | return this; |
| 971 | } |
| 972 | |
| 973 | /** |
| 974 | * Merges runfiles from a given runfiles support. |
| 975 | * |
| 976 | * @param runfilesSupport the runfiles support to be merged in |
| 977 | */ |
| 978 | public Builder merge(@Nullable RunfilesSupport runfilesSupport) { |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 979 | if (runfilesSupport == null) { |
| 980 | return this; |
| 981 | } |
Lukacs Berki | 1f82079 | 2015-03-06 18:16:10 +0000 | [diff] [blame] | 982 | merge(runfilesSupport.getRunfiles()); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 983 | return this; |
| 984 | } |
| 985 | |
| 986 | /** |
| 987 | * Adds the runfiles for a particular target and visits the transitive closure of "srcs", |
| 988 | * "deps" and "data", collecting all of their respective runfiles. |
| 989 | */ |
| 990 | public Builder addRunfiles(RuleContext ruleContext, |
| 991 | Function<TransitiveInfoCollection, Runfiles> mapping) { |
| 992 | Preconditions.checkNotNull(mapping); |
| 993 | Preconditions.checkNotNull(ruleContext); |
| 994 | addDataDeps(ruleContext); |
| 995 | addNonDataDeps(ruleContext, mapping); |
| 996 | return this; |
| 997 | } |
| 998 | |
| 999 | /** |
| 1000 | * Adds the files specified by a mapping from the transitive info collection to the runfiles. |
| 1001 | * |
| 1002 | * <p>Dependencies in {@code srcs} and {@code deps} are considered. |
| 1003 | */ |
| 1004 | public Builder add(RuleContext ruleContext, |
| 1005 | Function<TransitiveInfoCollection, Runfiles> mapping) { |
| 1006 | Preconditions.checkNotNull(ruleContext); |
| 1007 | Preconditions.checkNotNull(mapping); |
| 1008 | for (TransitiveInfoCollection dep : getNonDataDeps(ruleContext)) { |
| 1009 | Runfiles runfiles = mapping.apply(dep); |
| 1010 | if (runfiles != null) { |
| 1011 | merge(runfiles); |
| 1012 | } |
| 1013 | } |
| 1014 | |
| 1015 | return this; |
| 1016 | } |
| 1017 | |
| 1018 | /** |
| 1019 | * Collects runfiles from data dependencies of a target. |
| 1020 | */ |
| 1021 | public Builder addDataDeps(RuleContext ruleContext) { |
gregce | 475d91a | 2018-05-25 12:18:27 -0700 | [diff] [blame] | 1022 | addTargets(getPrerequisites(ruleContext, "data", Mode.DONT_CHECK), |
| 1023 | RunfilesProvider.DATA_RUNFILES); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 1024 | return this; |
| 1025 | } |
| 1026 | |
| 1027 | /** |
| 1028 | * Collects runfiles from "srcs" and "deps" of a target. |
| 1029 | */ |
| 1030 | public Builder addNonDataDeps(RuleContext ruleContext, |
| 1031 | Function<TransitiveInfoCollection, Runfiles> mapping) { |
| 1032 | for (TransitiveInfoCollection target : getNonDataDeps(ruleContext)) { |
| 1033 | addTargetExceptFileTargets(target, mapping); |
| 1034 | } |
| 1035 | return this; |
| 1036 | } |
| 1037 | |
| 1038 | public Builder addTargets(Iterable<? extends TransitiveInfoCollection> targets, |
| 1039 | Function<TransitiveInfoCollection, Runfiles> mapping) { |
| 1040 | for (TransitiveInfoCollection target : targets) { |
| 1041 | addTarget(target, mapping); |
| 1042 | } |
| 1043 | return this; |
| 1044 | } |
| 1045 | |
| 1046 | public Builder addTarget(TransitiveInfoCollection target, |
| 1047 | Function<TransitiveInfoCollection, Runfiles> mapping) { |
| 1048 | return addTargetIncludingFileTargets(target, mapping); |
| 1049 | } |
| 1050 | |
| 1051 | private Builder addTargetExceptFileTargets(TransitiveInfoCollection target, |
| 1052 | Function<TransitiveInfoCollection, Runfiles> mapping) { |
| 1053 | Runfiles runfiles = mapping.apply(target); |
| 1054 | if (runfiles != null) { |
| 1055 | merge(runfiles); |
| 1056 | } |
| 1057 | |
| 1058 | return this; |
| 1059 | } |
| 1060 | |
| 1061 | private Builder addTargetIncludingFileTargets(TransitiveInfoCollection target, |
| 1062 | Function<TransitiveInfoCollection, Runfiles> mapping) { |
| 1063 | if (target.getProvider(RunfilesProvider.class) == null |
| 1064 | && mapping == RunfilesProvider.DATA_RUNFILES) { |
| 1065 | // RuleConfiguredTarget implements RunfilesProvider, so this will only be called on |
| 1066 | // FileConfiguredTarget instances. |
| 1067 | // TODO(bazel-team): This is a terrible hack. We should be able to make this go away |
| 1068 | // by implementing RunfilesProvider on FileConfiguredTarget. We'd need to be mindful |
| 1069 | // of the memory use, though, since we have a whole lot of FileConfiguredTarget instances. |
| 1070 | addTransitiveArtifacts(target.getProvider(FileProvider.class).getFilesToBuild()); |
| 1071 | return this; |
| 1072 | } |
| 1073 | |
| 1074 | return addTargetExceptFileTargets(target, mapping); |
| 1075 | } |
| 1076 | |
ulfjack | 459ce87 | 2020-01-09 05:06:07 -0800 | [diff] [blame] | 1077 | /** Adds symlinks to given artifacts at their exec paths. */ |
| 1078 | public Builder addSymlinksToArtifacts(NestedSet<Artifact> artifacts) { |
| 1079 | // These are symlinks using the exec path, not the root-relative path, which currently |
| 1080 | // requires flattening. |
| 1081 | return addSymlinksToArtifacts(artifacts.toList()); |
| 1082 | } |
| 1083 | |
| 1084 | /** Adds symlinks to given artifacts at their exec paths. */ |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 1085 | public Builder addSymlinksToArtifacts(Iterable<Artifact> artifacts) { |
| 1086 | for (Artifact artifact : artifacts) { |
| 1087 | addSymlink(artifact.getExecPath(), artifact); |
| 1088 | } |
| 1089 | return this; |
| 1090 | } |
| 1091 | |
| 1092 | /** |
Benjamin Peterson | dbbcbcb | 2017-10-26 11:38:13 +0200 | [diff] [blame] | 1093 | * Add extra middlemen artifacts that should be built by reverse dependency binaries. This |
| 1094 | * method exists solely to support the unfortunate legacy behavior of some rules; new uses |
| 1095 | * should not be added. |
| 1096 | */ |
| 1097 | public Builder addLegacyExtraMiddleman(Artifact middleman) { |
| 1098 | Preconditions.checkArgument(middleman.isMiddlemanArtifact(), middleman); |
| 1099 | extraMiddlemenBuilder.add(middleman); |
| 1100 | return this; |
| 1101 | } |
| 1102 | |
ulfjack | 459ce87 | 2020-01-09 05:06:07 -0800 | [diff] [blame] | 1103 | /** Add the other {@link Runfiles} object transitively. */ |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 1104 | public Builder merge(Runfiles runfiles) { |
Googler | d7a780e | 2017-03-14 18:10:02 +0000 | [diff] [blame] | 1105 | return merge(runfiles, true, true); |
| 1106 | } |
| 1107 | |
| 1108 | /** |
ulfjack | 459ce87 | 2020-01-09 05:06:07 -0800 | [diff] [blame] | 1109 | * Add the other {@link Runfiles} object transitively, but don't merge unconditional artifacts. |
Googler | d7a780e | 2017-03-14 18:10:02 +0000 | [diff] [blame] | 1110 | */ |
| 1111 | public Builder mergeExceptUnconditionalArtifacts(Runfiles runfiles) { |
| 1112 | return merge(runfiles, false, true); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 1113 | } |
| 1114 | |
ulfjack | 459ce87 | 2020-01-09 05:06:07 -0800 | [diff] [blame] | 1115 | /** Add the other {@link Runfiles} object transitively, but don't merge pruning manifests. */ |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 1116 | public Builder mergeExceptPruningManifests(Runfiles runfiles) { |
Googler | d7a780e | 2017-03-14 18:10:02 +0000 | [diff] [blame] | 1117 | return merge(runfiles, true, false); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 1118 | } |
| 1119 | |
| 1120 | /** |
| 1121 | * Add the other {@link Runfiles} object transitively, with the option to include or exclude |
| 1122 | * pruning manifests in the merge. |
| 1123 | */ |
Googler | d7a780e | 2017-03-14 18:10:02 +0000 | [diff] [blame] | 1124 | private Builder merge(Runfiles runfiles, boolean includeUnconditionalArtifacts, |
| 1125 | boolean includePruningManifests) { |
Googler | c85af31 | 2016-03-10 17:55:17 +0000 | [diff] [blame] | 1126 | // Propagate the most strict conflict checking from merged-in runfiles |
| 1127 | if (runfiles.conflictPolicy.compareTo(conflictPolicy) > 0) { |
| 1128 | conflictPolicy = runfiles.conflictPolicy; |
| 1129 | } |
Ulf Adams | 4226be2 | 2015-09-03 17:00:54 +0000 | [diff] [blame] | 1130 | if (runfiles.isEmpty()) { |
| 1131 | return this; |
| 1132 | } |
| 1133 | // The suffix should be the same within any blaze build, except for the EMPTY runfiles, which |
| 1134 | // may have an empty suffix, but that is covered above. |
cushon | 44a4fd8 | 2019-02-06 09:34:50 -0800 | [diff] [blame] | 1135 | Preconditions.checkArgument( |
| 1136 | suffix.equals(runfiles.suffix), "%s != %s", suffix, runfiles.suffix); |
Googler | d7a780e | 2017-03-14 18:10:02 +0000 | [diff] [blame] | 1137 | if (includeUnconditionalArtifacts) { |
| 1138 | artifactsBuilder.addTransitive(runfiles.getUnconditionalArtifacts()); |
| 1139 | } |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 1140 | symlinksBuilder.addTransitive(runfiles.getSymlinks()); |
| 1141 | rootSymlinksBuilder.addTransitive(runfiles.getRootSymlinks()); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 1142 | if (includePruningManifests) { |
| 1143 | pruningManifestsBuilder.addTransitive(runfiles.getPruningManifests()); |
| 1144 | } |
Benjamin Peterson | dbbcbcb | 2017-10-26 11:38:13 +0200 | [diff] [blame] | 1145 | extraMiddlemenBuilder.addTransitive(runfiles.getExtraMiddlemen()); |
Michajlo Matijkiw | ac879b9 | 2015-04-15 21:28:33 +0000 | [diff] [blame] | 1146 | if (emptyFilesSupplier == DUMMY_EMPTY_FILES_SUPPLIER) { |
| 1147 | emptyFilesSupplier = runfiles.getEmptyFilesProvider(); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 1148 | } else { |
Michajlo Matijkiw | ac879b9 | 2015-04-15 21:28:33 +0000 | [diff] [blame] | 1149 | EmptyFilesSupplier otherSupplier = runfiles.getEmptyFilesProvider(); |
| 1150 | Preconditions.checkState((otherSupplier == DUMMY_EMPTY_FILES_SUPPLIER) |
| 1151 | || emptyFilesSupplier.equals(otherSupplier)); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 1152 | } |
| 1153 | return this; |
| 1154 | } |
| 1155 | |
| 1156 | private static Iterable<TransitiveInfoCollection> getNonDataDeps(RuleContext ruleContext) { |
| 1157 | return Iterables.concat( |
| 1158 | // TODO(bazel-team): This line shouldn't be here. Removing it requires that no rules have |
| 1159 | // dependent rules in srcs (except for filegroups and such), but always in deps. |
| 1160 | // TODO(bazel-team): DONT_CHECK is not optimal here. Rules that use split configs need to |
| 1161 | // be changed not to call into here. |
| 1162 | getPrerequisites(ruleContext, "srcs", Mode.DONT_CHECK), |
| 1163 | getPrerequisites(ruleContext, "deps", Mode.DONT_CHECK)); |
| 1164 | } |
| 1165 | |
| 1166 | /** |
| 1167 | * For the specified attribute "attributeName" (which must be of type list(label)), resolves all |
| 1168 | * the labels into ConfiguredTargets (for the same configuration as this one) and returns them |
| 1169 | * as a list. |
| 1170 | * |
| 1171 | * <p>If the rule does not have the specified attribute, returns the empty list. |
| 1172 | */ |
| 1173 | private static Iterable<? extends TransitiveInfoCollection> getPrerequisites( |
| 1174 | RuleContext ruleContext, String attributeName, Mode mode) { |
Lukacs Berki | ffa73ad | 2015-09-18 11:40:12 +0000 | [diff] [blame] | 1175 | if (ruleContext.getRule().isAttrDefined(attributeName, BuildType.LABEL_LIST)) { |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 1176 | return ruleContext.getPrerequisites(attributeName, mode); |
| 1177 | } else { |
| 1178 | return Collections.emptyList(); |
| 1179 | } |
| 1180 | } |
| 1181 | } |
Jon Brandvein | 8d10034 | 2016-12-28 17:04:37 +0000 | [diff] [blame] | 1182 | |
cparsons | 90f70b6 | 2018-05-01 12:45:02 -0700 | [diff] [blame] | 1183 | @Override |
| 1184 | public Runfiles merge(RunfilesApi other) { |
ulfjack | 8c007af | 2019-02-12 11:10:24 -0800 | [diff] [blame] | 1185 | Runfiles o = (Runfiles) other; |
| 1186 | if (isEmpty()) { |
| 1187 | // This is not just a memory / performance optimization. The Builder requires a valid suffix, |
| 1188 | // but the {@code Runfiles.EMPTY} singleton has an invalid one, which must not be used to |
| 1189 | // construct a Runfiles.Builder. |
| 1190 | return o; |
| 1191 | } else if (o.isEmpty()) { |
| 1192 | return this; |
| 1193 | } |
| 1194 | return new Runfiles.Builder(suffix, false).merge(this).merge(o).build(); |
Jon Brandvein | 8d10034 | 2016-12-28 17:04:37 +0000 | [diff] [blame] | 1195 | } |
felly | c31c8c9 | 2018-08-29 15:07:59 -0700 | [diff] [blame] | 1196 | |
| 1197 | /** |
| 1198 | * Fingerprint this {@link Runfiles} tree. |
| 1199 | */ |
| 1200 | public void fingerprint(Fingerprint fp) { |
| 1201 | fp.addBoolean(getLegacyExternalRunfiles()); |
| 1202 | fp.addPath(getSuffix()); |
mstaib | 0e81f9a | 2018-09-17 14:30:28 -0700 | [diff] [blame] | 1203 | Map<PathFragment, Artifact> symlinks = getSymlinksAsMap(null); |
felly | c31c8c9 | 2018-08-29 15:07:59 -0700 | [diff] [blame] | 1204 | fp.addInt(symlinks.size()); |
| 1205 | for (Map.Entry<PathFragment, Artifact> symlink : symlinks.entrySet()) { |
| 1206 | fp.addPath(symlink.getKey()); |
| 1207 | fp.addPath(symlink.getValue().getExecPath()); |
| 1208 | } |
mstaib | 0e81f9a | 2018-09-17 14:30:28 -0700 | [diff] [blame] | 1209 | Map<PathFragment, Artifact> rootSymlinks = getRootSymlinksAsMap(null); |
felly | c31c8c9 | 2018-08-29 15:07:59 -0700 | [diff] [blame] | 1210 | fp.addInt(rootSymlinks.size()); |
| 1211 | for (Map.Entry<PathFragment, Artifact> rootSymlink : rootSymlinks.entrySet()) { |
| 1212 | fp.addPath(rootSymlink.getKey()); |
| 1213 | fp.addPath(rootSymlink.getValue().getExecPath()); |
| 1214 | } |
| 1215 | |
ulfjack | 7a05289 | 2019-12-17 01:21:54 -0800 | [diff] [blame] | 1216 | for (Artifact artifact : getArtifacts().toList()) { |
felly | c31c8c9 | 2018-08-29 15:07:59 -0700 | [diff] [blame] | 1217 | fp.addPath(artifact.getRootRelativePath()); |
| 1218 | fp.addPath(artifact.getExecPath()); |
| 1219 | } |
brandjon | 08561e3 | 2018-09-25 06:59:00 -0700 | [diff] [blame] | 1220 | |
ulfjack | 7a05289 | 2019-12-17 01:21:54 -0800 | [diff] [blame] | 1221 | for (String name : getEmptyFilenames().toList()) { |
brandjon | 08561e3 | 2018-09-25 06:59:00 -0700 | [diff] [blame] | 1222 | fp.addString(name); |
| 1223 | } |
felly | c31c8c9 | 2018-08-29 15:07:59 -0700 | [diff] [blame] | 1224 | } |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 1225 | } |