|
| 1 | +use std::env; |
| 2 | +use std::fs; |
| 3 | +use std::path::{Path, PathBuf}; |
| 4 | + |
| 5 | +fn cargo_home() -> PathBuf { |
| 6 | + if let Some(home) = env::var_os("CARGO_HOME") { |
| 7 | + return PathBuf::from(home); |
| 8 | + } |
| 9 | + |
| 10 | + let home = env::var_os("HOME").expect("HOME must be set when CARGO_HOME is unset"); |
| 11 | + PathBuf::from(home).join(".cargo") |
| 12 | +} |
| 13 | + |
| 14 | +fn read_v8_version(lock_path: &Path) -> String { |
| 15 | + let lock = fs::read_to_string(lock_path) |
| 16 | + .unwrap_or_else(|error| panic!("failed to read {}: {}", lock_path.display(), error)); |
| 17 | + |
| 18 | + let mut in_v8_package = false; |
| 19 | + for line in lock.lines() { |
| 20 | + match line.trim() { |
| 21 | + "[[package]]" => in_v8_package = false, |
| 22 | + "name = \"v8\"" => in_v8_package = true, |
| 23 | + _ if in_v8_package && line.trim_start().starts_with("version = \"") => { |
| 24 | + let version = line |
| 25 | + .trim() |
| 26 | + .trim_start_matches("version = \"") |
| 27 | + .trim_end_matches('"'); |
| 28 | + return version.to_owned(); |
| 29 | + } |
| 30 | + _ => {} |
| 31 | + } |
| 32 | + } |
| 33 | + |
| 34 | + panic!("failed to locate v8 version in {}", lock_path.display()); |
| 35 | +} |
| 36 | + |
| 37 | +fn find_v8_icu_data(v8_version: &str) -> PathBuf { |
| 38 | + let registry_src = cargo_home().join("registry").join("src"); |
| 39 | + let candidates = [ |
| 40 | + Path::new("third_party/icu/common/icudtl.dat"), |
| 41 | + Path::new("third_party/icu/flutter_desktop/icudtl.dat"), |
| 42 | + Path::new("third_party/icu/chromecast_video/icudtl.dat"), |
| 43 | + ]; |
| 44 | + |
| 45 | + let entries = fs::read_dir(®istry_src).unwrap_or_else(|error| { |
| 46 | + panic!("failed to read cargo registry src {}: {}", registry_src.display(), error) |
| 47 | + }); |
| 48 | + |
| 49 | + for entry in entries { |
| 50 | + let entry = entry.unwrap_or_else(|error| panic!("failed to inspect cargo registry entry: {}", error)); |
| 51 | + let crate_root = entry.path().join(format!("v8-{}", v8_version)); |
| 52 | + for relative in candidates { |
| 53 | + let candidate = crate_root.join(relative); |
| 54 | + if candidate.exists() { |
| 55 | + return candidate; |
| 56 | + } |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + panic!( |
| 61 | + "failed to locate ICU data for v8-{} under {}", |
| 62 | + v8_version, |
| 63 | + registry_src.display(), |
| 64 | + ); |
| 65 | +} |
| 66 | + |
| 67 | +fn main() { |
| 68 | + let manifest_dir = PathBuf::from(env::var_os("CARGO_MANIFEST_DIR").expect("CARGO_MANIFEST_DIR must be set")); |
| 69 | + let lock_path = manifest_dir.join("Cargo.lock"); |
| 70 | + let out_dir = PathBuf::from(env::var_os("OUT_DIR").expect("OUT_DIR must be set")); |
| 71 | + |
| 72 | + println!("cargo:rerun-if-changed={}", lock_path.display()); |
| 73 | + println!("cargo:rerun-if-changed=build.rs"); |
| 74 | + |
| 75 | + let v8_version = read_v8_version(&lock_path); |
| 76 | + let icu_data = find_v8_icu_data(&v8_version); |
| 77 | + let dest_path = out_dir.join("icudtl.dat"); |
| 78 | + |
| 79 | + fs::copy(&icu_data, &dest_path).unwrap_or_else(|error| { |
| 80 | + panic!( |
| 81 | + "failed to copy ICU data from {} to {}: {}", |
| 82 | + icu_data.display(), |
| 83 | + dest_path.display(), |
| 84 | + error, |
| 85 | + ) |
| 86 | + }); |
| 87 | +} |
0 commit comments