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