Skip to content

Commit

Permalink
Add clangd wrapper script
Browse files Browse the repository at this point in the history
  • Loading branch information
septatrix committed Nov 22, 2024
1 parent 1477732 commit ff63747
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 0 deletions.
8 changes: 8 additions & 0 deletions modules/kernel/mkosi.build.chroot
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,14 @@ export KBUILD_BUILD_USER="mkosi"

make $EXTRA -j "$(nproc)" $( ((INCREMENTAL)) && echo modules)

# We put the compile_commands.json in /work/build
# because we do not have access to the kernel-specific build directory when starting clangd.
# Also we have to hardcode /work/build as the target because the original BUILDDIR
# variable has been reassigned.
scripts/clang-tools/gen_compile_commands.py -d "$BUILDDIR" -o /work/build/compile_commands.json
# This currently puts the clang-format on the host :/
cp kernel/.clang-format .

# FIXME: Re-enable when coreutils 9.5 gets into debian testing.
. /usr/lib/os-release
if ! ((INCREMENTAL)) && [ ! "$ID" = "debian" ]; then
Expand Down
37 changes: 37 additions & 0 deletions modules/kernel/mkosi.clangd
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#!/bin/bash
# SPDX-License-Identifier: LGPL-2.1-or-later
# Verbatim copy of mkosi.clangd from systemd repo tag v257-rc2

set -eu

if [ -z "${MKOSI_CONFIG-}" ]; then
exec mkosi \
--incremental=strict \
--build-sources-ephemeral=no \
--format=none \
--build-script= \
--build-script="$(realpath "$0")" \
build
fi

DISTRIBUTION="$(jq -r .Distribution "$MKOSI_CONFIG")"
RELEASE="$(jq -r .Release "$MKOSI_CONFIG")"
ARCH="$(jq -r .Architecture "$MKOSI_CONFIG")"

BUILD_DIRECTORY="$(jq -r .BuildDirectory "$MKOSI_CONFIG")"
CACHE_DIRECTORY="$(jq -r .CacheDirectory "$MKOSI_CONFIG")"
# This neglects build sources which have an absolute path as a target
# but that would make the jq filter more complex
# and can still added later if someone has a need for this.
BUILD_SOURCE_MAPPINGS="$(jq -r '[
.BuildSources[]
| .Source + "/='"$SRCDIR"'/" + .Target
] | join(",")' "$MKOSI_CONFIG")"

exec clangd \
--compile-commands-dir="$BUILDDIR" \
--path-mappings="\
$BUILD_SOURCE_MAPPINGS,\
$BUILD_DIRECTORY/=$BUILDDIR/,\
$CACHE_DIRECTORY/$DISTRIBUTION~$RELEASE~$ARCH~build.cache/usr/include/=$BUILDROOT/usr/include/" \
"$@"

0 comments on commit ff63747

Please sign in to comment.