Received: by 2002:a05:6602:18e:0:0:0:0 with SMTP id m14csp1671703ioo; Sun, 22 May 2022 23:50:36 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxi/P4mfrxtDJeNGjX+s3VD5CJUf9Bmg62k++uJGVp9gWBLpJikQ1ImzKK6Y9xRH7gR74xz X-Received: by 2002:a62:1b06:0:b0:518:1649:bb6d with SMTP id b6-20020a621b06000000b005181649bb6dmr22540901pfb.25.1653288636665; Sun, 22 May 2022 23:50:36 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1653288636; cv=none; d=google.com; s=arc-20160816; b=t3kPj3rV8FTz5WtB08orEjQrA4T8SpwQkjU7EMPXpLmCyVteScM8JDF2cR0wijajve a4KJHNFUKmSHSlxJdcP2dJuWyMPNW8ATdBddWQAFES2k5xcSiGwHeJG4dC3VNyC2bJjF NQF3lhcf9urSS2vBsYgudau0hqgvWlBHBmMzwxvTqCOKMJW4r0peR0cy7eZ1LcK+2qSH olNtmTavRCbSiIzgjq0QiXzHABKLGkqR3LQ/LvSNS2JiMLczrPaQxovdSN5S+WkzfI/T 7QUSa3saOlZd4clamTEuPJxMsR5ZVSD1rMw/MvPYgVBHlNYG9F0EyKaN4US9/vaHrKvI lAag== 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=HzGm3lnsEQoh54E8l8oswEtZU0q68/dyzQBvVCBof7M=; b=dKSRquoyEv5WricLhxmXQcx8xJ5V7/pDWaQYKHf8/NnOjJcbCTG5G+LPsW01uwW6wM vqVHIECzbxdGrZy1sAN1a+oiYg0oqMwguDw+Q3CgL/vDmKZhYKEgn4ZNuFaFu3Lap9a9 WWnQZOSovQoJPXJVNNtmcaQfuTlPybocnRmIBLqDnqhFoaanRwH6O5ocnTvlhT0pU6Xg 9QFRz8ZoseYlcfJpOBkhZnivDLWNcfXxqA3DvPBKThkUyjWDW8CN74L57+p3bBpDYe19 6gsNnaH09O+l5ozKxxS/TCk6M6p3b5IG8Nn7FN3lGfCXDi+JL/4MztJTYLEGo+dES/lG Fvjg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=lruh8V8b; spf=softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 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 lindbergh.monkeyblade.net (lindbergh.monkeyblade.net. [23.128.96.19]) by mx.google.com with ESMTPS id h9-20020a63e149000000b003a9fd439348si8188627pgk.582.2022.05.22.23.50.36 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 22 May 2022 23:50:36 -0700 (PDT) Received-SPF: softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 as permitted sender) client-ip=23.128.96.19; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=lruh8V8b; spf=softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id EDE3D41982; Sun, 22 May 2022 23:19:20 -0700 (PDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1352728AbiEWCF7 (ORCPT + 99 others); Sun, 22 May 2022 22:05:59 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40628 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1352651AbiEWCFH (ORCPT ); Sun, 22 May 2022 22:05:07 -0400 Received: from ams.source.kernel.org (ams.source.kernel.org [145.40.68.75]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E5FB839B9C; Sun, 22 May 2022 19:04:18 -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 ams.source.kernel.org (Postfix) with ESMTPS id 6DCFEB80EA4; Mon, 23 May 2022 02:04:17 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 9A6D6C34113; Mon, 23 May 2022 02:04:13 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1653271456; bh=r/RrOkadtDbdK1uBCMMOvDB064Bog4lQNJGV/GV3+ts=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=lruh8V8bbpIoX+gxeZcvbsYuzfwbmHv75U5j1u5nKcVxsseETPmRmwc488xbsD/Fc 5dGEGJqGsIs3vNQS9EKCtPfue4L4me9MjQNplVnMbTY03sxNvChCeaJ+x6Swh9xypG WebVULNkTgKmsddIfi+692utvSYb4hBft4S1Ky8AZqQfrskcqhdEefhIjbxZJ3cnm7 AsWzT/IxJMfNaW+ETLnbKdFaWpyAX0eMpvo5sTcqqzG0zf/TAutrT8xShNV07cosVS rLnTb+XqXzcQ/1I+aTKsQ7uewFPEBw10CP//72plVS/lKoPo6U4a/RUJskflyD51Gm QIKEzx8q/0WSw== From: Miguel Ojeda To: Linus Torvalds , Greg Kroah-Hartman Cc: rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org, Jarkko Sakkinen , Miguel Ojeda , Alex Gaynor , Finn Behrens , Wedson Almeida Filho , Gary Guo , Boris-Chengbiao Zhou Subject: [PATCH v7 18/25] scripts: add `generate_rust_analyzer.py` scripts Date: Mon, 23 May 2022 04:01:31 +0200 Message-Id: <20220523020209.11810-19-ojeda@kernel.org> In-Reply-To: <20220523020209.11810-1-ojeda@kernel.org> References: <20220523020209.11810-1-ojeda@kernel.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-2.9 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,MAILING_LIST_MULTI, RDNS_NONE,SPF_HELO_NONE,T_SCC_BODY_TEXT_LINE autolearn=unavailable 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. 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 Signed-off-by: Miguel Ojeda --- scripts/generate_rust_analyzer.py | 134 ++++++++++++++++++++++++++++++ 1 file changed, 134 insertions(+) create mode 100755 scripts/generate_rust_analyzer.py diff --git a/scripts/generate_rust_analyzer.py b/scripts/generate_rust_analyzer.py new file mode 100755 index 000000000000..37c049fb18f2 --- /dev/null +++ b/scripts/generate_rust_analyzer.py @@ -0,0 +1,134 @@ +#!/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( + "build_error", + srctree / "rust" / "build_error.rs", + ["core", "compiler_builtins"], + ) + + append_crate( + "kernel", + srctree / "rust" / "kernel" / "lib.rs", + ["core", "alloc", "macros", "build_error"], + cfg=cfg, + ) + crates[-1]["env"]["OBJTREE"] = str(objtree.resolve(True)) + 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.36.1