-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add the path of Matplotlib to the link search (for Anaconda)
- Loading branch information
Showing
3 changed files
with
141 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
use std::{ | ||
env, | ||
path::{Path, PathBuf}, | ||
process::Command, | ||
}; | ||
|
||
fn conda_bin() -> String { | ||
match env::var("CONDA") { | ||
Ok(mut p) => { | ||
p.push_str("/bin/conda"); | ||
p | ||
} | ||
Err(_) => "conda".to_string() | ||
} | ||
} | ||
|
||
fn conda_path() -> Result<PathBuf, std::io::Error> { | ||
Command::new(conda_bin()).args(["info", "--base"]).output() | ||
.map(|o| { | ||
let base = String::from_utf8_lossy(&o.stdout); | ||
base.as_ref().trim().into() | ||
}) | ||
} | ||
|
||
fn main() { | ||
let matplotlib = "import matplotlib; print(matplotlib.__file__)"; | ||
|
||
let plt = Command::new("python3").args(["-c", matplotlib]).output(); | ||
if let Ok(plt) = plt { | ||
if plt.status.success() { | ||
// The Python in the PATH has matplotlib. | ||
return | ||
} | ||
} | ||
// See whether Anaconda Python is installed and has matplotlib. | ||
if let Ok(conda) = conda_path() { | ||
let python = conda.join("bin").join("python3"); | ||
let plt = Command::new(python).args(["-c", matplotlib]).output(); | ||
if let Ok(plt) = plt { | ||
if plt.status.success() { | ||
let plt = String::from_utf8_lossy(&plt.stdout); | ||
let plt = Path::new(plt.as_ref()); | ||
if let Some(d) = plt.parent().and_then(|d| d.parent()) { | ||
let d = d.to_str().unwrap(); | ||
println!("cargo:rustc-env=PYTHONHOME={d}"); | ||
return | ||
} | ||
} | ||
} | ||
} | ||
panic!("Python or package matplotlib not found (including in Anaconda)"); | ||
|
||
// if let Some(d) = plt_path.parent().and_then(|d| d.parent()) { | ||
// let d = d.to_str().unwrap(); | ||
// println!("cargo:rustc-env=MATPLOTLIB_DIR={d}"); | ||
// } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters