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-01-08 21:37:25 -06:00
|
|
|
use core::prelude::*;
|
|
|
|
|
2013-03-01 12:44:43 -06:00
|
|
|
use core::io::{ReaderUtil, WriterUtil};
|
|
|
|
use core::io;
|
2013-03-26 15:38:07 -05:00
|
|
|
use core::libc::c_int;
|
2013-03-01 12:44:43 -06:00
|
|
|
use core::os;
|
|
|
|
use core::run::spawn_process;
|
|
|
|
use core::run;
|
|
|
|
use core::str;
|
|
|
|
use core::task;
|
2012-08-03 20:57:43 -05:00
|
|
|
|
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
|
2013-03-28 20:39:09 -05:00
|
|
|
assert!(prog.ends_with(~".exe"));
|
2013-04-03 13:54:49 -05:00
|
|
|
let aux_path = prog.slice(0u, prog.len() - 4u).to_owned() + ~".libaux";
|
2012-06-01 20:24:55 -05:00
|
|
|
|
2012-06-30 18:19:07 -05:00
|
|
|
env = do vec::map(env) |pair| {
|
2012-09-29 21:25:04 -05: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") {
|
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")]
|
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
|
|
|
}
|
|
|
|
|
2013-01-26 00:51:32 -06:00
|
|
|
struct Result {status: int, out: ~str, err: ~str}
|
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
|
2013-01-30 16:10:03 -06:00
|
|
|
pub fn run(lib_path: ~str,
|
|
|
|
prog: ~str,
|
|
|
|
args: ~[~str],
|
|
|
|
env: ~[(~str, ~str)],
|
|
|
|
input: Option<~str>) -> Result {
|
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,
|
2012-08-20 14:23:37 -05:00
|
|
|
&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);
|
2013-02-11 21:26:38 -06:00
|
|
|
fail!();
|
2012-02-07 20:55:02 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
writeclose(pipe_in.out, input);
|
2013-02-02 05:10:12 -06:00
|
|
|
let p = comm::PortSet();
|
2012-08-03 20:57:43 -05:00
|
|
|
let ch = p.chan();
|
2013-02-15 03:11:38 -06:00
|
|
|
do task::spawn_sched(task::SingleThreaded) || {
|
2012-02-10 10:03:34 -06:00
|
|
|
let errput = readclose(pipe_err.in);
|
2012-08-03 20:57:43 -05:00
|
|
|
ch.send((2, errput));
|
2012-06-26 15:55:56 -05:00
|
|
|
}
|
2012-08-03 20:57:43 -05:00
|
|
|
let ch = p.chan();
|
2013-02-15 03:11:38 -06:00
|
|
|
do task::spawn_sched(task::SingleThreaded) || {
|
2012-02-10 10:03:34 -06:00
|
|
|
let output = readclose(pipe_out.in);
|
2012-08-03 20:57:43 -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-06 14:34:08 -05:00
|
|
|
match p.recv() {
|
2012-08-03 20:57:43 -05:00
|
|
|
(1, s) => {
|
|
|
|
outs = s;
|
|
|
|
}
|
|
|
|
(2, s) => {
|
|
|
|
errs = s;
|
|
|
|
}
|
2013-02-11 21:26:38 -06:00
|
|
|
_ => { fail!() }
|
2012-02-10 10:03:34 -06:00
|
|
|
};
|
|
|
|
count -= 1;
|
|
|
|
};
|
2013-01-26 00:51:32 -06:00
|
|
|
return Result {status: status, out: outs, err: errs};
|
2011-07-30 23:11:14 -05:00
|
|
|
}
|
|
|
|
|
2012-08-20 14:23:37 -05:00
|
|
|
fn writeclose(fd: c_int, s: Option<~str>) {
|
2012-09-21 21:37:57 -05:00
|
|
|
if s.is_some() {
|
2012-01-11 08:15:54 -06:00
|
|
|
let writer = io::fd_writer(fd, false);
|
2012-09-21 21:37:57 -05:00
|
|
|
writer.write_str(s.get());
|
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 {
|
2013-01-10 23:23:07 -06:00
|
|
|
unsafe {
|
|
|
|
// Copied from run::program_output
|
|
|
|
let file = os::fdopen(fd);
|
|
|
|
let reader = io::FILE_reader(file, false);
|
|
|
|
let mut buf = ~"";
|
|
|
|
while !reader.eof() {
|
|
|
|
let bytes = reader.read_bytes(4096u);
|
|
|
|
str::push_str(&mut buf, str::from_bytes(bytes));
|
|
|
|
}
|
|
|
|
os::fclose(file);
|
|
|
|
return buf;
|
2011-07-30 23:11:14 -05:00
|
|
|
}
|
|
|
|
}
|