2022-01-25 13:46:27 +00:00
|
|
|
use pyo3_build_config::{
|
2022-02-18 18:20:32 +00:00
|
|
|
bail, ensure, print_feature_cfgs,
|
2022-01-25 13:46:27 +00:00
|
|
|
pyo3_build_script_impl::{
|
2022-03-25 09:12:05 +00:00
|
|
|
cargo_env_var, env_var, errors::Result, is_linking_libpython, resolve_interpreter_config,
|
|
|
|
InterpreterConfig, PythonVersion,
|
2022-01-25 13:46:27 +00:00
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
/// Minimum Python version PyO3 supports.
|
|
|
|
const MINIMUM_SUPPORTED_VERSION: PythonVersion = PythonVersion { major: 3, minor: 7 };
|
|
|
|
|
|
|
|
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_pointer_width(interpreter_config: &InterpreterConfig) -> Result<()> {
|
|
|
|
if let Some(pointer_width) = interpreter_config.pointer_width {
|
|
|
|
// Try to check whether the target architecture matches the python library
|
|
|
|
let rust_target = match cargo_env_var("CARGO_CFG_TARGET_POINTER_WIDTH")
|
|
|
|
.unwrap()
|
|
|
|
.as_str()
|
|
|
|
{
|
|
|
|
"64" => 64,
|
|
|
|
"32" => 32,
|
|
|
|
x => bail!("unexpected Rust target pointer width: {}", x),
|
|
|
|
};
|
|
|
|
|
|
|
|
ensure!(
|
|
|
|
rust_target == pointer_width,
|
|
|
|
"your Rust target architecture ({}-bit) does not match your python interpreter ({}-bit)",
|
|
|
|
rust_target,
|
|
|
|
pointer_width
|
|
|
|
);
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn emit_link_config(interpreter_config: &InterpreterConfig) -> Result<()> {
|
|
|
|
let target_os = cargo_env_var("CARGO_CFG_TARGET_OS").unwrap();
|
|
|
|
|
2022-03-25 09:12:05 +00:00
|
|
|
println!(
|
|
|
|
"cargo:rustc-link-lib={link_model}{alias}{lib_name}",
|
|
|
|
link_model = if interpreter_config.shared {
|
|
|
|
""
|
|
|
|
} else {
|
|
|
|
"static="
|
|
|
|
},
|
|
|
|
alias = if target_os == "windows" {
|
|
|
|
"pythonXY:"
|
|
|
|
} else {
|
|
|
|
""
|
|
|
|
},
|
|
|
|
lib_name = interpreter_config.lib_name.as_ref().ok_or(
|
|
|
|
"attempted to link to Python shared library but config does not contain lib_name"
|
|
|
|
)?,
|
|
|
|
);
|
|
|
|
|
|
|
|
if let Some(lib_dir) = &interpreter_config.lib_dir {
|
|
|
|
println!("cargo:rustc-link-search=native={}", lib_dir);
|
|
|
|
}
|
2022-03-17 14:29:43 +00:00
|
|
|
|
2022-01-25 13:46:27 +00:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Prepares the PyO3 crate for compilation.
|
|
|
|
///
|
|
|
|
/// This loads the config from pyo3-build-config and then makes some additional checks to improve UX
|
|
|
|
/// for users.
|
|
|
|
///
|
|
|
|
/// Emits the cargo configuration based on this config as well as a few checks of the Rust compiler
|
|
|
|
/// version to enable features which aren't supported on MSRV.
|
|
|
|
fn configure_pyo3() -> Result<()> {
|
|
|
|
let interpreter_config = resolve_interpreter_config()?;
|
|
|
|
|
|
|
|
if env_var("PYO3_PRINT_CONFIG").map_or(false, |os_str| os_str == "1") {
|
|
|
|
print_config_and_exit(&interpreter_config);
|
|
|
|
}
|
|
|
|
|
|
|
|
ensure_python_version(&interpreter_config)?;
|
|
|
|
ensure_target_pointer_width(&interpreter_config)?;
|
|
|
|
|
2022-03-25 09:12:05 +00:00
|
|
|
// Serialize the whole interpreter config into DEP_PYTHON_PYO3_CONFIG env var.
|
|
|
|
interpreter_config.to_cargo_dep_env()?;
|
|
|
|
|
|
|
|
if is_linking_libpython() && !interpreter_config.suppress_build_script_link_lines {
|
2022-01-25 13:46:27 +00:00
|
|
|
emit_link_config(&interpreter_config)?;
|
|
|
|
}
|
|
|
|
|
2023-10-22 22:15:08 +00:00
|
|
|
for cfg in interpreter_config.build_script_outputs() {
|
|
|
|
println!("{}", cfg)
|
|
|
|
}
|
2022-01-25 13:46:27 +00:00
|
|
|
|
|
|
|
// Extra lines come last, to support last write wins.
|
|
|
|
for line in &interpreter_config.extra_build_script_lines {
|
|
|
|
println!("{}", line);
|
|
|
|
}
|
|
|
|
|
2023-06-05 07:42:40 +00:00
|
|
|
// Emit cfgs like `thread_local_const_init`
|
2022-02-18 18:20:32 +00:00
|
|
|
print_feature_cfgs();
|
2022-02-16 14:14:10 +00:00
|
|
|
|
2022-01-25 13:46:27 +00:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn print_config_and_exit(config: &InterpreterConfig) {
|
|
|
|
println!("\n-- PYO3_PRINT_CONFIG=1 is set, printing configuration and halting compile --");
|
|
|
|
config
|
2023-10-22 22:15:08 +00:00
|
|
|
.to_writer(std::io::stdout())
|
2022-01-25 13:46:27 +00:00
|
|
|
.expect("failed to print config to stdout");
|
2022-04-12 06:54:23 +00:00
|
|
|
println!("\nnote: unset the PYO3_PRINT_CONFIG environment variable and retry to compile with the above config");
|
2022-01-25 13:46:27 +00:00
|
|
|
std::process::exit(101);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
if let Err(e) = configure_pyo3() {
|
|
|
|
eprintln!("error: {}", e.report());
|
|
|
|
std::process::exit(1)
|
|
|
|
}
|
|
|
|
}
|