Browse Source

Separated cflags and command args

cl-test
fedor 7 years ago
parent
commit
78af8025ca
  1. 32
      src/lib.rs
  2. 18
      tests/cc_env.rs

32
src/lib.rs

@ -148,6 +148,7 @@ impl From<io::Error> for Error {
#[derive(Clone, Debug)]
pub struct Tool {
path: PathBuf,
path_args: Vec<OsString>,
args: Vec<OsString>,
env: Vec<(OsString, OsString)>,
family: ToolFamily
@ -1285,7 +1286,7 @@ impl Build {
.map(|(tool, args)| {
let mut t = Tool::new(PathBuf::from(tool));
for arg in args {
t.args.push(arg.into());
t.path_args.push(arg.into());
}
t
})
@ -1401,6 +1402,7 @@ impl Build {
.collect()
}
fn env_tool(&self, name: &str) -> Option<(String, Vec<String>)> {
self.get_var(name).ok().map(|tool| {
let whitelist = ["ccache", "distcc", "sccache"];
@ -1539,6 +1541,7 @@ impl Tool {
};
Tool {
path: path,
path_args: Vec::new(),
args: Vec::new(),
env: Vec::new(),
family: family
@ -1552,6 +1555,7 @@ impl Tool {
/// variables configured.
pub fn to_command(&self) -> Command {
let mut cmd = Command::new(&self.path);
cmd.args(&self.path_args);
cmd.args(&self.args);
for &(ref k, ref v) in self.env.iter() {
cmd.env(k, v);
@ -1580,6 +1584,32 @@ impl Tool {
pub fn env(&self) -> &[(OsString, OsString)] {
&self.env
}
/// Returns the compiler command in format of CC environment variable.
///
/// This is typically used by configure script
pub fn cc_env(&self) -> OsString {
let mut cc = self.path.as_os_str().to_owned();
for arg in self.path_args.iter() {
cc.push(" ");
cc.push(arg);
}
cc
}
/// Returns the compiler flags in format of CFLAGS environment variable.
///
/// This is typically used by configure script
pub fn cflags_env(&self) -> OsString {
let mut flags = OsString::new();
for (i, arg) in self.args.iter().enumerate() {
if i > 0 {
flags.push(" ");
}
flags.push(arg);
}
flags
}
}
fn run(cmd: &mut Command, program: &str) -> Result<(), Error> {

18
tests/cc_env.rs

@ -11,6 +11,7 @@ fn main() {
ccache();
distcc();
ccache_spaces();
ccache_env_flags();
}
fn ccache() {
@ -47,3 +48,20 @@ fn distcc() {
.must_have("foo.c")
.must_not_have("distcc");
}
fn ccache_env_flags() {
use std::path::Path;
use std::ffi::OsString;
let test = Test::gnu();
test.shim("ccache");
env::set_var("CC", "ccache lol-this-is-not-a-compiler");
let compiler = test.gcc().file("foo.c").get_compiler();
assert_eq!(compiler.path(), Path::new("ccache"));
assert_eq!(compiler.cc_env(), OsString::from("ccache lol-this-is-not-a-compiler"));
assert!(compiler.cflags_env().into_string().unwrap().contains("ccache") == false);
assert!(compiler.cflags_env().into_string().unwrap().contains(" lol-this-is-not-a-compiler") == false);
env::set_var("CC", "");
}

Loading…
Cancel
Save