~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/scripts/generate_rust_analyzer.py

Version: ~ [ linux-6.12-rc7 ] ~ [ linux-6.11.7 ] ~ [ linux-6.10.14 ] ~ [ linux-6.9.12 ] ~ [ linux-6.8.12 ] ~ [ linux-6.7.12 ] ~ [ linux-6.6.60 ] ~ [ linux-6.5.13 ] ~ [ linux-6.4.16 ] ~ [ linux-6.3.13 ] ~ [ linux-6.2.16 ] ~ [ linux-6.1.116 ] ~ [ linux-6.0.19 ] ~ [ linux-5.19.17 ] ~ [ linux-5.18.19 ] ~ [ linux-5.17.15 ] ~ [ linux-5.16.20 ] ~ [ linux-5.15.171 ] ~ [ linux-5.14.21 ] ~ [ linux-5.13.19 ] ~ [ linux-5.12.19 ] ~ [ linux-5.11.22 ] ~ [ linux-5.10.229 ] ~ [ linux-5.9.16 ] ~ [ linux-5.8.18 ] ~ [ linux-5.7.19 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.285 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.323 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.336 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.337 ] ~ [ linux-4.4.302 ] ~ [ linux-3.10.108 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.12 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

Diff markup

Differences between /scripts/generate_rust_analyzer.py (Architecture i386) and /scripts/generate_rust_analyzer.py (Architecture sparc)


  1 #!/usr/bin/env python3                              1 #!/usr/bin/env python3
  2 # SPDX-License-Identifier: GPL-2.0                  2 # SPDX-License-Identifier: GPL-2.0
  3 """generate_rust_analyzer - Generates the `rus      3 """generate_rust_analyzer - Generates the `rust-project.json` file for `rust-analyzer`.
  4 """                                                 4 """
  5                                                     5 
  6 import argparse                                     6 import argparse
  7 import json                                         7 import json
  8 import logging                                      8 import logging
  9 import os                                           9 import os
 10 import pathlib                                     10 import pathlib
 11 import sys                                         11 import sys
 12                                                    12 
 13 def args_crates_cfgs(cfgs):                        13 def args_crates_cfgs(cfgs):
 14     crates_cfgs = {}                               14     crates_cfgs = {}
 15     for cfg in cfgs:                               15     for cfg in cfgs:
 16         crate, vals = cfg.split("=", 1)            16         crate, vals = cfg.split("=", 1)
 17         crates_cfgs[crate] = vals.replace("--c     17         crates_cfgs[crate] = vals.replace("--cfg", "").split()
 18                                                    18 
 19     return crates_cfgs                             19     return crates_cfgs
 20                                                    20 
 21 def generate_crates(srctree, objtree, sysroot_     21 def generate_crates(srctree, objtree, sysroot_src, external_src, cfgs):
 22     # Generate the configuration list.             22     # Generate the configuration list.
 23     cfg = []                                       23     cfg = []
 24     with open(objtree / "include" / "generated     24     with open(objtree / "include" / "generated" / "rustc_cfg") as fd:
 25         for line in fd:                            25         for line in fd:
 26             line = line.replace("--cfg=", "")      26             line = line.replace("--cfg=", "")
 27             line = line.replace("\n", "")          27             line = line.replace("\n", "")
 28             cfg.append(line)                       28             cfg.append(line)
 29                                                    29 
 30     # Now fill the crates list -- dependencies     30     # Now fill the crates list -- dependencies need to come first.
 31     #                                              31     #
 32     # Avoid O(n^2) iterations by keeping a map     32     # Avoid O(n^2) iterations by keeping a map of indexes.
 33     crates = []                                    33     crates = []
 34     crates_indexes = {}                            34     crates_indexes = {}
 35     crates_cfgs = args_crates_cfgs(cfgs)           35     crates_cfgs = args_crates_cfgs(cfgs)
 36                                                    36 
 37     def append_crate(display_name, root_module     37     def append_crate(display_name, root_module, deps, cfg=[], is_workspace_member=True, is_proc_macro=False):
 38         crates_indexes[display_name] = len(cra     38         crates_indexes[display_name] = len(crates)
 39         crates.append({                            39         crates.append({
 40             "display_name": display_name,          40             "display_name": display_name,
 41             "root_module": str(root_module),       41             "root_module": str(root_module),
 42             "is_workspace_member": is_workspac     42             "is_workspace_member": is_workspace_member,
 43             "is_proc_macro": is_proc_macro,        43             "is_proc_macro": is_proc_macro,
 44             "deps": [{"crate": crates_indexes[     44             "deps": [{"crate": crates_indexes[dep], "name": dep} for dep in deps],
 45             "cfg": cfg,                            45             "cfg": cfg,
 46             "edition": "2021",                     46             "edition": "2021",
 47             "env": {                               47             "env": {
 48                 "RUST_MODFILE": "This is only      48                 "RUST_MODFILE": "This is only for rust-analyzer"
 49             }                                      49             }
 50         })                                         50         })
 51                                                    51 
 52     # First, the ones in `rust/` since they ar     52     # First, the ones in `rust/` since they are a bit special.
 53     append_crate(                                  53     append_crate(
 54         "core",                                    54         "core",
 55         sysroot_src / "core" / "src" / "lib.rs     55         sysroot_src / "core" / "src" / "lib.rs",
 56         [],                                        56         [],
 57         cfg=crates_cfgs.get("core", []),           57         cfg=crates_cfgs.get("core", []),
 58         is_workspace_member=False,                 58         is_workspace_member=False,
 59     )                                              59     )
 60                                                    60 
 61     append_crate(                                  61     append_crate(
 62         "compiler_builtins",                       62         "compiler_builtins",
 63         srctree / "rust" / "compiler_builtins.     63         srctree / "rust" / "compiler_builtins.rs",
 64         [],                                        64         [],
 65     )                                              65     )
 66                                                    66 
 67     append_crate(                                  67     append_crate(
 68         "alloc",                                   68         "alloc",
 69         sysroot_src / "alloc" / "src" / "lib.r     69         sysroot_src / "alloc" / "src" / "lib.rs",
 70         ["core", "compiler_builtins"],             70         ["core", "compiler_builtins"],
 71         cfg=crates_cfgs.get("alloc", []),          71         cfg=crates_cfgs.get("alloc", []),
 72     )                                              72     )
 73                                                    73 
 74     append_crate(                                  74     append_crate(
 75         "macros",                                  75         "macros",
 76         srctree / "rust" / "macros" / "lib.rs"     76         srctree / "rust" / "macros" / "lib.rs",
 77         [],                                        77         [],
 78         is_proc_macro=True,                        78         is_proc_macro=True,
 79     )                                              79     )
 80     crates[-1]["proc_macro_dylib_path"] = f"{o     80     crates[-1]["proc_macro_dylib_path"] = f"{objtree}/rust/libmacros.so"
 81                                                    81 
 82     append_crate(                                  82     append_crate(
 83         "build_error",                             83         "build_error",
 84         srctree / "rust" / "build_error.rs",       84         srctree / "rust" / "build_error.rs",
 85         ["core", "compiler_builtins"],             85         ["core", "compiler_builtins"],
 86     )                                              86     )
 87                                                    87 
 88     append_crate(                                  88     append_crate(
 89         "bindings",                                89         "bindings",
 90         srctree / "rust"/ "bindings" / "lib.rs     90         srctree / "rust"/ "bindings" / "lib.rs",
 91         ["core"],                                  91         ["core"],
 92         cfg=cfg,                                   92         cfg=cfg,
 93     )                                              93     )
 94     crates[-1]["env"]["OBJTREE"] = str(objtree     94     crates[-1]["env"]["OBJTREE"] = str(objtree.resolve(True))
 95                                                    95 
 96     append_crate(                                  96     append_crate(
 97         "kernel",                                  97         "kernel",
 98         srctree / "rust" / "kernel" / "lib.rs"     98         srctree / "rust" / "kernel" / "lib.rs",
 99         ["core", "alloc", "macros", "build_err     99         ["core", "alloc", "macros", "build_error", "bindings"],
100         cfg=cfg,                                  100         cfg=cfg,
101     )                                             101     )
102     crates[-1]["source"] = {                      102     crates[-1]["source"] = {
103         "include_dirs": [                         103         "include_dirs": [
104             str(srctree / "rust" / "kernel"),     104             str(srctree / "rust" / "kernel"),
105             str(objtree / "rust")                 105             str(objtree / "rust")
106         ],                                        106         ],
107         "exclude_dirs": [],                       107         "exclude_dirs": [],
108     }                                             108     }
109                                                   109 
110     def is_root_crate(build_file, target):        110     def is_root_crate(build_file, target):
111         try:                                      111         try:
112             return f"{target}.o" in open(build    112             return f"{target}.o" in open(build_file).read()
113         except FileNotFoundError:                 113         except FileNotFoundError:
114             return False                          114             return False
115                                                   115 
116     # Then, the rest outside of `rust/`.          116     # Then, the rest outside of `rust/`.
117     #                                             117     #
118     # We explicitly mention the top-level fold    118     # We explicitly mention the top-level folders we want to cover.
119     extra_dirs = map(lambda dir: srctree / dir    119     extra_dirs = map(lambda dir: srctree / dir, ("samples", "drivers"))
120     if external_src is not None:                  120     if external_src is not None:
121         extra_dirs = [external_src]               121         extra_dirs = [external_src]
122     for folder in extra_dirs:                     122     for folder in extra_dirs:
123         for path in folder.rglob("*.rs"):         123         for path in folder.rglob("*.rs"):
124             logging.info("Checking %s", path)     124             logging.info("Checking %s", path)
125             name = path.name.replace(".rs", ""    125             name = path.name.replace(".rs", "")
126                                                   126 
127             # Skip those that are not crate ro    127             # Skip those that are not crate roots.
128             if not is_root_crate(path.parent /    128             if not is_root_crate(path.parent / "Makefile", name) and \
129                not is_root_crate(path.parent /    129                not is_root_crate(path.parent / "Kbuild", name):
130                 continue                          130                 continue
131                                                   131 
132             logging.info("Adding %s", name)       132             logging.info("Adding %s", name)
133             append_crate(                         133             append_crate(
134                 name,                             134                 name,
135                 path,                             135                 path,
136                 ["core", "alloc", "kernel"],      136                 ["core", "alloc", "kernel"],
137                 cfg=cfg,                          137                 cfg=cfg,
138             )                                     138             )
139                                                   139 
140     return crates                                 140     return crates
141                                                   141 
142 def main():                                       142 def main():
143     parser = argparse.ArgumentParser()            143     parser = argparse.ArgumentParser()
144     parser.add_argument('--verbose', '-v', act    144     parser.add_argument('--verbose', '-v', action='store_true')
145     parser.add_argument('--cfgs', action='appe    145     parser.add_argument('--cfgs', action='append', default=[])
146     parser.add_argument("srctree", type=pathli    146     parser.add_argument("srctree", type=pathlib.Path)
147     parser.add_argument("objtree", type=pathli    147     parser.add_argument("objtree", type=pathlib.Path)
148     parser.add_argument("sysroot", type=pathli    148     parser.add_argument("sysroot", type=pathlib.Path)
149     parser.add_argument("sysroot_src", type=pa    149     parser.add_argument("sysroot_src", type=pathlib.Path)
150     parser.add_argument("exttree", type=pathli    150     parser.add_argument("exttree", type=pathlib.Path, nargs="?")
151     args = parser.parse_args()                    151     args = parser.parse_args()
152                                                   152 
153     logging.basicConfig(                          153     logging.basicConfig(
154         format="[%(asctime)s] [%(levelname)s]     154         format="[%(asctime)s] [%(levelname)s] %(message)s",
155         level=logging.INFO if args.verbose els    155         level=logging.INFO if args.verbose else logging.WARNING
156     )                                             156     )
157                                                   157 
158     # Making sure that the `sysroot` and `sysr    158     # Making sure that the `sysroot` and `sysroot_src` belong to the same toolchain.
159     assert args.sysroot in args.sysroot_src.pa    159     assert args.sysroot in args.sysroot_src.parents
160                                                   160 
161     rust_project = {                              161     rust_project = {
162         "crates": generate_crates(args.srctree    162         "crates": generate_crates(args.srctree, args.objtree, args.sysroot_src, args.exttree, args.cfgs),
163         "sysroot": str(args.sysroot),             163         "sysroot": str(args.sysroot),
164     }                                             164     }
165                                                   165 
166     json.dump(rust_project, sys.stdout, sort_k    166     json.dump(rust_project, sys.stdout, sort_keys=True, indent=4)
167                                                   167 
168 if __name__ == "__main__":                        168 if __name__ == "__main__":
169     main()                                        169     main()
                                                      

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

sflogo.php