]> git.lizzy.rs Git - rust.git/blob - src/compiletest/procsrv.rs
auto merge of #13600 : brandonw/rust/master, r=brson
[rust.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::os;
12 use std::str;
13 use std::io::process::{ProcessExit, Process, ProcessConfig, ProcessOutput};
14
15 #[cfg(target_os = "win32")]
16 fn target_env(lib_path: &str, prog: &str) -> Vec<(~str, ~str)> {
17     let env = os::env();
18
19     // Make sure we include the aux directory in the path
20     assert!(prog.ends_with(".exe"));
21     let aux_path = prog.slice(0u, prog.len() - 4u).to_owned() + ".libaux";
22
23     let mut new_env: Vec<_> = env.move_iter().map(|(k, v)| {
24         let new_v = if "PATH" == k {
25             format!("{};{};{}", v, lib_path, aux_path)
26         } else {
27             v
28         };
29         (k, new_v)
30     }).collect();
31     if prog.ends_with("rustc.exe") {
32         new_env.push((~"RUST_THREADS", ~"1"));
33     }
34     return new_env;
35 }
36
37 #[cfg(target_os = "linux")]
38 #[cfg(target_os = "macos")]
39 #[cfg(target_os = "freebsd")]
40 fn target_env(lib_path: &str, prog: &str) -> Vec<(~str,~str)> {
41     // Make sure we include the aux directory in the path
42     let aux_path = prog + ".libaux";
43
44     let mut env: Vec<(~str,~str)> = os::env().move_iter().collect();
45     let var = if cfg!(target_os = "macos") {
46         "DYLD_LIBRARY_PATH"
47     } else {
48         "LD_LIBRARY_PATH"
49     };
50     let prev = match env.iter().position(|&(ref k, _)| k.as_slice() == var) {
51         Some(i) => env.remove(i).unwrap().val1(),
52         None => ~"",
53     };
54     env.push((var.to_owned(), if prev.is_empty() {
55         lib_path + ":" + aux_path
56     } else {
57         lib_path + ":" + aux_path + ":" + prev
58     }));
59     return env;
60 }
61
62 pub struct Result {pub status: ProcessExit, pub out: ~str, pub err: ~str}
63
64 pub fn run(lib_path: &str,
65            prog: &str,
66            args: &[~str],
67            env: Vec<(~str, ~str)> ,
68            input: Option<~str>) -> Option<Result> {
69
70     let env = env.clone().append(target_env(lib_path, prog).as_slice());
71     let mut opt_process = Process::configure(ProcessConfig {
72         program: prog,
73         args: args,
74         env: Some(env.as_slice()),
75         .. ProcessConfig::new()
76     });
77
78     match opt_process {
79         Ok(ref mut process) => {
80             for input in input.iter() {
81                 process.stdin.get_mut_ref().write(input.as_bytes()).unwrap();
82             }
83             let ProcessOutput { status, output, error } = process.wait_with_output();
84
85             Some(Result {
86                 status: status,
87                 out: str::from_utf8(output.as_slice()).unwrap().to_owned(),
88                 err: str::from_utf8(error.as_slice()).unwrap().to_owned()
89             })
90         },
91         Err(..) => None
92     }
93 }
94
95 pub fn run_background(lib_path: &str,
96            prog: &str,
97            args: &[~str],
98            env: Vec<(~str, ~str)> ,
99            input: Option<~str>) -> Option<Process> {
100
101     let env = env.clone().append(target_env(lib_path, prog).as_slice());
102     let opt_process = Process::configure(ProcessConfig {
103         program: prog,
104         args: args,
105         env: Some(env.as_slice()),
106         .. ProcessConfig::new()
107     });
108
109     match opt_process {
110         Ok(mut process) => {
111             for input in input.iter() {
112                 process.stdin.get_mut_ref().write(input.as_bytes()).unwrap();
113             }
114
115             Some(process)
116         },
117         Err(..) => None
118     }
119 }