Skip to content

Commit a6969c0

Browse files
badboyalexcrichton
authored andcommitted
Link against clang runtime on static builds on macOS (#283)
* 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. * Fix formatting
1 parent 1069cc5 commit a6969c0

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed

curl-sys/build.rs

Lines changed: 35 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");
@@ -480,3 +491,27 @@ 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")
497+
.arg("--print-search-dirs")
498+
.output()
499+
.ok()?;
500+
if !output.status.success() {
501+
println!(
502+
"failed to run 'clang --print-search-dirs', continuing without a link search path"
503+
);
504+
return None;
505+
}
506+
507+
let stdout = String::from_utf8_lossy(&output.stdout);
508+
for line in stdout.lines() {
509+
if line.contains("libraries: =") {
510+
let path = line.split('=').skip(1).next()?;
511+
return Some(format!("{}/lib/darwin", path));
512+
}
513+
}
514+
515+
println!("failed to determine link search path, continuing without it");
516+
None
517+
}

0 commit comments

Comments
 (0)