2 # SPDX-License-Identifier: GPL-2.0
3 """generate_rust_analyzer - Generates the `rust-project.json` file for `rust-analyzer`.
13 def generate_crates(srctree, objtree, sysroot_src, external_src):
14 # Generate the configuration list.
16 with open(objtree / "include" / "generated" / "rustc_cfg") as fd:
18 line = line.replace("--cfg=", "")
19 line = line.replace("\n", "")
22 # Now fill the crates list -- dependencies need to come first.
24 # Avoid O(n^2) iterations by keeping a map of indexes.
28 def append_crate(display_name, root_module, deps, cfg=[], is_workspace_member=True, is_proc_macro=False):
29 crates_indexes[display_name] = len(crates)
31 "display_name": display_name,
32 "root_module": str(root_module),
33 "is_workspace_member": is_workspace_member,
34 "is_proc_macro": is_proc_macro,
35 "deps": [{"crate": crates_indexes[dep], "name": dep} for dep in deps],
39 "RUST_MODFILE": "This is only for rust-analyzer"
43 # First, the ones in `rust/` since they are a bit special.
46 sysroot_src / "core" / "src" / "lib.rs",
48 is_workspace_member=False,
53 srctree / "rust" / "compiler_builtins.rs",
59 srctree / "rust" / "alloc" / "lib.rs",
60 ["core", "compiler_builtins"],
65 srctree / "rust" / "macros" / "lib.rs",
69 crates[-1]["proc_macro_dylib_path"] = f"{objtree}/rust/libmacros.so"
73 srctree / "rust" / "build_error.rs",
74 ["core", "compiler_builtins"],
79 srctree / "rust"/ "bindings" / "lib.rs",
83 crates[-1]["env"]["OBJTREE"] = str(objtree.resolve(True))
87 srctree / "rust" / "kernel" / "lib.rs",
88 ["core", "alloc", "macros", "build_error", "bindings"],
91 crates[-1]["source"] = {
93 str(srctree / "rust" / "kernel"),
99 def is_root_crate(build_file, target):
101 return f"{target}.o" in open(build_file).read()
102 except FileNotFoundError:
105 # Then, the rest outside of `rust/`.
107 # We explicitly mention the top-level folders we want to cover.
108 extra_dirs = map(lambda dir: srctree / dir, ("samples", "drivers"))
109 if external_src is not None:
110 extra_dirs = [external_src]
111 for folder in extra_dirs:
112 for path in folder.rglob("*.rs"):
113 logging.info("Checking %s", path)
114 name = path.name.replace(".rs", "")
116 # Skip those that are not crate roots.
117 if not is_root_crate(path.parent / "Makefile", name) and \
118 not is_root_crate(path.parent / "Kbuild", name):
121 logging.info("Adding %s", name)
125 ["core", "alloc", "kernel"],
132 parser = argparse.ArgumentParser()
133 parser.add_argument('--verbose', '-v', action='store_true')
134 parser.add_argument("srctree", type=pathlib.Path)
135 parser.add_argument("objtree", type=pathlib.Path)
136 parser.add_argument("sysroot_src", type=pathlib.Path)
137 parser.add_argument("exttree", type=pathlib.Path, nargs="?")
138 args = parser.parse_args()
141 format="[%(asctime)s] [%(levelname)s] %(message)s",
142 level=logging.INFO if args.verbose else logging.WARNING
146 "crates": generate_crates(args.srctree, args.objtree, args.sysroot_src, args.exttree),
147 "sysroot_src": str(args.sysroot_src),
150 json.dump(rust_project, sys.stdout, sort_keys=True, indent=4)
152 if __name__ == "__main__":