cmake: curl_add_clang_tidy_test_target tidy-ups

- simplify gathering header directories and compiler definitions
  recursively.

- handle the case when the cmake directory object doesn't define header
  directories or compiler definitions.

- honor more corners cases:
  - `INTERFACE_INCLUDE_DIRECTORIES` of the initial target.
  - handle no header directory for initial target.

- de-duplicate header directories and compiler redefinitions to mimic
  CMake.

- drop unnecessary `unset()`s.

Note that the order of header directories remains different compared to
how CMake passes them to the compiler when building tests. The order is
already different in the test target `INCLUDE_DIRECTORIES` property,
preventing to reproduce the exact CMake order. The distinction between
`-I` and `-isystem` is also missing from target properties.

Cherry-picked from #17768

Closes #17814
This commit is contained in:
Viktor Szakats 2025-07-04 06:15:54 +02:00
parent 5af2457848
commit e54c80b397
No known key found for this signature in database
GPG key ID: B5ABD165E2AEF201

View file

@ -99,55 +99,57 @@ endmacro()
# Internal: Recurse into target libraries and collect their include directories
# and macro definitions.
macro(curl_collect_target_options _target)
get_target_property(_libs ${_target} LINK_LIBRARIES)
if(_libs)
foreach(_lib IN LISTS _libs)
get_target_property(_val ${_target} INTERFACE_INCLUDE_DIRECTORIES)
if(_val)
list(APPEND _includes ${_val})
endif()
get_target_property(_val ${_target} INCLUDE_DIRECTORIES)
if(_val)
list(APPEND _includes ${_val})
endif()
get_target_property(_val ${_target} COMPILE_DEFINITIONS)
if(_val)
list(APPEND _definitions ${_val})
endif()
get_target_property(_val ${_target} LINK_LIBRARIES)
if(_val)
foreach(_lib IN LISTS _val)
if(TARGET "${_lib}")
get_target_property(_val ${_lib} INTERFACE_INCLUDE_DIRECTORIES)
if(_val)
list(APPEND _includes_l ${_val})
endif()
get_target_property(_val ${_lib} INCLUDE_DIRECTORIES)
if(_val)
list(APPEND _includes_l ${_val})
endif()
get_target_property(_val ${_lib} COMPILE_DEFINITIONS)
if(_val)
list(APPEND _definitions_l ${_val})
endif()
curl_collect_target_options(${_lib})
endif()
endforeach()
endif()
unset(_val)
endmacro()
# Create a clang-tidy target for test targets
macro(curl_add_clang_tidy_test_target _target_clang_tidy _target)
if(CURL_CLANG_TIDY)
set(_includes "")
set(_definitions "")
# Collect header directories and macro definitions applying to the directory
get_directory_property(_val INCLUDE_DIRECTORIES)
if(_val)
list(APPEND _includes ${_val})
endif()
get_directory_property(_val COMPILE_DEFINITIONS)
if(_val)
list(APPEND _definitions ${_val})
endif()
unset(_val)
# Collect header directories and macro definitions from lib dependencies
set(_includes_l "")
set(_definitions_l "")
curl_collect_target_options(${_target})
# Collect header directories applying to the target
get_directory_property(_includes_d INCLUDE_DIRECTORIES)
get_target_property(_includes_t ${_target} INCLUDE_DIRECTORIES)
set(_includes "${_includes_l};${_includes_d};${_includes_t}")
list(REMOVE_ITEM _includes "")
string(REPLACE ";" ";-I" _includes ";${_includes}")
list(REMOVE_DUPLICATES _includes)
# Collect macro definitions applying to the target
get_directory_property(_definitions_d COMPILE_DEFINITIONS)
get_target_property(_definitions_t ${_target} COMPILE_DEFINITIONS)
if(NOT _definitions_t)
unset(_definitions_t)
endif()
set(_definitions "${_definitions_l};${_definitions_d};${_definitions_t}")
list(REMOVE_ITEM _definitions "")
string(REPLACE ";" ";-D" _definitions ";${_definitions}")
list(REMOVE_DUPLICATES _definitions)
list(SORT _definitions) # Sort like CMake does
# Assemble source list
@ -165,17 +167,8 @@ macro(curl_add_clang_tidy_test_target _target_clang_tidy _target)
DEPENDS ${_sources})
add_dependencies(tests-clang-tidy ${_target_clang_tidy})
unset(_includes_d)
unset(_includes_t)
unset(_includes)
unset(_definitions_l)
unset(_definitions_d)
unset(_definitions_t)
unset(_definitions)
unset(_sources)
unset(_source)
unset(_libs)
unset(_lib)
unset(_val)
endif()
endmacro()