Skip to content

cmake: IMPORTED target improvements and fixes - #1581

Closed
vszakats wants to merge 7 commits into
libssh2:masterfrom
vszakats:cm-facetidy
Closed

vszakats wants to merge 7 commits into
libssh2:masterfrom
vszakats:cm-facetidy

Conversation

@vszakats

@vszakats vszakats commented Apr 23, 2025 •

Copy link
Copy Markdown
Member
  • 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

@vszakats vszakats added the build label Apr 23, 2025
@vszakats
vszakats force-pushed the cm-facetidy branch 4 times, most recently from f874095 to a0e689f Compare April 24, 2025 00:05
vszakats added a commit that referenced this pull request Apr 25, 2025
vszakats added a commit that referenced this pull request Apr 25, 2025
vszakats added a commit that referenced this pull request Apr 25, 2025
…ming libcurl with old cmake

Fixes:
```
CMake Error at bld-libssh2/_pkg/lib/cmake/libssh2/libssh2-config.cmake:35 (add_library):
  add_library cannot create ALIAS target "libssh2::libssh2" because target
  "libssh2::libssh2_shared" is imported but not globally visible.
Call Stack (most recent call first):
  CMakeLists.txt:27 (find_package)

CMake Error at bld-libssh2/_pkg/lib/cmake/libssh2/libssh2-config.cmake:40 (add_library):
  add_library cannot create ALIAS target "Libssh2::libssh2" because target
  "libssh2::libssh2_shared" is imported but not globally visible.
Call Stack (most recent call first):
  CMakeLists.txt:27 (find_package)
```

tests/cmake reproducer (requires #1581):
```shell
export CMAKE_CONSUMER=/path/to/CMake-3.12.0/bin/cmake
./test.sh find_package
```

I don't understand what this error says, why it happens in certain CMake
versions, and why a workaround is necessary for what seems like
a standard export/consume configuration. This patch is based on internet
suggestions and other projects ending up with this workaround.

Ref: curl/curl#16973
Ref: curl/curl#17140
Cherry-picked from #1581
Closes #1587
@vszakats vszakats changed the title cmake: misc imported target improvements/fixes, mostly for old cmake versions cmake: IMPORTED target improvements and fixes Apr 25, 2025
@vszakats vszakats added the tests label Apr 25, 2025
@vszakats vszakats closed this in ac80041 Apr 27, 2025
vszakats added a commit that referenced this pull request Apr 27, 2025
- ci/GHA: add cmake integration tests for Windows.
- ci/GHA: test `add_subdirectory` with Libgcrypt.
- make them run faster with prefill, unity, Ninja, omitting curl tool.
- add support for any build configuration.
- add old-cmake support with auto-detection.
- auto-detect Ninja.
- run consumer test apps to see if they work.
  Also show the cryptography backend.
- add support for Windows.
- make it more verbose.
- re-add `ExternalProject` cmake consumer test. It's broken.
- tidy up terminology.

Cherry-picked from #1581

Closes #1589
@vszakats
vszakats deleted the cm-facetidy branch April 27, 2025 11:47
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant