Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
854394e
windows: add opt-in CRT-free backend
ebraminio Nov 25, 2026
570a5c8
windows: move descriptor lookup tables to the heap
ebraminio Nov 26, 2026
fd0832d
windows: support CRT-free shared builds
ebraminio Nov 26, 2026
7dd840c
windows: clarify CRT compatibility and DllMain names
ebraminio Nov 27, 2026
3c92a86
windows: order no-CRT libraries by import usage
ebraminio Nov 27, 2026
34308a0
windows: support CRT-free Cygwin builds
ebraminio Nov 28, 2026
040267c
windows: tidy platform includes and no-CRT helpers
ebraminio Nov 28, 2026
c7fea59
windows: run no-CRT probe against shared DLL
ebraminio Nov 28, 2026
f5eaac0
ci: centralize no-CRT dependency check
ebraminio Nov 28, 2026
ec08b75
windows: use intrin.h on MinGW/Cygwin too
ebraminio Nov 30, 2026
6ece5cf
Fix Windows CRT-free builds and consumer checks
Youw Dec 1, 2026
b618cac
windows: minor comment tweak
ebraminio Dec 1, 2026
312289d
windows: guard against alloc fails in const main items additions
ebraminio Dec 1, 2026
237d2b1
windows: Make sure main_item_list doesn't leak
ebraminio Dec 1, 2026
38e37af
Merge pull request #1 from Youw/fix-windows-no-crt-build
ebraminio Dec 1, 2026
2180c06
windows: avoid signed overflow in hex conversion
ebraminio Dec 1, 2026
8018cb7
windows: bound WCSNCPY by capacity without narrowing
ebraminio Dec 1, 2026
b1c15e3
windows: format WinAPI errors without wnsprintfW
ebraminio Dec 1, 2026
0116876
windows: match CRT error formatting in no-CRT builds
ebraminio Dec 1, 2026
c898022
windows: format WinAPI errors with FormatMessageW directly
Youw Dec 1, 2026
382e6e3
windows: include stdio.h in pp_data_dump
Youw Dec 1, 2026
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
172 changes: 172 additions & 0 deletions .github/workflows/builds.yml
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,178 @@ jobs:
ninja


windows-cmake-no-crt:

name: windows no-CRT
runs-on: windows-latest

steps:
- uses: actions/checkout@v7
with:
path: hidapisrc
- name: Set up MSVC developer environment
uses: TheMrMilchmann/setup-msvc-dev@v4
with:
arch: x64
- name: Install Ninja
shell: cmd
run: choco install ninja -y --no-progress
- name: Build MSVC Debug no-CRT DLL
shell: cmd
run: |
cmake -G Ninja ^
-S hidapisrc ^
-B build/no-crt-msvc-debug ^
-DCMAKE_C_COMPILER=cl ^
-DCMAKE_BUILD_TYPE=Debug ^
-DHIDAPI_NO_CRT=ON ^
-DBUILD_SHARED_LIBS=ON || exit /b 1
cmake --build build/no-crt-msvc-debug --target hidapi_winapi
- name: Build and check all no-CRT compilers
shell: pwsh
run: |
function Test-NoCrtDependencies {
param([string]$Path)
$deps = dumpbin /nologo /dependents $Path | Out-String
return $deps -notmatch '(?i)(vcruntime|ucrtbase|msvcrt|api-ms-win-crt|libgcc|libwinpthread|cygwin1)'
}

$ok = $true
foreach ($compiler in @('cl', 'clang-cl', 'gcc')) {
Write-Host "`n===== $compiler ====="
$build = "build\no-crt-$compiler"

cmake -G Ninja `
-B $build `
-S hidapisrc `
"-DCMAKE_C_COMPILER=$compiler" `
-DCMAKE_BUILD_TYPE=Release `
-DBUILD_SHARED_LIBS=OFF `
-DHIDAPI_NO_CRT=ON `
-DHIDAPI_WITH_TESTS=OFF `
-DHIDAPI_BUILD_HIDTEST=OFF
if ($LASTEXITCODE -ne 0) { $ok = $false; continue }

cmake --build $build
if ($LASTEXITCODE -ne 0) { $ok = $false; continue }

$lib = Get-ChildItem "$build\src\windows" -File |
Where-Object { $_.Name -eq 'hidapi.lib' -or $_.Name -eq 'libhidapi.a' } |
Select-Object -First 1
if (-not $lib) {
Write-Host "::error title=$compiler::no-CRT static library not found"
$ok = $false
continue
}

$probeBuild = "$build\probes"
cmake -G Ninja `
-B $probeBuild `
-S hidapisrc\windows\test\no_crt `
"-DCMAKE_C_COMPILER=$compiler" `
-DCMAKE_BUILD_TYPE=Release `
"-DHIDAPI_SOURCE_DIR=$((Get-Location).Path)\hidapisrc" `
"-DHIDAPI_LIBRARY=$($lib.FullName)"
if ($LASTEXITCODE -ne 0) { $ok = $false; continue }

cmake --build $probeBuild
if ($LASTEXITCODE -ne 0) { $ok = $false; continue }

foreach ($probeName in @('no_crt_init_probe', 'no_crt_mem_probe')) {
$probePath = Join-Path $probeBuild "$probeName.exe"
$proc = Start-Process -FilePath $probePath -PassThru
if (-not $proc.WaitForExit(5000)) {
$proc.Kill()
Write-Host "::error title=$compiler/$probeName::no-CRT probe timed out"
$ok = $false
continue
}
if ($proc.ExitCode -ne 0) {
Write-Host "::error title=$compiler/$probeName::no-CRT probe exited with $($proc.ExitCode)"
$ok = $false
}

if (-not (Test-NoCrtDependencies $probePath)) {
Write-Host "::error title=$compiler/$probeName::CRT dependency found in the no-CRT probe"
$ok = $false
}
}

$sharedBuild = "$build-shared"
cmake -G Ninja `
-B $sharedBuild `
-S hidapisrc `
"-DCMAKE_C_COMPILER=$compiler" `
-DCMAKE_BUILD_TYPE=Release `
-DBUILD_SHARED_LIBS=ON `
-DHIDAPI_NO_CRT=ON `
-DHIDAPI_WITH_TESTS=OFF `
-DHIDAPI_BUILD_HIDTEST=OFF
if ($LASTEXITCODE -ne 0) { $ok = $false; continue }

cmake --build $sharedBuild
if ($LASTEXITCODE -ne 0) { $ok = $false; continue }

$dll = Get-ChildItem "$sharedBuild\src\windows" -File -Filter '*.dll' |
Select-Object -First 1
if (-not $dll) {
Write-Host "::error title=$compiler::no-CRT shared DLL not found"
$ok = $false
continue
}

if (-not (Test-NoCrtDependencies $dll.FullName)) {
Write-Host "::error title=$compiler::CRT dependency found in the no-CRT shared DLL"
$ok = $false
}
$exports = dumpbin /nologo /exports $dll.FullName | Out-String
if ($exports -notmatch '(?m)\bhid_init\b') {
Write-Host "::error title=$compiler::hid_init missing from the no-CRT shared DLL"
$ok = $false
}

$implib = Get-ChildItem "$sharedBuild\src\windows" -File |
Where-Object { $_.Name -eq 'hidapi.lib' -or $_.Name -eq 'libhidapi.dll.a' } |
Select-Object -First 1
if (-not $implib) {
Write-Host "::error title=$compiler::no-CRT shared import library not found"
$ok = $false
continue
}

$sharedProbeBuild = "$build\shared-probe"
cmake -G Ninja `
-B $sharedProbeBuild `
-S hidapisrc\windows\test\no_crt `
"-DCMAKE_C_COMPILER=$compiler" `
-DCMAKE_BUILD_TYPE=Release `
"-DHIDAPI_SOURCE_DIR=$((Get-Location).Path)\hidapisrc" `
"-DHIDAPI_LIBRARY=$($lib.FullName)" `
"-DHIDAPI_SHARED_LIBRARY=$($dll.FullName)" `
"-DHIDAPI_SHARED_IMPLIB=$($implib.FullName)"
if ($LASTEXITCODE -ne 0) { $ok = $false; continue }

cmake --build $sharedProbeBuild --target no_crt_shared_init_probe
if ($LASTEXITCODE -ne 0) { $ok = $false; continue }

$sharedProbePath = Join-Path $sharedProbeBuild "no_crt_shared_init_probe.exe"
$proc = Start-Process -FilePath $sharedProbePath -PassThru
if (-not $proc.WaitForExit(5000)) {
$proc.Kill()
Write-Host "::error title=$compiler/no_crt_shared_init_probe::shared no-CRT probe timed out"
$ok = $false
} elseif ($proc.ExitCode -ne 0) {
Write-Host "::error title=$compiler/no_crt_shared_init_probe::shared no-CRT probe exited with $($proc.ExitCode)"
$ok = $false
}

if (-not (Test-NoCrtDependencies $sharedProbePath)) {
Write-Host "::error title=$compiler/no_crt_shared_init_probe::CRT dependency found in the shared no-CRT probe"
$ok = $false
}
}
if (-not $ok) { exit 1 }

windows-cmake-mingw:

runs-on: windows-latest
Expand Down
2 changes: 2 additions & 0 deletions BUILD.cmake.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ most of this document may not be of interest for you; just check variables names

## Installing CMake

Windows builds require CMake 3.13 or later.

CMake can be installed either using your system's package manager,
or by downloading an installer/prebuilt version from the [official website](https://cmake.org/download/).

Expand Down
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ endif()

if(WIN32)
option(HIDAPI_BUILD_PP_DATA_DUMP "Build small Windows console application pp_data_dump.exe" ${IS_DEBUG_BUILD})
option(HIDAPI_NO_CRT "Build the Windows backend without the C runtime" OFF)
endif()

add_subdirectory(src)
Expand Down
1 change: 1 addition & 0 deletions configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,7 @@ fi
m4_undefine([HIDAPI_CONFIGURE_BACKEND_PC])

AC_SUBST([HIDAPI_LIB_NAME])
AC_SUBST([HIDAPI_PC_LIBS_PRIVATE], [])
AC_SUBST([HIDAPI_GENERIC_REQUIRES_PRIVATE])
AC_CONFIG_FILES([pc/hidapi.pc])

Expand Down
1 change: 1 addition & 0 deletions pc/hidapi-backend-specific.pc.in
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@ URL: https://github.com/libusb/hidapi
Version: @VERSION@
Libs: -L${libdir} -l@HIDAPI_BACKEND_LIBRARY_NAME@
@HIDAPI_BACKEND_REQUIRES_PRIVATE@
@HIDAPI_PC_LIBS_PRIVATE@
Cflags: -I${includedir}/hidapi
1 change: 1 addition & 0 deletions pc/hidapi.pc.in
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@ URL: https://github.com/libusb/hidapi
Version: @VERSION@
Libs: -L${libdir} -l@HIDAPI_LIB_NAME@
@HIDAPI_GENERIC_REQUIRES_PRIVATE@
@HIDAPI_PC_LIBS_PRIVATE@
Cflags: -I${includedir}/hidapi
8 changes: 8 additions & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,11 @@ function(hidapi_configure_pc PC_IN_FILE)
set(HIDAPI_BACKEND_REQUIRES_PRIVATE "")
endif()
set(HIDAPI_GENERIC_REQUIRES_PRIVATE "${HIDAPI_BACKEND_REQUIRES_PRIVATE}")
set(HIDAPI_PC_LIBS_PRIVATE "")
if(HIDAPI_WINAPI_SYSTEM_LIBS)
string(REPLACE ";" " -l" pc_system_libs "${HIDAPI_WINAPI_SYSTEM_LIBS}")
set(HIDAPI_PC_LIBS_PRIVATE "Libs.private: -l${pc_system_libs}")
endif()
set(PC_FILE "${CMAKE_CURRENT_BINARY_DIR}/pc/${PC_IN_FILENAME}.pc")
configure_file("${PC_IN_FILE}" "${PC_FILE}" @ONLY)
if(HIDAPI_INSTALL_TARGETS)
Expand Down Expand Up @@ -140,6 +145,9 @@ set(HIDAPI_NEED_EXPORT_LIBUDEV FALSE)
set(HIDAPI_NEED_EXPORT_ICONV FALSE)

if(WIN32)
if(HIDAPI_NO_CRT)
set(HIDAPI_WINAPI_SYSTEM_LIBS kernel32 shlwapi user32)
endif()
target_include_directories(hidapi_include INTERFACE
"$<BUILD_INTERFACE:${PROJECT_ROOT}/windows>"
)
Expand Down
35 changes: 35 additions & 0 deletions windows/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
cmake_minimum_required(VERSION 3.13...4.3)

list(APPEND HIDAPI_PUBLIC_HEADERS "hidapi_winapi.h")

set(SOURCES
Expand All @@ -8,10 +10,14 @@ set(SOURCES
hidapi_hidclass.h
hidapi_hidpi.h
hidapi_hidsdi.h
hidapi_winapi_crt_compat.h
)

if(BUILD_SHARED_LIBS)
list(APPEND SOURCES hidapi.rc)
if(HIDAPI_NO_CRT)
list(APPEND SOURCES hidapi_winapi_dllmain.c)
endif()
endif()

add_library(hidapi_winapi
Expand All @@ -32,6 +38,35 @@ target_compile_definitions(hidapi_winapi
PRIVATE HIDAPI_CMAKE_V0_BUILD
)

if(HIDAPI_NO_CRT)
target_compile_definitions(hidapi_winapi PRIVATE HIDAPI_NO_CRT)
target_link_libraries(hidapi_winapi PRIVATE ${HIDAPI_WINAPI_SYSTEM_LIBS})
if(MSVC)
include("${CMAKE_CURRENT_LIST_DIR}/no-crt-msvc.cmake")
hidapi_disable_msvc_runtime_checks(hidapi_winapi)
# The library does not need __security_cookie from the CRT.
target_compile_options(hidapi_winapi PRIVATE /GS-)
endif()
if(BUILD_SHARED_LIBS)
if(MSVC)
target_link_options(hidapi_winapi PRIVATE /NODEFAULTLIB /ENTRY:DllMain)
elseif(MINGW OR CYGWIN)
include(CheckSymbolExists)
check_symbol_exists(__i386__ "" HIDAPI_TARGET_I386)
if(HIDAPI_TARGET_I386)
set(dll_entry "_DllMain@12")
else()
set(dll_entry DllMain)
endif()
target_link_options(hidapi_winapi PRIVATE
-nostdlib
-nodefaultlibs
-Wl,--entry=${dll_entry}
)
endif()
endif()
endif()

if(NOT BUILD_SHARED_LIBS)
target_compile_definitions(hidapi_winapi
# prevent marking functions as __declspec(dllexport) for static library build
Expand Down
Loading
Loading