Received: by 2002:a05:6a10:f3d0:0:0:0:0 with SMTP id a16csp3278518pxv; Sun, 4 Jul 2021 13:30:48 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzpv3YEmD0iUTxP2sIWHHHtOUchamcI71cWPsGAk2I0lWHtfDcSvIaLVs8EsQWpCLeF1/ke X-Received: by 2002:a02:3845:: with SMTP id v5mr9212604jae.119.1625430648604; Sun, 04 Jul 2021 13:30:48 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1625430648; cv=none; d=google.com; s=arc-20160816; b=08yZRvorIole4F/pF0tgYYDsN/M9Ht4V8mXh7ZYYogCCImTM6Y+ggzNBNeza1z1cTC +vrgIwtL4FVcsJi/19hR0O8/032vSDo0Uy9mcBmhZeOOzLGOa+VpqEBxY31OiTyM26ps AZcQAuAB7bvrYOP1inFinN2r8ky7quDodweNUhOBt0Di7BwV0gD8RJSqs149qPNTV/a0 IRvChxY78+TGIdRVUMvDVsYcihFEFasNo6zraFVNBMGkLcWYPY9kg1PNuEVHCZI1oAnW r02A8kBHgOwfXottrBBRuWSVRSRRpwkm5lr18q2teAQ4MsVvnztda9i3sW3I2mq6qLzD BwnA== 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=tjlBnCcIE4IWgwLYD+Lm72NPpY42dI46Mb2+ckQryXI=; b=agMw7GPOQq8FuKRnXpVpAfeIQ2mIRYgNiVfXIGDKtQVBdmMh1iPdjz+usbkGV4FKyC lJD0FQGZ2JCvyxJnWdkYJqQVcITBjd6/P2GaloM3YDOOH8yp1Jrm8Yjhb12eodTDdUIC W9TiYTkgkx4lTkt6trdMwI0bXMhGT9HhItskJyLsUbYOS+mOzuoaxWHcMbSOjMRtGNew 8gMbe0M+go0GrKBlma47yEDyXFMV0HcZXZV3l2QwOla8sEAuSlbMJmNRvygBoh7Wxo5z LlvZs+nIb4NrF3lDSH0+Gc74QtSWH7eOppcHTqjLMnuajbqWhPGusdKSSupJbNo1jLBV MSwQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=KMltLd5Y; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 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 vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id d6si12677218ilu.46.2021.07.04.13.30.37; Sun, 04 Jul 2021 13:30:48 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=KMltLd5Y; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 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 S230398AbhGDUci (ORCPT + 99 others); Sun, 4 Jul 2021 16:32:38 -0400 Received: from mail.kernel.org ([198.145.29.99]:37698 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230396AbhGDUcU (ORCPT ); Sun, 4 Jul 2021 16:32:20 -0400 Received: by mail.kernel.org (Postfix) with ESMTPSA id 73383613DD; Sun, 4 Jul 2021 20:29:40 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1625430584; bh=pPDP+a5TXy0UR1cWe+2R+eaQaK2LjTd/t+sv/xsilhw=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=KMltLd5YzHT3x1uHVwp+UzxuzaB24SWu0STWeTQ3d3uozmeGBBRfmdFxDEfnyEklH hj5blhGhmJKpNfP4D4MVfd4HlWiLtOc2wrMs+oiCCtWVnrv5gksz/RFpLQ3OiZ2FKr zBLVcqsdAOX29AjLihsDRs/6yyy0EG00SYVwLwa9VTQpFK1rXU6+uy64JgixNnxBad 7lytrpBjGJFQCs1UEQ9fkv+fPdC4kj4Ve/gD4tW6AJaMN8fFmS8hsOJ4f1pSsWvz67 7uPBb5ymyMiQHes0xuXhtD1lobQaGGQdRWUQLXeh69K56Nq9CQCrFfsWCLiZXG/i+W lOs83qb++CC6g== From: ojeda@kernel.org To: Linus Torvalds , Greg Kroah-Hartman Cc: rust-for-linux@vger.kernel.org, linux-kbuild@vger.kernel.org, linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org, Miguel Ojeda , Alex Gaynor , Geoffrey Thomas , Finn Behrens , Adam Bratschi-Kaye , Wedson Almeida Filho , Boqun Feng , Sumera Priyadarsini , Michael Ellerman , Sven Van Asbroeck , Gary Guo , Boris-Chengbiao Zhou , Fox Chen , Ayaan Zaidi , Douglas Su , Yuki Okushi Subject: [PATCH 15/17] scripts: add `generate_rust_analyzer.py` Date: Sun, 4 Jul 2021 22:27:54 +0200 Message-Id: <20210704202756.29107-16-ojeda@kernel.org> In-Reply-To: <20210704202756.29107-1-ojeda@kernel.org> References: <20210704202756.29107-1-ojeda@kernel.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Miguel Ojeda 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: Geoffrey Thomas Signed-off-by: Geoffrey Thomas Co-developed-by: Finn Behrens Signed-off-by: Finn Behrens Co-developed-by: Adam Bratschi-Kaye Signed-off-by: Adam Bratschi-Kaye Co-developed-by: Wedson Almeida Filho Signed-off-by: Wedson Almeida Filho Co-developed-by: Boqun Feng Signed-off-by: Boqun Feng Co-developed-by: Sumera Priyadarsini Signed-off-by: Sumera Priyadarsini Co-developed-by: Michael Ellerman Signed-off-by: Michael Ellerman Co-developed-by: Sven Van Asbroeck Signed-off-by: Sven Van Asbroeck 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: Fox Chen Signed-off-by: Fox Chen Co-developed-by: Ayaan Zaidi Signed-off-by: Ayaan Zaidi Co-developed-by: Douglas Su Signed-off-by: Douglas Su Co-developed-by: Yuki Okushi Signed-off-by: Yuki Okushi Signed-off-by: Miguel Ojeda --- scripts/generate_rust_analyzer.py | 143 ++++++++++++++++++++++++++++++ 1 file changed, 143 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 00000000000..72c453e1aea --- /dev/null +++ b/scripts/generate_rust_analyzer.py @@ -0,0 +1,143 @@ +#!/usr/bin/env python3 +"""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, bindings_file): + # 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, is_workspace_member, deps, cfg): + crates_indexes[display_name] = len(crates) + crates.append({ + "display_name": display_name, + "root_module": str(root_module), + "is_workspace_member": is_workspace_member, + "deps": [{"crate": crates_indexes[dep], "name": dep} for dep in deps], + "cfg": cfg, + "edition": "2018", + "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", + False, + [], + [], + ) + + append_crate( + "compiler_builtins", + srctree / "rust" / "compiler_builtins.rs", + True, + [], + [], + ) + + append_crate( + "alloc", + srctree / "rust" / "alloc" / "lib.rs", + True, + ["core", "compiler_builtins"], + [], + ) + + append_crate( + "macros", + srctree / "rust" / "macros" / "lib.rs", + True, + [], + [], + ) + crates[-1]["proc_macro_dylib_path"] = "rust/libmacros.so" + + append_crate( + "build_error", + srctree / "rust" / "build_error.rs", + True, + ["core", "compiler_builtins"], + [], + ) + + append_crate( + "kernel", + srctree / "rust" / "kernel" / "lib.rs", + True, + ["core", "alloc", "macros", "build_error"], + cfg, + ) + crates[-1]["env"]["RUST_BINDINGS_FILE"] = str(bindings_file.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, + True, + ["core", "alloc", "kernel"], + 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) + parser.add_argument("bindings_file", 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, args.bindings_file), + "sysroot_src": str(args.sysroot_src), + } + + json.dump(rust_project, sys.stdout, sort_keys=True, indent=4) + +if __name__ == "__main__": + main() -- 2.32.0