2 # SPDX-License-Identifier: GPL-2.0
3 """generate_rust_analyzer - Generates the `rust-project.json` file for `rust-analyzer`.
14 def args_crates_cfgs(cfgs
):
17 crate
, vals
= cfg
.split("=", 1)
18 crates_cfgs
[crate
] = vals
.replace("--cfg", "").split()
22 def generate_crates(srctree
, objtree
, sysroot_src
, external_src
, cfgs
):
23 # Generate the configuration list.
25 with
open(objtree
/ "include" / "generated" / "rustc_cfg") as fd
:
27 line
= line
.replace("--cfg=", "")
28 line
= line
.replace("\n", "")
31 # Now fill the crates list -- dependencies need to come first.
33 # Avoid O(n^2) iterations by keeping a map of indexes.
36 crates_cfgs
= args_crates_cfgs(cfgs
)
38 def append_crate(display_name
, root_module
, deps
, cfg
=[], is_workspace_member
=True, is_proc_macro
=False):
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 proc_macro_dylib_name
= subprocess
.check_output(
53 [os
.environ
["RUSTC"], "--print", "file-names", "--crate-name", display_name
, "--crate-type", "proc-macro", "-"],
54 stdin
=subprocess
.DEVNULL
,
55 ).decode('utf-8').strip()
56 crate
["proc_macro_dylib_path"] = f
"{objtree}/rust/{proc_macro_dylib_name}"
57 crates_indexes
[display_name
] = len(crates
)
60 # First, the ones in `rust/` since they are a bit special.
63 sysroot_src
/ "core" / "src" / "lib.rs",
65 cfg
=crates_cfgs
.get("core", []),
66 is_workspace_member
=False,
71 srctree
/ "rust" / "compiler_builtins.rs",
77 srctree
/ "rust" / "macros" / "lib.rs",
84 srctree
/ "rust" / "build_error.rs",
85 ["core", "compiler_builtins"],
90 srctree
/ "rust"/ "bindings" / "lib.rs",
94 crates
[-1]["env"]["OBJTREE"] = str(objtree
.resolve(True))
98 srctree
/ "rust" / "kernel" / "lib.rs",
99 ["core", "macros", "build_error", "bindings"],
102 crates
[-1]["source"] = {
104 str(srctree
/ "rust" / "kernel"),
105 str(objtree
/ "rust")
110 def is_root_crate(build_file
, target
):
112 return f
"{target}.o" in open(build_file
).read()
113 except FileNotFoundError
:
116 # Then, the rest outside of `rust/`.
118 # We explicitly mention the top-level folders we want to cover.
119 extra_dirs
= map(lambda dir: srctree
/ dir, ("samples", "drivers"))
120 if external_src
is not None:
121 extra_dirs
= [external_src
]
122 for folder
in extra_dirs
:
123 for path
in folder
.rglob("*.rs"):
124 logging
.info("Checking %s", path
)
125 name
= path
.name
.replace(".rs", "")
127 # Skip those that are not crate roots.
128 if not is_root_crate(path
.parent
/ "Makefile", name
) and \
129 not is_root_crate(path
.parent
/ "Kbuild", name
):
132 logging
.info("Adding %s", name
)
143 parser
= argparse
.ArgumentParser()
144 parser
.add_argument('--verbose', '-v', action
='store_true')
145 parser
.add_argument('--cfgs', action
='append', default
=[])
146 parser
.add_argument("srctree", type=pathlib
.Path
)
147 parser
.add_argument("objtree", type=pathlib
.Path
)
148 parser
.add_argument("sysroot", type=pathlib
.Path
)
149 parser
.add_argument("sysroot_src", type=pathlib
.Path
)
150 parser
.add_argument("exttree", type=pathlib
.Path
, nargs
="?")
151 args
= parser
.parse_args()
154 format
="[%(asctime)s] [%(levelname)s] %(message)s",
155 level
=logging
.INFO
if args
.verbose
else logging
.WARNING
158 # Making sure that the `sysroot` and `sysroot_src` belong to the same toolchain.
159 assert args
.sysroot
in args
.sysroot_src
.parents
162 "crates": generate_crates(args
.srctree
, args
.objtree
, args
.sysroot_src
, args
.exttree
, args
.cfgs
),
163 "sysroot": str(args
.sysroot
),
166 json
.dump(rust_project
, sys
.stdout
, sort_keys
=True, indent
=4)
168 if __name__
== "__main__":