|
84 | 84 | //! * `CC_ENABLE_DEBUG_OUTPUT` - if set, compiler command invocations and exit codes will
|
85 | 85 | //! be logged to stdout. This is useful for debugging build script issues, but can be
|
86 | 86 | //! overly verbose for normal use.
|
| 87 | +//! * `CC_PARSE_FLAGS_WITH_SHLEX` - if set, *FLAGS will be parsed with shlex, similar to |
| 88 | +//! `make` and `cmake`. `CFLAGS='-foo "-bar baz"'` will be parsed as `["foo", "-bar baz"]` |
| 89 | +//! instead of `["-foo", "\"-bar", "baz\""]` |
87 | 90 | //! * `CXX...` - see [C++ Support](#c-support).
|
88 | 91 | //!
|
89 | 92 | //! Furthermore, projects using this crate may specify custom environment variables
|
@@ -227,6 +230,7 @@ use std::sync::{Arc, RwLock};
|
227 | 230 | #[cfg(feature = "parallel")]
|
228 | 231 | mod parallel;
|
229 | 232 | mod windows;
|
| 233 | +use shlex::Shlex; |
230 | 234 | // Regardless of whether this should be in this crate's public API,
|
231 | 235 | // it has been since 2015, so don't break it.
|
232 | 236 | pub use windows::find_tools as windows_registry;
|
@@ -301,6 +305,7 @@ pub struct Build {
|
301 | 305 | apple_versions_cache: Arc<RwLock<HashMap<Box<str>, Arc<str>>>>,
|
302 | 306 | emit_rerun_if_env_changed: bool,
|
303 | 307 | cached_compiler_family: Arc<RwLock<HashMap<Box<Path>, ToolFamily>>>,
|
| 308 | + parse_flags_with_shlex: Option<bool>, |
304 | 309 | }
|
305 | 310 |
|
306 | 311 | /// Represents the types of errors that may occur while using cc-rs.
|
@@ -425,6 +430,7 @@ impl Build {
|
425 | 430 | apple_versions_cache: Arc::new(RwLock::new(HashMap::new())),
|
426 | 431 | emit_rerun_if_env_changed: true,
|
427 | 432 | cached_compiler_family: Arc::default(),
|
| 433 | + parse_flags_with_shlex: None, |
428 | 434 | }
|
429 | 435 | }
|
430 | 436 |
|
@@ -1277,6 +1283,15 @@ impl Build {
|
1277 | 1283 | self
|
1278 | 1284 | }
|
1279 | 1285 |
|
| 1286 | + /// Configure whether *FLAGS variables are parsed using `shlex`, similarly to `make` and |
| 1287 | + /// `cmake`. |
| 1288 | + /// |
| 1289 | + /// This option defaults to `false`. |
| 1290 | + pub fn parse_flags_with_shlex(&mut self, parse_flags_with_shlex: bool) -> &mut Build { |
| 1291 | + self.parse_flags_with_shlex = Some(parse_flags_with_shlex); |
| 1292 | + self |
| 1293 | + } |
| 1294 | + |
1280 | 1295 | #[doc(hidden)]
|
1281 | 1296 | pub fn __set_env<A, B>(&mut self, a: A, b: B) -> &mut Build
|
1282 | 1297 | where
|
@@ -3634,6 +3649,10 @@ impl Build {
|
3634 | 3649 | })
|
3635 | 3650 | }
|
3636 | 3651 |
|
| 3652 | + fn get_parse_flags_with_shlex(&self) -> bool { |
| 3653 | + self.parse_flags_with_shlex.unwrap_or_else(|| self.getenv("CC_PARSE_FLAGS_WITH_SHLEX").is_some()) |
| 3654 | + } |
| 3655 | + |
3637 | 3656 | fn get_dwarf_version(&self) -> Option<u32> {
|
3638 | 3657 | // Tentatively matches the DWARF version defaults as of rustc 1.62.
|
3639 | 3658 | let target = self.get_target().ok()?;
|
@@ -3748,12 +3767,20 @@ impl Build {
|
3748 | 3767 | }
|
3749 | 3768 |
|
3750 | 3769 | fn envflags(&self, name: &str) -> Result<Vec<String>, Error> {
|
3751 |
| - Ok(self |
3752 |
| - .getenv_with_target_prefixes(name)? |
3753 |
| - .to_string_lossy() |
3754 |
| - .split_ascii_whitespace() |
3755 |
| - .map(ToString::to_string) |
3756 |
| - .collect()) |
| 3770 | + let env_os = self |
| 3771 | + .getenv_with_target_prefixes(name)?; |
| 3772 | + let env = env_os |
| 3773 | + .to_string_lossy(); |
| 3774 | + |
| 3775 | + if self.get_parse_flags_with_shlex() { |
| 3776 | + Ok(Shlex::new(&env) |
| 3777 | + .collect()) |
| 3778 | + } else { |
| 3779 | + Ok(env |
| 3780 | + .split_ascii_whitespace() |
| 3781 | + .map(ToString::to_string) |
| 3782 | + .collect()) |
| 3783 | + } |
3757 | 3784 | }
|
3758 | 3785 |
|
3759 | 3786 | fn fix_env_for_apple_os(&self, cmd: &mut Command) -> Result<(), Error> {
|
|
0 commit comments