Received: by 2002:a05:6359:c8b:b0:c7:702f:21d4 with SMTP id go11csp1006836rwb; Tue, 27 Sep 2022 07:19:20 -0700 (PDT) X-Google-Smtp-Source: AMsMyM6SdL4yJM/kgqNEkRekh6Vao2fPke33rrIE/bDzUr8gZBzWKbTqfpYfSo205kxXFowV1KJb X-Received: by 2002:a50:d79a:0:b0:457:d209:c09c with SMTP id w26-20020a50d79a000000b00457d209c09cmr776146edi.14.1664288360070; Tue, 27 Sep 2022 07:19:20 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1664288360; cv=none; d=google.com; s=arc-20160816; b=N7FXVo42BPDhKDMwsSeL66OOK1ZcYdvDlHSSWc58/vm6IESkAB8nFsV4iaLEa2IrCs ZlE9UhrHItipNNV83AsCvnqixYe352+HpkuXqRdOo7M8Vyum6wbtAPIoE1CzDbLwyonS XfHEGGiJS5vhx5HZnc6QiG7qb2DZ4zKYXn4So6yjE/gGnhQ2hZcfOqj+G7c/fDJCBjFB 4DZpnUxQSt7cpMq85NqsPypWoizxFIkV1CkR61k1bS6XQXJt6ZFgtSJk9bZnwAFFLIEi 67XU1CweG659HwRpr5m0BpDczQhTNe7aRzPgo7EcIUR3lt00c9flDv26LN81ujgiccjq fbLw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=GG3JTYJfa7dPxO2wnYJvhw5W8P/wi5DZDTD+6PRmlhU=; b=AobGdreYXyfvFb3ynBdg8B7gcKonwJCRj+hPdWRy5LI42IPSgl81tl1/TL8TlPqyC8 +WrizV1zcgFkCb5e9Gz2LeMIeFsslqMAqNFnZW47S0S9cQrmRzCNXuRekwJTbWvEBFnp R2zlf3XXL8kTkj5lbF+OmaUUpxYMKuZCcl9xhPp63WjzD6FxAJNtn+EIV+Lqz6hCHZS4 s0c3p3CwKJ58VD8e3o4M54cb831gA27MBQLQDbcRAUpk6Jedvjr3KtuISjKCIV0mwGLQ 8LFKTdpM94aFo95GDcnfGmjIr8E5IWg8SNsT5SrpejSSnrfpIIme4Rucdoc/WfYHbivq BPwA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=l4duhLbX; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id ds20-20020a170907725400b007803ce94339si1478627ejc.484.2022.09.27.07.18.54; Tue, 27 Sep 2022 07:19:20 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=l4duhLbX; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232832AbiI0NVE (ORCPT + 99 others); Tue, 27 Sep 2022 09:21:04 -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: 8bit X-Spam-Status: No, score=-7.2 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_HI, SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net 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örn Roy Baron Signed-off-by: Björn 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 # Documentation toolchain sphinx_*/ + +# Rust analyzer configuration +/rust-project.json diff --git a/scripts/generate_rust_analyzer.py b/scripts/generate_rust_analyzer.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 `rust-analyzer`. +""" + +import argparse +import json +import logging +import pathlib +import sys + +def generate_crates(srctree, objtree, sysroot_src): + # Generate the configuration list. + cfg = [] + with open(objtree / "include" / "generated" / "rustc_cfg") as fd: + for line in fd: + line = line.replace("--cfg=", "") + line = 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 = [] + crates_indexes = {} + + def append_crate(display_name, root_module, deps, cfg=[], is_workspace_member=True, is_proc_macro=False): + crates_indexes[display_name] = 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 in 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=False, + ) + + 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=True, + ) + crates[-1]["proc_macro_dylib_path"] = "rust/libmacros.so" + + append_crate( + "bindings", + srctree / "rust"/ "bindings" / "lib.rs", + ["core"], + cfg=cfg, + ) + crates[-1]["env"]["OBJTREE"] = str(objtree.resolve(True)) + + append_crate( + "kernel", + srctree / "rust" / "kernel" / "lib.rs", + ["core", "alloc", "macros", "bindings"], + cfg=cfg, + ) + crates[-1]["source"] = { + "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 = 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=cfg, + ) + + return crates + +def main(): + parser = argparse.ArgumentParser() + parser.add_argument('--verbose', '-v', action='store_true') + parser.add_argument("srctree", type=pathlib.Path) + parser.add_argument("objtree", type=pathlib.Path) + parser.add_argument("sysroot_src", type=pathlib.Path) + args = parser.parse_args() + + logging.basicConfig( + format="[%(asctime)s] [%(levelname)s] %(message)s", + level=logging.INFO if args.verbose else logging.WARNING + ) + + rust_project = { + "crates": generate_crates(args.srctree, args.objtree, args.sysroot_src), + "sysroot_src": str(args.sysroot_src), + } + + json.dump(rust_project, sys.stdout, sort_keys=True, indent=4) + +if __name__ == "__main__": + main() -- 2.37.3