| 1 |
use crate::{PythonInterpreter, Target}; |
| 2 |
use anyhow::{bail, Result}; |
| 3 |
use fs_err::{self as fs, DirEntry}; |
| 4 |
use normpath::PathExt as _; |
| 5 |
use std::collections::HashMap; |
| 6 |
use std::env; |
| 7 |
use std::path::{Path, PathBuf}; |
| 8 |
|
| 9 |
pub fn is_cross_compiling(target: &Target) -> Result<bool> { |
| 10 |
let target_triple = target.target_triple(); |
| 11 |
let host = target.host_triple(); |
| 12 |
if target_triple == host { |
| 13 |
// Not cross-compiling |
| 14 |
return Ok(false); |
| 15 |
} |
| 16 |
|
| 17 |
if target_triple == "x86_64-apple-darwin" && host == "aarch64-apple-darwin" { |
| 18 |
// Not cross-compiling to compile for x86-64 Python from macOS arm64 |
| 19 |
return Ok(false); |
| 20 |
} |
| 21 |
if target_triple == "aarch64-apple-darwin" && host == "x86_64-apple-darwin" { |
| 22 |
// Not cross-compiling to compile for arm64 Python from macOS x86_64 |
| 23 |
return Ok(false); |
| 24 |
} |
| 25 |
|
| 26 |
if target_triple.starts_with("i686-pc-windows") && host.starts_with("x86_64-pc-windows") { |
| 27 |
// Not cross-compiling to compile for 32-bit Python from windows 64-bit |
| 28 |
return Ok(false); |
| 29 |
} |
| 30 |
if target_triple.ends_with("windows-gnu") && host.ends_with("windows-msvc") { |
| 31 |
// Not cross-compiling to compile for Windows GNU from Windows MSVC host |
| 32 |
return Ok(false); |
| 33 |
} |
| 34 |
|
| 35 |
Ok(true) |
| 36 |
} |
| 37 |
|
| 38 |
/// Parse sysconfigdata file |
| 39 |
/// |
| 40 |
/// The sysconfigdata is simply a dictionary containing all the build time variables used for the |
| 41 |
/// python executable and library. Here it is read and added to a script to extract only what is |
| 42 |
/// necessary. This necessitates a python interpreter for the host machine to work. |
| 43 |
pub fn parse_sysconfigdata( |
| 44 |
interpreter: &PythonInterpreter, |
| 45 |
config_path: impl AsRef<Path>, |
| 46 |
) -> Result<HashMap<String, String>> { |
| 47 |
let mut script = fs::read_to_string(config_path)?; |
| 48 |
script += r#" |
| 49 |
print("version_major", build_time_vars["VERSION"][0]) # 3 |
| 50 |
print("version_minor", build_time_vars["VERSION"][2:]) # E.g., 8, 10 |
| 51 |
KEYS = [ |
| 52 |
"ABIFLAGS", |
| 53 |
"EXT_SUFFIX", |
| 54 |
"SOABI", |
| 55 |
"Py_GIL_DISABLED", |
| 56 |
] |
| 57 |
for key in KEYS: |
| 58 |
print(key, build_time_vars.get(key, "")) |
| 59 |
"#; |
| 60 |
let output = interpreter.run_script(&script)?; |
| 61 |
|
| 62 |
Ok(parse_script_output(&output)) |
| 63 |
} |
| 64 |
|
| 65 |
fn parse_script_output(output: &str) -> HashMap<String, String> { |
| 66 |
output |
| 67 |
.lines() |
| 68 |
.filter_map(|line| { |
| 69 |
line.split_once(' ') |
| 70 |
.map(|(x, y)| (x.to_string(), y.to_string())) |
| 71 |
}) |
| 72 |
.collect() |
| 73 |
} |
| 74 |
|
| 75 |
fn starts_with(entry: &DirEntry, pat: &str) -> bool { |
| 76 |
let name = entry.file_name(); |
| 77 |
name.to_string_lossy().starts_with(pat) |
| 78 |
} |
| 79 |
fn ends_with(entry: &DirEntry, pat: &str) -> bool { |
| 80 |
let name = entry.file_name(); |
| 81 |
name.to_string_lossy().ends_with(pat) |
| 82 |
} |
| 83 |
|
| 84 |
/// Finds the `_sysconfigdata*.py` file in the library path. |
| 85 |
/// |
| 86 |
/// From the python source for `_sysconfigdata*.py` is always going to be located at |
| 87 |
/// `build/lib.{PLATFORM}-{PY_MINOR_VERSION}` when built from source. The [exact line][1] is defined as: |
| 88 |
/// |
| 89 |
/// ```py |
| 90 |
/// pybuilddir = 'build/lib.%s-%s' % (get_platform(), sys.version_info[:2]) |
| 91 |
/// ``` |
| 92 |
/// |
| 93 |
/// Where get_platform returns a kebab-case formatted string containing the os, the architecture and |
| 94 |
/// possibly the os' kernel version (not the case on linux). However, when installed using a package |
| 95 |
/// manager, the `_sysconfigdata*.py` file is installed in the `${PREFIX}/lib/python3.Y/` directory. |
| 96 |
/// The `_sysconfigdata*.py` is generally in a sub-directory of the location of `libpython3.Y.so`. |
| 97 |
/// So we must find the file in the following possible locations: |
| 98 |
/// |
| 99 |
/// ```sh |
| 100 |
/// # distribution from package manager, lib_dir should include lib/ |
| 101 |
/// ${INSTALL_PREFIX}/lib/python3.Y/_sysconfigdata*.py |
| 102 |
/// ${INSTALL_PREFIX}/lib/libpython3.Y.so |
| 103 |
/// ${INSTALL_PREFIX}/lib/python3.Y/config-3.Y-${HOST_TRIPLE}/libpython3.Y.so |
| 104 |
/// |
| 105 |
/// # Built from source from host |
| 106 |
/// ${CROSS_COMPILED_LOCATION}/build/lib.linux-x86_64-Y/_sysconfigdata*.py |
| 107 |
/// ${CROSS_COMPILED_LOCATION}/libpython3.Y.so |
| 108 |
/// |
| 109 |
/// # if cross compiled, kernel release is only present on certain OS targets. |
| 110 |
/// ${CROSS_COMPILED_LOCATION}/build/lib.{OS}(-{OS-KERNEL-RELEASE})?-{ARCH}-Y/_sysconfigdata*.py |
| 111 |
/// ${CROSS_COMPILED_LOCATION}/libpython3.Y.so |
| 112 |
/// ``` |
| 113 |
/// |
| 114 |
/// [1]: https://github.com/python/cpython/blob/3.5/Lib/sysconfig.py#L389 |
| 115 |
pub fn find_sysconfigdata(lib_dir: &Path, target: &Target) -> Result<PathBuf> { |
| 116 |
let sysconfig_paths = search_lib_dir(lib_dir, target)?; |
| 117 |
let sysconfig_name = env::var_os("_PYTHON_SYSCONFIGDATA_NAME"); |
| 118 |
let mut sysconfig_paths = sysconfig_paths |
| 119 |
.iter() |
| 120 |
.filter_map(|p| { |
| 121 |
let canonical = p.normalize().ok().map(|p| p.into_path_buf()); |
| 122 |
match &sysconfig_name { |
| 123 |
Some(_) => canonical.filter(|p| p.file_stem() == sysconfig_name.as_deref()), |
| 124 |
None => canonical, |
| 125 |
} |
| 126 |
}) |
| 127 |
.collect::<Vec<PathBuf>>(); |
| 128 |
sysconfig_paths.dedup(); |
| 129 |
if sysconfig_paths.is_empty() { |
| 130 |
bail!("Could not find _sysconfigdata*.py in {}", lib_dir.display()); |
| 131 |
} else if sysconfig_paths.len() > 1 { |
| 132 |
bail!( |
| 133 |
"Detected multiple possible python versions, please set the PYO3_CROSS_PYTHON_VERSION \ |
| 134 |
variable to the wanted version on your system or set the _PYTHON_SYSCONFIGDATA_NAME \ |
| 135 |
variable to the wanted sysconfigdata file name\nsysconfigdata paths = {:?}", |
| 136 |
sysconfig_paths |
| 137 |
) |
| 138 |
} |
| 139 |
|
| 140 |
Ok(sysconfig_paths.remove(0)) |
| 141 |
} |
| 142 |
|
| 143 |
/// recursive search for _sysconfigdata, returns all possibilities of sysconfigdata paths |
| 144 |
fn search_lib_dir(path: impl AsRef<Path>, target: &Target) -> Result<Vec<PathBuf>> { |
| 145 |
let mut sysconfig_paths = vec![]; |
| 146 |
let (cpython_version_pat, pypy_version_pat) = if let Some(v) = |
| 147 |
env::var_os("PYO3_CROSS_PYTHON_VERSION").map(|s| s.into_string().unwrap()) |
| 148 |
{ |
| 149 |
(format!("python{v}"), format!("pypy{v}")) |
| 150 |
} else { |
| 151 |
("python3.".into(), "pypy3.".into()) |
| 152 |
}; |
| 153 |
for f in fs::read_dir(path.as_ref())? { |
| 154 |
let sysc = match &f { |
| 155 |
Ok(f) if starts_with(f, "_sysconfigdata") && ends_with(f, "py") => vec![f.path()], |
| 156 |
Ok(f) if starts_with(f, "build") => search_lib_dir(f.path(), target)?, |
| 157 |
Ok(f) if starts_with(f, "lib.") => { |
| 158 |
let name = f.file_name(); |
| 159 |
// check if right target os |
| 160 |
if !name.to_string_lossy().contains(target.get_python_os()) { |
| 161 |
continue; |
| 162 |
} |
| 163 |
// Check if right arch |
| 164 |
if !name |
| 165 |
.to_string_lossy() |
| 166 |
.contains(&target.target_arch().to_string()) |
| 167 |
{ |
| 168 |
continue; |
| 169 |
} |
| 170 |
search_lib_dir(f.path(), target)? |
| 171 |
} |
| 172 |
Ok(f) if starts_with(f, &cpython_version_pat) => search_lib_dir(f.path(), target)?, |
| 173 |
// PyPy 3.7: /opt/python/pp37-pypy37_pp73/lib_pypy/_sysconfigdata__linux_x86_64-linux-gnu.py |
| 174 |
Ok(f) if starts_with(f, "lib_pypy") => search_lib_dir(f.path(), target)?, |
| 175 |
// PyPy 3.8: /opt/python/pp38-pypy38_pp73/lib/pypy3.8/_sysconfigdata__linux_x86_64-linux-gnu.py |
| 176 |
Ok(f) if starts_with(f, &pypy_version_pat) => search_lib_dir(f.path(), target)?, |
| 177 |
Ok(f) if starts_with(f, "lib") && f.path().is_dir() => { |
| 178 |
search_lib_dir(f.path(), target)? |
| 179 |
} |
| 180 |
_ => continue, |
| 181 |
}; |
| 182 |
sysconfig_paths.extend(sysc); |
| 183 |
} |
| 184 |
// If we got more than one file, only take those that contain the arch name. |
| 185 |
// For ubuntu 20.04 with host architecture x86_64 and a foreign architecture of armhf |
| 186 |
// this reduces the number of candidates to 1: |
| 187 |
// |
| 188 |
// $ find /usr/lib/python3.8/ -name '_sysconfigdata*.py' -not -lname '*' |
| 189 |
// /usr/lib/python3.8/_sysconfigdata__x86_64-linux-gnu.py |
| 190 |
// /usr/lib/python3.8/_sysconfigdata__arm-linux-gnueabihf.py |
| 191 |
if sysconfig_paths.len() > 1 { |
| 192 |
let temp = sysconfig_paths |
| 193 |
.iter() |
| 194 |
.filter(|p| { |
| 195 |
p.to_string_lossy() |
| 196 |
.contains(&target.target_arch().to_string()) |
| 197 |
}) |
| 198 |
.cloned() |
| 199 |
.collect::<Vec<PathBuf>>(); |
| 200 |
if !temp.is_empty() { |
| 201 |
sysconfig_paths = temp; |
| 202 |
} |
| 203 |
} |
| 204 |
Ok(sysconfig_paths) |
| 205 |
} |