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 | |
| 17 | import com.google.common.collect.Iterables; |
kush | 8ae7a3b | 2018-06-29 15:43:11 -0700 | [diff] [blame] | 18 | import com.google.devtools.build.lib.actions.AbstractAction; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 19 | import com.google.devtools.build.lib.actions.ActionExecutionContext; |
| 20 | import com.google.devtools.build.lib.actions.EnvironmentalExecException; |
Benjamin Peterson | 757e481 | 2019-03-21 03:16:51 -0700 | [diff] [blame] | 21 | import com.google.devtools.build.lib.actions.RunningActionEvent; |
ulfjack | a0da434 | 2019-04-05 04:26:31 -0700 | [diff] [blame] | 22 | import com.google.devtools.build.lib.actions.SpawnContinuation; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 23 | import com.google.devtools.build.lib.analysis.actions.AbstractFileWriteAction; |
jcater | 3751ba9 | 2020-04-06 07:04:30 -0700 | [diff] [blame] | 24 | import com.google.devtools.build.lib.analysis.actions.DeterministicWriter; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 25 | import com.google.devtools.build.lib.analysis.actions.FileWriteActionContext; |
Nathan Harmata | 43e22a8 | 2015-09-08 21:21:04 +0000 | [diff] [blame] | 26 | import com.google.devtools.build.lib.profiler.AutoProfiler; |
janakr | 30e9915 | 2020-04-03 08:01:25 -0700 | [diff] [blame] | 27 | import com.google.devtools.build.lib.profiler.GoogleAutoProfilerUtils; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 28 | import com.google.devtools.build.lib.vfs.Path; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 29 | import java.io.BufferedOutputStream; |
| 30 | import java.io.IOException; |
| 31 | import java.io.OutputStream; |
janakr | 30e9915 | 2020-04-03 08:01:25 -0700 | [diff] [blame] | 32 | import java.time.Duration; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 33 | |
| 34 | /** |
| 35 | * A strategy for executing an {@link AbstractFileWriteAction}. |
| 36 | */ |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 37 | public final class FileWriteStrategy implements FileWriteActionContext { |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 38 | public static final Class<FileWriteStrategy> TYPE = FileWriteStrategy.class; |
janakr | 30e9915 | 2020-04-03 08:01:25 -0700 | [diff] [blame] | 39 | private static final Duration MIN_LOGGING = Duration.ofMillis(100); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 40 | |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 41 | @Override |
ulfjack | a0da434 | 2019-04-05 04:26:31 -0700 | [diff] [blame] | 42 | public SpawnContinuation beginWriteOutputToFile( |
kush | 8ae7a3b | 2018-06-29 15:43:11 -0700 | [diff] [blame] | 43 | AbstractAction action, |
| 44 | ActionExecutionContext actionExecutionContext, |
| 45 | DeterministicWriter deterministicWriter, |
ulfjack | a0da434 | 2019-04-05 04:26:31 -0700 | [diff] [blame] | 46 | boolean makeExecutable, |
ulfjack | 415165a | 2019-09-12 07:56:40 -0700 | [diff] [blame] | 47 | boolean isRemotable) { |
jmmv | 716e19c | 2019-04-01 07:59:54 -0700 | [diff] [blame] | 48 | actionExecutionContext.getEventHandler().post(new RunningActionEvent(action, "local")); |
Ulf Adams | cddaaa6 | 2017-03-02 17:32:28 +0000 | [diff] [blame] | 49 | // 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] | 50 | try (AutoProfiler p = |
janakr | 30e9915 | 2020-04-03 08:01:25 -0700 | [diff] [blame] | 51 | GoogleAutoProfilerUtils.logged( |
| 52 | "running write for action " + action.prettyPrint(), MIN_LOGGING)) { |
Benjamin Peterson | 757e481 | 2019-03-21 03:16:51 -0700 | [diff] [blame] | 53 | Path outputPath = |
| 54 | actionExecutionContext.getInputPath(Iterables.getOnlyElement(action.getOutputs())); |
Nathan Harmata | 43e22a8 | 2015-09-08 21:21:04 +0000 | [diff] [blame] | 55 | try { |
Nathan Harmata | 43e22a8 | 2015-09-08 21:21:04 +0000 | [diff] [blame] | 56 | try (OutputStream out = new BufferedOutputStream(outputPath.getOutputStream())) { |
kush | 8ae7a3b | 2018-06-29 15:43:11 -0700 | [diff] [blame] | 57 | deterministicWriter.writeOutputFile(out); |
Nathan Harmata | 43e22a8 | 2015-09-08 21:21:04 +0000 | [diff] [blame] | 58 | } |
kush | 8ae7a3b | 2018-06-29 15:43:11 -0700 | [diff] [blame] | 59 | if (makeExecutable) { |
Nathan Harmata | 43e22a8 | 2015-09-08 21:21:04 +0000 | [diff] [blame] | 60 | outputPath.setExecutable(true); |
| 61 | } |
| 62 | } catch (IOException e) { |
ulfjack | 415165a | 2019-09-12 07:56:40 -0700 | [diff] [blame] | 63 | return SpawnContinuation.failedWithExecException(new EnvironmentalExecException(e)); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 64 | } |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 65 | } |
ulfjack | a0da434 | 2019-04-05 04:26:31 -0700 | [diff] [blame] | 66 | return SpawnContinuation.immediate(); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 67 | } |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 68 | } |