Skip to content

windows: opt-in CRT-free build - #860

Open
ebraminio wants to merge 9 commits into
libusb:masterfrom
ebraminio:crt-free
Open

ebraminio wants to merge 9 commits into
libusb:masterfrom
ebraminio:crt-free

Conversation

@ebraminio

@ebraminio ebraminio commented Nov 19, 2026 •

Copy link
Copy Markdown
Contributor

HIDAPI's Windows backend uses the CRT for allocation, wide-string operations, and error formatting. That is fine for normal desktop builds, but it can matter when HIDAPI is linked into code that does not control the process runtime: ASI/Mod loader plugins, audio/video plugins, game overlays, and similar injected modules, or a native messaging host. Those hosts may build with /NODEFAULTLIB, supply their own entry point, or pin a runtime, and a library that pulls in CRT imports can be awkward or impossible to fit. This is not a claim that the UCRT is usually missing or that /MT is not a valid alternative; the narrow case is a host that has already chosen a no-CRT or fixed-runtime model. Binary size can improve as a side effect, but it is not the main argument.

This is a suggestion for an opt-in HIDAPI_NO_CRT mode that uses Win32 allocation, string, and memory helpers instead. The normal CRT build remains the default and its swprintf/wcstol/towupper paths are untouched; the replacements are compiled only when the option is enabled. Report descriptor reconstruction is covered by the same helpers.

A shared HIDAPI DLL still needs CRT startup unless its consumer supplies a custom entry point, so this only removes CRT function imports; the primary target is static embedding. now it supports DLL generation also.

If this is accepted, a reasonable follow-up would be to make the internal helpers inline and use them in the default Windows path as well, so the two implementations can converge into one over the time. The mem* shims are the part that needs care: they exist for compiler-generated calls, not for HIDAPI's own code, and removing them requires either compiler/linker changes or keeping a small shim. I would not expand this PR to change the default path yet; the safer sequence is to let the opt-in path get some use and CI coverage first. To not make things complicated, it doesn't include meson support but can be provided later.

If this does not fit HIDAPI's direction or the maintenance cost is not worth it, closing or ignoring it is completely fine.

@ebraminio
ebraminio force-pushed the crt-free branch 11 times, most recently from 3b6c69d to 4f86d3b Compare November 20, 2026 01:38
@mcuee mcuee added the Windows Related to Windows backend label Nov 22, 2026
@ebraminio
ebraminio force-pushed the crt-free branch 10 times, most recently from ac98609 to f7156d3 Compare November 22, 2026 12:45
@ebraminio
ebraminio force-pushed the crt-free branch 4 times, most recently from 826b88f to ddf3e3d Compare November 25, 2026 12:11
Comment thread windows/hidapi_descriptor_reconstruct.c Resolved
Comment thread .github/workflows/builds.yml Resolved
Comment thread .github/workflows/builds.yml Outdated
Comment on lines +389 to +413
if ($compiler -eq 'gcc') {
gcc -c -DHID_API_NO_EXPORT_DEFINE -Ihidapisrc\hidapi hidapisrc\windows\test\no_crt_probe.c -o no_crt_probe.o
if ($LASTEXITCODE -ne 0) { $ok = $false; continue }
gcc -nostdlib "-Wl,--entry=mainCRTStartup" "-Wl,--subsystem,console" `
-o no_crt_probe.exe no_crt_probe.o "$($lib.FullName)" -lkernel32 -luser32 -lshlwapi -lgcc
if ($LASTEXITCODE -ne 0) { $ok = $false; continue }

gcc -c -DHIDAPI_NO_CRT -DHIDAPI_TEST_MEM_FUNCTIONS -DHID_API_NO_EXPORT_DEFINE -Ihidapisrc\hidapi hidapisrc\windows\test\no_crt_probe.c -o no_crt_probe_amalgamated.o
if ($LASTEXITCODE -ne 0) { $ok = $false; continue }
gcc -nostdlib "-Wl,--entry=mainCRTStartup" "-Wl,--subsystem,console" `
-o no_crt_probe_amalgamated.exe no_crt_probe_amalgamated.o -lkernel32 -luser32 -lshlwapi -lgcc
if ($LASTEXITCODE -ne 0) { $ok = $false; continue }
} else {
& $compiler /nologo /c /GS- /Gs9999999 /DHID_API_NO_EXPORT_DEFINE /Ihidapisrc\hidapi hidapisrc\windows\test\no_crt_probe.c /Fo:no_crt_probe.obj
if ($LASTEXITCODE -ne 0) { $ok = $false; continue }
link /nologo /NODEFAULTLIB /ENTRY:mainCRTStartup /SUBSYSTEM:CONSOLE `
/OUT:no_crt_probe.exe no_crt_probe.obj "$($lib.FullName)" kernel32.lib user32.lib shlwapi.lib
if ($LASTEXITCODE -ne 0) { $ok = $false; continue }

& $compiler /nologo /c /GS- /Gs9999999 /DHIDAPI_NO_CRT /DHIDAPI_TEST_MEM_FUNCTIONS /DHID_API_NO_EXPORT_DEFINE /Ihidapisrc\hidapi hidapisrc\windows\test\no_crt_probe.c /Fo:no_crt_probe_amalgamated.obj
if ($LASTEXITCODE -ne 0) { $ok = $false; continue }
link /nologo /NODEFAULTLIB /ENTRY:mainCRTStartup /SUBSYSTEM:CONSOLE `
/OUT:no_crt_probe_amalgamated.exe no_crt_probe_amalgamated.obj kernel32.lib user32.lib shlwapi.lib
if ($LASTEXITCODE -ne 0) { $ok = $false; continue }
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

make a small CMake or Make/GMake/NMake script instead of direct compiler/linker invocation, this looks too verbose for a CI script

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Replaced the direct compiler/linker invocations with windows/test/no_crt/CMakeLists.txt. CI now configures/builds both probe targets through CMake for cl, clang-cl, and gcc.

Comment thread windows/hidapi_winapi_crt.h Resolved
@Youw

Youw commented Nov 25, 2026

Copy link
Copy Markdown
Member

windows/hidapi_winapi_crt.h is the only thing I haven't properly reviewed yet, I'll get back to it shortly. Everhting else - already has my review commenst

@ebraminio

Copy link
Copy Markdown
Contributor Author

I’ve addressed the review items in ad715f4 and resolved the threads:

  • HIDAPI_WCSTOL → explicit HIDAPI_WCSTOL_HEX
  • shlwapi is now a PRIVATE implementation dependency
  • CI probe builds moved into windows/test/no_crt/CMakeLists.txt
  • CMake configuration arguments split one per line
  • Removed the remaining compiler-generated memset from the aggregate zero-initialization

The fundamental tradeoff is still real: HIDAPI_NO_CRT has to prevent every compiler-generated CRT libcall, not just the ones we call explicitly. The CI now links and runs both probes across MSVC, clang-cl, and GCC, so a regression should be caught, but it does add maintenance sensitivity to compiler code generation. If maintainers conclude that burden is not worth an opt-in mode, I understand closing this rather than forcing it in.

@ebraminio
ebraminio force-pushed the crt-free branch 4 times, most recently from 7d92615 to 5720761 Compare November 26, 2026 07:24
@ebraminio

ebraminio commented Nov 26, 2026 •

Copy link
Copy Markdown
Contributor Author

Unrelated to this patch please consider registering hidapi in https://github.com/anthropics/oss-scanner it catches interesting bugs and only the maintainers can do the registration (no need to reply if that's not interesting to you).

And sorry for the repeated amends, will try to lower them from now on.

@ebraminio
ebraminio force-pushed the crt-free branch 4 times, most recently from cd82e22 to 7a8e7f2 Compare November 26, 2026 09:19
Add HIDAPI_NO_CRT to replace CRT allocation, string, memory and formatting
helpers with Win32 equivalents while leaving CRT builds unchanged. Route
the implementation through HIDAPI_* aliases and use the volatile byte-loop
fallback for clang instead of its CRT-lowered intrinsics.

Avoid compiler-generated memset calls in the descriptor reconstruction and
device-detection initialization so the library exports no standard memory
symbols. Add MSVC, clang-cl and MinGW no-CRT CI that builds the library,
links regular and amalgamated probes without the CRT, checks imports, and
runs the memory helpers at runtime.

Assisted-by: deepseek-harness:deepseek-flash
Move the two 8x256 descriptor lookup arrays off the stack so the no-CRT
build no longer needs stack-probe suppression. Keep /GS- because the
security cookie is still required for local SDK structs.

Assisted-by: deepseek-harness:deepseek-flash
Add a custom DllMain and no-default-lib link options for shared
HIDAPI_NO_CRT builds, and check the DLL imports/exports in CI.

Reproduced the official hidapi-0.16.0 x64 release from tag 750c87a
using windows/hidapi.vcxproj (MSBuild Release|x64, /MT static CRT,
LTCG): 169,472 bytes, importing only KERNEL32.dll.

x64 DLL sizes before/after:

  Official release (/MT static CRT, LTCG): 169,472 bytes
  this no-CRT build:
    MSVC LTCG:                              42,496 bytes (-75%)
    MSVC:                                   39,424 bytes (-77%)
    clang-cl:                               43,520 bytes (-74%)
    MinGW:                                  56,057 bytes (-67%)

The regular MSVC /MD build is 42,496 bytes but imports VCRUNTIME140 and
the UCRT API sets; the no-CRT builds import only system DLLs. Exact sizes
depend on compiler/linker versions and release packaging.

Assisted-by: deepseek-harness:deepseek-flash
@ebraminio

Copy link
Copy Markdown
Contributor Author

Added shared-DLL support for HIDAPI_NO_CRT in fd0832d.

Shared no-CRT builds now use a custom DllMain and no default libraries:

  • MSVC/clang-cl: /NODEFAULTLIB /ENTRY:DllMain
  • MinGW: -nostdlib -nodefaultlibs -Wl,--entry=DllMain

CI now also builds the shared DLL for cl, clang-cl, and gcc, checks that it has no CRT imports, and checks the hid_init export.

For size context, I reproduced the official hidapi-0.16.0 x64 release from tag 750c87a using windows/hidapi.vcxproj (MSBuild Release|x64, /MT static CRT, LTCG). It is exactly 169,472 bytes and imports only KERNEL32.dll.

Size comparison (x64, same backend):

Official release (/MT static CRT, LTCG): 169,472 bytes
this no-CRT build:
  MSVC LTCG:                              42,496 bytes (-75%)
  MSVC:                                   39,424 bytes (-77%)
  clang-cl:                               43,520 bytes (-74%)
  MinGW:                                  56,057 bytes (-67%)

The normal MSVC /MD build is also 42,496 bytes, but imports VCRUNTIME140.dll and the UCRT API sets. The no-CRT builds import only KERNEL32.dll, USER32.dll, and SHLWAPI.dll.

@ebraminio

ebraminio commented Nov 27, 2026 •

Copy link
Copy Markdown
Contributor Author

One open design question I’d like your input on before settling the patch:

Meson option

The Meson wrapper delegates the actual build to a CMake subproject, so a parent Meson project cannot pass HIDAPI_NO_CRT through something like c_args; I confirmed that path does not reach the CMake target. Exposing the CRT-free build through Meson would therefore require a dedicated Meson option that translates to a CMake define.

I’m unsure whether that belongs in hidapi. A Windows-only, opt-in build mode is fairly specialized, and adding a generic Meson option for it may be more surface area than it’s worth. The alternative is to leave CRT-free builds CMake-only and document that Meson subprojects cannot select it. Longer term, if CRT-free support becomes part of the normal Windows path rather than an opt-in mode, the option would become obsolete anyway. Would you prefer:

  • no Meson option, keeping this CMake-only for now;
  • a Meson no_crt option; or
  • deferring the Meson integration until the long-term direction is clearer?

Naming

Two names feel slightly off to me:

  • windows/hidapi_winapi_crt.h: the header now provides either normal CRT-based macros or the WinAPI-backed compatibility shim. The crt part describes only one branch, and winapi is redundant under windows/. I was thinking something like hidapi_runtime.h, hidapi_compat.h, hidapi_crt.h or hidapi_crt_compat.h, but I don’t feel strongly.
  • windows/hidapi_winapi_nocrt_dllmain.c: accurate for the current opt-in CRT-free shared build (aside winapi feels being redundant), but long and tied to the mode. If CRT-free eventually becomes the normal path, hidapi_winapi_entry.c or hidapi_winapi_dllmain.c might age better.

I’m happy to follow whichever naming and integration direction you prefer; I don’t want to be proactive about these until there’s a maintainer preference.

@Youw

Youw commented Nov 27, 2026

Copy link
Copy Markdown
Member

Meson option

Keep it CMake-only for now, no Meson-specific documentation is required.

Naming

  • hidapi_winapi_dllmain.c seem fine even now, when it is used optionally
  • windows/hidapi_winapi_crt_compat.h - it is what it is, this name is explicit leaves zero room for misinterpretation; if it feels too verbose - I don't think we should agree to anything less than windows/hidapi_crt_compat.h

Rename the CRT/WinAPI compatibility header to
hidapi_winapi_crt_compat.h and the optional CRT-free DLL entry source
to hidapi_winapi_dllmain.c. Keep the CRT-free Meson integration out of
this patch; it remains CMake-only for now.
KERNEL32.dll   26 symbols
  files, heaps, events, overlapped I/O, code pages,
  lstrlenW, lstrcpynW, FormatMessageW, etc.

SHLWAPI.dll     2 symbols
  wnsprintfW, StrStrW

USER32.dll      1 symbol
  CharUpperW

This does not change behavior; it makes the link declaration reflect which
system DLL provides the bulk of the backend's Win32 surface.
Apply the shared no-CRT link flags to Cygwin. Without those flags
Cygwin's GCC still pulls in the Cygwin runtime.

The main advantage is that the CRT-free Cygwin DLL no longer depends
on cygwin1.dll: the CMake build is about 57 KB instead of 96 KB and
imports only kernel32, shlwapi, and user32. Verified normal and no-CRT
builds with Cygwin CMake, MSVC, and MinGW.
@ebraminio

ebraminio commented Nov 28, 2026 •

Copy link
Copy Markdown
Contributor Author

Keep it CMake-only for now, no Meson-specific documentation is required.

  • hidapi_winapi_dllmain.c seem fine even now, when it is used optionally
  • windows/hidapi_winapi_crt_compat.h - it is what it is, this name is explicit leaves zero room for misinterpretation; if it feels too verbose - I don't think we should agree to anything less than windows/hidapi_crt_compat.h

Applied

Also added the Cygwin support in 34308a0 usually Cygwin generated artifacts are cygwin1.dll dependent but a no-CRT build is free from that (normal build isn't touched except that move of header which is needed just to build).

Just to note shlwapi is linked only because of wnsprintfW/StrStrW and user32 for CharUpperW as noted in 3c92a86 also. All workaround-able (perhaps for a future work not needed be now if is needed at all) to make the no-CRT build only kernel32 dependent.

Use one guarded ntdef include for MinGW and Cygwin, parenthesize all
compatibility macro arguments consistently, and add the standard
license header to both no-CRT probe sources.
Add optional shared-DLL/import-library inputs to the no-CRT probe
project, copy the DLL beside the probe, and run an init smoke test in
the no-CRT CI job. This covers loader, custom DllMain, and import-library
behavior that import-table checks alone miss.
Use one Test-NoCrtDependencies helper for the static probes, shared
DLL, and shared probe instead of repeating the dumpbin regex.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Windows Related to Windows backend

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants