Damien Martin-Guillerez | f88f4d8 | 2015-09-25 13:56:55 +0000 | [diff] [blame] | 1 | // Copyright 2014 The Bazel Authors. All rights reserved. |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [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.collect.ImmutableList; |
| 18 | import com.google.devtools.build.lib.actions.Artifact; |
Michajlo Matijkiw | 50d93a8 | 2015-06-12 17:58:38 +0000 | [diff] [blame] | 19 | import com.google.devtools.build.lib.actions.EmptyRunfilesSupplier; |
| 20 | import com.google.devtools.build.lib.actions.RunfilesSupplier; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 21 | import com.google.devtools.build.lib.concurrent.ThreadSafety.Immutable; |
John Field | 585d1a0 | 2015-12-16 16:03:52 +0000 | [diff] [blame] | 22 | import com.google.devtools.build.lib.skylarkinterface.SkylarkCallable; |
| 23 | import com.google.devtools.build.lib.skylarkinterface.SkylarkModule; |
Dmitry Lomov | 34cdae3 | 2016-06-28 16:13:35 +0000 | [diff] [blame] | 24 | import com.google.devtools.build.lib.skylarkinterface.SkylarkModuleCategory; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 25 | |
| 26 | import javax.annotation.Nullable; |
| 27 | |
Dmitry Lomov | 34cdae3 | 2016-06-28 16:13:35 +0000 | [diff] [blame] | 28 | /** Returns information about executables produced by a target and the files needed to run it. */ |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 29 | @Immutable |
Dmitry Lomov | 34cdae3 | 2016-06-28 16:13:35 +0000 | [diff] [blame] | 30 | @SkylarkModule(name = "FilesToRunProvider", doc = "", category = SkylarkModuleCategory.PROVIDER) |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 31 | public final class FilesToRunProvider implements TransitiveInfoProvider { |
Brian Silverman | 2ccd056 | 2015-08-27 13:40:02 +0000 | [diff] [blame] | 32 | /** The name of the field in Skylark used to access this class. */ |
| 33 | public static final String SKYLARK_NAME = "files_to_run"; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 34 | |
Ulf Adams | ad8678f | 2016-04-21 14:56:36 +0000 | [diff] [blame] | 35 | public static final FilesToRunProvider EMPTY = |
| 36 | new FilesToRunProvider(ImmutableList.<Artifact>of(), null, null); |
| 37 | |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 38 | private final ImmutableList<Artifact> filesToRun; |
| 39 | @Nullable private final RunfilesSupport runfilesSupport; |
| 40 | @Nullable private final Artifact executable; |
| 41 | |
Ulf Adams | ad8678f | 2016-04-21 14:56:36 +0000 | [diff] [blame] | 42 | public FilesToRunProvider(ImmutableList<Artifact> filesToRun, |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 43 | @Nullable RunfilesSupport runfilesSupport, @Nullable Artifact executable) { |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 44 | this.filesToRun = filesToRun; |
| 45 | this.runfilesSupport = runfilesSupport; |
| 46 | this.executable = executable; |
| 47 | } |
| 48 | |
| 49 | /** |
Lukacs Berki | 959ba5e | 2015-04-23 11:47:38 +0000 | [diff] [blame] | 50 | * Creates an instance that contains one single executable and no other files. |
| 51 | */ |
Ulf Adams | ad8678f | 2016-04-21 14:56:36 +0000 | [diff] [blame] | 52 | public static FilesToRunProvider fromSingleExecutableArtifact(Artifact artifact) { |
| 53 | return new FilesToRunProvider(ImmutableList.of(artifact), null, artifact); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 54 | } |
| 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 Silverman | 2ccd056 | 2015-08-27 13:40:02 +0000 | [diff] [blame] | 73 | @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 Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 81 | 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 Silverman | 2ccd056 | 2015-08-27 13:40:02 +0000 | [diff] [blame] | 88 | @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 Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 96 | return runfilesSupport != null ? runfilesSupport.getRunfilesManifest() : null; |
| 97 | } |
Michajlo Matijkiw | 50d93a8 | 2015-06-12 17:58:38 +0000 | [diff] [blame] | 98 | |
| 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 Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 107 | } |