cmake: replace cmakelint with cmake-lint from cmakelang, fix issues

This linter detects formatting and naming issues with minimal amount of
noise. It seems to be an improvement over the existing linter which was
only detecting line width overruns.

Also: fix to exclude `CurlTests.c` from linter.

Ref: https://cmake-format.readthedocs.io/en/latest/cmake-lint.html
Ref: https://github.com/cheshirekow/cmake_format

Closes #17576
This commit is contained in:
Viktor Szakats 2025-06-10 18:25:57 +02:00
parent 56d5982312
commit b761eb5add
No known key found for this signature in database
GPG Key ID: B5ABD165E2AEF201
12 changed files with 72 additions and 39 deletions

View File

@ -63,7 +63,7 @@ jobs:
python3-toml python3-markupsafe python3-jinja2 python3-tabulate \
python3-typing-extensions python3-libcst python3-impacket \
python3-websockets python3-pytest python3-filelock python3-pytest-xdist
python3 -m pip install --break-system-packages cmakelint==1.4.3 pytype==2024.10.11 ruff==0.11.9
python3 -m pip install --break-system-packages cmakelang==0.6.13 pytype==2024.10.11 ruff==0.11.9
- name: spellcheck
run: |

View File

@ -58,6 +58,7 @@ if(NOT GSS_ROOT_DIR AND NOT "$ENV{GSS_ROOT_DIR}")
find_package(PkgConfig QUIET)
pkg_search_module(_GSS ${_gnu_modname} ${_mit_modname} ${_heimdal_modname})
list(APPEND _gss_root_hints "${_GSS_PREFIX}")
set(_gss_version "${_GSS_VERSION}")
endif()
if(WIN32)
list(APPEND _gss_root_hints "[HKEY_LOCAL_MACHINE\\SOFTWARE\\MIT\\Kerberos;InstallDir]")
@ -135,14 +136,14 @@ if(NOT _GSS_FOUND) # Not found by pkg-config. Let us take more traditional appr
execute_process(
COMMAND ${_gss_configure_script} "--version"
OUTPUT_VARIABLE _GSS_VERSION
OUTPUT_VARIABLE _gss_version
RESULT_VARIABLE _gss_configure_failed
OUTPUT_STRIP_TRAILING_WHITESPACE
)
# Older versions may not have the "--version" parameter. In this case we just do not care.
if(_gss_configure_failed)
set(_GSS_VERSION 0)
set(_gss_version 0)
endif()
execute_process(
@ -269,23 +270,23 @@ else()
if(_GSS_MODULE_NAME STREQUAL _gnu_modname OR _GSS_${_gnu_modname}_VERSION)
set(GSS_FLAVOUR "GNU")
set(GSS_PC_REQUIRES "gss")
if(NOT _GSS_VERSION) # for old CMake versions?
set(_GSS_VERSION ${_GSS_${_gnu_modname}_VERSION})
if(NOT _gss_version) # for old CMake versions?
set(_gss_version ${_GSS_${_gnu_modname}_VERSION})
endif()
elseif(_GSS_MODULE_NAME STREQUAL _mit_modname OR _GSS_${_mit_modname}_VERSION)
set(GSS_FLAVOUR "MIT")
set(GSS_PC_REQUIRES "mit-krb5-gssapi")
if(NOT _GSS_VERSION) # for old CMake versions?
set(_GSS_VERSION ${_GSS_${_mit_modname}_VERSION})
if(NOT _gss_version) # for old CMake versions?
set(_gss_version ${_GSS_${_mit_modname}_VERSION})
endif()
else()
set(GSS_FLAVOUR "Heimdal")
set(GSS_PC_REQUIRES "heimdal-gssapi")
if(NOT _GSS_VERSION) # for old CMake versions?
set(_GSS_VERSION ${_GSS_${_heimdal_modname}_VERSION})
if(NOT _gss_version) # for old CMake versions?
set(_gss_version ${_GSS_${_heimdal_modname}_VERSION})
endif()
endif()
message(STATUS "Found GSS/${GSS_FLAVOUR} (via pkg-config): ${_GSS_INCLUDE_DIRS} (found version \"${_GSS_VERSION}\")")
message(STATUS "Found GSS/${GSS_FLAVOUR} (via pkg-config): ${_GSS_INCLUDE_DIRS} (found version \"${_gss_version}\")")
endif()
string(REPLACE ";" " " _GSS_CFLAGS "${_GSS_CFLAGS}")
@ -294,7 +295,7 @@ set(GSS_INCLUDE_DIRS ${_GSS_INCLUDE_DIRS})
set(GSS_LIBRARIES ${_GSS_LIBRARIES})
set(GSS_LIBRARY_DIRS ${_GSS_LIBRARY_DIRS})
set(GSS_CFLAGS ${_GSS_CFLAGS})
set(GSS_VERSION ${_GSS_VERSION})
set(GSS_VERSION ${_gss_version})
if(GSS_FLAVOUR)
if(NOT GSS_VERSION AND GSS_FLAVOUR STREQUAL "Heimdal")
@ -353,5 +354,5 @@ mark_as_advanced(
_GSS_LIBRARY_DIRS
_GSS_MODULE_NAME
_GSS_PREFIX
_GSS_VERSION
_gss_version
)

View File

@ -66,12 +66,12 @@ else()
endif()
include(FindPackageHandleStandardArgs)
find_package_handle_standard_args(Libgsasl
REQUIRED_VARS
LIBGSASL_INCLUDE_DIR
LIBGSASL_LIBRARY
VERSION_VAR
LIBGSASL_VERSION
find_package_handle_standard_args(Libgsasl
REQUIRED_VARS
LIBGSASL_INCLUDE_DIR
LIBGSASL_LIBRARY
VERSION_VAR
LIBGSASL_VERSION
)
if(LIBGSASL_FOUND)

View File

@ -65,6 +65,7 @@ macro(curl_internal_test _curl_test)
endif()
endmacro()
# Option for dependencies that accepts an 'AUTO' value, which enables the dependency if detected.
macro(curl_dependency_option _option_name _find_name _desc_name)
set(${_option_name} "AUTO" CACHE STRING "Build curl with ${_desc_name} support (AUTO, ON or OFF)")
set_property(CACHE ${_option_name} PROPERTY STRINGS "AUTO" "ON" "OFF")

View File

@ -25,6 +25,7 @@ include(CheckCSourceCompiles)
include(CheckCSourceRuns)
include(CheckTypeSize)
# #include header if condition is true
macro(curl_add_header_include _check _header)
if(${_check})
set(_source_epilogue "${_source_epilogue}

View File

@ -316,8 +316,9 @@ if(PICKY_COMPILER)
list(APPEND _picky "-wd4668") # 'M' is not defined as a preprocessor macro, replacing with '0' for '#if/#elif' (in winbase.h)
list(APPEND _picky "-wd4710") # 'snprintf': function not inlined
list(APPEND _picky "-wd4711") # function 'A' selected for automatic inline expansion
list(APPEND _picky "-wd4746") # volatile access of '<expression>' is subject to /volatile:<iso|ms> setting;
# consider using __iso_volatile_load/store intrinsic functions (ARM64)
# volatile access of '<expression>' is subject to /volatile:<iso|ms> setting;
# consider using __iso_volatile_load/store intrinsic functions (ARM64)
list(APPEND _picky "-wd4746")
list(APPEND _picky "-wd4774") # 'snprintf': format string expected in argument 3 is not a string literal
list(APPEND _picky "-wd4820") # 'A': 'N' bytes padding added after data member 'B'
if(MSVC_VERSION GREATER_EQUAL 1900)
@ -340,7 +341,7 @@ if(CMAKE_C_COMPILER_ID STREQUAL "Clang" AND MSVC)
list(APPEND _picky_tmp "-clang:${_ccopt}")
endif()
endforeach()
set("${_wlist}" ${_picky_tmp})
set("${_wlist}" ${_picky_tmp}) # cmake-lint: disable=C0103
endforeach()
endif()

View File

@ -520,7 +520,7 @@ if(PERL_EXECUTABLE)
DEPENDS "${PROJECT_SOURCE_DIR}/scripts/mk-ca-bundle.pl"
)
add_custom_target(curl-ca-firefox
COMMENT "generating a fresh ca-bundle.crt" VERBATIM USES_TERMINAL
COMMENT "Generating a fresh ca-bundle.crt" VERBATIM USES_TERMINAL
COMMAND "${PERL_EXECUTABLE}" "${PROJECT_SOURCE_DIR}/scripts/firefox-db2pem.sh" "lib/ca-bundle.crt"
DEPENDS "${PROJECT_SOURCE_DIR}/scripts/firefox-db2pem.sh"
)
@ -1735,7 +1735,7 @@ foreach(_variable IN ITEMS
HAVE_SYS_SOCKET_H
HAVE_SYS_TYPES_H
HAVE_UNISTD_H
)
)
if(${_variable})
string(APPEND CURL_TEST_DEFINES " -D${_variable}")
endif()
@ -1900,7 +1900,7 @@ foreach(_curl_test IN ITEMS
HAVE_BOOL_T
STDC_HEADERS
HAVE_ATOMIC
)
)
curl_internal_test(${_curl_test})
endforeach()
@ -2062,6 +2062,7 @@ endif()
# (= regenerate it).
function(curl_transform_makefile_inc _input_file _output_file)
file(READ ${_input_file} _makefile_inc_text)
# cmake-lint: disable=W0106
string(REPLACE "$(top_srcdir)" "\${PROJECT_SOURCE_DIR}" _makefile_inc_text ${_makefile_inc_text})
string(REPLACE "$(top_builddir)" "\${PROJECT_BINARY_DIR}" _makefile_inc_text ${_makefile_inc_text})

View File

@ -25,6 +25,7 @@
curl_transform_makefile_inc("Makefile.inc" "${CMAKE_CURRENT_BINARY_DIR}/Makefile.inc.cmake")
include("${CMAKE_CURRENT_BINARY_DIR}/Makefile.inc.cmake")
# Generate man pages
function(curl_add_manual_pages _listname)
# Maximum number of files per command to stay within shell/OS limits
if(CMAKE_HOST_UNIX)
@ -78,8 +79,8 @@ curl_add_manual_pages(man_MANS)
add_custom_target(curl-man ALL DEPENDS ${man_MANS})
if(NOT CURL_DISABLE_INSTALL)
set(_src "")
foreach(_f IN LISTS man_MANS)
list(APPEND _src "${CMAKE_CURRENT_BINARY_DIR}/${_f}")
foreach(_file IN LISTS man_MANS)
list(APPEND _src "${CMAKE_CURRENT_BINARY_DIR}/${_file}")
endforeach()
install(FILES ${_src} DESTINATION "${CMAKE_INSTALL_MANDIR}/man3")
unset(_src)

View File

@ -30,8 +30,8 @@ add_custom_target(curl-opts-man DEPENDS ${man_MANS})
add_dependencies(curl-man curl-opts-man)
if(NOT CURL_DISABLE_INSTALL)
set(_src "")
foreach(_f IN LISTS man_MANS)
list(APPEND _src "${CMAKE_CURRENT_BINARY_DIR}/${_f}")
foreach(_file IN LISTS man_MANS)
list(APPEND _src "${CMAKE_CURRENT_BINARY_DIR}/${_file}")
endforeach()
install(FILES ${_src} DESTINATION "${CMAKE_INSTALL_MANDIR}/man3")
endif()

View File

@ -6,7 +6,7 @@
# | (__| |_| | _ <| |___
# \___|\___/|_| \_\_____|
#
# Copyright (C) Dan Fandrich, <dan@coneharvesters.com>, et al.
# Copyright (C) Dan Fandrich, <dan@coneharvesters.com>, Viktor Szakats, et al.
#
# This software is licensed as described in the file COPYING, which
# you should have received as part of this distribution. The terms
@ -23,12 +23,16 @@
#
###########################################################################
# https://cmake-format.readthedocs.io/en/latest/cmake-lint.html
# https://cmake-format.readthedocs.io/en/latest/lint-usage.html
# https://github.com/cheshirekow/cmake_format/blob/master/cmakelang/configuration.py
# Run cmakelint on the curl source code. It will check all files given on the
# command-line, or else all relevant files in git, or if not in a git
# repository, all files starting in the tree rooted in the current directory.
#
# cmakelint can be installed from PyPi with the command "python3 -m pip install
# cmakelint".
# cmake-lint can be installed from PyPi with the command "python3 -m pip
# install cmakelang".
#
# The xargs invocation is portable, but does not preserve spaces in file names.
# If such a file is ever added, then this can be portably fixed by switching to
@ -43,8 +47,29 @@
# strip off the leading ./ to make the grep regexes work properly
find . -type f | sed 's@^\./@@'
fi
} | grep -E '(^CMake|/CMake|\.cmake$)' | grep -v -E '(\.h\.cmake|\.in)$' \
} | grep -E '(^CMake|/CMake|\.cmake$)' | grep -v -E '(\.h\.cmake|\.in|\.c)$' \
| xargs \
cmakelint \
--spaces=2 --linelength=132 \
--filter=-whitespace/indent,-convention/filename,-package/stdargs,-readability/wonkycase
cmake-lint \
--suppress-decorations \
--disable \
--line-width 132 \
--tab-size 2 \
--use-tabchars false \
--disabled-codes C0113 \
--function-pattern 'curl_[0-9a-z_]+' \
--macro-pattern '(curl_[0-9a-z_]+|check_include_file_concat_curl)' \
--global-var-pattern '[A-Z][0-9A-Z_]+' \
--internal-var-pattern '_[a-z][0-9a-z_]+' \
--local-var-pattern '_[a-z][0-9a-z_]+' \
--private-var-pattern '_[0-9a-z_]+' \
--public-var-pattern '([A-Z][0-9A-Z_]+|[A-Z][A-Za-z0-9]+_FOUND|[a-z]+_SOURCES|prefix|exec_prefix|includedir|libdir|ssize_t|_FILE_OFFSET_BITS)' \
--argument-var-pattern '_[a-z][0-9a-z_]+' \
--keyword-pattern '[A-Z][0-9A-Z_]+' \
--max-conditionals-custom-parser 2 \
--min-statement-spacing 1 \
--max-statement-spacing 2 \
--max-returns 6 \
--max-branches 12 \
--max-arguments 5 \
--max-localvars 15 \
--max-statements 50

View File

@ -42,6 +42,7 @@ add_subdirectory(tunit)
add_subdirectory(unit)
add_subdirectory(certs)
# Add a runtests target with customized flags
function(curl_add_runtests _targetname _test_flags)
if(NOT BUILD_LIBCURL_DOCS)
string(APPEND _test_flags " !documentation")
@ -68,6 +69,7 @@ function(curl_add_runtests _targetname _test_flags)
)
endfunction()
# Add a pytests target with customized flags
function(curl_add_pytests _targetname _test_flags)
set(_depends "")
if(NOT _targetname STREQUAL "pytest-ci")

View File

@ -47,15 +47,15 @@ if(TEST_INTEGRATION_MODE STREQUAL "find_package" OR
TEST_INTEGRATION_MODE STREQUAL "ExternalProject")
find_package(CURL REQUIRED CONFIG)
find_package(CURL REQUIRED CONFIG) # Double-inclusion test
foreach(result_var IN ITEMS
foreach(_result_var IN ITEMS
CURL_FOUND
CURL_SUPPORTS_HTTPS
CURL_SUPPORTS_Largefile
CURL_VERSION
CURL_VERSION_STRING
)
if(NOT ${result_var})
message(FATAL_ERROR "'${result_var}' variable expected, but not set by the CURL package.")
)
if(NOT ${_result_var})
message(FATAL_ERROR "'${_result_var}' variable expected, but not set by the CURL package.")
endif()
endforeach()
# Show variables set by find_package()