Skip to content

cmake: add native pkg-config detection for mbedTLS, MSH3, Quiche, Rustls, wolfSSL - #15193

Closed
vszakats wants to merge 21 commits into
curl:masterfrom
vszakats:cm-find-pkgconf-2
Closed

cmake: add native pkg-config detection for mbedTLS, MSH3, Quiche, Rustls, wolfSSL#15193
vszakats wants to merge 21 commits into
curl:masterfrom
vszakats:cm-find-pkgconf-2

Conversation

@vszakats

@vszakats vszakats commented Oct 8, 2024

Copy link
Copy Markdown
Member

Also:

  • detect and add required system libraries for Rustls on macOS and
    non-Windows.
  • add Linux CMake jobs for the touched dependencies.
    Caveats:

An internal change worthy of mention is that we are using the lib path
and name information returned by pkg-config as-is. Meaning the libname
doesn't include the full path, like it's usual with native cmake
detection. The path comes separately and needs to be rolled separately.
For this we add it to targets via link_directories(). We also keep tab
of them in CURL_LIBDIRS and use that in libcurl.pc. Feature checks
also need to receive these paths. CMake doesn't offer
a CMAKE_REQUIRED_* variable for this purpose, only
a CMAKE_REQUIRED_LINK_OPTIONS accepting raw linker flags. Add a macro
to convert a list of paths to linker options to solve it. wolfSSL
requires this for now.


w/o whitespace: https://github.com/curl/curl/pull/15193/files?w=1

@vszakats
vszakats force-pushed the cm-find-pkgconf-2 branch 3 times, most recently from 65c7bf5 to 0151919 Compare October 8, 2024 11:02
@vszakats vszakats changed the title cmake: add native pkg-config detection for mbedTLS, MSH3, Quiche, Rustls cmake: add native pkg-config detection for mbedTLS, MSH3, Quiche, Rustls, wolfSSL Oct 8, 2024
@vszakats vszakats added the TLS label Oct 8, 2024
@dfandrich

Copy link
Copy Markdown
Contributor

Analysis of PR #15193 at 15287516:

Test 2055 failed, which has NOT been flaky recently, so there could be a real issue in the PR.

Generated by Testclutch

@github-actions github-actions Bot added the CI Continuous Integration label Oct 8, 2024
vszakats added a commit to vszakats/curl that referenced this pull request Oct 8, 2024
vszakats added a commit that referenced this pull request Oct 9, 2024
- update mbedTLS repo URL.
- switch local mbedTLS build to use CMake, and Ninja.
  CMake build is required to create and install mbedTLS `pkg-config`
  files. (as of v3.6.1)
  `-DCMAKE_POSITION_INDEPENDENT_CODE=ON` required to avoid this error
  when linking mbedtls to `libcurl.so`:
  ```
  /usr/bin/ld: /home/runner/mbedtls/lib/libmbedcrypto.a(cipher.c.o): warning: relocation against `mbedtls_cipher_base_lookup_table' in read-only section `.text'
  /usr/bin/ld: /home/runner/mbedtls/lib/libmbedtls.a(ssl_tls.c.o): relocation R_X86_64_PC32 against symbol `mbedtls_x509_crt_profile_suiteb' can not be used when making a shared object; recompile with -fPIC
  /usr/bin/ld: final link failed: bad value
  ```
  Ref: https://github.com/curl/curl/actions/runs/11245069259/job/31264386723#step:40:43
- make local mbedTLS build 10x smaller by omitting programs and tests.
- GHA/linux: fix cmake warning by adding `-B .` option.
- GHA/linux: add build-only cmake job for packaged mbedTLS (2.x).
- fix compiler warning when building with mbedTLS 2.x:
  ```
  /home/runner/work/curl/curl/lib/vtls/mbedtls.c:344:1: error: ‘mbed_cipher_suite_get_str’ defined but not used [-Werror=unused-function]
    344 | mbed_cipher_suite_get_str(uint16_t id, char *buf, size_t buf_size,
        | ^~~~~~~~~~~~~~~~~~~~~~~~~
  ```
  Ref: https://github.com/curl/curl/actions/runs/11244999065/job/31264168295#step:40:50

Also in preparation for #15193
Closes #15208
@vszakats
vszakats force-pushed the cm-find-pkgconf-2 branch 2 times, most recently from c25512c to ca9b053 Compare October 9, 2024 01:21
@vszakats

vszakats commented Oct 9, 2024

Copy link
Copy Markdown
Member Author

Field notes:

mbedtls.pc only has references to its dependencies when building statically.

mbedtls.pc
```
prefix=/usr/local/Cellar/mbedtls/3.6.1
includedir=${prefix}/include
libdir=${prefix}/lib

Name: Mbed TLS
Description: Mbed TLS is a C library that implements cryptographic primitives, X.509 certificate manipulation and the SSL/TLS and DTLS protocols. Its small code footprint makes it suitable for embedded systems.
URL: https://www.trustedfirmware.org/projects/mbed-tls/
Version: 3.6.1
Requires.private: mbedcrypto mbedx509
Cflags: -I"${includedir}"
Libs: -L"${libdir}" -lmbedtls
```

mbedx509.pc
```
prefix=/usr/local/Cellar/mbedtls/3.6.1
includedir=${prefix}/include
libdir=${prefix}/lib

Name: Mbed TLS
Description: Mbed TLS is a C library that implements cryptographic primitives, X.509 certificate manipulation and the SSL/TLS and DTLS protocols. Its small code footprint makes it suitable for embedded systems.
URL: https://www.trustedfirmware.org/projects/mbed-tls/
Version: 3.6.1
Requires.private: mbedcrypto
Cflags: -I"${includedir}"
Libs: -L"${libdir}" -lmbedx509
```

mbedcrypto.pc
```
prefix=/usr/local/Cellar/mbedtls/3.6.1
includedir=${prefix}/include
libdir=${prefix}/lib

Name: Mbed TLS
Description: Mbed TLS is a C library that implements cryptographic primitives, X.509 certificate manipulation and the SSL/TLS and DTLS protocols. Its small code footprint makes it suitable for embedded systems.
URL: https://www.trustedfirmware.org/projects/mbed-tls/
Version: 3.6.1
Cflags: -I"${includedir}"
Libs: -L"${libdir}" -lmbedcrypto
```
@vszakats vszakats closed this in 7bab201 Oct 10, 2024
@vszakats
vszakats deleted the cm-find-pkgconf-2 branch October 10, 2024 12:46
vszakats added a commit that referenced this pull request Oct 12, 2024
- limit `SIZEOF_SA_FAMILY_T` detection to non-Windows.
- make sure `sys/socket.h` exists before detecting `SIZEOF_SA_FAMILY_T`.
- limit `mach_absolute_time()` detection to `APPLE`. Drop from Windows
  pre-cache.
- skip `HAVE_LIBSOCKET` detection for Windows, drop pre-cached value.
- drop redundant pre-cached `HAVE_LIBZ` for Windows.
- `curl_required_libpaths()`: stop accepting multiple arguments.
  To prepare for `CMAKE_REQUIRED_LINK_DIRECTORIES` support.
  Follow-up to 7bab201 #15193
- GSS: fix recent rebase mistakes:
  - fix variable name.
  - do not add a header twice.
  Follow-up to 91d451b #15157
- GSS: quote a variable.

Closes #15271
vszakats added a commit that referenced this pull request Oct 14, 2024
Add a build-only cmake job with system mbedTLS package and `pkg-config`
enabled. Ubuntu 24.04 comes with mbedTLS 2.28.8 which supports
`pkg-config`.

Follow-up to 7bab201 #15193
Closes #15286
vszakats added a commit that referenced this pull request Oct 24, 2024
To sync with other similar variables and hide them from cmake UIs.

Follow-up to 7bab201 #15193
Closes #15407
vszakats added a commit to vszakats/curl that referenced this pull request Oct 25, 2024
`pkg_check_modules()` seems to leave `<PACKAGE>_VERSION` defined with an
empty value, if the package is not found.

When the package is also not found in the fallback branch,
`find_package_handle_standard_args()` logs and error message. In this
message it includes the bogus empty value as: `(found version "")`:
```
Could NOT find Libssh2 (missing: LIBSSH2_INCLUDE_DIR LIBSSH2_LIBRARY) (found version "")
```
https://github.com/curl/curl/actions/runs/11509727553/job/32040378958?pr=15408#step:31:99

Clear the version number to avoid the confusion:
```
Could NOT find Libssh2 (missing: LIBSSH2_INCLUDE_DIR LIBSSH2_LIBRARY)
```
https://github.com/curl/curl/actions/runs/11510022503/job/32041149129?pr=15408#step:31:99

Seen with CMake v3.30.5

Follow-up to 7bab201 curl#15193
vszakats added a commit that referenced this pull request Oct 25, 2024
`pkg_check_modules()` seems to leave `<PACKAGE>_VERSION` defined with an
empty value, if the package is not found.

When the package is also not found in the fallback branch,
`find_package_handle_standard_args()` logs and error message. In this
message it includes the bogus empty value as: `(found version "")`:
```
Could NOT find Libssh2 (missing: LIBSSH2_INCLUDE_DIR LIBSSH2_LIBRARY) (found version "")
```
https://github.com/curl/curl/actions/runs/11509727553/job/32040378958?pr=15408#step:31:99

Clear the version number to avoid the confusion:
```
Could NOT find Libssh2 (missing: LIBSSH2_INCLUDE_DIR LIBSSH2_LIBRARY)
```
https://github.com/curl/curl/actions/runs/11510022503/job/32041149129?pr=15408#step:31:99

Seen with CMake v3.30.5.

Follow-up to 7bab201 #15193
Closes #15409
vszakats added a commit that referenced this pull request Dec 16, 2024
vszakats added a commit that referenced this pull request Dec 17, 2024
brotli, c-ares, libpsl, libssh2, nghttp2, nghttp3, ntgcp2, zstd.

Also:

Add workaround for CMake reporting successful libssh2 detection, but
leaving the header directory empty, and causing `libssh2.h` not found
while compiling. It happens when `pkgconf` is not detecting libssh2
dependency libcrypto in Homebrew after `brew unlink openssl` (as in
GHA/macos). The workaround is to require a non-empty header directory
to consider the detection successful. This workaround may need to be
tweaked and/or applied to other Find modules.

Follow-up to 7bab201 #15193

Closes #15408
vszakats added a commit that referenced this pull request Jan 26, 2025
- add `iphlpapi` library for c-ares.
  Ref: curl/curl-for-win@082d98b

- fix to not add system libs if the dependency was not found.
  librtmp, Rustls, wolfSSL
  Follow-up to 421e592 #15832
  Follow-up to 7bab201 #15193

Closes #16089
pps83 pushed a commit to pps83/curl that referenced this pull request Apr 26, 2025
- update mbedTLS repo URL.
- switch local mbedTLS build to use CMake, and Ninja.
  CMake build is required to create and install mbedTLS `pkg-config`
  files. (as of v3.6.1)
  `-DCMAKE_POSITION_INDEPENDENT_CODE=ON` required to avoid this error
  when linking mbedtls to `libcurl.so`:
  ```
  /usr/bin/ld: /home/runner/mbedtls/lib/libmbedcrypto.a(cipher.c.o): warning: relocation against `mbedtls_cipher_base_lookup_table' in read-only section `.text'
  /usr/bin/ld: /home/runner/mbedtls/lib/libmbedtls.a(ssl_tls.c.o): relocation R_X86_64_PC32 against symbol `mbedtls_x509_crt_profile_suiteb' can not be used when making a shared object; recompile with -fPIC
  /usr/bin/ld: final link failed: bad value
  ```
  Ref: https://github.com/curl/curl/actions/runs/11245069259/job/31264386723#step:40:43
- make local mbedTLS build 10x smaller by omitting programs and tests.
- GHA/linux: fix cmake warning by adding `-B .` option.
- GHA/linux: add build-only cmake job for packaged mbedTLS (2.x).
- fix compiler warning when building with mbedTLS 2.x:
  ```
  /home/runner/work/curl/curl/lib/vtls/mbedtls.c:344:1: error: ‘mbed_cipher_suite_get_str’ defined but not used [-Werror=unused-function]
    344 | mbed_cipher_suite_get_str(uint16_t id, char *buf, size_t buf_size,
        | ^~~~~~~~~~~~~~~~~~~~~~~~~
  ```
  Ref: https://github.com/curl/curl/actions/runs/11244999065/job/31264168295#step:40:50

Also in preparation for curl#15193
Closes curl#15208
pps83 pushed a commit to pps83/curl that referenced this pull request Apr 26, 2025
…ustls, wolfSSL

Also:
- detect and add required system libraries for Rustls on macOS and
  non-Windows.
- add Linux CMake jobs for the touched dependencies.
  Caveats:
  - MSH3 generates a broken `libmsh3.pc`, so needs manual config.
    Upstream PR: nibanks/msh3#225
  - Rustls `.pc` file missing, so needs manual config.

An internal change worthy of mention is that we are using the lib path
and name information returned by `pkg-config` as-is. Meaning the libname
doesn't include the full path, like it's usual with native cmake
detection. The path comes separately and needs to be rolled separately.
For this we add it to targets via `link_directories()`. We also keep tab
of them in `CURL_LIBDIRS` and use that in `libcurl.pc`. Feature checks
also need to receive these paths. CMake doesn't offer
a `CMAKE_REQUIRED_*` variable for this purpose, only
a `CMAKE_REQUIRED_LINK_OPTIONS` accepting raw linker flags. Add a macro
to convert a list of paths to linker options to solve it. wolfSSL
requires this for now.

Closes curl#15193
pps83 pushed a commit to pps83/curl that referenced this pull request Apr 26, 2025
- limit `SIZEOF_SA_FAMILY_T` detection to non-Windows.
- make sure `sys/socket.h` exists before detecting `SIZEOF_SA_FAMILY_T`.
- limit `mach_absolute_time()` detection to `APPLE`. Drop from Windows
  pre-cache.
- skip `HAVE_LIBSOCKET` detection for Windows, drop pre-cached value.
- drop redundant pre-cached `HAVE_LIBZ` for Windows.
- `curl_required_libpaths()`: stop accepting multiple arguments.
  To prepare for `CMAKE_REQUIRED_LINK_DIRECTORIES` support.
  Follow-up to 7bab201 curl#15193
- GSS: fix recent rebase mistakes:
  - fix variable name.
  - do not add a header twice.
  Follow-up to 91d451b curl#15157
- GSS: quote a variable.

Closes curl#15271
pps83 pushed a commit to pps83/curl that referenced this pull request Apr 26, 2025
Add a build-only cmake job with system mbedTLS package and `pkg-config`
enabled. Ubuntu 24.04 comes with mbedTLS 2.28.8 which supports
`pkg-config`.

Follow-up to 7bab201 curl#15193
Closes curl#15286
pps83 pushed a commit to pps83/curl that referenced this pull request Apr 26, 2025
To sync with other similar variables and hide them from cmake UIs.

Follow-up to 7bab201 curl#15193
Closes curl#15407
pps83 pushed a commit to pps83/curl that referenced this pull request Apr 26, 2025
`pkg_check_modules()` seems to leave `<PACKAGE>_VERSION` defined with an
empty value, if the package is not found.

When the package is also not found in the fallback branch,
`find_package_handle_standard_args()` logs and error message. In this
message it includes the bogus empty value as: `(found version "")`:
```
Could NOT find Libssh2 (missing: LIBSSH2_INCLUDE_DIR LIBSSH2_LIBRARY) (found version "")
```
https://github.com/curl/curl/actions/runs/11509727553/job/32040378958?pr=15408#step:31:99

Clear the version number to avoid the confusion:
```
Could NOT find Libssh2 (missing: LIBSSH2_INCLUDE_DIR LIBSSH2_LIBRARY)
```
https://github.com/curl/curl/actions/runs/11510022503/job/32041149129?pr=15408#step:31:99

Seen with CMake v3.30.5.

Follow-up to 7bab201 curl#15193
Closes curl#15409
pps83 pushed a commit to pps83/curl that referenced this pull request Apr 26, 2025
pps83 pushed a commit to pps83/curl that referenced this pull request Apr 26, 2025
brotli, c-ares, libpsl, libssh2, nghttp2, nghttp3, ntgcp2, zstd.

Also:

Add workaround for CMake reporting successful libssh2 detection, but
leaving the header directory empty, and causing `libssh2.h` not found
while compiling. It happens when `pkgconf` is not detecting libssh2
dependency libcrypto in Homebrew after `brew unlink openssl` (as in
GHA/macos). The workaround is to require a non-empty header directory
to consider the detection successful. This workaround may need to be
tweaked and/or applied to other Find modules.

Follow-up to 7bab201 curl#15193

Closes curl#15408
pps83 pushed a commit to pps83/curl that referenced this pull request Apr 26, 2025
- add `iphlpapi` library for c-ares.
  Ref: curl/curl-for-win@082d98b

- fix to not add system libs if the dependency was not found.
  librtmp, Rustls, wolfSSL
  Follow-up to 421e592 curl#15832
  Follow-up to 7bab201 curl#15193

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

Labels

build CI Continuous Integration cmake TLS

Development

Successfully merging this pull request may close these issues.

2 participants