| |
1 //use anyhow::bail; |
| |
2 //use regex::Regex; |
| |
3 pub use python_config::{Error as PyCError, PythonConfig}; |
| |
4 use std::env; |
| |
5 use std::ffi::OsString; |
| |
6 use std::path::PathBuf; |
| |
7 //use std::process::Command; |
| |
8 |
| |
9 /// Adds CONDA_PREFIX/lib/pkgconfig/ to PKG_CONFIG_PATH |
| |
10 pub fn pkgconfig_conda() -> Result<Option<PathBuf>, anyhow::Error> { |
| |
11 if let Some(conda_root_) = option_env!("CONDA_PREFIX") { |
| |
12 let conda_root = PathBuf::from(conda_root_); |
| |
13 let conda_pkgconfig = conda_root.join("lib/pkgconfig/"); |
| |
14 |
| |
15 // Prepend CONDA_PREFIX to any existing PKG_CONFIG_PATH |
| |
16 let new_path = match option_env!("PKG_CONFIG_PATH") { |
| |
17 Some(path) => { |
| |
18 let mut paths = env::split_paths(&path).collect::<Vec<_>>(); |
| |
19 paths.insert(0, conda_pkgconfig); |
| |
20 env::join_paths(paths)? |
| |
21 } |
| |
22 None => conda_pkgconfig.into(), |
| |
23 }; |
| |
24 |
| |
25 // Set the environment variable |
| |
26 unsafe { |
| |
27 env::set_var("PKG_CONFIG_PATH", &new_path); |
| |
28 } |
| |
29 |
| |
30 Ok(Some(conda_root)) |
| |
31 } else { |
| |
32 Ok(None) |
| |
33 } |
| |
34 } |
| |
35 |
| |
36 /// Looks up conda Python |
| |
37 pub fn python_conda() -> Option<OsString> { |
| |
38 option_env!("CONDA_PREFIX").and_then(|conda_prefix| { |
| |
39 /* This does not point to the right place. |
| |
40 option_env!("CONDA_PYTHON_EXE") |
| |
41 .map(OsString::from) |
| |
42 .or_else(|| { */ |
| |
43 let py = PathBuf::from(conda_prefix).join("bin/python"); |
| |
44 if py.exists() { |
| |
45 Some(py.into()) |
| |
46 } else { |
| |
47 None |
| |
48 } |
| |
49 /* })*/ |
| |
50 }) |
| |
51 } |
| |
52 |
| |
53 /// Returns a the python configuration and indication whether it's from Conda |
| |
54 pub fn python_config() -> (Result<PythonConfig, PyCError>, bool) { |
| |
55 if let Some(py) = python_conda() { |
| |
56 (PythonConfig::interpreter(py), true) |
| |
57 } else { |
| |
58 (Ok(PythonConfig::new()), false) |
| |
59 } |
| |
60 } |
| |
61 |
| |
62 /* |
| |
63 /// Looks up python includes given executable |
| |
64 pub fn python_includes(mut py: OsString) -> Result<Vec<String>, anyhow::Error> { |
| |
65 py.push("3-config"); |
| |
66 let output = Command::new(&py).args(["--includes"]).output()?; |
| |
67 if !output.status.success() { |
| |
68 bail!("Failed to run {}.", py.display()) |
| |
69 } |
| |
70 let re = Regex::new(r"\w+-I\w*").unwrap(); |
| |
71 let includes = re |
| |
72 .split(format!(" {}", String::from_utf8(output.stdout)?).as_str()) |
| |
73 .map(String::from) |
| |
74 .collect(); |
| |
75 Ok(includes) |
| |
76 } |
| |
77 */ |