cmake: enable pkg-config search on non-UNIX platforms#14140
cmake: enable pkg-config search on non-UNIX platforms#14140vszakats wants to merge 2 commits intocurl:masterfrom
pkg-config search on non-UNIX platforms#14140Conversation
pkg-config on non-UNIX platformspkg-config search on non-UNIX platforms
|
I wonder if this could use more tweaking to avoid cross-build issues. This was seen when doing Windows builds from Linux, and I wonder if Enclosing the if(NOT CMAKE_CROSSCOMPILING)
find_package(PkgConfig QUIET)
pkg_search_module(...)
endif()That would break Unix→Unix cross-builds, so if if(UNIX OR NOT CMAKE_CROSSCOMPILING)
find_package(PkgConfig QUIET)
pkg_search_module(...)
endif()edit: Or, is there a CMake setting to control |
|
@vszakats Are you planning to merge it next version window? |
|
Yes, in the next one, we have time to discuss. |
|
I hoped you would say no :D |
|
I tried also adding c-ares: and mbedtls Also found a patch in vcpkg for mbedtls: Can you take a look? |
|
The mbedtls patch was copied from 0c4b4c1, and should be fine. In general, yes, such fallouts are a sign that the new builds/tests increase code coverage by revealing issues. I suggest opening a separate PR for them, and continue there. As for |
|
I will create a PRs for each one. |
|
Brainstorming: The internet suggests that these envs should be set for cross-compiling, used by |
Why not implement the same logic that is on autotool and do the same inside the cmake? |
|
We can use autotools logic as an inspiration, but if there is a CMake "best practice" for this, it'd be nice to follow. |
|
Experiments show that if(NOT(CMAKE_CROSSCOMPILING AND "$ENV{PKG_CONFIG_LIBDIR}" STREQUAL ""))
find_package(PkgConfig QUIET)
pkg_search_module(...)
endif()According to https://cmake.org/cmake/help/v3.30/variable/CMAKE_CROSSCOMPILING.html, Since nobody uses this solution, I wonder if there is something better than this. Or, is this not an issue for most? I also tried edit: FWIW I've seen this issue and tested these with Libgcrypt + libssh2 in the Debian → Windows cross-build. |
|
@vszakats I think is better to post this msg on discussions that more people can react and talk about it. |
|
Feel free to open a discussion. Feedback may definitely help. |
|
I know that official Find modules in CMake use the same pattern, but I consider this a bad pattern: The canonical CMake pattern is to use input variables to guide the search for libs and include paths. I do understand that this approach was or is useful for some contexts, in particular transitions. |
|
@dg0yt Would something like in #14140 (comment) make it better for vcpkg? Also, which vcpkg platforms do you mean, as not beneficial for? (for UNIX targets this proposal doesn't change anything, only for non-UNIX, aka Windows) |
|
To be clear, I don't say it is bad for vcpkg. I say there is zero benefit. vcpkg cannot not be the reason to proliferate the pattern. MSYS2 etc. are a different story. Regardless of windows or build type (debug, release): vcpkg generally sets The flaw with the CMake
Depending on how search locations and cache variables play together. This may lead to a mix of debug and release binaries, or to mismatch of headers and binaries. This may explode at configuration time, at build time, or at runtime. Edit: In addition, the content can provide the actual link libs for static linkage. This valuable information is lost when only looking at the location. |
|
@dg0yt Thanks for the info. I understand pkg-config support in CMake is "somewhat haphasard", based on your description. Also, that vcpkg does the right thing with How it works with MSYS2, cross-builds targeting Windows, and other "random" Windows cases, remain the questions. |
Basically you identified the main problem: Finding the right pc files for non-native builds. Users who don't know how to setup pkg-config for a cross build are most likely to stumble over the automatism. CMake users might be unaware of pkg-config interfering with CMake dependency lookup. Simply installing IMHO the key is to use a fairly up-to-date CMake, and to leverage CMake's original search patterns, e.g. for
|
|
It comes down to two solutions:
We can kick off with either and switch to the other when issues are reported. The conditions can also be fine-tuned to reach the best experience for most. (I'd probably start defensive.) |
|
For a defensive approach: The user might provide The picture in CMake's official modules is "mixed", but that pattern exists: find_package(PkgConfig QUIET)
if(PKG_CONFIG_FOUND)
pkg_check_modules(PC_CURL QUIET libcurl)
...Note that |
|
Adding
The |
Yes.
Maybe not seen very often, but it is a general CMake pattern. CMake know-how for CMake users.
But it requires pkg-config tool know-how in order to AVOID use pkg-config. |
This is the problem of transitive usage requirements (in particular for static library linkage). Again, this is encoded in the content of the pc files. There is no Find module in CURL that makes use of the content. This PR only changes which locations to search with (Does CURL actually directly depend on nettle? If it does not, it should not have any explicit references to And that's why we continue to patch CURL in vcpkg. |
|
@dg0yt I don't mind if you solve the real problem here in curl, |
|
@talregev: I suggest sticking to the original point of this PR.
|
|
Correction: |
can we use find package nettle when using gnutls? |
|
Yes, it was the first option I tried, but |
I understand, that why I suggested with pkg-config. |
|
How would nettle be detected when pkg-config isn't available? |
|
|
Yes, it looks it will need a custom find module, with find_library and pkg-config (pkg-config alone is not enough). |
Suggested-by: Tal Regev Ref: curl#14136 (comment) Closes #xxxxx
0d6b61e to
68771d8
Compare
|
you may also want to add this? |
|
Reminder: Please do find_package(PkgConfig QUIET)
if(PKG_CONFIG_FOUND)
pkg_check_modules(PC_CURL QUIET libcurl)
...so that users can opt out of it globally with |
|
Speaking of This has the benefit that all the expected output variables will be initialized. With the manual/local solution they won't be. Either way, adding an extra local check doesn't seem to change the outcome of the detection. (Just realized that I missed to push this commit to the nettle PR) I also tested What am I missing? |
(Workaround if not needing gnutls:
This:
"Mixed" includes "broken". FTR I still think it is an anti-pattern to use
(Disclaimer: I spent more time on |
|
The anti-pattern you mention looks like a separate issue from the rest discussed here. I agree that using Some things learned there I applied to the recent As for what CMake does inside |
|
I'm abandoning this effort. |
Before this patch, `pkg-config` was used for `UNIX` builds only (with a few exceptions like wolfSSL, libssh, gsasl, libuv). This patch extends `pkg-config` use to all envs except: `MSVC` without vcpkg. Meaning MSVC with vcpkg will now use it. Also mingw on Windows. Also apply the new condition to options where `pkg-config` was used unconditionally (= for all targets). These are: `-DCURL_USE_WOLFSSL=ON`, `-DCURL_USE_LIBSSH=ON`, `-DCURL_USE_GSASL=ON` and `-DCURL_USE_LIBUV=ON` This patch may still cause regressions for cross-builds (e.g. mingw cross-build from Unix) and potentially other cases. If that happens, we recommend using some of these methods to explicitly disable `pkg-config` when using CMake: - CMake option: `-DPKG_CONFIG_EXECUTABLE=` (or `-DPKG_CONFIG_EXECUTABLE=nonexistent` or similar) This is similar to the (curl-specific) `PKG_CONFIG` env for autotools. - export env: `PKG_CONFIG_LIBDIR=` (or `PKG_CONFIG_PATH`, `PKG_CONFIG_SYSROOT_DIR`, or the CMake-specific `PKG_CONFIG`) We may improve control over this in a future patch, also allowing opting in MSVC (without vcpkg). Ref: #14405 Ref: #14408 Ref: #14140 Closes #14483
Suggested-by: Tal Regev
Ref: #14136 (comment)
Closes #14140
TODO:
FindNghttp2once cmake: detectnghttp2viapkg-config, enable by default #14136 merged.FindNettleonce cmake: detectnettlewhen building with GnuTLS #14285 merged.pkg_check_modules(LIBSSH "libssh").