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.actions; |
| 16 | |
| 17 | import com.google.common.collect.ImmutableList; |
| 18 | import com.google.common.collect.ImmutableMap; |
Michajlo Matijkiw | 767c372 | 2015-05-13 18:16:29 +0000 | [diff] [blame] | 19 | import com.google.common.collect.Iterables; |
John Cater | 9ac1f28 | 2017-12-04 19:34:53 -0800 | [diff] [blame] | 20 | import com.google.devtools.build.lib.analysis.platform.PlatformInfo; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 21 | import com.google.devtools.build.lib.vfs.PathFragment; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 22 | import java.util.Collection; |
| 23 | import java.util.List; |
| 24 | import java.util.Map; |
Michajlo Matijkiw | 767c372 | 2015-05-13 18:16:29 +0000 | [diff] [blame] | 25 | import java.util.Set; |
John Cater | 9ac1f28 | 2017-12-04 19:34:53 -0800 | [diff] [blame] | 26 | import javax.annotation.Nullable; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 27 | import javax.annotation.concurrent.Immutable; |
| 28 | |
Yue Gan | 4795564 | 2016-09-20 13:21:51 +0000 | [diff] [blame] | 29 | /** Base implementation of a Spawn. */ |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 30 | @Immutable |
| 31 | public class BaseSpawn implements Spawn { |
| 32 | private final ImmutableList<String> arguments; |
| 33 | private final ImmutableMap<String, String> environment; |
| 34 | private final ImmutableMap<String, String> executionInfo; |
Michajlo Matijkiw | 767c372 | 2015-05-13 18:16:29 +0000 | [diff] [blame] | 35 | private final RunfilesSupplier runfilesSupplier; |
Rumou Duan | 33bab46 | 2016-04-25 17:55:12 +0000 | [diff] [blame] | 36 | private final ActionExecutionMetadata action; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 37 | private final ResourceSet localResources; |
| 38 | |
Michajlo Matijkiw | 4a87738 | 2017-01-27 19:30:34 +0000 | [diff] [blame] | 39 | public BaseSpawn( |
Philipp Wollermann | 39125b2 | 2016-01-18 14:19:33 +0000 | [diff] [blame] | 40 | List<String> arguments, |
Michajlo Matijkiw | 767c372 | 2015-05-13 18:16:29 +0000 | [diff] [blame] | 41 | Map<String, String> environment, |
| 42 | Map<String, String> executionInfo, |
Michajlo Matijkiw | 767c372 | 2015-05-13 18:16:29 +0000 | [diff] [blame] | 43 | RunfilesSupplier runfilesSupplier, |
Rumou Duan | 33bab46 | 2016-04-25 17:55:12 +0000 | [diff] [blame] | 44 | ActionExecutionMetadata action, |
Ulf Adams | d345cf9 | 2017-03-07 10:04:53 +0000 | [diff] [blame] | 45 | ResourceSet localResources) { |
Michajlo Matijkiw | 767c372 | 2015-05-13 18:16:29 +0000 | [diff] [blame] | 46 | this.arguments = ImmutableList.copyOf(arguments); |
| 47 | this.environment = ImmutableMap.copyOf(environment); |
| 48 | this.executionInfo = ImmutableMap.copyOf(executionInfo); |
Michajlo Matijkiw | 767c372 | 2015-05-13 18:16:29 +0000 | [diff] [blame] | 49 | this.runfilesSupplier = runfilesSupplier; |
| 50 | this.action = action; |
| 51 | this.localResources = localResources; |
| 52 | } |
| 53 | |
Yue Gan | 4795564 | 2016-09-20 13:21:51 +0000 | [diff] [blame] | 54 | public BaseSpawn( |
| 55 | List<String> arguments, |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 56 | Map<String, String> environment, |
| 57 | Map<String, String> executionInfo, |
Rumou Duan | 33bab46 | 2016-04-25 17:55:12 +0000 | [diff] [blame] | 58 | ActionExecutionMetadata action, |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 59 | ResourceSet localResources) { |
Philipp Wollermann | 39125b2 | 2016-01-18 14:19:33 +0000 | [diff] [blame] | 60 | this( |
| 61 | arguments, |
| 62 | environment, |
| 63 | executionInfo, |
Michajlo Matijkiw | 4a87738 | 2017-01-27 19:30:34 +0000 | [diff] [blame] | 64 | EmptyRunfilesSupplier.INSTANCE, |
Philipp Wollermann | 39125b2 | 2016-01-18 14:19:33 +0000 | [diff] [blame] | 65 | action, |
| 66 | localResources); |
| 67 | } |
| 68 | |
Michajlo Matijkiw | 767c372 | 2015-05-13 18:16:29 +0000 | [diff] [blame] | 69 | public static PathFragment runfilesForFragment(PathFragment pathFragment) { |
| 70 | return pathFragment.getParentDirectory().getChild(pathFragment.getBaseName() + ".runfiles"); |
| 71 | } |
| 72 | |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 73 | @Override |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 74 | public final ImmutableMap<String, String> getExecutionInfo() { |
| 75 | return executionInfo; |
| 76 | } |
| 77 | |
| 78 | @Override |
Michajlo Matijkiw | 767c372 | 2015-05-13 18:16:29 +0000 | [diff] [blame] | 79 | public RunfilesSupplier getRunfilesSupplier() { |
| 80 | return runfilesSupplier; |
| 81 | } |
| 82 | |
| 83 | @Override |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 84 | public ImmutableList<String> getArguments() { |
| 85 | // TODO(bazel-team): this method should be final, as the correct value of the args can be |
| 86 | // injected in the ctor. |
| 87 | return arguments; |
| 88 | } |
| 89 | |
| 90 | @Override |
tomlu | 73eccc2 | 2018-09-06 08:02:37 -0700 | [diff] [blame] | 91 | public ImmutableMap<Artifact, ImmutableList<FilesetOutputSymlink>> getFilesetMappings() { |
kush | 2ce45a2 | 2018-05-02 14:15:37 -0700 | [diff] [blame] | 92 | return ImmutableMap.of(); |
| 93 | } |
| 94 | |
| 95 | @Override |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 96 | public ImmutableMap<String, String> getEnvironment() { |
Michajlo Matijkiw | 767c372 | 2015-05-13 18:16:29 +0000 | [diff] [blame] | 97 | PathFragment runfilesRoot = getRunfilesRoot(); |
Yue Gan | 5a4b445 | 2016-08-01 10:59:36 +0000 | [diff] [blame] | 98 | if (runfilesRoot == null |
| 99 | || (environment.containsKey("JAVA_RUNFILES") |
Adam Michael | 7618bbd | 2017-01-17 20:06:09 +0000 | [diff] [blame] | 100 | && environment.containsKey("PYTHON_RUNFILES"))) { |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 101 | return environment; |
Michajlo Matijkiw | 767c372 | 2015-05-13 18:16:29 +0000 | [diff] [blame] | 102 | } else { |
| 103 | ImmutableMap.Builder<String, String> env = ImmutableMap.builder(); |
| 104 | env.putAll(environment); |
Adam Michael | 7618bbd | 2017-01-17 20:06:09 +0000 | [diff] [blame] | 105 | // TODO(bazel-team): Unify these into a single env variable. |
Michajlo Matijkiw | 767c372 | 2015-05-13 18:16:29 +0000 | [diff] [blame] | 106 | String runfilesRootString = runfilesRoot.getPathString(); |
| 107 | env.put("JAVA_RUNFILES", runfilesRootString); |
| 108 | env.put("PYTHON_RUNFILES", runfilesRootString); |
| 109 | return env.build(); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 110 | } |
Michajlo Matijkiw | 767c372 | 2015-05-13 18:16:29 +0000 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | /** @return the runfiles directory if there is only one, otherwise null */ |
| 114 | private PathFragment getRunfilesRoot() { |
| 115 | Set<PathFragment> runfilesSupplierRoots = runfilesSupplier.getRunfilesDirs(); |
Michajlo Matijkiw | 4a87738 | 2017-01-27 19:30:34 +0000 | [diff] [blame] | 116 | if (runfilesSupplierRoots.size() == 1) { |
Michajlo Matijkiw | 767c372 | 2015-05-13 18:16:29 +0000 | [diff] [blame] | 117 | return Iterables.getOnlyElement(runfilesSupplierRoots); |
Michajlo Matijkiw | 767c372 | 2015-05-13 18:16:29 +0000 | [diff] [blame] | 118 | } else { |
| 119 | return null; |
| 120 | } |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | @Override |
Philipp Wollermann | af33c67 | 2015-11-04 17:52:32 +0000 | [diff] [blame] | 124 | public Iterable<? extends ActionInput> getToolFiles() { |
| 125 | return action.getTools(); |
| 126 | } |
| 127 | |
| 128 | @Override |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 129 | public Iterable<? extends ActionInput> getInputFiles() { |
| 130 | return action.getInputs(); |
| 131 | } |
| 132 | |
| 133 | @Override |
| 134 | public Collection<? extends ActionInput> getOutputFiles() { |
| 135 | return action.getOutputs(); |
| 136 | } |
| 137 | |
| 138 | @Override |
Rumou Duan | 33bab46 | 2016-04-25 17:55:12 +0000 | [diff] [blame] | 139 | public ActionExecutionMetadata getResourceOwner() { |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 140 | return action; |
| 141 | } |
| 142 | |
| 143 | @Override |
| 144 | public ResourceSet getLocalResources() { |
| 145 | return localResources; |
| 146 | } |
| 147 | |
| 148 | @Override |
Yue Gan | 4795564 | 2016-09-20 13:21:51 +0000 | [diff] [blame] | 149 | public String getMnemonic() { |
| 150 | return action.getMnemonic(); |
| 151 | } |
John Cater | 9ac1f28 | 2017-12-04 19:34:53 -0800 | [diff] [blame] | 152 | |
| 153 | @Override |
| 154 | @Nullable |
| 155 | public PlatformInfo getExecutionPlatform() { |
| 156 | return action.getExecutionPlatform(); |
| 157 | } |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 158 | } |