From 1d5a68660f09d6c6004c39f62769747f249062e6 Mon Sep 17 00:00:00 2001 From: iretq Date: Sat, 27 Dec 2025 03:03:44 +0100 Subject: [PATCH] gen_compile_db.py: add script --- gen_compile_db.py | 74 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 74 insertions(+) create mode 100755 gen_compile_db.py diff --git a/gen_compile_db.py b/gen_compile_db.py new file mode 100755 index 00000000..584a7148 --- /dev/null +++ b/gen_compile_db.py @@ -0,0 +1,74 @@ +#!/usr/bin/env python3 +# Generates a compile_commands.json file for the kernel + +import os +import pathlib +import json + +path = pathlib.Path(__file__).resolve() +kernel_source = path.parent / 'kernel-src' +cflags = [] + +with open(kernel_source / 'Makefile', 'r') as f: + lines = f.readlines() + + def find_var(name): + line = next(x for x in lines if x.startswith(f'{name}=')) + value = line.strip().split('=', 1)[1].strip() + + # Handle comment + if '#' in value: + value = value.split('#', 1)[0].strip() + + return value + + cflags_var = find_var('CFLAGS').split() + uacpiopts_var = find_var('UACPIOPTS').split() + printfopts_var = find_var('PRINTFOPTS').split() + kernelconfig_var = find_var('KERNELCONFIG').split() + flantermopts_var = find_var('FLANTERMOPTS').split() + + incdir = kernel_source / 'include' + archincdir = kernel_source / 'include' / 'x86-64' + flantermincdir = kernel_source / 'flanterm' + uacpiincdir = kernel_source / 'io' / 'acpi' / 'uacpi' / 'include' + + for f in cflags_var: + if f == '"$(UACPIINCDIR)"': + cflags.append(str(uacpiincdir)) + elif f == '"$(INCDIR)"': + cflags.append(str(incdir)) + elif f == '"$(ARCHINCDIR)"': + cflags.append(str(archincdir)) + elif f == '"$(FLANTERMINCDIR)"': + cflags.append(str(flantermincdir)) + elif f == '$(UACPIOPTS)': + cflags.extend(uacpiopts_var) + elif f == '$(PRINTFOPTS)': + cflags.extend(printfopts_var) + elif f == '$(KERNELCONFIG)': + cflags.extend(kernelconfig_var) + elif f == '$(FLANTERMOPTS)': + cflags.extend(flantermopts_var) + else: + cflags.append(f) + +csources = [ + os.path.join(root, f) + for root, _, files in os.walk(kernel_source) + for f in files + if f.endswith('.c') and '/uacpi/tests/' not in os.path.join(root, f) +] + +compile_commands = [] + +for src in csources: + command = { + 'directory': str(kernel_source), + 'arguments': ['gcc', *cflags, '-c', src], + 'file': src + } + compile_commands.append(command) + +with open(kernel_source / 'compile_commands.json', 'w') as f: + json.dump(compile_commands, f, indent=2)