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.
11 use std::dynamic_lib::DynamicLibrary;
12 use std::io::prelude::*;
13 use std::path::PathBuf;
14 use std::process::{ExitStatus, Command, Child, Output, Stdio};
16 fn add_target_env(cmd: &mut Command, lib_path: &str, aux_path: Option<&str>) {
17 // Need to be sure to put both the lib_path and the aux path in the dylib
18 // search path for the child.
19 let mut path = DynamicLibrary::search_path();
21 Some(p) => path.insert(0, PathBuf::from(p)),
24 path.insert(0, PathBuf::from(lib_path));
26 // Add the new dylib search path var
27 let var = DynamicLibrary::envvar();
28 let newpath = DynamicLibrary::create_path(&path);
29 let newpath = newpath.to_str().unwrap().to_string();
30 cmd.env(var, &newpath);
33 pub struct Result {pub status: ExitStatus, pub out: String, pub err: String}
35 pub fn run(lib_path: &str,
37 aux_path: Option<&str>,
39 env: Vec<(String, String)> ,
40 input: Option<String>) -> Option<Result> {
42 let mut cmd = Command::new(prog);
44 .stdin(Stdio::piped())
45 .stdout(Stdio::piped())
46 .stderr(Stdio::piped());
47 add_target_env(&mut cmd, lib_path, aux_path);
48 for (key, val) in env {
54 if let Some(input) = input {
55 process.stdin.as_mut().unwrap().write_all(input.as_bytes()).unwrap();
57 let Output { status, stdout, stderr } =
58 process.wait_with_output().unwrap();
62 out: String::from_utf8(stdout).unwrap(),
63 err: String::from_utf8(stderr).unwrap()
70 pub fn run_background(lib_path: &str,
72 aux_path: Option<&str>,
74 env: Vec<(String, String)> ,
75 input: Option<String>) -> Option<Child> {
77 let mut cmd = Command::new(prog);
79 .stdin(Stdio::piped())
80 .stdout(Stdio::piped())
81 .stderr(Stdio::piped());
82 add_target_env(&mut cmd, lib_path, aux_path);
83 for (key, val) in env {
89 if let Some(input) = input {
90 process.stdin.as_mut().unwrap().write_all(input.as_bytes()).unwrap();