2012-03-12 20:04:27 -07:00
|
|
|
import run::spawn_process;
|
2012-08-14 13:38:35 -07:00
|
|
|
import io::WriterUtil;
|
2012-03-12 20:04:27 -07:00
|
|
|
import libc::{c_int, pid_t};
|
2011-07-30 21:11:14 -07:00
|
|
|
|
2012-08-03 18:57:43 -07:00
|
|
|
import pipes::chan;
|
|
|
|
|
2011-07-30 21:11:14 -07:00
|
|
|
export run;
|
|
|
|
|
2012-02-07 18:55:02 -08:00
|
|
|
#[cfg(target_os = "win32")]
|
2012-07-14 18:28:20 -07:00
|
|
|
fn target_env(lib_path: ~str, prog: ~str) -> ~[(~str,~str)] {
|
2011-07-30 21:11:14 -07:00
|
|
|
|
2012-04-27 11:55:13 -07:00
|
|
|
let mut env = os::env();
|
2011-07-30 21:11:14 -07:00
|
|
|
|
2012-06-01 18:24:55 -07:00
|
|
|
// Make sure we include the aux directory in the path
|
2012-07-14 18:28:20 -07:00
|
|
|
assert prog.ends_with(~".exe");
|
|
|
|
let aux_path = prog.slice(0u, prog.len() - 4u) + ~".libaux";
|
2012-06-01 18:24:55 -07:00
|
|
|
|
2012-06-30 16:19:07 -07:00
|
|
|
env = do vec::map(env) |pair| {
|
2012-02-07 18:55:02 -08:00
|
|
|
let (k,v) = pair;
|
2012-07-14 18:28:20 -07:00
|
|
|
if k == ~"PATH" { (~"PATH", v + ~";" + lib_path + ~";" + aux_path) }
|
2012-02-07 18:55:02 -08:00
|
|
|
else { (k,v) }
|
2012-01-04 21:14:53 -08:00
|
|
|
};
|
2012-07-14 18:28:20 -07:00
|
|
|
if str::ends_with(prog, ~"rustc.exe") {
|
|
|
|
vec::push(env, (~"RUST_THREADS", ~"1"));
|
2012-02-07 18:55:02 -08:00
|
|
|
}
|
2012-08-01 17:30:05 -07:00
|
|
|
return env;
|
2011-07-30 21:11:14 -07:00
|
|
|
}
|
|
|
|
|
2012-02-07 18:55:02 -08:00
|
|
|
#[cfg(target_os = "linux")]
|
|
|
|
#[cfg(target_os = "macos")]
|
|
|
|
#[cfg(target_os = "freebsd")]
|
2012-07-13 22:57:48 -07:00
|
|
|
fn target_env(_lib_path: ~str, _prog: ~str) -> ~[(~str,~str)] {
|
2012-06-29 16:26:56 -07:00
|
|
|
~[]
|
2011-07-30 21:11:14 -07:00
|
|
|
}
|
|
|
|
|
2012-02-07 18:55:02 -08:00
|
|
|
|
2012-06-21 16:44:10 -07:00
|
|
|
// FIXME (#2659): This code is duplicated in core::run::program_output
|
2012-07-13 22:57:48 -07:00
|
|
|
fn run(lib_path: ~str,
|
|
|
|
prog: ~str,
|
|
|
|
args: ~[~str],
|
|
|
|
env: ~[(~str, ~str)],
|
|
|
|
input: option<~str>) -> {status: int, out: ~str, err: ~str} {
|
2011-07-31 15:33:40 -07:00
|
|
|
|
2012-02-07 18:55:02 -08:00
|
|
|
let pipe_in = os::pipe();
|
|
|
|
let pipe_out = os::pipe();
|
|
|
|
let pipe_err = os::pipe();
|
2012-04-13 09:19:07 -07:00
|
|
|
let pid = spawn_process(prog, args,
|
2012-08-14 15:08:08 -07:00
|
|
|
&some(env + target_env(lib_path, prog)),
|
|
|
|
&none, pipe_in.in, pipe_out.out, pipe_err.out);
|
2012-02-07 18:55:02 -08:00
|
|
|
|
|
|
|
os::close(pipe_in.in);
|
|
|
|
os::close(pipe_out.out);
|
|
|
|
os::close(pipe_err.out);
|
|
|
|
if pid == -1i32 {
|
|
|
|
os::close(pipe_in.out);
|
|
|
|
os::close(pipe_out.in);
|
|
|
|
os::close(pipe_err.in);
|
|
|
|
fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
writeclose(pipe_in.out, input);
|
2012-08-03 18:57:43 -07:00
|
|
|
let p = pipes::port_set();
|
|
|
|
let ch = p.chan();
|
2012-07-04 15:04:28 -04:00
|
|
|
do task::spawn_sched(task::single_threaded) {
|
2012-02-10 10:03:34 -06:00
|
|
|
let errput = readclose(pipe_err.in);
|
2012-08-03 18:57:43 -07:00
|
|
|
ch.send((2, errput));
|
2012-06-26 13:55:56 -07:00
|
|
|
}
|
2012-08-03 18:57:43 -07:00
|
|
|
let ch = p.chan();
|
2012-07-04 15:04:28 -04:00
|
|
|
do task::spawn_sched(task::single_threaded) {
|
2012-02-10 10:03:34 -06:00
|
|
|
let output = readclose(pipe_out.in);
|
2012-08-03 18:57:43 -07:00
|
|
|
ch.send((1, output));
|
2012-06-26 13:55:56 -07:00
|
|
|
}
|
2012-02-07 18:55:02 -08:00
|
|
|
let status = run::waitpid(pid);
|
2012-07-13 22:57:48 -07:00
|
|
|
let mut errs = ~"";
|
|
|
|
let mut outs = ~"";
|
2012-03-22 08:39:41 -07:00
|
|
|
let mut count = 2;
|
2012-02-10 10:03:34 -06:00
|
|
|
while count > 0 {
|
2012-08-06 12:34:08 -07:00
|
|
|
match p.recv() {
|
2012-08-03 18:57:43 -07:00
|
|
|
(1, s) => {
|
|
|
|
outs = s;
|
|
|
|
}
|
|
|
|
(2, s) => {
|
|
|
|
errs = s;
|
|
|
|
}
|
2012-08-06 09:55:53 -07:00
|
|
|
_ => { fail }
|
2012-02-10 10:03:34 -06:00
|
|
|
};
|
|
|
|
count -= 1;
|
|
|
|
};
|
2012-08-01 17:30:05 -07:00
|
|
|
return {status: status, out: outs, err: errs};
|
2011-07-30 21:11:14 -07:00
|
|
|
}
|
|
|
|
|
2012-07-13 22:57:48 -07:00
|
|
|
fn writeclose(fd: c_int, s: option<~str>) {
|
2011-07-31 15:33:40 -07:00
|
|
|
if option::is_some(s) {
|
2012-01-11 15:15:54 +01:00
|
|
|
let writer = io::fd_writer(fd, false);
|
2011-08-30 15:40:25 -07:00
|
|
|
writer.write_str(option::get(s));
|
2011-07-31 15:33:40 -07:00
|
|
|
}
|
|
|
|
|
2011-11-08 11:09:40 -08:00
|
|
|
os::close(fd);
|
2011-07-31 15:33:40 -07:00
|
|
|
}
|
|
|
|
|
2012-07-13 22:57:48 -07:00
|
|
|
fn readclose(fd: c_int) -> ~str {
|
2011-07-30 21:11:14 -07:00
|
|
|
// Copied from run::program_output
|
2012-03-12 20:04:27 -07:00
|
|
|
let file = os::fdopen(fd);
|
2012-01-11 15:15:54 +01:00
|
|
|
let reader = io::FILE_reader(file, false);
|
2012-07-13 22:57:48 -07:00
|
|
|
let mut buf = ~"";
|
2011-07-30 21:11:14 -07:00
|
|
|
while !reader.eof() {
|
2011-08-11 18:49:36 -07:00
|
|
|
let bytes = reader.read_bytes(4096u);
|
2012-01-25 01:00:21 -08:00
|
|
|
buf += str::from_bytes(bytes);
|
2011-07-30 21:11:14 -07:00
|
|
|
}
|
2011-11-08 11:09:40 -08:00
|
|
|
os::fclose(file);
|
2012-08-01 17:30:05 -07:00
|
|
|
return buf;
|
2011-07-30 21:11:14 -07:00
|
|
|
}
|