]> git.lizzy.rs Git - rust.git/blob - src/compiletest/procsrv.rs
Auto merge of #28355 - DiamondLovesYou:pnacl-librustc-trans, r=alexcrichton
[rust.git] / src / compiletest / procsrv.rs
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.
4 //
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.
10
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};
15
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();
20     if let Some(p) = aux_path {
21         path.insert(0, PathBuf::from(p))
22     }
23     path.insert(0, PathBuf::from(lib_path));
24
25     // Add the new dylib search path var
26     let var = DynamicLibrary::envvar();
27     let newpath = DynamicLibrary::create_path(&path);
28     cmd.env(var, newpath);
29 }
30
31 pub struct Result {pub status: ExitStatus, pub out: String, pub err: String}
32
33 pub fn run(lib_path: &str,
34            prog: &str,
35            aux_path: Option<&str>,
36            args: &[String],
37            env: Vec<(String, String)> ,
38            input: Option<String>) -> Option<Result> {
39
40     let mut cmd = Command::new(prog);
41     cmd.args(args)
42        .stdin(Stdio::piped())
43        .stdout(Stdio::piped())
44        .stderr(Stdio::piped());
45     add_target_env(&mut cmd, lib_path, aux_path);
46     for (key, val) in env {
47         cmd.env(&key, &val);
48     }
49
50     match cmd.spawn() {
51         Ok(mut process) => {
52             if let Some(input) = input {
53                 process.stdin.as_mut().unwrap().write_all(input.as_bytes()).unwrap();
54             }
55             let Output { status, stdout, stderr } =
56                 process.wait_with_output().unwrap();
57
58             Some(Result {
59                 status: status,
60                 out: String::from_utf8(stdout).unwrap(),
61                 err: String::from_utf8(stderr).unwrap()
62             })
63         },
64         Err(..) => None
65     }
66 }
67
68 pub fn run_background(lib_path: &str,
69            prog: &str,
70            aux_path: Option<&str>,
71            args: &[String],
72            env: Vec<(String, String)> ,
73            input: Option<String>) -> Option<Child> {
74
75     let mut cmd = Command::new(prog);
76     cmd.args(args)
77        .stdin(Stdio::piped())
78        .stdout(Stdio::piped())
79        .stderr(Stdio::piped());
80     add_target_env(&mut cmd, lib_path, aux_path);
81     for (key, val) in env {
82         cmd.env(&key, &val);
83     }
84
85     match cmd.spawn() {
86         Ok(mut process) => {
87             if let Some(input) = input {
88                 process.stdin.as_mut().unwrap().write_all(input.as_bytes()).unwrap();
89             }
90
91             Some(process)
92         },
93         Err(..) => None
94     }
95 }