Commit df47e8cfa2
Unsigned
Layout: unified · split
build.rs +63 −11
| @@ -1,20 +1,72 @@ | |||
| 1 | fn main() { | 1 | fn main() { |
| 2 | // On Windows, help the linker find mpv.lib when building with the mpv feature. | 2 | // On Windows, help the linker find mpv.lib when building with the mpv feature. |
| 3 | // Check MPV_LIB_DIR env var, then fall back to a local mpv-dev directory. | 3 | // Priority: MPV_LIB_DIR env var → local mpv-dev/ dir → run setup-mpv-dev.ps1 to create it. |
| 4 | #[cfg(all(target_os = "windows", feature = "mpv"))] | 4 | #[cfg(all(target_os = "windows", feature = "mpv"))] |
| 5 | { | 5 | { |
| 6 | // Tell Cargo to re-run this script only when these change, not on every build. | ||
| 7 | println!("cargo:rerun-if-changed=mpv-dev/mpv.lib"); | ||
| 8 | println!("cargo:rerun-if-env-changed=MPV_LIB_DIR"); | ||
| 9 | |||
| 10 | let manifest_dir = std::env::var("CARGO_MANIFEST_DIR").unwrap(); | ||
| 11 | let local_mpv = std::path::Path::new(&manifest_dir).join("mpv-dev"); | ||
| 12 | let mpv_lib = local_mpv.join("mpv.lib"); | ||
| 13 | |||
| 14 | // 1. Explicit env var override (CI / custom setups) | ||
| 6 | if let Ok(dir) = std::env::var("MPV_LIB_DIR") { | 15 | if let Ok(dir) = std::env::var("MPV_LIB_DIR") { |
| 7 | println!("cargo:rustc-link-search=native={}", dir); | 16 | println!("cargo:rustc-link-search=native={}", dir); |
| 8 | } else { | 17 | return; |
| 9 | // Check for a local mpv-dev directory (created by setup-mpv-dev.ps1) | ||
| 10 | let manifest_dir = std::env::var("CARGO_MANIFEST_DIR").unwrap(); | ||
| 11 | let local_mpv = std::path::Path::new(&manifest_dir).join("mpv-dev"); | ||
| 12 | if local_mpv.exists() { | ||
| 13 | println!( | ||
| 14 | "cargo:rustc-link-search=native={}", | ||
| 15 | local_mpv.display() | ||
| 16 | ); | ||
| 17 | } | ||
| 18 | } | 18 | } |
| 19 | |||
| 20 | // 2. mpv.lib already exists locally from a previous run — fast path | ||
| 21 | if mpv_lib.exists() { | ||
| 22 | println!("cargo:rustc-link-search=native={}", local_mpv.display()); | ||
| 23 | return; | ||
| 24 | } | ||
| 25 | |||
| 26 | // 3. Run setup-mpv-dev.ps1 to download and generate mpv.lib automatically | ||
| 27 | let script = std::path::Path::new(&manifest_dir).join("setup-mpv-dev.ps1"); | ||
| 28 | if !script.exists() { | ||
| 29 | panic!( | ||
| 30 | "\n\nERROR: mpv.lib not found and setup-mpv-dev.ps1 is missing.\n\ | ||
| 31 | Run the setup script manually:\n\ | ||
| 32 | \n .\\setup-mpv-dev.ps1\n\n\ | ||
| 33 | Or set MPV_LIB_DIR to a directory containing mpv.lib.\n" | ||
| 34 | ); | ||
| 35 | } | ||
| 36 | |||
| 37 | eprintln!( | ||
| 38 | "cargo:warning=mpv.lib not found — running setup-mpv-dev.ps1 to download it (~30MB)..." | ||
| 39 | ); | ||
| 40 | |||
| 41 | let status = std::process::Command::new("powershell") | ||
| 42 | .args([ | ||
| 43 | "-ExecutionPolicy", | ||
| 44 | "Bypass", | ||
| 45 | "-File", | ||
| 46 | &script.to_string_lossy(), | ||
| 47 | ]) | ||
| 48 | .status() | ||
| 49 | .expect("Failed to launch PowerShell to run setup-mpv-dev.ps1"); | ||
| 50 | |||
| 51 | if !status.success() { | ||
| 52 | panic!( | ||
| 53 | "\n\nERROR: setup-mpv-dev.ps1 failed (exit code: {:?}).\n\ | ||
| 54 | Run it manually to see the full error:\n\ | ||
| 55 | \n .\\setup-mpv-dev.ps1\n\n\ | ||
| 56 | Or set MPV_LIB_DIR to a directory containing mpv.lib.\n", | ||
| 57 | status.code() | ||
| 58 | ); | ||
| 59 | } | ||
| 60 | |||
| 61 | if !mpv_lib.exists() { | ||
| 62 | panic!( | ||
| 63 | "\n\nERROR: setup-mpv-dev.ps1 completed but mpv.lib was not found at:\n {}\n\ | ||
| 64 | Run the script manually to diagnose:\n\ | ||
| 65 | \n .\\setup-mpv-dev.ps1\n", | ||
| 66 | mpv_lib.display() | ||
| 67 | ); | ||
| 68 | } | ||
| 69 | |||
| 70 | println!("cargo:rustc-link-search=native={}", local_mpv.display()); | ||
| 19 | } | 71 | } |
| 20 | } | 72 | } |