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::*;
15 use std::path::PathBuf;
16 use std::process::{Child, Command, ExitStatus, Output, Stdio};
18 pub fn dylib_env_var() -> &'static str {
21 } else if cfg!(target_os = "macos") {
23 } else if cfg!(target_os = "haiku") {
30 fn add_target_env(cmd: &mut Command, lib_path: &str, aux_path: Option<&str>) {
31 // Need to be sure to put both the lib_path and the aux path in the dylib
32 // search path for the child.
33 let var = dylib_env_var();
34 let mut path = env::split_paths(&env::var_os(var).unwrap_or(OsString::new()))
36 if let Some(p) = aux_path {
37 path.insert(0, PathBuf::from(p))
39 path.insert(0, PathBuf::from(lib_path));
41 // Add the new dylib search path var
42 let newpath = env::join_paths(&path).unwrap();
43 cmd.env(var, newpath);
47 pub status: ExitStatus,
52 pub fn run(lib_path: &str,
54 aux_path: Option<&str>,
56 env: Vec<(String, String)>,
57 input: Option<String>)
58 -> io::Result<Result> {
60 let mut cmd = Command::new(prog);
62 .stdout(Stdio::piped())
63 .stderr(Stdio::piped())
64 .stdin(Stdio::piped());
66 add_target_env(&mut cmd, lib_path, aux_path);
67 for (key, val) in env {
71 let mut process = cmd.spawn()?;
72 if let Some(input) = input {
73 process.stdin.as_mut().unwrap().write_all(input.as_bytes()).unwrap();
75 let Output { status, stdout, stderr } = process.wait_with_output().unwrap();
79 out: String::from_utf8(stdout).unwrap(),
80 err: String::from_utf8(stderr).unwrap(),
84 pub fn run_background(lib_path: &str,
86 aux_path: Option<&str>,
88 env: Vec<(String, String)>,
89 input: Option<String>)
90 -> io::Result<Child> {
92 let mut cmd = Command::new(prog);
94 .stdin(Stdio::piped())
95 .stdout(Stdio::piped());
96 add_target_env(&mut cmd, lib_path, aux_path);
97 for (key, val) in env {
101 let mut process = cmd.spawn()?;
102 if let Some(input) = input {
103 process.stdin.as_mut().unwrap().write_all(input.as_bytes()).unwrap();