]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
mkosi: Run clangd within the tools tree instead of the build container
authorDaan De Meyer <daan.j.demeyer@gmail.com>
Wed, 23 Apr 2025 15:31:20 +0000 (17:31 +0200)
committerYu Watanabe <watanabe.yu+github@gmail.com>
Mon, 23 Jun 2025 18:49:24 +0000 (03:49 +0900)
Running within the build sandbox has a number of disadvantages:
- We have a separate clangd cache for each distribution/release combo
- It requires to build the full image before clangd can be used
- It breaks every time the image becomes out of date and requires a
  rebuild
- We can't look at system headers as we don't have the knowledge to map
  them from inside the build sandbox to the corresponding path on the host

Instead, let's have mkosi.clangd run clangd within the tools tree. We
already require building systemd for both the host and the target anyway,
and all the dependencies to build systemd are installed in the tools tree
already for that, as well as clangd since it's installed together with the
other clang tooling we install in the tools tree. Unlike the previous approach,
this approach only requires the mkosi tools tree to be built upfront, which has
a much higher chance of not invalidating its cache. We can also trivially map
system header lookups from within the sandbox to the path within mkosi.tools
on the host so that starts working as well.

(cherry picked from commit bde994efcc45d4e612a15fbd61de42b3149dea20)

docs/HACKING.md
mkosi/mkosi.clangd
mkosi/mkosi.images/build/mkosi.build.chroot [deleted file]

index cbaf82d08f00b9c02f23e4dab491db30b4b07115..aaacbbe9d8231cf7a5d995346c4e5364c029a993 100644 (file)
@@ -369,14 +369,25 @@ To debug systemd-boot in an IDE such as VSCode we can use a launch configuration
 
 [clangd](https://clangd.llvm.org/) is a language server that provides code completion, diagnostics and more
 right in your editor of choice (with the right plugin installed). When using mkosi, we can run clangd in the
-mkosi build container to avoid needing to build systemd on the host machine just to make clangd work.
+mkosi tools tree to avoid needing to install clangd on the host machine.
 
-All that is required is to run `mkosi` once to make sure cached images are available and to modify the path of the
-clangd binary used by your editor to the `mkosi.clangd` script included in the systemd repository. For example, for
-VScode, you'd have to add the following to the VSCode workspace settings of the systemd repository:
+All that is required is to run `mkosi -f sandbox true` once to make sure the tools tree is available and to modify
+the path of the clangd binary used by your editor to the `mkosi.clangd` script included in the systemd repository.
+For example, for VScode, you'd have to add the following to the VSCode workspace settings of the systemd repository:
 
 ```json
 {
     "clangd.path": "<path-to-systemd-repository>/mkosi/mkosi.clangd",
 }
 ```
+
+The script passes any arguments it receives directly to clangd which you can use
+for example to tell clangd where the compilation database can be found using the
+`--compile-commands-dir=` option.
+
+When using clangd, it's recommended to setup the build directory containing the
+compilation database used by clangd to use clang as the compiler as well:
+
+```sh
+$ mkosi sandbox -- env CC=clang CXX=clang++ meson setup build
+```
index 44c1128a91e5a3f6ed66466dc32d627d7f11e0af..f2e741f0faab3c4b86261b5a2b52d45e8e2f9569 100755 (executable)
@@ -8,17 +8,10 @@ else
     SPAWN=()
 fi
 
-MKOSI_CONFIG="$("${SPAWN[@]}" mkosi --json summary | jq -r .Images[-1])"
-BUILDDIR="$(jq -r .BuildDirectory <<< "$MKOSI_CONFIG")"
-BUILDSUBDIR="$(jq -r .BuildSubdirectory <<< "$MKOSI_CONFIG")"
-
-exec "${SPAWN[@]}" mkosi \
-    --rerun-build-scripts \
-    build \
-    -- \
-    clangd \
-    --compile-commands-dir=/work/build \
-    --path-mappings="\
-$(pwd)=/work/src,\
-$BUILDDIR/$BUILDSUBDIR=/work/build"\
-    "$@"
+exec "${SPAWN[@]}" \
+    mkosi sandbox -- \
+        clangd \
+            --compile-commands-dir=build \
+            --path-mappings="\
+$(pwd)/mkosi.tools/usr/include=/usr/include" \
+            "$@"
diff --git a/mkosi/mkosi.images/build/mkosi.build.chroot b/mkosi/mkosi.images/build/mkosi.build.chroot
deleted file mode 100755 (executable)
index 4a3c977..0000000
+++ /dev/null
@@ -1,7 +0,0 @@
-#!/bin/bash
-# SPDX-License-Identifier: LGPL-2.1-or-later
-set -e
-
-if [[ "$1" == "clangd" ]]; then
-    exec "$@"
-fi