Skip to content

Commit

Permalink
Add the path of Matplotlib to the link search (for Anaconda)
Browse files Browse the repository at this point in the history
  • Loading branch information
Chris00 committed Feb 29, 2024
1 parent ffbd993 commit febedbe
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 1 deletion.
30 changes: 30 additions & 0 deletions build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
use std::{env,
fs::File,
io::Write,
path::Path,
process::Command,
};

fn main() -> Result<(), Box<dyn std::error::Error>> {
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(())
}
4 changes: 3 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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})
}}


Expand Down

0 comments on commit febedbe

Please sign in to comment.