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 | |
| 17 | import com.google.common.annotations.VisibleForTesting; |
| 18 | import com.google.common.collect.ImmutableMap; |
| 19 | import com.google.common.collect.ImmutableSet; |
Michajlo Matijkiw | 7eef6f3 | 2015-05-26 16:17:36 +0000 | [diff] [blame] | 20 | import com.google.common.collect.Iterables; |
Michajlo Matijkiw | 767c372 | 2015-05-13 18:16:29 +0000 | [diff] [blame] | 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; |
| 24 | import com.google.devtools.build.lib.vfs.PathFragment; |
| 25 | |
| 26 | import java.io.IOException; |
| 27 | import java.util.Map; |
| 28 | import java.util.Map.Entry; |
| 29 | |
| 30 | /** |
| 31 | * {@link RunfilesSupplier} implementation wrapping directory to {@link Runfiles} objects mappings. |
| 32 | */ |
| 33 | public class RunfilesSupplierImpl implements RunfilesSupplier { |
| 34 | |
| 35 | private final ImmutableMap<PathFragment, Runfiles> inputRunfiles; |
| 36 | |
| 37 | /** |
Michajlo Matijkiw | 8bc4352 | 2015-06-05 03:05:16 +0000 | [diff] [blame] | 38 | * Create an instance for an executable. |
| 39 | * |
| 40 | * @param executable the executable the runfiles are for, used for determining the runfiles |
| 41 | * directory |
| 42 | * @param runfiles the associated runfiles |
| 43 | */ |
| 44 | public RunfilesSupplierImpl(Artifact executable, Runfiles runfiles) { |
| 45 | this(BaseSpawn.runfilesForFragment(executable.getExecPath()), runfiles); |
| 46 | } |
| 47 | |
| 48 | /** |
Michajlo Matijkiw | 767c372 | 2015-05-13 18:16:29 +0000 | [diff] [blame] | 49 | * Create an instance when you have a a single mapping. |
| 50 | * |
| 51 | * @param runfilesDir the desired runfiles directory. Should be relative. |
| 52 | * @param runfiles the runfiles for runfilesDir |
| 53 | */ |
| 54 | public RunfilesSupplierImpl(PathFragment runfilesDir, Runfiles runfiles) { |
| 55 | this.inputRunfiles = ImmutableMap.of(runfilesDir, runfiles); |
| 56 | } |
| 57 | |
| 58 | @VisibleForTesting |
| 59 | public RunfilesSupplierImpl(Map<PathFragment, Runfiles> inputRunfiles) { |
| 60 | this.inputRunfiles = ImmutableMap.copyOf(inputRunfiles); |
| 61 | } |
| 62 | |
| 63 | @Override |
Michajlo Matijkiw | dee781c | 2015-05-22 23:25:34 +0000 | [diff] [blame] | 64 | public Iterable<Artifact> getArtifacts() { |
| 65 | ImmutableSet.Builder<Artifact> builder = ImmutableSet.builder(); |
| 66 | for (Entry<PathFragment, Runfiles> entry : inputRunfiles.entrySet()) { |
Michajlo Matijkiw | 7eef6f3 | 2015-05-26 16:17:36 +0000 | [diff] [blame] | 67 | builder.addAll( |
| 68 | Iterables.filter(entry.getValue().getAllArtifacts(), Artifact.MIDDLEMAN_FILTER)); |
Michajlo Matijkiw | dee781c | 2015-05-22 23:25:34 +0000 | [diff] [blame] | 69 | } |
| 70 | return builder.build(); |
| 71 | } |
| 72 | |
| 73 | @Override |
Michajlo Matijkiw | 767c372 | 2015-05-13 18:16:29 +0000 | [diff] [blame] | 74 | public ImmutableSet<PathFragment> getRunfilesDirs() { |
| 75 | return inputRunfiles.keySet(); |
| 76 | } |
| 77 | |
| 78 | @Override |
| 79 | public ImmutableMap<PathFragment, Map<PathFragment, Artifact>> getMappings() throws IOException { |
| 80 | ImmutableMap.Builder<PathFragment, Map<PathFragment, Artifact>> result = |
| 81 | ImmutableMap.builder(); |
| 82 | for (Entry<PathFragment, Runfiles> entry : inputRunfiles.entrySet()) { |
| 83 | result.put(entry.getKey(), entry.getValue().getRunfilesInputs(null, null)); |
| 84 | } |
| 85 | return result.build(); |
| 86 | } |
| 87 | |
| 88 | } |