build: refactor for simplicity
This commit is contained in:
parent
28ccef2c72
commit
fe9ed5c1fd
430
build.rs
430
build.rs
|
@ -3,18 +3,17 @@ use std::{
|
||||||
convert::AsRef,
|
convert::AsRef,
|
||||||
env,
|
env,
|
||||||
ffi::OsString,
|
ffi::OsString,
|
||||||
|
fmt::Display,
|
||||||
fs::{self, DirEntry},
|
fs::{self, DirEntry},
|
||||||
io,
|
|
||||||
path::{Path, PathBuf},
|
path::{Path, PathBuf},
|
||||||
process::{Command, Stdio},
|
process::{Command, Stdio},
|
||||||
str::FromStr,
|
str::FromStr,
|
||||||
};
|
};
|
||||||
|
|
||||||
/// Minimum required Python version.
|
/// Minimum Python version PyO3 supports.
|
||||||
const PY3_MIN_MINOR: u8 = 6;
|
const MINIMUM_SUPPORTED_VERSION: PythonVersion = PythonVersion { major: 3, minor: 6 };
|
||||||
/// Maximum Python version that can be used as minimum required Python version with abi3.
|
/// Maximum Python version that can be used as minimum required Python version with abi3.
|
||||||
const ABI3_MAX_MINOR: u8 = 9;
|
const ABI3_MAX_MINOR: u8 = 9;
|
||||||
const CFG_KEY: &str = "py_sys_config";
|
|
||||||
|
|
||||||
type Result<T> = std::result::Result<T, Box<dyn std::error::Error>>;
|
type Result<T> = std::result::Result<T, Box<dyn std::error::Error>>;
|
||||||
|
|
||||||
|
@ -50,27 +49,31 @@ fn env_var(var: &str) -> Option<OsString> {
|
||||||
env::var_os(var)
|
env::var_os(var)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Information returned from python interpreter
|
/// Configuration needed by PyO3 to build for the correct Python implementation.
|
||||||
|
///
|
||||||
|
/// Usually this is queried directly from the Python interpreter. When the `PYO3_NO_PYTHON` variable
|
||||||
|
/// is set, or during cross compile situations, then alternative strategies are used to populate
|
||||||
|
/// this type.
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
struct InterpreterConfig {
|
struct InterpreterConfig {
|
||||||
version: PythonVersion,
|
version: PythonVersion,
|
||||||
libdir: Option<String>,
|
libdir: Option<String>,
|
||||||
shared: bool,
|
shared: bool,
|
||||||
ld_version: String,
|
ld_version: Option<String>,
|
||||||
/// Prefix used for determining the directory of libpython
|
base_prefix: Option<String>,
|
||||||
base_prefix: String,
|
executable: Option<String>,
|
||||||
executable: PathBuf,
|
|
||||||
calcsize_pointer: Option<u32>,
|
calcsize_pointer: Option<u32>,
|
||||||
implementation: PythonInterpreterKind,
|
implementation: PythonImplementation,
|
||||||
|
build_flags: BuildFlags,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl InterpreterConfig {
|
impl InterpreterConfig {
|
||||||
fn is_pypy(&self) -> bool {
|
fn is_pypy(&self) -> bool {
|
||||||
self.implementation == PythonInterpreterKind::PyPy
|
self.implementation == PythonImplementation::PyPy
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone, PartialEq, PartialOrd)]
|
#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord)]
|
||||||
struct PythonVersion {
|
struct PythonVersion {
|
||||||
major: u8,
|
major: u8,
|
||||||
minor: u8,
|
minor: u8,
|
||||||
|
@ -80,18 +83,24 @@ impl PythonVersion {
|
||||||
const PY37: Self = PythonVersion { major: 3, minor: 7 };
|
const PY37: Self = PythonVersion { major: 3, minor: 7 };
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone, PartialEq)]
|
impl Display for PythonVersion {
|
||||||
pub enum PythonInterpreterKind {
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||||
|
write!(f, "{}.{}", self.major, self.minor)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Copy, Clone, PartialEq)]
|
||||||
|
pub enum PythonImplementation {
|
||||||
CPython,
|
CPython,
|
||||||
PyPy,
|
PyPy,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl FromStr for PythonInterpreterKind {
|
impl FromStr for PythonImplementation {
|
||||||
type Err = Box<dyn std::error::Error>;
|
type Err = Box<dyn std::error::Error>;
|
||||||
fn from_str(s: &str) -> Result<Self> {
|
fn from_str(s: &str) -> Result<Self> {
|
||||||
match s {
|
match s {
|
||||||
"CPython" => Ok(PythonInterpreterKind::CPython),
|
"CPython" => Ok(PythonImplementation::CPython),
|
||||||
"PyPy" => Ok(PythonInterpreterKind::PyPy),
|
"PyPy" => Ok(PythonImplementation::PyPy),
|
||||||
_ => bail!("Invalid interpreter: {}", s),
|
_ => bail!("Invalid interpreter: {}", s),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -198,21 +207,20 @@ fn cross_compiling() -> Result<Option<CrossCompileConfig>> {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// A list of python interpreter compile-time preprocessor defines that
|
/// A list of python interpreter compile-time preprocessor defines that
|
||||||
/// we will pick up and pass to rustc via --cfg=py_sys_config={varname};
|
/// we will pick up and pass to rustc via `--cfg=py_sys_config={varname}`;
|
||||||
/// this allows using them conditional cfg attributes in the .rs files, so
|
/// this allows using them conditional cfg attributes in the .rs files, so
|
||||||
///
|
///
|
||||||
/// #[cfg(py_sys_config="{varname}"]
|
/// #[cfg(py_sys_config="{varname}"]
|
||||||
///
|
///
|
||||||
/// is the equivalent of #ifdef {varname} name in C.
|
/// is the equivalent of `#ifdef {varname}` in C.
|
||||||
///
|
///
|
||||||
/// see Misc/SpecialBuilds.txt in the python source for what these mean.
|
/// see Misc/SpecialBuilds.txt in the python source for what these mean.
|
||||||
///
|
#[derive(Debug)]
|
||||||
/// (hrm, this is sort of re-implementing what distutils does, except
|
|
||||||
/// by passing command line args instead of referring to a python.h)
|
|
||||||
struct BuildFlags(HashSet<&'static str>);
|
struct BuildFlags(HashSet<&'static str>);
|
||||||
|
|
||||||
impl BuildFlags {
|
impl BuildFlags {
|
||||||
const ALL: [&'static str; 5] = [
|
const ALL: [&'static str; 5] = [
|
||||||
|
// TODO: Remove WITH_THREAD once Python 3.6 support dropped (as it's always on).
|
||||||
"WITH_THREAD",
|
"WITH_THREAD",
|
||||||
"Py_DEBUG",
|
"Py_DEBUG",
|
||||||
"Py_REF_DEBUG",
|
"Py_REF_DEBUG",
|
||||||
|
@ -233,27 +241,25 @@ impl BuildFlags {
|
||||||
/// Examine python's compile flags to pass to cfg by launching
|
/// Examine python's compile flags to pass to cfg by launching
|
||||||
/// the interpreter and printing variables of interest from
|
/// the interpreter and printing variables of interest from
|
||||||
/// sysconfig.get_config_vars.
|
/// sysconfig.get_config_vars.
|
||||||
fn from_interpreter(python_path: &Path) -> Result<Self> {
|
fn from_interpreter(interpreter: &Path) -> Result<Self> {
|
||||||
if cargo_env_var("CARGO_CFG_TARGET_OS").unwrap() == "windows" {
|
if cargo_env_var("CARGO_CFG_TARGET_OS").unwrap() == "windows" {
|
||||||
return Ok(Self::windows_hardcoded());
|
return Ok(Self::windows_hardcoded());
|
||||||
}
|
}
|
||||||
|
|
||||||
let mut script = "import sysconfig; \
|
let mut script = String::from("import sysconfig\n");
|
||||||
config = sysconfig.get_config_vars();"
|
script.push_str("config = sysconfig.get_config_vars()\n");
|
||||||
.to_owned();
|
|
||||||
|
|
||||||
for k in BuildFlags::ALL.iter() {
|
for k in BuildFlags::ALL.iter() {
|
||||||
script.push_str(&format!("print(config.get('{}', '0'));", k));
|
script.push_str(&format!("print(config.get('{}', '0'))\n", k));
|
||||||
}
|
}
|
||||||
|
|
||||||
let stdout = run_python_script(python_path, &script)?;
|
let stdout = run_python_script(&interpreter, &script)?;
|
||||||
let split_stdout: Vec<&str> = stdout.trim_end().lines().collect();
|
let split_stdout: Vec<&str> = stdout.trim_end().lines().collect();
|
||||||
if split_stdout.len() != BuildFlags::ALL.len() {
|
ensure!(
|
||||||
bail!(
|
split_stdout.len() == BuildFlags::ALL.len(),
|
||||||
"Python stdout len didn't return expected number of lines: {}",
|
"Python stdout len didn't return expected number of lines: {}",
|
||||||
split_stdout.len()
|
split_stdout.len()
|
||||||
);
|
);
|
||||||
}
|
|
||||||
let flags = BuildFlags::ALL
|
let flags = BuildFlags::ALL
|
||||||
.iter()
|
.iter()
|
||||||
.zip(split_stdout)
|
.zip(split_stdout)
|
||||||
|
@ -267,41 +273,40 @@ impl BuildFlags {
|
||||||
fn windows_hardcoded() -> Self {
|
fn windows_hardcoded() -> Self {
|
||||||
// sysconfig is missing all the flags on windows, so we can't actually
|
// sysconfig is missing all the flags on windows, so we can't actually
|
||||||
// query the interpreter directly for its build flags.
|
// query the interpreter directly for its build flags.
|
||||||
//
|
|
||||||
// For the time being, this is the flags as defined in the python source's
|
|
||||||
// PC\pyconfig.h. This won't work correctly if someone has built their
|
|
||||||
// python with a modified pyconfig.h - sorry if that is you, you will have
|
|
||||||
// to comment/uncomment the lines below.
|
|
||||||
let mut flags = HashSet::new();
|
let mut flags = HashSet::new();
|
||||||
flags.insert("WITH_THREAD");
|
flags.insert("WITH_THREAD");
|
||||||
|
|
||||||
// This is defined #ifdef _DEBUG. The visual studio build seems to produce
|
|
||||||
// a specially named pythonXX_d.exe and pythonXX_d.dll when you build the
|
|
||||||
// Debug configuration, which this script doesn't currently support anyway.
|
|
||||||
// map.insert("Py_DEBUG", "1");
|
|
||||||
|
|
||||||
// Uncomment these manually if your python was built with these and you want
|
// Uncomment these manually if your python was built with these and you want
|
||||||
// the cfg flags to be set in rust.
|
// the cfg flags to be set in rust.
|
||||||
//
|
//
|
||||||
|
// map.insert("Py_DEBUG", "1");
|
||||||
// map.insert("Py_REF_DEBUG", "1");
|
// map.insert("Py_REF_DEBUG", "1");
|
||||||
// map.insert("Py_TRACE_REFS", "1");
|
// map.insert("Py_TRACE_REFS", "1");
|
||||||
// map.insert("COUNT_ALLOCS", 1");
|
// map.insert("COUNT_ALLOCS", 1");
|
||||||
Self(flags)
|
Self(flags)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn fixup(&mut self, interpreter_config: &InterpreterConfig) {
|
fn abi3() -> Self {
|
||||||
|
let mut flags = HashSet::new();
|
||||||
|
flags.insert("WITH_THREAD");
|
||||||
|
Self(flags)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn fixup(mut self, version: PythonVersion, implementation: PythonImplementation) -> Self {
|
||||||
if self.0.contains("Py_DEBUG") {
|
if self.0.contains("Py_DEBUG") {
|
||||||
self.0.insert("Py_REF_DEBUG");
|
self.0.insert("Py_REF_DEBUG");
|
||||||
if interpreter_config.version <= PythonVersion::PY37 {
|
if version <= PythonVersion::PY37 {
|
||||||
// Py_DEBUG only implies Py_TRACE_REFS until Python 3.7
|
// Py_DEBUG only implies Py_TRACE_REFS until Python 3.7
|
||||||
self.0.insert("Py_TRACE_REFS");
|
self.0.insert("Py_TRACE_REFS");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// WITH_THREAD is always on for Python 3.7, and for PyPy.
|
// WITH_THREAD is always on for Python 3.7, and for PyPy.
|
||||||
if interpreter_config.is_pypy() || interpreter_config.version >= PythonVersion::PY37 {
|
if implementation == PythonImplementation::PyPy || version >= PythonVersion::PY37 {
|
||||||
self.0.insert("WITH_THREAD");
|
self.0.insert("WITH_THREAD");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
self
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -462,7 +467,7 @@ fn search_lib_dir(path: impl AsRef<Path>, cross: &CrossCompileConfig) -> Vec<Pat
|
||||||
/// [1]: https://github.com/python/cpython/blob/3.8/Lib/sysconfig.py#L348
|
/// [1]: https://github.com/python/cpython/blob/3.8/Lib/sysconfig.py#L348
|
||||||
fn load_cross_compile_from_sysconfigdata(
|
fn load_cross_compile_from_sysconfigdata(
|
||||||
cross_compile_config: CrossCompileConfig,
|
cross_compile_config: CrossCompileConfig,
|
||||||
) -> Result<(InterpreterConfig, BuildFlags)> {
|
) -> Result<InterpreterConfig> {
|
||||||
let sysconfig_path = find_sysconfigdata(&cross_compile_config)?;
|
let sysconfig_path = find_sysconfigdata(&cross_compile_config)?;
|
||||||
let sysconfig_data = parse_sysconfigdata(sysconfig_path)?;
|
let sysconfig_data = parse_sysconfigdata(sysconfig_path)?;
|
||||||
|
|
||||||
|
@ -474,27 +479,25 @@ fn load_cross_compile_from_sysconfigdata(
|
||||||
};
|
};
|
||||||
let calcsize_pointer = sysconfig_data.get_numeric("SIZEOF_VOID_P").ok();
|
let calcsize_pointer = sysconfig_data.get_numeric("SIZEOF_VOID_P").ok();
|
||||||
|
|
||||||
let python_version = PythonVersion { major, minor };
|
let version = PythonVersion { major, minor };
|
||||||
|
let implementation = PythonImplementation::CPython;
|
||||||
|
|
||||||
let interpreter_config = InterpreterConfig {
|
Ok(InterpreterConfig {
|
||||||
version: python_version,
|
version,
|
||||||
libdir: cross_compile_config.lib_dir.to_str().map(String::from),
|
libdir: cross_compile_config.lib_dir.to_str().map(String::from),
|
||||||
shared: sysconfig_data.get_bool("Py_ENABLE_SHARED")?,
|
shared: sysconfig_data.get_bool("Py_ENABLE_SHARED")?,
|
||||||
ld_version,
|
ld_version: Some(ld_version),
|
||||||
base_prefix: "".to_string(),
|
base_prefix: None,
|
||||||
executable: PathBuf::new(),
|
executable: None,
|
||||||
calcsize_pointer,
|
calcsize_pointer,
|
||||||
implementation: PythonInterpreterKind::CPython,
|
implementation,
|
||||||
};
|
build_flags: BuildFlags::from_config_map(&sysconfig_data).fixup(version, implementation),
|
||||||
|
})
|
||||||
let build_flags = BuildFlags::from_config_map(&sysconfig_data);
|
|
||||||
|
|
||||||
Ok((interpreter_config, build_flags))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn windows_hardcoded_cross_compile(
|
fn windows_hardcoded_cross_compile(
|
||||||
cross_compile_config: CrossCompileConfig,
|
cross_compile_config: CrossCompileConfig,
|
||||||
) -> Result<(InterpreterConfig, BuildFlags)> {
|
) -> Result<InterpreterConfig> {
|
||||||
let (major, minor) = if let Some(version) = cross_compile_config.version {
|
let (major, minor) = if let Some(version) = cross_compile_config.version {
|
||||||
let mut parts = version.split('.');
|
let mut parts = version.split('.');
|
||||||
match (
|
match (
|
||||||
|
@ -514,25 +517,20 @@ fn windows_hardcoded_cross_compile(
|
||||||
bail!("PYO3_CROSS_PYTHON_VERSION or an abi3-py3* feature must be specified when cross-compiling for Windows.")
|
bail!("PYO3_CROSS_PYTHON_VERSION or an abi3-py3* feature must be specified when cross-compiling for Windows.")
|
||||||
};
|
};
|
||||||
|
|
||||||
let python_version = PythonVersion { major, minor };
|
Ok(InterpreterConfig {
|
||||||
|
version: PythonVersion { major, minor },
|
||||||
let interpreter_config = InterpreterConfig {
|
|
||||||
version: python_version,
|
|
||||||
libdir: cross_compile_config.lib_dir.to_str().map(String::from),
|
libdir: cross_compile_config.lib_dir.to_str().map(String::from),
|
||||||
shared: true,
|
shared: true,
|
||||||
ld_version: format!("{}.{}", major, minor),
|
ld_version: None,
|
||||||
base_prefix: "".to_string(),
|
base_prefix: None,
|
||||||
executable: PathBuf::new(),
|
executable: None,
|
||||||
calcsize_pointer: None,
|
calcsize_pointer: None,
|
||||||
implementation: PythonInterpreterKind::CPython,
|
implementation: PythonImplementation::CPython,
|
||||||
};
|
build_flags: BuildFlags::windows_hardcoded(),
|
||||||
|
})
|
||||||
Ok((interpreter_config, BuildFlags::windows_hardcoded()))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn load_cross_compile_info(
|
fn load_cross_compile_info(cross_compile_config: CrossCompileConfig) -> Result<InterpreterConfig> {
|
||||||
cross_compile_config: CrossCompileConfig,
|
|
||||||
) -> Result<(InterpreterConfig, BuildFlags)> {
|
|
||||||
match cargo_env_var("CARGO_CFG_TARGET_FAMILY") {
|
match cargo_env_var("CARGO_CFG_TARGET_FAMILY") {
|
||||||
// Configure for unix platforms using the sysconfigdata file
|
// Configure for unix platforms using the sysconfigdata file
|
||||||
Some(os) if os == "unix" => load_cross_compile_from_sysconfigdata(cross_compile_config),
|
Some(os) if os == "unix" => load_cross_compile_from_sysconfigdata(cross_compile_config),
|
||||||
|
@ -567,27 +565,17 @@ fn run_python_script(interpreter: &Path, script: &str) -> Result<String> {
|
||||||
});
|
});
|
||||||
|
|
||||||
match out {
|
match out {
|
||||||
Err(err) => {
|
Err(err) => bail!(
|
||||||
if err.kind() == io::ErrorKind::NotFound {
|
"failed to run the Python interpreter at {}: {}",
|
||||||
bail!(
|
interpreter.display(),
|
||||||
"Could not find any interpreter at {}, \
|
err
|
||||||
are you sure you have Python installed on your PATH?",
|
),
|
||||||
interpreter.display()
|
|
||||||
);
|
|
||||||
} else {
|
|
||||||
bail!(
|
|
||||||
"Failed to run the Python interpreter at {}: {}",
|
|
||||||
interpreter.display(),
|
|
||||||
err
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Ok(ok) if !ok.status.success() => bail!("Python script failed"),
|
Ok(ok) if !ok.status.success() => bail!("Python script failed"),
|
||||||
Ok(ok) => Ok(String::from_utf8(ok.stdout)?),
|
Ok(ok) => Ok(String::from_utf8(ok.stdout)?),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn get_rustc_link_lib(config: &InterpreterConfig) -> String {
|
fn get_rustc_link_lib(config: &InterpreterConfig) -> Result<String> {
|
||||||
let link_name = if cargo_env_var("CARGO_CFG_TARGET_OS").unwrap() == "windows" {
|
let link_name = if cargo_env_var("CARGO_CFG_TARGET_OS").unwrap() == "windows" {
|
||||||
if is_abi3() {
|
if is_abi3() {
|
||||||
// Link against python3.lib for the stable ABI on Windows.
|
// Link against python3.lib for the stable ABI on Windows.
|
||||||
|
@ -609,16 +597,19 @@ fn get_rustc_link_lib(config: &InterpreterConfig) -> String {
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
match config.implementation {
|
match config.implementation {
|
||||||
PythonInterpreterKind::CPython => format!("python{}", config.ld_version),
|
PythonImplementation::CPython => match &config.ld_version {
|
||||||
PythonInterpreterKind::PyPy => format!("pypy{}-c", config.version.major),
|
Some(ld_version) => format!("python{}", ld_version),
|
||||||
|
None => bail!("failed to configure `ld_version` when compiling for unix"),
|
||||||
|
},
|
||||||
|
PythonImplementation::PyPy => format!("pypy{}-c", config.version.major),
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
format!(
|
Ok(format!(
|
||||||
"cargo:rustc-link-lib={link_model}{link_name}",
|
"cargo:rustc-link-lib={link_model}{link_name}",
|
||||||
link_model = if config.shared { "" } else { "static=" },
|
link_model = if config.shared { "" } else { "static=" },
|
||||||
link_name = link_name
|
link_name = link_name
|
||||||
)
|
))
|
||||||
}
|
}
|
||||||
|
|
||||||
fn get_venv_path() -> Option<PathBuf> {
|
fn get_venv_path() -> Option<PathBuf> {
|
||||||
|
@ -636,6 +627,11 @@ fn get_venv_path() -> Option<PathBuf> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Attempts to locate a python interpreter. Locations are checked in the order listed:
|
||||||
|
/// 1. If `PYO3_PYTHON` is set, this intepreter is used.
|
||||||
|
/// 2. If in a virtualenv, that environment's interpreter is used.
|
||||||
|
/// 3. `python`, if this is functional a Python 3.x interpreter
|
||||||
|
/// 4. `python3`, as above
|
||||||
fn find_interpreter() -> Result<PathBuf> {
|
fn find_interpreter() -> Result<PathBuf> {
|
||||||
if let Some(exe) = env_var("PYO3_PYTHON") {
|
if let Some(exe) = env_var("PYO3_PYTHON") {
|
||||||
Ok(exe.into())
|
Ok(exe.into())
|
||||||
|
@ -657,37 +653,16 @@ fn find_interpreter() -> Result<PathBuf> {
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
.map(PathBuf::from)
|
.map(PathBuf::from)
|
||||||
.ok_or_else(|| "Python 3.x interpreter not found".into())
|
.ok_or_else(|| "no Python 3.x interpreter found".into())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Locate a suitable python interpreter and extract config from it.
|
|
||||||
///
|
|
||||||
/// The following locations are checked in the order listed:
|
|
||||||
///
|
|
||||||
/// 1. If `PYO3_PYTHON` is set, this intepreter is used and an error is raised if the
|
|
||||||
/// version doesn't match.
|
|
||||||
/// 2. `python`
|
|
||||||
/// 3. `python{major version}`
|
|
||||||
/// 4. `python{major version}.{minor version}`
|
|
||||||
///
|
|
||||||
/// If none of the above works, an error is returned
|
|
||||||
fn find_interpreter_and_get_config() -> Result<(InterpreterConfig, BuildFlags)> {
|
|
||||||
let python_interpreter = find_interpreter()?;
|
|
||||||
let interpreter_config = get_config_from_interpreter(&python_interpreter)?;
|
|
||||||
if interpreter_config.version.major == 3 {
|
|
||||||
return Ok((
|
|
||||||
interpreter_config,
|
|
||||||
BuildFlags::from_interpreter(&python_interpreter)?,
|
|
||||||
));
|
|
||||||
}
|
|
||||||
|
|
||||||
Err("No Python interpreter found".into())
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Extract compilation vars from the specified interpreter.
|
/// Extract compilation vars from the specified interpreter.
|
||||||
fn get_config_from_interpreter(interpreter: &Path) -> Result<InterpreterConfig> {
|
fn get_config_from_interpreter(interpreter: &Path) -> Result<InterpreterConfig> {
|
||||||
let script = r#"
|
let script = r#"
|
||||||
|
# Allow the script to run on Python 2, so that nicer error can be printed later.
|
||||||
|
from __future__ import print_function
|
||||||
|
|
||||||
import os.path
|
import os.path
|
||||||
import platform
|
import platform
|
||||||
import struct
|
import struct
|
||||||
|
@ -696,20 +671,30 @@ from sysconfig import get_config_var
|
||||||
|
|
||||||
PYPY = platform.python_implementation() == "PyPy"
|
PYPY = platform.python_implementation() == "PyPy"
|
||||||
|
|
||||||
# Anaconda based python distributions have a static python executable, but include
|
# sys.base_prefix is missing on Python versions older than 3.3; this allows the script to continue
|
||||||
# the shared library. Use the shared library for embedding to avoid rust trying to
|
# so that the version mismatch can be reported in a nicer way later.
|
||||||
# LTO the static library (and failing with newer gcc's, because it is old).
|
base_prefix = getattr(sys, "base_prefix", None)
|
||||||
ANACONDA = os.path.exists(os.path.join(sys.base_prefix, "conda-meta"))
|
|
||||||
|
if base_prefix:
|
||||||
|
# Anaconda based python distributions have a static python executable, but include
|
||||||
|
# the shared library. Use the shared library for embedding to avoid rust trying to
|
||||||
|
# LTO the static library (and failing with newer gcc's, because it is old).
|
||||||
|
ANACONDA = os.path.exists(os.path.join(base_prefix, "conda-meta"))
|
||||||
|
else:
|
||||||
|
ANACONDA = False
|
||||||
|
|
||||||
|
def print_if_set(varname, value):
|
||||||
|
if value is not None:
|
||||||
|
print(varname, value)
|
||||||
|
|
||||||
libdir = get_config_var("LIBDIR")
|
libdir = get_config_var("LIBDIR")
|
||||||
|
|
||||||
print("version_major", sys.version_info[0])
|
print("version_major", sys.version_info[0])
|
||||||
print("version_minor", sys.version_info[1])
|
print("version_minor", sys.version_info[1])
|
||||||
print("implementation", platform.python_implementation())
|
print("implementation", platform.python_implementation())
|
||||||
if libdir is not None:
|
print_if_set("libdir", libdir)
|
||||||
print("libdir", libdir)
|
print_if_set("ld_version", get_config_var("LDVERSION"))
|
||||||
print("ld_version", get_config_var("LDVERSION") or get_config_var("py_version_short"))
|
print_if_set("base_prefix", base_prefix)
|
||||||
print("base_prefix", sys.base_prefix)
|
|
||||||
print("framework", bool(get_config_var("PYTHONFRAMEWORK")))
|
print("framework", bool(get_config_var("PYTHONFRAMEWORK")))
|
||||||
print("shared", PYPY or ANACONDA or bool(get_config_var("Py_ENABLE_SHARED")))
|
print("shared", PYPY or ANACONDA or bool(get_config_var("Py_ENABLE_SHARED")))
|
||||||
print("executable", sys.executable)
|
print("executable", sys.executable)
|
||||||
|
@ -730,47 +715,52 @@ print("calcsize_pointer", struct.calcsize("P"))
|
||||||
_ => bail!("Unrecognised link model combination")
|
_ => bail!("Unrecognised link model combination")
|
||||||
};
|
};
|
||||||
|
|
||||||
|
let version = PythonVersion {
|
||||||
|
major: map["version_major"].parse()?,
|
||||||
|
minor: map["version_minor"].parse()?,
|
||||||
|
};
|
||||||
|
|
||||||
|
let implementation = map["implementation"].parse()?;
|
||||||
|
|
||||||
Ok(InterpreterConfig {
|
Ok(InterpreterConfig {
|
||||||
version: PythonVersion {
|
version,
|
||||||
major: map["version_major"].parse()?,
|
implementation,
|
||||||
minor: map["version_minor"].parse()?,
|
|
||||||
},
|
|
||||||
implementation: map["implementation"].parse()?,
|
|
||||||
libdir: map.get("libdir").cloned(),
|
libdir: map.get("libdir").cloned(),
|
||||||
shared,
|
shared,
|
||||||
ld_version: map["ld_version"].clone(),
|
ld_version: map.get("ld_version").cloned(),
|
||||||
base_prefix: map["base_prefix"].clone(),
|
base_prefix: map.get("base_prefix").cloned(),
|
||||||
executable: map["executable"].clone().into(),
|
executable: map.get("executable").cloned(),
|
||||||
calcsize_pointer: Some(map["calcsize_pointer"].parse()?),
|
calcsize_pointer: Some(map["calcsize_pointer"].parse()?),
|
||||||
|
build_flags: BuildFlags::from_interpreter(interpreter)?.fixup(version, implementation),
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
fn configure(interpreter_config: &InterpreterConfig) -> Result<()> {
|
fn ensure_python_version(interpreter_config: &InterpreterConfig) -> Result<()> {
|
||||||
if interpreter_config.version.major == 2 {
|
ensure!(
|
||||||
// fail PYO3_PYTHON=python2 cargo ...
|
interpreter_config.version >= MINIMUM_SUPPORTED_VERSION,
|
||||||
bail!("Python 2 is not supported");
|
"the configured Python interpreter version ({}) is lower than PyO3's minimum supported version ({})",
|
||||||
}
|
interpreter_config.version,
|
||||||
|
MINIMUM_SUPPORTED_VERSION,
|
||||||
|
);
|
||||||
|
|
||||||
if interpreter_config.version.minor < PY3_MIN_MINOR {
|
Ok(())
|
||||||
bail!(
|
}
|
||||||
"Python 3 required version is 3.{}, current version is 3.{}",
|
|
||||||
PY3_MIN_MINOR,
|
|
||||||
interpreter_config.version.minor
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
check_target_architecture(interpreter_config)?;
|
|
||||||
|
|
||||||
|
fn emit_cargo_configuration(interpreter_config: &InterpreterConfig) -> Result<()> {
|
||||||
let target_os = cargo_env_var("CARGO_CFG_TARGET_OS").unwrap();
|
let target_os = cargo_env_var("CARGO_CFG_TARGET_OS").unwrap();
|
||||||
let is_extension_module = cargo_env_var("CARGO_FEATURE_EXTENSION_MODULE").is_some();
|
let is_extension_module = cargo_env_var("CARGO_FEATURE_EXTENSION_MODULE").is_some();
|
||||||
match (is_extension_module, target_os.as_str()) {
|
match (is_extension_module, target_os.as_str()) {
|
||||||
(_, "windows") => {
|
(_, "windows") => {
|
||||||
// always link on windows, even with extension module
|
// always link on windows, even with extension module
|
||||||
println!("{}", get_rustc_link_lib(&interpreter_config));
|
println!("{}", get_rustc_link_lib(&interpreter_config)?);
|
||||||
println!(
|
// Set during cross-compiling.
|
||||||
"cargo:rustc-link-search=native={}\\libs",
|
if let Some(libdir) = &interpreter_config.libdir {
|
||||||
interpreter_config.base_prefix
|
println!("cargo:rustc-link-search=native={}", libdir);
|
||||||
);
|
}
|
||||||
|
// Set if we have an interpreter to use.
|
||||||
|
if let Some(base_prefix) = &interpreter_config.base_prefix {
|
||||||
|
println!("cargo:rustc-link-search=native={}\\libs", base_prefix);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
(true, "macos") => {
|
(true, "macos") => {
|
||||||
// with extension module on macos some extra linker arguments are needed
|
// with extension module on macos some extra linker arguments are needed
|
||||||
|
@ -780,17 +770,19 @@ fn configure(interpreter_config: &InterpreterConfig) -> Result<()> {
|
||||||
(false, _) | (_, "android") => {
|
(false, _) | (_, "android") => {
|
||||||
// other systems, only link libs if not extension module
|
// other systems, only link libs if not extension module
|
||||||
// android always link.
|
// android always link.
|
||||||
println!("{}", get_rustc_link_lib(&interpreter_config));
|
println!("{}", get_rustc_link_lib(&interpreter_config)?);
|
||||||
if let Some(libdir) = &interpreter_config.libdir {
|
if let Some(libdir) = &interpreter_config.libdir {
|
||||||
println!("cargo:rustc-link-search=native={}", libdir);
|
println!("cargo:rustc-link-search=native={}", libdir);
|
||||||
}
|
}
|
||||||
if interpreter_config.implementation == PythonInterpreterKind::PyPy {
|
if interpreter_config.implementation == PythonImplementation::PyPy {
|
||||||
// PyPy 3.7 changed LIBDIR to point to base_prefix/lib, so need to hard-code /bin
|
// PyPy 7.3.4 changed LIBDIR to point to base_prefix/lib as a regression, so need
|
||||||
// search path too: https://foss.heptapod.net/pypy/pypy/-/issues/3442
|
// to hard-code /bin search path too: https://foss.heptapod.net/pypy/pypy/-/issues/3442
|
||||||
println!(
|
//
|
||||||
"cargo:rustc-link-search=native={}/bin",
|
// TODO: this workaround can probably be removed when PyPy 7.3.5 is released (and we
|
||||||
interpreter_config.base_prefix
|
// can call it a PyPy bug).
|
||||||
);
|
if let Some(base_prefix) = &interpreter_config.base_prefix {
|
||||||
|
println!("cargo:rustc-link-search=native={}/bin", base_prefix);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
_ => {}
|
_ => {}
|
||||||
|
@ -850,14 +842,18 @@ fn configure(interpreter_config: &InterpreterConfig) -> Result<()> {
|
||||||
interpreter_config.version.minor
|
interpreter_config.version.minor
|
||||||
};
|
};
|
||||||
|
|
||||||
for i in PY3_MIN_MINOR..=minor {
|
for i in MINIMUM_SUPPORTED_VERSION.minor..=minor {
|
||||||
println!("cargo:rustc-cfg=Py_3_{}", i);
|
println!("cargo:rustc-cfg=Py_3_{}", i);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
for flag in &interpreter_config.build_flags.0 {
|
||||||
|
println!("cargo:rustc-cfg=py_sys_config=\"{}\"", flag)
|
||||||
|
}
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn check_target_architecture(interpreter_config: &InterpreterConfig) -> Result<()> {
|
fn ensure_target_architecture(interpreter_config: &InterpreterConfig) -> Result<()> {
|
||||||
// Try to check whether the target architecture matches the python library
|
// Try to check whether the target architecture matches the python library
|
||||||
let rust_target = match cargo_env_var("CARGO_CFG_TARGET_POINTER_WIDTH")
|
let rust_target = match cargo_env_var("CARGO_CFG_TARGET_POINTER_WIDTH")
|
||||||
.unwrap()
|
.unwrap()
|
||||||
|
@ -884,86 +880,60 @@ fn check_target_architecture(interpreter_config: &InterpreterConfig) -> Result<(
|
||||||
Some(n) => bail!("unexpected Python calcsize_pointer value: {}", n),
|
Some(n) => bail!("unexpected Python calcsize_pointer value: {}", n),
|
||||||
};
|
};
|
||||||
|
|
||||||
if rust_target != python_target {
|
ensure!(
|
||||||
bail!(
|
rust_target == python_target,
|
||||||
"Your Rust target architecture ({}) does not match your python interpreter ({})",
|
"Your Rust target architecture ({}) does not match your python interpreter ({})",
|
||||||
rust_target,
|
rust_target,
|
||||||
python_target
|
python_target
|
||||||
);
|
);
|
||||||
}
|
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn get_abi3_minor_version() -> Option<u8> {
|
fn get_abi3_minor_version() -> Option<u8> {
|
||||||
(PY3_MIN_MINOR..=ABI3_MAX_MINOR)
|
(MINIMUM_SUPPORTED_VERSION.minor..=ABI3_MAX_MINOR)
|
||||||
.find(|i| cargo_env_var(&format!("CARGO_FEATURE_ABI3_PY3{}", i)).is_some())
|
.find(|i| cargo_env_var(&format!("CARGO_FEATURE_ABI3_PY3{}", i)).is_some())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn configure_abi3_without_interpreter() {
|
fn get_interpreter_config() -> Result<InterpreterConfig> {
|
||||||
println!("cargo:rustc-cfg=Py_LIMITED_API");
|
// If PYO3_NO_PYTHON is set with abi3, we can build PyO3 without calling Python.
|
||||||
let mut flags = "FLAG_WITH_THREAD=1".to_string();
|
if let Some(abi3_version) = get_abi3_minor_version() {
|
||||||
let abi_version = get_abi3_minor_version().unwrap_or(ABI3_MAX_MINOR);
|
if env_var("PYO3_NO_PYTHON").is_some() {
|
||||||
for minor in PY3_MIN_MINOR..=abi_version {
|
return Ok(InterpreterConfig {
|
||||||
println!("cargo:rustc-cfg=Py_3_{}", minor);
|
version: PythonVersion {
|
||||||
flags += &format!(",CFG_Py_3_{}", minor);
|
major: 3,
|
||||||
|
minor: abi3_version,
|
||||||
|
},
|
||||||
|
implementation: PythonImplementation::CPython,
|
||||||
|
libdir: None,
|
||||||
|
build_flags: BuildFlags::abi3(),
|
||||||
|
base_prefix: None,
|
||||||
|
calcsize_pointer: None,
|
||||||
|
executable: None,
|
||||||
|
ld_version: None,
|
||||||
|
shared: true,
|
||||||
|
});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
println!("cargo:rustc-cfg=py_sys_config=\"WITH_THREAD\"");
|
|
||||||
println!("cargo:python_flags={}", flags);
|
|
||||||
|
|
||||||
if cargo_env_var("CARGO_CFG_TARGET_FAMILY").unwrap() == "windows" {
|
if let Some(paths) = cross_compiling()? {
|
||||||
// Unfortunately, on windows we can't build without at least providing
|
load_cross_compile_info(paths)
|
||||||
// python.lib to the linker. While maturin tells the linker the location
|
} else {
|
||||||
// of python.lib, we need to do the renaming here, otherwise cargo
|
get_config_from_interpreter(&find_interpreter()?)
|
||||||
// complains that the crate using pyo3 does not contains a `#[link(...)]`
|
|
||||||
// attribute with pythonXY.
|
|
||||||
println!("cargo:rustc-link-lib=pythonXY:python3");
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn main_impl() -> Result<()> {
|
fn configure_pyo3() -> Result<()> {
|
||||||
// If PYO3_NO_PYTHON is set with abi3, we can build PyO3 without calling Python.
|
let interpreter_config = get_interpreter_config()?;
|
||||||
// We only check for the abi3-py3{ABI3_MAX_MINOR} because lower versions depend on it.
|
ensure_python_version(&interpreter_config)?;
|
||||||
if cargo_env_var("PYO3_NO_PYTHON").is_some()
|
ensure_target_architecture(&interpreter_config)?;
|
||||||
&& cargo_env_var(&format!("CARGO_FEATURE_ABI3_PY3{}", ABI3_MAX_MINOR)).is_some()
|
emit_cargo_configuration(&interpreter_config)
|
||||||
{
|
|
||||||
println!("cargo:rerun-if-env-changed=PYO3_NO_PYTHON");
|
|
||||||
configure_abi3_without_interpreter();
|
|
||||||
return Ok(());
|
|
||||||
}
|
|
||||||
// 1. Setup cfg variables so we can do conditional compilation in this library based on the
|
|
||||||
// python interpeter's compilation flags. This is necessary for e.g. matching the right unicode
|
|
||||||
// and threading interfaces. First check if we're cross compiling, if so, we cannot run the
|
|
||||||
// target Python interpreter and have to parse pyconfig.h instead. If we're not cross
|
|
||||||
// compiling, locate the python interpreter based on the PATH, which should work smoothly with
|
|
||||||
// an activated virtualenv, and load from there.
|
|
||||||
//
|
|
||||||
// If you have troubles with your shell accepting '.' in a var name,
|
|
||||||
// try using 'env' (sorry but this isn't our fault - it just has to
|
|
||||||
// match the pkg-config package name, which is going to have a . in it).
|
|
||||||
//
|
|
||||||
// Detecting if cross-compiling by checking if the target triple is different from the host
|
|
||||||
// rustc's triple.
|
|
||||||
let (interpreter_config, mut build_flags) = if let Some(paths) = cross_compiling()? {
|
|
||||||
load_cross_compile_info(paths)?
|
|
||||||
} else {
|
|
||||||
find_interpreter_and_get_config()?
|
|
||||||
};
|
|
||||||
|
|
||||||
build_flags.fixup(&interpreter_config);
|
|
||||||
configure(&interpreter_config)?;
|
|
||||||
|
|
||||||
for flag in &build_flags.0 {
|
|
||||||
println!("cargo:rustc-cfg={}=\"{}\"", CFG_KEY, flag)
|
|
||||||
}
|
|
||||||
|
|
||||||
Ok(())
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
// Print out error messages using display, to get nicer formatting.
|
// Print out error messages using display, to get nicer formatting.
|
||||||
let _ = main_impl().map_err(|e| {
|
if let Err(e) = configure_pyo3() {
|
||||||
eprintln!("Error: {}", e);
|
eprintln!("error: {}", e);
|
||||||
std::process::exit(1)
|
std::process::exit(1)
|
||||||
});
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue