Skip to content

cmake: make Find modules use INTERFACE - #1535

Closed
vszakats wants to merge 44 commits into
libssh2:masterfrom
vszakats:cm-interfaces
Closed

vszakats wants to merge 44 commits into
libssh2:masterfrom
vszakats:cm-interfaces

Conversation

@vszakats

@vszakats vszakats commented Feb 3, 2025 •

Copy link
Copy Markdown
Member
  • move dependency properties (libs, libdirs, C flags, header dirs,
    pkg-config module names) from global lists to imported target
    INTERFACE properties. Rework FInd modules to return their results
    like this and update the libssh2 build process to use it. It makes
    Find modules re-usable from the cmake-config script by libssh2
    consumers, to integrate with libssh2 dependencies.

  • define libssh2 dependencies as "imported targets" by the name:
    libssh2::<depname>, e.g. libssh2::libgcrypt.

  • cmake-config: add fall-back logic for CMake without
    CMP0099 (v3.17 2020-03-20) to set lib directories.

  • generate libssh2.pc based on imported target properties (instead of
    global lists).

  • add target property dump debug function.

  • ci/GHA: also test cmake integration on macOS.

Follow-up to 96d7f40 #1534


w/o ws https://github.com/libssh2/libssh2/pull/1535/files?w=1

Continuing #1534 from:
#1534 (comment)

  • move raw C flags into target props.
    • drop include dirs also passed as CFLAGS (as -I, not -isystem) (when detected via pkg-config)
  • figure out how to pass libdirs to consumer projects.
  • how to store dependency version as target prop?
  • how to store pc module name as target prop?
  • ideally make all Find result variables private (lowercase-underscore).
  • finalize target names. For now it's: libssh2::<DependencyName>.
  • assemble pc module list in a central place like libdirs/libs, for libssh2.pc.
    It can now be extracted from target props (TODO: OpenSSL, ZLIB).
  • add integration tests on macOS.
  • split off non-strictly related commits.

@vszakats

vszakats commented Feb 4, 2025 •

Copy link
Copy Markdown
Member Author

The fallout mentioned in #1534 (comment) doesn't replicate in CI, presumably because it's on Linux where the dependency is at a default, system, location. While in my env it requires the -L, and it's not there.

(edit: The issue is limited to static consumption.)

The rest looks about fine. It's basically moving the Find module custom result variables into (mostly) standardized target properties. This makes it possible to let CMake apply these properties automatically to targets. It allows to set custom properties, which I used for the pkg-config module name.

@dg0yt

dg0yt commented Feb 4, 2025

Copy link
Copy Markdown

Wrt #1534 (comment):

On target names and namespaces:
AFAICT namespace can only be used for IMPORTED targets and for ALIAS targets, i.e. for targets which are not direct citizens of your project.
This also applies to INTERFACE libraries: If they are owned by your project, you can't add a namespace directly. (But you can export or alias them, adding namespace in these steps.)

On INTERFACE_LINK_DIRECTORIES for INTERFACE targets:
Interface targets must carry interface link libraries (targets) in order to resolve transitive usage requirements. So they must export them. Now the question is how much CMake really generalized the concept of carrying transitive usage requirements vs. specifically targeting header-only libraries. I admit that I don't know the answer.

@vszakats

vszakats commented Feb 4, 2025

Copy link
Copy Markdown
Member Author

I tried looking at how to export them, but so far no results. Probably the next is peeking into the source code. I don't have high hopes. It's sad because otherwise the concept makes dep handling on the whole more organized.

My impression is that CMake's concept of lib deps is rooted from Windows, and this abstraction doesn't work well elsewhere.

@vszakats

vszakats commented Feb 4, 2025 •

Copy link
Copy Markdown
Member Author

Extended integration tests to macOS, where the issue replicates.

An old thread complaining about this: https://cmake.org/pipermail/cmake/2017-August/066086.html
The reaction (or lack thereof) from CMake also isn't giving much hope.

Found this policy describing something close to what we experience here:
https://cmake.org/cmake/help/latest/policy/CMP0099.html
Requires CMake 3.17 (2020-03-20):

It needs to be set by the consumer project:

if(POLICY CMP0099)  # new in CMake 3.17, defaults to OLD
  cmake_policy(SET CMP0099 NEW)
endif()

or opt-in via ...3.17 in cmake_minimum_required().

Also it turns out INTERFACE_LINK_DIRECTORIES requires CMake 3.13 (2018-10-21).
(We aim for 3.7 (2016-11-11), as in curl, for old-linux compatibility.)
This makes this PR need more workarounds for old CMake versions.

I'll now push a terrible manual workaround to fill in for CMP0099.

@vszakats

vszakats commented Feb 4, 2025 •

Copy link
Copy Markdown
Member Author

I tend to consider the namespace pollution issue unresolvable.

CMake is a hot mess of namespace pollution. Avoiding it fully seems
impossible, at best impractical and unusual. Some of the modifications would
break compatibility with existing control variables, which had broken
namespacing rules a decade+ ago.

In theory LIBSSH2_MBEDTLS_INCLUDE_DIR sounds reasonable.
In practice, less so. Some name collisions are probably used in accidental,
useful/convenient ways.

As a fan of namespacing, I'm calling defeat here. Instead of inflicting
a lot of pain on everyone by spilling countless more hours into this
to break things, I'd rather go by fixing such issues if explicitly reported.

Other than that, do what's reasonably possible, without going overboard.

@vszakats vszakats changed the title cmake: try using INTERFACE in Find modules cmake: make Find modules use INTERFACE Feb 4, 2025
@vszakats
vszakats marked this pull request as ready for review February 4, 2025 17:01
vszakats added a commit that referenced this pull request Feb 5, 2025
To avoid cmake warning:
```
CMake Warning:
  Manually-specified variables were not used by the project:

    ENABLE_ECDSA_WINCNG
```
https://github.com/libssh2/libssh2/actions/runs/13126062299/job/36622533686?pr=1535#step:3:88

Follow-up to 3f98bfb #1368
Cherry-picked from #1535
vszakats added a commit that referenced this pull request Feb 5, 2025
vszakats added a commit that referenced this pull request Feb 5, 2025
Make sure to keep any previously added settings. For good measure;
this hasn't been an issue with libssh2 builds yet.

Cherry-picked from #1535
vszakats added a commit that referenced this pull request Feb 5, 2025
- drop adding redundant ZLIB manual include headers.
  It's done now via `ZLIB::ZLIB`.
  Follow-up to 96d7f40 #1534

- src: replace missed `ZLIB_LIBRARIES` with `ZLIB::ZLIB`.
  Follow-up to 96d7f40 #1534

- src: drop always true `ZLIB_FOUND` check.

Cherry-picked from #1535
@vszakats
vszakats force-pushed the cm-interfaces branch 4 times, most recently from daf870c to c7ec656 Compare February 7, 2025 13:13
@vszakats

vszakats commented Mar 15, 2025 •

Copy link
Copy Markdown
Member Author

With the changes made after my last comment, that is, moving module
properties under namespaced imported targets, I think it does the trick
to not pollute the namespace more than necessary.

I plan to merge this soon.

@vszakats vszakats closed this in df0563a Mar 19, 2025
@vszakats
vszakats deleted the cm-interfaces branch March 19, 2025 17:32
vszakats added a commit that referenced this pull request Apr 14, 2025
- drop `VERSION` target property for cmake <3.19 compatibility
  ```
  CMake Error at CMake/Find*.cmake:90 (set_target_properties):
    INTERFACE_LIBRARY targets may only have whitelisted properties.
    The property "VERSION" is not allowed.
  ```
- move custom target property to the `INTERFACE_` namespace
  for cmake <3.19 compatibility. (To avoid same error as above)
- fix forwarding multiple `CFLAGS`, when detected via `pkg-config`.
- restore support for `-framework` and raw libs when processing
  the internal lib list for generating `libssh2.pc`. For good measure,
  at the moment libssh2 doesn't depend on a Framework.
- limit `libssh2_dumptargetprops()` to cmake 3.19+. It doesn't work with
  older versions.

Issues found while applying this change to curl. They did not surface in
libssh2 CI.

Follow-up to df0563a #1535

Closes #1571
vszakats added a commit that referenced this pull request Apr 27, 2025
- fix `add_subdirectory` builds for old CMake versions.
- libssh2-config.cmake: fix to set CMP0099 for CMake 3.17+ only.
- libssh2-config.cmake: generalize code to support any number of deps.
  (mainly to sync with curl.)
- libssh2-config.cmake: bind dependencies to the static libssh2 only.

Follow-up to a0d8529 #1571
Follow-up to df0563a #1535

Closes #1581
vszakats added a commit to vszakats/libssh2 that referenced this pull request Jul 13, 2025
The `libssh2.pc` generator logic automatically adds `-lws2_32` while
parsing `LIBSSH2_LIBS`, which contains this lib already. Then discard
the duplicate.

This change introduces a change in the position of `ws2_32` within
the lib list.

This order might in cases by significant, but:

- libssh2 no longer links against `libssl`, which was the library
  also referencing `ws2_32` and breaking picky binutils `ld` linker
  when not passed in strict dependency order.
  Ref: c84745e libssh2#1128

- since switching to INTERFACE targets, cmake messes up the lib order
  anyway, adding `OpenSSL:Crypto` last, instead of `ws2_32`. This did
  not seem to cause an issue so far.
  Ref: df0563a libssh2#1535

Ref: 33b6d5f libssh2#827
Ref: 31fb886 libssh2#811
vszakats added a commit that referenced this pull request Jul 13, 2025
The `libssh2.pc` generator logic automatically adds `-lws2_32` while
parsing `LIBSSH2_LIBS`, which contains this lib already. Then discard
the duplicate.

This patch introduces a change in the position of `ws2_32` within
the lib list advertised via `libssh2.pc` for static builds.

This order might in cases by significant, but:

- libssh2 no longer links against `libssl`, which was the library
  also referencing `ws2_32` and breaking picky binutils `ld` linker
  when not passed in strict dependency order.
  Ref: c84745e #1128

- since switching to INTERFACE targets, cmake messes up the lib order
  anyway, adding `OpenSSL:Crypto` last, instead of `ws2_32`. This did
  not seem to cause an issue so far.
  Ref: df0563a #1535

For these reasons it seems unlikely this position change could break
Windows OpenSSL static gcc/ld builds relying on `libssh2.pc` for their
lib list. It least no more than it was before this patch.

Turns out these theories don't stand in practice and the order is broken
possibly by introducing INTERFACE targets, with or without this patch.
`libcrypto` (tested with LibreSSL) is in fact depending on `ws2_32`, and
the `bcrypt` dependency is also causing breakage with picky binutils ld.
I may try addressing it in a separate PR.

Ref: 33b6d5f #827
Ref: 31fb886 #811

Closes #1619
vszakats added a commit that referenced this pull request Jul 13, 2025
- Reworking the `Libs.private` collector logic for INTERFACE targets,
  broke the original lib order. Fix it by going back a single loop
  to retain order.
  Follow-up to df0563a #1535

- Implement the above with one change: move implicit CMake libs
  to the end of the list (was: the beginning).
  I expect these to be libs that any custom libs may depend on,
  like system libs, C runtime, compiler runtime lib.
  Follow-up to c87f129 #1466

Closes #1623
vszakats added a commit to curl/curl that referenced this pull request Nov 29, 2025
Rework the way curl's custom Find modules advertise their properties.

Before this patch, Find modules returned detected dependency properties
(header dirs, libs, libdirs, C flags, etc.) via global variables. curl's
main `CMakeLists.txt` copied their values into global lists, which it
later applied to targets. This solution worked internally, but it was
unsuited for the public, distributed `CURLConfig.cmake` and publishing
curl's Find modules with it, due to polluting the namespace of consumer
projects. It's also impractical to apply the many individual variables
to every targets depending on libcurl.

To allow using Find modules in consumer projects, this patch makes them
define as imported interface targets, named `CURL::<dependency>`. Then
store dependency information as target properties. It avoids namespace
pollution and makes the dependency information apply automatically
to all targets using `CURL::libcurl_static`.

Find modules continue to return `*_FOUND` and `*_VERSION` variables.

For dependencies detected via `pkg-config`, CMake 3.16+ is recommended.
Older CMake versions have a varying degree of support for
propagating/handling library directories. This may cause issues in envs
where dependencies reside in non-system locations and detected via
`pkg-config` (e.g. macOS + Homebrew). Use `CURL_USE_PKGCONFIG=OFF`
to fix these issues. Or upgrade to newer CMake, or link libcurl
dynamically.

Also:
- re-enable `pkg-config` for old cmake `find_library()` integration
  tests.
- make `curlinfo` build after these changes.
- distribute local Find modules.
- export the raw list of lib dependencies via `CURL_LIBRARIES_PRIVATE`.
- `CURLconfig.cmake`: use curl's Find modules to detect dependencies in
  the consumer env.
- add custom property to target property debug function.
- the curl build process no longer modifies `CMAKE_C_FLAGS`.
  Follow-up to e865420 #17047

Ref: #14930
Ref: libssh2/libssh2#1535
Ref: libssh2/libssh2#1571
Ref: libssh2/libssh2#1581
Ref: libssh2/libssh2#1623

Closes #16973
vszakats added a commit that referenced this pull request Jan 14, 2026
In some cases `ZLIB::ZLIB` and/or `OpenSSL::Crypto` may be aliases,
which prevents setting a libssh2-specific property (.pc module name)
in them:
```
CMake Error at [...]/src/CMakeLists.txt:... (set_target_properties):
  set_target_properties can not be used on an ALIAS target.
```

This can happen when doing "superbuilds" with classic zlib or zlib-ng,
which define `ZLIB::ZLIB` on their own, but as an alias, unlike CMake
does with the canonical `ZLIB::ZLIB` target.

Fix by special-casing these built-in targets and manually converting
them to .pc module names, without using the targets themselves
to carry this information throughout libssh2's internal build logic.

A side-effect of this change is that `zlib` is now present in libssh2.pc
when zlib is an indirect dependency via a crypto backend (OpenSSL or
wolfSSL). Before this patch it only appeared there when enabling zlib
explicitly for libssh2.

Ref: curl/curl#20316
Follow-up to df0563a #1535

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

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants