2 # SPDX-License-Identifier: GPL-2.0
3 """generate_rust_analyzer - Generates the `rust-project.json` file for `rust-analyzer`.
12 def generate_crates(srctree, objtree, sysroot_src):
13 # Generate the configuration list.
15 with open(objtree / "include" / "generated" / "rustc_cfg") as fd:
17 line = line.replace("--cfg=", "")
18 line = line.replace("\n", "")
21 # Now fill the crates list -- dependencies need to come first.
23 # Avoid O(n^2) iterations by keeping a map of indexes.
27 def append_crate(display_name, root_module, deps, cfg=[], is_workspace_member=True, is_proc_macro=False):
28 crates_indexes[display_name] = len(crates)
30 "display_name": display_name,
31 "root_module": str(root_module),
32 "is_workspace_member": is_workspace_member,
33 "is_proc_macro": is_proc_macro,
34 "deps": [{"crate": crates_indexes[dep], "name": dep} for dep in deps],
38 "RUST_MODFILE": "This is only for rust-analyzer"
42 # First, the ones in `rust/` since they are a bit special.
45 sysroot_src / "core" / "src" / "lib.rs",
47 is_workspace_member=False,
52 srctree / "rust" / "compiler_builtins.rs",
58 srctree / "rust" / "alloc" / "lib.rs",
59 ["core", "compiler_builtins"],
64 srctree / "rust" / "macros" / "lib.rs",
68 crates[-1]["proc_macro_dylib_path"] = "rust/libmacros.so"
72 srctree / "rust" / "build_error.rs",
73 ["core", "compiler_builtins"],
78 srctree / "rust"/ "bindings" / "lib.rs",
82 crates[-1]["env"]["OBJTREE"] = str(objtree.resolve(True))
86 srctree / "rust" / "kernel" / "lib.rs",
87 ["core", "alloc", "macros", "build_error", "bindings"],
90 crates[-1]["source"] = {
92 str(srctree / "rust" / "kernel"),
98 # Then, the rest outside of `rust/`.
100 # We explicitly mention the top-level folders we want to cover.
101 for folder in ("samples", "drivers"):
102 for path in (srctree / folder).rglob("*.rs"):
103 logging.info("Checking %s", path)
104 name = path.name.replace(".rs", "")
106 # Skip those that are not crate roots.
107 if f"{name}.o" not in open(path.parent / "Makefile").read():
110 logging.info("Adding %s", name)
114 ["core", "alloc", "kernel"],
121 parser = argparse.ArgumentParser()
122 parser.add_argument('--verbose', '-v', action='store_true')
123 parser.add_argument("srctree", type=pathlib.Path)
124 parser.add_argument("objtree", type=pathlib.Path)
125 parser.add_argument("sysroot_src", type=pathlib.Path)
126 args = parser.parse_args()
129 format="[%(asctime)s] [%(levelname)s] %(message)s",
130 level=logging.INFO if args.verbose else logging.WARNING
134 "crates": generate_crates(args.srctree, args.objtree, args.sysroot_src),
135 "sysroot_src": str(args.sysroot_src),
138 json.dump(rust_project, sys.stdout, sort_keys=True, indent=4)
140 if __name__ == "__main__":