]> git.proxmox.com Git - rustc.git/blob - src/compiletest/procsrv.rs
Imported Upstream version 1.9.0+dfsg1
[rustc.git] / src / compiletest / 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::path::PathBuf;
15 use std::process::{ExitStatus, Command, Child, Output, Stdio};
16
17 fn add_target_env(cmd: &mut Command, lib_path: &str, aux_path: Option<&str>) {
18 // Need to be sure to put both the lib_path and the aux path in the dylib
19 // search path for the child.
20 let var = if cfg!(windows) {
21 "PATH"
22 } else if cfg!(target_os = "macos") {
23 "DYLD_LIBRARY_PATH"
24 } else {
25 "LD_LIBRARY_PATH"
26 };
27 let mut path = env::split_paths(&env::var_os(var).unwrap_or(OsString::new()))
28 .collect::<Vec<_>>();
29 if let Some(p) = aux_path {
30 path.insert(0, PathBuf::from(p))
31 }
32 path.insert(0, PathBuf::from(lib_path));
33
34 // Add the new dylib search path var
35 let newpath = env::join_paths(&path).unwrap();
36 cmd.env(var, newpath);
37 }
38
39 pub struct Result {pub status: ExitStatus, pub out: String, pub err: String}
40
41 pub fn run(lib_path: &str,
42 prog: &str,
43 aux_path: Option<&str>,
44 args: &[String],
45 env: Vec<(String, String)> ,
46 input: Option<String>) -> Option<Result> {
47
48 let mut cmd = Command::new(prog);
49 cmd.args(args)
50 .stdin(Stdio::piped())
51 .stdout(Stdio::piped())
52 .stderr(Stdio::piped());
53 add_target_env(&mut cmd, lib_path, aux_path);
54 for (key, val) in env {
55 cmd.env(&key, &val);
56 }
57
58 match cmd.spawn() {
59 Ok(mut process) => {
60 if let Some(input) = input {
61 process.stdin.as_mut().unwrap().write_all(input.as_bytes()).unwrap();
62 }
63 let Output { status, stdout, stderr } =
64 process.wait_with_output().unwrap();
65
66 Some(Result {
67 status: status,
68 out: String::from_utf8(stdout).unwrap(),
69 err: String::from_utf8(stderr).unwrap()
70 })
71 },
72 Err(..) => None
73 }
74 }
75
76 pub fn run_background(lib_path: &str,
77 prog: &str,
78 aux_path: Option<&str>,
79 args: &[String],
80 env: Vec<(String, String)> ,
81 input: Option<String>) -> Option<Child> {
82
83 let mut cmd = Command::new(prog);
84 cmd.args(args)
85 .stdin(Stdio::piped())
86 .stdout(Stdio::piped())
87 .stderr(Stdio::piped());
88 add_target_env(&mut cmd, lib_path, aux_path);
89 for (key, val) in env {
90 cmd.env(&key, &val);
91 }
92
93 match cmd.spawn() {
94 Ok(mut process) => {
95 if let Some(input) = input {
96 process.stdin.as_mut().unwrap().write_all(input.as_bytes()).unwrap();
97 }
98
99 Some(process)
100 },
101 Err(..) => None
102 }
103 }