Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 26 additions & 41 deletions bindgen/private/bindgen.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -69,13 +69,13 @@ def rust_bindgen_library(

sub_tags = tags + ([] if "manual" in tags else ["manual"])

deps = kwargs.get("deps") or []
if "deps" in kwargs:
kwargs.pop("deps")

bindgen_kwargs = {}
if "leak_symbols" in kwargs:
bindgen_kwargs.update({"leak_symbols": kwargs.pop("leak_symbols")})
for shared in (
"target_compatible_with",
"exec_compatible_with",
):
if shared in kwargs:
bindgen_kwargs.update({shared: kwargs[shared]})

rust_bindgen(
name = name + "__bindgen",
Expand All @@ -88,8 +88,11 @@ def rust_bindgen_library(
**bindgen_kwargs
)

for custom_tag in ["__bindgen", "no-clippy", "no-rustfmt"]:
tags = tags + ([] if custom_tag in tags else [custom_tag])
tags = depset(tags + ["__bindgen", "no-clippy", "no-rustfmt"]).to_list()

deps = kwargs.get("deps") or []
if "deps" in kwargs:
kwargs.pop("deps")

rust_library(
name = name,
Expand Down Expand Up @@ -281,33 +284,22 @@ def _rust_bindgen_impl(ctx):
tools = tools,
)

if ctx.attr.leak_symbols:
# buildifier: disable=print
print("WARN: rust_bindgen.leak_symbols is set to True for {} - please file an issue at https://github.com/bazelbuild/rules_rust/issues explaining why this was necessary, as this support will be removed soon.".format(ctx.label))
providers = [cc_common.merge_cc_infos(
direct_cc_infos = [cc_lib[CcInfo]],
)]
else:
providers = [
_generate_cc_link_build_info(ctx, cc_lib),
# As in https://github.com/bazelbuild/rules_rust/pull/2361, we want
# to link cc_lib to the direct parent (rlib) using `-lstatic=<cc_lib>` rustc flag
# Hence, we do not need to provide the whole CcInfo of cc_lib because
# it will cause the downstream binary to link the cc_lib again
# (same effect as setting `leak_symbols` attribute above)
# The CcInfo here only contains the custom link flags (i.e. linkopts attribute)
# specified by users in cc_lib
CcInfo(
linking_context = cc_common.create_linking_context(
linker_inputs = depset([cc_common.create_linker_input(
owner = ctx.label,
user_link_flags = _get_user_link_flags(cc_lib),
)]),
),
return [
_generate_cc_link_build_info(ctx, cc_lib),
# As in https://github.com/bazelbuild/rules_rust/pull/2361, we want
# to link cc_lib to the direct parent (rlib) using `-lstatic=<cc_lib>`
# rustc flag. Hence, we do not need to provide the whole CcInfo of
# cc_lib because it will cause the downstream binary to link the cc_lib
# again. The CcInfo here only contains the custom link flags (i.e.
# linkopts attribute) specified by users in cc_lib.
CcInfo(
linking_context = cc_common.create_linking_context(
linker_inputs = depset([cc_common.create_linker_input(
owner = ctx.label,
user_link_flags = _get_user_link_flags(cc_lib),
)]),
),
]

return providers + [
),
OutputGroupInfo(
bindgen_bindings = depset([output]),
),
Expand All @@ -333,13 +325,6 @@ rust_bindgen = rule(
allow_single_file = True,
mandatory = True,
),
"leak_symbols": attr.bool(
doc = (
"If True, `cc_lib` will be exposed and linked into all downstream consumers of the target vs. the " +
"`rust_library` directly consuming it."
),
default = False,
),
"_cc_toolchain": attr.label(
default = Label("@bazel_tools//tools/cpp:current_cc_toolchain"),
),
Expand Down
3 changes: 1 addition & 2 deletions docs/flatten.md
Original file line number Diff line number Diff line change
Expand Up @@ -359,7 +359,7 @@ is available under the key `dsym_folder` in `OutputGroupInfo`.
## rust_bindgen

<pre>
rust_bindgen(<a href="#rust_bindgen-name">name</a>, <a href="#rust_bindgen-bindgen_flags">bindgen_flags</a>, <a href="#rust_bindgen-cc_lib">cc_lib</a>, <a href="#rust_bindgen-clang_flags">clang_flags</a>, <a href="#rust_bindgen-header">header</a>, <a href="#rust_bindgen-leak_symbols">leak_symbols</a>)
rust_bindgen(<a href="#rust_bindgen-name">name</a>, <a href="#rust_bindgen-bindgen_flags">bindgen_flags</a>, <a href="#rust_bindgen-cc_lib">cc_lib</a>, <a href="#rust_bindgen-clang_flags">clang_flags</a>, <a href="#rust_bindgen-header">header</a>)
</pre>

Generates a rust source file from a cc_library and a header.
Expand All @@ -374,7 +374,6 @@ Generates a rust source file from a cc_library and a header.
| <a id="rust_bindgen-cc_lib"></a>cc_lib | The cc_library that contains the <code>.h</code> file. This is used to find the transitive includes. | <a href="https://bazel.build/concepts/labels">Label</a> | required | |
| <a id="rust_bindgen-clang_flags"></a>clang_flags | Flags to pass directly to the clang executable. | List of strings | optional | <code>[]</code> |
| <a id="rust_bindgen-header"></a>header | The <code>.h</code> file to generate bindings for. | <a href="https://bazel.build/concepts/labels">Label</a> | required | |
| <a id="rust_bindgen-leak_symbols"></a>leak_symbols | If True, <code>cc_lib</code> will be exposed and linked into all downstream consumers of the target vs. the <code>rust_library</code> directly consuming it. | Boolean | optional | <code>False</code> |


<a id="rust_bindgen_toolchain"></a>
Expand Down
3 changes: 1 addition & 2 deletions docs/rust_bindgen.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ toolchains following the instructions for [rust_bindgen_toolchain](#rust_bindgen
## rust_bindgen

<pre>
rust_bindgen(<a href="#rust_bindgen-name">name</a>, <a href="#rust_bindgen-bindgen_flags">bindgen_flags</a>, <a href="#rust_bindgen-cc_lib">cc_lib</a>, <a href="#rust_bindgen-clang_flags">clang_flags</a>, <a href="#rust_bindgen-header">header</a>, <a href="#rust_bindgen-leak_symbols">leak_symbols</a>)
rust_bindgen(<a href="#rust_bindgen-name">name</a>, <a href="#rust_bindgen-bindgen_flags">bindgen_flags</a>, <a href="#rust_bindgen-cc_lib">cc_lib</a>, <a href="#rust_bindgen-clang_flags">clang_flags</a>, <a href="#rust_bindgen-header">header</a>)
</pre>

Generates a rust source file from a cc_library and a header.
Expand All @@ -68,7 +68,6 @@ Generates a rust source file from a cc_library and a header.
| <a id="rust_bindgen-cc_lib"></a>cc_lib | The cc_library that contains the <code>.h</code> file. This is used to find the transitive includes. | <a href="https://bazel.build/concepts/labels">Label</a> | required | |
| <a id="rust_bindgen-clang_flags"></a>clang_flags | Flags to pass directly to the clang executable. | List of strings | optional | <code>[]</code> |
| <a id="rust_bindgen-header"></a>header | The <code>.h</code> file to generate bindings for. | <a href="https://bazel.build/concepts/labels">Label</a> | required | |
| <a id="rust_bindgen-leak_symbols"></a>leak_symbols | If True, <code>cc_lib</code> will be exposed and linked into all downstream consumers of the target vs. the <code>rust_library</code> directly consuming it. | Boolean | optional | <code>False</code> |


<a id="rust_bindgen_toolchain"></a>
Expand Down
23 changes: 0 additions & 23 deletions examples/bindgen/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ rust_bindgen_library(
],
cc_lib = "//bindgen/simple",
header = "//bindgen/simple:simple.h",
leak_symbols = False,
)

rust_binary(
Expand All @@ -22,25 +21,3 @@ rust_test(
name = "simple_test",
crate = ":simple_example",
)

rust_bindgen_library(
name = "simple_leaked_bindgen",
bindgen_flags = [
"--allowlist-function=simple_.*",
"--allowlist-var=SIMPLE_.*",
],
cc_lib = "//bindgen/simple",
header = "//bindgen/simple:simple.h",
leak_symbols = True,
)

rust_binary(
name = "simple_leaked_example",
srcs = ["main_leaked.rs"],
deps = [":simple_leaked_bindgen"],
)

rust_test(
name = "simple_leaked_test",
crate = ":simple_leaked_example",
)
22 changes: 0 additions & 22 deletions examples/bindgen/main_leaked.rs

This file was deleted.