-
Notifications
You must be signed in to change notification settings - Fork 522
Fix #2406: use codegen option link-args to forward user_link_flags in bindgen.
#2407
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
…` in bindgen. This commit fixes issue #2406 by using the [`link-args`] codegen option of `rustc`: ``` -C link-args=<user_link_flags> ``` where `<user_link_flags>` are the flags from the [`user_link_flags`] field of [`LinkerInput`]. [`user_link_flags`]: https://bazel.build/rules/lib/builtins/LinkerInput#user_link_flags [`LinkerInput`]: https://bazel.build/rules/lib/builtins/LinkerInput [`link-args`]: https://doc.rust-lang.org/rustc/codegen-options/index.html#link-args
UebelAndre
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks!
| compile_data.append(lib.pic_static_library) | ||
|
|
||
| linker_flags.extend(linker_input.user_link_flags) | ||
| if linker_input.user_link_flags: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@UebelAndre I think this will only work if we link using rustc. If we run the linker directly (i.e. enabling experimental_use_cc_common_link, it won't work because the linker doesn't understand -C link-args. Only rustc understands -C link-args=<args> and passes <args> to the linker.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Dang, good catch! Any suggestions on how to deal wit this?
cc @thb-sb
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ho, I haven't though of cc_common.link…
So basically, the solution would be to use the former if cc_common.link is used, otherwise the latter.
I'll try something to fix this.
Thank you @daivinhtran for reporting this corner case!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Another subtle thing I notice in the example provided in #2406 is that rust_bindgen_library shouldn't trigger any linking (neither calling rustc with -C linker=<> -C link-args or a separate linking action) because the macro (which creates rust_library -> rust_bindgen) only produces an rlib, not a shared library or binary.
Is
# BUILD
load("@rules_rust//bindgen:defs.bzl", "rust_bindgen_library")
cc_library(
name = "foo",
srcs = ["foo.c"],
hdrs = ["foo.h"],
linkopts = ["-framework", "Security"],
)
rust_bindgen_library(
name = "foo_bindgen",
cc_lib = ":foo",
header = "foo.h",
)
all you need to reproduce the error with the link flags? Or is there another downstream target like rust_binary that requires linking?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
IIUC, the right fix isn't simply
- If use
cc_common.link, don't use-C link-args=<link flags> - Else, use
-C link-args
at the bindgen rule level.
IMO the right fix is to keep link flags without -C link-args= as before.
At the downstream (shared library or binary) targets which run linking, we then decide whether to add -C link-args or not based on the setting of experimental_use_cc_common_link. We already have this logic implemented.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
More context: In
rules_rust/rust/private/rustc.bzl
Lines 998 to 1013 in 90e4505
| if ("link" in emit and crate_info.type not in ["rlib", "lib"]) or rustdoc: | |
| # Rust's built-in linker can handle linking wasm files. We don't want to attempt to use the cc | |
| # linker since it won't understand. | |
| compilation_mode = ctx.var["COMPILATION_MODE"] | |
| if toolchain.target_arch != "wasm32": | |
| if output_dir: | |
| use_pic = _should_use_pic(cc_toolchain, feature_configuration, crate_info.type, compilation_mode) | |
| rpaths = _compute_rpaths(toolchain, output_dir, dep_info, use_pic) | |
| else: | |
| rpaths = depset() | |
| ld, link_args, link_env = get_linker_and_args(ctx, attr, crate_info.type, cc_toolchain, feature_configuration, rpaths, rustdoc) | |
| env.update(link_env) | |
| rustc_flags.add(ld, format = "--codegen=linker=%s") | |
| rustc_flags.add_joined("--codegen", link_args, join_with = " ", format_joined = "link-args=%s") |
The rules' implementation already ignore linking when building rlib and lib. rust_bindgen_library produces rlib which shouldn't involve any link flags at all.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I filed #2413 to triage the issue.
…d `user_link_flags` in bindgen. (bazelbuild#2407)" This reverts commit 90e4505.
This commit fixes issue #2406 by using the
link-argscodegen option ofrustc:where
<user_link_flags>are the flags from theuser_link_flagsfield ofLinkerInput.