1
0
mirror of https://github.com/libssh2/libssh2.git synced 2025-11-17 04:22:32 +03:00
Commit Graph

76 Commits

Author SHA1 Message Date
Viktor Szakats
3969991b68 cmake: small tidy-ups
- say "Absolute path" for include and library paths in Find modules.
- flatten an if tree.
- fix a typo in comment.

Closes #1732
2025-10-21 01:09:36 +02:00
Viktor Szakats
f2838c7674 cmake: link wolfSSL macOS/Unix system lib dependencies
To support linking wolfSSL statically.

Closes #1722
2025-10-14 00:13:30 +02:00
Viktor Szakats
549d90b83e cmake: enable picky warnings for MSC_VERSION=1944
Closes #1687
2025-09-27 22:07:15 +02:00
Viktor Szakats
14a709a72f cmake: pass picky C options to C targets only (and not to C++ ossfuzz)
This also means that we no longer pass any picky warning option to
ossfuzz. It's probably not worth maintaining picky C++ options for this
single, small target.

Silencing:
```
cc1plus: warning: command-line option '-Wbad-function-cast' is valid for C/ObjC but not for C++
cc1plus: warning: command-line option '-Wdeclaration-after-statement' is valid for C/ObjC but not for C++
cc1plus: warning: command-line option '-Wenum-int-mismatch' is valid for C/ObjC but not for C++
cc1plus: warning: command-line option '-Wjump-misses-init' is valid for C/ObjC but not for C++
cc1plus: warning: command-line option '-Wmissing-parameter-type' is valid for C/ObjC but not for C++
cc1plus: warning: command-line option '-Wmissing-prototypes' is valid for C/ObjC but not for C++
cc1plus: warning: command-line option '-Wnested-externs' is valid for C/ObjC but not for C++
cc1plus: warning: command-line option '-Wold-style-declaration' is valid for C/ObjC but not for C++
cc1plus: warning: command-line option '-Wold-style-definition' is valid for C/ObjC but not for C++
cc1plus: warning: command-line option '-Wstrict-prototypes' is valid for C/ObjC but not for C++
```
Ref: https://github.com/libssh2/libssh2/actions/runs/18063134305/job/51402236388#step:7:15

Closes #1686
2025-09-27 21:50:34 +02:00
Viktor Szakats
664d7654c5 build: fix/extend Apple clang and mainline llvm version translations
Ref: https://en.wikipedia.org/wiki/Xcode#Toolchain_versions
Ref: https://github.com/curl/curl/pull/18477

Closes #1658
2025-09-14 12:43:29 +02:00
Viktor Szakats
694b9d964b cmake: add linter, fix issues
Ref: https://cmake-format.readthedocs.io/en/latest/cmake-lint.html
Ref: https://github.com/cheshirekow/cmake_format

Closes #1610
2025-06-11 07:09:10 +02:00
Viktor Szakats
72982d1589 build: sync up picky warnings with curl, fix fallouts
- cmake: sync `-ftree-vrp` behavior with autotools.
- build: enable `-Wjump-misses-init` for GCC 4.5+.
  Credits-to: Marcel Raad
- packet: fix `-Wjump-misses-init` warnings.
  ```
  src/packet.c: In function ‘_libssh2_packet_add’:
  src/packet.c:671:9: error: jump skips variable initialization [-Werror=jump-misses-init]
  src/packet.c:920:31: note: ‘want_reply’ declared here
  src/packet.c:671:9: error: jump skips variable initialization [-Werror=jump-misses-init]
  src/packet.c:919:26: note: ‘len’ declared here
  src/packet.c:669:9: error: jump skips variable initialization [-Werror=jump-misses-init]
  src/packet.c:1121:31: note: ‘want_reply’ declared here
  src/packet.c:669:9: error: jump skips variable initialization [-Werror=jump-misses-init]
  src/packet.c:1120:26: note: ‘len’ declared here
  src/packet.c:669:9: error: jump skips variable initialization [-Werror=jump-misses-init]
  src/packet.c:1119:26: note: ‘channel’ declared here
  ```
- build: enable gcc-12/13+, clang-10+ picky warnings
- acinclude.m4: sync formatting/comments with curl.
- autotools: fix `-Wtrampolines` picky warning for gcc 4.x versions.
  Follow-up to 854cfa8292 #1524
- cmake: enable `-Wall` for MSVC when `PICKY_COMPILER=ON`.
- MSVC: fix `-Wall` warnings.
  Seen on VS2015. Not seen on VS2022. Unknown for other versions.
  ```
  tests\test_simple.c(60): warning C4777: 'fprintf' : format string '%d' requires an argument of type 'int', but variadic argument 1 has type 'std::size_t'
  tests\test_simple.c(60): warning C4777: 'fprintf' : format string '%.*s' requires an argument of type 'int', but variadic argument 2 has type 'std::size_t'
  ```
- mbedtls: stop silencing warnings in 3rd-party header.
  Follow-up to a3aa6b4ca8 #1525
- cmake: stop deleting `-W<n>` from `CMAKE_C_FLAGS` (MSVC)
  1. `CMAKE_C_FLAGS` may apply to other projects, and deleting/altering it
     may be unexpected.
  2. We pass `-W4`/`-Wall` internally now, which do override custom
     `-W<n>` options as tested with VS2008 and newer VS generators.

Closes #1588
2025-05-07 23:24:41 +02:00
Viktor Szakats
62ad6a84ce cmake: honor individual picky option overrides found in CMAKE_C_FLAGS
Also to sync up with similar `./configure` feature via
`CURL_ADD_COMPILER_WARNINGS()`.

Example: `-DCMAKE_C_FLAGS=-Wno-xor-used-as-pow`

It may be useful as a workaround if a specific build combination hits
a picky warning within libssh2's source code. If such happens, we do
appreciate a report to fix it in libssh2 itself.

Closes #1590
2025-05-07 23:22:16 +02:00
Viktor Szakats
ac80041852 cmake: IMPORTED target improvements and fixes
- 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 a0d8529b08 #1571
Follow-up to df0563a857 #1535

Closes #1581
2025-04-27 13:46:30 +02:00
Viktor Szakats
a8dd5090d6 cmake: avoid 'target is imported but not globally visible' when consuming 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: https://github.com/curl/curl/pull/16973
Ref: https://github.com/curl/curl/pull/17140
Cherry-picked from #1581
Closes #1587
2025-04-25 16:44:55 +02:00
Viktor Szakats
117b02b442 cmake: fix libssh2-config.cmake for cmake <3.15
Replace `list(PREPEND ...)` for compatibility with CMake < 3.15.

Ref: https://cmake.org/cmake/help/latest/command/list.html#prepend

Follow-up to 82b09f9b3a #1322
Cherry-picked from #1581
Closes #1586
2025-04-25 16:44:55 +02:00
Viktor Szakats
3ae5ae3248 cmake: whitespace in Find modules [ci skip]
Cherry-picked from #1581
2025-04-25 15:04:58 +02:00
Viktor Szakats
6af8a7ca7f cmake: use CMAKE_COMPILE_WARNING_AS_ERROR if available
It's available in CMake >= 3.24.

Ref: https://cmake.org/cmake/help/latest/variable/CMAKE_COMPILE_WARNING_AS_ERROR.html

Closes #1583
2025-04-25 14:37:00 +02:00
Viktor Szakats
8a871d0b40 cmake: tidy-up ENABLE_WERROR logic
Tidy up the way we handle options not to be passed to feature checks,
and make sure to show them in the configure log.

Also:
- drop redundant and obsolete compiler check.
- drop redundant parenthesis.

Closes #1576
2025-04-15 16:18:44 +02:00
Viktor Szakats
a8e4456b05 cmake: avoid passing options via CMAKE_*_FLAGS
- replace `CMAKE_C_FLAGS*` and `CMAKE_CXX_FLAGS` with `COMPILE_OPTIONS`.
- replace `CMAKE_SHARED_LINKER_FLAGS_DEBUG` with
  `LINK_OPTIONS`/`LINK_FLAGS`.
- make it explicit to pass these C flags to feature checks.
- enable `-pedantic-errors` picky option for GCC with CMake <3.23.
- drop redundant condition when stripping existing MSVC `/Wn` options.

CMake passes `CMAKE_C_FLAGS` to targets, feature checks and raw
`try_compile()` calls. With `COMPILE_OPTIONS`, this is limited to
targets, and we must explicitly pass them to feature checks. This
makes the build logic clearer, and offers more control. It also
reduces log noise by omitting these options from linker commands,
and from `CMAKE_C_FLAGS` dumps in feature checks.

Closes #1575
2025-04-15 01:47:31 +02:00
Viktor Szakats
a0d8529b08 cmake: fix target interfaces for old CMake versions
- drop `VERSION` target property for cmake <3.19 compatibility
  ```
  CMake Error at CMake/Find*.cmake:90 (set_target_properties):
    INTERFACE_LIBRARY targets may only have whitelisted properties.
    The property "VERSION" is not allowed.
  ```
- move custom target property to the `INTERFACE_` namespace
  for cmake <3.19 compatibility. (To avoid same error as above)
- fix forwarding multiple `CFLAGS`, when detected via `pkg-config`.
- restore support for `-framework` and raw libs when processing
  the internal lib list for generating `libssh2.pc`. For good measure,
  at the moment libssh2 doesn't depend on a Framework.
- limit `libssh2_dumptargetprops()` to cmake 3.19+. It doesn't work with
  older versions.

Issues found while applying this change to curl. They did not surface in
libssh2 CI.

Follow-up to df0563a857 #1535

Closes #1571
2025-04-14 02:13:37 +02:00
Viktor Szakats
71179930e1 cmake: use VERSION_GREATER_EQUAL
Available since 3.7, which is the minimum required for libssh2:
https://cmake.org/cmake/help/latest/command/if.html#version-greater-equal

Closes #1573
2025-04-10 08:59:47 +02:00
Viktor Szakats
902fc9574d cmake: replace deprecated CMAKE_COMPILER_IS_GNUCC
Closes #1564
2025-03-23 21:13:26 +01:00
Viktor Szakats
df0563a857 cmake: make Find modules use INTERFACE
- 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 96d7f404e7 #1534

Closes #1535
2025-03-19 18:31:19 +01:00
Viktor Szakats
96d7f404e7 cmake: make libssh2-config work with all TLS-backends
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 a3aa6b4ca8 #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 d9c2e550ca #1460
Follow-up to 82b09f9b3a #1322

Closes #1534
2025-02-04 00:39:38 +01:00
Viktor Szakats
d033c1eaf6 cmake: tidy up string append and list prepend syntax
- `set(VAR "${VAR}<value>")` ->
  `string(APPEND VAR "<value>")`
  Available since CMake 3.4:
  https://cmake.org/cmake/help/latest/command/string.html#append

- `set(VAR "${VAR2}-or-<value>;${VAR}")` ->
  `set(VAR "${VAR2}-or-<value>" ${VAR})`

- prefer dash-style MSVC option.
  Follow-up to 854cfa8292 #1524

Closes #1533
2025-02-02 02:57:52 +01:00
Viktor Szakats
606c102e52 build: enable -Wcast-qual, fix fallouts
- enable compiler warning `-Wcast-qual`.
- add `LIBSSH2_UNCONST()` macro to strip const where absolutely
  necessary to avoid compiler warnings.
- fix const stripping by constifying where necessary.
- fix const stripping by using `LIBSSH2_UNCONST()`.
- libgcrypt.h: drop unnecessary casts.
- openssl: fix to use new `BIO_new_mem_buf()` parameter types
  with wolfSSL.

Cherry-picked from #1484
Closes #1527
2025-01-31 00:57:26 +01:00
Viktor Szakats
a3aa6b4ca8 cmake: misc improvements, add LIBSSH2_USE_PKGCONFIG option
- show platform flags (via curl).
- add `LIBSSH2_USE_PKGCONFIG` option to control whether to use
  `pkg-config` to find dependencies.
- set `.pc` names withing the Find modules.
- add `mbedcrypto` to `libssh2.pc` only when detected via `pkg-config`.
  Workaround for older mbedtls versions and non-CMake mbedTLS builds
  (as of mbedTLS 3.6.2) that don't emit an `mbedcrypto.pc` file.
- set header paths relative to the project root (tidy-up).
- use `-isystem` for crypto backend and zlib header paths.
  To match autotools.
- sync header path order with autotools.
- rename local variables to underscore-lowercase.
- minor tidy-ups.

Cherry-picked from #1484
Closes #1525
2025-01-30 23:36:39 +01:00
Viktor Szakats
854cfa8292 build: prepare builds for clang-cl, add cmake ossfuzz support
- cmake: add support to build ossfuzz.
  Enable with `-DBUILD_OSSFUZZ=ON`.
  Also supports `-DLIB_FUZZING_ENGINE=` like autotools does.
- check for `__clang__` when suppressing warnings in source. Necessary
  for clang-cl, which set `__clang__`, but doesn't set `__GNU__`.
- cmake: optimize out 4 picky warning option detections with gcc.
- cmake: bring `-pedantic-error`, `-Wall` use closer to curl's.
- cmake: set `-Wno-language-extension-token` for clang-cl.
- cmake: escape only the necessary `-W` options for clang-cl.
- cmake: apply picky warnings to C++.
- cmake: replace `unset(VAR)` with `set(VAR "")` for init.
- cmake: prefer dash-style MSVC options.
- cmake: simplify `MATCHES` expression.
- cmake: formatting/whitespace.
- ci/GHA: bump `actions/upload-artifact` to v4

Closes #1524
2025-01-30 22:01:49 +01:00
Viktor Szakats
4725826550 cmake: fix cmake warnings (#1511)
```
CMake Warning (dev) at cmake/CopyRuntimeDependencies.cmake:59 (add_custom_command):
  The following keywords are not supported when using
  add_custom_command(TARGET): DEPENDS.

  Policy CMP0175 is not set: add_custom_command() rejects invalid arguments.
  Run "cmake --help-policy CMP0175" for policy details.  Use the cmake_policy
  command to set the policy and suppress this warning.
Call Stack (most recent call first):
  example/CMakeLists.txt:58 (add_target_to_copy_dependencies)
```
```
CMake Warning (dev) at cmake/CopyRuntimeDependencies.cmake:59 (add_custom_command):
  Exactly one of PRE_BUILD, PRE_LINK, or POST_BUILD must be given.  Assuming
  POST_BUILD to preserve backward compatibility.

  Policy CMP0175 is not set: add_custom_command() rejects invalid arguments.
  Run "cmake --help-policy CMP0175" for policy details.  Use the cmake_policy
  command to set the policy and suppress this warning.
Call Stack (most recent call first):
  example/CMakeLists.txt:58 (add_target_to_copy_dependencies)
```
Ref: https://github.com/libssh2/libssh2/actions/runs/12614228505/job/35152908184?pr=1510#step:15:77
2025-01-05 00:48:55 +01:00
Viktor Szakats
4844e36840 build: disable warning -Wunreachable-code-break
This warning remains silent in unity builds. Since we're using unity
in CI for most jobs, warnings remain undetected there.
Disable them for all builds to avoid a surprise warning outside our CI.

It also doesn't work in any #included `.c` sources, like the crypto
backend sources.

The issue caught by the warning is useful for a tidy codebase, but
doesn't affect executed code. It was enabled in
84338c4de2d7c798e3c270c9610d51a4ad18a90b #12331 (2023-11-15).

llvm source: fee2953f23/clang/lib/Sema/AnalysisBasedWarnings.cpp (L125-L134)
llvm issue: https://github.com/llvm/llvm-project/issues/71046

Follow-up to 7ecc309cd1 #1224
Closes #1481
2024-10-28 12:30:02 +01:00
Viktor Szakats
bf9bb04571 cmake: add clang-cl support to PickyWarnings.cmake
via curl. (Untested with clang-cl in libssh2.)

Closes #1480
2024-10-28 12:30:02 +01:00
Viktor Szakats
d4e43c7b69 cmake/Find*: clear package version after pkg-config detection
`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 Libgcrypt (missing: LIBGCRYPT_INCLUDE_DIR LIBGCRYPT_LIBRARY)
  (found version "")
```

Clear the version number to avoid the confusion:
```
  Could NOT find Libgcrypt (missing: LIBGCRYPT_INCLUDE_DIR LIBGCRYPT_LIBRARY)
```

Seen with CMake v3.30.3 and 3.30.5.

Closes #1479
2024-10-28 11:35:41 +01:00
Viktor Szakats
ac00f6c437 cmake: tidy up variables names in PickyWarnings.cmake
Use underscore-lowercase for internal variables to match the rest of
the source.

Closes #1478
2024-10-28 11:35:41 +01:00
Viktor Szakats
efad5f9076 cmake: decorate comments in Find modules
Make them Markdown-like and make them pass spellcheckers better.

Closes #1477
2024-10-28 11:35:40 +01:00
Viktor Szakats
45064137ac cmake: sync and improve Find modules, add pkg-config native detection
- sync code between Find modules.
- wolfssl: replace `pkg-config` hints with native detection.
- libgcrypt, mbedtls: add `pkg-config`-based native detection.
- libgcrypt: add version detection.
- limit `pkg-config` use for `UNIX`, vcpkg, and non-cross MinGW builds,
  and builds with no manual customization via `*_INCLUDE_DIR` or
  `*_LIBRARY`.
- replace and sync Find module header comments.
- ci: delete manual mbedTLS config that's now redundant.

Based on similar work done in curl.

Second attempt at #1420
Closes #1445
2024-10-05 20:59:27 +02:00
Viktor Szakats
d9c2e550ca cmake: prefer find_dependency() in libssh2-config.cmake
CMake manual suggest using `find_dependency()` (over  `find_package()`)
in `config.cmake` scripts.

Ref: https://cmake.org/cmake/help/latest/module/CMakeFindDependencyMacro.html

Closes #1460
2024-09-28 11:41:48 +02:00
Viktor Szakats
9d9ee7807d cmake: tidy up syntax, minor improvements
- make internal variables underscore-lowercase.
- unfold lines.
- fold lines setting header directories.
- fix indent.
- drop interim variable `EXAMPLES`.
- initialize some variables before populating them.
- clear a variable after use.
- add `libssh2_dumpvars()` function for debugging.
- allow to override default `CMAKE_UNITY_BUILD_BATCH_SIZE`.
- bump up default `CMAKE_UNITY_BUILD_BATCH_SIZE` to 0 (was 32).
- tidy up option descriptions.

Closes #1446
2024-09-07 21:27:55 +02:00
Viktor Szakats
570de0f23f cmake: rename mbedTLS and wolfSSL Find modules
To match the curl ones.

Cherry-picked from #1445
2024-08-18 12:57:04 +02:00
Viktor Szakats
8e3c40b2a3 cmake: fixup version detection in mbedTLS find module
- avoid warning with 2.x versions about missing header file while
  extracting the version number.

- clear temp variables.

Closes #1444
2024-08-13 19:34:25 +02:00
Viktor Szakats
6d1d13c2a6 cmake: mbedTLS detection tidy-ups
- set and use `MBEDTLS_INCLUDE_DIRS`.
- stop marking `MBEDTLS_LIBRARIES` as advanced.

Closes #1438
2024-08-06 10:56:22 +02:00
Viktor Szakats
a0310699f5 cmake: sync formatting in cmake/Find* modules 2024-07-03 22:27:18 +02:00
Viktor Szakats
8788bb90a8 cmake: style tidy-up (more)
Follow-up to 3fa5282d62 #1166
Closes #1365
2024-04-14 01:52:11 +00:00
Viktor Szakats
03547cb8a8 cmake: tidy up function name casing in CopyRuntimeDependencies.cmake
Use lowercase to match callers.
2024-04-09 09:55:04 +00:00
Jiwoo Park
82b09f9b3a cmake: use the imported target of FindOpenSSL module (#1322)
* Use the imported target of FindOpenSSL module
* Build libssh2 before test runner
* Use find_package() in the CMake config file
* Use find_dependency() rather than find_package()
* Install CMake module files and use them in the config file
* Use elseif() to choose the crypto backend
2024-03-28 23:30:20 +01:00
Viktor Szakats
3ec53f3ea2 build: enable -pedantic-errors
According to the manual, this isn't the same as `-Werror -pedantic`.
Enable it together with `-Werror`.

https://gcc.gnu.org/onlinedocs/gcc/Warning-Options.html#index-pedantic-errors-1

This option results in autotools feature detection going into crazies.
To avoid this, we add it to `CFLAGS` late. Idea copied from curl.

This option has an effect only with gcc 5.0 and newer as of this commit.
Let's enable it for clang and older versions too for simplicity. Ref:
d5c0351055
https://github.com/curl/curl/pull/2747

Closes #1286
2023-12-17 15:15:34 +00:00
Viktor Szakats
ac68fbe475 build: FIXME -Wsign-conversion to be errors [ci skip] 2023-12-13 06:33:54 +00:00
Viktor Szakats
afa6b86560 build: enable missing OpenSSF-recommended warnings, with fixes
Ref:
https://best.openssf.org/Compiler-Hardening-Guides/Compiler-Options-Hardening-Guide-for-C-and-C++.html
(2023-11-29)

Enable new warnings:

- replace `-Wno-sign-conversion` with `-Wsign-conversion`.

  Fix them in example, tests and wincng. There remain about 360 of these
  warnings in `src`. Add a TODO item for those and disable `-Werror` for
  this particular warning.

- enable `-Wformat=2` for clang (in both cmake and autotools).

- enable `__attribute__((format))` for `_libssh2_debug()`,
  `_libssh2_snprintf()` and in tests for `run_command()`.

  `LIBSSH2_PRINTF()` copied from `CURL_TEMP_PRINTF()` in curl.

- enable `-Wimplicit-fallthrough`.

- enable `-Wtrampolines`.

Fix them:

- src: replace obsolete fall-through-comments with
  `__attribute__((fallthrough))`.

- wincng: fix `-Wsign-conversion` warnings.

- tests: fix `-Wsign-conversion` warnings.

- example: fix `-Wsign-conversion` warnings.

- src: fix `-Wformat` issues in trace calls.

  Also, where necessary fix `int` and `unsigned char` casts to
  `unsigned int` and adjust printf format strings. These were not
  causing compiler warnings.

  Cast large types to `long` to avoid dealing with printf masks for
  `size_t` and other C99 types. Existing code often used `int` for this.
  I'll update them to `long` in an upcoming commit.

- tests: fix `-Wformat` warning.

- silence `-Wformat-nonliteral` warnings.

- mbedtls: silence `-Wsign-conversion`/`-Warith-conversion`
  in external header.

Closes #1257
2023-12-03 01:32:20 +00:00
Viktor Szakats
744e059f31 example, tests: fix/silence -Wformat-truncation=2 gcc warnings
Then sync this warning option with curl.

Seems like a false positive and/or couldn't figure how to fix it, so silence:
```
example/ssh2.c:227:38: error: '%s' directive output may be truncated writing likely 1 or more bytes into a region of size 0 [-Werror=format-truncation=]
  227 |             snprintf(fn1, fn1sz, "%s/%s", h, pubkey);
      |                                      ^~
example/ssh2.c:227:34: note: assuming directive output of 1 byte
  227 |             snprintf(fn1, fn1sz, "%s/%s", h, pubkey);
      |                                  ^~~~~~~
example/ssh2.c:227:13: note: 'snprintf' output 3 or more bytes (assuming 4) into a destination of size 2
  227 |             snprintf(fn1, fn1sz, "%s/%s", h, pubkey);
      |             ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
example/ssh2.c:228:38: error: '%s' directive output may be truncated writing likely 1 or more bytes into a region of size 0 [-Werror=format-truncation=]
  228 |             snprintf(fn2, fn2sz, "%s/%s", h, privkey);
      |                                      ^~
example/ssh2.c:228:34: note: assuming directive output of 1 byte
  228 |             snprintf(fn2, fn2sz, "%s/%s", h, privkey);
      |                                  ^~~~~~~
example/ssh2.c:228:13: note: 'snprintf' output 3 or more bytes (assuming 4) into a destination of size 2
  228 |             snprintf(fn2, fn2sz, "%s/%s", h, privkey);
      |             ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
```
Ref: https://github.com/libssh2/libssh2/actions/runs/7055480458/job/19205970397#step:10:98

Fix:
```
tests/openssh_fixture.c:116:38: error: ' 2>&1' directive output may be truncated writing 5 bytes into a region of size between 1 and 1024 [-Werror=format-truncation=]
tests/openssh_fixture.c:116:11: note: 'snprintf' output between 6 and 1029 bytes into a destination of size 1024
```
Ref: https://github.com/libssh2/libssh2/actions/runs/7055480458/job/19205969221#step:10:51

Tested via #1257
2023-12-01 13:31:47 +00:00
Viktor Szakats
64d6789f71 cmake: rename picky warnings script
To match the camel-case style used in other CMake scripts and also
to match the name used in curl.

Closes #1225
2023-11-16 17:08:46 +00:00
Viktor Szakats
7ecc309cd1 build: enable more compiler warnings and fix them
Enable more picky compiler warnings. I've found these options in the
nghttp3 project when implementing the CMake quick picky warning
functionality for it.

Fix issues found along the way:

- wincng, mbedtls: delete duplicate function declarations.
  Most of this was due to re-#defining crypto functions to
  crypto-backend specific implementations These redefines also remapped
  the declarations in `crypto.h`, making the backend-specific
  declarations duplicates.
  This patch deletes the backend-specific declarations.

- wincng mapped two crypto functions to the same local function.
  Also causing double declarations.
  Fix this by adding two disctinct wrappers and moving
  the common function to a static one.

- delete unreachable `break;` statements.

- kex: disable macros when unused.

- agent: disable unused constants.

- mbedtls: disable double declaration warnings because public mbedTLS
  headers trigger it. (with function `psa_set_key_domain_parameters`)

- crypto.h: formatting.

Ref: a70edb08e9/cmake/PickyWarningsC.cmake

Closes #1224
2023-11-16 14:35:23 +00:00
Viktor Szakats
328a96b3da build: picky warning updates
- cmake, autotools: sync picky gcc warnings with curl.
- cmake, autotools: add `-Wold-style-definition` for clang too.
- cmake, autotools: add comment for `-Wformat-truncation=1`.
- cmake: more precise version info for old clang options.

Closes #1219
2023-11-15 11:02:04 +00:00
Viktor Szakats
932d6a3277 cmake: fix multiple include of libssh2 package
Also extend our integration test double inclusion. It will still not
catch this case, because that requires
`cmake_minimum_required(VERSION 3.18)` or higher.

Fixes:
```
CMake Error at .../lib/cmake/libssh2/libssh2-config.cmake:8 (add_library):
  add_library cannot create ALIAS target "libssh2::libssh2" because another
  target with the same name already exists.
Call Stack (most recent call first):
  CMakeLists.txt:24 (find_package)

CMake Error at .../lib/cmake/libssh2/libssh2-config.cmake:13 (add_library):
  add_library cannot create ALIAS target "Libssh2::libssh2" because another
  target with the same name already exists.
Call Stack (most recent call first):
  CMakeLists.txt:24 (find_package)
```

Test to reproduce:
```cmake
cmake_minimum_required(VERSION 3.18)  # must be 3.18 or higher

project(test)

find_package(libssh2 CONFIG)
find_package(libssh2 CONFIG)  # fails

add_executable(test main.c)
target_link_libraries(test libssh2::libssh2)
```

Ref: https://cmake.org/cmake/help/latest/release/3.18.html#other-changes
Ref: https://cmake.org/cmake/help/v3.18/policy/CMP0107.html

Assisted-by: Kai Pastor
Assisted-by: Harry Mallon
Ref: https://github.com/curl/curl/pull/11913

Closes #1216
2023-11-09 22:38:07 +00:00
Viktor Szakats
4c241d5c65 cmake: cleanup mbedTLS version detection more
- lowercase, underscored local variables.
- fix `find_library()` to use the multiple names passed.
- rely more on `find_package_handle_standard_args()`.
  Logic based on our `Findwolfssl.cmake`.
- delete ignored/unused `MBEDTLS_LIBRARY_DIR`.
- revert CI configuration to use `MBEDCRTYPO_LIBRARY`.
- clarify inputs/outputs in comment header.
- use variable for regex.
- formatting.

Follow-up to 4159467507 #1192

Closes #1196
2023-09-29 13:49:09 +00:00
Viktor Szakats
4159467507 cmake: improve/fix mbedTLS detection
- libssh2 needs the crypto lib only, stop dealing with the rest.

- simplify logic.

- drop hard-wired toolchain specific options that broke with e.g. MSVC.

  Reported by: AR Visions
  Fixes #1191

- add mbedTLS version detection for recent releases.

- merge custom detection results display into a single line.

- shorten mbedTLS configuration in macOS CI job.

Used the curl mbedTLS detection logic for ideas:
a8c773845f/CMake/FindMbedTLS.cmake

Closes #1192
2023-09-28 18:06:13 +00:00