1 // Copyright 2012 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
12 use std
::ffi
::OsString
;
13 use std
::io
::prelude
::*;
14 use std
::path
::PathBuf
;
15 use std
::process
::{ExitStatus, Command, Child, Output, Stdio}
;
17 fn add_target_env(cmd
: &mut Command
, lib_path
: &str, aux_path
: Option
<&str>) {
18 // Need to be sure to put both the lib_path and the aux path in the dylib
19 // search path for the child.
20 let var
= if cfg
!(windows
) {
22 } else if cfg
!(target_os
= "macos") {
27 let mut path
= env
::split_paths(&env
::var_os(var
).unwrap_or(OsString
::new()))
29 if let Some(p
) = aux_path
{
30 path
.insert(0, PathBuf
::from(p
))
32 path
.insert(0, PathBuf
::from(lib_path
));
34 // Add the new dylib search path var
35 let newpath
= env
::join_paths(&path
).unwrap();
36 cmd
.env(var
, newpath
);
39 pub struct Result {pub status: ExitStatus, pub out: String, pub err: String}
41 pub fn run(lib_path
: &str,
43 aux_path
: Option
<&str>,
45 env
: Vec
<(String
, String
)> ,
46 input
: Option
<String
>) -> Option
<Result
> {
48 let mut cmd
= Command
::new(prog
);
50 .stdin(Stdio
::piped())
51 .stdout(Stdio
::piped())
52 .stderr(Stdio
::piped());
53 add_target_env(&mut cmd
, lib_path
, aux_path
);
54 for (key
, val
) in env
{
60 if let Some(input
) = input
{
61 process
.stdin
.as_mut().unwrap().write_all(input
.as_bytes()).unwrap();
63 let Output { status, stdout, stderr }
=
64 process
.wait_with_output().unwrap();
68 out
: String
::from_utf8(stdout
).unwrap(),
69 err
: String
::from_utf8(stderr
).unwrap()
76 pub fn run_background(lib_path
: &str,
78 aux_path
: Option
<&str>,
80 env
: Vec
<(String
, String
)> ,
81 input
: Option
<String
>) -> Option
<Child
> {
83 let mut cmd
= Command
::new(prog
);
85 .stdin(Stdio
::piped())
86 .stdout(Stdio
::piped())
87 .stderr(Stdio
::piped());
88 add_target_env(&mut cmd
, lib_path
, aux_path
);
89 for (key
, val
) in env
{
95 if let Some(input
) = input
{
96 process
.stdin
.as_mut().unwrap().write_all(input
.as_bytes()).unwrap();