Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Environment variables: For one accepting boolean, treat "0", "false" and empty env as false #1238

Merged
merged 6 commits into from
Oct 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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