Include What You Use
IWYU is a llvm-based project that allows automatically manage the includes in .cpp files. It ensures that only the files actually used in a translation unit are included. It also tries to replace includes with forward declarations to avoid unnecessary includes.
Official website: https://github.com/include-what-you-use/include-what-you-use
How to use it in Krita
- Create a normal docker image with Krita dependencies preinstalled
- Install LLVM runtime from LLVM's PPA (see instruction on https://apt.llvm.org/)
echo 'deb http://apt.llvm.org/focal/ llvm-toolchain-focal-18 main' >> /etc/apt/sources.list wget -O - https://apt.llvm.org/llvm-snapshot.gpg.key | apt-key add - apt-get update apt-get install libllvm-18-ocaml-dev libllvm18 llvm-18 llvm-18-dev llvm-18-doc llvm-18-examples llvm-18-runtime clang-18 clang-tools-18 clang-18-doc libclang-common-18-dev libclang-18-dev libclang1-18 clang-format-18 python3-clang-18 clangd-18 clang-tidy-18
- Install the newest libstdc++:
apt install libstdc++-13-dev
- Checkout IWYU and build it locally: https://github.com/include-what-you-use/include-what-you-use
cd ~/persistent/include-what-you-use git checkout clang_18 mkdir -p ~/appimage-workspace/iwyu-build cd ~/appimage-workspace/iwyu-build CC="clang-18" CXX="clang++-18" cmake ~/persistent/include-what-you-use make -j8
- Set up PATH:
prepend PATH ~/appimage-workspace/iwyu-build/bin
- Generate Qt mappings:
python ~/persistent/include-what-you-use/mapgen/iwyu-mapgen-qt.py ~/appimage-workspace/deps/usr/include/ > ~/qt-mapings.txt
- Set up one more Krita build directory (don't build):
mkdir -p ~/appimage-workspace/krita-iwyu cd ~/appimage-workspace/krita-iwyu CC="clang-18" CXX="clang++-18" run_cmake.sh -DCMAKE_EXPORT_COMPILE_COMMANDS=ON ~/persistent/krita
- Apply fixes to, e.g. kis_image.cc (if you skip the filename, the fixes will be applied to the entire Krita source tree):
python ~/persistent/include-what-you-use/iwyu_tool.py -p . ~/persistent/krita/libs/image/kis_image.cpp -- -Xiwyu --mapping_file=~/qt-mapings.txt | python ~/persistent/include-what-you-use/fix_includes.py
- Build normally in your main build directory with gcc: ~/appimage-workspace/krita-build
My results
I have tried to apply IWYU to the most popular files in Krita, like kis_image.h and kis_paint_device.h, and then test the build speed of kritaimage target. My results were very sad: compilation speed after application of IWYU decreased by 8%
Before IWYU: 699s
real 11m39.240s user 69m35.119s sys 6m44.053s
After IWYU: 758s
real 12m38.415s user 72m51.695s sys 6m51.995s
So it seems like IWYU will not help us increase compilation time. At least until we found some ways to split the libraries into smaller chunks.