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.
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.
13 use std::io::process::{ProcessExit, Process, ProcessConfig, ProcessOutput};
15 #[cfg(target_os = "win32")]
16 fn target_env(lib_path: &str, prog: &str) -> ~[(~str,~str)] {
18 let mut env = os::env();
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";
24 env = env.map(|pair| {
25 let (k,v) = (*pair).clone();
26 if k == ~"PATH" { (~"PATH", v + ";" + lib_path + ";" + aux_path) }
29 if prog.ends_with("rustc.exe") {
30 env.push((~"RUST_THREADS", ~"1"));
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";
42 let mut env = os::env();
43 let var = if cfg!(target_os = "macos") {
48 let prev = match env.iter().position(|&(ref k, _)| k.as_slice() == var) {
49 Some(i) => env.remove(i).unwrap().val1(),
52 env.push((var.to_owned(), if prev.is_empty() {
53 lib_path + ":" + aux_path
55 lib_path + ":" + aux_path + ":" + prev
60 pub struct Result {status: ProcessExit, out: ~str, err: ~str}
62 pub fn run(lib_path: &str,
66 input: Option<~str>) -> Option<Result> {
68 let env = env + target_env(lib_path, prog);
69 let mut opt_process = Process::configure(ProcessConfig {
72 env: Some(env.as_slice()),
73 .. ProcessConfig::new()
77 Ok(ref mut process) => {
78 for input in input.iter() {
79 process.stdin.get_mut_ref().write(input.as_bytes()).unwrap();
81 let ProcessOutput { status, output, error } = process.wait_with_output();
85 out: str::from_utf8_owned(output).unwrap(),
86 err: str::from_utf8_owned(error).unwrap()
93 pub fn run_background(lib_path: &str,
97 input: Option<~str>) -> Option<Process> {
99 let env = env + target_env(lib_path, prog);
100 let opt_process = Process::configure(ProcessConfig {
103 env: Some(env.as_slice()),
104 .. ProcessConfig::new()
109 for input in input.iter() {
110 process.stdin.get_mut_ref().write(input.as_bytes()).unwrap();