#pyo3 #built #cpython #cargo-build #python

build pyo3-built

Expose build variables obtained with built as a PyDict

12 releases

Uses old Rust 2015

0.5.0 Apr 11, 2024
0.4.7 May 2, 2022
0.4.6 Aug 19, 2021
0.4.5 Feb 19, 2021
0.2.0 May 28, 2019

#60 in FFI

Download history 418/week @ 2024-07-22 554/week @ 2024-07-29 504/week @ 2024-08-05 707/week @ 2024-08-12 615/week @ 2024-08-19 427/week @ 2024-08-26 786/week @ 2024-09-02 2288/week @ 2024-09-09 1566/week @ 2024-09-16 1575/week @ 2024-09-23 3130/week @ 2024-09-30 2377/week @ 2024-10-07 1701/week @ 2024-10-14 2914/week @ 2024-10-21 1423/week @ 2024-10-28 1276/week @ 2024-11-04

7,341 downloads per month
Used in nanoset-py

Apache-2.0

9KB
90 lines

pyo3-built

Simple macro to expose metadata obtained with the built crate as a PyDict

Build License Source Crate GitHub issues

Usage

Add the following to your Cargo.toml manifest:

[build-dependencies]
built = { version = "0.7", features = ["chrono"] }
[dependencies]
pyo3-built = "0.5"

Create your build.rs as you normally would with built, but activate dependencies metadata as well:

//! 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:

//! 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:

>>> 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:

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.

No runtime deps