// Resolve the active sysroot (from the pinned rust-toolchain.toml) and // teach the linker where to find librustc_driver.dylib at build + run time. // Without this, the binary links but crashes at startup with "dyld: Library // not loaded: librustc_driver-*.dylib" — rustc_driver lives under // $sysroot/lib, not /usr/local/lib. use std::process::Command; fn main() { println!("cargo:rerun-if-changed=build.rs"); println!("cargo:rerun-if-changed=rust-toolchain.toml"); let out = Command::new("rustc") .arg("--print") .arg("sysroot") .output() .expect("failed to invoke rustc --print sysroot"); if !out.status.success() { panic!("rustc --print sysroot exited non-zero"); } let sysroot = String::from_utf8(out.stdout).expect("non-utf8 sysroot"); let sysroot = sysroot.trim(); let libdir = format!("{}/lib", sysroot); let version = Command::new("rustc") .arg("-Vv") .output() .expect("failed to invoke rustc -Vv"); if !version.status.success() { panic!("rustc -Vv exited non-zero"); } let version = String::from_utf8(version.stdout).expect("non-utf8 rustc version"); for (key, prefix) in [ ("FOLD_RUST_ANALYZER_RELEASE", "release: "), ("FOLD_RUST_ANALYZER_COMMIT", "commit-hash: "), ("FOLD_RUST_ANALYZER_HOST", "host: "), ] { let value = version .lines() .find_map(|line| line.strip_prefix(prefix)) .unwrap_or("unknown"); println!("cargo:rustc-env={}={}", key, value); } println!("cargo:rustc-link-search=native={}", libdir); // macOS dyld: rpath via -Wl,-rpath; Linux: -Wl,-rpath plus // -Wl,--enable-new-dtags. The double-link-arg form works on both. println!("cargo:rustc-link-arg=-Wl,-rpath,{}", libdir); }