From 6bfbdd63184e210e2e5cb976f00f2ad6cd333607 Mon Sep 17 00:00:00 2001 From: Dominik Maier Date: Tue, 8 Feb 2022 18:29:48 +0100 Subject: [PATCH] Add sdk linker flag for broken MacOS systems (#527) --- libafl_cc/build.rs | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/libafl_cc/build.rs b/libafl_cc/build.rs index 57f0c6f9f7..4648aa74ca 100644 --- a/libafl_cc/build.rs +++ b/libafl_cc/build.rs @@ -73,6 +73,18 @@ fn find_llvm_config() -> String { }) } +/// Use `xcrun` to get the path to the Xcode SDK tools library path, for linking +fn find_macos_sdk_libs() -> String { + let sdk_path_out = Command::new("xcrun") + .args(&["--show-sdk-path"]) + .output() + .expect("Failed to execute xcrun. Make sure you have Xcode installed and executed `sudo xcode-select --install`"); + format!( + "-L{}/usr/lib", + String::from_utf8(sdk_path_out.stdout).unwrap().trim() + ) +} + #[allow(clippy::too_many_lines)] fn main() { let out_dir = env::var_os("OUT_DIR").unwrap(); @@ -150,11 +162,16 @@ fn main() { let cxxflags: Vec<&str> = cxxflags.trim().split_whitespace().collect(); let mut ldflags: Vec<&str> = ldflags.trim().split_whitespace().collect(); + let sdk_path; if env::var("CARGO_CFG_TARGET_VENDOR").unwrap().as_str() == "apple" { // Needed on macos. // Explanation at https://github.com/banach-space/llvm-tutor/blob/787b09ed31ff7f0e7bdd42ae20547d27e2991512/lib/CMakeLists.txt#L59 ldflags.push("-undefined"); ldflags.push("dynamic_lookup"); + + // In case the system is configured oddly, we may have trouble finding the SDK. Manually add the linker flag, just in case. + sdk_path = find_macos_sdk_libs(); + ldflags.push(&sdk_path); }; println!("cargo:rerun-if-changed=src/common-llvm.h");