Brian Silverman | cc09f18 | 2022-03-09 15:40:20 -0800 | [diff] [blame^] | 1 | // Copyright 2020 The Bazel Authors. All rights reserved. |
| 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 | mod flags; |
| 16 | mod options; |
| 17 | mod util; |
| 18 | |
| 19 | use std::fs::{copy, OpenOptions}; |
| 20 | use std::process::{exit, Command, Stdio}; |
| 21 | |
| 22 | use crate::options::options; |
| 23 | |
| 24 | fn main() { |
| 25 | let opts = match options() { |
| 26 | Err(err) => panic!("process wrapper error: {}", err), |
| 27 | Ok(v) => v, |
| 28 | }; |
| 29 | let stdout = if let Some(stdout_file) = opts.stdout_file { |
| 30 | OpenOptions::new() |
| 31 | .create(true) |
| 32 | .truncate(true) |
| 33 | .write(true) |
| 34 | .open(stdout_file) |
| 35 | .expect("process wrapper error: unable to open stdout file") |
| 36 | .into() |
| 37 | } else { |
| 38 | Stdio::inherit() |
| 39 | }; |
| 40 | let stderr = if let Some(stderr_file) = opts.stderr_file { |
| 41 | OpenOptions::new() |
| 42 | .create(true) |
| 43 | .truncate(true) |
| 44 | .write(true) |
| 45 | .open(stderr_file) |
| 46 | .expect("process wrapper error: unable to open stderr file") |
| 47 | .into() |
| 48 | } else { |
| 49 | Stdio::inherit() |
| 50 | }; |
| 51 | let status = Command::new(opts.executable) |
| 52 | .args(opts.child_arguments) |
| 53 | .env_clear() |
| 54 | .envs(opts.child_environment) |
| 55 | .stdout(stdout) |
| 56 | .stderr(stderr) |
| 57 | .status() |
| 58 | .expect("process wrapper error: failed to spawn child process"); |
| 59 | |
| 60 | if status.success() { |
| 61 | if let Some(tf) = opts.touch_file { |
| 62 | OpenOptions::new() |
| 63 | .create(true) |
| 64 | .write(true) |
| 65 | .open(tf) |
| 66 | .expect("process wrapper error: failed to create touch file"); |
| 67 | } |
| 68 | if let Some((copy_source, copy_dest)) = opts.copy_output { |
| 69 | copy(©_source, ©_dest).unwrap_or_else(|_| { |
| 70 | panic!( |
| 71 | "process wrapper error: failed to copy {} into {}", |
| 72 | copy_source, copy_dest |
| 73 | ) |
| 74 | }); |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | exit(status.code().unwrap()) |
| 79 | } |