Received: by 2002:a6b:500f:0:0:0:0:0 with SMTP id e15csp4734320iob; Sun, 8 May 2022 23:40:31 -0700 (PDT) X-Google-Smtp-Source: ABdhPJz5wDznhytwjfYHTQztLsCYPFqec7jFy4dEc8xtfMKKnV1iJCQ75EkWrOA+C9io5BN3cMQ7 X-Received: by 2002:a17:90b:4a4a:b0:1dc:4731:31a4 with SMTP id lb10-20020a17090b4a4a00b001dc473131a4mr16774132pjb.19.1652078431450; Sun, 08 May 2022 23:40:31 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1652078431; cv=none; d=google.com; s=arc-20160816; b=SecHRD7mZCuyPSj4hOeSxSGHiOrFtIe466bw029JgvnyhMGmC0nwG53fY+gGbise0u fyV347nqA4lkSlB9C5Mldf7KEWR9jAkLHZVnfZGStZ21u1qdS0bjLs5EkRypU17HWsl6 SYeVq8GhqC9dx/aXCitRmi0yyJV73zfthIIPgEswmNdY/dTEnaT61KGVCjXW46De148k axVmPZC8e77thW5thQrzEAioxIMLUhrEgkJwY5uqWKq4FGJ5I53Cm7UwENHZjB4L6Tqf 9mtGKfSsjARWzb3+Mxw4VQFgaruv00CZRERqa6jOLMK/1kw73vT7cq7G0ClWxciZthMD ETpQ== 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=g+cImDkQbkHlDQWRVJxUTo5HF1W1UqxmrY+yMIr2UgM=; b=ENQ6cBCHlF5I011UCg56+NT+FMBNkEwUnvyK4G94hFbqLoCYhhRrsdwy0/vOJV5icE HZLM6glVgiFglYgm8gG9FNnEkZavpCZ0wPkFiZGMavNAQrt8KrqLE08kWTnJG3cPK84r +HQy0VQaGkFZgoqsO2utQonYJpOAZUWrApAkSeaEjAnQM3ZPIJ9Zi4+3qnUJE51b1Tyh P9n59lkX+fBPKnbj6tvzI1cwdzcHrWU1PrR6+WbSIvOAgxvmROYaquI6m/f8apfs/Kq8 e53GUwls//M3GsxkfLbm7Bn0ArKNk1CHGXJCmbujp1Ui+OvcJDOtAyPj/oFCzMYlpGtr mHaA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=OGq17qup; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1: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 lindbergh.monkeyblade.net (lindbergh.monkeyblade.net. [2620:137:e000::1:18]) by mx.google.com with ESMTPS id a6-20020a170902ecc600b0015e7a2f9741si11877849plh.395.2022.05.08.23.40.31 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 08 May 2022 23:40:31 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 as permitted sender) client-ip=2620:137:e000::1:18; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=OGq17qup; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1: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: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 4AEE8177063; Sun, 8 May 2022 23:38:00 -0700 (PDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1445692AbiEGFdA (ORCPT + 99 others); Sat, 7 May 2022 01:33:00 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:57492 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1445704AbiEGFa4 (ORCPT ); Sat, 7 May 2022 01:30:56 -0400 Received: from ams.source.kernel.org (ams.source.kernel.org [IPv6:2604:1380:4601:e00::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 36D9F69CC3; Fri, 6 May 2022 22:27:09 -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 EAFEDB80CF3; Sat, 7 May 2022 05:27:07 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 3EA39C385A9; Sat, 7 May 2022 05:27:04 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1651901226; bh=Sxln/TZwIGd1Pfm1OPudQottOOoYz8MkuziHWI4cAzk=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=OGq17qupT4D1XZJk+eUbhrl8Rth/mLC2MxbDa2uH8SRQP5fGpQGXvuP0yKiDdM7Q1 dxiTOlJ9iAqUm1SW+aCvRxB6W8crXGGMpYQoOCrWoucC3RyBMylKXSFlo8J6DJ2yDl SLB+Kv7jRBgRFwZxOtRTl2OIjCann4rBjrSbgExFlSJtB8IU/eLoJ7VpUUZpwnQb8j TidH9TNZcAE90G7yq4wRw2WTLnNb1g/ZcwDA/1i0ggYQZ+eCf5OosMBAEq392kso83 L+Bmv3/SYfDMBN9x9QIuhGDdFuDWe591VCyzblui63QeqqR1LC8m3+W9S1+0WOahuz ytlENzFoZQGLg== 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 v6 16/23] scripts: add `generate_rust_analyzer.py` scripts Date: Sat, 7 May 2022 07:24:14 +0200 Message-Id: <20220507052451.12890-17-ojeda@kernel.org> In-Reply-To: <20220507052451.12890-1-ojeda@kernel.org> References: <20220507052451.12890-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.35.3