mkosi: Run clangd within the tools tree instead of the build container

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.
This commit is contained in:
Daan De Meyer
2025-04-23 17:31:20 +02:00
parent cfe8f3ec74
commit bde994efcc
3 changed files with 22 additions and 25 deletions

View File

@@ -309,14 +309,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
```

View File

@@ -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" \
"$@"

View File

@@ -1,7 +0,0 @@
#!/bin/bash
# SPDX-License-Identifier: LGPL-2.1-or-later
set -e
if [[ "$1" == "clangd" ]]; then
exec "$@"
fi