|
| 1 | +use std::env; |
| 2 | +use std::fs; |
| 3 | +use std::path::PathBuf; |
| 4 | +use std::process::Command; |
| 5 | + |
| 6 | +fn main() { |
| 7 | + if env::var("CARGO_FEATURE_CRONET_BACKEND").is_err() { |
| 8 | + return; |
| 9 | + } |
| 10 | + |
| 11 | + let out_dir = PathBuf::from(env::var("OUT_DIR").unwrap()); |
| 12 | + let target_os = env::var("CARGO_CFG_TARGET_OS").unwrap(); |
| 13 | + let target_arch = env::var("CARGO_CFG_TARGET_ARCH").unwrap(); |
| 14 | + |
| 15 | + let os = match target_os.as_str() { |
| 16 | + "linux" => "linux", |
| 17 | + "macos" => "mac", |
| 18 | + "windows" => "win", |
| 19 | + _ => return, |
| 20 | + }; |
| 21 | + |
| 22 | + let arch = match target_arch.as_str() { |
| 23 | + "x86_64" => "x64", |
| 24 | + "aarch64" => "arm64", |
| 25 | + "x86" => "x86", |
| 26 | + "arm" => "arm", |
| 27 | + _ => return, |
| 28 | + }; |
| 29 | + |
| 30 | + let version = "119.0.6045.66"; |
| 31 | + let ext = if os == "win" { "zip" } else { "tar.xz" }; |
| 32 | + let filename = format!("cronet-v{}-{}-{}.{}", version, os, arch, ext); |
| 33 | + let folder_name = format!("cronet-v{}-{}-{}", version, os, arch); |
| 34 | + |
| 35 | + let url = format!( |
| 36 | + "https://github.com/sleeyax/cronet-binaries/releases/download/v{}/{}", |
| 37 | + version, filename |
| 38 | + ); |
| 39 | + |
| 40 | + let archive_path = out_dir.join(&filename); |
| 41 | + let extract_dir = out_dir.join(&folder_name); |
| 42 | + |
| 43 | + if !extract_dir.exists() { |
| 44 | + println!("cargo:warning=Downloading cronet binaries from {}...", url); |
| 45 | + let status = Command::new("curl") |
| 46 | + .arg("-L") |
| 47 | + .arg("-o") |
| 48 | + .arg(&archive_path) |
| 49 | + .arg(&url) |
| 50 | + .status() |
| 51 | + .expect("Failed to execute curl to download cronet binaries"); |
| 52 | + |
| 53 | + if !status.success() { |
| 54 | + panic!("Failed to download cronet binaries"); |
| 55 | + } |
| 56 | + |
| 57 | + println!("cargo:warning=Extracting {}...", filename); |
| 58 | + if os == "win" { |
| 59 | + Command::new("unzip") |
| 60 | + .arg(&archive_path) |
| 61 | + .arg("-d") |
| 62 | + .arg(&out_dir) |
| 63 | + .status() |
| 64 | + .expect("Failed to execute unzip"); |
| 65 | + } else { |
| 66 | + Command::new("tar") |
| 67 | + .arg("-xf") |
| 68 | + .arg(&archive_path) |
| 69 | + .arg("-C") |
| 70 | + .arg(&out_dir) |
| 71 | + .status() |
| 72 | + .expect("Failed to execute tar"); |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + println!("cargo:rustc-link-search=native={}", extract_dir.display()); |
| 77 | + |
| 78 | + // Add rpath so the binary can find the shared library at runtime |
| 79 | + if os == "linux" || os == "mac" { |
| 80 | + println!("cargo:rustc-link-arg=-Wl,-rpath,{}", extract_dir.display()); |
| 81 | + } |
| 82 | + |
| 83 | + // Tell cargo to rebuild if this script changes |
| 84 | + println!("cargo:rerun-if-changed=build.rs"); |
| 85 | +} |
0 commit comments