Skip to content

cmake: fix building with CMAKE_FIND_PACKAGE_PREFER_CONFIG=ON#20784

Closed
vszakats wants to merge 23 commits into
curl:masterfrom
vszakats:cm-bld-module
Closed

cmake: fix building with CMAKE_FIND_PACKAGE_PREFER_CONFIG=ON#20784
vszakats wants to merge 23 commits into
curl:masterfrom
vszakats:cm-bld-module

Conversation

@vszakats
Copy link
Copy Markdown
Member

@vszakats vszakats commented Mar 1, 2026

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:

Reported-by: Valerie Snyder
Fixes #20729


https://github.com/curl/curl/pull/20784/files?w=1

The CI confirms a number of fallout when this custom option is set:
https://github.com/curl/curl/actions/runs/22546213460 macOS
https://github.com/curl/curl/actions/runs/22546213435 Linux
https://github.com/curl/curl/actions/runs/22546213465 Windows

@vszakats vszakats changed the title [TEST] cmake: try building with -DCMAKE_FIND_PACKAGE_PREFER_CONFIG=ON cmake: fix building with -DCMAKE_FIND_PACKAGE_PREFER_CONFIG=ON Mar 1, 2026
@vszakats vszakats marked this pull request as draft March 1, 2026 15:49
@vszakats vszakats added the feature-window A merge of this requires an open feature window label Mar 2, 2026
@vszakats vszakats changed the title cmake: fix building with -DCMAKE_FIND_PACKAGE_PREFER_CONFIG=ON cmake: fix building & consuming with CMAKE_FIND_PACKAGE_PREFER_CONFIG=ON Mar 2, 2026
@vszakats
Copy link
Copy Markdown
Member Author

vszakats commented Mar 2, 2026

This may also be a way to add support to curl Find modules for using cmake CONFIGs
(in addition to existing pkg-config and raw/manual detection). Which in turn may
help with static builds. When using MODULE, it's guaranteed to hit curl's Find
module first without using CONFIGs, then the Find module may possibly use a
find_package() limited to CONFIGs, effectively "chaining" them. Then the curl
logic needs to create aliases to map the found targets to the CURL:: namespaced
ones, and possibly do other things to make those targets compatible with the
rest of the build. I haven't made tests, so it's all theory at this point.

Ref: #20013, #19117 and #19156.

vszakats added a commit that referenced this pull request Mar 2, 2026
- add support for separate provider / consumer cmake options in
  `find_package` tests. To help test more integration scenarios.
  Refs: #20784 #20729 #20764

- dump generated curl config files in `find_package` tests.
  (cmake CONFIG source, `libcurl.pc`, `curl-config`.

- test.sh: use `sha256sum` (was: `openssl`).

Closes #20773
@github-actions github-actions Bot added the CI Continuous Integration label Mar 2, 2026
@vszakats vszakats force-pushed the cm-bld-module branch 2 times, most recently from 5213f78 to d3fcf41 Compare March 2, 2026 15:40
@vszakats vszakats marked this pull request as ready for review March 2, 2026 15:47
@vszakats vszakats requested a review from Copilot March 2, 2026 17:28
@vszakats
Copy link
Copy Markdown
Member Author

vszakats commented Mar 2, 2026

@aisle-analyzer augment review

@aisle-research-bot
Copy link
Copy Markdown

aisle-research-bot Bot commented Mar 2, 2026

🔒 Aisle Security Analysis

✅ We scanned this PR and did not find any security vulnerabilities.

Aisle supplements but does not replace security review.


Analyzed PR: #20784 at commit 5122d1b

Last updated on: 2026-03-02T17:37:54Z

Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR adjusts curl’s CMake dependency discovery to behave correctly when users enable CMAKE_FIND_PACKAGE_PREFER_CONFIG=ON, ensuring curl’s build and exported CURLConfig.cmake consistently use curl’s bundled Find*.cmake modules (so expected CURL::<dep> targets are created).

Changes:

  • Force find_package(... MODULE ...) for dependencies that have curl-provided Find<Dep>.cmake modules (both in-tree build and exported config consumption).
  • Update the exported CMake/curl-config.in.cmake to use find_dependency(... MODULE) for these dependencies while preserving CONFIG-based behavior for OpenSSL and ZLIB.
  • Add a Windows distcheck job that builds and consumes curl with CMAKE_FIND_PACKAGE_PREFER_CONFIG=ON.

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated no comments.

File Description
CMakeLists.txt Forces MODULE-mode package discovery for curl-bundled Find modules so CURL::<dep> targets are reliably defined.
CMake/curl-config.in.cmake Forces MODULE-mode dependency discovery when consuming curl via find_package(CURL), avoiding CONFIG packages that don’t create CURL::<dep> targets.
CMake/Macros.cmake Updates curl_dependency_option() to prefer MODULE finds for curl-bundled deps, with an explicit exception for ZLIB.
.github/workflows/distcheck.yml Adds a Windows integration test that exercises build+consume under CMAKE_FIND_PACKAGE_PREFER_CONFIG=ON.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@augmentcode
Copy link
Copy Markdown

augmentcode Bot commented Mar 2, 2026

🤖 Augment PR Summary

Summary: This PR hardens curl’s CMake build/packaging against builds where CMAKE_FIND_PACKAGE_PREFER_CONFIG=ON would otherwise select upstream Config.cmake packages and skip curl’s own Find.cmake logic/targets.

Changes:

  • Force module-mode discovery (find_package(... MODULE)) for curl-managed dependencies in CMakeLists.txt and CMake/Macros.cmake (with ZLIB intentionally left flexible).
  • Update the installed curl CMake package (CMake/curl-config.in.cmake) to use find_dependency(... MODULE) for the same dependency set, ensuring expected CURL::* targets are created when consuming curl.
  • Add a distcheck workflow job to build and then consume curl via find_package with CMAKE_FIND_PACKAGE_PREFER_CONFIG=ON enabled (Windows matrix).

Technical Notes: This keeps curl’s custom Find* modules in the resolution path even when users prefer config packages globally, avoiding successful-but-incomplete dependency discovery that would omit required imported targets.

🤖 Was this summary useful? React with 👍 or 👎

Copy link
Copy Markdown

@augmentcode augmentcode Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review completed. No suggestions at this time.

Comment augment review to trigger a new review at any time.

@vszakats vszakats changed the title cmake: fix building & consuming with CMAKE_FIND_PACKAGE_PREFER_CONFIG=ON cmake: fix build & consume with CMAKE_FIND_PACKAGE_PREFER_CONFIG=ON Mar 2, 2026
@vszakats vszakats changed the title cmake: fix build & consume with CMAKE_FIND_PACKAGE_PREFER_CONFIG=ON cmake: fix building with CMAKE_FIND_PACKAGE_PREFER_CONFIG=ON Mar 2, 2026
@vszakats vszakats removed the feature-window A merge of this requires an open feature window label Mar 3, 2026
vszakats and others added 2 commits March 3, 2026 12:35
make sure to omit for ZLIB (no visible fallout in CI)
When using -DCMAKE_FIND_PACKAGE_PREFER_CONFIG=ON, the find_dependency()
and find_package() macros give preference to CMake config files (e.g.
`<prefix>/lib/cmake/libssh2/libssh2-config.cmake`) if found, over using
curl's `Find<Package>.cmake` file and results in different target names
such as Libssh2::libssh2 instead of CURL::libssh2.

Beyond the target naming discrepency, this also relies heavily on
correctness of those config files, which in my experience is a mixed
bag.

Using the MODULE parameter for `find_dependency()` and `find_package()`
will force CMake to use the `Find<Package>.cmake` module instead of
using the config file.

Fixes 20729
Follow-up to 16f073e curl#16973
vszakats added 16 commits March 3, 2026 12:35
This reverts commit 3c5c511.

https://github.com/curl/curl/actions/runs/22584370194/job/65424999010?pr=20784

Not sure why it failed when setting the setting via CMakeLists.txt.
Ah, it was on macOS.
This reverts commit bc1582e.

This settings causes `find_package(OpenSSL)` to not find Homebrew's
OpenSSL. Not sure why. Unrelated to this PR, just wanted to confirm.

https://github.com/curl/curl/actions/runs/22584940990/job/65427083479?pr=20784
…to suppoert it when consumers"""

This reverts commit b86535b.
… to suppoert it when consumers"""

This reverts commit b357bdf.
@vszakats vszakats closed this in 91e06fd Mar 3, 2026
@vszakats vszakats deleted the cm-bld-module branch March 3, 2026 15:35
vszakats added a commit to vszakats/curl that referenced this pull request Mar 4, 2026
- [ ] how to control priority? (honor CMAKE's PREFER_CONFIG option?)
- [ ] switch default to OFF, initially.
- [ ] make a list of deps that support this (and since which versions)
- [ ] add for the rest
- [ ] test locally?
- [ ] test in CI?

This may also be a way to add support to curl Find modules for using
cmake Configs (in addition to existing `pkg-config` and raw/manual
detection). Which in turn may help with static builds. When using
MODULE, it's guaranteed to hit curl's Find module first without using
Configs, then the Find module may possibly use a `find_package()`
limited to Configs, effectively "chaining" them. Then the curl logic
needs to create aliases to map the found targets to the `CURL::`
namespaced ones, and possibly do other things to make those targets
compatible with the rest of the build. I have made minimal tests, so
it's mostly theory at this point.

curl#20784 (comment)

Refs:
curl#20013
curl#19117
curl#19156
vszakats added a commit to vszakats/curl that referenced this pull request Mar 4, 2026
- [ ] how to control priority? (honor CMAKE's PREFER_CONFIG option?)
- [ ] switch default to OFF, initially.
- [ ] make a list of deps that support this (and since which versions)
- [ ] add for the rest
- [ ] test locally?
- [ ] test in CI?

This may also be a way to add support to curl Find modules for using
cmake Configs (in addition to existing `pkg-config` and raw/manual
detection). Which in turn may help with static builds. When using
MODULE, it's guaranteed to hit curl's Find module first without using
Configs, then the Find module may possibly use a `find_package()`
limited to Configs, effectively "chaining" them. Then the curl logic
needs to create aliases to map the found targets to the `CURL::`
namespaced ones, and possibly do other things to make those targets
compatible with the rest of the build. I have made minimal tests, so
it's mostly theory at this point.

curl#20784 (comment)

Refs:
curl#20013
curl#19117
curl#19156
vszakats added a commit to vszakats/curl that referenced this pull request Mar 5, 2026
- [ ] how to control priority? (honor CMAKE's PREFER_CONFIG option?)
- [ ] switch default to OFF, initially.
- [ ] make a list of deps that support this (and since which versions)
- [ ] add for the rest
- [ ] test locally?
- [ ] test in CI?

This may also be a way to add support to curl Find modules for using
cmake Configs (in addition to existing `pkg-config` and raw/manual
detection). Which in turn may help with static builds. When using
MODULE, it's guaranteed to hit curl's Find module first without using
Configs, then the Find module may possibly use a `find_package()`
limited to Configs, effectively "chaining" them. Then the curl logic
needs to create aliases to map the found targets to the `CURL::`
namespaced ones, and possibly do other things to make those targets
compatible with the rest of the build. I have made minimal tests, so
it's mostly theory at this point.

curl#20784 (comment)

Refs:
curl#20013
curl#19117
curl#19156
vszakats added a commit to vszakats/curl that referenced this pull request Mar 5, 2026
- [ ] how to control priority? (honor CMAKE's PREFER_CONFIG option?)
- [ ] switch default to OFF, initially.
- [ ] make a list of deps that support this (and since which versions)
- [ ] add for the rest
- [ ] test locally?
- [ ] test in CI?

This may also be a way to add support to curl Find modules for using
cmake Configs (in addition to existing `pkg-config` and raw/manual
detection). Which in turn may help with static builds. When using
MODULE, it's guaranteed to hit curl's Find module first without using
Configs, then the Find module may possibly use a `find_package()`
limited to Configs, effectively "chaining" them. Then the curl logic
needs to create aliases to map the found targets to the `CURL::`
namespaced ones, and possibly do other things to make those targets
compatible with the rest of the build. I have made minimal tests, so
it's mostly theory at this point.

curl#20784 (comment)

Refs:
curl#20013
curl#19117
curl#19156
vszakats added a commit to vszakats/curl that referenced this pull request Mar 6, 2026
- [ ] how to control priority? (honor CMAKE's PREFER_CONFIG option?)
- [ ] switch default to OFF, initially.
- [ ] make a list of deps that support this (and since which versions)
- [ ] add for the rest
- [ ] test locally?
- [ ] test in CI?

This may also be a way to add support to curl Find modules for using
cmake Configs (in addition to existing `pkg-config` and raw/manual
detection). Which in turn may help with static builds. When using
MODULE, it's guaranteed to hit curl's Find module first without using
Configs, then the Find module may possibly use a `find_package()`
limited to Configs, effectively "chaining" them. Then the curl logic
needs to create aliases to map the found targets to the `CURL::`
namespaced ones, and possibly do other things to make those targets
compatible with the rest of the build. I have made minimal tests, so
it's mostly theory at this point.

curl#20784 (comment)

Refs:
curl#20013
curl#19117
curl#19156
vszakats added a commit to vszakats/curl that referenced this pull request Mar 9, 2026
- [ ] how to control priority? (honor CMAKE's PREFER_CONFIG option?)
- [ ] switch default to OFF, initially.
- [ ] make a list of deps that support this (and since which versions)
- [ ] add for the rest
- [ ] test locally?
- [ ] test in CI?

This may also be a way to add support to curl Find modules for using
cmake Configs (in addition to existing `pkg-config` and raw/manual
detection). Which in turn may help with static builds. When using
MODULE, it's guaranteed to hit curl's Find module first without using
Configs, then the Find module may possibly use a `find_package()`
limited to Configs, effectively "chaining" them. Then the curl logic
needs to create aliases to map the found targets to the `CURL::`
namespaced ones, and possibly do other things to make those targets
compatible with the rest of the build. I have made minimal tests, so
it's mostly theory at this point.

curl#20784 (comment)

Refs:
curl#20013
curl#19117
curl#19156
vszakats added a commit to vszakats/curl that referenced this pull request Mar 16, 2026
- [ ] how to control priority? (honor CMAKE's PREFER_CONFIG option?)
- [ ] switch default to OFF, initially.
- [ ] make a list of deps that support this (and since which versions)
- [ ] add for the rest
- [ ] test locally?
- [ ] test in CI?

This may also be a way to add support to curl Find modules for using
cmake Configs (in addition to existing `pkg-config` and raw/manual
detection). Which in turn may help with static builds. When using
MODULE, it's guaranteed to hit curl's Find module first without using
Configs, then the Find module may possibly use a `find_package()`
limited to Configs, effectively "chaining" them. Then the curl logic
needs to create aliases to map the found targets to the `CURL::`
namespaced ones, and possibly do other things to make those targets
compatible with the rest of the build. I have made minimal tests, so
it's mostly theory at this point.

curl#20784 (comment)

Refs:
curl#20013
curl#19117
curl#19156
vszakats added a commit to vszakats/curl that referenced this pull request Mar 21, 2026
- [ ] how to control priority? (honor CMAKE's PREFER_CONFIG option?)
- [ ] switch default to OFF, initially.
- [ ] make a list of deps that support this (and since which versions)
- [ ] add for the rest
- [ ] test locally?
- [ ] test in CI?

This may also be a way to add support to curl Find modules for using
cmake Configs (in addition to existing `pkg-config` and raw/manual
detection). Which in turn may help with static builds. When using
MODULE, it's guaranteed to hit curl's Find module first without using
Configs, then the Find module may possibly use a `find_package()`
limited to Configs, effectively "chaining" them. Then the curl logic
needs to create aliases to map the found targets to the `CURL::`
namespaced ones, and possibly do other things to make those targets
compatible with the rest of the build. I have made minimal tests, so
it's mostly theory at this point.

curl#20784 (comment)

Refs:
curl#20013
curl#19117
curl#19156
vszakats added a commit that referenced this pull request Mar 21, 2026
After limiting `find_package()`/`find_dependency()` calls to curl local
Find modules via the `MODULES` keyword, it became possible to detect
dependencies via CMake Configs from within those local Find modules, by
calling `find_package()` again with the `CONFIG` keyword. This patch
implements this. Then maps detection results to the result variables and
curl-specific imported targets the rest of the build expects.

Also honor recently introduced `*_USE_STATIC_LIBS` (experimental) flags
to map to the static target when requested.

This adds CMake Configs as an alternative to the existing `pkg-config`
and `find_path()`/`find_library()` auto-detection methods.

Enabled by default for MSVC, outside vcpkg and when not cross-building.
To enable for other cases, or override the default, you can use
`-DCURL_USE_CMAKECONFIG=ON` or `OFF`.

When enabled, Config detection happens after `pkg-config` and before
`find_path()`/`find_library()`. Using CMake's built-in options, you may
also manually point to the absolute directory holding Config files:

`Libssh2_DIR`, `MbedTLS_DIR`, `NGHTTP2_DIR`, `NGHTTP3_DIR`,
`NGTCP2_DIR` v1.19.0+ (with non-fork OpenSSL only), `Zstd_DIR` v1.4.5+

E.g. `-DMbedTLS_DIR=/path/to/mbedtls/lib/cmake/MbedTLS`

These dependencies typically need to be built with CMake to support
this.

Tagged as experimental.

Refs:
#20013 #19156 #19117
#20784 (comment)

Depends-on: fad1eba #20840
Follow-up to 91e06fd #20784
Follow-up to 26c39d8 #20015

Closes #20814
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

build CI Continuous Integration cmake

Development

Successfully merging this pull request may close these issues.

CMake Error using lib/cmake/CURL/CURLConfig.cmake when statically linked with nghttp2, libssh2

3 participants