pyo3-built
Simple macro to expose metadata obtained with the built
crate as a PyDict
Add the following to your Cargo.toml
manifest:
toml
[build-dependencies]
built = { version = "0.4", features = ["chrono"] }
[dependencies]
pyo3-built = "0.4"
Create your build.rs
as you normally would with built
, but activate
dependencies metadata as well:
```rust
//! build.rs
extern crate built;
fn main() { let src = std::env::var("CARGOMANIFESTDIR").unwrap(); let dst = std::path::Path::new(&std::env::var("OUT_DIR").unwrap()).join("built.rs"); let mut opts = built::Options::default();
opts.set_dependencies(true)
.set_compiler(true)
.set_env(true);
built::write_built_file_with_opts(&opts, std::path::Path::new(&src), &dst)
.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
//! lib.rs
extern crate pyo3_built; extern crate pyo3;
use pyo3::prelude::*;
mod build { include!(concat!(env!("OUT_DIR"), "/built.rs")); }
/// Module documentation string
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": "x8664-unknown-linux-gnu" }, "opt-level": "0", "rustc": "rustc", "rustc-version": "rustc 1.27.0-nightly (acd3871ba 2018-05-10)", "target": { "arch": "x8664", "endianness": "little", "env": "gnu", "family": "unix", "os": "linux", "pointer-width": "64", "profile": "debug", "triple": "x86_64-unknown-linux-gnu" } } ```
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
m.add("__build__", pyo3_built!(py, build, "time", "git", "target"))?;
The following parameters are available (they mirror built categories):
- "build"
- "time"
- "deps"
- "features"
- "host"
- "target"
- "git"
The corresponding options must be enabled in the built crate and the build.rs for this to work.
By default everything except "git"
is enabled.