Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions source/ports/rs_port/sys/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
[package]
name = "metacall-sys"
version = "0.1.0"
repository = "https://github.com/metacall/core/tree/develop/source/ports/rs_port"
version = "0.1.1"
repository = "https://github.com/metacall/core/tree/develop/source/ports/rs_port/sys"
keywords = ["ffi", "bindings", "metacall"]
edition = "2021"
license = "Apache-2.0"
description = "Crate for finding metacall library in the system."

[lib]
crate-type = ["lib"]
doctest = false
edition = "2021"
name = "metacall_sys"
path = "src/lib.rs"
42 changes: 42 additions & 0 deletions source/ports/rs_port/sys/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,45 @@ fn define_library_search_path(env_var: &str, separator: &str, path: &Path) -> St
format!("{}={}", env_var, combined)
}

/// Set RPATH for runtime library discovery
/// This binaries work outside cargo
fn set_rpath(lib_path: &Path) {
let path_str = lib_path.to_str().unwrap();

#[cfg(target_os = "linux")]
{
// On Linux, use RPATH with $ORIGIN for relocatable binaries
println!("cargo:rustc-link-arg=-Wl,-rpath,{}", path_str);
// Also set a backup rpath relative to the executable location
println!("cargo:rustc-link-arg=-Wl,-rpath,$ORIGIN");
println!("cargo:rustc-link-arg=-Wl,-rpath,$ORIGIN/../lib");
}

#[cfg(target_os = "macos")]
{
// On macOS, use @rpath and @loader_path
println!("cargo:rustc-link-arg=-Wl,-rpath,{}", path_str);
// Also set loader-relative paths for relocatable binaries
println!("cargo:rustc-link-arg=-Wl,-rpath,@loader_path");
println!("cargo:rustc-link-arg=-Wl,-rpath,@loader_path/../lib");
}

#[cfg(target_os = "aix")]
{
// Add default system library paths to avoid breaking standard lookup
println!("cargo:rustc-link-arg=-Wl,-blibpath:{}:/usr/lib:/lib", path_str);
}

#[cfg(target_os = "windows")]
{
// Windows doesn't use RPATH, but we can inform the user
println!(
"cargo:warning=On Windows, make sure {} is in your PATH or next to your executable",
path_str
);
}
}

pub fn build() {
// When running tests from CMake
if let Ok(val) = env::var("PROJECT_OUTPUT_DIR") {
Expand Down Expand Up @@ -216,6 +255,9 @@ pub fn build() {
println!("cargo:rustc-link-search=native={}", lib_path.path.display());
println!("cargo:rustc-link-lib=dylib={}", lib_path.library);

// Set RPATH so the binary can find libraries at runtime
set_rpath(&lib_path.path);

// Set the runtime environment variable for finding the library during tests
#[cfg(target_os = "linux")]
const ENV_VAR: &str = "LD_LIBRARY_PATH";
Expand Down
Loading