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.exec; |
| 16 | |
ruperts | 7967f33 | 2017-11-21 16:37:13 -0800 | [diff] [blame] | 17 | import com.google.common.collect.ImmutableList; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 18 | import com.google.common.collect.Iterables; |
kush | 8ae7a3b | 2018-06-29 15:43:11 -0700 | [diff] [blame] | 19 | import com.google.devtools.build.lib.actions.AbstractAction; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 20 | import com.google.devtools.build.lib.actions.ActionExecutionContext; |
| 21 | import com.google.devtools.build.lib.actions.EnvironmentalExecException; |
| 22 | import com.google.devtools.build.lib.actions.ExecException; |
| 23 | import com.google.devtools.build.lib.actions.ExecutionStrategy; |
Benjamin Peterson | 757e481 | 2019-03-21 03:16:51 -0700 | [diff] [blame] | 24 | import com.google.devtools.build.lib.actions.RunningActionEvent; |
ruperts | 4050a89 | 2017-10-07 00:46:20 +0200 | [diff] [blame] | 25 | import com.google.devtools.build.lib.actions.SpawnResult; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 26 | import com.google.devtools.build.lib.analysis.actions.AbstractFileWriteAction; |
kush | 8ae7a3b | 2018-06-29 15:43:11 -0700 | [diff] [blame] | 27 | import com.google.devtools.build.lib.analysis.actions.AbstractFileWriteAction.DeterministicWriter; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 28 | import com.google.devtools.build.lib.analysis.actions.FileWriteActionContext; |
Nathan Harmata | 43e22a8 | 2015-09-08 21:21:04 +0000 | [diff] [blame] | 29 | import com.google.devtools.build.lib.profiler.AutoProfiler; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 30 | import com.google.devtools.build.lib.vfs.Path; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 31 | import java.io.BufferedOutputStream; |
| 32 | import java.io.IOException; |
| 33 | import java.io.OutputStream; |
ruperts | 7967f33 | 2017-11-21 16:37:13 -0800 | [diff] [blame] | 34 | import java.util.List; |
Nathan Harmata | 43e22a8 | 2015-09-08 21:21:04 +0000 | [diff] [blame] | 35 | import java.util.logging.Logger; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 36 | |
| 37 | /** |
| 38 | * A strategy for executing an {@link AbstractFileWriteAction}. |
| 39 | */ |
| 40 | @ExecutionStrategy(name = { "local" }, contextType = FileWriteActionContext.class) |
| 41 | public final class FileWriteStrategy implements FileWriteActionContext { |
lberki | 97abb52 | 2017-09-04 18:51:57 +0200 | [diff] [blame] | 42 | private static final Logger logger = Logger.getLogger(FileWriteStrategy.class.getName()); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 43 | public static final Class<FileWriteStrategy> TYPE = FileWriteStrategy.class; |
| 44 | |
| 45 | public FileWriteStrategy() { |
| 46 | } |
| 47 | |
| 48 | @Override |
kush | 8ae7a3b | 2018-06-29 15:43:11 -0700 | [diff] [blame] | 49 | public List<SpawnResult> writeOutputToFile( |
| 50 | AbstractAction action, |
| 51 | ActionExecutionContext actionExecutionContext, |
| 52 | DeterministicWriter deterministicWriter, |
| 53 | boolean makeExecutable, boolean isRemotable) |
| 54 | throws ExecException { |
Benjamin Peterson | 757e481 | 2019-03-21 03:16:51 -0700 | [diff] [blame] | 55 | actionExecutionContext.getEventHandler().post(new RunningActionEvent(action, null)); |
Ulf Adams | cddaaa6 | 2017-03-02 17:32:28 +0000 | [diff] [blame] | 56 | // TODO(ulfjack): Consider acquiring local resources here before trying to write the file. |
Janak Ramakrishnan | d41017b | 2016-01-30 01:21:57 +0000 | [diff] [blame] | 57 | try (AutoProfiler p = |
lberki | 97abb52 | 2017-09-04 18:51:57 +0200 | [diff] [blame] | 58 | AutoProfiler.logged( |
kush | 8ae7a3b | 2018-06-29 15:43:11 -0700 | [diff] [blame] | 59 | "running write for action " + action.prettyPrint(), |
| 60 | logger, |
| 61 | /*minTimeForLoggingInMilliseconds=*/ 100)) { |
Benjamin Peterson | 757e481 | 2019-03-21 03:16:51 -0700 | [diff] [blame] | 62 | Path outputPath = |
| 63 | actionExecutionContext.getInputPath(Iterables.getOnlyElement(action.getOutputs())); |
Nathan Harmata | 43e22a8 | 2015-09-08 21:21:04 +0000 | [diff] [blame] | 64 | try { |
Nathan Harmata | 43e22a8 | 2015-09-08 21:21:04 +0000 | [diff] [blame] | 65 | try (OutputStream out = new BufferedOutputStream(outputPath.getOutputStream())) { |
kush | 8ae7a3b | 2018-06-29 15:43:11 -0700 | [diff] [blame] | 66 | deterministicWriter.writeOutputFile(out); |
Nathan Harmata | 43e22a8 | 2015-09-08 21:21:04 +0000 | [diff] [blame] | 67 | } |
kush | 8ae7a3b | 2018-06-29 15:43:11 -0700 | [diff] [blame] | 68 | if (makeExecutable) { |
Nathan Harmata | 43e22a8 | 2015-09-08 21:21:04 +0000 | [diff] [blame] | 69 | outputPath.setExecutable(true); |
| 70 | } |
| 71 | } catch (IOException e) { |
kush | 8ae7a3b | 2018-06-29 15:43:11 -0700 | [diff] [blame] | 72 | throw new EnvironmentalExecException("IOException during file write", e); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 73 | } |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 74 | } |
ruperts | 7967f33 | 2017-11-21 16:37:13 -0800 | [diff] [blame] | 75 | return ImmutableList.of(); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 76 | } |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 77 | } |