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 | |
Klaus Aehlig | d2fcd9d | 2016-08-26 08:16:25 +0000 | [diff] [blame] | 17 | import com.google.common.collect.ImmutableMap; |
Michael Thvedt | 434e68e | 2016-02-09 00:57:46 +0000 | [diff] [blame] | 18 | import com.google.devtools.build.lib.actions.Artifact.ArtifactExpander; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 19 | import com.google.devtools.build.lib.actions.cache.MetadataHandler; |
Mark Schaller | 6df8179 | 2015-12-10 18:47:47 +0000 | [diff] [blame] | 20 | import com.google.devtools.build.lib.util.Preconditions; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 21 | import com.google.devtools.build.lib.util.io.FileOutErr; |
Janak Ramakrishnan | 29c5ab4 | 2015-05-14 19:38:12 +0000 | [diff] [blame] | 22 | import com.google.devtools.build.skyframe.SkyFunction; |
| 23 | import com.google.devtools.build.skyframe.SkyFunction.Environment; |
ulfjack | 1a328e3 | 2017-04-06 10:25:16 +0000 | [diff] [blame^] | 24 | import java.io.Closeable; |
| 25 | import java.io.IOException; |
Klaus Aehlig | d2fcd9d | 2016-08-26 08:16:25 +0000 | [diff] [blame] | 26 | import java.util.Map; |
Janak Ramakrishnan | 29c5ab4 | 2015-05-14 19:38:12 +0000 | [diff] [blame] | 27 | import javax.annotation.Nullable; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 28 | |
| 29 | /** |
| 30 | * A class that groups services in the scope of the action. Like the FileOutErr object. |
| 31 | */ |
ulfjack | 1a328e3 | 2017-04-06 10:25:16 +0000 | [diff] [blame^] | 32 | public class ActionExecutionContext implements Closeable { |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 33 | |
| 34 | private final Executor executor; |
| 35 | private final ActionInputFileCache actionInputFileCache; |
| 36 | private final MetadataHandler metadataHandler; |
| 37 | private final FileOutErr fileOutErr; |
Klaus Aehlig | d2fcd9d | 2016-08-26 08:16:25 +0000 | [diff] [blame] | 38 | private final ImmutableMap<String, String> clientEnv; |
Michael Thvedt | 434e68e | 2016-02-09 00:57:46 +0000 | [diff] [blame] | 39 | private final ArtifactExpander artifactExpander; |
Janak Ramakrishnan | 29c5ab4 | 2015-05-14 19:38:12 +0000 | [diff] [blame] | 40 | @Nullable |
| 41 | private final Environment env; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 42 | |
Klaus Aehlig | d2fcd9d | 2016-08-26 08:16:25 +0000 | [diff] [blame] | 43 | private ActionExecutionContext( |
| 44 | Executor executor, |
| 45 | ActionInputFileCache actionInputFileCache, |
| 46 | MetadataHandler metadataHandler, |
| 47 | FileOutErr fileOutErr, |
| 48 | Map<String, String> clientEnv, |
Michael Thvedt | 434e68e | 2016-02-09 00:57:46 +0000 | [diff] [blame] | 49 | @Nullable ArtifactExpander artifactExpander, |
Janak Ramakrishnan | 29c5ab4 | 2015-05-14 19:38:12 +0000 | [diff] [blame] | 50 | @Nullable SkyFunction.Environment env) { |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 51 | this.actionInputFileCache = actionInputFileCache; |
| 52 | this.metadataHandler = metadataHandler; |
| 53 | this.fileOutErr = fileOutErr; |
Klaus Aehlig | d2fcd9d | 2016-08-26 08:16:25 +0000 | [diff] [blame] | 54 | this.clientEnv = ImmutableMap.copyOf(clientEnv); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 55 | this.executor = executor; |
Michael Thvedt | 434e68e | 2016-02-09 00:57:46 +0000 | [diff] [blame] | 56 | this.artifactExpander = artifactExpander; |
Janak Ramakrishnan | 29c5ab4 | 2015-05-14 19:38:12 +0000 | [diff] [blame] | 57 | this.env = env; |
| 58 | } |
| 59 | |
Klaus Aehlig | d2fcd9d | 2016-08-26 08:16:25 +0000 | [diff] [blame] | 60 | 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 Ramakrishnan | 29c5ab4 | 2015-05-14 19:38:12 +0000 | [diff] [blame] | 75 | } |
| 76 | |
Klaus Aehlig | d2fcd9d | 2016-08-26 08:16:25 +0000 | [diff] [blame] | 77 | 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 Ramakrishnan | 29c5ab4 | 2015-05-14 19:38:12 +0000 | [diff] [blame] | 92 | } |
| 93 | |
Klaus Aehlig | d2fcd9d | 2016-08-26 08:16:25 +0000 | [diff] [blame] | 94 | 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 Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 103 | } |
| 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 Aehlig | 4c10f3f | 2016-08-26 15:58:48 +0000 | [diff] [blame] | 117 | public ImmutableMap<String, String> getClientEnv() { |
| 118 | return clientEnv; |
| 119 | } |
| 120 | |
Michael Thvedt | 434e68e | 2016-02-09 00:57:46 +0000 | [diff] [blame] | 121 | public ArtifactExpander getArtifactExpander() { |
| 122 | return artifactExpander; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 123 | } |
| 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 Ramakrishnan | 29c5ab4 | 2015-05-14 19:38:12 +0000 | [diff] [blame] | 134 | * 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 | |
ulfjack | 1a328e3 | 2017-04-06 10:25:16 +0000 | [diff] [blame^] | 140 | @Override |
| 141 | public void close() throws IOException { |
| 142 | fileOutErr.close(); |
| 143 | } |
| 144 | |
Janak Ramakrishnan | 29c5ab4 | 2015-05-14 19:38:12 +0000 | [diff] [blame] | 145 | /** |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 146 | * 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 Aehlig | d2fcd9d | 2016-08-26 08:16:25 +0000 | [diff] [blame] | 150 | return new ActionExecutionContext( |
| 151 | executor, |
| 152 | actionInputFileCache, |
| 153 | metadataHandler, |
| 154 | fileOutErr, |
| 155 | clientEnv, |
| 156 | artifactExpander, |
| 157 | env); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 158 | } |
| 159 | } |