From b615af11e3f8dfcc7957d1f0b5a267c6e505c4c2 Mon Sep 17 00:00:00 2001 From: WillBux Date: Wed, 24 Sep 2025 15:15:53 +0200 Subject: [PATCH] feat: check for system liblsl presence Find via "LIBLSL_LIB_DIR" environment variable Can automatically find on macos if installed with homebrew --- lsl-sys/build.rs | 38 ++++++++++++++++++++++++++++++++++++-- 1 file changed, 36 insertions(+), 2 deletions(-) diff --git a/lsl-sys/build.rs b/lsl-sys/build.rs index 0747fa8..0bbe1f1 100644 --- a/lsl-sys/build.rs +++ b/lsl-sys/build.rs @@ -1,11 +1,45 @@ use std::env; +use std::path::Path; fn main() { - // TODO: find out if liblsl already present on system and usable (if so, link to that instead) - // println!("cargo:warning={}", "rebuilding..."); + // Check for system liblsl + if link_to_system_liblsl() { + return; + } + + println!("cargo:warning={}", "rebuilding..."); build_liblsl(); } +// Try to link to a system-installed dynamic liblsl +fn link_to_system_liblsl() -> bool { + // If lsl path set with environment variable + if let Ok(lib_path) = env::var("LIBLSL_LIB_DIR") { + println!("cargo:rustc-link-search=native={}", lib_path); + println!("cargo:rustc-link-lib=dylib=lsl"); + println!("liblsl at {}", lib_path); + return true; + } + + // Check homebrew path on macOS + #[cfg(target_os = "macos")] + { + if let Ok(homebrew) = env::var("HOMEBREW_PREFIX") { + let path = &format!("{}/lib", homebrew); + let lib_file = Path::new(path).join("liblsl.dylib"); + if lib_file.exists() { + println!("cargo:rustc-link-search=native={}", path); + println!("cargo:rustc-link-lib=dylib=lsl"); + println!("liblsl at {}", lib_file.display()); + return true; + } + } + } + // TODO: automatic finding for Linux and Windows + // Fallback to building from source + false +} + // Build the liblsl library from source using cmake fn build_liblsl() { let target = env::var("TARGET").unwrap();