blob: 89c674110623ed0e9f674d500809842e9c946d12 [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
Michajlo Matijkiw4a877382017-01-27 19:30:34 +000017import com.google.common.base.Preconditions;
18import com.google.common.collect.ImmutableList;
Michajlo Matijkiw767c3722015-05-13 18:16:29 +000019import com.google.common.collect.ImmutableMap;
20import com.google.common.collect.ImmutableSet;
21import 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;
cpeysera41e01e2018-02-22 09:27:40 -080024import com.google.devtools.build.lib.skyframe.serialization.autocodec.AutoCodec;
Michajlo Matijkiw767c3722015-05-13 18:16:29 +000025import com.google.devtools.build.lib.vfs.PathFragment;
Michajlo Matijkiw767c3722015-05-13 18:16:29 +000026import java.io.IOException;
27import java.util.Map;
Michajlo Matijkiw4a877382017-01-27 19:30:34 +000028import javax.annotation.Nullable;
Michajlo Matijkiw767c3722015-05-13 18:16:29 +000029
Michajlo Matijkiw4a877382017-01-27 19:30:34 +000030/** {@link RunfilesSupplier} implementation wrapping a single {@link Runfiles} directory mapping. */
31// TODO(bazel-team): Consider renaming to SingleRunfilesSupplierImpl.
cpeysera41e01e2018-02-22 09:27:40 -080032@AutoCodec
Michajlo Matijkiw767c3722015-05-13 18:16:29 +000033public class RunfilesSupplierImpl implements RunfilesSupplier {
Michajlo Matijkiw4a877382017-01-27 19:30:34 +000034 private final PathFragment runfilesDir;
35 private final Runfiles runfiles;
36 @Nullable
37 private final Artifact manifest;
Michajlo Matijkiw767c3722015-05-13 18:16:29 +000038
39 /**
Michajlo Matijkiw8bc43522015-06-05 03:05:16 +000040 * 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 Matijkiw4a877382017-01-27 19:30:34 +000051 * Create an instance. When a manifest is available consider using
52 * {@link #RunfilesSupplierImpl(PathFragment, Runfiles, Artifact)} instead.
Michajlo Matijkiw767c3722015-05-13 18:16:29 +000053 */
54 public RunfilesSupplierImpl(PathFragment runfilesDir, Runfiles runfiles) {
Michajlo Matijkiw4a877382017-01-27 19:30:34 +000055 this(runfilesDir, runfiles, /*manifest=*/ null);
Michajlo Matijkiw767c3722015-05-13 18:16:29 +000056 }
57
Michajlo Matijkiw4a877382017-01-27 19:30:34 +000058 /**
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 */
cpeysera41e01e2018-02-22 09:27:40 -080065 @AutoCodec.Instantiator
Michajlo Matijkiw4a877382017-01-27 19:30:34 +000066 public RunfilesSupplierImpl(
cpeysera41e01e2018-02-22 09:27:40 -080067 PathFragment runfilesDir, Runfiles runfiles, @Nullable Artifact manifest) {
Ulf Adamsedd531d2017-03-20 15:01:45 +000068 Preconditions.checkArgument(!runfilesDir.isAbsolute());
Michajlo Matijkiw4a877382017-01-27 19:30:34 +000069 this.runfilesDir = Preconditions.checkNotNull(runfilesDir);
70 this.runfiles = Preconditions.checkNotNull(runfiles);
71 this.manifest = manifest;
Michajlo Matijkiw767c3722015-05-13 18:16:29 +000072 }
73
74 @Override
Michajlo Matijkiwdee781c2015-05-22 23:25:34 +000075 public Iterable<Artifact> getArtifacts() {
ulfjackff179a32018-02-06 04:16:34 -080076 return runfiles.getAllArtifacts();
Michajlo Matijkiwdee781c2015-05-22 23:25:34 +000077 }
78
79 @Override
Michajlo Matijkiw767c3722015-05-13 18:16:29 +000080 public ImmutableSet<PathFragment> getRunfilesDirs() {
Michajlo Matijkiw4a877382017-01-27 19:30:34 +000081 return ImmutableSet.of(runfilesDir);
Michajlo Matijkiw767c3722015-05-13 18:16:29 +000082 }
83
84 @Override
85 public ImmutableMap<PathFragment, Map<PathFragment, Artifact>> getMappings() throws IOException {
Michajlo Matijkiw4a877382017-01-27 19:30:34 +000086 return ImmutableMap.of(
87 runfilesDir,
88 runfiles.getRunfilesInputs(/*eventHandler=*/ null, /*location=*/ null));
Michajlo Matijkiw767c3722015-05-13 18:16:29 +000089 }
90
Michajlo Matijkiw4a877382017-01-27 19:30:34 +000091 @Override
92 public ImmutableList<Artifact> getManifests() {
93 return manifest != null ? ImmutableList.of(manifest) : ImmutableList.<Artifact>of();
94 }
Michajlo Matijkiw767c3722015-05-13 18:16:29 +000095}