2012-12-03 18:48:01 -06:00
|
|
|
// Copyright 2012 The Rust Project Developers. See the COPYRIGHT
|
|
|
|
// file at the top-level directory of this distribution and at
|
|
|
|
// http://rust-lang.org/COPYRIGHT.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
// except according to those terms.
|
|
|
|
|
2013-06-30 21:36:55 -05:00
|
|
|
use std::os;
|
|
|
|
use std::str;
|
2014-02-18 14:04:51 -06:00
|
|
|
use std::io::process::{ProcessExit, Process, ProcessConfig, ProcessOutput};
|
2012-08-03 20:57:43 -05:00
|
|
|
|
2012-02-07 20:55:02 -06:00
|
|
|
#[cfg(target_os = "win32")]
|
2014-04-01 02:10:56 -05:00
|
|
|
fn target_env(lib_path: &str, prog: &str) -> Vec<(~str, ~str)> {
|
|
|
|
let env = os::env();
|
2011-07-30 23:11:14 -05:00
|
|
|
|
2012-06-01 20:24:55 -05:00
|
|
|
// Make sure we include the aux directory in the path
|
2013-05-29 13:10:16 -05:00
|
|
|
assert!(prog.ends_with(".exe"));
|
|
|
|
let aux_path = prog.slice(0u, prog.len() - 4u).to_owned() + ".libaux";
|
2012-06-01 20:24:55 -05:00
|
|
|
|
2014-04-01 02:10:56 -05:00
|
|
|
let mut new_env: Vec<_> = env.move_iter().map(|(k, v)| {
|
|
|
|
let new_v = if "PATH" == k {
|
|
|
|
format!("{};{};{}", v, lib_path, aux_path)
|
|
|
|
} else {
|
|
|
|
v
|
|
|
|
};
|
|
|
|
(k, new_v)
|
|
|
|
}).collect();
|
2013-06-10 10:03:16 -05:00
|
|
|
if prog.ends_with("rustc.exe") {
|
2014-04-15 20:17:48 -05:00
|
|
|
new_env.push(("RUST_THREADS".to_owned(), "1".to_owned()));
|
2012-02-07 20:55:02 -06:00
|
|
|
}
|
2014-04-01 02:10:56 -05:00
|
|
|
return new_env;
|
2011-07-30 23:11:14 -05:00
|
|
|
}
|
|
|
|
|
2012-02-07 20:55:02 -06:00
|
|
|
#[cfg(target_os = "linux")]
|
|
|
|
#[cfg(target_os = "macos")]
|
|
|
|
#[cfg(target_os = "freebsd")]
|
2014-03-05 17:28:08 -06:00
|
|
|
fn target_env(lib_path: &str, prog: &str) -> Vec<(~str,~str)> {
|
2014-02-21 18:32:49 -06:00
|
|
|
// Make sure we include the aux directory in the path
|
|
|
|
let aux_path = prog + ".libaux";
|
|
|
|
|
2014-03-05 17:28:08 -06:00
|
|
|
let mut env: Vec<(~str,~str)> = os::env().move_iter().collect();
|
2014-02-21 18:32:49 -06:00
|
|
|
let var = if cfg!(target_os = "macos") {
|
|
|
|
"DYLD_LIBRARY_PATH"
|
|
|
|
} else {
|
|
|
|
"LD_LIBRARY_PATH"
|
|
|
|
};
|
|
|
|
let prev = match env.iter().position(|&(ref k, _)| k.as_slice() == var) {
|
|
|
|
Some(i) => env.remove(i).unwrap().val1(),
|
2014-04-15 20:17:48 -05:00
|
|
|
None => "".to_owned(),
|
2014-02-21 18:32:49 -06:00
|
|
|
};
|
|
|
|
env.push((var.to_owned(), if prev.is_empty() {
|
|
|
|
lib_path + ":" + aux_path
|
|
|
|
} else {
|
|
|
|
lib_path + ":" + aux_path + ":" + prev
|
|
|
|
}));
|
|
|
|
return env;
|
2011-07-30 23:11:14 -05:00
|
|
|
}
|
|
|
|
|
2014-03-28 13:10:15 -05:00
|
|
|
pub struct Result {pub status: ProcessExit, pub out: ~str, pub err: ~str}
|
2012-02-07 20:55:02 -06:00
|
|
|
|
2013-05-11 21:45:28 -05:00
|
|
|
pub fn run(lib_path: &str,
|
|
|
|
prog: &str,
|
|
|
|
args: &[~str],
|
2014-03-05 17:28:08 -06:00
|
|
|
env: Vec<(~str, ~str)> ,
|
2013-12-12 08:07:43 -06:00
|
|
|
input: Option<~str>) -> Option<Result> {
|
2012-02-07 20:55:02 -06:00
|
|
|
|
2014-03-30 22:53:26 -05:00
|
|
|
let env = env.clone().append(target_env(lib_path, prog).as_slice());
|
2014-05-05 18:58:42 -05:00
|
|
|
let opt_process = Process::configure(ProcessConfig {
|
2014-02-18 14:04:51 -06:00
|
|
|
program: prog,
|
|
|
|
args: args,
|
|
|
|
env: Some(env.as_slice()),
|
|
|
|
.. ProcessConfig::new()
|
2013-08-29 16:20:48 -05:00
|
|
|
});
|
2012-02-07 20:55:02 -06:00
|
|
|
|
2013-12-12 08:07:43 -06:00
|
|
|
match opt_process {
|
2014-05-05 18:58:42 -05:00
|
|
|
Ok(mut process) => {
|
2013-12-12 08:07:43 -06:00
|
|
|
for input in input.iter() {
|
2014-02-18 14:04:51 -06:00
|
|
|
process.stdin.get_mut_ref().write(input.as_bytes()).unwrap();
|
2013-12-12 08:07:43 -06:00
|
|
|
}
|
2014-05-05 18:58:42 -05:00
|
|
|
let ProcessOutput { status, output, error } =
|
|
|
|
process.wait_with_output().unwrap();
|
2011-07-30 23:11:14 -05:00
|
|
|
|
2013-12-12 08:07:43 -06:00
|
|
|
Some(Result {
|
|
|
|
status: status,
|
2014-03-26 11:24:16 -05:00
|
|
|
out: str::from_utf8(output.as_slice()).unwrap().to_owned(),
|
|
|
|
err: str::from_utf8(error.as_slice()).unwrap().to_owned()
|
2013-12-12 08:07:43 -06:00
|
|
|
})
|
|
|
|
},
|
2014-01-30 16:37:10 -06:00
|
|
|
Err(..) => None
|
2011-07-31 17:33:40 -05:00
|
|
|
}
|
|
|
|
}
|
2013-11-04 00:53:01 -06:00
|
|
|
|
|
|
|
pub fn run_background(lib_path: &str,
|
|
|
|
prog: &str,
|
|
|
|
args: &[~str],
|
2014-03-05 17:28:08 -06:00
|
|
|
env: Vec<(~str, ~str)> ,
|
2014-02-18 14:04:51 -06:00
|
|
|
input: Option<~str>) -> Option<Process> {
|
2013-11-04 00:53:01 -06:00
|
|
|
|
2014-03-30 22:53:26 -05:00
|
|
|
let env = env.clone().append(target_env(lib_path, prog).as_slice());
|
2014-02-18 14:04:51 -06:00
|
|
|
let opt_process = Process::configure(ProcessConfig {
|
|
|
|
program: prog,
|
|
|
|
args: args,
|
|
|
|
env: Some(env.as_slice()),
|
|
|
|
.. ProcessConfig::new()
|
2013-11-04 00:53:01 -06:00
|
|
|
});
|
|
|
|
|
2013-12-12 08:07:43 -06:00
|
|
|
match opt_process {
|
2014-01-30 16:37:10 -06:00
|
|
|
Ok(mut process) => {
|
2013-12-12 08:07:43 -06:00
|
|
|
for input in input.iter() {
|
2014-02-18 14:04:51 -06:00
|
|
|
process.stdin.get_mut_ref().write(input.as_bytes()).unwrap();
|
2013-12-12 08:07:43 -06:00
|
|
|
}
|
2013-11-04 00:53:01 -06:00
|
|
|
|
2013-12-12 08:07:43 -06:00
|
|
|
Some(process)
|
|
|
|
},
|
2014-01-30 16:37:10 -06:00
|
|
|
Err(..) => None
|
2013-12-12 08:07:43 -06:00
|
|
|
}
|
2013-11-04 00:53:01 -06:00
|
|
|
}
|