Files
kdm-re/cmake/toolchain-mingw-i686.cmake

66 lines
3.8 KiB
CMake

# CMake cross-compilation toolchain file for the Windows (32-bit) build of
# gamekdm.dll, targeting i686-w64-mingw32 with the pinned no-sudo Nix toolchain
# recorded in ../toolchain.lock (GCC 16.2.0, mingw-w64 14.0.0 headers/CRT).
#
# Usage (after `source ./activate.sh` to put the cross toolchain on PATH):
# cmake -S . -B build -DCMAKE_TOOLCHAIN_FILE=cmake/toolchain-mingw-i686.cmake
# cmake --build build
#
# Pattern reused/adapted from ~/kinect/cmake/mingw-w64.cmake, fixed to the
# i686 (32-bit) target that matches the shipping gamekdm.dll's bitness
# (confirmed PE32 by `objdump -f` on the stub build; see task-2.log).
set(CMAKE_SYSTEM_NAME Windows)
set(CMAKE_SYSTEM_PROCESSOR x86)
set(_gk_mingw_prefix i686-w64-mingw32)
set(CMAKE_C_COMPILER ${_gk_mingw_prefix}-gcc)
set(CMAKE_CXX_COMPILER ${_gk_mingw_prefix}-g++)
set(CMAKE_RC_COMPILER ${_gk_mingw_prefix}-windres)
set(CMAKE_RC_COMPILER_INIT ${_gk_mingw_prefix}-windres)
find_program(CMAKE_DLLTOOL NAMES ${_gk_mingw_prefix}-dlltool dlltool)
set(CMAKE_FIND_ROOT_PATH /usr/${_gk_mingw_prefix})
set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)
set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)
# GCC 16.2.0's i686-w64-mingw32 default link spec needs -lmcfgthread, which
# ships as its own Nix package (pkgsCross.mingw32.windows.mcfgthreads) rather
# than bundled with the gcc derivation itself; point the linker at its lib
# dir explicitly for every link (shared libs/DLLs and any executables alike).
# Store path pinned in ../toolchain.lock; keep in sync if the pin moves.
set(_gk_mcfgthread_lib "/nix/store/g72bh3mrbh6gkh59axd2199js9kccxfs-mcfgthread-i686-w64-mingw32-2.5.1/lib")
# The original gamekdm.dll was built by MSVC with a statically linked CRT and
# imports ZERO C/C++-runtime DLLs (confirmed: its import table has no
# msvcrXX.dll-style runtime entries beyond genuine Win32 system DLLs). A
# MinGW rebuild must reproduce that shape (no libgcc_s_dw2-1.dll,
# libstdc++-6.dll, libwinpthread-1.dll or libmcfgthread-*.dll import) or it is
# not a true drop-in replacement for the original launcher/Wine (IS-2).
#
# -static-libgcc / -static-libstdc++ fold libgcc and libstdc++ in statically;
# plain -static additionally forces every other -lFOO (including -lmcfgthread,
# pulled in by GCC 16.2.0's default i686-w64-mingw32 link spec) to resolve
# against its .a archive instead of its .dll.a import-library counterpart when
# both exist side by side, which is the case for all three runtime libs here:
# libgcc.a / libgcc_s.a (gcc derivation, lib/gcc/.../16.2.0 and i686-w64-mingw32/lib)
# libstdc++.a (gcc derivation, i686-w64-mingw32/lib)
# libmcfgthread.a (mcfgthreads package, see _gk_mcfgthread_lib above)
# All three static archives were confirmed present in this pinned toolchain
# before relying on -static here (see task-2.log fix transcript); if a future
# toolchain pin drops one of them, -static will fail to link with a clear
# "cannot find -l<name>" error rather than silently falling back to the
# dynamic import library.
#
# Set via the *_INIT cache-seed variables (not CMAKE_*_LINKER_FLAGS directly):
# the toolchain file runs before project()/enable_language() establish the
# CMAKE_*_LINKER_FLAGS cache entries, so a plain set() here would be
# overwritten by CMake's own default-flags initialization; the _INIT variables
# are what CMake seeds those cache entries from, so every target (now and any
# added later) inherits this without each CMakeLists.txt repeating it.
set(CMAKE_EXE_LINKER_FLAGS_INIT "-L${_gk_mcfgthread_lib} -static-libgcc -static-libstdc++ -static")
set(CMAKE_SHARED_LINKER_FLAGS_INIT "-L${_gk_mcfgthread_lib} -static-libgcc -static-libstdc++ -static")
set(CMAKE_MODULE_LINKER_FLAGS_INIT "-L${_gk_mcfgthread_lib} -static-libgcc -static-libstdc++ -static")