Damien Martin-Guillerez | f88f4d8 | 2015-09-25 13:56:55 +0000 | [diff] [blame] | 1 | // Copyright 2015 The Bazel Authors. All rights reserved. |
Michajlo Matijkiw | 767c372 | 2015-05-13 18:16:29 +0000 | [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 | |
Michajlo Matijkiw | 4a87738 | 2017-01-27 19:30:34 +0000 | [diff] [blame] | 17 | import com.google.common.base.Preconditions; |
| 18 | import com.google.common.collect.ImmutableList; |
Michajlo Matijkiw | 767c372 | 2015-05-13 18:16:29 +0000 | [diff] [blame] | 19 | import com.google.common.collect.ImmutableMap; |
| 20 | import com.google.common.collect.ImmutableSet; |
| 21 | import com.google.devtools.build.lib.actions.Artifact; |
Michajlo Matijkiw | 8bc4352 | 2015-06-05 03:05:16 +0000 | [diff] [blame] | 22 | import com.google.devtools.build.lib.actions.BaseSpawn; |
Michajlo Matijkiw | 767c372 | 2015-05-13 18:16:29 +0000 | [diff] [blame] | 23 | import com.google.devtools.build.lib.actions.RunfilesSupplier; |
cpeyser | a41e01e | 2018-02-22 09:27:40 -0800 | [diff] [blame^] | 24 | import com.google.devtools.build.lib.skyframe.serialization.autocodec.AutoCodec; |
Michajlo Matijkiw | 767c372 | 2015-05-13 18:16:29 +0000 | [diff] [blame] | 25 | import com.google.devtools.build.lib.vfs.PathFragment; |
Michajlo Matijkiw | 767c372 | 2015-05-13 18:16:29 +0000 | [diff] [blame] | 26 | import java.io.IOException; |
| 27 | import java.util.Map; |
Michajlo Matijkiw | 4a87738 | 2017-01-27 19:30:34 +0000 | [diff] [blame] | 28 | import javax.annotation.Nullable; |
Michajlo Matijkiw | 767c372 | 2015-05-13 18:16:29 +0000 | [diff] [blame] | 29 | |
Michajlo Matijkiw | 4a87738 | 2017-01-27 19:30:34 +0000 | [diff] [blame] | 30 | /** {@link RunfilesSupplier} implementation wrapping a single {@link Runfiles} directory mapping. */ |
| 31 | // TODO(bazel-team): Consider renaming to SingleRunfilesSupplierImpl. |
cpeyser | a41e01e | 2018-02-22 09:27:40 -0800 | [diff] [blame^] | 32 | @AutoCodec |
Michajlo Matijkiw | 767c372 | 2015-05-13 18:16:29 +0000 | [diff] [blame] | 33 | public class RunfilesSupplierImpl implements RunfilesSupplier { |
Michajlo Matijkiw | 4a87738 | 2017-01-27 19:30:34 +0000 | [diff] [blame] | 34 | private final PathFragment runfilesDir; |
| 35 | private final Runfiles runfiles; |
| 36 | @Nullable |
| 37 | private final Artifact manifest; |
Michajlo Matijkiw | 767c372 | 2015-05-13 18:16:29 +0000 | [diff] [blame] | 38 | |
| 39 | /** |
Michajlo Matijkiw | 8bc4352 | 2015-06-05 03:05:16 +0000 | [diff] [blame] | 40 | * Create an instance for an executable. |
| 41 | * |
| 42 | * @param executable the executable the runfiles are for, used for determining the runfiles |
| 43 | * directory |
| 44 | * @param runfiles the associated runfiles |
| 45 | */ |
| 46 | public RunfilesSupplierImpl(Artifact executable, Runfiles runfiles) { |
| 47 | this(BaseSpawn.runfilesForFragment(executable.getExecPath()), runfiles); |
| 48 | } |
| 49 | |
| 50 | /** |
Michajlo Matijkiw | 4a87738 | 2017-01-27 19:30:34 +0000 | [diff] [blame] | 51 | * Create an instance. When a manifest is available consider using |
| 52 | * {@link #RunfilesSupplierImpl(PathFragment, Runfiles, Artifact)} instead. |
Michajlo Matijkiw | 767c372 | 2015-05-13 18:16:29 +0000 | [diff] [blame] | 53 | */ |
| 54 | public RunfilesSupplierImpl(PathFragment runfilesDir, Runfiles runfiles) { |
Michajlo Matijkiw | 4a87738 | 2017-01-27 19:30:34 +0000 | [diff] [blame] | 55 | this(runfilesDir, runfiles, /*manifest=*/ null); |
Michajlo Matijkiw | 767c372 | 2015-05-13 18:16:29 +0000 | [diff] [blame] | 56 | } |
| 57 | |
Michajlo Matijkiw | 4a87738 | 2017-01-27 19:30:34 +0000 | [diff] [blame] | 58 | /** |
| 59 | * Create an instance mapping {@code runfiles} to {@code runfilesDir}. |
| 60 | * |
| 61 | * @param runfilesDir the desired runfiles directory. Should be relative. |
| 62 | * @param runfiles the runfiles for runilesDir. |
| 63 | * @param manifest runfiles' associated runfiles manifest artifact, if present. |
| 64 | */ |
cpeyser | a41e01e | 2018-02-22 09:27:40 -0800 | [diff] [blame^] | 65 | @AutoCodec.Instantiator |
Michajlo Matijkiw | 4a87738 | 2017-01-27 19:30:34 +0000 | [diff] [blame] | 66 | public RunfilesSupplierImpl( |
cpeyser | a41e01e | 2018-02-22 09:27:40 -0800 | [diff] [blame^] | 67 | PathFragment runfilesDir, Runfiles runfiles, @Nullable Artifact manifest) { |
Ulf Adams | edd531d | 2017-03-20 15:01:45 +0000 | [diff] [blame] | 68 | Preconditions.checkArgument(!runfilesDir.isAbsolute()); |
Michajlo Matijkiw | 4a87738 | 2017-01-27 19:30:34 +0000 | [diff] [blame] | 69 | this.runfilesDir = Preconditions.checkNotNull(runfilesDir); |
| 70 | this.runfiles = Preconditions.checkNotNull(runfiles); |
| 71 | this.manifest = manifest; |
Michajlo Matijkiw | 767c372 | 2015-05-13 18:16:29 +0000 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | @Override |
Michajlo Matijkiw | dee781c | 2015-05-22 23:25:34 +0000 | [diff] [blame] | 75 | public Iterable<Artifact> getArtifacts() { |
ulfjack | ff179a3 | 2018-02-06 04:16:34 -0800 | [diff] [blame] | 76 | return runfiles.getAllArtifacts(); |
Michajlo Matijkiw | dee781c | 2015-05-22 23:25:34 +0000 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | @Override |
Michajlo Matijkiw | 767c372 | 2015-05-13 18:16:29 +0000 | [diff] [blame] | 80 | public ImmutableSet<PathFragment> getRunfilesDirs() { |
Michajlo Matijkiw | 4a87738 | 2017-01-27 19:30:34 +0000 | [diff] [blame] | 81 | return ImmutableSet.of(runfilesDir); |
Michajlo Matijkiw | 767c372 | 2015-05-13 18:16:29 +0000 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | @Override |
| 85 | public ImmutableMap<PathFragment, Map<PathFragment, Artifact>> getMappings() throws IOException { |
Michajlo Matijkiw | 4a87738 | 2017-01-27 19:30:34 +0000 | [diff] [blame] | 86 | return ImmutableMap.of( |
| 87 | runfilesDir, |
| 88 | runfiles.getRunfilesInputs(/*eventHandler=*/ null, /*location=*/ null)); |
Michajlo Matijkiw | 767c372 | 2015-05-13 18:16:29 +0000 | [diff] [blame] | 89 | } |
| 90 | |
Michajlo Matijkiw | 4a87738 | 2017-01-27 19:30:34 +0000 | [diff] [blame] | 91 | @Override |
| 92 | public ImmutableList<Artifact> getManifests() { |
| 93 | return manifest != null ? ImmutableList.of(manifest) : ImmutableList.<Artifact>of(); |
| 94 | } |
Michajlo Matijkiw | 767c372 | 2015-05-13 18:16:29 +0000 | [diff] [blame] | 95 | } |