Commit df47e8cfa2

df47e8cfa292472e2bca16bbe72edcf3f3d8b041

parent: 06f7a85807

Unsigned

Azin Sharaf <azin.sharaf@icloud.com> · 2026-04-27 22:31 UTC

build(windows): auto-download mpv dev library via build.rs

On Windows with the mpv feature, build.rs now automatically invokes
setup-mpv-dev.ps1 if mpv.lib is not already present, so a plain
\cargo build --release\ works without any manual setup steps.

Priority order:
1. MPV_LIB_DIR env var (CI / custom installs)
2. mpv-dev/mpv.lib already on disk (fast path on subsequent builds)
3. Run setup-mpv-dev.ps1 to download and generate mpv.lib (~30MB)
4. Fail the build with a clear error message if the script fails

rerun-if-changed directives ensure the check is skipped on every
subsequent build once mpv.lib exists.

Layout: unified · split

build.rs +63 −11
@@ -1,20 +1,72 @@
11fn main() {
22 // 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.
44 #[cfg(all(target_os = "windows", feature = "mpv"))]
55 {
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)
615 if let Ok(dir) = std::env::var("MPV_LIB_DIR") {
716 println!("cargo:rustc-link-search=native={}", dir);
8 } else {
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 }
17 return;
1818 }
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());
1971 }
2072}