]> git.lizzy.rs Git - rust.git/blob - src/tools/compiletest/src/procsrv.rs
appveyor: Downgrade MinGW to 6.2.0
[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         .stdin(Stdio::piped());
63
64     add_target_env(&mut cmd, lib_path, aux_path);
65     for (key, val) in env {
66         cmd.env(&key, &val);
67     }
68
69     let mut process = cmd.spawn()?;
70     if let Some(input) = input {
71         process.stdin.as_mut().unwrap().write_all(input.as_bytes()).unwrap();
72     }
73     let Output { status, stdout, stderr } = process.wait_with_output().unwrap();
74
75     Ok(Result {
76         status: status,
77         out: String::from_utf8(stdout).unwrap(),
78         err: String::from_utf8(stderr).unwrap(),
79     })
80 }
81
82 pub fn run_background(lib_path: &str,
83                       prog: &str,
84                       aux_path: Option<&str>,
85                       args: &[String],
86                       env: Vec<(String, String)>,
87                       input: Option<String>)
88                       -> io::Result<Child> {
89
90     let mut cmd = Command::new(prog);
91     cmd.args(args)
92        .stdin(Stdio::piped())
93        .stdout(Stdio::piped());
94     add_target_env(&mut cmd, lib_path, aux_path);
95     for (key, val) in env {
96         cmd.env(&key, &val);
97     }
98
99     let mut process = cmd.spawn()?;
100     if let Some(input) = input {
101         process.stdin.as_mut().unwrap().write_all(input.as_bytes()).unwrap();
102     }
103
104     Ok(process)
105 }