From 3f1ed8d3e387746e87d68b2ad0878adb72213265 Mon Sep 17 00:00:00 2001 From: Yorick Peterse Date: Fri, 2 Aug 2024 19:02:46 +0200 Subject: [PATCH 1/2] Also include /usr/local/lib for FreeBSD This is needed to ensure that additional libraries such as zstd are found when building. --- build.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/build.rs b/build.rs index e74ad50..1ab7b44 100644 --- a/build.rs +++ b/build.rs @@ -351,7 +351,7 @@ fn get_system_libraries(llvm_config_path: &Path, kind: LibraryKind) -> Vec impl IntoIterator { - if target_os_is("openbsd") { + if target_os_is("openbsd") || target_os_is("freebsd") { Some("/usr/local/lib") } else { None -- GitLab From f84767ec931cbf7b2a304ce031e715bbbdf5e01f Mon Sep 17 00:00:00 2001 From: Yorick Peterse Date: Fri, 2 Aug 2024 22:09:34 +0200 Subject: [PATCH 2/2] Detect Homebrew LLVM paths on macOS Homebrew installs LLVM in non-standard locations, and compilers won't find the necessary libraries without fiddling around with `brew link`, PATH, and LIBRARY_PATH. This updates the build script such that it tries to automatically detect the necessary paths when using Homebrew, removing the need for any manual intervention. --- build.rs | 48 ++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 46 insertions(+), 2 deletions(-) diff --git a/build.rs b/build.rs index 1ab7b44..e4df16f 100644 --- a/build.rs +++ b/build.rs @@ -83,6 +83,28 @@ fn locate_llvm_config() -> Option { let prefix = env::var_os(&*ENV_LLVM_PREFIX) .map(|p| PathBuf::from(p).join("bin")) .unwrap_or_else(PathBuf::new); + + if let Some(x) = llvm_compatible_binary_name(&prefix) { + return Some(x); + } + + // For users of Homebrew, LLVM is located in a non-standard location and + // typically not linked such that it appears in PATH. We try to handle that + // here, but only if we didn't already find a working llvm-config in PATH. + // This removes the need for fiddling around with LIBRARY_PATH or `brew + // link`. + if target_os_is("macos") { + if let Some(p) = homebrew_prefix(Some(&format!("llvm@{}", CRATE_VERSION.major))) + .or_else(|| homebrew_prefix(Some("llvm"))) + { + return llvm_compatible_binary_name(&PathBuf::from(p).join("bin")); + } + } + + None +} + +fn llvm_compatible_binary_name(prefix: &Path) -> Option { for binary_name in llvm_config_binary_names() { let binary_name = prefix.join(binary_name); match llvm_version(&binary_name) { @@ -350,14 +372,36 @@ fn get_system_libraries(llvm_config_path: &Path, kind: LibraryKind) -> Vec impl IntoIterator { +fn get_system_library_dirs() -> impl IntoIterator { if target_os_is("openbsd") || target_os_is("freebsd") { - Some("/usr/local/lib") + Some("/usr/local/lib".to_string()) + } else if target_os_is("macos") { + if let Some(p) = homebrew_prefix(None) { + Some(format!("{}/lib", p)) + } else { + None + } } else { None } } +fn homebrew_prefix(name: Option<&str>) -> Option { + let mut cmd = Command::new("brew"); + + cmd.arg("--prefix"); + + if let Some(name) = name { + cmd.arg(name); + } + + cmd.output() + .ok() + .filter(|o| !o.stdout.is_empty()) + .and_then(|out| String::from_utf8(out.stdout).ok()) + .map(|val| val.trim().to_string()) +} + fn target_dylib_extension() -> &'static str { if target_os_is("macos") { ".dylib" -- GitLab