From: Daan De Meyer Date: Tue, 21 Apr 2026 20:31:22 +0000 (+0000) Subject: bpf: register compile_commands.json entries for bpf programs X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=b779c52d4c26dc3d241458106495ca844de7fa96;p=thirdparty%2Fsystemd.git bpf: register compile_commands.json entries for bpf programs compile_commands.json is generated by ninja from c_COMPILER rules, so BPF programs (built via custom_target and thus emitted as CUSTOM_COMMAND rules) never show up there. Clangd consequently falls back to heuristics when opening .bpf.c files, with poor diagnostic fidelity. Register a meson postconf script per BPF program that upserts an entry into compile_commands.json using the same argv meson constructed for the custom_target. The script runs after meson has written the DB, substitutes @INPUT@/@OUTPUT@, and keys entries by source path so repeated reconfigures don't accumulate duplicates. --- diff --git a/src/bpf/merge-bpf-compdb.py b/src/bpf/merge-bpf-compdb.py new file mode 100755 index 00000000000..c93e685afd9 --- /dev/null +++ b/src/bpf/merge-bpf-compdb.py @@ -0,0 +1,40 @@ +#!/usr/bin/env python3 +# SPDX-License-Identifier: LGPL-2.1-or-later + +import json +import os +import sys + + +def main() -> int: + build_root = os.environ['MESON_BUILD_ROOT'] + + sep = sys.argv.index('--') + sources = sys.argv[1:sep] + command = sys.argv[sep + 1:] + + db_path = os.path.join(build_root, 'compile_commands.json') + try: + with open(db_path) as f: + db = json.load(f) + except FileNotFoundError: + db = [] + + sources_set = set(sources) + db = [entry for entry in db if entry['file'] not in sources_set] + + for source in sources: + db.append({ + 'directory': build_root, + 'file': source, + 'arguments': [source if a == '@INPUT@' else a for a in command], + }) + + with open(db_path, 'w') as f: + json.dump(db, f, indent=2) + + return 0 + + +if __name__ == '__main__': + sys.exit(main()) diff --git a/src/bpf/meson.build b/src/bpf/meson.build index 54b3027a97c..32ab32e93dc 100644 --- a/src/bpf/meson.build +++ b/src/bpf/meson.build @@ -343,6 +343,7 @@ bpf_programs = [ ] bpf_programs_by_name = {} +bpf_sources = [] foreach program : bpf_programs if conf.get(program['condition']) != 1 @@ -350,6 +351,7 @@ foreach program : bpf_programs endif source = program['source'][0] + # Strip .bpf.c extension name = fs.stem(fs.stem(source)) bpf_o_unstripped = custom_target( @@ -382,4 +384,14 @@ foreach program : bpf_programs bpf_programs_by_name += { name : skel_h } generated_sources += skel_h + bpf_sources += source endforeach + +if bpf_sources.length() > 0 + meson.add_postconf_script( + python, + files('merge-bpf-compdb.py'), + bpf_sources, + '--', + bpf_o_unstripped_cmd) +endif