diff options
Diffstat (limited to 'bindgen/private/bindgen.bzl')
-rw-r--r-- | bindgen/private/bindgen.bzl | 49 |
1 files changed, 36 insertions, 13 deletions
diff --git a/bindgen/private/bindgen.bzl b/bindgen/private/bindgen.bzl index f32a4fc8..445036ba 100644 --- a/bindgen/private/bindgen.bzl +++ b/bindgen/private/bindgen.bzl @@ -99,6 +99,14 @@ def rust_bindgen_library( **kwargs ) +def _get_user_link_flags(cc_lib): + linker_flags = [] + + for linker_input in cc_lib[CcInfo].linking_context.linker_inputs.to_list(): + linker_flags.extend(linker_input.user_link_flags) + + return linker_flags + def _generate_cc_link_build_info(ctx, cc_lib): """Produce the eqivilant cargo_build_script providers for use in linking the library. @@ -110,33 +118,30 @@ def _generate_cc_link_build_info(ctx, cc_lib): The `BuildInfo` provider. """ compile_data = [] - linker_flags = [] + + rustc_flags = [] linker_search_paths = [] for linker_input in cc_lib[CcInfo].linking_context.linker_inputs.to_list(): for lib in linker_input.libraries: if lib.static_library: - linker_flags.append("-lstatic={}".format(get_lib_name_default(lib.static_library))) + rustc_flags.append("-lstatic={}".format(get_lib_name_default(lib.static_library))) linker_search_paths.append(lib.static_library.dirname) compile_data.append(lib.static_library) elif lib.pic_static_library: - linker_flags.append("-lstatic={}".format(get_lib_name_default(lib.pic_static_library))) + rustc_flags.append("-lstatic={}".format(get_lib_name_default(lib.pic_static_library))) linker_search_paths.append(lib.pic_static_library.dirname) compile_data.append(lib.pic_static_library) - if linker_input.user_link_flags: - linker_flags.append("-C") - linker_flags.append("link-args={}".format(" ".join(linker_input.user_link_flags))) - if not compile_data: fail("No static libraries found in {}".format( cc_lib.label, )) - link_flags = ctx.actions.declare_file("{}.link_flags".format(ctx.label.name)) + rustc_flags_file = ctx.actions.declare_file("{}.rustc_flags".format(ctx.label.name)) ctx.actions.write( - output = link_flags, - content = "\n".join(linker_flags), + output = rustc_flags_file, + content = "\n".join(rustc_flags), ) link_search_paths = ctx.actions.declare_file("{}.link_search_paths".format(ctx.label.name)) @@ -151,8 +156,9 @@ def _generate_cc_link_build_info(ctx, cc_lib): return BuildInfo( compile_data = depset(compile_data), dep_env = None, - flags = None, - link_flags = link_flags, + flags = rustc_flags_file, + # linker_flags is provided via CcInfo + linker_flags = None, link_search_paths = link_search_paths, out_dir = None, rustc_env = None, @@ -282,7 +288,24 @@ def _rust_bindgen_impl(ctx): direct_cc_infos = [cc_lib[CcInfo]], )] else: - providers = [_generate_cc_link_build_info(ctx, cc_lib)] + 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 providers + [ OutputGroupInfo( |