Skip to content

cmake: make libssh2-config work with all TLS-backends - #1534

Closed
vszakats wants to merge 27 commits into
libssh2:masterfrom
vszakats:cm-config-find
Closed

vszakats wants to merge 27 commits into
libssh2:masterfrom
vszakats:cm-config-find

Conversation

@vszakats

@vszakats vszakats commented Feb 2, 2025 •

Copy link
Copy Markdown
Member

CMake:

  • Find*: set <modulename>_FOUND for compatibility when found via
    pkg-config. E.g. MbedTLS_FOUND.
    find_package_handle_standard_args() sets both <MODULENAME>_FOUND
    and <Modulename>_FOUND when detecting the dependency. Some CMake
    code relies on this and 3rd-party code may rely on it too. Make sure
    to set the latter variant when detecting the dependency via
    pkg-config, where we don't call
    find_package_handle_standard_args().

    CMake sets these variable to TRUE (not ON or 1). Replicate this
    for compatibility.

  • libssh2-config.cmake: inherit default LIBSSH2_USE_PKGCONFIG.
    Follow-up to a3aa6b4 cmake: misc improvements, add LIBSSH2_USE_PKGCONFIG option #1525

  • document variables consumed by libssh2-config.cmake.in.

  • libssh2-config.cmake: fix to link to non-OpenSSL crypto backends.
    This is most likely not how this is supposed to be done, but better
    than failing.
    What's the canonical way to do this, and how OpenSSL and zlib does it
    is yet to be figured out.

  • use ZLIB::ZLIB to reference zlib.

  • use IN ITEMS where missed.

  • harmonize variable dump output formats.

CMake find_package integration tests:

  • extend to all crypto backends (was: OpenSSL).
  • show libssh2 variables set by find_package().
  • stop building examples and tests for the consumed package.
    For performance.
  • enable zlib, for coverage.
  • be verbose when building the test targets.

ci/GHA:

  • add packaged mbedTLS (2.x) build to Linux matrix.
  • alphasort some tests.

Follow-up to d9c2e55 #1460
Follow-up to 82b09f9 #1322

E.g. `MbedTLS_FOUND`.

It's necessary because `find_dependency()` (as of CMake 3.31.5)
does check for this variable when deciding if a Find module
had found the dependency successfully. It does not check for
the `<uppercase-module-name>_FOUND` name.

https://github.com/Kitware/CMake/blob/eacc7287297267203fd65be1ca1daaaf62ebca37/Modules/CMakeFindDependencyMacro.cmake#L93-L97

Since find_package_handle_standard_args() is setting both of
these `_FOUND` variables, make sure we set it on the pkg-config
codepath which doesn't call find_package_handle_standard_args().
Also document variables consumed by `libssh2-config.cmake.in`.
This is most likely not how this is supposed to be done, but better than
just failing in this cases.

What's the canonical way to do this, and how OpenSSL and zlib does it
(or they just work by accident) is yet to be figured out.
@vszakats vszakats added the build label Feb 2, 2025
This reverts commit aeee1ae.

Several CI Jobs fail:

AppVeyor / VS2022, OpenSSL 3, x64, Server 2019, clang-cl — failed
https://ci.appveyor.com/project/libssh2org/libssh2/builds/51438007/job/y278mlo1w05dnjyp

CI / linux (gcc, amd64, OpenSSL-102-from-source, cmake, ON) (pull_request) Failing after 1m
CI / linux (gcc, amd64, OpenSSL-110-from-source, cmake, ON) (pull_request) Failing after 1m
CI / macOS (cmake, LibreSSL) (pull_request) Failing after 15s
CI / macOS (cmake, OpenSSL 1.1) (pull_request) Failing after 16s
CI / macOS (cmake, OpenSSL 3) (pull_request) Failing after 15s
CI / msvc (x64, windows, OpenSSL, OFF, OFF, ON, OFF, ON) (pull_request) Failing after 1m
https://github.com/libssh2/libssh2/actions/runs/13101423309/job/36550138366?pr=1534

```
C:\projects\libssh2\src\openssl.h(97,10): fatal  error : 'openssl/opensslconf.h' file not found
```
@vszakats

vszakats commented Feb 3, 2025 •

Copy link
Copy Markdown
Member Author

/cc @talregev in case you're interested. I used your idea to inherit the pkg-config flag default from the build. It seems cleaner this way. I kept the option to override it at consumption-time. The namespace pollution remains unchanged with this PR, but the integration tests now do work.

CMake's built-in OpenSSL Find module implements this by keeping track of dependency details within OpenSSL::* target properties.

For those, CMake has a mandatory IMPORTED_LOCATION property. This is a full path to a library. It's problematic for us, because we don't have such information when detecting dependencies via pkg-config. We have library paths and libnames instead. OpenSSL does it by feeding pkg-config detection results as input to the CMake-native-detection, but that way most of the pkg-config information is ignored and lost.

If targets supported link paths and lib name properties as first class citizens, it'd be a nice and clean way to store all detection results as properties, instead of rolling them in custom variables (that pollute the namespace). Though even with TARGET::TARGET names, pollution remains an issue, albeit a lesser one.

Maybe this mandatory property can be reconstructed with a fair chance of working most of the time, but it seems shaky.

Warning about the mandatory property:

CMake Warning (dev) in CMakeLists.txt:
  IMPORTED_LOCATION not set for imported target "WolfSSL::WolfSSL".

@talregev

talregev commented Feb 3, 2025

Copy link
Copy Markdown

@dg0yt
FYI

@dg0yt

dg0yt commented Feb 3, 2025

Copy link
Copy Markdown

CMake has a mandatory IMPORTED_LOCATION property. This is a full path to a library. It's problematic for us, because we don't have such information when detecting dependencies via pkg-config. We have library paths and libnames instead. OpenSSL does it by feeding pkg-config detection results as input to the CMake-native-detection, but that way most of the pkg-config information is ignored and lost.

  • IMPORTED_LOCATION isn't needed for INTERFACE targets.
  • CMake would actually use IMPORTED_LOCATION_<CONFIG> in some cases.
  • IMPORTED_LOCATION isn't the right property for Windows import libs of imported SHARED targets.

pollute the namespace

Mitigated by prefixing with the package name, the only prefix the package may take ownership of.

@vszakats

vszakats commented Feb 3, 2025 •

Copy link
Copy Markdown
Member Author

I tried INTERFACE, but the manual says it's meant for header-only packages,
which wasn't reassuring. Then it turned out it doesn't support ::-style target names
(temply worked around by using __). Then got an odd error, worked around
with appending IMPORTED to add_library(). After this it allows :: again.
Then it still fails because it seems to ignore the libdir set via
INTERFACE_LINK_DIRECTORIES, and doesn't find -lwolfssl.

Do you have a hint where to look?

(INTERFACE is also not compatible with our .pc file assembly logic, but
that's hopefully a lighter issue.)

edit: libdir (and linker options) are picked up as expected when building libssh2
with the libssh2:WolfSSL target, but ignored when building the test project
consuming libssh2.
https://cmake.org/cmake/help/latest/command/add_library.html#interface-libraries

@vszakats vszakats closed this in 96d7f40 Feb 3, 2025
@vszakats
vszakats deleted the cm-config-find branch February 3, 2025 23:50
@vszakats vszakats mentioned this pull request Feb 3, 2025
9 of 10 tasks
@vszakats

vszakats commented Feb 3, 2025 •

Copy link
Copy Markdown
Member Author

Continuing the INTERFACE experiments and this discussion thread in → #1535

vszakats added a commit to vszakats/libssh2 that referenced this pull request Feb 4, 2025
vszakats added a commit to vszakats/libssh2 that referenced this pull request Feb 4, 2025
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 added a commit that referenced this pull request Mar 19, 2025
- 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

Closes #1535
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.

3 participants