Skip to content

cmake: prefer COMPILE_OPTIONS over CMAKE_C_FLAGS for custom C options - #17047

Closed
vszakats wants to merge 10 commits into
curl:masterfrom
vszakats:cm-compoptions
Closed

vszakats wants to merge 10 commits into
curl:masterfrom
vszakats:cm-compoptions

Conversation

@vszakats

@vszakats vszakats commented Apr 14, 2025 •

Copy link
Copy Markdown
Member

Also:

  • pass -D_GNU_SOURCE via COMPILE_DEFINITIONS.
  • make it explicit to pass these C flags to feature checks.
  • update _GNU_SOURCE comment with pipe2().
  • 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.

@github-actions github-actions Bot added the build label Apr 14, 2025
@vszakats
vszakats marked this pull request as draft April 14, 2025 08:07
@vszakats
vszakats marked this pull request as ready for review April 14, 2025 10:01
@github-actions github-actions Bot added the CI Continuous Integration label Apr 14, 2025
@vszakats vszakats closed this in e865420 Apr 14, 2025
@vszakats
vszakats deleted the cm-compoptions branch April 14, 2025 19:37
vszakats added a commit to vszakats/curl that referenced this pull request Apr 14, 2025
vszakats added a commit to vszakats/curl that referenced this pull request Apr 14, 2025
vszakats added a commit that referenced this pull request Apr 14, 2025
vszakats added a commit to vszakats/curl that referenced this pull request Apr 15, 2025
Safe to do this now, as the code no longer relies on setting these
options after feature detection.

Follow-up to e865420 curl#17047
vszakats added a commit that referenced this pull request Apr 15, 2025
Safe to do this now, as the code no longer relies on setting these
options after feature detection.

Also: Tidy up the way we handle options not to be passed to feature
checks, and make sure to show them in the configure log.

Follow-up to e865420 #17047
Closes #17062
vszakats added a commit to vszakats/curl that referenced this pull request Apr 24, 2025
vszakats added a commit to vszakats/curl that referenced this pull request Apr 25, 2025
vszakats added a commit that referenced this pull request Apr 25, 2025
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 in all supported MSVC versions.
   (as tested with Visual Studio generators)
   Ref: https://ci.appveyor.com/project/curlorg/curl/builds/51945416

Follow-up to e865420 #17047
Ref: 866e029 #1711

Closes #17179
nbaws pushed a commit to nbaws/curl that referenced this pull request Apr 26, 2025
…ions

Also:
- pass `-D_GNU_SOURCE` via `COMPILE_DEFINITIONS`.
- make it explicit to pass these C flags to feature checks.
- update `_GNU_SOURCE` comment with `pipe2()`.
- 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 curl#17047
nbaws pushed a commit to nbaws/curl that referenced this pull request Apr 26, 2025
nbaws pushed a commit to nbaws/curl that referenced this pull request Apr 26, 2025
Safe to do this now, as the code no longer relies on setting these
options after feature detection.

Also: Tidy up the way we handle options not to be passed to feature
checks, and make sure to show them in the configure log.

Follow-up to e865420 curl#17047
Closes curl#17062
nbaws pushed a commit to nbaws/curl that referenced this pull request Apr 26, 2025
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 in all supported MSVC versions.
   (as tested with Visual Studio generators)
   Ref: https://ci.appveyor.com/project/curlorg/curl/builds/51945416

Follow-up to e865420 curl#17047
Ref: 866e029 curl#1711

Closes curl#17179
nbaws pushed a commit to nbaws/curl that referenced this pull request Apr 26, 2025
…ions

Also:
- pass `-D_GNU_SOURCE` via `COMPILE_DEFINITIONS`.
- make it explicit to pass these C flags to feature checks.
- update `_GNU_SOURCE` comment with `pipe2()`.
- 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 curl#17047
nbaws pushed a commit to nbaws/curl that referenced this pull request Apr 26, 2025
nbaws pushed a commit to nbaws/curl that referenced this pull request Apr 26, 2025
Safe to do this now, as the code no longer relies on setting these
options after feature detection.

Also: Tidy up the way we handle options not to be passed to feature
checks, and make sure to show them in the configure log.

Follow-up to e865420 curl#17047
Closes curl#17062
vszakats added a commit 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
vszakats added a commit to vszakats/curl that referenced this pull request Feb 15, 2026
@vszakats vszakats mentioned this pull request Feb 15, 2026
8 tasks done
vszakats added a commit to vszakats/curl that referenced this pull request Feb 16, 2026
vszakats added a commit to vszakats/curl that referenced this pull request Feb 16, 2026
vszakats added a commit to vszakats/curl that referenced this pull request Feb 18, 2026
vszakats added a commit that referenced this pull request Feb 18, 2026
Fix bigger and smaller kinks in how clang-tidy is configured and used.
Sync behavior more between autotools and cmake, lib/src and tests. Bump
clang-tidy minimum version and prepare logic to allow using clang-tidy
to a fuller extent.

- move clang-tidy settings from builds to a new `.clang-tidy.yml`.
  To make it easy to see and edit checks at one place. Also to allow
  using the `--checks=` option internally to silence tests-specific
  checks. (clang-tidy does not support multiple `--check=` options via
  the command-line.)
  Use explicit `--config-file=` option to point to the configuration.
- .clang-tidy.yml: link to documentation.
- suppress `clang-diagnostic-nullability-extension` due to a false
  positive in libtests with `CURL_WERROR=ON` and `PICKY_COMPILER=OFF`.
- .clang-tidy.yml: enable `portability-*`, `misc-const-correctness`.
- drop `--quiet` clang-tidy option by default to make its working a bit
  more transparent. The extra output is minimial.
- consistently use double-dashes in clang-tidy command-line options.
  Supported by clang-tidy 9.0.0+ (2019-09-19). Before this patch single
  and double were used arbitrarily.
- src/tool_parsecfg: silence false positive `clang-analyzer-unix.Stream`.
  Seen with clang 18 + clang-tidy 19 and 20 (only with autotools.)
- INTERNALS: require clang-tidy 14.0.0+. For the `--config-file` option.
- INTERNALS: recommend clang-tidy 19.1.0+, to avoid bogus
  `clang-analyzer-valist.Uninitialized` warnings. (bug details below)

autotools:

- allow configuring the clang-tidy tool via `CLANG_TIDY` env.
  Also to use in GHA to point to a suffixed clang-tody tool.
- fix to pass CFLAGS to lib, src sources.
  (keep omitting them when using a non-clang compiler.)
- fix to pass `--warnings-as-errors=*` in quotes to avoid globbing.

cmake:

- fix to not pass an empty `-I` to clang-tidy.
- fix to pass CFLAGS (picky warnings) to clang-tidy for test sources.
  (keep omitting them when using a non-clang compiler.)
- fix to disable `clang-diagnostic-unused-function` for test sources.
  (tests have static entry points, which trigger this check when
  checking them as individidual sources.)
- fix forwarding `CURL_CLANG_TIDYFLAGS` to clang-tidy.
- force disable picky warnings when running clang-tidy with a non-clang
  compiler. To not pass these flags when checking lib and src.

CI:

- GHA/linux: avoid clang-tidy bug by upgrading to v19, and drop the
  workaround.
- GHA/linux: switch to clang from gcc in the clang-tidy job. Using gcc
  doesn't allow passing CFLAGS to clang-tidy, making it less effective.
  (My guess this was one factor contributing to this job often missing
  to find certain issues compared to GHA/macos.)

I recomment using clang-tidy with a clang compiler, preferably the same
version or one that's compatible. Other cases are best effort, and may
fail if a C flag is passed to clang-tidy that it does not understand.
Picky warnings are mostly omitted when using a non-clang compiler,
reducing its usefulness.

Details and reproducer for the v18 (and earlier) clang-tidy bug,
previously affecting the GHA/linux job:

clang-tidy <=18 emits false warnings way when passing multiple C sources
at once (as done with autotools):

```sh
cat > src1.c <<EOF
#include <string.h>
static void dummy(void *p) { memcmp(p, p, 0); }
EOF

cat > src2.c <<EOF
#include <stdarg.h>
void vafunc(int option, ...)
{
  va_list param;
  va_start(param, option);
  if(option)
    (void)va_arg(param, int);
  va_end(param);
}
EOF

/opt/homebrew/opt/llvm@18/bin/clang-tidy --checks=clang-analyzer-valist.Uninitialized src1.c src2.c

# src2.c:7:11: warning: va_arg() is called on an uninitialized va_list [clang-analyzer-valist.Uninitialized]
```

Follow-up to e865420 #17047

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

Labels

build CI Continuous Integration cmake tidy-up

Development

Successfully merging this pull request may close these issues.

1 participant