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) -> Vec<(~str, ~str)> {
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";
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)
31 if prog.ends_with("rustc.exe") {
32 new_env.push((~"RUST_THREADS", ~"1"));
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";
44 let mut env: Vec<(~str,~str)> = os::env().move_iter().collect();
45 let var = if cfg!(target_os = "macos") {
50 let prev = match env.iter().position(|&(ref k, _)| k.as_slice() == var) {
51 Some(i) => env.remove(i).unwrap().val1(),
54 env.push((var.to_owned(), if prev.is_empty() {
55 lib_path + ":" + aux_path
57 lib_path + ":" + aux_path + ":" + prev
62 pub struct Result {pub status: ProcessExit, pub out: ~str, pub err: ~str}
64 pub fn run(lib_path: &str,
67 env: Vec<(~str, ~str)> ,
68 input: Option<~str>) -> Option<Result> {
70 let env = env.clone().append(target_env(lib_path, prog).as_slice());
71 let mut opt_process = Process::configure(ProcessConfig {
74 env: Some(env.as_slice()),
75 .. ProcessConfig::new()
79 Ok(ref mut process) => {
80 for input in input.iter() {
81 process.stdin.get_mut_ref().write(input.as_bytes()).unwrap();
83 let ProcessOutput { status, output, error } = process.wait_with_output();
87 out: str::from_utf8(output.as_slice()).unwrap().to_owned(),
88 err: str::from_utf8(error.as_slice()).unwrap().to_owned()
95 pub fn run_background(lib_path: &str,
98 env: Vec<(~str, ~str)> ,
99 input: Option<~str>) -> Option<Process> {
101 let env = env.clone().append(target_env(lib_path, prog).as_slice());
102 let opt_process = Process::configure(ProcessConfig {
105 env: Some(env.as_slice()),
106 .. ProcessConfig::new()
111 for input in input.iter() {
112 process.stdin.get_mut_ref().write(input.as_bytes()).unwrap();