Skip to content

Commit cfe7262

Browse files
committed
libhdf5-lib::emit_cfg_flags() - pass flags down
1 parent 04ddde5 commit cfe7262

File tree

5 files changed

+51
-43
lines changed

5 files changed

+51
-43
lines changed

hdf5-rs/build.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
fn main() {
2-
libhdf5_lib::dump_build_flags();
2+
libhdf5_lib::emit_cfg_flags();
33
}

hdf5-rs/src/lib.rs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -95,9 +95,14 @@ mod internal_prelude {
9595
#[cfg(test)]
9696
pub mod test;
9797

98-
/// Returns the version of the HDF5 library that the crate was compiled against.
98+
/// Returns the runtime version of the HDF5 library.
9999
pub fn hdf5_version() -> (u8, u8, u8) {
100-
h5lock!(libhdf5_lib::hdf5_version()).unwrap_or((0, 0, 0))
100+
use self::internal_prelude::c_uint;
101+
use libhdf5_sys::h5::H5get_libversion;
102+
let mut v: (c_uint, c_uint, c_uint) = (0, 0, 0);
103+
h5call!(H5get_libversion(&mut v.0, &mut v.1, &mut v.2))
104+
.map(|_| (v.0 as _, v.1 as _, v.2 as _))
105+
.unwrap_or((0, 0, 0))
101106
}
102107

103108
#[cfg(test)]
@@ -106,6 +111,6 @@ pub mod tests {
106111

107112
#[test]
108113
pub fn test_hdf5_version() {
109-
assert!(hdf5_version() >= (1, 8, 0));
114+
assert!(hdf5_version() >= (1, 8, 4));
110115
}
111116
}

libhdf5-lib/build.rs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -546,20 +546,31 @@ pub struct Config {
546546
}
547547

548548
impl Config {
549-
pub fn emit_flags(&self) {
549+
pub fn emit_link_flags(&self) {
550550
println!("cargo:rustc-link-lib=dylib=hdf5");
551551
for dir in &self.link_paths {
552552
println!("cargo:rustc-link-search=native={}", dir.to_str().unwrap());
553553
}
554554
println!("cargo:rerun-if-env-changed=HDF5_DIR");
555555
println!("cargo:rerun-if-env-changed=HDF5_VERSION");
556556
}
557+
558+
pub fn emit_cfg_flags(&self) {
559+
let version = self.header.version;
560+
assert!(version >= Version::new(1, 8, 4), "required HDF5 version: >=1.8.4");
561+
let mut vs: Vec<_> = (5..=21).map(|v| Version::new(1, 8, v)).collect(); // 1.8.5-21
562+
vs.extend((0..=4).map(|v| Version::new(1, 10, v))); // 1.10.0-4
563+
for v in vs.into_iter().filter(|&v| version >= v) {
564+
println!("cargo:rustc-cfg=hdf5_{}_{}_{}", v.major, v.minor, v.micro);
565+
}
566+
}
557567
}
558568

559569
fn main() {
560570
let mut searcher = LibrarySearcher::new_from_env();
561571
searcher.try_locate_hdf5_library();
562572
let config = searcher.finalize();
563573
println!("{:#?}", config);
564-
config.emit_flags();
574+
config.emit_link_flags();
575+
config.emit_cfg_flags();
565576
}

libhdf5-lib/src/lib.rs

Lines changed: 28 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,32 @@
1-
use std::os::raw::{c_int, c_uint};
2-
3-
extern "C" {
4-
pub fn H5open() -> c_int;
5-
pub fn H5get_libversion(majnum: *mut c_uint, minnum: *mut c_uint, relnum: *mut c_uint)
6-
-> c_int;
7-
}
8-
9-
pub fn hdf5_version() -> Result<(u8, u8, u8), &'static str> {
10-
let mut v: (c_uint, c_uint, c_uint) = (0, 0, 0);
11-
unsafe {
12-
if H5open() != 0 {
13-
Err("cannot open HDF5 library")
14-
} else if H5get_libversion(&mut v.0, &mut v.1, &mut v.2) != 0 {
15-
Err("cannot get HDF5 version")
16-
} else {
17-
Ok((v.0 as _, v.1 as _, v.2 as _))
1+
macro_rules! check_and_emit {
2+
($flag:ident) => {
3+
if cfg!($flag) {
4+
println!("cargo:rustc-cfg={}", stringify!($flag));
185
}
19-
}
6+
};
207
}
218

22-
pub fn dump_build_flags() {
23-
let version = hdf5_version().unwrap();
24-
assert!(version >= (1, 8, 4));
25-
let mut vs: Vec<_> = (5..=21).map(|v| (1, 8, v)).collect();
26-
vs.extend((0..=1).map(|v| (1, 10, v)));
27-
for v in vs.into_iter().filter(|&v| version >= v) {
28-
println!("cargo:rustc-cfg=hdf5_{}_{}_{}", v.0, v.1, v.2);
29-
}
30-
}
31-
32-
#[cfg(test)]
33-
pub mod tests {
34-
use super::hdf5_version;
35-
36-
#[test]
37-
fn test_version() {
38-
assert!(hdf5_version().unwrap() >= (1, 8, 4));
39-
}
9+
pub fn emit_cfg_flags() {
10+
check_and_emit!(hdf5_1_8_5);
11+
check_and_emit!(hdf5_1_8_6);
12+
check_and_emit!(hdf5_1_8_7);
13+
check_and_emit!(hdf5_1_8_8);
14+
check_and_emit!(hdf5_1_8_9);
15+
check_and_emit!(hdf5_1_8_10);
16+
check_and_emit!(hdf5_1_8_11);
17+
check_and_emit!(hdf5_1_8_12);
18+
check_and_emit!(hdf5_1_8_13);
19+
check_and_emit!(hdf5_1_8_14);
20+
check_and_emit!(hdf5_1_8_15);
21+
check_and_emit!(hdf5_1_8_16);
22+
check_and_emit!(hdf5_1_8_17);
23+
check_and_emit!(hdf5_1_8_18);
24+
check_and_emit!(hdf5_1_8_19);
25+
check_and_emit!(hdf5_1_8_20);
26+
check_and_emit!(hdf5_1_8_21);
27+
check_and_emit!(hdf5_1_10_0);
28+
check_and_emit!(hdf5_1_10_1);
29+
check_and_emit!(hdf5_1_10_2);
30+
check_and_emit!(hdf5_1_10_3);
31+
check_and_emit!(hdf5_1_10_4);
4032
}

libhdf5-sys/build.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
fn main() {
2-
libhdf5_lib::dump_build_flags();
2+
libhdf5_lib::emit_cfg_flags();
33
}

0 commit comments

Comments
 (0)