blob: dcba4032547055c7f8aed201982eb58d3b4c9aef [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.actions;
16
17import com.google.common.collect.ImmutableList;
18import com.google.common.collect.ImmutableMap;
Michajlo Matijkiw767c3722015-05-13 18:16:29 +000019import com.google.common.collect.Iterables;
John Cater9ac1f282017-12-04 19:34:53 -080020import com.google.devtools.build.lib.analysis.platform.PlatformInfo;
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010021import com.google.devtools.build.lib.vfs.PathFragment;
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010022import java.util.Collection;
23import java.util.List;
24import java.util.Map;
Michajlo Matijkiw767c3722015-05-13 18:16:29 +000025import java.util.Set;
John Cater9ac1f282017-12-04 19:34:53 -080026import javax.annotation.Nullable;
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010027import javax.annotation.concurrent.Immutable;
28
Yue Gan47955642016-09-20 13:21:51 +000029/** Base implementation of a Spawn. */
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010030@Immutable
31public 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 Matijkiw767c3722015-05-13 18:16:29 +000035 private final RunfilesSupplier runfilesSupplier;
Rumou Duan33bab462016-04-25 17:55:12 +000036 private final ActionExecutionMetadata action;
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010037 private final ResourceSet localResources;
38
Michajlo Matijkiw4a877382017-01-27 19:30:34 +000039 public BaseSpawn(
Philipp Wollermann39125b22016-01-18 14:19:33 +000040 List<String> arguments,
Michajlo Matijkiw767c3722015-05-13 18:16:29 +000041 Map<String, String> environment,
42 Map<String, String> executionInfo,
Michajlo Matijkiw767c3722015-05-13 18:16:29 +000043 RunfilesSupplier runfilesSupplier,
Rumou Duan33bab462016-04-25 17:55:12 +000044 ActionExecutionMetadata action,
Ulf Adamsd345cf92017-03-07 10:04:53 +000045 ResourceSet localResources) {
Michajlo Matijkiw767c3722015-05-13 18:16:29 +000046 this.arguments = ImmutableList.copyOf(arguments);
47 this.environment = ImmutableMap.copyOf(environment);
48 this.executionInfo = ImmutableMap.copyOf(executionInfo);
Michajlo Matijkiw767c3722015-05-13 18:16:29 +000049 this.runfilesSupplier = runfilesSupplier;
50 this.action = action;
51 this.localResources = localResources;
52 }
53
Yue Gan47955642016-09-20 13:21:51 +000054 public BaseSpawn(
55 List<String> arguments,
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010056 Map<String, String> environment,
57 Map<String, String> executionInfo,
Rumou Duan33bab462016-04-25 17:55:12 +000058 ActionExecutionMetadata action,
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010059 ResourceSet localResources) {
Philipp Wollermann39125b22016-01-18 14:19:33 +000060 this(
61 arguments,
62 environment,
63 executionInfo,
Michajlo Matijkiw4a877382017-01-27 19:30:34 +000064 EmptyRunfilesSupplier.INSTANCE,
Philipp Wollermann39125b22016-01-18 14:19:33 +000065 action,
66 localResources);
67 }
68
Michajlo Matijkiw767c3722015-05-13 18:16:29 +000069 public static PathFragment runfilesForFragment(PathFragment pathFragment) {
70 return pathFragment.getParentDirectory().getChild(pathFragment.getBaseName() + ".runfiles");
71 }
72
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010073 @Override
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010074 public final ImmutableMap<String, String> getExecutionInfo() {
75 return executionInfo;
76 }
77
78 @Override
Michajlo Matijkiw767c3722015-05-13 18:16:29 +000079 public RunfilesSupplier getRunfilesSupplier() {
80 return runfilesSupplier;
81 }
82
83 @Override
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010084 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
tomlu73eccc22018-09-06 08:02:37 -070091 public ImmutableMap<Artifact, ImmutableList<FilesetOutputSymlink>> getFilesetMappings() {
kush2ce45a22018-05-02 14:15:37 -070092 return ImmutableMap.of();
93 }
94
95 @Override
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010096 public ImmutableMap<String, String> getEnvironment() {
Michajlo Matijkiw767c3722015-05-13 18:16:29 +000097 PathFragment runfilesRoot = getRunfilesRoot();
Yue Gan5a4b4452016-08-01 10:59:36 +000098 if (runfilesRoot == null
99 || (environment.containsKey("JAVA_RUNFILES")
Adam Michael7618bbd2017-01-17 20:06:09 +0000100 && environment.containsKey("PYTHON_RUNFILES"))) {
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100101 return environment;
Michajlo Matijkiw767c3722015-05-13 18:16:29 +0000102 } else {
103 ImmutableMap.Builder<String, String> env = ImmutableMap.builder();
104 env.putAll(environment);
Adam Michael7618bbd2017-01-17 20:06:09 +0000105 // TODO(bazel-team): Unify these into a single env variable.
Michajlo Matijkiw767c3722015-05-13 18:16:29 +0000106 String runfilesRootString = runfilesRoot.getPathString();
107 env.put("JAVA_RUNFILES", runfilesRootString);
108 env.put("PYTHON_RUNFILES", runfilesRootString);
109 return env.build();
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100110 }
Michajlo Matijkiw767c3722015-05-13 18:16:29 +0000111 }
112
113 /** @return the runfiles directory if there is only one, otherwise null */
114 private PathFragment getRunfilesRoot() {
115 Set<PathFragment> runfilesSupplierRoots = runfilesSupplier.getRunfilesDirs();
Michajlo Matijkiw4a877382017-01-27 19:30:34 +0000116 if (runfilesSupplierRoots.size() == 1) {
Michajlo Matijkiw767c3722015-05-13 18:16:29 +0000117 return Iterables.getOnlyElement(runfilesSupplierRoots);
Michajlo Matijkiw767c3722015-05-13 18:16:29 +0000118 } else {
119 return null;
120 }
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100121 }
122
123 @Override
Philipp Wollermannaf33c672015-11-04 17:52:32 +0000124 public Iterable<? extends ActionInput> getToolFiles() {
125 return action.getTools();
126 }
127
128 @Override
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100129 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 Duan33bab462016-04-25 17:55:12 +0000139 public ActionExecutionMetadata getResourceOwner() {
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100140 return action;
141 }
142
143 @Override
144 public ResourceSet getLocalResources() {
145 return localResources;
146 }
147
148 @Override
Yue Gan47955642016-09-20 13:21:51 +0000149 public String getMnemonic() {
150 return action.getMnemonic();
151 }
John Cater9ac1f282017-12-04 19:34:53 -0800152
153 @Override
154 @Nullable
155 public PlatformInfo getExecutionPlatform() {
156 return action.getExecutionPlatform();
157 }
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100158}