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::run;
|
|
|
|
use std::str;
|
2013-11-11 19:37:14 -06:00
|
|
|
use std::rt::io::process::ProcessExit;
|
2012-08-03 20:57:43 -05:00
|
|
|
|
2012-02-07 20:55:02 -06:00
|
|
|
#[cfg(target_os = "win32")]
|
2013-05-11 21:45:28 -05:00
|
|
|
fn target_env(lib_path: &str, prog: &str) -> ~[(~str,~str)] {
|
2011-07-30 23:11:14 -05:00
|
|
|
|
2012-04-27 13:55:13 -05:00
|
|
|
let mut 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
|
|
|
|
2013-06-11 20:06:38 -05:00
|
|
|
env = do env.map() |pair| {
|
2013-07-02 14:47:32 -05:00
|
|
|
let (k,v) = (*pair).clone();
|
2013-05-29 13:10:16 -05:00
|
|
|
if k == ~"PATH" { (~"PATH", v + ";" + lib_path + ";" + aux_path) }
|
2012-02-07 20:55:02 -06:00
|
|
|
else { (k,v) }
|
2012-01-04 23:14:53 -06:00
|
|
|
};
|
2013-06-10 10:03:16 -05:00
|
|
|
if prog.ends_with("rustc.exe") {
|
2012-09-26 19:33:34 -05:00
|
|
|
env.push((~"RUST_THREADS", ~"1"));
|
2012-02-07 20:55:02 -06:00
|
|
|
}
|
2012-08-01 19:30:05 -05:00
|
|
|
return 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")]
|
2013-05-11 21:45:28 -05:00
|
|
|
fn target_env(_lib_path: &str, _prog: &str) -> ~[(~str,~str)] {
|
2013-05-12 07:58:00 -05:00
|
|
|
os::env()
|
2011-07-30 23:11:14 -05:00
|
|
|
}
|
|
|
|
|
2013-11-11 19:37:14 -06:00
|
|
|
pub struct Result {status: ProcessExit, out: ~str, 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],
|
2013-01-30 16:10:03 -06:00
|
|
|
env: ~[(~str, ~str)],
|
|
|
|
input: Option<~str>) -> Result {
|
2012-02-07 20:55:02 -06:00
|
|
|
|
2013-05-12 07:58:00 -05:00
|
|
|
let env = env + target_env(lib_path, prog);
|
2013-10-28 17:22:49 -05:00
|
|
|
let mut process = run::Process::new(prog, args, run::ProcessOptions {
|
2013-08-13 17:56:17 -05:00
|
|
|
env: Some(env),
|
2013-05-12 07:58:00 -05:00
|
|
|
dir: None,
|
|
|
|
in_fd: None,
|
|
|
|
out_fd: None,
|
|
|
|
err_fd: None
|
2013-08-29 16:20:48 -05:00
|
|
|
});
|
2012-02-07 20:55:02 -06:00
|
|
|
|
2013-08-03 11:45:23 -05:00
|
|
|
for input in input.iter() {
|
2013-10-28 17:22:49 -05:00
|
|
|
process.input().write(input.as_bytes());
|
2012-06-26 15:55:56 -05:00
|
|
|
}
|
2013-10-28 17:22:49 -05:00
|
|
|
let output = process.finish_with_output();
|
2011-07-30 23:11:14 -05:00
|
|
|
|
2013-05-12 07:58:00 -05:00
|
|
|
Result {
|
|
|
|
status: output.status,
|
2013-09-05 07:17:24 -05:00
|
|
|
out: str::from_utf8(output.output),
|
|
|
|
err: str::from_utf8(output.error)
|
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],
|
|
|
|
env: ~[(~str, ~str)],
|
|
|
|
input: Option<~str>) -> run::Process {
|
|
|
|
|
|
|
|
let env = env + target_env(lib_path, prog);
|
|
|
|
let mut process = run::Process::new(prog, args, run::ProcessOptions {
|
|
|
|
env: Some(env),
|
|
|
|
dir: None,
|
|
|
|
in_fd: None,
|
|
|
|
out_fd: None,
|
|
|
|
err_fd: None
|
|
|
|
});
|
|
|
|
|
|
|
|
for input in input.iter() {
|
|
|
|
process.input().write(input.as_bytes());
|
|
|
|
}
|
|
|
|
|
|
|
|
return process;
|
|
|
|
}
|
|
|
|
|