-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbuild.rs
More file actions
97 lines (84 loc) · 2.83 KB
/
Copy pathbuild.rs
File metadata and controls
97 lines (84 loc) · 2.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
use std::{
env, fs,
path::{Path, PathBuf},
process::Command,
};
fn main() {
let manifest_dir =
PathBuf::from(env::var("CARGO_MANIFEST_DIR").expect("CARGO_MANIFEST_DIR missing"));
build_hdf5_zstd_filter(&manifest_dir);
if let Some(git_dir) = resolve_git_dir(&manifest_dir) {
emit_git_rerun_hints(&git_dir);
}
let fallback = env::var("CARGO_PKG_VERSION").expect("CARGO_PKG_VERSION missing");
let full_version = git_describe(
&manifest_dir,
&[
"describe",
"--always",
"--dirty=-modified",
"--tags",
"--abbrev=4",
],
)
.unwrap_or_else(|| fallback.clone());
let short_version =
git_describe(&manifest_dir, &["describe", "--tags", "--abbrev=0"]).unwrap_or(fallback);
println!("cargo:rustc-env=H5V_GIT_VERSION={full_version}");
println!("cargo:rustc-env=H5V_GIT_VERSION_SHORT={short_version}");
}
fn build_hdf5_zstd_filter(manifest_dir: &Path) {
let source = manifest_dir.join("src/hdf5_zstd_filter.c");
println!("cargo:rerun-if-changed={}", source.display());
let hdf5_include = env::var("DEP_HDF5_INCLUDE").expect("DEP_HDF5_INCLUDE missing");
let zstd_include = env::var("DEP_ZSTD_INCLUDE").expect("DEP_ZSTD_INCLUDE missing");
let mut build = cc::Build::new();
build.file(source).include(hdf5_include);
for include in zstd_include.split(';').filter(|path| !path.is_empty()) {
build.include(include);
}
build.compile("h5v_hdf5_zstd_filter");
}
fn git_describe(manifest_dir: &Path, args: &[&str]) -> Option<String> {
let output = Command::new("git")
.args(args)
.current_dir(manifest_dir)
.output()
.ok()?;
if !output.status.success() {
return None;
}
let version = String::from_utf8(output.stdout).ok()?;
let version = version.trim();
(!version.is_empty()).then(|| version.to_string())
}
fn resolve_git_dir(manifest_dir: &Path) -> Option<PathBuf> {
let git_path = manifest_dir.join(".git");
if git_path.is_dir() {
return Some(git_path);
}
let gitdir = fs::read_to_string(git_path).ok()?;
let gitdir = gitdir.trim().strip_prefix("gitdir: ")?;
let gitdir = Path::new(gitdir);
Some(if gitdir.is_absolute() {
gitdir.to_path_buf()
} else {
manifest_dir.join(gitdir)
})
}
fn emit_git_rerun_hints(git_dir: &Path) {
for path in ["HEAD", "index", "packed-refs"] {
println!("cargo:rerun-if-changed={}", git_dir.join(path).display());
}
let head_path = git_dir.join("HEAD");
let Ok(head) = fs::read_to_string(head_path) else {
return;
};
let Some(reference) = head.trim().strip_prefix("ref: ") else {
return;
};
println!(
"cargo:rerun-if-changed={}",
git_dir.join(reference).display()
);
}