|
|
|
@ -12,6 +12,10 @@ use std::{
|
|
|
|
|
str::FromStr,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
pub use target_lexicon::Triple;
|
|
|
|
|
|
|
|
|
|
use target_lexicon::{Architecture, BinaryFormat, Environment, OperatingSystem, Vendor};
|
|
|
|
|
|
|
|
|
|
use crate::{
|
|
|
|
|
bail, ensure,
|
|
|
|
|
errors::{Context, Error, Result},
|
|
|
|
@ -40,6 +44,16 @@ pub fn env_var(var: &str) -> Option<OsString> {
|
|
|
|
|
env::var_os(var)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Gets the compilation target triple from environment variables set by Cargo.
|
|
|
|
|
///
|
|
|
|
|
/// Must be called from a crate build script.
|
|
|
|
|
pub fn target_triple_from_env() -> Triple {
|
|
|
|
|
env::var("TARGET")
|
|
|
|
|
.expect("target_triple_from_env() must be called from a build script")
|
|
|
|
|
.parse()
|
|
|
|
|
.expect("Unrecognized TARGET environment variable value")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Configuration needed by PyO3 to build for the correct Python implementation.
|
|
|
|
|
///
|
|
|
|
|
/// Usually this is queried directly from the Python interpreter, or overridden using the
|
|
|
|
@ -524,6 +538,29 @@ print("mingw", get_platform().startswith("mingw"))
|
|
|
|
|
envs,
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Lowers the configured version to the abi3 version, if set.
|
|
|
|
|
fn fixup_for_abi3_version(&mut self, abi3_version: Option<PythonVersion>) -> Result<()> {
|
|
|
|
|
// PyPy doesn't support abi3; don't adjust the version
|
|
|
|
|
if self.implementation.is_pypy() {
|
|
|
|
|
return Ok(());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if let Some(version) = abi3_version {
|
|
|
|
|
ensure!(
|
|
|
|
|
version <= self.version,
|
|
|
|
|
"cannot set a minimum Python version {} higher than the interpreter version {} \
|
|
|
|
|
(the minimum Python version is implied by the abi3-py3{} feature)",
|
|
|
|
|
version,
|
|
|
|
|
self.version,
|
|
|
|
|
version.minor,
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
self.version = version;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord)]
|
|
|
|
@ -608,52 +645,37 @@ fn is_abi3() -> bool {
|
|
|
|
|
cargo_env_var("CARGO_FEATURE_ABI3").is_some()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[derive(Debug, PartialEq)]
|
|
|
|
|
struct TargetInfo {
|
|
|
|
|
/// The `arch` component of the compilation target triple.
|
|
|
|
|
///
|
|
|
|
|
/// e.g. x86_64, i386, arm, thumb, mips, etc.
|
|
|
|
|
arch: String,
|
|
|
|
|
|
|
|
|
|
/// The `vendor` component of the compilation target triple.
|
|
|
|
|
///
|
|
|
|
|
/// e.g. apple, pc, unknown, etc.
|
|
|
|
|
vendor: String,
|
|
|
|
|
|
|
|
|
|
/// The `os` component of the compilation target triple.
|
|
|
|
|
///
|
|
|
|
|
/// e.g. darwin, freebsd, linux, windows, etc.
|
|
|
|
|
os: String,
|
|
|
|
|
/// Gets the minimum supported Python version from PyO3 `abi3-py*` features.
|
|
|
|
|
///
|
|
|
|
|
/// Must be called from a PyO3 crate build script.
|
|
|
|
|
pub fn get_abi3_version() -> Option<PythonVersion> {
|
|
|
|
|
let minor_version = (MINIMUM_SUPPORTED_VERSION.minor..=ABI3_MAX_MINOR)
|
|
|
|
|
.find(|i| cargo_env_var(&format!("CARGO_FEATURE_ABI3_PY3{}", i)).is_some());
|
|
|
|
|
minor_version.map(|minor| PythonVersion { major: 3, minor })
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl TargetInfo {
|
|
|
|
|
fn from_cargo_env() -> Result<Self> {
|
|
|
|
|
Ok(Self {
|
|
|
|
|
arch: cargo_env_var("CARGO_CFG_TARGET_ARCH")
|
|
|
|
|
.ok_or("expected CARGO_CFG_TARGET_ARCH env var")?,
|
|
|
|
|
vendor: cargo_env_var("CARGO_CFG_TARGET_VENDOR")
|
|
|
|
|
.ok_or("expected CARGO_CFG_TARGET_VENDOR env var")?,
|
|
|
|
|
os: cargo_env_var("CARGO_CFG_TARGET_OS")
|
|
|
|
|
.ok_or("expected CARGO_CFG_TARGET_OS env var")?,
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
/// Checks if the `extension-module` feature is enabled for the PyO3 crate.
|
|
|
|
|
///
|
|
|
|
|
/// Must be called from a PyO3 crate build script.
|
|
|
|
|
pub fn is_extension_module() -> bool {
|
|
|
|
|
cargo_env_var("CARGO_FEATURE_EXTENSION_MODULE").is_some()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn to_target_triple(&self) -> String {
|
|
|
|
|
format!(
|
|
|
|
|
"{}-{}-{}",
|
|
|
|
|
if self.arch == "x86" {
|
|
|
|
|
"i686"
|
|
|
|
|
} else {
|
|
|
|
|
&self.arch
|
|
|
|
|
},
|
|
|
|
|
self.vendor,
|
|
|
|
|
if self.os == "macos" {
|
|
|
|
|
"darwin"
|
|
|
|
|
} else {
|
|
|
|
|
&self.os
|
|
|
|
|
}
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
/// Checks if we need to link to `libpython` for the current build target.
|
|
|
|
|
///
|
|
|
|
|
/// Must be called from a crate PyO3 build script.
|
|
|
|
|
pub fn is_linking_libpython() -> bool {
|
|
|
|
|
is_linking_libpython_for_target(&target_triple_from_env())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Checks if we need to link to `libpython` for the target.
|
|
|
|
|
///
|
|
|
|
|
/// Must be called from a crate PyO3 build script.
|
|
|
|
|
fn is_linking_libpython_for_target(target: &Triple) -> bool {
|
|
|
|
|
target.operating_system == OperatingSystem::Windows
|
|
|
|
|
|| target.environment == Environment::Android
|
|
|
|
|
|| target.environment == Environment::Androideabi
|
|
|
|
|
|| !is_extension_module()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Configuration needed by PyO3 to cross-compile for a target platform.
|
|
|
|
@ -668,32 +690,63 @@ pub struct CrossCompileConfig {
|
|
|
|
|
/// The version of the Python library to link against.
|
|
|
|
|
version: Option<PythonVersion>,
|
|
|
|
|
|
|
|
|
|
/// The target information
|
|
|
|
|
target_info: TargetInfo,
|
|
|
|
|
/// The compile target triple (e.g. aarch64-unknown-linux-gnu)
|
|
|
|
|
target: Triple,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl CrossCompileConfig {
|
|
|
|
|
fn from_env_vars(env_vars: CrossCompileEnvVars, target_info: TargetInfo) -> Result<Self> {
|
|
|
|
|
Ok(CrossCompileConfig {
|
|
|
|
|
lib_dir: env_vars
|
|
|
|
|
.pyo3_cross_lib_dir
|
|
|
|
|
.ok_or(
|
|
|
|
|
"The PYO3_CROSS_LIB_DIR environment variable must be set when cross-compiling",
|
|
|
|
|
)?
|
|
|
|
|
.into(),
|
|
|
|
|
target_info,
|
|
|
|
|
version: env_vars
|
|
|
|
|
.pyo3_cross_python_version
|
|
|
|
|
.map(|os_string| {
|
|
|
|
|
let utf8_str = os_string
|
|
|
|
|
.to_str()
|
|
|
|
|
.ok_or("PYO3_CROSS_PYTHON_VERSION is not valid utf-8.")?;
|
|
|
|
|
utf8_str
|
|
|
|
|
.parse()
|
|
|
|
|
.context("failed to parse PYO3_CROSS_PYTHON_VERSION")
|
|
|
|
|
})
|
|
|
|
|
.transpose()?,
|
|
|
|
|
})
|
|
|
|
|
/// Creates a new cross compile config struct from PyO3 environment variables
|
|
|
|
|
/// and the build environment when cross compilation mode is detected.
|
|
|
|
|
///
|
|
|
|
|
/// Returns `None` when not cross compiling.
|
|
|
|
|
fn try_from_env_vars_host_target(
|
|
|
|
|
env_vars: CrossCompileEnvVars,
|
|
|
|
|
host: &Triple,
|
|
|
|
|
target: &Triple,
|
|
|
|
|
) -> Result<Option<Self>> {
|
|
|
|
|
if env_vars.any() || Self::is_cross_compiling_from_to(host, target) {
|
|
|
|
|
let lib_dir = env_vars.lib_dir_path()?;
|
|
|
|
|
let version = env_vars.parse_version()?;
|
|
|
|
|
let target = target.clone();
|
|
|
|
|
|
|
|
|
|
Ok(Some(CrossCompileConfig {
|
|
|
|
|
lib_dir,
|
|
|
|
|
target,
|
|
|
|
|
version,
|
|
|
|
|
}))
|
|
|
|
|
} else {
|
|
|
|
|
Ok(None)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Checks if compiling on `host` for `target` required "real" cross compilation.
|
|
|
|
|
///
|
|
|
|
|
/// Returns `false` if the target Python interpreter can run on the host.
|
|
|
|
|
fn is_cross_compiling_from_to(host: &Triple, target: &Triple) -> bool {
|
|
|
|
|
// Not cross-compiling if arch-vendor-os is all the same
|
|
|
|
|
// e.g. x86_64-unknown-linux-musl on x86_64-unknown-linux-gnu host
|
|
|
|
|
// x86_64-pc-windows-gnu on x86_64-pc-windows-msvc host
|
|
|
|
|
let mut compatible = host.architecture == target.architecture
|
|
|
|
|
&& host.vendor == target.vendor
|
|
|
|
|
&& host.operating_system == target.operating_system;
|
|
|
|
|
|
|
|
|
|
// Not cross-compiling to compile for 32-bit Python from windows 64-bit
|
|
|
|
|
compatible |= target.operating_system == OperatingSystem::Windows
|
|
|
|
|
&& host.operating_system == OperatingSystem::Windows;
|
|
|
|
|
|
|
|
|
|
// Not cross-compiling to compile for x86-64 Python from macOS arm64 and vice versa
|
|
|
|
|
compatible |= target.operating_system == OperatingSystem::Darwin
|
|
|
|
|
&& host.operating_system == OperatingSystem::Darwin;
|
|
|
|
|
|
|
|
|
|
!compatible
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Converts `lib_dir` member field to an UTF-8 string.
|
|
|
|
|
///
|
|
|
|
|
/// The conversion can not fail because `PYO3_CROSS_LIB_DIR` variable
|
|
|
|
|
/// is ensured contain a valid UTF-8 string.
|
|
|
|
|
fn lib_dir_string(&self) -> String {
|
|
|
|
|
self.lib_dir.to_str().unwrap().to_owned()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -709,6 +762,44 @@ impl CrossCompileEnvVars {
|
|
|
|
|
|| self.pyo3_cross_lib_dir.is_some()
|
|
|
|
|
|| self.pyo3_cross_python_version.is_some()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Parses `PYO3_CROSS_PYTHON_VERSION` environment variable value
|
|
|
|
|
/// into `PythonVersion`.
|
|
|
|
|
fn parse_version(&self) -> Result<Option<PythonVersion>> {
|
|
|
|
|
let version = self
|
|
|
|
|
.pyo3_cross_python_version
|
|
|
|
|
.as_ref()
|
|
|
|
|
.map(|os_string| {
|
|
|
|
|
let utf8_str = os_string
|
|
|
|
|
.to_str()
|
|
|
|
|
.ok_or("PYO3_CROSS_PYTHON_VERSION is not valid a UTF-8 string")?;
|
|
|
|
|
utf8_str
|
|
|
|
|
.parse()
|
|
|
|
|
.context("failed to parse PYO3_CROSS_PYTHON_VERSION")
|
|
|
|
|
})
|
|
|
|
|
.transpose()?;
|
|
|
|
|
|
|
|
|
|
Ok(version)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Converts the stored `PYO3_CROSS_LIB_DIR` variable value (if any)
|
|
|
|
|
/// into a `PathBuf` instance.
|
|
|
|
|
///
|
|
|
|
|
/// Ensures that the path is a valid UTF-8 string.
|
|
|
|
|
fn lib_dir_path(&self) -> Result<PathBuf> {
|
|
|
|
|
let lib_dir = self.pyo3_cross_lib_dir.as_ref().map(PathBuf::from);
|
|
|
|
|
|
|
|
|
|
if let Some(dir) = lib_dir.as_ref() {
|
|
|
|
|
ensure!(
|
|
|
|
|
dir.to_str().is_some(),
|
|
|
|
|
"PYO3_CROSS_LIB_DIR variable value is not a valid UTF-8 string"
|
|
|
|
|
);
|
|
|
|
|
Ok(dir.clone())
|
|
|
|
|
} else {
|
|
|
|
|
// FIXME: Relax this restriction in the future.
|
|
|
|
|
bail!("The PYO3_CROSS_LIB_DIR environment variable must be set when cross-compiling")
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub(crate) fn cross_compile_env_vars() -> CrossCompileEnvVars {
|
|
|
|
@ -733,39 +824,76 @@ pub(crate) fn cross_compile_env_vars() -> CrossCompileEnvVars {
|
|
|
|
|
/// `PYO3_CROSS_LIB_DIR`.
|
|
|
|
|
///
|
|
|
|
|
/// See the [PyO3 User Guide](https://pyo3.rs/) for more info on cross-compiling.
|
|
|
|
|
#[deprecated(
|
|
|
|
|
since = "0.17.0",
|
|
|
|
|
note = "please use cross_compiling_from_to() instead"
|
|
|
|
|
)]
|
|
|
|
|
pub fn cross_compiling(
|
|
|
|
|
host: &str,
|
|
|
|
|
target_arch: &str,
|
|
|
|
|
target_vendor: &str,
|
|
|
|
|
target_os: &str,
|
|
|
|
|
) -> Result<Option<CrossCompileConfig>> {
|
|
|
|
|
let env_vars = cross_compile_env_vars();
|
|
|
|
|
let host: Triple = host.parse().map_err(|_| "bad host triple")?;
|
|
|
|
|
|
|
|
|
|
let target_info = TargetInfo {
|
|
|
|
|
arch: target_arch.to_owned(),
|
|
|
|
|
vendor: target_vendor.to_owned(),
|
|
|
|
|
os: target_os.to_owned(),
|
|
|
|
|
let architecture: Architecture = target_arch.parse().map_err(|_| "bad target arch")?;
|
|
|
|
|
let vendor: Vendor = target_vendor.parse().map_err(|_| "bad target vendor")?;
|
|
|
|
|
let operating_system: OperatingSystem = target_os.parse().map_err(|_| "bad target os")?;
|
|
|
|
|
|
|
|
|
|
// FIXME: This is a very bad approximation that only works
|
|
|
|
|
// for the current `CrossCompileConfig` implementation.
|
|
|
|
|
let environment = match operating_system {
|
|
|
|
|
OperatingSystem::Windows => Environment::Msvc,
|
|
|
|
|
_ => Environment::Gnu,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
if !env_vars.any() && is_not_cross_compiling(host, &target_info) {
|
|
|
|
|
return Ok(None);
|
|
|
|
|
}
|
|
|
|
|
// FIXME: This field is currently unused.
|
|
|
|
|
let binary_format = BinaryFormat::Elf;
|
|
|
|
|
|
|
|
|
|
CrossCompileConfig::from_env_vars(env_vars, target_info).map(Some)
|
|
|
|
|
let target = Triple {
|
|
|
|
|
architecture,
|
|
|
|
|
vendor,
|
|
|
|
|
operating_system,
|
|
|
|
|
environment,
|
|
|
|
|
binary_format,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
cross_compiling_from_to(&host, &target)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn is_not_cross_compiling(host: &str, target_info: &TargetInfo) -> bool {
|
|
|
|
|
let target_triple = target_info.to_target_triple();
|
|
|
|
|
// Not cross-compiling if arch-vendor-os is all the same
|
|
|
|
|
// e.g. x86_64-unknown-linux-musl on x86_64-unknown-linux-gnu host
|
|
|
|
|
// x86_64-pc-windows-gnu on x86_64-pc-windows-msvc host
|
|
|
|
|
host.starts_with(&target_triple)
|
|
|
|
|
// Not cross-compiling to compile for 32-bit Python from windows 64-bit
|
|
|
|
|
|| (target_triple == "i686-pc-windows" && host.starts_with("x86_64-pc-windows"))
|
|
|
|
|
// Not cross-compiling to compile for x86-64 Python from macOS arm64
|
|
|
|
|
|| (target_triple == "x86_64-apple-darwin" && host == "aarch64-apple-darwin")
|
|
|
|
|
// Not cross-compiling to compile for arm64 Python from macOS x86_64
|
|
|
|
|
|| (target_triple == "aarch64-apple-darwin" && host == "x86_64-apple-darwin")
|
|
|
|
|
/// Detect whether we are cross compiling and return an assembled CrossCompileConfig if so.
|
|
|
|
|
///
|
|
|
|
|
/// This function relies on PyO3 cross-compiling environment variables:
|
|
|
|
|
///
|
|
|
|
|
/// * `PYO3_CROSS`: If present, forces PyO3 to configure as a cross-compilation.
|
|
|
|
|
/// * `PYO3_CROSS_LIB_DIR`: If present, must be set to the directory containing
|
|
|
|
|
/// the target's libpython DSO and the associated `_sysconfigdata*.py` file for
|
|
|
|
|
/// Unix-like targets, or the Python DLL import libraries for the Windows target.
|
|
|
|
|
/// * `PYO3_CROSS_PYTHON_VERSION`: Major and minor version (e.g. 3.9) of the target Python
|
|
|
|
|
/// installation. This variable is only needed if PyO3 cannnot determine the version to target
|
|
|
|
|
/// from `abi3-py3*` features, or if there are multiple versions of Python present in
|
|
|
|
|
/// `PYO3_CROSS_LIB_DIR`.
|
|
|
|
|
///
|
|
|
|
|
/// See the [PyO3 User Guide](https://pyo3.rs/) for more info on cross-compiling.
|
|
|
|
|
pub fn cross_compiling_from_to(
|
|
|
|
|
host: &Triple,
|
|
|
|
|
target: &Triple,
|
|
|
|
|
) -> Result<Option<CrossCompileConfig>> {
|
|
|
|
|
let env_vars = cross_compile_env_vars();
|
|
|
|
|
CrossCompileConfig::try_from_env_vars_host_target(env_vars, host, target)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Detect whether we are cross compiling from Cargo and `PYO3_CROSS_*` environment
|
|
|
|
|
/// variables and return an assembled `CrossCompileConfig` if so.
|
|
|
|
|
///
|
|
|
|
|
/// This must be called from PyO3's build script, because it relies on environment
|
|
|
|
|
/// variables such as `CARGO_CFG_TARGET_OS` which aren't available at any other time.
|
|
|
|
|
pub fn cross_compiling_from_cargo_env() -> Result<Option<CrossCompileConfig>> {
|
|
|
|
|
let env_vars = cross_compile_env_vars();
|
|
|
|
|
let host = Triple::host();
|
|
|
|
|
let target = target_triple_from_env();
|
|
|
|
|
|
|
|
|
|
CrossCompileConfig::try_from_env_vars_host_target(env_vars, &host, &target)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[allow(non_camel_case_types)]
|
|
|
|
@ -1091,15 +1219,11 @@ fn search_lib_dir(path: impl AsRef<Path>, cross: &CrossCompileConfig) -> Vec<Pat
|
|
|
|
|
search_lib_dir(f.path(), cross)
|
|
|
|
|
} else if file_name.starts_with("lib.") {
|
|
|
|
|
// check if right target os
|
|
|
|
|
if !file_name.contains(if cross.target_info.os == "android" {
|
|
|
|
|
"linux"
|
|
|
|
|
} else {
|
|
|
|
|
&cross.target_info.os
|
|
|
|
|
}) {
|
|
|
|
|
if !file_name.contains(&cross.target.operating_system.to_string()) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
// Check if right arch
|
|
|
|
|
if !file_name.contains(&cross.target_info.arch) {
|
|
|
|
|
if !file_name.contains(&cross.target.architecture.to_string()) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
search_lib_dir(f.path(), cross)
|
|
|
|
@ -1124,7 +1248,10 @@ fn search_lib_dir(path: impl AsRef<Path>, cross: &CrossCompileConfig) -> Vec<Pat
|
|
|
|
|
if sysconfig_paths.len() > 1 {
|
|
|
|
|
let temp = sysconfig_paths
|
|
|
|
|
.iter()
|
|
|
|
|
.filter(|p| p.to_string_lossy().contains(&cross.target_info.arch))
|
|
|
|
|
.filter(|p| {
|
|
|
|
|
p.to_string_lossy()
|
|
|
|
|
.contains(&cross.target.architecture.to_string())
|
|
|
|
|
})
|
|
|
|
|
.cloned()
|
|
|
|
|
.collect::<Vec<PathBuf>>();
|
|
|
|
|
if !temp.is_empty() {
|
|
|
|
@ -1150,11 +1277,19 @@ fn cross_compile_from_sysconfigdata(
|
|
|
|
|
fn windows_hardcoded_cross_compile(
|
|
|
|
|
cross_compile_config: CrossCompileConfig,
|
|
|
|
|
) -> Result<InterpreterConfig> {
|
|
|
|
|
let version = cross_compile_config.version.or_else(get_abi3_version)
|
|
|
|
|
.ok_or("PYO3_CROSS_PYTHON_VERSION or an abi3-py3* feature must be specified when cross-compiling for Windows.")?;
|
|
|
|
|
let version = cross_compile_config
|
|
|
|
|
.version
|
|
|
|
|
.or_else(get_abi3_version)
|
|
|
|
|
.ok_or(
|
|
|
|
|
"PYO3_CROSS_PYTHON_VERSION or an abi3-py3* feature must be specified \
|
|
|
|
|
when cross-compiling and PYO3_CROSS_LIB_DIR is not set.",
|
|
|
|
|
)?;
|
|
|
|
|
|
|
|
|
|
let abi3 = is_abi3();
|
|
|
|
|
let implementation = PythonImplementation::CPython;
|
|
|
|
|
let mingw = cross_compile_config.target.environment == Environment::Gnu;
|
|
|
|
|
|
|
|
|
|
let lib_dir = Some(cross_compile_config.lib_dir_string());
|
|
|
|
|
|
|
|
|
|
Ok(InterpreterConfig {
|
|
|
|
|
implementation,
|
|
|
|
@ -1165,9 +1300,9 @@ fn windows_hardcoded_cross_compile(
|
|
|
|
|
version,
|
|
|
|
|
PythonImplementation::CPython,
|
|
|
|
|
abi3,
|
|
|
|
|
false,
|
|
|
|
|
mingw,
|
|
|
|
|
)),
|
|
|
|
|
lib_dir: cross_compile_config.lib_dir.to_str().map(String::from),
|
|
|
|
|
lib_dir,
|
|
|
|
|
executable: None,
|
|
|
|
|
pointer_width: None,
|
|
|
|
|
build_flags: BuildFlags::default(),
|
|
|
|
@ -1303,7 +1438,7 @@ fn conda_env_interpreter(conda_prefix: &OsStr, windows: bool) -> PathBuf {
|
|
|
|
|
|
|
|
|
|
fn get_env_interpreter() -> Option<PathBuf> {
|
|
|
|
|
match (env_var("VIRTUAL_ENV"), env_var("CONDA_PREFIX")) {
|
|
|
|
|
// Use cfg rather can CARGO_TARGET_OS because this affects where files are located on the
|
|
|
|
|
// Use cfg rather than CARGO_CFG_TARGET_OS because this affects where files are located on the
|
|
|
|
|
// build host
|
|
|
|
|
(Some(dir), None) => Some(venv_interpreter(&dir, cfg!(windows))),
|
|
|
|
|
(None, Some(dir)) => Some(conda_env_interpreter(&dir, cfg!(windows))),
|
|
|
|
@ -1347,62 +1482,16 @@ pub fn find_interpreter() -> Result<PathBuf> {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn get_abi3_version() -> Option<PythonVersion> {
|
|
|
|
|
let minor_version = (MINIMUM_SUPPORTED_VERSION.minor..=ABI3_MAX_MINOR)
|
|
|
|
|
.find(|i| cargo_env_var(&format!("CARGO_FEATURE_ABI3_PY3{}", i)).is_some());
|
|
|
|
|
minor_version.map(|minor| PythonVersion { major: 3, minor })
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Lowers the configured version to the abi3 version, if set.
|
|
|
|
|
fn fixup_config_for_abi3(
|
|
|
|
|
config: &mut InterpreterConfig,
|
|
|
|
|
abi3_version: Option<PythonVersion>,
|
|
|
|
|
) -> Result<()> {
|
|
|
|
|
// PyPy doesn't support abi3; don't adjust the version
|
|
|
|
|
if config.implementation.is_pypy() {
|
|
|
|
|
return Ok(());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if let Some(version) = abi3_version {
|
|
|
|
|
ensure!(
|
|
|
|
|
version <= config.version,
|
|
|
|
|
"cannot set a minimum Python version {} higher than the interpreter version {} \
|
|
|
|
|
(the minimum Python version is implied by the abi3-py3{} feature)",
|
|
|
|
|
version,
|
|
|
|
|
config.version,
|
|
|
|
|
version.minor,
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
config.version = version;
|
|
|
|
|
}
|
|
|
|
|
Ok(())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Generates an interpreter config suitable for cross-compilation.
|
|
|
|
|
///
|
|
|
|
|
/// This must be called from PyO3's build script, because it relies on environment variables such as
|
|
|
|
|
/// CARGO_CFG_TARGET_OS which aren't available at any other time.
|
|
|
|
|
pub fn make_cross_compile_config() -> Result<Option<InterpreterConfig>> {
|
|
|
|
|
let env_vars = cross_compile_env_vars();
|
|
|
|
|
|
|
|
|
|
let host = cargo_env_var("HOST").ok_or("expected HOST env var")?;
|
|
|
|
|
let target = cargo_env_var("TARGET").ok_or("expected TARGET env var")?;
|
|
|
|
|
|
|
|
|
|
let target_info = TargetInfo::from_cargo_env()?;
|
|
|
|
|
|
|
|
|
|
let interpreter_config = if env_vars.any() {
|
|
|
|
|
let cross_config = CrossCompileConfig::from_env_vars(env_vars, target_info)?;
|
|
|
|
|
let interpreter_config = if let Some(cross_config) = cross_compiling_from_cargo_env()? {
|
|
|
|
|
let mut interpreter_config = load_cross_compile_config(cross_config)?;
|
|
|
|
|
fixup_config_for_abi3(&mut interpreter_config, get_abi3_version())?;
|
|
|
|
|
interpreter_config.fixup_for_abi3_version(get_abi3_version())?;
|
|
|
|
|
Some(interpreter_config)
|
|
|
|
|
} else {
|
|
|
|
|
ensure!(
|
|
|
|
|
host == target || is_not_cross_compiling(&host, &target_info),
|
|
|
|
|
"PyO3 detected compile host {host} and build target {target}, but none of PYO3_CROSS, PYO3_CROSS_LIB_DIR \
|
|
|
|
|
or PYO3_CROSS_PYTHON_VERSION environment variables are set.",
|
|
|
|
|
host=host,
|
|
|
|
|
target=target,
|
|
|
|
|
);
|
|
|
|
|
None
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
@ -1414,13 +1503,14 @@ pub fn make_cross_compile_config() -> Result<Option<InterpreterConfig>> {
|
|
|
|
|
#[allow(dead_code)]
|
|
|
|
|
pub fn make_interpreter_config() -> Result<InterpreterConfig> {
|
|
|
|
|
let mut interpreter_config = InterpreterConfig::from_interpreter(find_interpreter()?)?;
|
|
|
|
|
fixup_config_for_abi3(&mut interpreter_config, get_abi3_version())?;
|
|
|
|
|
interpreter_config.fixup_for_abi3_version(get_abi3_version())?;
|
|
|
|
|
Ok(interpreter_config)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
|
mod tests {
|
|
|
|
|
use std::{io::Cursor, iter::FromIterator};
|
|
|
|
|
use target_lexicon::triple;
|
|
|
|
|
|
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
|
@ -1670,11 +1760,7 @@ mod tests {
|
|
|
|
|
let cross_config = CrossCompileConfig {
|
|
|
|
|
lib_dir: "C:\\some\\path".into(),
|
|
|
|
|
version: Some(PythonVersion { major: 3, minor: 7 }),
|
|
|
|
|
target_info: TargetInfo {
|
|
|
|
|
os: "os".into(),
|
|
|
|
|
arch: "arch".into(),
|
|
|
|
|
vendor: "vendor".into(),
|
|
|
|
|
},
|
|
|
|
|
target: triple!("i686-pc-windows-msvc"),
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
@ -1695,6 +1781,32 @@ mod tests {
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn mingw_hardcoded_cross_compile() {
|
|
|
|
|
let cross_config = CrossCompileConfig {
|
|
|
|
|
lib_dir: "/usr/lib/mingw".into(),
|
|
|
|
|
version: Some(PythonVersion { major: 3, minor: 8 }),
|
|
|
|
|
target: triple!("i686-pc-windows-gnu"),
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
|
super::windows_hardcoded_cross_compile(cross_config).unwrap(),
|
|
|
|
|
InterpreterConfig {
|
|
|
|
|
implementation: PythonImplementation::CPython,
|
|
|
|
|
version: PythonVersion { major: 3, minor: 8 },
|
|
|
|
|
shared: true,
|
|
|
|
|
abi3: false,
|
|
|
|
|
lib_name: Some("python3.8".into()),
|
|
|
|
|
lib_dir: Some("/usr/lib/mingw".into()),
|
|
|
|
|
executable: None,
|
|
|
|
|
pointer_width: None,
|
|
|
|
|
build_flags: BuildFlags::default(),
|
|
|
|
|
suppress_build_script_link_lines: false,
|
|
|
|
|
extra_build_script_lines: vec![],
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn default_lib_name_windows() {
|
|
|
|
|
use PythonImplementation::*;
|
|
|
|
@ -1780,6 +1892,36 @@ mod tests {
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn parse_cross_python_version() {
|
|
|
|
|
let env_vars = CrossCompileEnvVars {
|
|
|
|
|
pyo3_cross: None,
|
|
|
|
|
pyo3_cross_lib_dir: None,
|
|
|
|
|
pyo3_cross_python_version: Some("3.9".into()),
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
|
env_vars.parse_version().unwrap(),
|
|
|
|
|
Some(PythonVersion { major: 3, minor: 9 })
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
let env_vars = CrossCompileEnvVars {
|
|
|
|
|
pyo3_cross: None,
|
|
|
|
|
pyo3_cross_lib_dir: None,
|
|
|
|
|
pyo3_cross_python_version: None,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
assert_eq!(env_vars.parse_version().unwrap(), None);
|
|
|
|
|
|
|
|
|
|
let env_vars = CrossCompileEnvVars {
|
|
|
|
|
pyo3_cross: None,
|
|
|
|
|
pyo3_cross_lib_dir: None,
|
|
|
|
|
pyo3_cross_python_version: Some("100".into()),
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
assert!(env_vars.parse_version().is_err());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn interpreter_version_reduced_to_abi3() {
|
|
|
|
|
let mut config = InterpreterConfig {
|
|
|
|
@ -1796,7 +1938,9 @@ mod tests {
|
|
|
|
|
extra_build_script_lines: vec![],
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
fixup_config_for_abi3(&mut config, Some(PythonVersion { major: 3, minor: 7 })).unwrap();
|
|
|
|
|
config
|
|
|
|
|
.fixup_for_abi3_version(Some(PythonVersion { major: 3, minor: 7 }))
|
|
|
|
|
.unwrap();
|
|
|
|
|
assert_eq!(config.version, PythonVersion { major: 3, minor: 7 });
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -1816,12 +1960,13 @@ mod tests {
|
|
|
|
|
extra_build_script_lines: vec![],
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
assert!(
|
|
|
|
|
fixup_config_for_abi3(&mut config, Some(PythonVersion { major: 3, minor: 8 }))
|
|
|
|
|
assert!(config
|
|
|
|
|
.fixup_for_abi3_version(Some(PythonVersion { major: 3, minor: 8 }))
|
|
|
|
|
.unwrap_err()
|
|
|
|
|
.to_string()
|
|
|
|
|
.contains("cannot set a minimum Python version 3.8 higher than the interpreter version 3.7")
|
|
|
|
|
);
|
|
|
|
|
.contains(
|
|
|
|
|
"cannot set a minimum Python version 3.8 higher than the interpreter version 3.7"
|
|
|
|
|
));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
@ -1846,11 +1991,7 @@ mod tests {
|
|
|
|
|
let cross = CrossCompileConfig {
|
|
|
|
|
lib_dir: lib_dir.into(),
|
|
|
|
|
version: Some(interpreter_config.version),
|
|
|
|
|
target_info: TargetInfo {
|
|
|
|
|
arch: "x86_64".into(),
|
|
|
|
|
vendor: "unknown".into(),
|
|
|
|
|
os: "linux".into(),
|
|
|
|
|
},
|
|
|
|
|
target: triple!("x86_64-unknown-linux-gnu"),
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
let sysconfigdata_path = match find_sysconfigdata(&cross) {
|
|
|
|
@ -1906,6 +2047,7 @@ mod tests {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
#[allow(deprecated)]
|
|
|
|
|
fn test_not_cross_compiling() {
|
|
|
|
|
assert!(
|
|
|
|
|
cross_compiling("aarch64-apple-darwin", "x86_64", "apple", "darwin")
|
|
|
|
@ -1924,6 +2066,51 @@ mod tests {
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_not_cross_compiling_from_to() {
|
|
|
|
|
assert!(cross_compiling_from_to(
|
|
|
|
|
&triple!("x86_64-unknown-linux-gnu"),
|
|
|
|
|
&triple!("x86_64-unknown-linux-gnu"),
|
|
|
|
|
)
|
|
|
|
|
.unwrap()
|
|
|
|
|
.is_none());
|
|
|
|
|
|
|
|
|
|
assert!(cross_compiling_from_to(
|
|
|
|
|
&triple!("x86_64-apple-darwin"),
|
|
|
|
|
&triple!("x86_64-apple-darwin")
|
|
|
|
|
)
|
|
|
|
|
.unwrap()
|
|
|
|
|
.is_none());
|
|
|
|
|
|
|
|
|
|
assert!(cross_compiling_from_to(
|
|
|
|
|
&triple!("aarch64-apple-darwin"),
|
|
|
|
|
&triple!("x86_64-apple-darwin")
|
|
|
|
|
)
|
|
|
|
|
.unwrap()
|
|
|
|
|
.is_none());
|
|
|
|
|
|
|
|
|
|
assert!(cross_compiling_from_to(
|
|
|
|
|
&triple!("x86_64-apple-darwin"),
|
|
|
|
|
&triple!("aarch64-apple-darwin")
|
|
|
|
|
)
|
|
|
|
|
.unwrap()
|
|
|
|
|
.is_none());
|
|
|
|
|
|
|
|
|
|
assert!(cross_compiling_from_to(
|
|
|
|
|
&triple!("x86_64-pc-windows-msvc"),
|
|
|
|
|
&triple!("i686-pc-windows-msvc")
|
|
|
|
|
)
|
|
|
|
|
.unwrap()
|
|
|
|
|
.is_none());
|
|
|
|
|
|
|
|
|
|
assert!(cross_compiling_from_to(
|
|
|
|
|
&triple!("x86_64-unknown-linux-gnu"),
|
|
|
|
|
&triple!("x86_64-unknown-linux-musl")
|
|
|
|
|
)
|
|
|
|
|
.unwrap()
|
|
|
|
|
.is_none());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_run_python_script() {
|
|
|
|
|
// as above, this should be okay in CI where Python is presumed installed
|
|
|
|
|