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