Commit bd8311e940
Unsigned
Layout: unified · split
build.rs +52 −7
| @@ -2,23 +2,29 @@ fn main() { | ||
| 2 | 2 | // On Windows, help the linker find mpv.lib when building with the mpv feature. |
| 3 | 3 | // |
| 4 | 4 | // Priority: |
| 5 | // 1. MPV_LIB_DIR env var — CI / custom installs | |
| 5 | // 1. MPV_LIB_DIR env var — CI / custom installs | |
| 6 | 6 | // 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) | |
| 9 | 13 | #[cfg(all(target_os = "windows", feature = "mpv"))] |
| 10 | 14 | { |
| 11 | 15 | println!("cargo:rerun-if-env-changed=MPV_LIB_DIR"); |
| 12 | 16 | println!("cargo:rerun-if-env-changed=LOCALAPPDATA"); |
| 17 | println!("cargo:rerun-if-env-changed=CARGO_HOME"); | |
| 13 | 18 | |
| 14 | 19 | // 1. Explicit env var override (CI / custom installs) |
| 15 | 20 | 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); | |
| 17 | 24 | return; |
| 18 | 25 | } |
| 19 | 26 | |
| 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. | |
| 22 | 28 | let persistent_dir = std::env::var("LOCALAPPDATA") |
| 23 | 29 | .map(|d| { |
| 24 | 30 | std::path::PathBuf::from(d) |
| @@ -37,12 +43,12 @@ fn main() { | ||
| 37 | 43 | println!("cargo:rerun-if-changed={}", lib.display()); |
| 38 | 44 | if lib.exists() { |
| 39 | 45 | println!("cargo:rustc-link-search=native={}", candidate.display()); |
| 46 | copy_dll(candidate); | |
| 40 | 47 | return; |
| 41 | 48 | } |
| 42 | 49 | } |
| 43 | 50 | |
| 44 | 51 | // 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. | |
| 46 | 52 | let script = std::path::Path::new(&manifest_dir).join("setup-mpv-dev.ps1"); |
| 47 | 53 | if !script.exists() { |
| 48 | 54 | panic!( |
| @@ -93,5 +99,44 @@ fn main() { | ||
| 93 | 99 | } |
| 94 | 100 | |
| 95 | 101 | 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"))] | |
| 113 | fn 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 | } | |
| 96 | 141 | } |
| 97 | 142 | } |