Commit bd8311e940

bd8311e940a4bdd99dec395d4c07b7f0cc145968

parent: 095d07cbe9

Unsigned

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

build(windows): auto-copy libmpv-2.dll next to the binary at build time

The linker could find mpv.lib but the runtime loader couldn't find
libmpv-2.dll because it wasn't placed next to the exe.

build.rs now copies libmpv-2.dll after locating the mpv-dev directory:
- %CARGO_HOME%\bin\        for \cargo install\ (where the exe lands)
- target/<profile>/        for \cargo build\  (derived from OUT_DIR)

Both copies are attempted silently; failure to copy is non-fatal so
the build still succeeds if the user provides the DLL another way
(e.g. adding the mpv-dev dir to PATH).

Layout: unified · split

build.rs +52 −7
@@ -2,23 +2,29 @@ fn main() {
22 // On Windows, help the linker find mpv.lib when building with the mpv feature.
33 //
44 // Priority:
5 // 1. MPV_LIB_DIR env var — CI / custom installs
5 // 1. MPV_LIB_DIR env var — CI / custom installs
66 // 2. %LOCALAPPDATA%\youtube-tui\mpv-dev — persistent user location (survives cargo install)
7 // 3. <manifest>/mpv-dev — local project checkout
8 // 4. Run setup-mpv-dev.ps1 — download and generate mpv.lib on first build
7 // 3. <manifest>/mpv-dev — local project checkout
8 // 4. Run setup-mpv-dev.ps1 — download and generate mpv.lib on first build
9 //
10 // After locating mpv.lib, libmpv-2.dll is copied to both:
11 // - %CARGO_HOME%\bin\ (where cargo install places the exe)
12 // - target\release\ (where cargo build places the exe)
913 #[cfg(all(target_os = "windows", feature = "mpv"))]
1014 {
1115 println!("cargo:rerun-if-env-changed=MPV_LIB_DIR");
1216 println!("cargo:rerun-if-env-changed=LOCALAPPDATA");
17 println!("cargo:rerun-if-env-changed=CARGO_HOME");
1318
1419 // 1. Explicit env var override (CI / custom installs)
1520 if let Ok(dir) = std::env::var("MPV_LIB_DIR") {
16 println!("cargo:rustc-link-search=native={}", dir);
21 let dir = std::path::PathBuf::from(dir);
22 println!("cargo:rustc-link-search=native={}", dir.display());
23 copy_dll(&dir);
1724 return;
1825 }
1926
20 // Persistent user-level location — works for both `cargo build` and `cargo install`.
21 // %LOCALAPPDATA%\youtube-tui\mpv-dev is always writable and survives across installs.
27 // Persistent user-level location — always writable, survives across `cargo install` runs.
2228 let persistent_dir = std::env::var("LOCALAPPDATA")
2329 .map(|d| {
2430 std::path::PathBuf::from(d)
@@ -37,12 +43,12 @@ fn main() {
3743 println!("cargo:rerun-if-changed={}", lib.display());
3844 if lib.exists() {
3945 println!("cargo:rustc-link-search=native={}", candidate.display());
46 copy_dll(candidate);
4047 return;
4148 }
4249 }
4350
4451 // 4. Neither location has mpv.lib — run setup-mpv-dev.ps1 to download it.
45 // Output goes to the persistent user location so subsequent installs are instant.
4652 let script = std::path::Path::new(&manifest_dir).join("setup-mpv-dev.ps1");
4753 if !script.exists() {
4854 panic!(
@@ -93,5 +99,44 @@ fn main() {
9399 }
94100
95101 println!("cargo:rustc-link-search=native={}", output_dir.display());
102 copy_dll(&output_dir);
103 }
104}
105
106/// Copy libmpv-2.dll next to the final binary so it can be found at runtime.
107///
108/// Two destinations are tried:
109/// 1. %CARGO_HOME%\bin\ — where `cargo install` places the exe
110/// 2. The `target/<profile>/` directory — where `cargo build` places the exe,
111/// derived by walking up from OUT_DIR
112#[cfg(all(target_os = "windows", feature = "mpv"))]
113fn copy_dll(mpv_dir: &std::path::Path) {
114 let dll = mpv_dir.join("libmpv-2.dll");
115 if !dll.exists() {
116 // DLL not present in the mpv-dev dir — nothing to copy.
117 return;
118 }
119
120 // 1. cargo install destination: %CARGO_HOME%\bin\
121 if let Ok(cargo_home) = std::env::var("CARGO_HOME") {
122 let dest = std::path::Path::new(&cargo_home)
123 .join("bin")
124 .join("libmpv-2.dll");
125 let _ = std::fs::copy(&dll, &dest);
126 }
127
128 // 2. cargo build destination: OUT_DIR is something like
129 // <root>/target/<profile>/build/<crate>/out
130 // Walking up 3 levels reaches <root>/target/<profile>/
131 if let Ok(out_dir) = std::env::var("OUT_DIR") {
132 let target_profile = std::path::Path::new(&out_dir)
133 .parent() // out
134 .and_then(|p| p.parent()) // <crate>
135 .and_then(|p| p.parent()) // build
136 .and_then(|p| p.parent()); // <profile> (e.g. release / debug)
137 if let Some(dir) = target_profile {
138 let dest = dir.join("libmpv-2.dll");
139 let _ = std::fs::copy(&dll, dest);
140 }
96141 }
97142}