Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Emit check-cfg #5

Merged
merged 3 commits into from
Aug 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,7 @@ jobs:
with: {submodules: true}
- name: Install Rust
uses: dtolnay/rust-toolchain@stable
with: {toolchain: "1.70"}
with: {toolchain: "1.77"}
- name: Build and test all crates
run:
cargo test --workspace -vv --features=hdf5-sys/static,hdf5-sys/zlib --exclude=hdf5-derive
Expand Down
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ default-members = ["hdf5", "hdf5-types", "hdf5-derive", "hdf5-sys"]

[workspace.package]
version = "0.8.1" # !V
rust-version = "1.64.0"
rust-version = "1.77.0"
authors = [
"Ivan Smirnov <[email protected]>",
"Magnus Ulimoen <[email protected]>",
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ HDF5 for Rust.
[![Latest Version](https://img.shields.io/crates/v/hdf5.svg)](https://crates.io/crates/hdf5)
[![Documentation](https://docs.rs/hdf5/badge.svg)](https://docs.rs/hdf5)
[![Changelog](https://img.shields.io/github/v/release/aldanor/hdf5-rust)](https://github.com/aldanor/hdf5-rust/blob/master/CHANGELOG.md)
![hdf5: rustc 1.51+](https://img.shields.io/badge/hdf5-rustc_1.51+-lightblue.svg)
![hdf5: rustc 1.77+](https://img.shields.io/badge/hdf5-rustc_1.77+-lightblue.svg)
[![Total Lines](https://tokei.rs/b1/github/aldanor/hdf5-rust)](https://github.com/aldanor/hdf5-rust)
[![Apache 2.0](https://img.shields.io/badge/License-Apache%202.0-blue.svg)](https://opensource.org/licenses/Apache-2.0)
[![MIT](https://img.shields.io/badge/License-MIT-blue.svg)](https://opensource.org/licenses/MIT)
Expand Down Expand Up @@ -112,7 +112,7 @@ toolchains; macOS Catalina).
### Rust

`hdf5` crate is tested continuously for all three official release channels, and
requires a reasonably recent Rust compiler (e.g. of version 1.51 or newer).
requires a reasonably recent Rust compiler (e.g. of version 1.77 or newer).

### HDF5

Expand Down
104 changes: 56 additions & 48 deletions hdf5-sys/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
use std::convert::TryInto;
use std::env;
use std::error::Error;
use std::fmt::{self, Debug, Display};
use std::fmt::{self, Debug};
use std::fs;
use std::os::raw::{c_int, c_uint};
use std::path::{Path, PathBuf};
Expand Down Expand Up @@ -49,6 +49,16 @@ impl Debug for Version {
}
}

fn known_hdf5_versions() -> Vec<Version> {
// Keep up to date with known_versions in hdf5
let mut vs = Vec::new();
vs.extend((5..=21).map(|v| Version::new(1, 8, v))); // 1.8.[5-23]
vs.extend((0..=8).map(|v| Version::new(1, 10, v))); // 1.10.[0-10]
vs.extend((0..=2).map(|v| Version::new(1, 12, v))); // 1.12.[0-2]
vs.extend((0..=4).map(|v| Version::new(1, 14, v))); // 1.14.[0-4]
vs
}

#[allow(dead_code)]
fn run_command(cmd: &str, args: &[&str]) -> Option<String> {
let out = Command::new(cmd).args(args).output();
Expand Down Expand Up @@ -80,17 +90,6 @@ fn is_msvc() -> bool {
std::env::var("CARGO_CFG_TARGET_ENV").unwrap() == "msvc"
}

#[derive(Clone, Debug)]
struct RuntimeError(String);

impl Error for RuntimeError {}

impl Display for RuntimeError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "HDF5 runtime error: {}", self.0)
}
}

#[allow(non_snake_case, non_camel_case_types)]
fn get_runtime_version_single<P: AsRef<Path>>(path: P) -> Result<Version, Box<dyn Error>> {
type H5open_t = unsafe extern "C" fn() -> c_int;
Expand Down Expand Up @@ -634,59 +633,68 @@ pub struct Config {

impl Config {
pub fn emit_link_flags(&self) {
println!("cargo:rustc-link-lib=dylib=hdf5");
println!("cargo::rustc-link-lib=dylib=hdf5");
for dir in &self.link_paths {
println!("cargo:rustc-link-search=native={}", dir.to_str().unwrap());
println!("cargo::rustc-link-search=native={}", dir.to_str().unwrap());
}
println!("cargo:rerun-if-env-changed=HDF5_DIR");
println!("cargo:rerun-if-env-changed=HDF5_VERSION");
println!("cargo::rerun-if-env-changed=HDF5_DIR");
println!("cargo::rerun-if-env-changed=HDF5_VERSION");

if is_msvc() {
println!("cargo:msvc_dll_indirection=1");
println!("cargo::metadata=msvc_dll_indirection=1");
}
println!("cargo:include={}", self.inc_dir.to_str().unwrap());
println!("cargo::metadata=include={}", self.inc_dir.to_str().unwrap());

println!("cargo:library=hdf5");
println!("cargo::metadata=library=hdf5");

if feature_enabled("HL") {
println!("cargo:hl_library=hdf5_hl");
println!("cargo::metadata=hl_library=hdf5_hl");
}
}

pub fn emit_cfg_flags(&self) {
let version = self.header.version;
assert!(version >= Version::new(1, 8, 4), "required HDF5 version: >=1.8.4");
let mut vs: Vec<_> = (5..=21).map(|v| Version::new(1, 8, v)).collect(); // 1.8.[5-23]
vs.extend((0..=8).map(|v| Version::new(1, 10, v))); // 1.10.[0-10]
vs.extend((0..=2).map(|v| Version::new(1, 12, v))); // 1.12.[0-2]
vs.extend((0..=4).map(|v| Version::new(1, 14, v))); // 1.14.[0-4]
for v in vs.into_iter().filter(|&v| version >= v) {
println!("cargo:rustc-cfg=feature=\"{}.{}.{}\"", v.major, v.minor, v.micro);
println!("cargo:version_{}_{}_{}=1", v.major, v.minor, v.micro);

for v in known_hdf5_versions() {
println!(
"cargo::rustc-check-cfg=cfg(feature, values(\"{}.{}.{}\"))",
v.major, v.minor, v.micro
);
}
for v in known_hdf5_versions().into_iter().filter(|&v| version >= v) {
println!("cargo::rustc-cfg=feature=\"{}.{}.{}\"", v.major, v.minor, v.micro);
println!("cargo::metadata=version_{}_{}_{}=1", v.major, v.minor, v.micro);
}

println!("cargo::rustc-check-cfg=cfg(have_stdbool_h)");
if self.header.have_stdbool_h {
println!("cargo:rustc-cfg=have_stdbool_h");
println!("cargo::rustc-cfg=have_stdbool_h");
// there should be no need to export have_stdbool_h downstream
}
println!("cargo::rustc-check-cfg=cfg(feature, values(\"have-direct\"))");
if self.header.have_direct {
println!("cargo:rustc-cfg=feature=\"have-direct\"");
println!("cargo:have_direct=1");
println!("cargo::rustc-cfg=feature=\"have-direct\"");
println!("cargo::metadata=have_direct=1");
}
println!("cargo::rustc-check-cfg=cfg(feature, values(\"have-parallel\"))");
if self.header.have_parallel {
println!("cargo:rustc-cfg=feature=\"have-parallel\"");
println!("cargo:have_parallel=1");
println!("cargo::rustc-cfg=feature=\"have-parallel\"");
println!("cargo::metadata=have_parallel=1");
}
println!("cargo::rustc-check-cfg=cfg(feature, values(\"have-threadsafe\"))");
if self.header.have_threadsafe {
println!("cargo:rustc-cfg=feature=\"have-threadsafe\"");
println!("cargo:have_threadsafe=1");
println!("cargo::rustc-cfg=feature=\"have-threadsafe\"");
println!("cargo::metadata=have_threadsafe=1");
}
println!("cargo::rustc-check-cfg=cfg(feature, values(\"have-filter-deflate\"))");
if self.header.have_filter_deflate {
println!("cargo:rustc-cfg=feature=\"have-filter-deflate\"");
println!("cargo:have_filter_deflate=1");
println!("cargo::rustc-cfg=feature=\"have-filter-deflate\"");
println!("cargo::metadata=have_filter_deflate=1");
}

if cfg!(windows) && version >= Version::new(1, 14, 0) {
println!("cargo:rustc-link-lib=shlwapi");
println!("cargo::rustc-link-lib=shlwapi");
}
}

Expand Down Expand Up @@ -721,31 +729,31 @@ fn main() {
}

fn get_build_and_emit() {
println!("cargo:rerun-if-changed=build.rs");
println!("cargo::rerun-if-changed=build.rs");

if feature_enabled("ZLIB") {
let zlib_lib = env::var("DEP_HDF5SRC_ZLIB").unwrap();
println!("cargo:zlib={}", &zlib_lib);
println!("cargo::metadata=zlib={}", &zlib_lib);
let zlib_lib_header = env::var("DEP_HDF5SRC_ZLIB").unwrap();
println!("cargo:zlib={}", &zlib_lib_header);
println!("cargo:rustc-link-lib=static={}", &zlib_lib);
println!("cargo::metadata=zlib={}", &zlib_lib_header);
println!("cargo::rustc-link-lib=static={}", &zlib_lib);
}

if feature_enabled("HL") {
let hdf5_hl_lib = env::var("DEP_HDF5SRC_HL_LIBRARY").unwrap();
println!("cargo:rustc-link-lib=static={}", &hdf5_hl_lib);
println!("cargo:hl_library={}", &hdf5_hl_lib);
println!("cargo::rustc-link-lib=static={}", &hdf5_hl_lib);
println!("cargo::metadata=hl_library={}", &hdf5_hl_lib);
}

let hdf5_root = env::var("DEP_HDF5SRC_ROOT").unwrap();
println!("cargo:root={}", &hdf5_root);
println!("cargo::metadata=root={}", &hdf5_root);
let hdf5_incdir = env::var("DEP_HDF5SRC_INCLUDE").unwrap();
println!("cargo:include={}", &hdf5_incdir);
println!("cargo::metadata=include={}", &hdf5_incdir);
let hdf5_lib = env::var("DEP_HDF5SRC_LIBRARY").unwrap();
println!("cargo:library={}", &hdf5_lib);
println!("cargo::metadata=library={}", &hdf5_lib);

println!("cargo:rustc-link-search=native={}/lib", &hdf5_root);
println!("cargo:rustc-link-lib=static={}", &hdf5_lib);
println!("cargo::rustc-link-search=native={}/lib", &hdf5_root);
println!("cargo::rustc-link-lib=static={}", &hdf5_lib);

let header = Header::parse(&hdf5_incdir);
let config = Config { header, inc_dir: "".into(), link_paths: Vec::new() };
Expand Down
2 changes: 1 addition & 1 deletion hdf5-sys/src/h5f.rs
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ impl Default for H5F_libver_t {

extern "C" {
#[cfg_attr(
hdf5_1_10_2,
feature = "1.10.2",
deprecated(note = "deprecated in HDF5 1.10.2, use H5Fset_libver_bounds()")
)]
pub fn H5Fset_latest_format(file_id: hid_t, latest_format: hbool_t) -> herr_t;
Expand Down
6 changes: 3 additions & 3 deletions hdf5-sys/src/h5l.rs
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,7 @@ extern "C" {
loc_id: hid_t, name: *const c_char, linfo: *mut H5L_info2_t, lapl_id: hid_t,
) -> herr_t;
#[cfg_attr(
hdf5_1_12_0,
feature = "1.12.0",
deprecated(note = "deprecated in HDF5 1.12.0, use H5Lget_info_by_idx2()")
)]
#[cfg_attr(not(feature = "1.12.0"), link_name = "H5Lget_info_by_idx")]
Expand Down Expand Up @@ -307,7 +307,7 @@ extern "C" {
op: H5L_iterate2_t, op_data: *mut c_void,
) -> herr_t;
#[cfg_attr(
hdf5_1_12_0,
feature = "1.12.0",
deprecated(note = "deprecated in HDF5 1.12.0, use H5Literate_by_name2()")
)]
#[cfg_attr(not(feature = "1.12.0"), link_name = "H5Literate_by_name")]
Expand All @@ -332,7 +332,7 @@ extern "C" {
op_data: *mut c_void,
) -> herr_t;
#[cfg_attr(
hdf5_1_12_0,
feature = "1.12.0",
deprecated(note = "deprecated in HDF5 1.12.0, use H5Lvisit_by_name2()")
)]
#[cfg_attr(not(feature = "1.12.0"), link_name = "H5Lvisit_by_name")]
Expand Down
4 changes: 2 additions & 2 deletions hdf5-sys/src/h5p.rs
Original file line number Diff line number Diff line change
Expand Up @@ -709,14 +709,14 @@ extern "C" {
) -> herr_t;
pub fn H5Pdecode(buf: *const c_void) -> hid_t;
#[cfg_attr(
hdf5_1_10_1,
feature = "1.10.1",
deprecated(note = "deprecated in HDF5 1.10.1, use H5Pset_file_space_strategy()")
)]
pub fn H5Pset_file_space(
plist_id: hid_t, strategy: H5F_file_space_type_t, threshold: hsize_t,
) -> herr_t;
#[cfg_attr(
hdf5_1_10_1,
feature = "1.10.1",
deprecated(note = "deprecated in HDF5 1.10.1, use H5Pget_file_space_strategy()")
)]
pub fn H5Pget_file_space(
Expand Down
5 changes: 3 additions & 2 deletions hdf5-types/build.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
fn main() {
println!("cargo:rerun-if-changed=build.rs");
println!("cargo::rerun-if-changed=build.rs");
println!("cargo::rustc-check-cfg=cfg(windows_dll)");
if std::env::var_os("DEP_HDF5_MSVC_DLL_INDIRECTION").is_some() {
println!("cargo:rustc-cfg=windows_dll");
println!("cargo::rustc-cfg=windows_dll");
}
}
38 changes: 36 additions & 2 deletions hdf5/build.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,42 @@
use std::env;

#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Default)]
pub struct Version {
pub major: u8,
pub minor: u8,
pub micro: u8,
}

impl Version {
pub const fn new(major: u8, minor: u8, micro: u8) -> Self {
Self { major, minor, micro }
}
}

fn known_hdf5_versions() -> Vec<Version> {
// Keep up to date with known_hdf5_versions in hdf5-sys
let mut vs = Vec::new();
vs.extend((5..=21).map(|v| Version::new(1, 8, v))); // 1.8.[5-23]
vs.extend((0..=8).map(|v| Version::new(1, 10, v))); // 1.10.[0-10]
vs.extend((0..=2).map(|v| Version::new(1, 12, v))); // 1.12.[0-2]
vs.extend((0..=4).map(|v| Version::new(1, 14, v))); // 1.14.[0-4]
vs
}

fn main() {
let print_feature = |key: &str| println!("cargo:rustc-cfg=feature=\"{}\"", key);
let print_cfg = |key: &str| println!("cargo:rustc-cfg={}", key);
for version in known_hdf5_versions() {
println!(
"cargo::rustc-check-cfg=cfg(feature, values(\"{}.{}.{}\"))",
version.major, version.minor, version.micro
);
}
for feature in ["have-direct", "have-parallel", "have-threadsafe", "have-filter-deflate"] {
println!("cargo::rustc-check-cfg=cfg(feature, values(\"{feature}\"))");
}
println!("cargo::rustc-check-cfg=cfg(msvc_dll_indirection)");

let print_feature = |key: &str| println!("cargo::rustc-cfg=feature=\"{}\"", key);
let print_cfg = |key: &str| println!("cargo::rustc-cfg={}", key);
for (key, _) in env::vars() {
match key.as_str() {
// public features
Expand Down
2 changes: 1 addition & 1 deletion hdf5/src/hl/location.rs
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ pub enum LocationType {
Dataset,
NamedDatatype,
#[cfg(feature = "1.12.0")]
#[cfg_attr(docrs, doc(cfg(feature = "1.12.0")))]
#[cfg_attr(docsrs, doc(cfg(feature = "1.12.0")))]
TypeMap,
}

Expand Down
Loading