]> git.ipfire.org Git - thirdparty/curl.git/commitdiff
cmake: `curl_add_clang_tidy_test_target` tidy-ups
authorViktor Szakats <commit@vsz.me>
Fri, 4 Jul 2025 04:15:54 +0000 (06:15 +0200)
committerViktor Szakats <commit@vsz.me>
Fri, 4 Jul 2025 22:40:32 +0000 (00:40 +0200)
- 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

CMake/Macros.cmake

index 2acd7ff98bf4b354f54109885653f81adb933c93..710df701168dba374cbf5f29489552b477361498 100644 (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()