fork/rules_rust

git clone https://git.julia.blue/fork/rules_rust

Sam McCallrust_analyzer: don't build a tree of RustAnalyzerInfos (#3028)443b089c

main
13.3 KiB347 linesraw
1# Copyright 2020 Google
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7#     http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14
15"""
16Rust Analyzer Bazel rules.
17
18rust_analyzer will generate a rust-project.json file for the
19given targets. This file can be consumed by rust-analyzer as an alternative
20to Cargo.toml files.
21"""
22
23load("//rust/platform:triple_mappings.bzl", "system_to_dylib_ext", "triple_to_system")
24load("//rust/private:common.bzl", "rust_common")
25load("//rust/private:providers.bzl", "RustAnalyzerGroupInfo", "RustAnalyzerInfo")
26load("//rust/private:rustc.bzl", "BuildInfo")
27load(
28    "//rust/private:utils.bzl",
29    "concat",
30    "dedent",
31    "dedup_expand_location",
32    "find_toolchain",
33)
34
35def write_rust_analyzer_spec_file(ctx, attrs, owner, base_info):
36    """Write a rust-analyzer spec info file.
37
38    Args:
39        ctx (ctx): The current rule's context object.
40        attrs (dict): A mapping of attributes.
41        owner (Label): The label of the owner of the spec info.
42        base_info (RustAnalyzerInfo): The data the resulting RustAnalyzerInfo is based on.
43
44    Returns:
45        RustAnalyzerInfo: Info with the embedded spec file.
46    """
47    crate_spec = ctx.actions.declare_file("{}.rust_analyzer_crate_spec.json".format(owner.name))
48
49    # Recreate the provider with the spec file embedded in it.
50    rust_analyzer_info = RustAnalyzerInfo(
51        aliases = base_info.aliases,
52        crate = base_info.crate,
53        cfgs = base_info.cfgs,
54        env = base_info.env,
55        deps = base_info.deps,
56        id = base_info.id,
57        crate_specs = depset(direct = [crate_spec], transitive = [base_info.crate_specs]),
58        proc_macro_dylib_path = base_info.proc_macro_dylib_path,
59        build_info = base_info.build_info,
60    )
61
62    ctx.actions.write(
63        output = crate_spec,
64        content = json.encode_indent(
65            _create_single_crate(
66                ctx,
67                attrs,
68                rust_analyzer_info,
69            ),
70            indent = " " * 4,
71        ),
72    )
73
74    return rust_analyzer_info
75
76def _rust_analyzer_aspect_impl(target, ctx):
77    if (rust_common.crate_info not in target and
78        rust_common.test_crate_info not in target and
79        rust_common.crate_group_info not in target):
80        return []
81
82    if RustAnalyzerInfo in target or RustAnalyzerGroupInfo in target:
83        return []
84
85    toolchain = find_toolchain(ctx)
86
87    # Always add `test` & `debug_assertions`. See rust-analyzer source code:
88    # https://github.com/rust-analyzer/rust-analyzer/blob/2021-11-15/crates/project_model/src/workspace.rs#L529-L531
89    cfgs = ["test", "debug_assertions"]
90    if hasattr(ctx.rule.attr, "crate_features"):
91        cfgs += ['feature="{}"'.format(f) for f in ctx.rule.attr.crate_features]
92    if hasattr(ctx.rule.attr, "rustc_flags"):
93        cfgs += [f[6:] for f in ctx.rule.attr.rustc_flags if f.startswith("--cfg ") or f.startswith("--cfg=")]
94
95    build_info = None
96    for dep in getattr(ctx.rule.attr, "deps", []):
97        # Save BuildInfo if we find any (for build script output)
98        if BuildInfo in dep:
99            build_info = dep[BuildInfo]
100
101    # Gather required info from dependencies.
102    label_to_id = {}  # {Label of dependency => crate_id}
103    crate_specs = []  # [depset of File - transitive crate_spec.json files]
104    attrs = ctx.rule.attr
105    all_deps = getattr(attrs, "deps", []) + getattr(attrs, "proc_macro_deps", []) + \
106               [dep for dep in [getattr(attrs, "crate", None), getattr(attrs, "actual", None)] if dep != None]
107    for dep in all_deps:
108        if RustAnalyzerInfo in dep:
109            label_to_id[dep.label] = dep[RustAnalyzerInfo].id
110            crate_specs.append(dep[RustAnalyzerInfo].crate_specs)
111        if RustAnalyzerGroupInfo in dep:
112            for expanded_dep in dep[RustAnalyzerGroupInfo].deps:
113                label_to_id[expanded_dep] = expanded_dep
114            crate_specs.append(dep[RustAnalyzerGroupInfo].crate_specs)
115
116    deps = label_to_id.values()
117    crate_specs = depset(transitive = crate_specs)
118
119    if rust_common.crate_group_info in target:
120        return [RustAnalyzerGroupInfo(deps = deps, crate_specs = crate_specs)]
121    elif rust_common.crate_info in target:
122        crate_info = target[rust_common.crate_info]
123    elif rust_common.test_crate_info in target:
124        crate_info = target[rust_common.test_crate_info].crate
125    else:
126        fail("Unexpected target type: {}".format(target))
127
128    aliases = {
129        label_to_id[target.label]: name
130        for (target, name) in getattr(attrs, "aliases", {}).items()
131        if target.label in label_to_id
132    }
133
134    # An arbitrary unique and stable identifier.
135    crate_id = "ID-" + crate_info.root.path
136
137    rust_analyzer_info = write_rust_analyzer_spec_file(ctx, ctx.rule.attr, ctx.label, RustAnalyzerInfo(
138        id = crate_id,
139        aliases = aliases,
140        crate = crate_info,
141        cfgs = cfgs,
142        env = crate_info.rustc_env,
143        deps = deps,
144        crate_specs = crate_specs,
145        proc_macro_dylib_path = find_proc_macro_dylib_path(toolchain, target),
146        build_info = build_info,
147    ))
148
149    return [
150        rust_analyzer_info,
151        OutputGroupInfo(rust_analyzer_crate_spec = rust_analyzer_info.crate_specs),
152    ]
153
154def find_proc_macro_dylib_path(toolchain, target):
155    """Find the proc_macro_dylib_path of target. Returns None if target crate is not type proc-macro.
156
157    Args:
158        toolchain: The current rust toolchain.
159        target: The current target.
160    Returns:
161        (path): The path to the proc macro dylib, or None if this crate is not a proc-macro.
162    """
163    if rust_common.crate_info in target:
164        crate_info = target[rust_common.crate_info]
165    elif rust_common.test_crate_info in target:
166        crate_info = target[rust_common.test_crate_info].crate
167    else:
168        return None
169
170    if crate_info.type != "proc-macro":
171        return None
172
173    dylib_ext = system_to_dylib_ext(triple_to_system(toolchain.target_triple))
174    for action in target.actions:
175        for output in action.outputs.to_list():
176            if output.extension == dylib_ext[1:]:
177                return output.path
178
179    # Failed to find the dylib path inside a proc-macro crate.
180    # TODO: Should this be an error?
181    return None
182
183rust_analyzer_aspect = aspect(
184    attr_aspects = ["deps", "proc_macro_deps", "crate", "actual", "proto"],
185    implementation = _rust_analyzer_aspect_impl,
186    toolchains = [str(Label("//rust:toolchain_type"))],
187    doc = "Annotates rust rules with RustAnalyzerInfo later used to build a rust-project.json",
188)
189
190# Paths in the generated JSON file begin with one of these placeholders.
191# The gen_rust_project driver will replace them with absolute paths.
192_WORKSPACE_TEMPLATE = "__WORKSPACE__/"
193_EXEC_ROOT_TEMPLATE = "__EXEC_ROOT__/"
194_OUTPUT_BASE_TEMPLATE = "__OUTPUT_BASE__/"
195
196def _create_single_crate(ctx, attrs, info):
197    """Creates a crate in the rust-project.json format.
198
199    Args:
200        ctx (ctx): The rule context.
201        attrs (dict): A mapping of attributes.
202        info (RustAnalyzerInfo): RustAnalyzerInfo for the current crate.
203
204    Returns:
205        (dict) The crate rust-project.json representation
206    """
207    crate_name = info.crate.name
208    crate = dict()
209    crate["crate_id"] = info.id
210    crate["display_name"] = crate_name
211    crate["edition"] = info.crate.edition
212    crate["env"] = {}
213    crate["crate_type"] = info.crate.type
214
215    # Switch on external/ to determine if crates are in the workspace or remote.
216    # TODO: Some folks may want to override this for vendored dependencies.
217    is_external = info.crate.root.path.startswith("external/")
218    is_generated = not info.crate.root.is_source
219    path_prefix = _EXEC_ROOT_TEMPLATE if is_external or is_generated else _WORKSPACE_TEMPLATE
220    crate["is_workspace_member"] = not is_external
221    crate["root_module"] = path_prefix + info.crate.root.path
222    crate["source"] = {"exclude_dirs": [], "include_dirs": []}
223
224    if is_generated:
225        srcs = getattr(ctx.rule.files, "srcs", [])
226        src_map = {src.short_path: src for src in srcs if src.is_source}
227        if info.crate.root.short_path in src_map:
228            crate["root_module"] = _WORKSPACE_TEMPLATE + src_map[info.crate.root.short_path].path
229            crate["source"]["include_dirs"].append(path_prefix + info.crate.root.dirname)
230
231    if info.build_info != None and info.build_info.out_dir != None:
232        out_dir_path = info.build_info.out_dir.path
233        crate["env"].update({"OUT_DIR": _EXEC_ROOT_TEMPLATE + out_dir_path})
234
235        # We have to tell rust-analyzer about our out_dir since it's not under the crate root.
236        crate["source"]["include_dirs"].extend([
237            path_prefix + info.crate.root.dirname,
238            _EXEC_ROOT_TEMPLATE + out_dir_path,
239        ])
240
241    # TODO: The only imagined use case is an env var holding a filename in the workspace passed to a
242    # macro like include_bytes!. Other use cases might exist that require more complex logic.
243    expand_targets = concat([getattr(attrs, attr, []) for attr in ["data", "compile_data"]])
244
245    crate["env"].update({k: dedup_expand_location(ctx, v, expand_targets) for k, v in info.env.items()})
246
247    # Omit when a crate appears to depend on itself (e.g. foo_test crates).
248    # It can happen a single source file is present in multiple crates - there can
249    # be a `rust_library` with a `lib.rs` file, and a `rust_test` for the `test`
250    # module in that file. Tests can declare more dependencies than what library
251    # had. Therefore we had to collect all RustAnalyzerInfos for a given crate
252    # and take deps from all of them.
253
254    # There's one exception - if the dependency is the same crate name as the
255    # the crate being processed, we don't add it as a dependency to itself. This is
256    # common and expected - `rust_test.crate` pointing to the `rust_library`.
257    crate["deps"] = [id for id in info.deps if id != info.id]
258    crate["aliases"] = info.aliases
259    crate["cfg"] = info.cfgs
260    toolchain = find_toolchain(ctx)
261    crate["target"] = (_EXEC_ROOT_TEMPLATE + toolchain.target_json.path) if toolchain.target_json else toolchain.target_flag_value
262    if info.proc_macro_dylib_path != None:
263        crate["proc_macro_dylib_path"] = _EXEC_ROOT_TEMPLATE + info.proc_macro_dylib_path
264    return crate
265
266def _rust_analyzer_toolchain_impl(ctx):
267    toolchain = platform_common.ToolchainInfo(
268        proc_macro_srv = ctx.executable.proc_macro_srv,
269        rustc = ctx.executable.rustc,
270        rustc_srcs = ctx.attr.rustc_srcs,
271    )
272
273    return [toolchain]
274
275rust_analyzer_toolchain = rule(
276    implementation = _rust_analyzer_toolchain_impl,
277    doc = "A toolchain for [rust-analyzer](https://rust-analyzer.github.io/).",
278    attrs = {
279        "proc_macro_srv": attr.label(
280            doc = "The path to a `rust_analyzer_proc_macro_srv` binary.",
281            cfg = "exec",
282            executable = True,
283            allow_single_file = True,
284        ),
285        "rustc": attr.label(
286            doc = "The path to a `rustc` binary.",
287            cfg = "exec",
288            executable = True,
289            allow_single_file = True,
290            mandatory = True,
291        ),
292        "rustc_srcs": attr.label(
293            doc = "The source code of rustc.",
294            mandatory = True,
295        ),
296    },
297)
298
299def _rust_analyzer_detect_sysroot_impl(ctx):
300    rust_analyzer_toolchain = ctx.toolchains[Label("@rules_rust//rust/rust_analyzer:toolchain_type")]
301
302    if not rust_analyzer_toolchain.rustc_srcs:
303        fail(
304            "Current Rust-Analyzer toolchain doesn't contain rustc sources in `rustc_srcs` attribute.",
305            "These are needed by rust-analyzer. If you are using the default Rust toolchain, add `rust_repositories(include_rustc_srcs = True, ...).` to your WORKSPACE file.",
306        )
307
308    rustc_srcs = rust_analyzer_toolchain.rustc_srcs
309
310    sysroot_src = rustc_srcs.label.package + "/library"
311    if rustc_srcs.label.workspace_root:
312        sysroot_src = _OUTPUT_BASE_TEMPLATE + rustc_srcs.label.workspace_root + "/" + sysroot_src
313    else:
314        sysroot_src = _WORKSPACE_TEMPLATE + sysroot_src
315
316    rustc = rust_analyzer_toolchain.rustc
317    sysroot_dir, _, bin_dir = rustc.dirname.rpartition("/")
318    if bin_dir != "bin":
319        fail("The rustc path is expected to be relative to the sysroot as `bin/rustc`. Instead got: {}".format(
320            rustc.path,
321        ))
322
323    sysroot = _OUTPUT_BASE_TEMPLATE + sysroot_dir
324
325    toolchain_info = {
326        "sysroot": sysroot,
327        "sysroot_src": sysroot_src,
328    }
329
330    output = ctx.actions.declare_file(ctx.label.name + ".rust_analyzer_toolchain.json")
331    ctx.actions.write(
332        output = output,
333        content = json.encode_indent(toolchain_info, indent = " " * 4),
334    )
335
336    return [DefaultInfo(files = depset([output]))]
337
338rust_analyzer_detect_sysroot = rule(
339    implementation = _rust_analyzer_detect_sysroot_impl,
340    toolchains = [
341        "@rules_rust//rust:toolchain_type",
342        "@rules_rust//rust/rust_analyzer:toolchain_type",
343    ],
344    doc = dedent("""\
345        Detect the sysroot and store in a file for use by the gen_rust_project tool.
346    """),
347)