Skip to content

Commit

Permalink
Merge pull request #48 from magnusuMET/const_hdf5_version
Browse files Browse the repository at this point in the history
Emit hdf5 version as a build constant
  • Loading branch information
magnusuMET authored Jan 13, 2025
2 parents c086ddd + ee9511b commit 2f03deb
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 2 deletions.
8 changes: 7 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,13 @@
## hdf5-sys unreleased
## hdf5-src unreleased

## hdf5 unreleased v0.9.4
## hdf5 unreleased
- Added `HDF5_VERSION` which contains the version used when building

## hdf5-sys unreleased
- Added `HDF5_VERSION` which contains the version used when building

## hdf5 v0.9.4
Release date: Jan 7, 2025.
- Fixed bundled hdf5 version back to 1.14.5

Expand Down
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();
}
15 changes: 15 additions & 0 deletions hdf5-sys/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,13 +76,28 @@ 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::H5get_libversion;
use super::h5::H5open;
use super::h5p::H5P_CLS_ROOT;
use super::{Version, HDF5_VERSION, LOCK};

#[test]
fn version_test() {
let _lock = LOCK.lock();
let (mut major, mut minor, mut micro) = (0, 0, 0);
unsafe { H5get_libversion(&mut major, &mut minor, &mut micro) };
let runtime_version = Version { major: major as _, minor: minor as _, micro: micro as _ };

assert_eq!(runtime_version, HDF5_VERSION);
}

#[test]
pub fn test_smoke() {
let _lock = LOCK.lock();
unsafe {
H5open();
assert!(*H5P_CLS_ROOT > 0);
Expand Down
15 changes: 14 additions & 1 deletion hdf5/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -206,12 +206,25 @@ pub fn is_library_threadsafe() -> bool {
}
}

/// HDF5 library version used at link time. See [`hdf5_sys::HDF5_VERSION`]
pub const HDF5_VERSION: hdf5_sys::Version = hdf5_sys::HDF5_VERSION;

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

#[test]
pub fn test_library_version() {
pub fn test_minimum_library_version() {
assert!(library_version() >= (1, 8, 4));
}

#[test]
fn library_version_eq_compile_version() {
use hdf5_sys::Version;
let (major, minor, micro) = library_version();
let runtime_version = Version { major, minor, micro };

assert_eq!(runtime_version, HDF5_VERSION);
}
}

0 comments on commit 2f03deb

Please sign in to comment.