blob: e8762ef945efd73dd07f5cf80fd4d6360bd8b332 [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
Klaus Aehligd2fcd9d2016-08-26 08:16:25 +000017import com.google.common.collect.ImmutableMap;
Michael Thvedt434e68e2016-02-09 00:57:46 +000018import com.google.devtools.build.lib.actions.Artifact.ArtifactExpander;
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010019import com.google.devtools.build.lib.actions.cache.MetadataHandler;
Mark Schaller6df81792015-12-10 18:47:47 +000020import com.google.devtools.build.lib.util.Preconditions;
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010021import com.google.devtools.build.lib.util.io.FileOutErr;
Janak Ramakrishnan29c5ab42015-05-14 19:38:12 +000022import com.google.devtools.build.skyframe.SkyFunction;
23import com.google.devtools.build.skyframe.SkyFunction.Environment;
ulfjack1a328e32017-04-06 10:25:16 +000024import java.io.Closeable;
25import java.io.IOException;
Klaus Aehligd2fcd9d2016-08-26 08:16:25 +000026import java.util.Map;
Janak Ramakrishnan29c5ab42015-05-14 19:38:12 +000027import javax.annotation.Nullable;
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010028
29/**
30 * A class that groups services in the scope of the action. Like the FileOutErr object.
31 */
ulfjack1a328e32017-04-06 10:25:16 +000032public class ActionExecutionContext implements Closeable {
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010033
34 private final Executor executor;
35 private final ActionInputFileCache actionInputFileCache;
36 private final MetadataHandler metadataHandler;
37 private final FileOutErr fileOutErr;
Klaus Aehligd2fcd9d2016-08-26 08:16:25 +000038 private final ImmutableMap<String, String> clientEnv;
Michael Thvedt434e68e2016-02-09 00:57:46 +000039 private final ArtifactExpander artifactExpander;
Janak Ramakrishnan29c5ab42015-05-14 19:38:12 +000040 @Nullable
41 private final Environment env;
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010042
Klaus Aehligd2fcd9d2016-08-26 08:16:25 +000043 private ActionExecutionContext(
44 Executor executor,
45 ActionInputFileCache actionInputFileCache,
46 MetadataHandler metadataHandler,
47 FileOutErr fileOutErr,
48 Map<String, String> clientEnv,
Michael Thvedt434e68e2016-02-09 00:57:46 +000049 @Nullable ArtifactExpander artifactExpander,
Janak Ramakrishnan29c5ab42015-05-14 19:38:12 +000050 @Nullable SkyFunction.Environment env) {
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010051 this.actionInputFileCache = actionInputFileCache;
52 this.metadataHandler = metadataHandler;
53 this.fileOutErr = fileOutErr;
Klaus Aehligd2fcd9d2016-08-26 08:16:25 +000054 this.clientEnv = ImmutableMap.copyOf(clientEnv);
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010055 this.executor = executor;
Michael Thvedt434e68e2016-02-09 00:57:46 +000056 this.artifactExpander = artifactExpander;
Janak Ramakrishnan29c5ab42015-05-14 19:38:12 +000057 this.env = env;
58 }
59
Klaus Aehligd2fcd9d2016-08-26 08:16:25 +000060 public ActionExecutionContext(
61 Executor executor,
62 ActionInputFileCache actionInputFileCache,
63 MetadataHandler metadataHandler,
64 FileOutErr fileOutErr,
65 Map<String, String> clientEnv,
66 ArtifactExpander artifactExpander) {
67 this(
68 executor,
69 actionInputFileCache,
70 metadataHandler,
71 fileOutErr,
72 clientEnv,
73 artifactExpander,
74 null);
Janak Ramakrishnan29c5ab42015-05-14 19:38:12 +000075 }
76
Klaus Aehligd2fcd9d2016-08-26 08:16:25 +000077 public static ActionExecutionContext normal(
78 Executor executor,
79 ActionInputFileCache actionInputFileCache,
80 MetadataHandler metadataHandler,
81 FileOutErr fileOutErr,
82 Map<String, String> clientEnv,
83 ArtifactExpander artifactExpander) {
84 return new ActionExecutionContext(
85 executor,
86 actionInputFileCache,
87 metadataHandler,
88 fileOutErr,
89 clientEnv,
90 artifactExpander,
91 null);
Janak Ramakrishnan29c5ab42015-05-14 19:38:12 +000092 }
93
Klaus Aehligd2fcd9d2016-08-26 08:16:25 +000094 public static ActionExecutionContext forInputDiscovery(
95 Executor executor,
96 ActionInputFileCache actionInputFileCache,
97 MetadataHandler metadataHandler,
98 FileOutErr fileOutErr,
99 Map<String, String> clientEnv,
100 Environment env) {
101 return new ActionExecutionContext(
102 executor, actionInputFileCache, metadataHandler, fileOutErr, clientEnv, null, env);
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100103 }
104
105 public ActionInputFileCache getActionInputFileCache() {
106 return actionInputFileCache;
107 }
108
109 public MetadataHandler getMetadataHandler() {
110 return metadataHandler;
111 }
112
113 public Executor getExecutor() {
114 return executor;
115 }
116
Klaus Aehlig4c10f3f2016-08-26 15:58:48 +0000117 public ImmutableMap<String, String> getClientEnv() {
118 return clientEnv;
119 }
120
Michael Thvedt434e68e2016-02-09 00:57:46 +0000121 public ArtifactExpander getArtifactExpander() {
122 return artifactExpander;
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100123 }
124
125 /**
126 * Provide that {@code FileOutErr} that the action should use for redirecting the output and error
127 * stream.
128 */
129 public FileOutErr getFileOutErr() {
130 return fileOutErr;
131 }
132
133 /**
Janak Ramakrishnan29c5ab42015-05-14 19:38:12 +0000134 * Provides a mechanism for the action to request values from Skyframe while it discovers inputs.
135 */
136 public Environment getEnvironmentForDiscoveringInputs() {
137 return Preconditions.checkNotNull(env);
138 }
139
ulfjack1a328e32017-04-06 10:25:16 +0000140 @Override
141 public void close() throws IOException {
142 fileOutErr.close();
143 }
144
Janak Ramakrishnan29c5ab42015-05-14 19:38:12 +0000145 /**
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100146 * Allows us to create a new context that overrides the FileOutErr with another one. This is
147 * useful for muting the output for example.
148 */
149 public ActionExecutionContext withFileOutErr(FileOutErr fileOutErr) {
Klaus Aehligd2fcd9d2016-08-26 08:16:25 +0000150 return new ActionExecutionContext(
151 executor,
152 actionInputFileCache,
153 metadataHandler,
154 fileOutErr,
155 clientEnv,
156 artifactExpander,
157 env);
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100158 }
159}