blob: 474784c5a7e2953c9a8cc09ca733e72c13fd3113 [file] [log] [blame]
Damien Martin-Guillerezf88f4d82015-09-25 13:56:55 +00001// Copyright 2015 The Bazel Authors. All rights reserved.
Michajlo Matijkiw767c3722015-05-13 18:16:29 +00002//
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
15package com.google.devtools.build.lib.analysis;
16
17import com.google.common.annotations.VisibleForTesting;
18import com.google.common.collect.ImmutableMap;
19import com.google.common.collect.ImmutableSet;
Michajlo Matijkiw7eef6f32015-05-26 16:17:36 +000020import com.google.common.collect.Iterables;
Michajlo Matijkiw767c3722015-05-13 18:16:29 +000021import com.google.devtools.build.lib.actions.Artifact;
Michajlo Matijkiw8bc43522015-06-05 03:05:16 +000022import com.google.devtools.build.lib.actions.BaseSpawn;
Michajlo Matijkiw767c3722015-05-13 18:16:29 +000023import com.google.devtools.build.lib.actions.RunfilesSupplier;
24import com.google.devtools.build.lib.vfs.PathFragment;
25
26import java.io.IOException;
27import java.util.Map;
28import java.util.Map.Entry;
29
30/**
31 * {@link RunfilesSupplier} implementation wrapping directory to {@link Runfiles} objects mappings.
32 */
33public class RunfilesSupplierImpl implements RunfilesSupplier {
34
35 private final ImmutableMap<PathFragment, Runfiles> inputRunfiles;
36
37 /**
Michajlo Matijkiw8bc43522015-06-05 03:05:16 +000038 * 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 Matijkiw767c3722015-05-13 18:16:29 +000049 * 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 Matijkiwdee781c2015-05-22 23:25:34 +000064 public Iterable<Artifact> getArtifacts() {
65 ImmutableSet.Builder<Artifact> builder = ImmutableSet.builder();
66 for (Entry<PathFragment, Runfiles> entry : inputRunfiles.entrySet()) {
Michajlo Matijkiw7eef6f32015-05-26 16:17:36 +000067 builder.addAll(
68 Iterables.filter(entry.getValue().getAllArtifacts(), Artifact.MIDDLEMAN_FILTER));
Michajlo Matijkiwdee781c2015-05-22 23:25:34 +000069 }
70 return builder.build();
71 }
72
73 @Override
Michajlo Matijkiw767c3722015-05-13 18:16:29 +000074 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}