setup-mpv-dev.ps1

84c4617332578aaf4e9ee68a0a2d7d6258e48243
youtube-tui/setup-mpv-dev.ps1 history · blame · raw

147 lines · 5521 bytes

  1# Downloads mpv dev library for Windows (required to build with the `mpv` feature)
  2#
  3# Usage:
  4#   .\setup-mpv-dev.ps1                          # outputs to .\mpv-dev\
  5#   .\setup-mpv-dev.ps1 -OutputDir "C:\some\dir" # outputs to a custom directory
  6#
  7# When called from build.rs, -OutputDir is set to %LOCALAPPDATA%\youtube-tui\mpv-dev
  8# so the files persist across `cargo install` invocations.
  9
 10param(
 11    [string]$OutputDir = ""
 12)
 13
 14$ErrorActionPreference = "Stop"
 15
 16# Determine output directory:
 17#   - Explicit -OutputDir argument (used by build.rs)
 18#   - Otherwise default to a sibling mpv-dev\ next to this script
 19if ($OutputDir -eq "") {
 20    $OutputDir = Join-Path $PSScriptRoot "mpv-dev"
 21}
 22
 23$mpvLib = Join-Path $OutputDir "mpv.lib"
 24
 25if (Test-Path $mpvLib) {
 26    Write-Host "mpv.lib already exists at $OutputDir, skipping download."
 27    $env:MPV_LIB_DIR = $OutputDir
 28    Write-Host "MPV_LIB_DIR set to $OutputDir"
 29    return
 30}
 31
 32# Detect architecture
 33$arch = if ([Environment]::Is64BitOperatingSystem) { "x86_64" } else { "i686" }
 34$url = "https://github.com/shinchiro/mpv-winbuild-cmake/releases/download/20260412/mpv-dev-${arch}-20260412-git-062f4bf.7z"
 35
 36# Use the user-local temp directory (always writable, unlike C:\Windows\Temp)
 37$userTemp = [System.IO.Path]::GetTempPath()
 38$tempFile = Join-Path $userTemp "mpv-dev.7z"
 39$tempExtract = Join-Path $userTemp "mpv-dev-extract"
 40
 41Write-Host "Downloading mpv-dev for $arch..."
 42Invoke-WebRequest -Uri $url -OutFile $tempFile -UserAgent "Mozilla/5.0"
 43
 44# Create output directory
 45New-Item -ItemType Directory -Force -Path $OutputDir | Out-Null
 46
 47# Locate 7z — check PATH first, then common install locations
 48$7zPaths = @(
 49    "7z",
 50    "$env:ProgramFiles\7-Zip\7z.exe",
 51    "$env:LOCALAPPDATA\Programs\7-Zip\7z.exe",
 52    "$env:USERPROFILE\scoop\apps\7zip\current\7z.exe"
 53)
 54
 55$7zExe = $null
 56foreach ($p in $7zPaths) {
 57    if (Get-Command $p -ErrorAction SilentlyContinue) {
 58        $7zExe = $p
 59        break
 60    }
 61    if (Test-Path $p) {
 62        $7zExe = $p
 63        break
 64    }
 65}
 66
 67if (-not $7zExe) {
 68    Write-Host "7z not found. Attempting to install via scoop..."
 69    scoop install 7zip
 70    $7zExe = "$env:USERPROFILE\scoop\apps\7zip\current\7z.exe"
 71}
 72
 73Write-Host "Extracting mpv-dev..."
 74if (Test-Path $tempExtract) { Remove-Item -Recurse -Force $tempExtract }
 75& $7zExe x $tempFile -o"$tempExtract" -y | Out-Null
 76
 77# Copy the files we need
 78$libFile = Get-ChildItem -Path $tempExtract -Filter "mpv.lib" -Recurse | Select-Object -First 1
 79$dllFile = Get-ChildItem -Path $tempExtract -Filter "libmpv-2.dll" -Recurse | Select-Object -First 1
 80
 81if (-not $libFile) {
 82    # The shinchiro builds ship libmpv.dll.a (GCC format), not mpv.lib (MSVC format).
 83    # Generate mpv.lib from the DLL using MSVC dumpbin + lib tools.
 84    Write-Host "mpv.lib not found in archive. Generating from libmpv-2.dll..."
 85
 86    $dllForLib = Get-ChildItem -Path $tempExtract -Filter "libmpv-2.dll" -Recurse | Select-Object -First 1
 87    if (-not $dllForLib) {
 88        Write-Error "Could not find libmpv-2.dll in the archive!"
 89    }
 90
 91    # Find MSVC tools — installed but not in PATH by default
 92    $vsBase = "${env:ProgramFiles(x86)}\Microsoft Visual Studio"
 93    if (-not (Test-Path $vsBase)) { $vsBase = "$env:ProgramFiles\Microsoft Visual Studio" }
 94    $dumpbin = Get-ChildItem -Path $vsBase -Recurse -Filter "dumpbin.exe" -ErrorAction SilentlyContinue |
 95        Where-Object { $_.FullName -like "*Hostx64\x64*" -or $_.FullName -like "*HostX64\x64*" } |
 96        Select-Object -First 1 -ExpandProperty FullName
 97    $libExe = Get-ChildItem -Path $vsBase -Recurse -Filter "lib.exe" -ErrorAction SilentlyContinue |
 98        Where-Object { $_.FullName -like "*Hostx64\x64*" -or $_.FullName -like "*HostX64\x64*" } |
 99        Select-Object -First 1 -ExpandProperty FullName
100
101    if (-not $dumpbin -or -not $libExe) {
102        Write-Error "Could not find MSVC build tools (dumpbin.exe, lib.exe). Install 'Desktop development with C++' workload."
103    }
104
105    # Export function names from DLL and write .def file
106    $defPath = Join-Path $OutputDir "mpv.def"
107    $exports = & $dumpbin /exports "$($dllForLib.FullName)" 2>&1 |
108        Select-String "^\s+\d+\s+[0-9A-Fa-f]+\s+[0-9A-Fa-f]+\s+(\S+)" |
109        ForEach-Object { $_.Matches[0].Groups[1].Value }
110
111    $defContent = "LIBRARY libmpv-2`nEXPORTS`n"
112    $defContent += ($exports | ForEach-Object { "    $_" }) -join "`n"
113    Set-Content -Path $defPath -Value $defContent -Encoding ASCII
114
115    # Generate .lib from .def
116    $machine = if ($arch -eq "x86_64") { "x64" } else { "x86" }
117    & $libExe /def:"$defPath" /machine:$machine /out:"$OutputDir\mpv.lib" 2>&1 | Out-Null
118
119    if (-not (Test-Path "$OutputDir\mpv.lib")) {
120        Write-Error "Failed to generate mpv.lib!"
121    } else {
122        Write-Host "Generated mpv.lib successfully."
123    }
124} else {
125    Copy-Item $libFile.FullName $OutputDir
126}
127
128if ($dllFile) {
129    Copy-Item $dllFile.FullName $OutputDir
130}
131
132# Also grab any other DLLs from the archive
133Get-ChildItem -Path $tempExtract -Filter "*.dll" -Recurse | ForEach-Object {
134    Copy-Item $_.FullName $OutputDir -Force
135}
136
137# Cleanup temp files
138Remove-Item -Force $tempFile -ErrorAction SilentlyContinue
139Remove-Item -Recurse -Force $tempExtract -ErrorAction SilentlyContinue
140
141$env:MPV_LIB_DIR = $OutputDir
142Write-Host ""
143Write-Host "Done! mpv dev files extracted to: $OutputDir"
144Write-Host "MPV_LIB_DIR set to: $OutputDir"
145Write-Host ""
146Write-Host "To make MPV_LIB_DIR permanent, add to your PowerShell profile:"
147Write-Host "  `$env:MPV_LIB_DIR = '$OutputDir'"