From 43b0f4ec713acd12d700be649cf2515a02adb28f Mon Sep 17 00:00:00 2001 From: "L. E. Segovia" Date: Fri, 6 Sep 2024 17:37:30 +0000 Subject: [PATCH] Add workaround for linking against macOS SDK's relocatable dylibs See https://github.com/rust-lang/cargo/issues/5077#issuecomment-1284482987 and https://github.com/rust-lang/rust/issues/127100 Part-of: --- Cargo.lock | 2 ++ examples/Cargo.toml | 6 ++++++ examples/build.rs | 18 ++++++++++++++++++ tutorials/Cargo.toml | 6 ++++++ tutorials/build.rs | 23 +++++++++++++++++++++++ 5 files changed, 55 insertions(+) create mode 100644 tutorials/build.rs diff --git a/Cargo.lock b/Cargo.lock index da8c82b71..7d75eda7d 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -513,6 +513,7 @@ dependencies = [ "pango", "pangocairo", "raw-window-handle", + "system-deps", "uds", "windows", "winit", @@ -2413,6 +2414,7 @@ dependencies = [ "gstreamer-pbutils", "gstreamer-video", "objc", + "system-deps", "termion", ] diff --git a/examples/Cargo.toml b/examples/Cargo.toml index c7f0695f2..0addf79f2 100644 --- a/examples/Cargo.toml +++ b/examples/Cargo.toml @@ -56,6 +56,12 @@ windows = { version = "0.58", features=["Win32_Graphics_Direct3D11", cocoa = "0.25" objc = "0.2.7" +[target.'cfg(target_os = "macos")'.build-dependencies] +system-deps = "7" + +[package.metadata.system-deps] +"gstreamer-1.0" = "1.14" + [build-dependencies] gl_generator = { version = "0.14", optional = true } diff --git a/examples/build.rs b/examples/build.rs index d447680a8..56d3b534a 100644 --- a/examples/build.rs +++ b/examples/build.rs @@ -19,4 +19,22 @@ fn generate_gl_bindings() {} fn main() { println!("cargo:rerun-if-changed=build.rs"); generate_gl_bindings(); + + // https://github.com/rust-lang/cargo/issues/5077#issuecomment-1284482987 + #[cfg(all(not(docsrs), target_os = "macos"))] + match system_deps::Config::new().probe() { + Ok(deps) => { + let usr = std::path::Path::new("/usr/lib"); + let usr_local = std::path::Path::new("/usr/local/lib"); + for dep in deps.all_link_paths() { + if dep != &usr && dep != &usr_local { + println!("cargo:rustc-link-arg=-Wl,-rpath,{:?}", dep.as_os_str()); + } + } + } + Err(s) => { + println!("cargo:warning={s}"); + std::process::exit(1); + } + } } diff --git a/tutorials/Cargo.toml b/tutorials/Cargo.toml index 5fea837d5..c783e4ea5 100644 --- a/tutorials/Cargo.toml +++ b/tutorials/Cargo.toml @@ -23,6 +23,12 @@ futures = "0.3" cocoa = "0.25" objc = "0.2.7" +[target.'cfg(target_os = "macos")'.build-dependencies] +system-deps = "7" + +[package.metadata.system-deps] +"gstreamer-1.0" = "1.14" + [[bin]] name = "basic-tutorial-13" required-features = ["termion"] diff --git a/tutorials/build.rs b/tutorials/build.rs new file mode 100644 index 000000000..66740cbdf --- /dev/null +++ b/tutorials/build.rs @@ -0,0 +1,23 @@ +#[cfg(docsrs)] +fn main() {} // prevent linking libraries to avoid documentation failure + +// https://github.com/rust-lang/cargo/issues/5077#issuecomment-1284482987 +#[cfg(not(docsrs))] +fn main() { + #[cfg(target_os = "macos")] + match system_deps::Config::new().probe() { + Ok(deps) => { + let usr = std::path::Path::new("/usr/lib"); + let usr_local = std::path::Path::new("/usr/local/lib"); + for dep in deps.all_link_paths() { + if dep != &usr && dep != &usr_local { + println!("cargo:rustc-link-arg=-Wl,-rpath,{:?}", dep.as_os_str()); + } + } + } + Err(s) => { + println!("cargo:warning={s}"); + std::process::exit(1); + } + } +}