Skip to content

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

Merged
Youw merged 21 commits into
libusb:masterfrom
ebraminio:crt-free
Dec 2, 2026
Merged

Youw merged 21 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
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.
Comment thread windows/hidapi_winapi_crt_compat.h Resolved
The x86 memory-intrinsic path only recognized MSVC/clang-cl, so
GCC fell back to byte loops even though its intrin.h provides __stosb
and __movsb as inline asm. Include it for MinGW as well and rename the
feature macro to HIDAPI_WINAPI_HAVE_X86_MEM_INTRIN.
Comment thread windows/hidapi_descriptor_reconstruct.c Resolved

@Youw Youw left a comment

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.

Two suggestions for the CRT-free helpers.

Generated by Codex (gpt-6.2-astra).

Comment thread windows/hidapi_winapi_crt_compat.h Resolved
Comment thread windows/hidapi_winapi_crt_compat.h Resolved
Comment thread windows/hidapi_winapi_crt_compat.h Resolved
Require CMake 3.13 for Windows. Link the MSVC Debug DLL without runtime checks, select the x86 MinGW entry points, and publish private libraries for static pkg-config consumers. Report lifecycle failures from the initialization probe.

Assisted-by: Codex:gpt-6.2-astra
ebraminio and others added 6 commits December 1, 2026 14:40
It needs to wrap the ending part of the function with a block
otherwise it will become an invalid C++ code as -Wjump-misses-init,
the alternative solution is to move the variables at the of the
function that however decrease readability as widens lifespan of
the variables to places they didn't actually touched.
If an allocation failure happens the above list freeing part is bypassed
so this makes sure leak won't happen even if "Encode the report descriptor output"
part is bypassed by the goto.
Fix Windows CRT-free builds and static consumer linking
The no-CRT fallback shifted a signed long and could overflow. Saturate
at LONG_MAX while still advancing endptr, matching wcstol behavior.
Use a size_t-capacity helper so 64-bit callers don't truncate maxlen to
int, and always terminate the destination.
Use HIDAPI_SWPRINTF in the CRT path and FormatMessageW with an argument
array in the no-CRT path, behind a shared helper.
Build the no-CRT WinAPI error string directly instead of using
FormatMessageW's FROM_STRING formatting. FROM_STRING requires a second
format language and argument array, and %3 dereferences the detail even
when the system lookup returned nothing. The direct formatter preserves
the original `%.*ls: (0x%08X) %.*ls` output without changing the shared
caller or its trimming behavior.
Comment thread windows/hid.c Resolved
Youw added 2 commits December 2, 2026 01:04
Drop hid_internal_format_winapi_error and HIDAPI_SWPRINTF. The CRT and
the no-CRT builds now format the error string with one FormatMessageW
call in register_winapi_error_to_buffer. The output is unchanged.

Assisted-by: claude-code:claude-fable-5-2
pp_data_dump.c uses stdio functions and got <stdio.h> only through
hid.c, which no longer includes it.

Assisted-by: claude-code:claude-fable-5-2

@Youw Youw left a comment

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.

Nice!

@Youw
Youw merged commit 9b44c12 into libusb:master Dec 2, 2026
21 checks passed
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