Add cmake scripts for iwyu targets. (#6042)
authorGereon Kremer <gereon.kremer@cs.rwth-aachen.de>
Thu, 4 Mar 2021 19:03:24 +0000 (20:03 +0100)
committerGitHub <noreply@github.com>
Thu, 4 Mar 2021 19:03:24 +0000 (19:03 +0000)
This PR adds some utility targets that simplify the usage of iwyu (include-what-you-use) on our code base.

CMakeLists.txt
cmake/IWYU.cmake [new file with mode: 0644]

index 7cb4e6291bcc893522cfd9e26e456898af0c2f7f..7ac2eb2620cf5519fdbd568ab2a696aacb9e2248 100644 (file)
@@ -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 (file)
index 0000000..1a6c4d3
--- /dev/null
@@ -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()