From: Daan De Meyer Date: Wed, 23 Apr 2025 15:31:20 +0000 (+0200) Subject: mkosi: Run clangd within the tools tree instead of the build container X-Git-Tag: v258-rc1~751 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=bde994efcc45d4e612a15fbd61de42b3149dea20;p=thirdparty%2Fsystemd.git 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. --- diff --git a/docs/HACKING.md b/docs/HACKING.md index 85adca5974e..25ad0aa17e0 100644 --- a/docs/HACKING.md +++ b/docs/HACKING.md @@ -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": "/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 +``` diff --git a/mkosi/mkosi.clangd b/mkosi/mkosi.clangd index 44c1128a91e..f2e741f0faa 100755 --- a/mkosi/mkosi.clangd +++ b/mkosi/mkosi.clangd @@ -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 index 4a3c977a780..00000000000 --- a/mkosi/mkosi.images/build/mkosi.build.chroot +++ /dev/null @@ -1,7 +0,0 @@ -#!/bin/bash -# SPDX-License-Identifier: LGPL-2.1-or-later -set -e - -if [[ "$1" == "clangd" ]]; then - exec "$@" -fi