Try using IWYU on Krita
Open, Needs TriagePublic

Description

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

  1. Create a normal docker image with Krita dependencies preinstalled
  2. 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
  1. Install the newest libstdc++:
apt install libstdc++-13-dev
  1. 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
  1. Set up PATH:
prepend PATH ~/appimage-workspace/iwyu-build/bin
  1. Generate Qt mappings:
python ~/persistent/include-what-you-use/mapgen/iwyu-mapgen-qt.py ~/appimage-workspace/deps/usr/include/ > ~/qt-mapings.txt
  1. 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
  1. 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
  1. 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.

dkazakov updated the task description. (Show Details)Sep 20 2024, 12:10 PM

Here is the patch I tried (generated with IWYU): https://invent.kde.org/dkazakov/krita/-/commit/c14fd2d1948105f192819a6db708a9e6dd1af751
Full branch: https://invent.kde.org/dkazakov/krita/-/commits/kazakov/iwyu-try-for-kritaimage?ref_type=heads

The patch doesn't compile fully (some issues in kritaui), so I tested only kritaimage.