fork/rules_rust

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

UebelAndreMoved rust extension rules into a separate `rules_rust_ext` workspace. (#3007)2bad96d9

main
18.0 KiB487 linesraw
1# Copyright 2019 The Bazel Authors. All rights reserved.
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"""Rust Bindgen rules"""
16
17load(
18    "@bazel_tools//tools/build_defs/cc:action_names.bzl",
19    "CPP_COMPILE_ACTION_NAME",
20)
21load("@rules_cc//cc:defs.bzl", "CcInfo", "cc_library")
22load("@rules_rust//rust:defs.bzl", "rust_library")
23load("@rules_rust//rust:rust_common.bzl", "BuildInfo")
24
25# buildifier: disable=bzl-visibility
26load("@rules_rust//rust/private:rustc.bzl", "get_linker_and_args")
27
28# buildifier: disable=bzl-visibility
29load("@rules_rust//rust/private:utils.bzl", "find_cc_toolchain", "get_lib_name_default", "get_preferred_artifact")
30
31# TODO(hlopko): use the more robust logic from rustc.bzl also here, through a reasonable API.
32def _get_libs_for_static_executable(dep):
33    """find the libraries used for linking a static executable.
34
35    Args:
36        dep (Target): A cc_library target.
37
38    Returns:
39        depset: A depset[File]
40    """
41    linker_inputs = dep[CcInfo].linking_context.linker_inputs.to_list()
42    return depset([get_preferred_artifact(lib, use_pic = False) for li in linker_inputs for lib in li.libraries])
43
44def rust_bindgen_library(
45        name,
46        header,
47        cc_lib,
48        bindgen_flags = None,
49        bindgen_features = None,
50        clang_flags = None,
51        wrap_static_fns = False,
52        **kwargs):
53    """Generates a rust source file for `header`, and builds a rust_library.
54
55    Arguments are the same as `rust_bindgen`, and `kwargs` are passed directly to rust_library.
56
57    Args:
58        name (str): A unique name for this target.
59        header (str): The label of the .h file to generate bindings for.
60        cc_lib (str): The label of the cc_library that contains the .h file. This is used to find the transitive includes.
61        bindgen_flags (list, optional): Flags to pass directly to the bindgen executable. See https://rust-lang.github.io/rust-bindgen/ for details.
62        bindgen_features (list, optional): The `features` attribute for the `rust_bindgen` target.
63        clang_flags (list, optional): Flags to pass directly to the clang executable.
64        wrap_static_fns (bool): Whether to create a separate .c file for static fns. Requires nightly toolchain, and a header that actually needs this feature (otherwise bindgen won't generate the file and Bazel complains",
65        **kwargs: Arguments to forward to the underlying `rust_library` rule.
66    """
67
68    tags = kwargs.get("tags") or []
69    if "tags" in kwargs:
70        kwargs.pop("tags")
71
72    sub_tags = tags + ([] if "manual" in tags else ["manual"])
73
74    bindgen_kwargs = {}
75    for shared in (
76        "target_compatible_with",
77        "exec_compatible_with",
78    ):
79        if shared in kwargs:
80            bindgen_kwargs.update({shared: kwargs[shared]})
81    if "merge_cc_lib_objects_into_rlib" in kwargs:
82        bindgen_kwargs.update({"merge_cc_lib_objects_into_rlib": kwargs["merge_cc_lib_objects_into_rlib"]})
83        kwargs.pop("merge_cc_lib_objects_into_rlib")
84
85    rust_bindgen(
86        name = name + "__bindgen",
87        header = header,
88        cc_lib = cc_lib,
89        bindgen_flags = bindgen_flags or [],
90        features = bindgen_features,
91        clang_flags = clang_flags or [],
92        tags = sub_tags,
93        wrap_static_fns = wrap_static_fns,
94        **bindgen_kwargs
95    )
96
97    tags = depset(tags + ["__bindgen", "no-clippy", "no-rustfmt"]).to_list()
98
99    deps = kwargs.get("deps") or []
100    if "deps" in kwargs:
101        kwargs.pop("deps")
102
103    if wrap_static_fns:
104        native.filegroup(
105            name = name + "__bindgen_c_thunks",
106            srcs = [":" + name + "__bindgen"],
107            output_group = "bindgen_c_thunks",
108        )
109
110        cc_library(
111            name = name + "__bindgen_c_thunks_library",
112            srcs = [":" + name + "__bindgen_c_thunks"],
113            deps = [cc_lib],
114        )
115
116    rust_library(
117        name = name,
118        srcs = [name + "__bindgen.rs"],
119        deps = deps + [":" + name + "__bindgen"] + ([":" + name + "__bindgen_c_thunks_library"] if wrap_static_fns else []),
120        tags = tags,
121        **kwargs
122    )
123
124def _get_user_link_flags(cc_lib):
125    linker_flags = []
126
127    for linker_input in cc_lib[CcInfo].linking_context.linker_inputs.to_list():
128        linker_flags.extend(linker_input.user_link_flags)
129
130    return linker_flags
131
132def _generate_cc_link_build_info(ctx, cc_lib):
133    """Produce the eqivilant cargo_build_script providers for use in linking the library.
134
135    Args:
136        ctx (ctx): The rule's context object
137        cc_lib (Target): The `rust_bindgen.cc_lib` target.
138
139    Returns:
140        The `BuildInfo` provider.
141    """
142    compile_data = []
143
144    rustc_flags = []
145    linker_search_paths = []
146
147    for linker_input in cc_lib[CcInfo].linking_context.linker_inputs.to_list():
148        for lib in linker_input.libraries:
149            if lib.static_library:
150                rustc_flags.append("-lstatic={}".format(get_lib_name_default(lib.static_library)))
151                linker_search_paths.append(lib.static_library.dirname)
152                compile_data.append(lib.static_library)
153            elif lib.pic_static_library:
154                rustc_flags.append("-lstatic={}".format(get_lib_name_default(lib.pic_static_library)))
155                linker_search_paths.append(lib.pic_static_library.dirname)
156                compile_data.append(lib.pic_static_library)
157
158    if not compile_data:
159        fail("No static libraries found in {}".format(
160            cc_lib.label,
161        ))
162
163    rustc_flags_file = ctx.actions.declare_file("{}.rustc_flags".format(ctx.label.name))
164    ctx.actions.write(
165        output = rustc_flags_file,
166        content = "\n".join(rustc_flags),
167    )
168
169    link_search_paths = ctx.actions.declare_file("{}.link_search_paths".format(ctx.label.name))
170    ctx.actions.write(
171        output = link_search_paths,
172        content = "\n".join([
173            "-Lnative=${{pwd}}/{}".format(path)
174            for path in depset(linker_search_paths).to_list()
175        ]),
176    )
177
178    return BuildInfo(
179        compile_data = depset(compile_data),
180        dep_env = None,
181        flags = rustc_flags_file,
182        # linker_flags is provided via CcInfo
183        linker_flags = None,
184        link_search_paths = link_search_paths,
185        out_dir = None,
186        rustc_env = None,
187    )
188
189def _rust_bindgen_impl(ctx):
190    # nb. We can't grab the cc_library`s direct headers, so a header must be provided.
191    cc_lib = ctx.attr.cc_lib
192    header = ctx.file.header
193    cc_header_list = ctx.attr.cc_lib[CcInfo].compilation_context.headers.to_list()
194    if header not in cc_header_list:
195        fail("Header {} is not in {}'s transitive headers.".format(ctx.attr.header, cc_lib), "header")
196
197    toolchain = ctx.toolchains[Label("//:toolchain_type")]
198    bindgen_bin = toolchain.bindgen
199    clang_bin = toolchain.clang
200    libclang = toolchain.libclang
201    libstdcxx = toolchain.libstdcxx
202
203    output = ctx.outputs.out
204
205    cc_toolchain, feature_configuration = find_cc_toolchain(ctx = ctx)
206
207    tools = depset(([clang_bin] if clang_bin else []), transitive = [cc_toolchain.all_files])
208
209    # libclang should only have 1 output file
210    libclang_dir = _get_libs_for_static_executable(libclang).to_list()[0].dirname
211
212    env = {
213        "LIBCLANG_PATH": libclang_dir,
214        "RUST_BACKTRACE": "1",
215    }
216    if clang_bin:
217        env["CLANG_PATH"] = clang_bin.path
218
219    args = ctx.actions.args()
220
221    # Configure Bindgen Arguments
222    args.add_all(ctx.attr.bindgen_flags)
223    args.add(header)
224    args.add("--output", output)
225
226    wrap_static_fns = getattr(ctx.attr, "wrap_static_fns", False)
227
228    c_output = None
229    if wrap_static_fns:
230        if "--wrap-static-fns" in ctx.attr.bindgen_flags:
231            fail("Do not pass `--wrap-static-fns` to `bindgen_flags, it's added automatically." +
232                 "The generated C file is accesible in the `bindgen_c_thunks` output group.")
233        c_output = ctx.actions.declare_file(ctx.label.name + ".bindgen_c_thunks.c")
234        args.add("--experimental")
235        args.add("--wrap-static-fns")
236        args.add("--wrap-static-fns-path")
237        args.add(c_output.path)
238
239    # Vanilla usage of bindgen produces formatted output, here we do the same if we have `rustfmt` in our toolchain.
240    rustfmt_toolchain = ctx.toolchains[Label("@rules_rust//rust/rustfmt:toolchain_type")]
241    if rustfmt_toolchain and toolchain.default_rustfmt:
242        # Bindgen is able to find rustfmt using the RUSTFMT environment variable
243        env.update({"RUSTFMT": rustfmt_toolchain.rustfmt.path})
244        tools = depset(transitive = [tools, rustfmt_toolchain.all_files])
245    else:
246        args.add("--no-rustfmt-bindings")
247
248    # Configure Clang Arguments
249    args.add("--")
250
251    compile_variables = cc_common.create_compile_variables(
252        cc_toolchain = cc_toolchain,
253        feature_configuration = feature_configuration,
254        include_directories = cc_lib[CcInfo].compilation_context.includes,
255        quote_include_directories = cc_lib[CcInfo].compilation_context.quote_includes,
256        system_include_directories = depset(
257            transitive = [cc_lib[CcInfo].compilation_context.system_includes],
258            direct = cc_toolchain.built_in_include_directories,
259        ),
260        user_compile_flags = ctx.attr.clang_flags,
261    )
262    compile_flags = cc_common.get_memory_inefficient_command_line(
263        feature_configuration = feature_configuration,
264        action_name = CPP_COMPILE_ACTION_NAME,
265        variables = compile_variables,
266    )
267
268    # Bindgen forcibly uses clang.
269    # It's possible that the selected cc_toolchain isn't clang, and may specify flags which clang doesn't recognise.
270    # Ideally we could depend on a more specific toolchain, requesting one which is specifically clang via some constraint.
271    # Unfortunately, we can't currently rely on this, so instead we filter only to flags we know clang supports.
272    # We can add extra flags here as needed.
273    flags_known_to_clang = (
274        "-I",
275        "-iquote",
276        "-isystem",
277        "--sysroot",
278        "--gcc-toolchain",
279        "--target",
280        "-W",
281        "--system-header-prefix",
282        "--no-system-header-prefix",
283        "-Xclang",
284        "-D",
285        "-no-canonical-prefixes",
286        "-nostd",
287    )
288    open_arg = False
289    for arg in compile_flags:
290        if open_arg:
291            args.add(arg)
292            open_arg = False
293            continue
294
295        # The cc_toolchain merged these flags into its returned flags - don't strip these out.
296        if arg in ctx.attr.clang_flags:
297            args.add(arg)
298            continue
299
300        if not arg.startswith(flags_known_to_clang):
301            continue
302
303        args.add(arg)
304
305        if arg in flags_known_to_clang:
306            open_arg = True
307            continue
308
309    _, _, linker_env = get_linker_and_args(ctx, "bin", cc_toolchain, feature_configuration, None)
310    env.update(**linker_env)
311
312    # Set the dynamic linker search path so that clang uses the libstdcxx from the toolchain.
313    # DYLD_LIBRARY_PATH is LD_LIBRARY_PATH on macOS.
314    if libstdcxx:
315        env["LD_LIBRARY_PATH"] = ":".join([f.dirname for f in _get_libs_for_static_executable(libstdcxx).to_list()])
316        env["DYLD_LIBRARY_PATH"] = env["LD_LIBRARY_PATH"]
317
318    ctx.actions.run(
319        executable = bindgen_bin,
320        inputs = depset(
321            [header],
322            transitive = [
323                cc_lib[CcInfo].compilation_context.headers,
324                _get_libs_for_static_executable(libclang),
325            ] + ([
326                _get_libs_for_static_executable(libstdcxx),
327            ] if libstdcxx else []),
328        ),
329        outputs = [output] + ([c_output] if wrap_static_fns else []),
330        mnemonic = "RustBindgen",
331        progress_message = "Generating bindings for {}..".format(header.path),
332        env = env,
333        arguments = [args],
334        tools = tools,
335        # ctx.actions.run now require (through a buildifier check) that we
336        # specify this
337        toolchain = None,
338    )
339
340    if ctx.attr.merge_cc_lib_objects_into_rlib:
341        providers = [
342            _generate_cc_link_build_info(ctx, cc_lib),
343            # As in https://github.com/bazelbuild/rules_rust/pull/2361, we want
344            # to link cc_lib to the direct parent (rlib) using `-lstatic=<cc_lib>`
345            # rustc flag. Hence, we do not need to provide the whole CcInfo of
346            # cc_lib because it will cause the downstream binary to link the cc_lib
347            # again. The CcInfo here only contains the custom link flags (i.e.
348            # linkopts attribute) specified by users in cc_lib.
349            CcInfo(
350                linking_context = cc_common.create_linking_context(
351                    linker_inputs = depset([cc_common.create_linker_input(
352                        owner = ctx.label,
353                        user_link_flags = _get_user_link_flags(cc_lib),
354                    )]),
355                ),
356            ),
357        ]
358    else:
359        providers = [cc_lib[CcInfo]]
360
361    return providers + [
362        OutputGroupInfo(
363            bindgen_bindings = depset([output]),
364            bindgen_c_thunks = depset(([c_output] if wrap_static_fns else [])),
365        ),
366    ]
367
368rust_bindgen = rule(
369    doc = "Generates a rust source file from a cc_library and a header.",
370    implementation = _rust_bindgen_impl,
371    attrs = {
372        "bindgen_flags": attr.string_list(
373            doc = "Flags to pass directly to the bindgen executable. See https://rust-lang.github.io/rust-bindgen/ for details.",
374        ),
375        "cc_lib": attr.label(
376            doc = "The cc_library that contains the `.h` file. This is used to find the transitive includes.",
377            providers = [CcInfo],
378            mandatory = True,
379        ),
380        "clang_flags": attr.string_list(
381            doc = "Flags to pass directly to the clang executable.",
382        ),
383        "header": attr.label(
384            doc = "The `.h` file to generate bindings for.",
385            allow_single_file = True,
386            mandatory = True,
387        ),
388        "merge_cc_lib_objects_into_rlib": attr.bool(
389            doc = ("When True, objects from `cc_lib` will be copied into the `rlib` archive produced by " +
390                   "the rust_library that depends on this `rust_bindgen` rule (using `BuildInfo` provider)"),
391            default = True,
392        ),
393        "wrap_static_fns": attr.bool(
394            doc = "Whether to create a separate .c file for static fns. Requires nightly toolchain, and a header that actually needs this feature (otherwise bindgen won't generate the file and Bazel complains).",
395            default = False,
396        ),
397        "_cc_toolchain": attr.label(
398            default = Label("@bazel_tools//tools/cpp:current_cc_toolchain"),
399        ),
400        "_process_wrapper": attr.label(
401            default = Label("@rules_rust//util/process_wrapper"),
402            executable = True,
403            allow_single_file = True,
404            cfg = "exec",
405        ),
406    },
407    outputs = {"out": "%{name}.rs"},
408    fragments = ["cpp"],
409    toolchains = [
410        config_common.toolchain_type("//:toolchain_type"),
411        config_common.toolchain_type("@rules_rust//rust:toolchain_type"),
412        config_common.toolchain_type("@rules_rust//rust/rustfmt:toolchain_type", mandatory = False),
413        config_common.toolchain_type("@bazel_tools//tools/cpp:toolchain_type"),
414    ],
415)
416
417def _rust_bindgen_toolchain_impl(ctx):
418    return platform_common.ToolchainInfo(
419        bindgen = ctx.executable.bindgen,
420        clang = ctx.executable.clang,
421        libclang = ctx.attr.libclang,
422        libstdcxx = ctx.attr.libstdcxx,
423        default_rustfmt = ctx.attr.default_rustfmt,
424    )
425
426rust_bindgen_toolchain = rule(
427    _rust_bindgen_toolchain_impl,
428    doc = """\
429The tools required for the `rust_bindgen` rule.
430
431This rule depends on the [`bindgen`](https://crates.io/crates/bindgen) binary crate, and it
432in turn depends on both a clang binary and the clang library. To obtain these dependencies,
433`rust_bindgen_dependencies` imports bindgen and its dependencies.
434
435```python
436load("@rules_rust_bindgen//:defs.bzl", "rust_bindgen_toolchain")
437
438rust_bindgen_toolchain(
439    name = "bindgen_toolchain_impl",
440    bindgen = "//my/rust:bindgen",
441    clang = "//my/clang:clang",
442    libclang = "//my/clang:libclang.so",
443    libstdcxx = "//my/cpp:libstdc++",
444)
445
446toolchain(
447    name = "bindgen_toolchain",
448    toolchain = "bindgen_toolchain_impl",
449    toolchain_type = "@rules_rust_bindgen//:toolchain_type",
450)
451```
452
453This toolchain will then need to be registered in the current `WORKSPACE`.
454For additional information, see the [Bazel toolchains documentation](https://docs.bazel.build/versions/master/toolchains.html).
455""",
456    attrs = {
457        "bindgen": attr.label(
458            doc = "The label of a `bindgen` executable.",
459            executable = True,
460            cfg = "exec",
461        ),
462        "clang": attr.label(
463            doc = "The label of a `clang` executable.",
464            executable = True,
465            cfg = "exec",
466            allow_files = True,
467        ),
468        "default_rustfmt": attr.bool(
469            doc = "If set, `rust_bindgen` targets will always format generated sources with `rustfmt`.",
470            mandatory = False,
471            default = True,
472        ),
473        "libclang": attr.label(
474            doc = "A cc_library that provides bindgen's runtime dependency on libclang.",
475            cfg = "exec",
476            providers = [CcInfo],
477            allow_files = True,
478        ),
479        "libstdcxx": attr.label(
480            doc = "A cc_library that satisfies libclang's libstdc++ dependency. This is used to make the execution of clang hermetic. If None, system libraries will be used instead.",
481            cfg = "exec",
482            providers = [CcInfo],
483            mandatory = False,
484            allow_files = True,
485        ),
486    },
487)