Skip to content

Commit fa637d0

Browse files
committed
Refactor binary download logic in build.rs to check for existing SHERPA_LIB_PATH before downloading. This change ensures that libraries are only fetched if the environment variable is not set, improving efficiency and preventing unnecessary downloads. Additionally, fallback handling for manual builds is maintained.
1 parent 8bb029e commit fa637d0

File tree

1 file changed

+59
-57
lines changed

1 file changed

+59
-57
lines changed

crates/sherpa-rs-sys/build.rs

Lines changed: 59 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -360,68 +360,70 @@ fn main() {
360360

361361
#[cfg(feature = "download-binaries")]
362362
{
363-
// Download libraries, cache and set SHERPA_LIB_PATH
364-
use download_binaries::{extract_tbz, fetch_file, get_cache_dir, sha256, DIST_TABLE};
365-
debug_log!("Download binaries enabled");
366-
// debug_log!("Dist table: {:?}", DIST_TABLE.targets);
367-
// Try download sherpa libs and set SHERPA_LIB_PATH
368-
if let Some(dist) = DIST_TABLE.get(&target, &mut is_dynamic) {
369-
debug_log!("is_dynamic after: {}", is_dynamic);
370-
optional_dist = Some(dist.clone());
371-
let mut cache_dir = if let Some(dir) = get_cache_dir() {
372-
dir.join(target.clone()).join(&dist.checksum)
373-
} else {
374-
println!("cargo:warning=Could not determine cache directory, using OUT_DIR");
375-
PathBuf::from(env::var("OUT_DIR").unwrap())
376-
};
377-
if fs::create_dir_all(&cache_dir).is_err() {
378-
println!("cargo:warning=Could not create cache directory, using OUT_DIR");
379-
cache_dir = env::var("OUT_DIR").unwrap().into();
380-
}
381-
debug_log!("Cache dir: {}", cache_dir.display());
382-
383-
let lib_dir = cache_dir.join(&dist.name);
384-
385-
// if is mobile then check if cache dir not empty
386-
// Sherpa uses special directory structure for mobile
387-
let cache_dir_empty = cache_dir
388-
.read_dir()
389-
.map(|mut entries| entries.next().is_none())
390-
.unwrap_or(true);
391-
392-
if (is_mobile && cache_dir_empty) || (!is_mobile && !lib_dir.exists()) {
393-
let downloaded_file = fetch_file(&dist.url);
394-
let hash = sha256(&downloaded_file);
395-
verify_checksum(&hash, &dist.checksum);
396-
extract_tbz(&downloaded_file, &cache_dir);
397-
} else {
398-
debug_log!("Skip fetch file. Using cache from {}", lib_dir.display());
399-
}
400-
401-
// In Android, we need to set SHERPA_LIB_PATH to the cache directory sincie it has jniLibs
402-
if is_mobile {
403-
env::set_var("SHERPA_LIB_PATH", &cache_dir);
404-
} else {
405-
env::set_var("SHERPA_LIB_PATH", cache_dir.join(&dist.name));
406-
}
363+
if let Err(_) = env::var("SHERPA_LIB_PATH") {
364+
// Download libraries, cache and set SHERPA_LIB_PATH
365+
use download_binaries::{extract_tbz, fetch_file, get_cache_dir, sha256, DIST_TABLE};
366+
debug_log!("Download binaries enabled");
367+
// debug_log!("Dist table: {:?}", DIST_TABLE.targets);
368+
// Try download sherpa libs and set SHERPA_LIB_PATH
369+
if let Some(dist) = DIST_TABLE.get(&target, &mut is_dynamic) {
370+
debug_log!("is_dynamic after: {}", is_dynamic);
371+
optional_dist = Some(dist.clone());
372+
let mut cache_dir = if let Some(dir) = get_cache_dir() {
373+
dir.join(target.clone()).join(&dist.checksum)
374+
} else {
375+
println!("cargo:warning=Could not determine cache directory, using OUT_DIR");
376+
PathBuf::from(env::var("OUT_DIR").unwrap())
377+
};
378+
if fs::create_dir_all(&cache_dir).is_err() {
379+
println!("cargo:warning=Could not create cache directory, using OUT_DIR");
380+
cache_dir = env::var("OUT_DIR").unwrap().into();
381+
}
382+
debug_log!("Cache dir: {}", cache_dir.display());
383+
384+
let lib_dir = cache_dir.join(&dist.name);
385+
386+
// if is mobile then check if cache dir not empty
387+
// Sherpa uses special directory structure for mobile
388+
let cache_dir_empty = cache_dir
389+
.read_dir()
390+
.map(|mut entries| entries.next().is_none())
391+
.unwrap_or(true);
392+
393+
if (is_mobile && cache_dir_empty) || (!is_mobile && !lib_dir.exists()) {
394+
let downloaded_file = fetch_file(&dist.url);
395+
let hash = sha256(&downloaded_file);
396+
verify_checksum(&hash, &dist.checksum);
397+
extract_tbz(&downloaded_file, &cache_dir);
398+
} else {
399+
debug_log!("Skip fetch file. Using cache from {}", lib_dir.display());
400+
}
407401

408-
debug_log!("dist libs: {:?}", dist.libs);
409-
if let Some(libs) = dist.libs {
410-
for lib in libs.iter() {
411-
let lib_path = cache_dir.join(lib);
412-
let lib_parent = lib_path.parent().unwrap();
413-
add_search_path(lib_parent);
402+
// In Android, we need to set SHERPA_LIB_PATH to the cache directory sincie it has jniLibs
403+
if is_mobile {
404+
env::set_var("SHERPA_LIB_PATH", &cache_dir);
405+
} else {
406+
env::set_var("SHERPA_LIB_PATH", cache_dir.join(&dist.name));
414407
}
415408

416-
sherpa_libs = libs
417-
.iter()
418-
.map(download_binaries::extract_lib_name)
419-
.collect();
409+
debug_log!("dist libs: {:?}", dist.libs);
410+
if let Some(libs) = dist.libs {
411+
for lib in libs.iter() {
412+
let lib_path = cache_dir.join(lib);
413+
let lib_parent = lib_path.parent().unwrap();
414+
add_search_path(lib_parent);
415+
}
416+
417+
sherpa_libs = libs
418+
.iter()
419+
.map(download_binaries::extract_lib_name)
420+
.collect();
421+
} else {
422+
sherpa_libs = extract_lib_names(&lib_dir, is_dynamic, &target_os);
423+
}
420424
} else {
421-
sherpa_libs = extract_lib_names(&lib_dir, is_dynamic, &target_os);
425+
println!("cargo:warning=Failed to download binaries. fallback to manual build.");
422426
}
423-
} else {
424-
println!("cargo:warning=Failed to download binaries. fallback to manual build.");
425427
}
426428
}
427429

0 commit comments

Comments
 (0)