https://github.com/pyo3/pyo3-built
Expose build variables obtained with built as a PyDict
https://github.com/pyo3/pyo3-built
Last synced: 3 months ago
JSON representation
Expose build variables obtained with built as a PyDict
- Host: GitHub
- URL: https://github.com/pyo3/pyo3-built
- Owner: PyO3
- License: apache-2.0
- Created: 2018-05-11T14:11:14.000Z (over 7 years ago)
- Default Branch: main
- Last Pushed: 2025-01-22T11:19:54.000Z (12 months ago)
- Last Synced: 2025-03-28T06:06:58.877Z (10 months ago)
- Language: Rust
- Homepage:
- Size: 45.9 KB
- Stars: 34
- Watchers: 9
- Forks: 7
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# `pyo3-built`
*Simple macro to expose metadata obtained with the [`built`](https://crates.io/crates/built)
crate as a [`PyDict`](https://pyo3.github.io/pyo3/pyo3/struct.PyDict.html)*
[](https://github.com/PyO3/pyo3-built/actions)
[](https://choosealicense.com/licenses/apache-2.0/)
[](https://github.com/PyO3/pyo3-built)
[](https://crates.io/crates/pyo3-built)
[](https://github.com/PyO3/pyo3-built/issues)
## Usage
Add the following to your `Cargo.toml` manifest:
```toml
[build-dependencies]
built = { version = "0.7", features = ["chrono"] }
[dependencies]
pyo3-built = "0.6"
```
Create your `build.rs` as you normally would with `built`, but activate
dependencies metadata as well:
```rust,ignore
//! build.rs
extern crate built;
fn main() {
built::write_built_file().expect("Failed to acquire build-time information");
}
```
Then, include the generated file anywhere in a dedicated module in your Python
extension, and use the `pyo3_built!` macro to generate the `PyDict`:
```rust,ignore
//! lib.rs
#[macro_use]
extern crate pyo3_built;
extern crate pyo3;
use pyo3::prelude::*;
#[allow(dead_code)]
mod build {
include!(concat!(env!("OUT_DIR"), "/built.rs"));
}
/// Module documentation string
#[modinit("mymodule")]
fn init(py: Python, m: &PyModule) -> PyResult<()> {
// ... //
m.add("__build__", pyo3_built!(py, build))?;
Ok(())
}
```
That's it ! After compiling your extension module, the `__build__` attribute
will contain the following metadata:
```python
>>> import mymodule
>>> mymodule.__build__
{
"build-time": datetime.datetime(2018, 5, 11, 16, 43, 28),
"debug": true,
"dependencies": {
...
"pyo3": "0.6.0",
"pyo3-built": "0.1.0",
"pyo3cls": "0.6.0",
...
},
"features": [
"PYO3"
],
"host": {
"triple": "x86_64-unknown-linux-gnu"
},
"opt-level": "0",
"rustc": "rustc",
"rustc-version": "rustc 1.27.0-nightly (acd3871ba 2018-05-10)",
"target": {
"arch": "x86_64",
"endianness": "little",
"env": "gnu",
"family": "unix",
"os": "linux",
"pointer-width": "64",
"profile": "debug",
"triple": "x86_64-unknown-linux-gnu"
}
}
```
### Customization
When invoking the macro, one can control what will be added
to the build dictionary by postfixing the list of the parameters we want in the dictionary.
See the following example:
```rust,ignore
m.add("__build__", pyo3_built!(py, build, "time", "git", "target"))?;
```
The following parameters are available, mirroring built categories:
- `"build"`
- `"time"` (requires the `chrono` feature of `built`)
- `"deps"`
- `"features"` (requires the `cargo-lock` feature of `built`)
- `"host"`
- `"target"`
- `"git"` (requires the `git2` feature of `built`)
By default everything except `"git"` is enabled.