Skip to content

Commit ad42450

Browse files
authored
Merge pull request #106 from nagisa/nagisa/var-os
Use `env::var_os` (almost) everywhere
2 parents ef356f3 + 01bc49f commit ad42450

File tree

1 file changed

+31
-30
lines changed

1 file changed

+31
-30
lines changed

src/lib.rs

Lines changed: 31 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -313,8 +313,8 @@ impl Config {
313313
}
314314

315315
pub fn target_supported(&self) -> bool {
316-
let target = env::var("TARGET").unwrap_or_default();
317-
let host = env::var("HOST").unwrap_or_default();
316+
let target = env::var_os("TARGET").unwrap_or_default();
317+
let host = env::var_os("HOST").unwrap_or_default();
318318

319319
// Only use pkg-config in host == target situations by default (allowing an
320320
// override).
@@ -326,13 +326,13 @@ impl Config {
326326
// a wrapper script that sets up platform-specific prefixes.
327327
match self.targetted_env_var("PKG_CONFIG_ALLOW_CROSS") {
328328
// don't use pkg-config if explicitly disabled
329-
Ok(ref val) if val == "0" => false,
330-
Ok(_) => true,
331-
Err(_) => {
329+
Some(ref val) if val == "0" => false,
330+
Some(_) => true,
331+
None => {
332332
// if not disabled, and pkg-config is customized,
333333
// then assume it's prepared for cross-compilation
334-
self.targetted_env_var("PKG_CONFIG").is_ok()
335-
|| self.targetted_env_var("PKG_CONFIG_SYSROOT_DIR").is_ok()
334+
self.targetted_env_var("PKG_CONFIG").is_some()
335+
|| self.targetted_env_var("PKG_CONFIG_SYSROOT_DIR").is_some()
336336
}
337337
}
338338
}
@@ -343,26 +343,27 @@ impl Config {
343343
get_variable(package, variable).map_err(|e| e.to_string())
344344
}
345345

346-
fn targetted_env_var(&self, var_base: &str) -> Result<String, env::VarError> {
347-
if let Ok(target) = env::var("TARGET") {
348-
let host = env::var("HOST")?;
349-
let kind = if host == target { "HOST" } else { "TARGET" };
350-
let target_u = target.replace("-", "_");
351-
352-
self.env_var(&format!("{}_{}", var_base, target))
353-
.or_else(|_| self.env_var(&format!("{}_{}", var_base, target_u)))
354-
.or_else(|_| self.env_var(&format!("{}_{}", kind, var_base)))
355-
.or_else(|_| self.env_var(var_base))
356-
} else {
357-
self.env_var(var_base)
358-
}
359-
}
346+
fn targetted_env_var(&self, var_base: &str) -> Option<OsString> {
347+
match (env::var("TARGET"), env::var("HOST")) {
348+
(Ok(target), Ok(host)) => {
349+
let kind = if host == target { "HOST" } else { "TARGET" };
350+
let target_u = target.replace("-", "_");
360351

361-
fn env_var(&self, name: &str) -> Result<String, env::VarError> {
362-
if self.env_metadata {
363-
println!("cargo:rerun-if-env-changed={}", name);
352+
self.env_var_os(&format!("{}_{}", var_base, target))
353+
.or_else(|| self.env_var_os(&format!("{}_{}", var_base, target_u)))
354+
.or_else(|| self.env_var_os(&format!("{}_{}", kind, var_base)))
355+
.or_else(|| self.env_var_os(var_base))
356+
}
357+
(Err(env::VarError::NotPresent), _) | (_, Err(env::VarError::NotPresent)) => {
358+
self.env_var_os(var_base)
359+
}
360+
(Err(env::VarError::NotUnicode(s)), _) | (_, Err(env::VarError::NotUnicode(s))) => {
361+
panic!(
362+
"HOST or TARGET environment variable is not valid unicode: {:?}",
363+
s
364+
)
365+
}
364366
}
365-
env::var(name)
366367
}
367368

368369
fn env_var_os(&self, name: &str) -> Option<OsString> {
@@ -378,21 +379,21 @@ impl Config {
378379

379380
fn command(&self, name: &str, args: &[&str]) -> Command {
380381
let exe = self
381-
.env_var("PKG_CONFIG")
382-
.unwrap_or_else(|_| String::from("pkg-config"));
382+
.env_var_os("PKG_CONFIG")
383+
.unwrap_or_else(|| OsString::from("pkg-config"));
383384
let mut cmd = Command::new(exe);
384385
if self.is_static(name) {
385386
cmd.arg("--static");
386387
}
387388
cmd.args(args).args(&self.extra_args);
388389

389-
if let Ok(value) = self.targetted_env_var("PKG_CONFIG_PATH") {
390+
if let Some(value) = self.targetted_env_var("PKG_CONFIG_PATH") {
390391
cmd.env("PKG_CONFIG_PATH", value);
391392
}
392-
if let Ok(value) = self.targetted_env_var("PKG_CONFIG_LIBDIR") {
393+
if let Some(value) = self.targetted_env_var("PKG_CONFIG_LIBDIR") {
393394
cmd.env("PKG_CONFIG_LIBDIR", value);
394395
}
395-
if let Ok(value) = self.targetted_env_var("PKG_CONFIG_SYSROOT_DIR") {
396+
if let Some(value) = self.targetted_env_var("PKG_CONFIG_SYSROOT_DIR") {
396397
cmd.env("PKG_CONFIG_SYSROOT_DIR", value);
397398
}
398399
if self.print_system_libs {

0 commit comments

Comments
 (0)