Skip to content

cmake: resolve imported targets recursively when generating libcurl.pc#20840

Closed
vszakats wants to merge 1 commit intocurl:masterfrom
vszakats:cm-pc-resolve-recurse
Closed

cmake: resolve imported targets recursively when generating libcurl.pc#20840
vszakats wants to merge 1 commit intocurl:masterfrom
vszakats:cm-pc-resolve-recurse

Conversation

@vszakats
Copy link
Copy Markdown
Member

@vszakats vszakats commented Mar 6, 2026

To allow simplifying the binutils ld hack, by chaining the original
imported target to curl's local duplicate target. Also to allow linking
to dependencies' native imported targets via their CMake Configs, which
will always be hooked up to a CURL:: interface, and may also be
chained upstream.

Fixing (seen on Linux with simplified binutils hack via #20839):

 Requires:
 Requires.private: libzstd openssl zlib
 Libs: -L${libdir} -lcurl
-Libs.private:  -lcrypto -lssl -lz -lzstd
+Libs.private:  -lOpenSSL::Crypto -lZLIB::ZLIB -lcrypto -lssl -lz -lzstd
 Cflags: -I${includedir}
 Cflags.private: -DCURL_STATICLIB
Error: Process completed with exit code

Ref: https://github.com/curl/curl/actions/runs/22768301699/job/66041980258?pr=20839

Note this makes it possible to run into an infinite loop because CMake
allows cyclic dependencies. It isn't added by curl's CMake script nor by
any dependencies as defined by default, but may happen in theory with
custom-created targets. In such case CMake automatically stops with
an error at 1000 iterations. I find it overkill to add custom protection
for it.

Cherry-picked from #20814
Cherry-picked from #20839

To allow simplifying the binutils ld hack, by chaining the original
imported target to curl's local duplicate target. Also to allow linking
to dependencies' native imported targes via their CMake Configs, which
may similarly be chained by default.

Fixing (seen on Linux with simplified binutils hack via curl#20839):
```
 Requires:
 Requires.private: libzstd openssl zlib
 Libs: -L${libdir} -lcurl
-Libs.private:  -lcrypto -lssl -lz -lzstd
+Libs.private:  -lOpenSSL::Crypto -lZLIB::ZLIB -lcrypto -lssl -lz -lzstd
 Cflags: -I${includedir}
 Cflags.private: -DCURL_STATICLIB
Error: Process completed with exit code
```
Ref: https://github.com/curl/curl/actions/runs/22768301699/job/66041980258?pr=20839

Cherry-picked from curl#20814
Cherry-picked from curl#20839
@vszakats vszakats changed the title cmake: resolve imported targets recursively for .pc file generation cmake: resolve imported targets recursively for generating libcurl.pc Mar 6, 2026
@vszakats vszakats changed the title cmake: resolve imported targets recursively for generating libcurl.pc cmake: resolve imported targets recursively when generating libcurl.pc Mar 9, 2026
@vszakats vszakats closed this in fad1eba Mar 16, 2026
@vszakats vszakats deleted the cm-pc-resolve-recurse branch March 16, 2026 10:58
vszakats added a commit that referenced this pull request Mar 16, 2026
Instead hook up the upstream target name as-is to the local wrapper
target.

To:
- make the hack work regardless of how the upstream target was created.
- make it work the same way in `curl-config.cmake`.
  Before this patch it had no guard for `IMPORTED` targets even though
  `find_dependencies()` is not guaranteed to create the target expected
  if it already existed and was potentially created differently.

Depends-on: #20840
Follow-up to 4f1646e #20486 #20419

Closes #20839
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

Development

Successfully merging this pull request may close these issues.

1 participant