build: fix MinGW-w64 cross-compilation for Windows - #443
Open
hjiawei wants to merge 2 commits into
Open
Conversation
3 tasks
hjiawei
force-pushed
the
mingw-cross-build
branch
from
July 7, 2026 07:08
a1505a3 to
d63e955
Compare
Allow the tree to build for a Windows target with the MinGW-w64 UCRT toolchain (GCC) in addition to MSVC: - Set _POSIX_THREAD_SAFE_FUNCTIONS so MinGW declares the POSIX *_r time functions (localtime_r, ...), and keep the incompatible-pointer-type mismatches (Winsock recv/send/getsockopt take char* buffers where POSIX uses void*) as warnings; GCC 14 and later otherwise reject them as errors, while MSVC accepts both. - Use the POSIX threading model on MinGW, which ships a real pthread implementation (winpthreads); the hand-rolled Windows pthread shim stays MSVC-only. - Guard the MSVC-only typedefs and shims (mode_t, pid_t, timespec, the nanosleep and localtime_r replacements, the stdint fallback) that the MinGW headers already provide. - Widen MSVC-only gates that cover Windows functionality (the glob fallback, _fullpath, MK_INLINE) to _WIN32. - Use the standard GCC container_of on MinGW; the PCHAR/ULONG_PTR variant relies on Windows SDK typedefs only MSVC pulls in implicitly. - Lowercase capitalized Windows header includes so they resolve on a case-sensitive filesystem when cross-compiling from Linux. - In the bundled flb_libco, check _WIN32 before the architecture so x86_64 MinGW selects the fiber backend instead of amd64.c, whose co_swap executes a read-only machine-code blob that faults under DEP on the first coroutine switch -- the same failure the MSVC branch already avoids by disabling amd64.c "due to SIGSEGV bug". - For the bundled libevent: record the winsock dependency on the static archives; and, for MinGW only, pin NTDDI_VERSION next to the existing _WIN32_WINNT override in evutil.c (its sdkddkver.h errors when the two disagree, where MSVC does not, so MSVC compiles the file unchanged) and define EVENT__HAVE_STRTOK_R (libevent's WIN32 function check takes the function's address and only sees the force-included winsock headers, so it mis-detects strtok_r from <string.h> and then defines a conflicting static copy). Signed-off-by: Jiawei Huang <jiawei@tigera.io>
Cross-build monkey as a library for Windows from Linux with the Fedora MinGW-w64 UCRT64 toolchain, alongside the existing MSVC Windows job, so the MinGW support does not regress. Uses the same library-only profile (MK_LIB_ONLY, no bin/conf) as the MSVC job. Signed-off-by: Jiawei Huang <jiawei@tigera.io>
hjiawei
force-pushed
the
mingw-cross-build
branch
from
July 7, 2026 07:20
d63e955 to
c7b888a
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This lets monkey build for a Windows target with the MinGW-w64 UCRT toolchain (GCC), in addition to MSVC. Everything MinGW-specific is gated on
MINGW/__MINGW32__(or falls under existing!__MINGW32__guards), so MSVC builds compile the same code as before.Build fixes (
build: fix MinGW-w64 cross-compilation for Windows):_POSIX_THREAD_SAFE_FUNCTIONSso MinGW declares the POSIX*_rtime functions (localtime_r, ...), and keep the incompatible-pointer-type mismatches (Winsockrecv/send/getsockopttakechar*buffers where POSIX usesvoid*) as warnings — GCC 14+ otherwise rejects them as errors, while MSVC accepts both.mode_t,pid_t,timespec, thenanosleep/localtime_rreplacements, the stdint fallback) that the MinGW headers already provide.globfallback,_fullpath,MK_INLINE) to_WIN32.container_ofon MinGW; thePCHAR/ULONG_PTRvariant relies on Windows SDK typedefs only MSVC pulls in implicitly._WIN32before the architecture so x86_64 MinGW selects the fiber backend instead ofamd64.c(whoseco_swapruns a read-only machine-code blob that faults under DEP on the first coroutine switch), matching the MSVC branch that already disablesamd64.c. This same fix is submitted upstream to flb_libco as libco: select the fiber backend on Windows edsiper/flb_libco#12 — monkey's vendored copy can drop it once that merges and flb_libco is re-synced.NTDDI_VERSIONnext to the existing_WIN32_WINNToverride inevutil.c(itssdkddkver.herrors when the two disagree, where MSVC does not, so MSVC compiles the file unchanged) and defineEVENT__HAVE_STRTOK_R(libevent's WIN32 function check takes the function's address and only sees the force-included winsock headers, so it mis-detectsstrtok_rfrom<string.h>and then defines a conflicting static copy).CI (
workflows: add a MinGW-w64 cross-compile check): a newbuild-windows-mingwjob cross-builds monkey as a library from Linux with the Fedora MinGW-w64 UCRT64 toolchain, alongside the existing MSVC Windows job, so the MinGW support doesn't regress. It uses the same library-only profile (MK_LIB_ONLY, no bin/conf) as the MSVC job.Testing
libmonkey.aproduced, 0 errors.fluent-bit.exe(PE32+ x86-64).MINGW/__MINGW32__, andevutil.ccompiles byte-for-byte as before under MSVC.