Skip to content

cmake: imported target Libssh2::libssh2 disappeared #1103

Closed

Description

@rbrich

Describe the bug
In 1.11.0, there is now only Libssh2::libssh2_static or Libssh2::libssh2_shared.
The Libssh2::libssh2 which worked with 1.10.0 is no longer there.

To Reproduce
Build and install libssh2, use cmake config to import libssh2 into another CMake project

find_package(Libssh2 REQUIRED)
target_link_libraries(some_target PRIVATE Libssh2::libssh2)

-> CMake error, Libssh2::libssh2 doesn't exist

Works only with Libssh2::libssh2_static.

Expected behavior
Libssh2::libssh2 exists as alias to either static or shared, depending on what kind of libssh2 was built.

Version (please complete the following information):

  • OS and version: macOS
  • libssh2 version: 1.11.0
  • crypto backend and version: OpenSSL

Additional context
As there is already a breaking change, maybe reconsider #731.
You can also add another alias for compatibility, e.g.:

add_library(libssh2::libssh2 ALIAS ${LIB_SELECTED})
add_library(Libssh2::libssh2 ALIAS ${LIB_SELECTED})

Activity

  1. added a commit that references this issue on Jun 23, 2023
    51045ad
  2. vszakats commented on Jun 23, 2023

    @vszakats
    Member

    Thanks. I've made a PR, can you give it a test and suggest a way to finish it?: #1104

    It's trying to implement your suggestion + #731, but it fails with a CMake error.

  3. rbrich commented on Jun 24, 2023

    @rbrich
    Author

    I think the ALIAS needs to be created directly in Libssh2Config. The package config is usually split into two parts:

    • export(TARGETS ... FILE Libssh2Targets.cmake) - export targets into another file
    • use configure_package_config_file (see CMakePackageConfigHelpers) to create Libssh2Config.cmake from a template

    The config template should include the targets like this:

    @PACKAGE_INIT@
    include("${CMAKE_CURRENT_LIST_DIR}/@[email protected]")
    check_required_components(@PROJECT_NAME@)
    

    It can also contain additional cmake code, including the ALIAS libraries. In there, the exported targets are already namespaced, so it's possible to create alias in a different namespace.

    Unfortunately, there is not much material on CMake best practices. One such source is Mastering CMake which documented this practice of Targets+Config, but it seems to be out-of-date. E.g. it doesn't mention CMakePackageConfigHelpers.

  4. rbrich commented on Jun 24, 2023

    @rbrich
    Author

    I've tried it here: libssh2:766bde9...rbrich:b7891fd

    It's actually better to use plain configure_file instead of configure_package_config_file because the later adds installation prefix inside, making it non portable. The plain one will work in both build / install dirs. The additional macros provided by configure_package_config_file are not really needed in this case.

    I've also renamed the files to libssh2-targets.cmake, libssh2-config.cmake, libssh2-config-version.cmake (i.e. lowercase variant). This should allow both find_package(libssh2) and find_package(Libssh2) because CMake normalizes the package name to lowercase when looking for the libssh2-config.cmake. I think this won't work with Libssh2Config.cmake.

  5. added a commit that references this issue on Jun 24, 2023
  6. vszakats commented on Jun 24, 2023

    @vszakats
    Member

    I've updated the #1104 with your changes. It's building fine now and seems ready to merge.

  7. added a commit that references this issue on Jul 16, 2023
    de1160a
  8. added a commit that references this issue on Mar 28, 2024
    2da13c1
  9. added a commit that references this issue on Jul 14, 2024
    5c57002
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions