Skip to content

Commit 31a058f

Browse files
committed
Link against clang runtime on static builds on macOS
On OSX we need to link against the clang runtime, which is hidden in some non-default path. We can get the path from `clang --print-search-dirs`. Kudos to @ehuss for finding that workaround. Fixes #279.
1 parent 1069cc5 commit 31a058f

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

curl-sys/build.rs

+30
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,17 @@ fn main() {
4747
.status();
4848
}
4949

50+
if target.contains("apple") {
51+
// On (older) OSX we need to link against the clang runtime,
52+
// which is hidden in some non-default path.
53+
//
54+
// More details at https://github.com/alexcrichton/curl-rust/issues/279.
55+
if let Some(path) = macos_link_search_path() {
56+
println!("cargo:rustc-link-lib=clang_rt.osx");
57+
println!("cargo:rustc-link-search={}", path);
58+
}
59+
}
60+
5061
let dst = PathBuf::from(env::var_os("OUT_DIR").unwrap());
5162
let include = dst.join("include");
5263
let build = dst.join("build");
@@ -480,3 +491,22 @@ fn curl_config_reports_http2() -> bool {
480491

481492
return true;
482493
}
494+
495+
fn macos_link_search_path() -> Option<String> {
496+
let output = Command::new("clang").arg("--print-search-dirs").output().ok()?;
497+
if !output.status.success() {
498+
println!("failed to run 'clang --print-search-dirs', continuing without a link search path");
499+
return None;
500+
}
501+
502+
let stdout = String::from_utf8_lossy(&output.stdout);
503+
for line in stdout.lines() {
504+
if line.contains("libraries: =") {
505+
let path = line.split('=').skip(1).next()?;
506+
return Some(format!("{}/lib/darwin", path));
507+
}
508+
}
509+
510+
println!("failed to determine link search path, continuing without it");
511+
None
512+
}

0 commit comments

Comments
 (0)