build.rs
142 lines · 5766 bytes
1fn main() {
2 // On Windows, help the linker find mpv.lib when building with the mpv feature.
3 //
4 // Priority:
5 // 1. MPV_LIB_DIR env var — CI / custom installs
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
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)
13 #[cfg(all(target_os = "windows", feature = "mpv"))]
14 {
15 println!("cargo:rerun-if-env-changed=MPV_LIB_DIR");
16 println!("cargo:rerun-if-env-changed=LOCALAPPDATA");
17 println!("cargo:rerun-if-env-changed=CARGO_HOME");
18
19 // 1. Explicit env var override (CI / custom installs)
20 if let Ok(dir) = std::env::var("MPV_LIB_DIR") {
21 let dir = std::path::PathBuf::from(dir);
22 println!("cargo:rustc-link-search=native={}", dir.display());
23 copy_dll(&dir);
24 return;
25 }
26
27 // Persistent user-level location — always writable, survives across `cargo install` runs.
28 let persistent_dir = std::env::var("LOCALAPPDATA")
29 .map(|d| {
30 std::path::PathBuf::from(d)
31 .join("youtube-tui")
32 .join("mpv-dev")
33 })
34 .ok();
35
36 // Local project-relative fallback (convenient for development checkouts)
37 let manifest_dir = std::env::var("CARGO_MANIFEST_DIR").unwrap();
38 let local_dir = std::path::Path::new(&manifest_dir).join("mpv-dev");
39
40 // 2 & 3. Check both locations for an existing mpv.lib — fast path
41 for candidate in persistent_dir.iter().chain(std::iter::once(&local_dir)) {
42 let lib = candidate.join("mpv.lib");
43 println!("cargo:rerun-if-changed={}", lib.display());
44 if lib.exists() {
45 println!("cargo:rustc-link-search=native={}", candidate.display());
46 copy_dll(candidate);
47 return;
48 }
49 }
50
51 // 4. Neither location has mpv.lib — run setup-mpv-dev.ps1 to download it.
52 let script = std::path::Path::new(&manifest_dir).join("setup-mpv-dev.ps1");
53 if !script.exists() {
54 panic!(
55 "\n\nERROR: mpv.lib not found and setup-mpv-dev.ps1 is missing.\n\
56 Run the setup script manually:\n\
57 \n .\\setup-mpv-dev.ps1\n\n\
58 Or set MPV_LIB_DIR to a directory containing mpv.lib.\n"
59 );
60 }
61
62 // Prefer the persistent directory; fall back to the local project dir.
63 let output_dir = persistent_dir.unwrap_or(local_dir.clone());
64
65 eprintln!(
66 "cargo:warning=mpv.lib not found — running setup-mpv-dev.ps1 (~30MB download)..."
67 );
68
69 let status = std::process::Command::new("powershell")
70 .args([
71 "-ExecutionPolicy",
72 "Bypass",
73 "-File",
74 &script.to_string_lossy(),
75 "-OutputDir",
76 &output_dir.to_string_lossy(),
77 ])
78 .status()
79 .expect("Failed to launch PowerShell to run setup-mpv-dev.ps1");
80
81 if !status.success() {
82 panic!(
83 "\n\nERROR: setup-mpv-dev.ps1 failed (exit code: {:?}).\n\
84 Run it manually to see the full error:\n\
85 \n .\\setup-mpv-dev.ps1\n\n\
86 Or set MPV_LIB_DIR to a directory containing mpv.lib.\n",
87 status.code()
88 );
89 }
90
91 let mpv_lib = output_dir.join("mpv.lib");
92 if !mpv_lib.exists() {
93 panic!(
94 "\n\nERROR: setup-mpv-dev.ps1 completed but mpv.lib was not found at:\n {}\n\
95 Run the script manually to diagnose:\n\
96 \n .\\setup-mpv-dev.ps1\n",
97 mpv_lib.display()
98 );
99 }
100
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"))]
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 }
141 }
142}