-
Notifications
You must be signed in to change notification settings - Fork 76
Expand file tree
/
Copy pathsetup-hidapi.ps1
More file actions
107 lines (88 loc) · 5.16 KB
/
Copy pathsetup-hidapi.ps1
File metadata and controls
107 lines (88 loc) · 5.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
<#
.SYNOPSIS
Download and build hidapi for Windows x64.
.DESCRIPTION
Downloads hidapi 0.14.0 source from GitHub, builds it with CMake + MSVC,
and places headers/lib/dll in third_party/hidapi/ ready for CMake.
Required for USB HID device support (Stream Deck, Icom RC-28,
Griffin PowerMate, Contour Shuttle, etc.).
.EXAMPLE
.\setup-hidapi.ps1
#>
$ErrorActionPreference = "Stop"
. "$PSScriptRoot\_verify_sha256.ps1"
$HidapiVersion = "0.14.0"
$HidapiUrl = "https://github.com/libusb/hidapi/archive/refs/tags/hidapi-${HidapiVersion}.tar.gz"
# SHA256 of the GitHub source archive (#3665). Bump alongside the version.
$HidapiSha256 = "a5714234abe6e1f53647dd8cba7d69f65f71c558b7896ed218864ffcf405bcbd"
$OutDir = "third_party\hidapi"
$TarFile = "third_party\hidapi-${HidapiVersion}.tar.gz"
# ── Check if already set up ──────────────────────────────────────────────
if (Test-Path "$OutDir\lib\hidapi.lib") {
Write-Host "hidapi already set up in $OutDir" -ForegroundColor Green
exit 0
}
# ── Create directories ───────────────────────────────────────────────────
New-Item -ItemType Directory -Force -Path "third_party" | Out-Null
New-Item -ItemType Directory -Force -Path $OutDir | Out-Null
New-Item -ItemType Directory -Force -Path "$OutDir\lib" | Out-Null
New-Item -ItemType Directory -Force -Path "$OutDir\include\hidapi" | Out-Null
New-Item -ItemType Directory -Force -Path "$OutDir\bin" | Out-Null
# ── Download source ─────────────────────────────────────────────────────
if (-not (Test-Path $TarFile)) {
Write-Host "Downloading hidapi ${HidapiVersion} source..." -ForegroundColor Cyan
Invoke-WebRequest -Uri $HidapiUrl -OutFile $TarFile
Confirm-Sha256 -Path $TarFile -Expected $HidapiSha256
}
# ── Extract ──────────────────────────────────────────────────────────────
Write-Host "Extracting..." -ForegroundColor Cyan
$tempDir = "third_party\hidapi-temp"
if (Test-Path $tempDir) { Remove-Item -Recurse -Force $tempDir }
New-Item -ItemType Directory -Force -Path $tempDir | Out-Null
tar -xzf $TarFile -C $tempDir 2>$null
$srcDir = Get-ChildItem "$tempDir\hidapi-hidapi-*" -Directory | Select-Object -First 1
if (-not $srcDir) {
$srcDir = Get-ChildItem "$tempDir\hidapi-*" -Directory | Select-Object -First 1
}
# ── Copy headers ─────────────────────────────────────────────────────────
Copy-Item "$($srcDir.FullName)\hidapi\hidapi.h" "$OutDir\include\hidapi\"
# ── Build with CMake + MSVC ──────────────────────────────────────────────
Write-Host "Building hidapi from source with MSVC..." -ForegroundColor Cyan
$buildDir = "$($srcDir.FullName)\build"
# CMAKE_POLICY_VERSION_MINIMUM: hidapi 0.14.0's CMakeLists declares a
# cmake_minimum_required below 3.5, which CMake 4.x (on GitHub's
# windows-latest runner image since 2026-06-10) refuses outright. The
# override tells CMake to configure with 3.5 policy semantics — the build
# itself is unaffected. Drop this once hidapi is bumped to a release with
# a modern minimum.
#
# The flag MUST be quoted: PowerShell's native-argument tokenizer splits
# an unquoted -Dkey=3.5 at the dot, so CMake receives "3" and rejects it
# ("Invalid CMAKE_POLICY_VERSION_MINIMUM value"). The other -D flags only
# survive unquoted because their values contain no dot.
cmake -B $buildDir -S $srcDir.FullName -G "Ninja" `
-DCMAKE_BUILD_TYPE=Release `
-DBUILD_SHARED_LIBS=ON `
"-DCMAKE_POLICY_VERSION_MINIMUM=3.5"
cmake --build $buildDir --config Release -j $env:NUMBER_OF_PROCESSORS
# ── Find and copy built artifacts ────────────────────────────────────────
# hidapi builds hidapi.dll + hidapi.lib (import lib) on Windows
$dllFile = Get-ChildItem "$buildDir" -Recurse -Filter "hidapi.dll" | Select-Object -First 1
$libFile = Get-ChildItem "$buildDir" -Recurse -Filter "hidapi.lib" | Select-Object -First 1
if (-not $libFile) {
Write-Error "Failed to build hidapi.lib"
exit 1
}
Copy-Item $libFile.FullName "$OutDir\lib\hidapi.lib"
if ($dllFile) {
Copy-Item $dllFile.FullName "$OutDir\bin\hidapi.dll"
}
# ── Cleanup ──────────────────────────────────────────────────────────────
Remove-Item -Recurse -Force $tempDir
Remove-Item -Force $TarFile
Write-Host "hidapi ready in $OutDir" -ForegroundColor Green
Write-Host " Header: $OutDir\include\hidapi\hidapi.h"
Write-Host " Lib: $OutDir\lib\hidapi.lib"
if ($dllFile) {
Write-Host " DLL: $OutDir\bin\hidapi.dll"
}