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

fix: cherry pick the fix for download-binaries #10

Merged
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
39 changes: 29 additions & 10 deletions ort-sys/build.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use std::{
env, fs,
env, fs, io,
path::{Path, PathBuf},
process::Command
};
Expand All @@ -12,9 +12,9 @@ const ORT_ENV_SYSTEM_LIB_PROFILE: &str = "ORT_LIB_PROFILE";

const DIST_TABLE: &str = include_str!("dist.txt");

#[path = "src/internal/dirs.rs"]
mod dirs;
use self::dirs::cache_dir;
#[path = "src/internal/mod.rs"]
mod internal;
use self::internal::dirs::cache_dir;

#[cfg(feature = "download-binaries")]
fn fetch_file(source_url: &str) -> Vec<u8> {
Expand Down Expand Up @@ -416,21 +416,40 @@ fn prepare_libort_dir() -> (PathBuf, bool) {

let (prebuilt_url, prebuilt_hash) = dist.unwrap();

let mut cache_dir = cache_dir()
let bin_extract_dir = cache_dir()
.expect("could not determine cache directory")
.join("dfbin")
.join(target)
.join(prebuilt_hash);
if fs::create_dir_all(&cache_dir).is_err() {
cache_dir = env::var("OUT_DIR").unwrap().into();
}

let ort_extract_dir = prebuilt_url.split('/').last().unwrap().strip_suffix(".tgz").unwrap();
let lib_dir = cache_dir.join(ort_extract_dir);
let lib_dir = bin_extract_dir.join(ort_extract_dir);
if !lib_dir.exists() {
let downloaded_file = fetch_file(prebuilt_url);
assert!(verify_file(&downloaded_file, prebuilt_hash), "hash of downloaded ONNX Runtime binary does not match!");
extract_tgz(&downloaded_file, &cache_dir);

let mut temp_extract_dir = bin_extract_dir
.parent()
.unwrap()
.join(format!("tmp.{}_{prebuilt_hash}", self::internal::random_identifier()));
let mut should_rename = true;
if fs::create_dir_all(&temp_extract_dir).is_err() {
temp_extract_dir = env::var("OUT_DIR").unwrap().into();
should_rename = false;
}
extract_tgz(&downloaded_file, &temp_extract_dir);
if should_rename {
match std::fs::rename(&temp_extract_dir, &bin_extract_dir) {
Ok(()) => {}
Err(e) => {
if bin_extract_dir.exists() {
let _ = fs::remove_dir_all(temp_extract_dir);
} else {
panic!("failed to extract downloaded binaries: {e}");
}
}
}
}
}

static_link_prerequisites(true);
Expand Down
16 changes: 14 additions & 2 deletions ort-sys/src/internal/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,16 @@
use std::hash::{BuildHasher, Hasher, RandomState};

pub mod dirs;

#[cfg(feature = "download-binaries")]
include!(concat!(env!("OUT_DIR"), "/downloaded_version.rs"));
pub fn random_identifier() -> String {
let mut state = RandomState::new().build_hasher().finish();
std::iter::repeat_with(move || {
state ^= state << 13;
state ^= state >> 7;
state ^= state << 17;
state
})
.take(12)
.map(|i| b"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"[i as usize % 62] as char)
.collect()
}
3 changes: 3 additions & 0 deletions ort-sys/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@
#[doc(hidden)]
pub mod internal;

#[cfg(feature = "download-binaries")]
include!(concat!(env!("OUT_DIR"), "/downloaded_version.rs"));

pub const ORT_API_VERSION: u32 = 17;

pub use std::ffi::{c_char, c_int, c_ulong, c_ulonglong, c_ushort, c_void};
Expand Down
Loading