Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
117 changes: 117 additions & 0 deletions .github/workflows/builds.yml
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,123 @@ 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 and check all no-CRT compilers
shell: pwsh
run: |
$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
}

$deps = dumpbin /nologo /dependents $probePath | Out-String
if ($deps -match '(?i)(vcruntime|ucrtbase|msvcrt|api-ms-win-crt|libgcc|libwinpthread)') {
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
}

$deps = dumpbin /nologo /dependents $dll.FullName | Out-String
if ($deps -match '(?i)(vcruntime|ucrtbase|msvcrt|api-ms-win-crt|libgcc|libwinpthread)') {
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
}
}
if (-not $ok) { exit 1 }

windows-cmake-mingw:

runs-on: windows-latest
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
24 changes: 24 additions & 0 deletions windows/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,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 +36,26 @@ 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 kernel32 shlwapi user32)
if(MSVC)
# 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)
target_link_options(hidapi_winapi PRIVATE
-nostdlib
-nodefaultlibs
-Wl,--entry=DllMain
)
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