From 7f68568cfa94f018afa57b70658e1d3d2dcc9db2 Mon Sep 17 00:00:00 2001 From: fruffy Date: Sun, 24 Mar 2024 12:40:04 -0400 Subject: [PATCH] Use the new helper for IWYU. Move it to the Linters CMake. --- CMakeLists.txt | 50 --------------------------------------------- cmake/Linters.cmake | 36 ++++++++++++++++++++++++++++++++ 2 files changed, 36 insertions(+), 50 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 48ff15c5700..5a65d2124f9 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -620,53 +620,3 @@ SET(CPACK_SOURCE_IGNORE_FILES INCLUDE(CPack) ADD_CUSTOM_TARGET(dist COMMAND ${CMAKE_MAKE_PROGRAM} clean package_source) - -#################### IWYU -# Needs to be part of the top-level to be able to find all targets in the compiler framework. -if(ENABLE_IWYU) - # Set up IWYU for P4C. - message("Enabling IWYU checks.") - find_program(iwyu_path NAMES include-what-you-use iwyu REQUIRED) - set(iwyu_path - ${iwyu_path} - -Xiwyu - --max_line_length=100 - -Xiwyu - --no_fwd_decls - -Xiwyu - --cxx17ns - -Xiwyu - --mapping_file=${P4C_SOURCE_DIR}/tools/iwyu_mappings/p4c.imp - ) - message("IWYU command: ${iwyu_path}") - function(get_all_targets var) - set(targets) - get_all_targets_recursive(targets ${CMAKE_CURRENT_SOURCE_DIR}) - set(${var} ${targets} PARENT_SCOPE) - endfunction() - - macro(get_all_targets_recursive targets dir) - get_property(subdirectories DIRECTORY ${dir} PROPERTY SUBDIRECTORIES) - foreach(subdir ${subdirectories}) - get_all_targets_recursive(${targets} ${subdir}) - endforeach() - - get_property(current_targets DIRECTORY ${dir} PROPERTY BUILDSYSTEM_TARGETS) - list(APPEND ${targets} ${current_targets}) - endmacro() - - # Apply IWYU to all targets. - get_all_targets(all_targets) - # Remove generated files from IWYU. - list(FILTER all_targets EXCLUDE REGEX "controlplane-gen") - list(FILTER all_targets EXCLUDE REGEX "dpdk_runtime") - list(FILTER all_targets EXCLUDE REGEX "ir-generated") - list(FILTER all_targets EXCLUDE REGEX "genIR") - list(FILTER all_targets EXCLUDE REGEX "parser-gen") - list(FILTER all_targets EXCLUDE REGEX "gtest") - message("Applying IWYU to targets: ${all_targets}") - foreach(target ${all_targets}) - set_property(TARGET ${target} PROPERTY CXX_INCLUDE_WHAT_YOU_USE ${iwyu_path}) - endforeach() - -endif() diff --git a/cmake/Linters.cmake b/cmake/Linters.cmake index 1a10961c002..16c4d3ac85f 100644 --- a/cmake/Linters.cmake +++ b/cmake/Linters.cmake @@ -143,3 +143,39 @@ if(NOT ${BLACK_CMD} OR NOT (NOT ${ISORT_CMD})) else() message(WARNING "black or isort executable not found. Disabling black/isort checks. black/isort can be installed with \"pip3 install --user --upgrade black\" and \"pip3 install --user --upgrade isort\" ") endif() + +#################### IWYU +if(ENABLE_IWYU) + # Set up IWYU for P4C. + message("Enabling IWYU checks.") + find_program(iwyu_path NAMES include-what-you-use iwyu REQUIRED) + set(iwyu_path + ${iwyu_path} + -Xiwyu + --max_line_length=100 + -Xiwyu + --no_fwd_decls + -Xiwyu + --cxx17ns + -Xiwyu + --mapping_file=${P4C_SOURCE_DIR}/tools/iwyu_mappings/p4c.imp + ) + message("IWYU command: ${iwyu_path}") + + + get_all_targets(ALL_IWYU_TARGETS ${CMAKE_CURRENT_SOURCE_DIR}) + # Apply IWYU to all targets. + get_all_targets(ALL_IWYU_TARGETS) + # Remove generated files from IWYU. + list(FILTER ALL_IWYU_TARGETS EXCLUDE REGEX "controlplane-gen") + list(FILTER ALL_IWYU_TARGETS EXCLUDE REGEX "dpdk_runtime") + list(FILTER ALL_IWYU_TARGETS EXCLUDE REGEX "ir-generated") + list(FILTER ALL_IWYU_TARGETS EXCLUDE REGEX "genIR") + list(FILTER ALL_IWYU_TARGETS EXCLUDE REGEX "parser-gen") + list(FILTER ALL_IWYU_TARGETS EXCLUDE REGEX "gtest") + message("Applying IWYU to targets: ${all_targets}") + foreach(target ${ALL_IWYU_TARGETS}) + set_property(TARGET ${target} PROPERTY CXX_INCLUDE_WHAT_YOU_USE ${iwyu_path}) + endforeach() + +endif()