2 # SPDX-License-Identifier: GPL-2.0
3 """generate_rust_analyzer - Generates the `rust-project.json` file for `rust-analyzer`.
13 def args_crates_cfgs(cfgs):
16 crate, vals = cfg.split("=", 1)
17 crates_cfgs[crate] = vals.replace("--cfg", "").split()
21 def generate_crates(srctree, objtree, sysroot_src, external_src, cfgs):
22 # Generate the configuration list.
24 with open(objtree / "include" / "generated" / "rustc_cfg") as fd:
26 line = line.replace("--cfg=", "")
27 line = line.replace("\n", "")
30 # Now fill the crates list -- dependencies need to come first.
32 # Avoid O(n^2) iterations by keeping a map of indexes.
35 crates_cfgs = args_crates_cfgs(cfgs)
37 def append_crate(display_name, root_module, deps, cfg=[], is_workspace_member=True, is_proc_macro=False):
38 crates_indexes[display_name] = len(crates)
40 "display_name": display_name,
41 "root_module": str(root_module),
42 "is_workspace_member": is_workspace_member,
43 "is_proc_macro": is_proc_macro,
44 "deps": [{"crate": crates_indexes[dep], "name": dep} for dep in deps],
48 "RUST_MODFILE": "This is only for rust-analyzer"
52 # First, the ones in `rust/` since they are a bit special.
55 sysroot_src / "core" / "src" / "lib.rs",
57 cfg=crates_cfgs.get("core", []),
58 is_workspace_member=False,
63 srctree / "rust" / "compiler_builtins.rs",
69 srctree / "rust" / "macros" / "lib.rs",
73 crates[-1]["proc_macro_dylib_path"] = f"{objtree}/rust/libmacros.so"
77 srctree / "rust" / "build_error.rs",
78 ["core", "compiler_builtins"],
83 srctree / "rust"/ "bindings" / "lib.rs",
87 crates[-1]["env"]["OBJTREE"] = str(objtree.resolve(True))
91 srctree / "rust" / "kernel" / "lib.rs",
92 ["core", "macros", "build_error", "bindings"],
95 crates[-1]["source"] = {
97 str(srctree / "rust" / "kernel"),
103 def is_root_crate(build_file, target):
105 return f"{target}.o" in open(build_file).read()
106 except FileNotFoundError:
109 # Then, the rest outside of `rust/`.
111 # We explicitly mention the top-level folders we want to cover.
112 extra_dirs = map(lambda dir: srctree / dir, ("samples", "drivers"))
113 if external_src is not None:
114 extra_dirs = [external_src]
115 for folder in extra_dirs:
116 for path in folder.rglob("*.rs"):
117 logging.info("Checking %s", path)
118 name = path.name.replace(".rs", "")
120 # Skip those that are not crate roots.
121 if not is_root_crate(path.parent / "Makefile", name) and \
122 not is_root_crate(path.parent / "Kbuild", name):
125 logging.info("Adding %s", name)
136 parser = argparse.ArgumentParser()
137 parser.add_argument('--verbose', '-v', action='store_true')
138 parser.add_argument('--cfgs', action='append', default=[])
139 parser.add_argument("srctree", type=pathlib.Path)
140 parser.add_argument("objtree", type=pathlib.Path)
141 parser.add_argument("sysroot", type=pathlib.Path)
142 parser.add_argument("sysroot_src", type=pathlib.Path)
143 parser.add_argument("exttree", type=pathlib.Path, nargs="?")
144 args = parser.parse_args()
147 format="[%(asctime)s] [%(levelname)s] %(message)s",
148 level=logging.INFO if args.verbose else logging.WARNING
151 # Making sure that the `sysroot` and `sysroot_src` belong to the same toolchain.
152 assert args.sysroot in args.sysroot_src.parents
155 "crates": generate_crates(args.srctree, args.objtree, args.sysroot_src, args.exttree, args.cfgs),
156 "sysroot": str(args.sysroot),
159 json.dump(rust_project, sys.stdout, sort_keys=True, indent=4)
161 if __name__ == "__main__":