Skip to content

Commit 5093c29

Browse files
authored
Merge pull request #1749 from davidhewitt/building-distribution-rewrite
guide: rewrite "Building and Distribution" chapter
2 parents 9a69d12 + 582d9c5 commit 5093c29

File tree

11 files changed

+180
-159
lines changed

11 files changed

+180
-159
lines changed

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@
1616

1717
## Usage
1818

19-
PyO3 supports Python 3.6 and up. The minimum required Rust version is 1.41.
20-
21-
PyPy is also supported. Some minor features are unavailable on PyPy - please refer to the [pypy section in the guide](https://pyo3.rs/latest/building_and_distribution/pypy.html) for more information.
19+
PyO3 supports the following software versions:
20+
- Python 3.6 and up (CPython and PyPy)
21+
- Rust 1.41 and up
2222

2323
You can use PyO3 to write a native Python module in Rust, or to embed Python in a Rust binary. The following sections explain each of these in turn.
2424

build.rs

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use std::{env, process::Command};
22

33
use pyo3_build_config::{
4-
bail, ensure,
4+
bail, cargo_env_var, ensure, env_var,
55
errors::{Context, Result},
66
InterpreterConfig, PythonImplementation, PythonVersion,
77
};
@@ -22,7 +22,10 @@ fn ensure_python_version(interpreter_config: &InterpreterConfig) -> Result<()> {
2222

2323
fn ensure_target_architecture(interpreter_config: &InterpreterConfig) -> Result<()> {
2424
// Try to check whether the target architecture matches the python library
25-
let rust_target = match env::var("CARGO_CFG_TARGET_POINTER_WIDTH").unwrap().as_str() {
25+
let rust_target = match cargo_env_var("CARGO_CFG_TARGET_POINTER_WIDTH")
26+
.unwrap()
27+
.as_str()
28+
{
2629
"64" => "64-bit",
2730
"32" => "32-bit",
2831
x => bail!("unexpected Rust target pointer width: {}", x),
@@ -55,14 +58,14 @@ fn ensure_target_architecture(interpreter_config: &InterpreterConfig) -> Result<
5558
}
5659

5760
fn get_rustc_link_lib(config: &InterpreterConfig) -> Result<String> {
58-
let link_name = if env::var_os("CARGO_CFG_TARGET_OS").unwrap() == "windows" {
61+
let link_name = if cargo_env_var("CARGO_CFG_TARGET_OS").unwrap() == "windows" {
5962
if config.abi3 {
6063
// Link against python3.lib for the stable ABI on Windows.
6164
// See https://www.python.org/dev/peps/pep-0384/#linkage
6265
//
6366
// This contains only the limited ABI symbols.
6467
"pythonXY:python3".to_owned()
65-
} else if env::var_os("CARGO_CFG_TARGET_ENV").unwrap() == "gnu" {
68+
} else if cargo_env_var("CARGO_CFG_TARGET_ENV").unwrap() == "gnu" {
6669
// https://packages.msys2.org/base/mingw-w64-python
6770
format!(
6871
"pythonXY:python{}.{}",
@@ -103,8 +106,8 @@ fn rustc_minor_version() -> Option<u32> {
103106
}
104107

105108
fn emit_cargo_configuration(interpreter_config: &InterpreterConfig) -> Result<()> {
106-
let target_os = env::var("CARGO_CFG_TARGET_OS").unwrap();
107-
let is_extension_module = env::var_os("CARGO_FEATURE_EXTENSION_MODULE").is_some();
109+
let target_os = cargo_env_var("CARGO_CFG_TARGET_OS").unwrap();
110+
let is_extension_module = cargo_env_var("CARGO_FEATURE_EXTENSION_MODULE").is_some();
108111
match (is_extension_module, target_os.as_str()) {
109112
(_, "windows") => {
110113
// always link on windows, even with extension module
@@ -144,7 +147,7 @@ fn emit_cargo_configuration(interpreter_config: &InterpreterConfig) -> Result<()
144147
_ => {}
145148
}
146149

147-
if env::var_os("CARGO_FEATURE_AUTO_INITIALIZE").is_some() {
150+
if cargo_env_var("CARGO_FEATURE_AUTO_INITIALIZE").is_some() {
148151
if !interpreter_config.shared {
149152
bail!(
150153
"The `auto-initialize` feature is enabled, but your python installation only supports \
@@ -179,6 +182,9 @@ fn emit_cargo_configuration(interpreter_config: &InterpreterConfig) -> Result<()
179182
/// (including `pyo3-macros-backend` during macro expansion).
180183
fn configure_pyo3() -> Result<()> {
181184
let interpreter_config = pyo3_build_config::make_interpreter_config()?;
185+
if env_var("PYO3_PRINT_CONFIG").map_or(false, |os_str| os_str == "1") {
186+
print_config_and_exit(&interpreter_config);
187+
}
182188
ensure_python_version(&interpreter_config)?;
183189
ensure_target_architecture(&interpreter_config)?;
184190
emit_cargo_configuration(&interpreter_config)?;
@@ -207,6 +213,20 @@ fn configure_pyo3() -> Result<()> {
207213
Ok(())
208214
}
209215

216+
fn print_config_and_exit(config: &InterpreterConfig) {
217+
println!("\n-- PYO3_PRINT_CONFIG=1 is set, printing configuration and halting compile --");
218+
println!("implementation: {}", config.implementation);
219+
println!("interpreter version: {}", config.version);
220+
println!("interpreter path: {:?}", config.executable);
221+
println!("libdir: {:?}", config.libdir);
222+
println!("shared: {}", config.shared);
223+
println!("base prefix: {:?}", config.base_prefix);
224+
println!("ld_version: {:?}", config.ld_version);
225+
println!("pointer width: {:?}", config.calcsize_pointer);
226+
227+
std::process::exit(101);
228+
}
229+
210230
fn main() {
211231
// Print out error messages using display, to get nicer formatting.
212232
if let Err(e) = configure_pyo3() {

guide/src/SUMMARY.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
- [Advanced Topics](advanced.md)
2121
- [Building and Distribution](building_and_distribution.md)
2222
- [Supporting multiple Python versions](building_and_distribution/multiple_python_versions.md)
23-
- [PyPy support](building_and_distribution/pypy.md)
2423
- [Useful Crates](ecosystem.md)
2524
- [Logging](ecosystem/logging.md)
2625
- [Async / Await](ecosystem/async-await.md)

0 commit comments

Comments
 (0)