cmake: fix building with CMAKE_FIND_PACKAGE_PREFER_CONFIG=ON

This CMake global custom option tells it to find dependencies as cmake
Configs first, and only then look for `Find*` modules. This may result
in `find_package()` succeeding, but without actually creating `CURL::*`
imported targets the curl build scripts are expecting.

For dependencies with curl-specific, local, `Find*` modules, we always
want to use them, via the module detection method, and never a
Config-based detection. Ensure this by passing the `MODULE` option to
`find_package()` and `find_dependency()` to make them use `Find*`
modules unconditionally, making them work as expected with the
`CMAKE_FIND_PACKAGE_PREFER_CONFIG=ON` option set.

curl uses local Find modules for all dependencies except OpenSSL and
ZLIB. The latter two keep using either CMake's built-in Find modules or
Config method as before this patch.

Also:

- apply the same change to `curl-config.cmake`. To fix consuming curl
  with this option set.
  Authored-by: Valerie Snyder
  Ref: #20764
  Follow-up to 16f073ef49 #16973

- GHA/distcheck: add a job testing both building and consuming curl with
  this option set. (takes 15 seconds)
  Use custom NGHTTP2 configuration for an extra twist (not required
  to trigger this issue.)
  Follow-up to fcde8d7e37 #20773

Reported-by: Valerie Snyder
Fixes #20729

Closes #20784
This commit is contained in:
Viktor Szakats 2026-03-01 16:19:33 +01:00
parent 3d708e239b
commit 91e06fde1b
No known key found for this signature in database
4 changed files with 64 additions and 50 deletions

View file

@ -319,6 +319,12 @@ jobs:
- name: 'via find_package (C++)'
if: ${{ contains(matrix.image, 'ubuntu') }}
run: TEST_CMAKE_FLAGS=-DTEST_CPP=ON ./tests/cmake/test.sh find_package ${TESTOPTS} -DCURL_USE_OPENSSL=ON
- name: 'via find_package (PREFER_CONFIG=ON)'
if: ${{ contains(matrix.image, 'windows') }}
run: |
export TEST_CMAKE_FLAGS_PROVIDER='-DCMAKE_FIND_PACKAGE_PREFER_CONFIG=ON -DCURL_ZSTD=OFF -DNGHTTP2_INCLUDE_DIR=C:/msys64/mingw64/include -DNGHTTP2_LIBRARY=C:/msys64/mingw64/lib/libnghttp2.dll.a'
export TEST_CMAKE_FLAGS_CONSUMER="${TEST_CMAKE_FLAGS_PROVIDER}"
./tests/cmake/test.sh find_package ${TESTOPTS} -DCURL_USE_OPENSSL=ON
- name: 'via ExternalProject (old cmake)'
if: ${{ contains(matrix.image, 'ubuntu') }}