1 // Copyright 2012 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
12 use std::ffi::OsString;
13 use std::io::prelude::*;
15 use std::path::PathBuf;
16 use std::process::{Child, Command, ExitStatus, Output, Stdio};
18 pub fn dylib_env_var() -> &'static str {
21 } else if cfg!(target_os = "macos") {
28 fn add_target_env(cmd: &mut Command, lib_path: &str, aux_path: Option<&str>) {
29 // Need to be sure to put both the lib_path and the aux path in the dylib
30 // search path for the child.
31 let var = dylib_env_var();
32 let mut path = env::split_paths(&env::var_os(var).unwrap_or(OsString::new()))
34 if let Some(p) = aux_path {
35 path.insert(0, PathBuf::from(p))
37 path.insert(0, PathBuf::from(lib_path));
39 // Add the new dylib search path var
40 let newpath = env::join_paths(&path).unwrap();
41 cmd.env(var, newpath);
45 pub status: ExitStatus,
50 pub fn run(lib_path: &str,
52 aux_path: Option<&str>,
54 env: Vec<(String, String)>,
55 input: Option<String>)
56 -> io::Result<Result> {
58 let mut cmd = Command::new(prog);
60 .stdout(Stdio::piped())
61 .stderr(Stdio::piped());
63 // Why oh why do we sometimes make a pipe and sometimes inherit the stdin
64 // stream, well that's an excellent question! In theory it should suffice to
65 // always create a pipe here and be done with it. Unfortunately though
66 // there's apparently something odd with the gdb that comes with gcc 6.3.0
67 // on MinGW. Tracked at rust-lang/rust#40184 when stdin is piped here
68 // (unconditionally) then all gdb tests will fail on MinGW when using gcc
69 // 6.3.0. WHen using an inherited stdin though they happen to all work!
71 // As to why this fixes the issue, well, I have no idea. If you can remove
72 // this branch and unconditionally use `piped` and it gets past @bors please
73 // feel free to send a PR!
74 if input.is_some() || !cfg!(windows) {
75 cmd.stdin(Stdio::piped());
77 cmd.stdin(Stdio::inherit());
80 add_target_env(&mut cmd, lib_path, aux_path);
81 for (key, val) in env {
85 let mut process = cmd.spawn()?;
86 if let Some(input) = input {
87 process.stdin.as_mut().unwrap().write_all(input.as_bytes()).unwrap();
89 let Output { status, stdout, stderr } = process.wait_with_output().unwrap();
93 out: String::from_utf8(stdout).unwrap(),
94 err: String::from_utf8(stderr).unwrap(),
98 pub fn run_background(lib_path: &str,
100 aux_path: Option<&str>,
102 env: Vec<(String, String)>,
103 input: Option<String>)
104 -> io::Result<Child> {
106 let mut cmd = Command::new(prog);
108 .stdin(Stdio::piped())
109 .stdout(Stdio::piped());
110 add_target_env(&mut cmd, lib_path, aux_path);
111 for (key, val) in env {
115 let mut process = cmd.spawn()?;
116 if let Some(input) = input {
117 process.stdin.as_mut().unwrap().write_all(input.as_bytes()).unwrap();