Skip to content

Commit

Permalink
Link against clang runtime on static builds on macOS
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
badboy committed Aug 22, 2019
1 parent 20162df commit eb35c01
Showing 1 changed file with 30 additions and 0 deletions.
30 changes: 30 additions & 0 deletions curl-sys/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,17 @@ fn main() {
.status();
}

if target.contains("apple") {
// On (older) OSX we need to link against the clang runtime,
// which is hidden in some non-default path.
//
// More details at https://github.com/alexcrichton/curl-rust/issues/279.
if let Some(path) = macos_link_search_path() {
println!("cargo:rustc-link-lib=clang_rt.osx");
println!("cargo:rustc-link-search={}", path);
}
}

let dst = PathBuf::from(env::var_os("OUT_DIR").unwrap());
let include = dst.join("include");
let build = dst.join("build");
Expand Down Expand Up @@ -467,3 +478,22 @@ fn curl_config_reports_http2() -> bool {

return true;
}

fn macos_link_search_path() -> Option<String> {
let output = Command::new("clang").arg("--print-search-dirs").output().ok()?;
if !output.status.success() {
println!("failed to run 'clang --print-search-dirs', continuing without a link search path");
return None;
}

let stdout = String::from_utf8_lossy(&output.stdout);
for line in stdout.lines() {
if line.contains("libraries: =") {
let path = line.split('=').skip(1).next()?;
return Some(format!("{}/lib/darwin", path));
}
}

println!("failed to determine link search path, continuing without it");
None
}

0 comments on commit eb35c01

Please sign in to comment.