Skip to content

build(windows): the ImageMagick closure links zlib.lib, not a bare z.lib - #822

Merged
transfix merged 1 commit into
masterfrom
fix/msvc-im-zlib-closure
Oct 26, 2026
Merged

transfix merged 1 commit into
masterfrom
fix/msvc-im-zlib-closure

Conversation

@transfix

Copy link
Copy Markdown
Owner

Fixes a local-Windows regression from #801 (5536b65): a native MSVC build with CVC_ENABLE_IMAGEMAGICK=ON stops linking cvc.dll.

LINK : fatal error LNK1181: cannot open input file 'z.lib'

Cause

#801 added "zlib|z" to the ImageMagick delegate closure in src/cvc/CMakeLists.txt. When CMake finds a pkg-config on a Windows box (MSYS2's C:/msys64/mingw64/bin/pkg-config.exe on PATH, which is normal on a dev machine), the steps go like this:

  1. pkg_check_modules(_im_dep_zlib ... IMPORTED_TARGET zlib) succeeds from cvcpkg's zlib.pc. The PKG_CONFIG_LIBDIR pin keeps pkg-config on the cvcpkg prefix, so the .pc file is the right one. Its Libs: line is -lz.
  2. FindPkgConfig then calls find_library(z), which fails (pkgcfg_lib__im_dep_zlib_z-NOTFOUND), because the MSVC import library is zlib.lib. FindPkgConfig keeps an unresolved -l name bare in _im_dep_zlib_LINK_LIBRARIES and in PkgConfig::_im_dep_zlib, but still reports _FOUND.
  3. The loop trusted _FOUND and hung that target off ImageMagick::MagickCore. On a fresh configure here, build.ninja had 314 bare z.lib link inputs, cvc.dll among them.

CI stays green because the GitHub windows-2022 image has no pkg-config. There the loop takes the find_library fallback, NAMES z finds nothing, and zlib is silently left out of the closure.

Fix

  • On MSVC, a pkg-config answer is used only when every entry in <prefix>_LINK_LIBRARIES resolved to an absolute path. Otherwise the loop takes the find_library fallback.
  • A closure entry can now list several library names. zlib's are z zlib. The fallback uses NAMES_PER_DIR, so the first directory that has the library under either name wins.

Unchanged:

  • The static-export handling: delegates still hang off the build-local ImageMagick::MagickCore. The generated cvcTargets.cmake names only ImageMagick::Magick++ / ImageMagick::MagickCore, with no zlib path and no PkgConfig::_im_dep_* (checked).
  • The consumer-side copy in cvcConfig.cmake.in, which isn't affected: it runs only for a static MagickCore and never asks pkg-config for the delegates.

Verification (sandipaws, native MSVC 14.4x, CMake 3.31.7, ./deps cvcpkg prefix)

Configure (fresh, no z.lib shim on LIB) _im_dep_zlib resolves to bare z.lib in build.ninja ninja cvc
master (197a0d6), MSYS2 pkg-config found bare z (pkgcfg_lib_..._z-NOTFOUND) 314 LNK1181 (seen in the tf_editor tree with this recipe)
this branch, MSYS2 pkg-config found deps/lib/zlib.lib (fallback) 0 305/305, bin/cvc.dll links
this branch, -DCMAKE_DISABLE_FIND_PACKAGE_PkgConfig=ON (like CI) deps/lib/zlib.lib 0 configure only; same cvc.dll link inputs as the row above

All the other delegates resolve to the same deps/lib/*.lib paths before and after. One translation unit's INCLUDES/FLAGS/DEFINES are byte-identical between master and this branch, so only the link changes.

Not run locally: Linux, macOS and wasm. The pkg-config change is gated on MSVC. The fallback changes there are limited to zlib's extra name and NAMES_PER_DIR, which only matters for entries with more than one name.

#801 added zlib to the ImageMagick delegate closure in src/cvc/CMakeLists.txt
as "zlib|z". On a native MSVC build that finds a pkg-config (MSYS2's
C:/msys64/mingw64/bin/pkg-config.exe on PATH; the PKG_CONFIG_LIBDIR pin keeps it
on the cvcpkg .pc files), pkg_check_modules(... zlib) succeeds from cvcpkg's
zlib.pc, whose Libs: line is `-lz`. FindPkgConfig then cannot find_library() a
`z` (pkgcfg_lib__im_dep_zlib_z-NOTFOUND), because the MSVC import library is
zlib.lib, and it keeps the unresolved name bare in _im_dep_zlib_LINK_LIBRARIES
and in PkgConfig::_im_dep_zlib. The loop trusted _FOUND and hung that target off
ImageMagick::MagickCore, so every link of cvc.dll asked link.exe for z.lib:

    LINK : fatal error LNK1181: cannot open input file 'z.lib'

CI stays green because the GitHub windows-2022 image has no pkg-config. The loop
takes the find_library fallback there, `NAMES z` finds nothing, and zlib is
silently left out of the closure.

On MSVC, the loop now uses a pkg-config answer only when every library in
<prefix>_LINK_LIBRARIES resolved to an absolute path. Otherwise it takes the
find_library fallback. Other toolchains keep today's behaviour, because their
linkers search for a bare name themselves. The wasm libxml-2.0.pc lists `-lm`,
which no sysroot file backs and emcc handles itself, so a stricter rule there
would move wasm's libxml2 off the pkg-config path #801 just made work.

A closure entry can now list several library names. zlib's are `z zlib`, and the
fallback uses NAMES_PER_DIR so the prefix's zlib.lib is not passed over for a
later directory that matches `z`. With or without pkg-config, cvc.dll now links
deps/lib/zlib.lib. The static-export handling is unchanged: delegates still hang
off the build-local ImageMagick::MagickCore and never reach cvcTargets.cmake.
@transfix

Copy link
Copy Markdown
Owner Author

Verified on sandipaws (Windows, MSVC 14.44, Ninja, shared Release, ImageMagick ON, MSYS2 pkg-config on PATH). Without this change cvc.dll fails with LNK1181 cannot open input file 'z.lib'. With it, and no z.lib alias on LIB, cvc.dll links, build.ninja has no bare z.lib left (only deps\lib\zlib.lib), and lbie_mesher_test passes 57/57.

@transfix
transfix merged commit 6d40579 into master Oct 26, 2026
24 checks passed
@transfix
transfix deleted the fix/msvc-im-zlib-closure branch October 29, 2026 20:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant