Skip to content

Commit 2009182

Browse files
authored
Merge pull request #1976 from davidhewitt/pyo3-build-config-docs
docs: pyo3 config files
2 parents 2784b31 + b0af3ec commit 2009182

File tree

3 files changed

+34
-13
lines changed

3 files changed

+34
-13
lines changed

Architecture.md

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,9 @@ To summarize, there are six main parts to the PyO3 codebase.
2828
- [`src/class`]
2929
5. [Procedural macros to simplify usage for users.](#5-procedural-macros-to-simplify-usage-for-users)
3030
- [`src/derive_utils.rs`], [`pyo3-macros`] and [`pyo3-macros-backend`]
31-
6. [`build.rs`](#6-buildrs)
31+
6. [`build.rs` and `pyo3-build-config`](#6-buildrs-and-pyo3-build-config)
3232
- [`build.rs`](https://github.com/PyO3/pyo3/tree/main/build.rs)
33+
- [`pyo3-build-config`]
3334

3435
## 1. Low-level bindings of Python/C API
3536

@@ -50,7 +51,7 @@ With `Py_LIMITED_API`, we can build a Python-version-agnostic binary called an
5051
`Py_37` means that the API is available from Python >= 3.7.
5152
There are also `Py_38`, `Py_39`, and so on.
5253
`PyPy` means that the API definition is for PyPy.
53-
Those flags are set in [`build.rs`](#6-buildrs).
54+
Those flags are set in [`build.rs`](#6-buildrs-and-pyo3-build-config).
5455

5556
## 2. Bindings to Python objects
5657

@@ -166,25 +167,37 @@ some internal tricks for making `#[pyproto]` flexible.
166167
[`src/derive_utils.rs`] contains some utilities used in code generated by these proc-macros,
167168
such as parsing function arguments.
168169

169-
## 6. `build.rs`
170+
## 6. `build.rs` and `pyo3-build-config`
170171

171-
PyO3's [`build.rs`](https://github.com/PyO3/pyo3/tree/main/build.rs) is relatively long
172-
(about 900 lines) to support multiple architectures, interpreters, and usages.
173-
Below is a non-exhaustive list of its functionality:
172+
PyO3 supports a wide range of OSes, interpreters and use cases. The correct environment must be
173+
detected at build time in order to set up relevant conditional compilation correctly. This logic
174+
is captured in the [`pyo3-build-config`] crate, which is a `build-dependency` of `pyo3` and
175+
`pyo3-macros`, and can also be used by downstream users in the same way.
174176

175-
- Cross-compiling support.
176-
- If `TARGET` architecture and `HOST` architecture differ, we find cross compile information
177-
from environment variables (`PYO3_CROSS_LIB_DIR` and `PYO3_CROSS_PYTHON_VERSION`) or system files.
177+
In [`pyo3-build-config`]'s `build.rs` the build environment is detected and inlined into the crate
178+
as a "config file". This works in all cases except for cross-compiling, where it is necessary to
179+
capture this from the `pyo3` `build.rs` to get some extra environment variables that Cargo doesn't
180+
set for build dependencies.
181+
182+
The `pyo3` `build.rs` also runs some safety checks such as ensuring the Python version detected is
183+
actually supported.
184+
185+
Some of the functionality of `pyo3-build-config`:
178186
- Find the interpreter for build and detect the Python version.
179-
- We have to set some version flags like `Py_37`.
180-
- If the interpreter is PyPy, we set `PyPy`.
181-
- If `PYO3_NO_PYTHON` environment variable is set then the interpreter detection is bypassed
187+
- We have to set some version flags like `#[cfg(Py_3_7)]`.
188+
- If the interpreter is PyPy, we set `#[cfg(PyPy)`.
189+
- If the `PYO3_CONFIG_FILE` environment variable is set then that file's contents will be used
190+
instead of any detected configuration.
191+
- If the `PYO3_NO_PYTHON` environment variable is set then the interpreter detection is bypassed
182192
entirely and only abi3 extensions can be built.
183193
- Check if we are building a Python extension.
184194
- If we are building an extension (e.g., Python library installable by `pip`),
185195
we don't link `libpython`.
186196
Currently we use the `extension-module` feature for this purpose. This may change in the future.
187197
See [#1123](https://github.com/PyO3/pyo3/pull/1123).
198+
- Cross-compiling configuration
199+
- If `TARGET` architecture and `HOST` architecture differ, we find cross compile information
200+
from environment variables (`PYO3_CROSS_LIB_DIR` and `PYO3_CROSS_PYTHON_VERSION`) or system files.
188201

189202
<!-- External Links -->
190203

@@ -194,6 +207,7 @@ Below is a non-exhaustive list of its functionality:
194207

195208
[`pyo3-macros`]: https://github.com/PyO3/pyo3/tree/main/pyo3-macros
196209
[`pyo3-macros-backend`]: https://github.com/PyO3/pyo3/tree/main/pyo3-macros-backend
210+
[`pyo3-build-config`]: https://github.com/PyO3/pyo3/tree/main/pyo3-build-config
197211

198212
<!-- Directories -->
199213

guide/pyo3_version.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,13 @@
1818

1919
if PYO3_VERSION_TAG == "main":
2020
PYO3_DOCS_URL = "https://pyo3.rs/main/doc"
21+
PYO3_DOCS_VERSION = "latest"
2122
PYO3_CRATE_VERSION = 'git = "https://github.com/pyo3/pyo3"'
2223
else:
2324
# v0.13.2 -> 0.13.2
2425
version = PYO3_VERSION_TAG.lstrip("v")
2526
PYO3_DOCS_URL = f"https://docs.rs/pyo3/{version}"
27+
PYO3_DOCS_VERSION = version
2628
PYO3_CRATE_VERSION = f'version = "{version}"'
2729

2830

@@ -35,6 +37,7 @@ def replace_section_content(section):
3537
section["Chapter"]["content"]
3638
.replace("{{#PYO3_VERSION_TAG}}", PYO3_VERSION_TAG)
3739
.replace("{{#PYO3_DOCS_URL}}", PYO3_DOCS_URL)
40+
.replace("{{#PYO3_DOCS_VERSION}}", PYO3_DOCS_VERSION)
3841
.replace("{{#PYO3_CRATE_VERSION}}", PYO3_CRATE_VERSION)
3942
)
4043

guide/src/building_and_distribution.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,11 @@ Caused by:
4444
build_flags=WITH_THREAD
4545
```
4646

47-
> Note: if you save the output config to a file, it is possible to manually override the contents and feed it back into PyO3 using the `PYO3_CONFIG_FILE` env var. For now, this is an advanced feature that should not be needed for most users. The format of the config file and its contents are deliberately unstable and undocumented. If you have a production use-case for this config file, please file an issue and help us stabilize it!
47+
### Advanced: config files
48+
49+
If you save the above output config from `PYO3_PRINT_CONFIG` to a file, it is possible to manually override the contents and feed it back into PyO3 using the `PYO3_CONFIG_FILE` env var.
50+
51+
If your build environment is unusual enough that PyO3's regular configuration detection doesn't work, using a config file like this will give you the flexibility to make PyO3 work for you. To see the full set of options supported, see the documentation for the [`InterpreterConfig` struct](https://docs.rs/pyo3-build-config/{{#PYO3_DOCS_VERSION}}/pyo3_build_config/struct.InterpreterConfig.html).
4852

4953
## Building Python extension modules
5054

0 commit comments

Comments
 (0)