From 278e04ebd6e5b5d0e96d77e1fc839af3ccd2b3ce Mon Sep 17 00:00:00 2001 From: Selim Sandal <49725809+selimsandal@users.noreply.github.com> Date: Tue, 17 Feb 2026 22:05:53 +0300 Subject: [PATCH] windows: make zlib static by default --- CMakeLists.txt | 47 ++++++++++++++++++++++++++++++++++--------- doc/guide/install.rst | 9 +++++++++ 2 files changed, 47 insertions(+), 9 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 53a9c1c..4bed17b 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -18,6 +18,7 @@ option(ENABLE_OPTIM "Enable build with -O3 optimization level" option(BUILD_STATIC "Whether or not to build with static libraries" OFF) option(USE_PKGCONFIG "Use pkgconfig to find libraries" ON) option(LINK_CMAKE_THREADS "Use CMake find_package to link the threading library" OFF) +option(WINDOWS_STATIC_ZLIB "Link zlib statically for Windows builds" ON) if (${CMAKE_SYSTEM_NAME} MATCHES "Windows") set(ENABLE_UDEV OFF) @@ -223,16 +224,44 @@ if (USE_PKGCONFIG) endif() # zlib support (gzip) - pkg_check_modules(ZLIB zlib) - if (NOT ZLIB_FOUND) - # try zlib-ng - pkg_check_modules(ZLIB zlib-ng) - if (ZLIB_FOUND) - add_definitions(-DHAS_ZLIBNG) - else() - message(FATAL_ERROR "Could find ZLIB") + if (${CMAKE_SYSTEM_NAME} MATCHES "Windows") + if (WINDOWS_STATIC_ZLIB) + set(ZLIB_USE_STATIC_LIBS ON) endif() - endif(NOT ZLIB_FOUND) + + # Use CMake zlib finder for Windows builds to avoid host pkg-config contamination + # during cross-compilation and to allow static linking selection. + find_package(ZLIB QUIET) + if (NOT ZLIB_FOUND) + if (windows_crosscompile) + message(FATAL_ERROR + "zlib for Windows target not found. Install MinGW zlib development files " + "(for example: libz-mingw-w64-dev on Debian/Ubuntu or mingw64-zlib on Fedora).") + else() + message(FATAL_ERROR + "zlib for Windows build not found. Install zlib development files for your " + "Windows toolchain.") + endif() + endif() + + if (WINDOWS_STATIC_ZLIB AND ZLIB_LIBRARIES MATCHES "\\.dll\\.a$") + message(FATAL_ERROR + "Static zlib requested (WINDOWS_STATIC_ZLIB=ON), but CMake selected " + "'${ZLIB_LIBRARIES}'. Install static zlib (for example mingw64-zlib-static) " + "or set -DWINDOWS_STATIC_ZLIB=OFF.") + endif() + else() + pkg_check_modules(ZLIB zlib) + if (NOT ZLIB_FOUND) + # try zlib-ng + pkg_check_modules(ZLIB zlib-ng) + if (ZLIB_FOUND) + add_definitions(-DHAS_ZLIBNG) + else() + message(FATAL_ERROR "Could find ZLIB") + endif() + endif(NOT ZLIB_FOUND) + endif() if (ENABLE_UDEV) pkg_check_modules(LIBUDEV libudev) diff --git a/doc/guide/install.rst b/doc/guide/install.rst index 27fa0de..757bc7a 100644 --- a/doc/guide/install.rst +++ b/doc/guide/install.rst @@ -216,6 +216,7 @@ The build system will automatically download and build the required dependencies sudo apt install \ mingw-w64 \ + libz-mingw-w64-dev \ cmake \ pkg-config \ p7zip-full @@ -247,6 +248,13 @@ The build system will automatically download and build the required dependencies The resulting ``openFPGALoader.exe`` will be a statically-linked executable that only depends on standard Windows system DLLs (KERNEL32, msvcrt, WS2_32). +.. NOTE:: + ``zlib`` for the Windows target is required. Configuration fails if it is + missing (install ``libz-mingw-w64-dev`` on Debian/Ubuntu or + ``mingw64-zlib`` on Fedora/RHEL/Rocky). + ``zlib`` is linked statically by default on Windows builds + (``-DWINDOWS_STATIC_ZLIB=ON``). + **Optional: Strip debug symbols to reduce size:** .. code-block:: bash @@ -257,6 +265,7 @@ that only depends on standard Windows system DLLs (KERNEL32, msvcrt, WS2_32). - ``-DCROSS_COMPILE_DEPS=OFF`` - Disable automatic dependency download (use system libraries) - ``-DENABLE_CMSISDAP=ON`` - Enable CMSIS-DAP support (requires manually providing hidapi) +- ``-DWINDOWS_STATIC_ZLIB=OFF`` - Allow dynamic zlib linkage (produces ``zlib1.dll`` runtime dependency) Common ======