blob: 0631a46a438d04978296464c07240504651b6f34 [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.exec;
16
ruperts7967f332017-11-21 16:37:13 -080017import com.google.common.collect.ImmutableList;
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010018import com.google.common.collect.Iterables;
kush8ae7a3b2018-06-29 15:43:11 -070019import com.google.devtools.build.lib.actions.AbstractAction;
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010020import com.google.devtools.build.lib.actions.ActionExecutionContext;
21import com.google.devtools.build.lib.actions.EnvironmentalExecException;
22import com.google.devtools.build.lib.actions.ExecException;
23import com.google.devtools.build.lib.actions.ExecutionStrategy;
Benjamin Peterson757e4812019-03-21 03:16:51 -070024import com.google.devtools.build.lib.actions.RunningActionEvent;
ruperts4050a892017-10-07 00:46:20 +020025import com.google.devtools.build.lib.actions.SpawnResult;
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010026import com.google.devtools.build.lib.analysis.actions.AbstractFileWriteAction;
kush8ae7a3b2018-06-29 15:43:11 -070027import com.google.devtools.build.lib.analysis.actions.AbstractFileWriteAction.DeterministicWriter;
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010028import com.google.devtools.build.lib.analysis.actions.FileWriteActionContext;
Nathan Harmata43e22a82015-09-08 21:21:04 +000029import com.google.devtools.build.lib.profiler.AutoProfiler;
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010030import com.google.devtools.build.lib.vfs.Path;
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010031import java.io.BufferedOutputStream;
32import java.io.IOException;
33import java.io.OutputStream;
ruperts7967f332017-11-21 16:37:13 -080034import java.util.List;
Nathan Harmata43e22a82015-09-08 21:21:04 +000035import java.util.logging.Logger;
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010036
37/**
38 * A strategy for executing an {@link AbstractFileWriteAction}.
39 */
40@ExecutionStrategy(name = { "local" }, contextType = FileWriteActionContext.class)
41public final class FileWriteStrategy implements FileWriteActionContext {
lberki97abb522017-09-04 18:51:57 +020042 private static final Logger logger = Logger.getLogger(FileWriteStrategy.class.getName());
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010043 public static final Class<FileWriteStrategy> TYPE = FileWriteStrategy.class;
44
45 public FileWriteStrategy() {
46 }
47
48 @Override
kush8ae7a3b2018-06-29 15:43:11 -070049 public List<SpawnResult> writeOutputToFile(
50 AbstractAction action,
51 ActionExecutionContext actionExecutionContext,
52 DeterministicWriter deterministicWriter,
53 boolean makeExecutable, boolean isRemotable)
54 throws ExecException {
Benjamin Peterson757e4812019-03-21 03:16:51 -070055 actionExecutionContext.getEventHandler().post(new RunningActionEvent(action, null));
Ulf Adamscddaaa62017-03-02 17:32:28 +000056 // TODO(ulfjack): Consider acquiring local resources here before trying to write the file.
Janak Ramakrishnand41017b2016-01-30 01:21:57 +000057 try (AutoProfiler p =
lberki97abb522017-09-04 18:51:57 +020058 AutoProfiler.logged(
kush8ae7a3b2018-06-29 15:43:11 -070059 "running write for action " + action.prettyPrint(),
60 logger,
61 /*minTimeForLoggingInMilliseconds=*/ 100)) {
Benjamin Peterson757e4812019-03-21 03:16:51 -070062 Path outputPath =
63 actionExecutionContext.getInputPath(Iterables.getOnlyElement(action.getOutputs()));
Nathan Harmata43e22a82015-09-08 21:21:04 +000064 try {
Nathan Harmata43e22a82015-09-08 21:21:04 +000065 try (OutputStream out = new BufferedOutputStream(outputPath.getOutputStream())) {
kush8ae7a3b2018-06-29 15:43:11 -070066 deterministicWriter.writeOutputFile(out);
Nathan Harmata43e22a82015-09-08 21:21:04 +000067 }
kush8ae7a3b2018-06-29 15:43:11 -070068 if (makeExecutable) {
Nathan Harmata43e22a82015-09-08 21:21:04 +000069 outputPath.setExecutable(true);
70 }
71 } catch (IOException e) {
kush8ae7a3b2018-06-29 15:43:11 -070072 throw new EnvironmentalExecException("IOException during file write", e);
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010073 }
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010074 }
ruperts7967f332017-11-21 16:37:13 -080075 return ImmutableList.of();
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010076 }
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010077}