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") {
28 fn add_target_env(cmd: &mut Command, lib_path: &str, aux_path: Option<&str>) {
29 // Need to be sure to put both the lib_path and the aux path in the dylib
30 // search path for the child.
31 let var = dylib_env_var();
32 let mut path = env::split_paths(&env::var_os(var).unwrap_or(OsString::new()))
34 if let Some(p) = aux_path {
35 path.insert(0, PathBuf::from(p))
37 path.insert(0, PathBuf::from(lib_path));
39 // Add the new dylib search path var
40 let newpath = env::join_paths(&path).unwrap();
41 cmd.env(var, newpath);
45 pub status: ExitStatus,
50 pub fn run(lib_path: &str,
52 aux_path: Option<&str>,
54 env: Vec<(String, String)>,
55 input: Option<String>)
56 -> io::Result<Result> {
58 let mut cmd = Command::new(prog);
60 .stdout(Stdio::piped())
61 .stderr(Stdio::piped())
62 .stdin(Stdio::piped());
64 add_target_env(&mut cmd, lib_path, aux_path);
65 for (key, val) in env {
69 let mut process = cmd.spawn()?;
70 if let Some(input) = input {
71 process.stdin.as_mut().unwrap().write_all(input.as_bytes()).unwrap();
73 let Output { status, stdout, stderr } = process.wait_with_output().unwrap();
77 out: String::from_utf8(stdout).unwrap(),
78 err: String::from_utf8(stderr).unwrap(),
82 pub fn run_background(lib_path: &str,
84 aux_path: Option<&str>,
86 env: Vec<(String, String)>,
87 input: Option<String>)
88 -> io::Result<Child> {
90 let mut cmd = Command::new(prog);
92 .stdin(Stdio::piped())
93 .stdout(Stdio::piped());
94 add_target_env(&mut cmd, lib_path, aux_path);
95 for (key, val) in env {
99 let mut process = cmd.spawn()?;
100 if let Some(input) = input {
101 process.stdin.as_mut().unwrap().write_all(input.as_bytes()).unwrap();