Skip to content

Commit

Permalink
Attempt to auto detect library path on macOS (#102)
Browse files Browse the repository at this point in the history
The Vulkan SDK for macOS installs to /usr/local/ by default
when installing with sudo ./install_vulkan.py as documented.
This PR adds support for auto detecting this install location
without needing to set SHADERC_LIB_DIR or VULKAN_SDK.
  • Loading branch information
David Bailey authored Feb 1, 2021
1 parent 4b61d5a commit 6f64c36
Showing 1 changed file with 29 additions and 21 deletions.
50 changes: 29 additions & 21 deletions shaderc-sys/build/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -156,32 +156,40 @@ fn main() {
// If no explicit path is set and no explicit request is made to build from
// source, check known system locations before falling back to build from source.
// This set `search_dir` for later usage.
if search_dir.is_none() && target_os == "linux" && !config_build_from_source {
if search_dir.is_none() && !config_build_from_source {
println!(
"cargo:warning=shaderc: searching for native shaderc libraries on system; \
use '--features build-from-source' to force building from source code"
);

// https://wiki.ubuntu.com/MultiarchSpec
// https://wiki.debian.org/Multiarch/Implementation
let debian_arch = match env::var("CARGO_CFG_TARGET_ARCH").unwrap() {
arch if arch == "x86" => "i386".to_owned(),
arch => arch,
};
let debian_triple_path = format!("/usr/lib/{}-linux-gnu/", debian_arch);

search_dir = if Path::new(&debian_triple_path).exists() {
// Debian, Ubuntu and their derivatives.
Some(debian_triple_path)
} else if env::var("CARGO_CFG_TARGET_ARCH").unwrap() == "x86_64"
&& Path::new("/usr/lib64/").exists()
{
// Other distributions running on x86_64 usually use this path.
Some("/usr/lib64/".to_owned())
} else {
// Other distributions, not x86_64.
Some("/usr/lib/".to_owned())
};
if target_os == "macos" {
// Vulkan SDK is installed in `/usr/local/` by default on macOS
let macos_path = "/usr/local/lib/";
if Path::new(macos_path).exists() {
search_dir = Some(macos_path.to_owned());
}
} else if target_os == "linux" {
// https://wiki.ubuntu.com/MultiarchSpec
// https://wiki.debian.org/Multiarch/Implementation
let debian_arch = match env::var("CARGO_CFG_TARGET_ARCH").unwrap() {
arch if arch == "x86" => "i386".to_owned(),
arch => arch,
};
let debian_triple_path = format!("/usr/lib/{}-linux-gnu/", debian_arch);

search_dir = if Path::new(&debian_triple_path).exists() {
// Debian, Ubuntu and their derivatives.
Some(debian_triple_path)
} else if env::var("CARGO_CFG_TARGET_ARCH").unwrap() == "x86_64"
&& Path::new("/usr/lib64/").exists()
{
// Other distributions running on x86_64 usually use this path.
Some("/usr/lib64/".to_owned())
} else {
// Other distributions, not x86_64.
Some("/usr/lib/".to_owned())
};
}
}

// Canonicalize the sarch directory first.
Expand Down

0 comments on commit 6f64c36

Please sign in to comment.