blob: 8077a59d35a350d62f28b213c6a3f04443eb35fa [file] [log] [blame]
Damien Martin-Guillerezf88f4d82015-09-25 13:56:55 +00001// Copyright 2014 The Bazel Authors. All rights reserved.
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +01002//
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.collect.ImmutableList;
18import com.google.devtools.build.lib.actions.Artifact;
Michajlo Matijkiw50d93a82015-06-12 17:58:38 +000019import com.google.devtools.build.lib.actions.EmptyRunfilesSupplier;
20import com.google.devtools.build.lib.actions.RunfilesSupplier;
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010021import com.google.devtools.build.lib.concurrent.ThreadSafety.Immutable;
John Field585d1a02015-12-16 16:03:52 +000022import com.google.devtools.build.lib.skylarkinterface.SkylarkCallable;
23import com.google.devtools.build.lib.skylarkinterface.SkylarkModule;
Dmitry Lomov34cdae32016-06-28 16:13:35 +000024import com.google.devtools.build.lib.skylarkinterface.SkylarkModuleCategory;
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010025
26import javax.annotation.Nullable;
27
Dmitry Lomov34cdae32016-06-28 16:13:35 +000028/** Returns information about executables produced by a target and the files needed to run it. */
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010029@Immutable
Dmitry Lomov34cdae32016-06-28 16:13:35 +000030@SkylarkModule(name = "FilesToRunProvider", doc = "", category = SkylarkModuleCategory.PROVIDER)
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010031public final class FilesToRunProvider implements TransitiveInfoProvider {
Brian Silverman2ccd0562015-08-27 13:40:02 +000032 /** The name of the field in Skylark used to access this class. */
33 public static final String SKYLARK_NAME = "files_to_run";
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010034
Ulf Adamsad8678f2016-04-21 14:56:36 +000035 public static final FilesToRunProvider EMPTY =
36 new FilesToRunProvider(ImmutableList.<Artifact>of(), null, null);
37
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010038 private final ImmutableList<Artifact> filesToRun;
39 @Nullable private final RunfilesSupport runfilesSupport;
40 @Nullable private final Artifact executable;
41
Ulf Adamsad8678f2016-04-21 14:56:36 +000042 public FilesToRunProvider(ImmutableList<Artifact> filesToRun,
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010043 @Nullable RunfilesSupport runfilesSupport, @Nullable Artifact executable) {
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010044 this.filesToRun = filesToRun;
45 this.runfilesSupport = runfilesSupport;
46 this.executable = executable;
47 }
48
49 /**
Lukacs Berki959ba5e2015-04-23 11:47:38 +000050 * Creates an instance that contains one single executable and no other files.
51 */
Ulf Adamsad8678f2016-04-21 14:56:36 +000052 public static FilesToRunProvider fromSingleExecutableArtifact(Artifact artifact) {
53 return new FilesToRunProvider(ImmutableList.of(artifact), null, artifact);
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010054 }
55
56 /**
57 * Returns artifacts needed to run the executable for this target.
58 */
59 public ImmutableList<Artifact> getFilesToRun() {
60 return filesToRun;
61 }
62
63 /**
64 * Returns the {@RunfilesSupport} object associated with the target or null if it does not exist.
65 */
66 @Nullable public RunfilesSupport getRunfilesSupport() {
67 return runfilesSupport;
68 }
69
70 /**
71 * Returns the Executable or null if it does not exist.
72 */
Brian Silverman2ccd0562015-08-27 13:40:02 +000073 @SkylarkCallable(
74 name = "executable",
75 doc = "The main executable or None if it does not exist",
76 structField = true,
77 allowReturnNones = true
78 )
79 @Nullable
80 public Artifact getExecutable() {
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010081 return executable;
82 }
83
84 /**
85 * Returns the RunfilesManifest or null if it does not exist. It is a shortcut to
86 * getRunfilesSupport().getRunfilesManifest().
87 */
Brian Silverman2ccd0562015-08-27 13:40:02 +000088 @SkylarkCallable(
89 name = "runfiles_manifest",
90 doc = "The runfiles manifest or None if it does not exist",
91 structField = true,
92 allowReturnNones = true
93 )
94 @Nullable
95 public Artifact getRunfilesManifest() {
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010096 return runfilesSupport != null ? runfilesSupport.getRunfilesManifest() : null;
97 }
Michajlo Matijkiw50d93a82015-06-12 17:58:38 +000098
99 /** Return a {@link RunfilesSupplier} encapsulating runfiles for this tool. */
100 public RunfilesSupplier getRunfilesSupplier() {
101 if (executable != null && runfilesSupport != null) {
102 return new RunfilesSupplierImpl(executable, runfilesSupport.getRunfiles());
103 } else {
104 return EmptyRunfilesSupplier.INSTANCE;
105 }
106 }
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100107}