From: Gereon Kremer Date: Thu, 4 Mar 2021 19:03:24 +0000 (+0100) Subject: Add cmake scripts for iwyu targets. (#6042) X-Git-Tag: cvc5-1.0.0~2154 X-Git-Url: https://git.libre-soc.org/?a=commitdiff_plain;h=b0ab269c2039051a16212d5c9e7276c5f5c20b1d;p=cvc5.git Add cmake scripts for iwyu targets. (#6042) This PR adds some utility targets that simplify the usage of iwyu (include-what-you-use) on our code base. --- diff --git a/CMakeLists.txt b/CMakeLists.txt index 7cb4e6291..7ac2eb262 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -535,6 +535,11 @@ if(GPL_LIBS) set(CVC4_GPL_DEPS 1) endif() +#-----------------------------------------------------------------------------# +# Provide targets to inspect iwyu suggestions + +include(IWYU) + #-----------------------------------------------------------------------------# # Generate CVC4's cvc4autoconfig.h header diff --git a/cmake/IWYU.cmake b/cmake/IWYU.cmake new file mode 100644 index 000000000..1a6c4d372 --- /dev/null +++ b/cmake/IWYU.cmake @@ -0,0 +1,39 @@ +find_program(IWYU_PATH NAMES iwyu_tool iwyu-tool) + +if(IWYU_PATH) + # iwyu is available + message(STATUS "Found IWYU: ${IWYU_PATH}") + + # add a target to inspect number of times headers are included. + add_custom_target(iwyu-list-includes + COMMAND grep -hor "\"#include .*\"" ${PROJECT_SOURCE_DIR}/src | sort | uniq -c | sort -n + ) + + # find the standard library directory + find_program(CLANG_PATH clang REQUIRED) + execute_process(COMMAND ${CLANG_PATH} -print-resource-dir + OUTPUT_VARIABLE LLVM_INCLUDE_DIR + OUTPUT_STRIP_TRAILING_WHITESPACE + ) + if(LLVM_INCLUDE_DIR) + set(LLVM_INCLUDE_DIR "-I${LLVM_INCLUDE_DIR}/include") + endif() + + # add a target to run iwyu on all files (within the compilation database) + add_custom_target(iwyu-all + COMMAND ${IWYU_PATH} -o clang -p . -- -Xiwyu --cxx17ns ${LLVM_INCLUDE_DIR} + ) + + file(GLOB subdirs ${PROJECT_SOURCE_DIR}/src/*) + foreach(dir ${subdirs}) + # add a target for every subdirectory + file(GLOB_RECURSE source_files + ${dir}/*.cpp + ${dir}/*.cc + ) + get_filename_component(dirname ${dir} NAME) + add_custom_target(iwyu-${dirname} + COMMAND ${IWYU_PATH} -o clang -p . ${source_files} -- -Xiwyu --cxx17ns ${LLVM_INCLUDE_DIR} + ) + endforeach() +endif()