From 86710edcf553f391c2b514ae8e2bff4d362a0e0f Mon Sep 17 00:00:00 2001 From: Nikolay Arhipov Date: Sat, 14 Oct 2023 11:22:55 +0300 Subject: [PATCH] Passing env variables for pkgconfig during cargo build --- src/commands/build.rs | 53 ++++++++++++++++++++++++++++++------------- 1 file changed, 37 insertions(+), 16 deletions(-) diff --git a/src/commands/build.rs b/src/commands/build.rs index 1a96169..090401b 100644 --- a/src/commands/build.rs +++ b/src/commands/build.rs @@ -224,26 +224,18 @@ impl<'a> BuildContext<'a> { // FIXME: A horrible solution, the same -Z flag will be used for all of the crates in a workspace. let (meta, _, _) = parse_crate_metadata(None)?; - let openssl_lib = env::var("OPENSSL_LIB_DIR").unwrap_or_else(|_| { - self.sdk("arm-vita-eabi") - .join("lib") - .to_string_lossy() - .into() - }); - - let openssl_include = env::var("OPENSSL_INCLUDE_DIR").unwrap_or_else(|_| { - self.sdk("arm-vita-eabi") - .join("include") - .to_string_lossy() - .into() - }); - command .env("RUSTFLAGS", rust_flags) .env("TARGET_CC", "arm-vita-eabi-gcc") .env("TARGET_CXX", "arm-vita-eabi-g++") - .env("OPENSSL_LIB_DIR", openssl_lib) - .env("OPENSSL_INCLUDE_DIR", openssl_include) + .pass_path_env("OPENSSL_LIB_DIR", || self.sdk("arm-vita-eabi").join("lib")) + .pass_path_env("OPENSSL_INCLUDE_DIR", || { + self.sdk("arm-vita-eabi").join("include") + }) + .pass_path_env("PKG_CONFIG_PATH", || { + self.sdk("arm-vita-eabi").join("lib").join("pkgconfig") + }) + .pass_env("PKG_CONFIG_SYSROOT_DIR", || &self.sdk) .env("VITASDK", &self.sdk) .arg("build") .arg("-Z") @@ -536,3 +528,32 @@ impl<'a> BuildContext<'a> { Ok(()) } } + +trait CommandExt { + fn pass_env(&mut self, key: K, default: impl Fn() -> V) -> &mut Command + where + K: AsRef, + V: AsRef; + + fn pass_path_env(&mut self, key: K, default: impl Fn() -> V) -> &mut Command + where + K: AsRef, + V: AsRef, + { + self.pass_env(key, || default().as_ref().to_string_lossy().to_string()) + } +} + +impl CommandExt for Command { + fn pass_env(&mut self, key: K, default: impl Fn() -> V) -> &mut Command + where + K: AsRef, + V: AsRef, + { + let key = key.as_ref(); + match env::var(key) { + Ok(val) => self.env(key, val), + Err(_) => self.env(key, default().as_ref()), + } + } +}