Skip to content

Commit eb35c01

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 20162df commit eb35c01

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

curl-sys/build.rs

Lines changed: 30 additions & 0 deletions
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");
@@ -467,3 +478,22 @@ fn curl_config_reports_http2() -> bool {
467478

468479
return true;
469480
}
481+
482+
fn macos_link_search_path() -> Option<String> {
483+
let output = Command::new("clang").arg("--print-search-dirs").output().ok()?;
484+
if !output.status.success() {
485+
println!("failed to run 'clang --print-search-dirs', continuing without a link search path");
486+
return None;
487+
}
488+
489+
let stdout = String::from_utf8_lossy(&output.stdout);
490+
for line in stdout.lines() {
491+
if line.contains("libraries: =") {
492+
let path = line.split('=').skip(1).next()?;
493+
return Some(format!("{}/lib/darwin", path));
494+
}
495+
}
496+
497+
println!("failed to determine link search path, continuing without it");
498+
None
499+
}

0 commit comments

Comments
 (0)