Skip to content

Commit

Permalink
Fix up the environment variable string conversion
Browse files Browse the repository at this point in the history
* Tweak string conversion

For correctness and MSRV compatibility.

* Match and explicitly do nothing for absent or strange value

* Fix the conversion lifetime by reorganizing the code

* Slightly simplify
  • Loading branch information
EliahKagan committed Sep 22, 2024
1 parent 66df3f5 commit 8636464
Showing 1 changed file with 15 additions and 18 deletions.
33 changes: 15 additions & 18 deletions zng/cmake.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use std::env;
use std::ffi::OsStr;

pub fn build_zlib_ng(target: &str, compat: bool) {
let mut cmake = cmake::Config::new("src/zlib-ng");
Expand All @@ -18,23 +17,21 @@ pub fn build_zlib_ng(target: &str, compat: bool) {
if target.contains("riscv") {
// Check if we should pass on an explicit boolean value of the WITH_RVV build option.
// See: https://github.com/zlib-ng/zlib-ng?tab=readme-ov-file#advanced-build-options
match env::var_os("RISCV_WITH_RVV")
.map(OsStr::to_str)
.map(str::trim)
.map(str::to_uppercase)
.map(Into::into)
{
Some("OFF" | "NO" | "FALSE" | "0") => {
// Force RVV off. This turns off RVV entirely, as well as the runtime check for it.
// This is not usually necessary, but can be useful for building binaries portable
// to systems that do not support RVV but where auto-detection fails to identify
// this (as in https://github.com/zlib-ng/zlib-ng/issues/1705).
cmake.define("WITH_RVV", "OFF");
}
Some("ON" | "YES" | "TRUE" | "1") => {
// Try to use RVV, but still don't do so if a runtime check finds it unavailable.
// This has the same effect as omitting WITH_RVV, unless it has already been set.
cmake.define("WITH_RVV", "ON");
if let Ok(value) = env::var("RISCV_WITH_RVV") {
match value.trim().to_uppercase().as_str() {
"OFF" | "NO" | "FALSE" | "0" => {
// Force RVV off. This turns off RVV entirely, as well as the runtime check for it.
// This is not usually necessary, but can be useful for building binaries portable
// to systems that do not support RVV but where auto-detection fails to identify
// this (as in https://github.com/zlib-ng/zlib-ng/issues/1705).
cmake.define("WITH_RVV", "OFF");
}
"ON" | "YES" | "TRUE" | "1" => {
// Try to use RVV, but still don't do so if a runtime check finds it unavailable.
// This has the same effect as omitting WITH_RVV, unless it has already been set.
cmake.define("WITH_RVV", "ON");
}
_ => {}
}
}
}
Expand Down

0 comments on commit 8636464

Please sign in to comment.