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

@ -71,9 +71,17 @@ macro(curl_dependency_option _option_name _find_name _desc_name)
set_property(CACHE ${_option_name} PROPERTY STRINGS "AUTO" "ON" "OFF")
if(${_option_name} STREQUAL "AUTO")
find_package(${_find_name})
if(_find_name STREQUAL "ZLIB")
find_package(${_find_name})
else()
find_package(${_find_name} MODULE)
endif()
elseif(${_option_name})
find_package(${_find_name} REQUIRED)
if(_find_name STREQUAL "ZLIB")
find_package(${_find_name} REQUIRED)
else()
find_package(${_find_name} MODULE REQUIRED)
endif()
else()
string(TOUPPER "${_find_name}" _find_name_upper)
set(${_find_name}_FOUND OFF) # cmake-lint: disable=C0103

View file

@ -62,89 +62,89 @@ set(CMAKE_MODULE_PATH ${CMAKE_CURRENT_LIST_DIR} ${CMAKE_MODULE_PATH})
set(_curl_libs "")
if("@HAVE_BROTLI@")
find_dependency(Brotli)
find_dependency(Brotli MODULE)
list(APPEND _curl_libs CURL::brotli)
endif()
if("@USE_ARES@")
find_dependency(Cares)
find_dependency(Cares MODULE)
list(APPEND _curl_libs CURL::cares)
endif()
if("@HAVE_GSSAPI@")
find_dependency(GSS)
find_dependency(GSS MODULE)
list(APPEND _curl_libs CURL::gss)
endif()
if("@USE_BACKTRACE@")
find_dependency(Libbacktrace)
find_dependency(Libbacktrace MODULE)
list(APPEND _curl_libs CURL::libbacktrace)
endif()
if("@USE_GSASL@")
find_dependency(Libgsasl)
find_dependency(Libgsasl MODULE)
list(APPEND _curl_libs CURL::libgsasl)
endif()
if(NOT "@USE_WIN32_LDAP@" AND NOT "@CURL_DISABLE_LDAP@")
find_dependency(LDAP)
find_dependency(LDAP MODULE)
list(APPEND _curl_libs CURL::ldap)
endif()
if("@HAVE_LIBIDN2@")
find_dependency(Libidn2)
find_dependency(Libidn2 MODULE)
list(APPEND _curl_libs CURL::libidn2)
endif()
if("@USE_LIBPSL@")
find_dependency(Libpsl)
find_dependency(Libpsl MODULE)
list(APPEND _curl_libs CURL::libpsl)
endif()
if("@USE_LIBRTMP@")
find_dependency(Librtmp)
find_dependency(Librtmp MODULE)
list(APPEND _curl_libs CURL::librtmp)
endif()
if("@USE_LIBSSH@")
find_dependency(Libssh)
find_dependency(Libssh MODULE)
list(APPEND _curl_libs CURL::libssh)
endif()
if("@USE_LIBSSH2@")
find_dependency(Libssh2)
find_dependency(Libssh2 MODULE)
list(APPEND _curl_libs CURL::libssh2)
endif()
if("@USE_LIBUV@")
find_dependency(Libuv)
find_dependency(Libuv MODULE)
list(APPEND _curl_libs CURL::libuv)
endif()
if("@USE_MBEDTLS@")
find_dependency(MbedTLS)
find_dependency(MbedTLS MODULE)
list(APPEND _curl_libs CURL::mbedtls)
endif()
if("@USE_NGHTTP2@")
find_dependency(NGHTTP2)
find_dependency(NGHTTP2 MODULE)
list(APPEND _curl_libs CURL::nghttp2)
endif()
if("@USE_NGHTTP3@")
find_dependency(NGHTTP3)
find_dependency(NGHTTP3 MODULE)
list(APPEND _curl_libs CURL::nghttp3)
endif()
if("@USE_NGTCP2@")
find_dependency(NGTCP2)
find_dependency(NGTCP2 MODULE)
list(APPEND _curl_libs CURL::ngtcp2)
endif()
if("@USE_GNUTLS@")
find_dependency(GnuTLS)
find_dependency(GnuTLS MODULE)
list(APPEND _curl_libs CURL::gnutls)
find_dependency(Nettle)
find_dependency(Nettle MODULE)
list(APPEND _curl_libs CURL::nettle)
endif()
if("@USE_QUICHE@")
find_dependency(Quiche)
find_dependency(Quiche MODULE)
list(APPEND _curl_libs CURL::quiche)
endif()
if("@USE_RUSTLS@")
find_dependency(Rustls)
find_dependency(Rustls MODULE)
list(APPEND _curl_libs CURL::rustls)
endif()
if("@USE_WOLFSSL@")
find_dependency(WolfSSL)
find_dependency(WolfSSL MODULE)
list(APPEND _curl_libs CURL::wolfssl)
endif()
if("@HAVE_ZSTD@")
find_dependency(Zstd)
find_dependency(Zstd MODULE)
list(APPEND _curl_libs CURL::zstd)
endif()