This repository was archived by the owner on Oct 31, 2026. It is now read-only.
Repository navigation
Expand file tree
/
Copy pathbuild.rs
More file actions
112 lines (102 loc) · 3.65 KB
/
Copy pathbuild.rs
File metadata and controls
112 lines (102 loc) · 3.65 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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
use sha2::{Digest, Sha256};
use std::fs;
use std::path::{Path, PathBuf};
use std::{env, process};
/// Release of this repository holding the archives listed in `SHA256SUMS`.
const ARCHIVE_TAG: &str = "v1.23.7-2";
const CHECKSUMS: &str = include_str!("SHA256SUMS");
const MAX_ARCHIVE_SIZE: u64 = 256 * 1024 * 1024;
struct Platform {
triplet: &'static str,
/// In link order: libheif needs the decoders, which come after it.
libs: [&'static str; 3],
cpp: Option<&'static str>,
}
fn platform(target: &str) -> Option<Platform> {
let unix = |triplet, cpp| Platform {
triplet,
libs: ["heif", "de265", "dav1d"],
cpp: Some(cpp),
};
match target {
"x86_64-unknown-linux-gnu" => Some(unix("x64-linux", "stdc++")),
"aarch64-unknown-linux-gnu" => Some(unix("arm64-linux", "stdc++")),
"aarch64-apple-darwin" => Some(unix("arm64-osx", "c++")),
"x86_64-pc-windows-msvc" => Some(Platform {
triplet: "x64-windows-static-md",
libs: ["heif", "libde265", "dav1d"],
cpp: None,
}),
_ => None,
}
}
fn main() {
println!("cargo:rerun-if-changed=build.rs");
println!("cargo:rerun-if-changed=SHA256SUMS");
println!("cargo:rerun-if-env-changed=LIBHEIF_STATIC_DIR");
if env::var_os("DOCS_RS").is_some() {
return;
}
let target = env::var("TARGET").unwrap();
let Some(platform) = platform(&target) else {
eprintln!("no prebuilt libheif for {target}");
process::exit(1);
};
let root = match env::var_os("LIBHEIF_STATIC_DIR") {
Some(dir) => PathBuf::from(dir),
None => fetch(&target, &PathBuf::from(env::var_os("OUT_DIR").unwrap())),
};
let lib_dir = root.join("installed").join(platform.triplet).join("lib");
println!("cargo:rustc-link-search=native={}", lib_dir.display());
for lib in platform.libs {
println!("cargo:rustc-link-lib=static={lib}");
}
if let Some(cpp) = platform.cpp {
println!("cargo:rustc-link-lib=dylib={cpp}");
}
}
/// Downloads and unpacks the target's archive, returning its root directory.
fn fetch(target: &str, out_dir: &Path) -> PathBuf {
let name = format!("libheif-static-{target}");
let root = out_dir.join(&name);
let archive = format!("{name}.tar.gz");
let expected = CHECKSUMS
.lines()
.find_map(|line| line.strip_suffix(&format!(" {archive}")))
.unwrap_or_else(|| panic!("{archive} is not listed in SHA256SUMS"));
let url = format!(
"https://github.com/kmworks/libheif-static/releases/download/{ARCHIVE_TAG}/{archive}"
);
let bytes = ureq::get(&url)
.call()
.and_then(|mut response| {
response
.body_mut()
.with_config()
.limit(MAX_ARCHIVE_SIZE)
.read_to_vec()
})
.unwrap_or_else(|e| {
panic!("downloading {url} failed: {e}; set LIBHEIF_STATIC_DIR to build offline")
});
let actual: String = Sha256::digest(&bytes)
.iter()
.map(|byte| format!("{byte:02x}"))
.collect();
assert_eq!(actual, expected, "checksum mismatch for {archive}");
if root.exists() {
fs::remove_dir_all(&root).unwrap();
}
let mut entries = tar::Archive::new(flate2::read::GzDecoder::new(&bytes[..]));
for entry in entries.entries().unwrap() {
let mut entry = entry.unwrap();
let path = entry.path().unwrap();
let is_library = path
.extension()
.is_some_and(|extension| extension == "a" || extension == "lib");
if is_library {
entry.unpack_in(out_dir).unwrap();
}
}
root
}