From patchwork Wed May 8 10:56:10 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Benjamin Berg X-Patchwork-Id: 1932997 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (2048-bit key; secure) header.d=lists.infradead.org header.i=@lists.infradead.org header.a=rsa-sha256 header.s=bombadil.20210309 header.b=wzyLs4SM; dkim=fail reason="signature verification failed" (2048-bit key; secure) header.d=sipsolutions.net header.i=@sipsolutions.net header.a=rsa-sha256 header.s=mail header.b=JwDiJnOP; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=none (no SPF record) smtp.mailfrom=lists.infradead.org (client-ip=2607:7c80:54:3::133; helo=bombadil.infradead.org; envelope-from=hostap-bounces+incoming=patchwork.ozlabs.org@lists.infradead.org; receiver=patchwork.ozlabs.org) Received: from bombadil.infradead.org (bombadil.infradead.org [IPv6:2607:7c80:54:3::133]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (secp384r1) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4VZBsv65hkz214L for ; Wed, 8 May 2024 20:56:51 +1000 (AEST) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:MIME-Version:References:In-Reply-To: Message-ID:Date:Subject:Cc:To:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=gFu7DeC364pOmiEIrx4bs5YJoup8a5H1QlVjaKVA23w=; b=wzyLs4SMJv/qym mdd4v3arNhRb4rc0RAMIRjG2GW5CVU4VNkf/H6htD30wvwBYQsy/ZZu7nwCnME7+rijwNVZUibCs7 CHRp9165+JaG/44ja/vkGGHp2Wy+WaPFYcnobFIlffsHT9MGZ1oEsVzIJRGc8VuaVSHK0R62gx41H PcNn4Z9SN16Y96Cf9TrTzcw0TvLZoToTN+9HwrK63i3mVnZC/rd+2Tej9XITGF5fC+/JLQ5jFNqK2 2uEFwKhwWMJvEp/oopPYV1wS7Rj5FGLtYpCWk6fPXPu4zWqrNpojNPWRPq592HLu4Ibf7agd96212 qV04iDShHK9tZVXzHQVA==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1s4eyK-0000000F4og-26Eu; Wed, 08 May 2024 10:56:28 +0000 Received: from s3.sipsolutions.net ([2a01:4f8:242:246e::2] helo=sipsolutions.net) by bombadil.infradead.org with esmtps (Exim 4.97.1 #2 (Red Hat Linux)) id 1s4eyG-0000000F4ny-2Kgq for hostap@lists.infradead.org; Wed, 08 May 2024 10:56:26 +0000 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=sipsolutions.net; s=mail; h=Content-Transfer-Encoding:MIME-Version: References:In-Reply-To:Message-ID:Date:Subject:Cc:To:From:Content-Type:Sender :Reply-To:Content-ID:Content-Description:Resent-Date:Resent-From:Resent-To: Resent-Cc:Resent-Message-ID; bh=6+NoYH0Xy2mSpZFRxMOlg/vqEShyZZX/BwfXSS+nPss=; t=1715165784; x=1716375384; b=JwDiJnOPNkopVs9qDj3tp6viTFTrQm7ByNndvXgYH0Nis8H 3BsNorPjyADd3y0gV8ldqmIgmh/dAKZ9VG6U7CE/BpuhiOgFJSM6aRoLDIeJnrFSE7lgkKONK8KEK FIT/9G25Zr/4IoN9PNhXFE4sjMneKLECyM4OHweh1BYkuOmeiOpAHX5odfLY7dPwREJDrMyiRXLFI iFcvHUJiSeI+ZqSvz1yqzxMz3WtdgaQjD/xtbGhTqi5pq7lVffrnJFQDX2bJ8b2j8aUTLJwR3ICsx glDQ48eJdbT6H3Qe7UKiWUlopOwOZX9O1tUFRVKwH8+elRrpKhL1/HTSkAwKnm1Q==; Received: by sipsolutions.net with esmtpsa (TLS1.3:ECDHE_X25519__RSA_PSS_RSAE_SHA256__AES_256_GCM:256) (Exim 4.97) (envelope-from ) id 1s4eyD-0000000BGDP-3OOU; Wed, 08 May 2024 12:56:22 +0200 From: benjamin@sipsolutions.net To: hostap@lists.infradead.org Cc: Benjamin Berg Subject: [PATCH] build: Add simple compile_commands.json generation Date: Wed, 8 May 2024 12:56:10 +0200 Message-ID: <20240508105610.30837-1-benjamin@sipsolutions.net> X-Mailer: git-send-email 2.45.0 In-Reply-To: <20240508101727.27156-1-benjamin@sipsolutions.net> References: <20240508101727.27156-1-benjamin@sipsolutions.net> MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20240508_035624_703310_7DD5A109 X-CRM114-Status: GOOD ( 17.16 ) X-Spam-Score: -0.2 (/) X-Spam-Report: Spam detection software, running on the system "bombadil.infradead.org", has NOT identified this incoming email as spam. The original message has been attached to this so you can view it or label similar future email. If you have any questions, see the administrator of that system for details. Content preview: From: Benjamin Berg This can be used with a clangd server to get code completion and cross references in editor. To simplify the generation, create .cmd files for most object files while building that contains the base d [...] Content analysis details: (-0.2 points, 5.0 required) pts rule name description ---- ---------------------- -------------------------------------------------- -0.0 SPF_HELO_PASS SPF: HELO matches SPF record -0.0 SPF_PASS SPF: sender matches SPF record -0.1 DKIM_VALID Message has at least one valid DKIM or DK signature -0.1 DKIM_VALID_AU Message has a valid DKIM or DK signature from author's domain -0.1 DKIM_VALID_EF Message has a valid DKIM or DK signature from envelope-from domain 0.1 DKIM_SIGNED Message has a DKIM or DK signature, not necessarily valid X-BeenThere: hostap@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "Hostap" Errors-To: hostap-bounces+incoming=patchwork.ozlabs.org@lists.infradead.org From: Benjamin Berg This can be used with a clangd server to get code completion and cross references in editor. To simplify the generation, create .cmd files for most object files while building that contains the base directory and command that was used when compiling it. A very simple gen_compile_commands.py is provided which will read one or more build directories and generate the compile_commands.json file for it. Signed-off-by: Benjamin Berg --- v2: * Remove accidental content from commit message --- .gitignore | 3 +++ gen_compile_commands.py | 54 +++++++++++++++++++++++++++++++++++++++++ src/build.rules | 3 +++ 3 files changed, 60 insertions(+) create mode 100755 gen_compile_commands.py diff --git a/.gitignore b/.gitignore index b064303ce..00e23862a 100644 --- a/.gitignore +++ b/.gitignore @@ -6,3 +6,6 @@ wpaspy/build **/parallel-vm.log tags build/ +# clangd commands and cache +compile_commands.json +.cache diff --git a/gen_compile_commands.py b/gen_compile_commands.py new file mode 100755 index 000000000..6c3437daa --- /dev/null +++ b/gen_compile_commands.py @@ -0,0 +1,54 @@ +#!/usr/bin/env python3 +# compile_commands.json generator +# Copyright (C) 2024 Intel Corporation +# +# This software may be distributed under the terms of the BSD license. +# See README for more details. + +import os +import sys +import glob +import json +import argparse + +parser = argparse.ArgumentParser(description='Read each of the build directories that are given and generate ' + 'a compile_commands.json file. If a source file is found multiple times, ' + 'then the compile flags from the last build directory will be used.') +parser.add_argument('-o', '--output', default='compile_commands.json', type=str, + help="Output file to generate") +parser.add_argument('builddirs', nargs='+', type=str, metavar="builddir", + help='Build directories to search') + +args = parser.parse_args() + +files = {} + +for builddir in args.builddirs: + for cmd_file in glob.glob('**/*.o.cmd', root_dir=builddir, recursive=True): + with open(os.path.join(builddir, cmd_file), encoding='ascii') as f: + base_dir, cmd = f.readline().split(':', 1) + src_file = cmd.rsplit(maxsplit=1)[1] + + src_file = os.path.abspath(os.path.join(base_dir, src_file)) + files[src_file] = { + 'command': cmd.strip(), + 'directory': base_dir, + 'file': src_file, + } + +flist = json.dumps(sorted(list(files.values()), key=lambda k: k['file']), indent=2, sort_keys=True) + +try: + # Avoid writing the file if it did not change, first read original + with open(args.output, 'rt', encoding='UTF-8') as f: + orig = [] + while data := f.read(): + orig.append(data) + orig = ''.join(orig) +except OSError: + orig = '' + +# And only write if something changed +if orig != flist: + with open(args.output, 'wt', encoding='UTF-8') as f: + f.write(flist) diff --git a/src/build.rules b/src/build.rules index c756ccb84..eb6759ac2 100644 --- a/src/build.rules +++ b/src/build.rules @@ -83,13 +83,16 @@ _make_dirs: @mkdir -p $(sort $(_DIRS)) $(BUILDDIR)/$(PROJ)/src/%.o: $(ROOTDIR)src/%.c $(CONFIG_FILE) | _make_dirs + @echo $(CURDIR): '$(CC) -c -o $@ $(CFLAGS) $<' >$@.cmd $(Q)$(CC) -c -o $@ $(CFLAGS) $< @$(E) " CC " $< $(BUILDDIR)/$(PROJ)/%.o: %.c $(CONFIG_FILE) | _make_dirs + @echo $(CURDIR): '$(CC) -c -o $@ $(CFLAGS) $<' >$@.cmd $(Q)$(CC) -c -o $@ $(CFLAGS) $< @$(E) " CC " $< # for the fuzzing tests $(BUILDDIR)/$(PROJ)/wpa_supplicant/%.o: $(ROOTDIR)wpa_supplicant/%.c $(CONFIG_FILE) | _make_dirs + @echo $(CURDIR): '$(CC) -c -o $@ $(CFLAGS) $<' >$@.cmd $(Q)$(CC) -c -o $@ $(CFLAGS) $< @$(E) " CC " $<