summaryrefslogtreecommitdiff
path: root/cmake/IWYU.cmake
diff options
context:
space:
mode:
authorGereon Kremer <gereon.kremer@cs.rwth-aachen.de>2021-03-04 20:03:24 +0100
committerGitHub <noreply@github.com>2021-03-04 19:03:24 +0000
commitb0ab269c2039051a16212d5c9e7276c5f5c20b1d (patch)
tree561c2e177a84aa78667f24c50378e046a1733706 /cmake/IWYU.cmake
parent0942d5af032a2e58bd455df6e8b585f245a43dc9 (diff)
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.
Diffstat (limited to 'cmake/IWYU.cmake')
-rw-r--r--cmake/IWYU.cmake39
1 files changed, 39 insertions, 0 deletions
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()
generated by cgit on debian on lair
contact matthew@masot.net with questions or feedback