Skip to content

Commit cba65ec

Browse files
committed
Add buildinfo() Python method
This currently returns a dict with a single entry for the rustc version. We do this because the Rust compiler doesn't embed its own version into the resulting library on macOS (not sure about Windows), so we can't reliably extract the compiler version that was used to build reclass-rs from the library `.so`. The implementation uses a Rust build script (`build.rs`) which uses the `rustc_version` crate to get the Rust compiler's version. For now, the crate just provides a convenience wrapper for calling `rustc --version`, but would allow us to access more structured compiler info in the future.
1 parent feb7982 commit cba65ec

4 files changed

Lines changed: 51 additions & 0 deletions

File tree

Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@ serde_yaml = "0.9.34"
3131
walkdir = "2.5.0"
3232
yaml-merge-keys = { version = "0.8.1", features = ["serde_yaml"] }
3333

34+
[build-dependencies]
35+
rustc_version = "0.4.1"
36+
3437
[dev-dependencies]
3538
criterion = "0.6.0"
3639
paste = "1.0.14"

build.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
use std::env;
2+
use std::fs;
3+
use std::path::Path;
4+
5+
use rustc_version::version_meta;
6+
7+
fn main() {
8+
let rustc_version = version_meta().unwrap().short_version_string;
9+
let out_dir = env::var("OUT_DIR").unwrap();
10+
let dest_path = Path::new(&out_dir).join("rustc_version.rs");
11+
fs::write(
12+
&dest_path,
13+
format!(r#"static RUSTC_VERSION: &str = "{rustc_version}";"#,),
14+
)
15+
.unwrap();
16+
}

src/lib.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@
77
#![allow(clippy::module_name_repetitions)]
88
#![allow(clippy::similar_names)]
99

10+
// embed RUSTC_VERSION definition which is generated by `build.rs` using the `rustc_version` crate
11+
include!(concat!(env!("OUT_DIR"), "/rustc_version.rs"));
12+
1013
mod config;
1114
mod fsutil;
1215
mod inventory;
@@ -405,6 +408,13 @@ impl Default for Reclass {
405408
}
406409
}
407410

411+
#[pyfunction]
412+
fn buildinfo() -> HashMap<&'static str, &'static str> {
413+
let mut res = HashMap::new();
414+
res.insert("rustc_version", RUSTC_VERSION);
415+
res
416+
}
417+
408418
#[pymodule]
409419
fn reclass_rs(_py: Python, m: &Bound<'_, PyModule>) -> PyResult<()> {
410420
// Register the top-level `Reclass` Python class which is used to configure the library
@@ -417,6 +427,8 @@ fn reclass_rs(_py: Python, m: &Bound<'_, PyModule>) -> PyResult<()> {
417427
m.add_class::<NodeInfo>()?;
418428
// Register the Inventory class
419429
m.add_class::<Inventory>()?;
430+
// Register the buildinfo method
431+
m.add_function(wrap_pyfunction!(buildinfo, m)?)?;
420432
Ok(())
421433
}
422434

tests/test_buildinfo.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import subprocess
2+
import pytest
3+
4+
import reclass_rs
5+
6+
7+
def test_buildinfo():
8+
buildinfo = reclass_rs.buildinfo()
9+
10+
try:
11+
rustc_version = (
12+
subprocess.run(
13+
["rustc", "--version"], stderr=subprocess.STDOUT, stdout=subprocess.PIPE
14+
)
15+
.stdout.decode("utf-8")
16+
.strip()
17+
)
18+
except FileNotFoundError:
19+
pytest.skip("No rustc compiler available in test environment")
20+
assert buildinfo == {"rustc_version": rustc_version}

0 commit comments

Comments
 (0)