#pyo3 #python #cpython #built

pyo3-built

Expose build variables obtained with built as a PyDict

10 releases

Uses old Rust 2015

0.4.6 Aug 19, 2021
0.4.5 Feb 19, 2021
0.4.4 Sep 26, 2020
0.4.2 Jun 30, 2020
0.2.0 May 28, 2019

#24 in FFI

Download history 722/week @ 2021-10-04 597/week @ 2021-10-11 1125/week @ 2021-10-18 796/week @ 2021-10-25 850/week @ 2021-11-01 742/week @ 2021-11-08 652/week @ 2021-11-15 421/week @ 2021-11-22 644/week @ 2021-11-29 902/week @ 2021-12-06 615/week @ 2021-12-13 365/week @ 2021-12-20 198/week @ 2021-12-27 1431/week @ 2022-01-03 647/week @ 2022-01-10 867/week @ 2022-01-17

3,156 downloads per month
Used in nanoset-py

Apache-2.0

10KB
99 lines

pyo3-built

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

TravisCI License Source Crate GitHub issues

Usage

Add the following to your Cargo.toml manifest:

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

//! build.rs
extern crate built;

fn main() {
    let src = std::env::var("CARGO_MANIFEST_DIR").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:

//! 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"
   }
}

No runtime deps