]> git.lizzy.rs Git - rust.git/blob - src/tools/compiletest/src/procsrv.rs
f55667f93c031939c534cd8d79ced66704d71e91
[rust.git] / src / tools / compiletest / src / procsrv.rs
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.
4 //
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.
10
11 use std::env;
12 use std::ffi::OsString;
13 use std::io::prelude::*;
14 use std::io;
15 use std::path::PathBuf;
16 use std::process::{Child, Command, ExitStatus, Output, Stdio};
17
18 pub fn dylib_env_var() -> &'static str {
19     if cfg!(windows) {
20         "PATH"
21     } else if cfg!(target_os = "macos") {
22         "DYLD_LIBRARY_PATH"
23     } else {
24         "LD_LIBRARY_PATH"
25     }
26 }
27
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()))
33         .collect::<Vec<_>>();
34     if let Some(p) = aux_path {
35         path.insert(0, PathBuf::from(p))
36     }
37     path.insert(0, PathBuf::from(lib_path));
38
39     // Add the new dylib search path var
40     let newpath = env::join_paths(&path).unwrap();
41     cmd.env(var, newpath);
42 }
43
44 pub struct Result {
45     pub status: ExitStatus,
46     pub out: String,
47     pub err: String,
48 }
49
50 pub fn run(lib_path: &str,
51            prog: &str,
52            aux_path: Option<&str>,
53            args: &[String],
54            env: Vec<(String, String)>,
55            input: Option<String>)
56            -> io::Result<Result> {
57
58     let mut cmd = Command::new(prog);
59     cmd.args(args)
60         .stdout(Stdio::piped())
61         .stderr(Stdio::piped());
62
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!
70     //
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());
76     } else {
77         cmd.stdin(Stdio::inherit());
78     }
79
80     add_target_env(&mut cmd, lib_path, aux_path);
81     for (key, val) in env {
82         cmd.env(&key, &val);
83     }
84
85     let mut process = cmd.spawn()?;
86     if let Some(input) = input {
87         process.stdin.as_mut().unwrap().write_all(input.as_bytes()).unwrap();
88     }
89     let Output { status, stdout, stderr } = process.wait_with_output().unwrap();
90
91     Ok(Result {
92         status: status,
93         out: String::from_utf8(stdout).unwrap(),
94         err: String::from_utf8(stderr).unwrap(),
95     })
96 }
97
98 pub fn run_background(lib_path: &str,
99                       prog: &str,
100                       aux_path: Option<&str>,
101                       args: &[String],
102                       env: Vec<(String, String)>,
103                       input: Option<String>)
104                       -> io::Result<Child> {
105
106     let mut cmd = Command::new(prog);
107     cmd.args(args)
108        .stdin(Stdio::piped())
109        .stdout(Stdio::piped());
110     add_target_env(&mut cmd, lib_path, aux_path);
111     for (key, val) in env {
112         cmd.env(&key, &val);
113     }
114
115     let mut process = cmd.spawn()?;
116     if let Some(input) = input {
117         process.stdin.as_mut().unwrap().write_all(input.as_bytes()).unwrap();
118     }
119
120     Ok(process)
121 }