Skip to content

Commit

Permalink
Emit version as a build constant
Browse files Browse the repository at this point in the history
  • Loading branch information
magnusuMET committed Jan 13, 2025
1 parent c086ddd commit 3664338
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 0 deletions.
24 changes: 24 additions & 0 deletions hdf5-sys/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use std::env;
use std::error::Error;
use std::fmt::{self, Debug};
use std::fs;
use std::io::Write;
use std::os::raw::{c_int, c_uint};
use std::path::{Path, PathBuf};
use std::process::Command;
Expand Down Expand Up @@ -725,6 +726,7 @@ fn main() {
println!("{:#?}", config);
config.emit_link_flags();
config.emit_cfg_flags();
write_hdf5_version(config.header.version);
}
}

Expand Down Expand Up @@ -755,4 +757,26 @@ fn get_build_and_emit() {
let header = Header::parse(&hdf5_incdir);
let config = Config { header, inc_dir: "".into(), link_paths: Vec::new() };
config.emit_cfg_flags();
write_hdf5_version(header.version);
}

fn write_hdf5_version(version: Version) {
let out_dir = std::env::var("OUT_DIR").unwrap();
let destination = std::path::Path::new(&out_dir).join("version.rs");
let mut f = std::fs::File::create(destination).unwrap();
let (major, minor, micro) = (version.major, version.minor, version.micro);
let string = format!(
"
#[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord)]
pub struct Version {{
pub major: u8,
pub minor: u8,
pub micro: u8,
}}
/// HDF5 library version used at link time ({major}.{minor}.{micro})
pub const HDF5_VERSION: Version = Version {{ major: {major}, minor: {minor}, micro: {micro} }};
"
);
f.write_all(string.as_bytes()).unwrap();
}
2 changes: 2 additions & 0 deletions hdf5-sys/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@ use parking_lot::ReentrantMutex;
/// Lock which can be used to serialise access to the hdf5 library
pub static LOCK: ReentrantMutex<()> = ReentrantMutex::new(());

include!(concat!(env!("OUT_DIR"), "/version.rs"));

#[cfg(test)]
mod tests {
use super::h5::H5open;
Expand Down
2 changes: 2 additions & 0 deletions hdf5/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,8 @@ pub fn is_library_threadsafe() -> bool {
}
}

pub const HDF5_VERSION: hdf5_sys::Version = hdf5_sys::HDF5_VERSION;

#[cfg(test)]
pub mod tests {
use crate::library_version;
Expand Down

0 comments on commit 3664338

Please sign in to comment.