Skip to content

Commit

Permalink
Environment variables: For one accepting boolean, treat "0", "false" …
Browse files Browse the repository at this point in the history
…and empty env as false (#1238)

* Add getenv_boolean

* Update Build methods to use getenv_boolean

And update getenv_boolean, to treat "false" as false.

* Fix Build::getenv_Boolean and treat empty env as false

* Env CC_ENABLE_DEBUG_OUTPUT: treat "false" or empty str as false

* Fix Build::getenv_boolean

* Fix clippy
  • Loading branch information
NobodyXu authored Oct 6, 2024
1 parent 3bbb384 commit 1a6a710
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 7 deletions.
2 changes: 1 addition & 1 deletion src/command_helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ impl CargoOutput {
warnings: true,
output: OutputKind::Forward,
debug: match std::env::var_os("CC_ENABLE_DEBUG_OUTPUT") {
Some(v) => v != "0",
Some(v) => v != "0" && v != "false" && v != "",
None => false,
},
checked_dbg_var: Arc::new(AtomicBool::new(false)),
Expand Down
17 changes: 11 additions & 6 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1857,7 +1857,7 @@ impl Build {
let mut cmd = self.get_base_compiler()?;

// Disable default flag generation via `no_default_flags` or environment variable
let no_defaults = self.no_default_flags || self.getenv("CRATE_CC_NO_DEFAULTS").is_some();
let no_defaults = self.no_default_flags || self.getenv_boolean("CRATE_CC_NO_DEFAULTS");

if !no_defaults {
self.add_default_flags(&mut cmd, &target, &opt_level)?;
Expand Down Expand Up @@ -3668,15 +3668,12 @@ impl Build {
}

fn get_debug(&self) -> bool {
self.debug.unwrap_or_else(|| match self.getenv("DEBUG") {
Some(s) => &*s != "false",
None => false,
})
self.debug.unwrap_or_else(|| self.getenv_boolean("DEBUG"))
}

fn get_shell_escaped_flags(&self) -> bool {
self.shell_escaped_flags
.unwrap_or_else(|| self.getenv("CC_SHELL_ESCAPED_FLAGS").is_some())
.unwrap_or_else(|| self.getenv_boolean("CC_SHELL_ESCAPED_FLAGS"))
}

fn get_dwarf_version(&self) -> Option<u32> {
Expand Down Expand Up @@ -3752,6 +3749,14 @@ impl Build {
r
}

/// get boolean flag that is either true or false
fn getenv_boolean(&self, v: &str) -> bool {
match self.getenv(v) {
Some(s) => &*s != "0" && &*s != "false" && !s.is_empty(),
None => false,
}
}

fn getenv_unwrap(&self, v: &str) -> Result<Arc<OsStr>, Error> {
match self.getenv(v) {
Some(s) => Ok(s),
Expand Down

0 comments on commit 1a6a710

Please sign in to comment.