From febedbe370a789281c2b53374ae112fe9a6a2555 Mon Sep 17 00:00:00 2001 From: Christophe Troestler Date: Thu, 29 Feb 2024 17:08:53 +0100 Subject: [PATCH] Add the path of Matplotlib to the link search (for Anaconda) --- build.rs | 30 ++++++++++++++++++++++++++++++ src/lib.rs | 4 +++- 2 files changed, 33 insertions(+), 1 deletion(-) create mode 100644 build.rs diff --git a/build.rs b/build.rs new file mode 100644 index 0000000..27b2b43 --- /dev/null +++ b/build.rs @@ -0,0 +1,30 @@ +use std::{env, + fs::File, + io::Write, + path::Path, + process::Command, +}; + +fn main() -> Result<(), Box> { + let outdir = env::var("OUT_DIR") + .expect("The OUT_DIR variable must be defined during compilation"); + let py = Path::new(&outdir).join("matplotlib_location.py"); + let mut fh = File::create(&py)?; + writeln!(fh, "import matplotlib\nprint(matplotlib.__file__)")?; + drop(fh); + + let plt_path = Command::new("python3") + .arg(py) + .output() + .expect("Python3 or matplotlib not found.") + .stdout; + let plt_path = String::from_utf8_lossy(&plt_path); + let plt_path = Path::new(plt_path.as_ref()); + + if let Some(d) = plt_path.parent() { + let d = d.to_str().unwrap(); + println!("cargo:rustc-link-search={d}"); + println!("cargo:rustc-env=RUSTFLAGS=-C link-arg=-Wl,-rpath,{d}"); + } + Ok(()) +} diff --git a/src/lib.rs b/src/lib.rs index 459ca1d..b17bd43 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -128,7 +128,9 @@ lazy_static! { /// ⚠ This may try to lock Python's GIL. Make sure it is executed /// outside a call to `Python::with_gil`. macro_rules! pymod { ($m: ident) => { - $m.as_ref().map_err(|_| Error::NoMatplotlib) + $m.as_ref().map_err(|e| { + eprintln!("............... {e}"); + Error::NoMatplotlib}) }}