From 78af8025ca529266d54bd5bacada5ae2dc2a1df2 Mon Sep 17 00:00:00 2001 From: fedor Date: Thu, 19 Oct 2017 01:32:38 +0300 Subject: [PATCH] Separated cflags and command args --- src/lib.rs | 32 +++++++++++++++++++++++++++++++- tests/cc_env.rs | 18 ++++++++++++++++++ 2 files changed, 49 insertions(+), 1 deletion(-) diff --git a/src/lib.rs b/src/lib.rs index 0f07bc1..05679ca 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -148,6 +148,7 @@ impl From for Error { #[derive(Clone, Debug)] pub struct Tool { path: PathBuf, + path_args: Vec, args: Vec, 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)> { 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> { diff --git a/tests/cc_env.rs b/tests/cc_env.rs index 642200d..c65f55c 100644 --- a/tests/cc_env.rs +++ b/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", ""); +}