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 cmd.env(var, newpath);
32 pub struct Result {pub status: ExitStatus, pub out: String, pub err: String}
34 pub fn run(lib_path: &str,
36 aux_path: Option<&str>,
38 env: Vec<(String, String)> ,
39 input: Option<String>) -> Option<Result> {
41 let mut cmd = Command::new(prog);
43 .stdin(Stdio::piped())
44 .stdout(Stdio::piped())
45 .stderr(Stdio::piped());
46 add_target_env(&mut cmd, lib_path, aux_path);
47 for (key, val) in env {
53 if let Some(input) = input {
54 process.stdin.as_mut().unwrap().write_all(input.as_bytes()).unwrap();
56 let Output { status, stdout, stderr } =
57 process.wait_with_output().unwrap();
61 out: String::from_utf8(stdout).unwrap(),
62 err: String::from_utf8(stderr).unwrap()
69 pub fn run_background(lib_path: &str,
71 aux_path: Option<&str>,
73 env: Vec<(String, String)> ,
74 input: Option<String>) -> Option<Child> {
76 let mut cmd = Command::new(prog);
78 .stdin(Stdio::piped())
79 .stdout(Stdio::piped())
80 .stderr(Stdio::piped());
81 add_target_env(&mut cmd, lib_path, aux_path);
82 for (key, val) in env {
88 if let Some(input) = input {
89 process.stdin.as_mut().unwrap().write_all(input.as_bytes()).unwrap();