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

Passing env variables for pkgconfig during cargo build #3

Merged
merged 1 commit into from
Oct 14, 2023
Merged
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
53 changes: 37 additions & 16 deletions src/commands/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -536,3 +528,32 @@ impl<'a> BuildContext<'a> {
Ok(())
}
}

trait CommandExt {
fn pass_env<K, V>(&mut self, key: K, default: impl Fn() -> V) -> &mut Command
where
K: AsRef<str>,
V: AsRef<str>;

fn pass_path_env<K, V>(&mut self, key: K, default: impl Fn() -> V) -> &mut Command
where
K: AsRef<str>,
V: AsRef<Path>,
{
self.pass_env(key, || default().as_ref().to_string_lossy().to_string())
}
}

impl CommandExt for Command {
fn pass_env<K, V>(&mut self, key: K, default: impl Fn() -> V) -> &mut Command
where
K: AsRef<str>,
V: AsRef<str>,
{
let key = key.as_ref();
match env::var(key) {
Ok(val) => self.env(key, val),
Err(_) => self.env(key, default().as_ref()),
}
}
}