2021-05-19 20:50:25 +00:00
|
|
|
use std::{env, process::Command};
|
2020-03-08 07:17:25 +00:00
|
|
|
|
2021-05-21 07:38:59 +00:00
|
|
|
use pyo3_build_config::{InterpreterConfig, PythonImplementation, PythonVersion};
|
2020-03-08 07:17:25 +00:00
|
|
|
|
|
|
|
type Result<T> = std::result::Result<T, Box<dyn std::error::Error>>;
|
|
|
|
|
2021-05-21 07:38:59 +00:00
|
|
|
/// Minimum Python version PyO3 supports.
|
|
|
|
const MINIMUM_SUPPORTED_VERSION: PythonVersion = PythonVersion { major: 3, minor: 6 };
|
|
|
|
|
|
|
|
// A simple macro for returning an error. Resembles anyhow::bail.
|
|
|
|
macro_rules! bail {
|
|
|
|
($msg: expr) => { return Err($msg.into()); };
|
|
|
|
($fmt: literal $($args: tt)+) => { return Err(format!($fmt $($args)+).into()); };
|
|
|
|
}
|
|
|
|
|
|
|
|
// A simple macro for checking a condition. Resembles anyhow::ensure.
|
|
|
|
macro_rules! ensure {
|
|
|
|
($condition:expr, $($args: tt)+) => { if !($condition) { bail!($($args)+) } };
|
|
|
|
}
|
|
|
|
|
|
|
|
fn ensure_python_version(interpreter_config: &InterpreterConfig) -> Result<()> {
|
|
|
|
ensure!(
|
|
|
|
interpreter_config.version >= MINIMUM_SUPPORTED_VERSION,
|
|
|
|
"the configured Python interpreter version ({}) is lower than PyO3's minimum supported version ({})",
|
|
|
|
interpreter_config.version,
|
|
|
|
MINIMUM_SUPPORTED_VERSION,
|
|
|
|
);
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn ensure_target_architecture(interpreter_config: &InterpreterConfig) -> Result<()> {
|
|
|
|
// Try to check whether the target architecture matches the python library
|
|
|
|
let rust_target = match env::var("CARGO_CFG_TARGET_POINTER_WIDTH").unwrap().as_str() {
|
|
|
|
"64" => "64-bit",
|
|
|
|
"32" => "32-bit",
|
|
|
|
x => bail!("unexpected Rust target pointer width: {}", x),
|
|
|
|
};
|
|
|
|
|
|
|
|
// The reason we don't use platform.architecture() here is that it's not
|
|
|
|
// reliable on macOS. See https://stackoverflow.com/a/1405971/823869.
|
|
|
|
// Similarly, sys.maxsize is not reliable on Windows. See
|
|
|
|
// https://stackoverflow.com/questions/1405913/how-do-i-determine-if-my-python-shell-is-executing-in-32bit-or-64bit-mode-on-os/1405971#comment6209952_1405971
|
|
|
|
// and https://stackoverflow.com/a/3411134/823869.
|
|
|
|
let python_target = match interpreter_config.calcsize_pointer {
|
|
|
|
Some(8) => "64-bit",
|
|
|
|
Some(4) => "32-bit",
|
|
|
|
None => {
|
|
|
|
// Unset, e.g. because we're cross-compiling. Don't check anything
|
|
|
|
// in this case.
|
|
|
|
return Ok(());
|
|
|
|
}
|
|
|
|
Some(n) => bail!("unexpected Python calcsize_pointer value: {}", n),
|
|
|
|
};
|
|
|
|
|
|
|
|
ensure!(
|
|
|
|
rust_target == python_target,
|
|
|
|
"Your Rust target architecture ({}) does not match your python interpreter ({})",
|
|
|
|
rust_target,
|
|
|
|
python_target
|
|
|
|
);
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2021-04-24 09:52:58 +00:00
|
|
|
fn get_rustc_link_lib(config: &InterpreterConfig) -> Result<String> {
|
2021-05-19 20:50:25 +00:00
|
|
|
let link_name = if env::var_os("CARGO_CFG_TARGET_OS").unwrap() == "windows" {
|
|
|
|
if config.abi3 {
|
2021-03-25 05:14:07 +00:00
|
|
|
// Link against python3.lib for the stable ABI on Windows.
|
|
|
|
// See https://www.python.org/dev/peps/pep-0384/#linkage
|
|
|
|
//
|
|
|
|
// This contains only the limited ABI symbols.
|
|
|
|
"pythonXY:python3".to_owned()
|
2021-05-19 20:50:25 +00:00
|
|
|
} else if env::var_os("CARGO_CFG_TARGET_ENV").unwrap() == "gnu" {
|
2021-02-12 09:16:30 +00:00
|
|
|
// https://packages.msys2.org/base/mingw-w64-python
|
2020-12-31 00:55:03 +00:00
|
|
|
format!(
|
2021-02-12 09:16:30 +00:00
|
|
|
"pythonXY:python{}.{}",
|
2020-12-31 00:55:03 +00:00
|
|
|
config.version.major, config.version.minor
|
|
|
|
)
|
2021-02-12 09:16:30 +00:00
|
|
|
} else {
|
2021-03-25 05:14:07 +00:00
|
|
|
format!(
|
|
|
|
"pythonXY:python{}{}",
|
|
|
|
config.version.major, config.version.minor
|
|
|
|
)
|
2020-09-23 12:31:24 +00:00
|
|
|
}
|
2019-04-23 11:18:42 +00:00
|
|
|
} else {
|
2021-03-05 06:27:36 +00:00
|
|
|
match config.implementation {
|
2021-04-24 09:52:58 +00:00
|
|
|
PythonImplementation::CPython => match &config.ld_version {
|
|
|
|
Some(ld_version) => format!("python{}", ld_version),
|
2021-05-19 20:50:25 +00:00
|
|
|
None => {
|
|
|
|
return Err("failed to configure `ld_version` when compiling for unix".into())
|
|
|
|
}
|
2021-04-24 09:52:58 +00:00
|
|
|
},
|
|
|
|
PythonImplementation::PyPy => format!("pypy{}-c", config.version.major),
|
2019-04-23 11:18:42 +00:00
|
|
|
}
|
2020-12-30 16:27:18 +00:00
|
|
|
};
|
2017-05-13 05:05:00 +00:00
|
|
|
|
2021-04-24 09:52:58 +00:00
|
|
|
Ok(format!(
|
2020-12-30 16:27:18 +00:00
|
|
|
"cargo:rustc-link-lib={link_model}{link_name}",
|
|
|
|
link_model = if config.shared { "" } else { "static=" },
|
|
|
|
link_name = link_name
|
2021-04-24 09:52:58 +00:00
|
|
|
))
|
2019-04-23 11:18:42 +00:00
|
|
|
}
|
|
|
|
|
2021-03-31 15:04:15 +00:00
|
|
|
fn rustc_minor_version() -> Option<u32> {
|
|
|
|
let rustc = env::var_os("RUSTC")?;
|
|
|
|
let output = Command::new(rustc).arg("--version").output().ok()?;
|
|
|
|
let version = core::str::from_utf8(&output.stdout).ok()?;
|
|
|
|
let mut pieces = version.split('.');
|
|
|
|
if pieces.next() != Some("rustc 1") {
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
pieces.next()?.parse().ok()
|
|
|
|
}
|
|
|
|
|
2021-04-24 09:52:58 +00:00
|
|
|
fn emit_cargo_configuration(interpreter_config: &InterpreterConfig) -> Result<()> {
|
2021-05-19 20:50:25 +00:00
|
|
|
let target_os = env::var("CARGO_CFG_TARGET_OS").unwrap();
|
|
|
|
let is_extension_module = env::var_os("CARGO_FEATURE_EXTENSION_MODULE").is_some();
|
2021-04-02 13:04:44 +00:00
|
|
|
match (is_extension_module, target_os.as_str()) {
|
|
|
|
(_, "windows") => {
|
|
|
|
// always link on windows, even with extension module
|
2021-04-24 09:52:58 +00:00
|
|
|
println!("{}", get_rustc_link_lib(&interpreter_config)?);
|
|
|
|
// Set during cross-compiling.
|
|
|
|
if let Some(libdir) = &interpreter_config.libdir {
|
|
|
|
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);
|
|
|
|
}
|
2017-05-13 05:05:00 +00:00
|
|
|
}
|
2021-04-02 13:04:44 +00:00
|
|
|
(true, "macos") => {
|
|
|
|
// with extension module on macos some extra linker arguments are needed
|
|
|
|
println!("cargo:rustc-cdylib-link-arg=-undefined");
|
|
|
|
println!("cargo:rustc-cdylib-link-arg=dynamic_lookup");
|
|
|
|
}
|
|
|
|
(false, _) | (_, "android") => {
|
|
|
|
// other systems, only link libs if not extension module
|
|
|
|
// android always link.
|
2021-04-24 09:52:58 +00:00
|
|
|
println!("{}", get_rustc_link_lib(&interpreter_config)?);
|
2021-04-02 13:04:44 +00:00
|
|
|
if let Some(libdir) = &interpreter_config.libdir {
|
|
|
|
println!("cargo:rustc-link-search=native={}", libdir);
|
|
|
|
}
|
2021-04-24 09:52:58 +00:00
|
|
|
if interpreter_config.implementation == PythonImplementation::PyPy {
|
|
|
|
// PyPy 7.3.4 changed LIBDIR to point to base_prefix/lib as a regression, so need
|
|
|
|
// to hard-code /bin search path too: https://foss.heptapod.net/pypy/pypy/-/issues/3442
|
|
|
|
//
|
|
|
|
// TODO: this workaround can probably be removed when PyPy 7.3.5 is released (and we
|
|
|
|
// can call it a PyPy bug).
|
|
|
|
if let Some(base_prefix) = &interpreter_config.base_prefix {
|
|
|
|
println!("cargo:rustc-link-search=native={}/bin", base_prefix);
|
|
|
|
}
|
2021-04-18 07:17:20 +00:00
|
|
|
}
|
2021-04-02 13:04:44 +00:00
|
|
|
}
|
|
|
|
_ => {}
|
2017-05-13 05:05:00 +00:00
|
|
|
}
|
|
|
|
|
2021-04-11 08:27:50 +00:00
|
|
|
if env::var_os("CARGO_FEATURE_AUTO_INITIALIZE").is_some() {
|
2021-05-19 20:50:25 +00:00
|
|
|
if !interpreter_config.shared {
|
|
|
|
return Err(format!(
|
|
|
|
"The `auto-initialize` feature is enabled, but your python installation only supports \
|
|
|
|
embedding the Python interpreter statically. If you are attempting to run tests, or a \
|
|
|
|
binary which is okay to link dynamically, install a Python distribution which ships \
|
|
|
|
with the Python shared library.\n\
|
|
|
|
\n\
|
|
|
|
Embedding the Python interpreter statically does not yet have first-class support in \
|
|
|
|
PyO3. If you are sure you intend to do this, disable the `auto-initialize` feature.\n\
|
|
|
|
\n\
|
|
|
|
For more information, see \
|
|
|
|
https://pyo3.rs/v{pyo3_version}/\
|
|
|
|
building_and_distribution.html#embedding-python-in-rust",
|
|
|
|
pyo3_version = env::var("CARGO_PKG_VERSION").unwrap()
|
|
|
|
)
|
|
|
|
.into());
|
|
|
|
}
|
2021-04-11 08:27:50 +00:00
|
|
|
|
|
|
|
// TODO: PYO3_CI env is a hack to workaround CI with PyPy, where the `dev-dependencies`
|
|
|
|
// currently cause `auto-initialize` to be enabled in CI.
|
|
|
|
// Once cargo's `resolver = "2"` is stable (~ MSRV Rust 1.52), remove this.
|
2021-05-19 20:50:25 +00:00
|
|
|
if interpreter_config.is_pypy() && env::var_os("PYO3_CI").is_none() {
|
|
|
|
return Err("The `auto-initialize` feature is not supported with PyPy.".into());
|
2020-10-31 07:35:14 +00:00
|
|
|
}
|
2017-06-11 23:35:24 +00:00
|
|
|
}
|
2019-05-16 21:24:55 +00:00
|
|
|
|
2021-05-19 20:50:25 +00:00
|
|
|
Ok(())
|
|
|
|
}
|
2021-04-24 09:52:58 +00:00
|
|
|
|
2021-05-19 20:50:25 +00:00
|
|
|
fn configure_pyo3() -> Result<()> {
|
2021-05-21 07:38:59 +00:00
|
|
|
let interpreter_config = pyo3_build_config::get();
|
|
|
|
ensure_python_version(&interpreter_config)?;
|
|
|
|
ensure_target_architecture(&interpreter_config)?;
|
|
|
|
emit_cargo_configuration(&interpreter_config)?;
|
|
|
|
interpreter_config.emit_pyo3_cfgs();
|
2021-03-31 15:04:15 +00:00
|
|
|
|
2021-05-19 20:50:25 +00:00
|
|
|
// Enable use of const generics on Rust 1.51 and greater
|
2021-03-31 15:04:15 +00:00
|
|
|
if rustc_minor_version().unwrap_or(0) >= 51 {
|
|
|
|
println!("cargo:rustc-cfg=min_const_generics");
|
|
|
|
}
|
|
|
|
|
2020-12-30 23:45:03 +00:00
|
|
|
Ok(())
|
2017-06-11 23:35:24 +00:00
|
|
|
}
|
2017-05-13 05:05:00 +00:00
|
|
|
|
2021-03-29 07:37:27 +00:00
|
|
|
fn main() {
|
|
|
|
// Print out error messages using display, to get nicer formatting.
|
2021-04-24 09:52:58 +00:00
|
|
|
if let Err(e) = configure_pyo3() {
|
|
|
|
eprintln!("error: {}", e);
|
2021-03-29 07:37:27 +00:00
|
|
|
std::process::exit(1)
|
2021-04-24 09:52:58 +00:00
|
|
|
}
|
2021-03-29 07:37:27 +00:00
|
|
|
}
|