From nobody Sat Apr 11 22:31:08 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 64D1AC6FA8E for ; Tue, 27 Sep 2022 13:21:09 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232834AbiI0NVG (ORCPT ); Tue, 27 Sep 2022 09:21:06 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37460 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232700AbiI0NTZ (ORCPT ); Tue, 27 Sep 2022 09:19:25 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id DC6208260D; Tue, 27 Sep 2022 06:17:50 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id 11ADA6197F; Tue, 27 Sep 2022 13:17:16 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id E36F8C433C1; Tue, 27 Sep 2022 13:17:11 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1664284635; bh=2AyqfdsRANUr2JCHnML/lxlV688B2B/d8RHpA6il5Dc=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=l4duhLbXVvf/Q7kCJcEPzq3/lPrzl05/oDPRIqLG42CbnL7Rnn0b2d813sJNHbYMT JXvNsQnZRi+IqGblOs3FoNxFGHKLqTZK1vNrcRdpc1hGiFGqkHz3871QGNZx95HbXy 9TKYEXLusdWIDdpOGMjTWV2QdxjgUULFYuC9ymcz+821T59/hiXoKEzOQim100i5/e MvE1tG8D49ayoiJ9Ln0yVm8TRKJB969a6E1HhqBnsT6Qy3z+LN6oLN1t1iEqXKJgbX tLLB66CdPe0zoIWPU1Y42pu94b56pvHCzPYFt72NhJAN8oBplPzRyQzOPyWkS7ILqb avV6N3GsBXkuQ== From: Miguel Ojeda To: Linus Torvalds , Greg Kroah-Hartman Cc: rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org, linux-fsdevel@vger.kernel.org, patches@lists.linux.dev, Jarkko Sakkinen , Miguel Ojeda , Kees Cook , Alex Gaynor , Finn Behrens , Wedson Almeida Filho , Gary Guo , Boris-Chengbiao Zhou , =?UTF-8?q?Bj=C3=B6rn=20Roy=20Baron?= , Boqun Feng Subject: [PATCH v10 18/27] scripts: add `generate_rust_analyzer.py` Date: Tue, 27 Sep 2022 15:14:49 +0200 Message-Id: <20220927131518.30000-19-ojeda@kernel.org> In-Reply-To: <20220927131518.30000-1-ojeda@kernel.org> References: <20220927131518.30000-1-ojeda@kernel.org> MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org The `generate_rust_analyzer.py` script generates the configuration file (`rust-project.json`) for rust-analyzer. rust-analyzer is a modular compiler frontend for the Rust language. It provides an LSP server which can be used in editors such as VS Code, Emacs or Vim. Reviewed-by: Kees Cook Co-developed-by: Alex Gaynor Signed-off-by: Alex Gaynor Co-developed-by: Finn Behrens Signed-off-by: Finn Behrens Co-developed-by: Wedson Almeida Filho Signed-off-by: Wedson Almeida Filho Co-developed-by: Gary Guo Signed-off-by: Gary Guo Co-developed-by: Boris-Chengbiao Zhou Signed-off-by: Boris-Chengbiao Zhou Co-developed-by: Bj=C3=B6rn Roy Baron Signed-off-by: Bj=C3=B6rn Roy Baron Signed-off-by: Miguel Ojeda --- .gitignore | 3 + scripts/generate_rust_analyzer.py | 135 ++++++++++++++++++++++++++++++ 2 files changed, 138 insertions(+) create mode 100755 scripts/generate_rust_analyzer.py diff --git a/.gitignore b/.gitignore index 265959544978..80989914c97d 100644 --- a/.gitignore +++ b/.gitignore @@ -162,3 +162,6 @@ x509.genkey =20 # Documentation toolchain sphinx_*/ + +# Rust analyzer configuration +/rust-project.json diff --git a/scripts/generate_rust_analyzer.py b/scripts/generate_rust_anal= yzer.py new file mode 100755 index 000000000000..75bb611bd751 --- /dev/null +++ b/scripts/generate_rust_analyzer.py @@ -0,0 +1,135 @@ +#!/usr/bin/env python3 +# SPDX-License-Identifier: GPL-2.0 +"""generate_rust_analyzer - Generates the `rust-project.json` file for `ru= st-analyzer`. +""" + +import argparse +import json +import logging +import pathlib +import sys + +def generate_crates(srctree, objtree, sysroot_src): + # Generate the configuration list. + cfg =3D [] + with open(objtree / "include" / "generated" / "rustc_cfg") as fd: + for line in fd: + line =3D line.replace("--cfg=3D", "") + line =3D line.replace("\n", "") + cfg.append(line) + + # Now fill the crates list -- dependencies need to come first. + # + # Avoid O(n^2) iterations by keeping a map of indexes. + crates =3D [] + crates_indexes =3D {} + + def append_crate(display_name, root_module, deps, cfg=3D[], is_workspa= ce_member=3DTrue, is_proc_macro=3DFalse): + crates_indexes[display_name] =3D len(crates) + crates.append({ + "display_name": display_name, + "root_module": str(root_module), + "is_workspace_member": is_workspace_member, + "is_proc_macro": is_proc_macro, + "deps": [{"crate": crates_indexes[dep], "name": dep} for dep i= n deps], + "cfg": cfg, + "edition": "2021", + "env": { + "RUST_MODFILE": "This is only for rust-analyzer" + } + }) + + # First, the ones in `rust/` since they are a bit special. + append_crate( + "core", + sysroot_src / "core" / "src" / "lib.rs", + [], + is_workspace_member=3DFalse, + ) + + append_crate( + "compiler_builtins", + srctree / "rust" / "compiler_builtins.rs", + [], + ) + + append_crate( + "alloc", + srctree / "rust" / "alloc" / "lib.rs", + ["core", "compiler_builtins"], + ) + + append_crate( + "macros", + srctree / "rust" / "macros" / "lib.rs", + [], + is_proc_macro=3DTrue, + ) + crates[-1]["proc_macro_dylib_path"] =3D "rust/libmacros.so" + + append_crate( + "bindings", + srctree / "rust"/ "bindings" / "lib.rs", + ["core"], + cfg=3Dcfg, + ) + crates[-1]["env"]["OBJTREE"] =3D str(objtree.resolve(True)) + + append_crate( + "kernel", + srctree / "rust" / "kernel" / "lib.rs", + ["core", "alloc", "macros", "bindings"], + cfg=3Dcfg, + ) + crates[-1]["source"] =3D { + "include_dirs": [ + str(srctree / "rust" / "kernel"), + str(objtree / "rust") + ], + "exclude_dirs": [], + } + + # Then, the rest outside of `rust/`. + # + # We explicitly mention the top-level folders we want to cover. + for folder in ("samples", "drivers"): + for path in (srctree / folder).rglob("*.rs"): + logging.info("Checking %s", path) + name =3D path.name.replace(".rs", "") + + # Skip those that are not crate roots. + if f"{name}.o" not in open(path.parent / "Makefile").read(): + continue + + logging.info("Adding %s", name) + append_crate( + name, + path, + ["core", "alloc", "kernel"], + cfg=3Dcfg, + ) + + return crates + +def main(): + parser =3D argparse.ArgumentParser() + parser.add_argument('--verbose', '-v', action=3D'store_true') + parser.add_argument("srctree", type=3Dpathlib.Path) + parser.add_argument("objtree", type=3Dpathlib.Path) + parser.add_argument("sysroot_src", type=3Dpathlib.Path) + args =3D parser.parse_args() + + logging.basicConfig( + format=3D"[%(asctime)s] [%(levelname)s] %(message)s", + level=3Dlogging.INFO if args.verbose else logging.WARNING + ) + + rust_project =3D { + "crates": generate_crates(args.srctree, args.objtree, args.sysroot= _src), + "sysroot_src": str(args.sysroot_src), + } + + json.dump(rust_project, sys.stdout, sort_keys=3DTrue, indent=3D4) + +if __name__ =3D=3D "__main__": + main() --=20 2.37.3