Skip to content

Commit ca9a40f

Browse files
author
Vinh Tran
committed
Fix link_flags file produced by bindgen rule
1 parent 8a13173 commit ca9a40f

File tree

5 files changed

+52
-29
lines changed

5 files changed

+52
-29
lines changed

bindgen/private/bindgen.bzl

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -110,17 +110,21 @@ def _generate_cc_link_build_info(ctx, cc_lib):
110110
The `BuildInfo` provider.
111111
"""
112112
compile_data = []
113+
114+
# rustc_flags are passed directly to rustc while linker_flags are passed
115+
# the linker invoked by either rustc or cc_common.link
116+
rustc_flags = []
113117
linker_flags = []
114118
linker_search_paths = []
115119

116120
for linker_input in cc_lib[CcInfo].linking_context.linker_inputs.to_list():
117121
for lib in linker_input.libraries:
118122
if lib.static_library:
119-
linker_flags.append("-lstatic={}".format(get_lib_name_default(lib.static_library)))
123+
rustc_flags.append("-lstatic={}".format(get_lib_name_default(lib.static_library)))
120124
linker_search_paths.append(lib.static_library.dirname)
121125
compile_data.append(lib.static_library)
122126
elif lib.pic_static_library:
123-
linker_flags.append("-lstatic={}".format(get_lib_name_default(lib.pic_static_library)))
127+
rustc_flags.append("-lstatic={}".format(get_lib_name_default(lib.pic_static_library)))
124128
linker_search_paths.append(lib.pic_static_library.dirname)
125129
compile_data.append(lib.pic_static_library)
126130

@@ -131,9 +135,15 @@ def _generate_cc_link_build_info(ctx, cc_lib):
131135
cc_lib.label,
132136
))
133137

134-
link_flags = ctx.actions.declare_file("{}.link_flags".format(ctx.label.name))
138+
rustc_flags_file = ctx.actions.declare_file("{}.rustc_flags".format(ctx.label.name))
139+
ctx.actions.write(
140+
output = rustc_flags_file,
141+
content = "\n".join(rustc_flags),
142+
)
143+
144+
linker_flags_file = ctx.actions.declare_file("{}.linker_flags".format(ctx.label.name))
135145
ctx.actions.write(
136-
output = link_flags,
146+
output = linker_flags_file,
137147
content = "\n".join(linker_flags),
138148
)
139149

@@ -149,8 +159,8 @@ def _generate_cc_link_build_info(ctx, cc_lib):
149159
return BuildInfo(
150160
compile_data = depset(compile_data),
151161
dep_env = None,
152-
flags = None,
153-
link_flags = link_flags,
162+
flags = rustc_flags_file,
163+
linker_flags = linker_flags_file,
154164
link_search_paths = link_search_paths,
155165
out_dir = None,
156166
rustc_env = None,

cargo/private/cargo_build_script.bzl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -299,7 +299,7 @@ def _cargo_build_script_impl(ctx):
299299
rustc_env = env_out,
300300
dep_env = dep_env_out,
301301
flags = flags_out,
302-
link_flags = link_flags,
302+
linker_flags = link_flags,
303303
link_search_paths = link_search_paths,
304304
compile_data = depset([]),
305305
),

examples/bindgen/simple/BUILD.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,6 @@ cc_library(
1111
name = "simple",
1212
srcs = ["simple.cc"],
1313
hdrs = ["simple.h"],
14+
linkopts = ["-framework"],
1415
visibility = ["//bindgen:__pkg__"],
1516
)

rust/private/providers.bzl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,8 @@ BuildInfo = provider(
7575
"compile_data": "Depset[File]: Compile data provided by the build script that was not copied into `out_dir`.",
7676
"dep_env": "Optinal[File]: extra build script environment varibles to be set to direct dependencies.",
7777
"flags": "Optional[File]: file containing additional flags to pass to rustc",
78-
"link_flags": "Optional[File]: file containing flags to pass to the linker",
79-
"link_search_paths": "Optional[File]: file containing search paths to pass to the linker",
78+
"linker_flags": "Optional[File]: file containing flags to pass to the linker",
79+
"link_search_paths": "Optional[File]: file containing search paths to pass to rustc and linker",
8080
"out_dir": "Optional[File]: directory containing the result of a build script",
8181
"rustc_env": "Optional[File]: file containing additional environment variables to set for rustc.",
8282
},

rust/private/rustc.bzl

Lines changed: 32 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -478,9 +478,9 @@ def _process_build_scripts(
478478
- (File): An optional path to a generated environment file from a `cargo_build_script` target
479479
- (depset[File]): All direct and transitive build flags from the current build info.
480480
"""
481-
extra_inputs, out_dir, build_env_file, build_flags_files = _create_extra_input_args(build_info, dep_info)
481+
extra_inputs, out_dir, build_env_file, flags_files, linker_flags_files = _create_extra_input_args(build_info, dep_info)
482482
compile_inputs = depset(transitive = [extra_inputs, compile_inputs])
483-
return compile_inputs, out_dir, build_env_file, build_flags_files
483+
return compile_inputs, out_dir, build_env_file, flags_files, linker_flags_files
484484

485485
def _symlink_for_ambiguous_lib(actions, toolchain, crate_info, lib):
486486
"""Constructs a disambiguating symlink for a library dependency.
@@ -764,12 +764,11 @@ def collect_inputs(
764764
# For backwards compatibility, we also check the value of the `rustc_env_files` attribute when
765765
# `crate_info.rustc_env_files` is not populated.
766766
build_env_files = crate_info.rustc_env_files if crate_info.rustc_env_files else getattr(files, "rustc_env_files", [])
767-
compile_inputs, out_dir, build_env_file, build_flags_files = _process_build_scripts(build_info, dep_info, compile_inputs)
767+
compile_inputs, out_dir, build_env_file, flags_files, link_flag_files = _process_build_scripts(build_info, dep_info, compile_inputs)
768768
if build_env_file:
769769
build_env_files = [f for f in build_env_files] + [build_env_file]
770770
compile_inputs = depset(build_env_files, transitive = [compile_inputs])
771-
772-
return compile_inputs, out_dir, build_env_files, build_flags_files, linkstamp_outs, ambiguous_libs
771+
return compile_inputs, out_dir, build_env_files, flags_files, link_flag_files, linkstamp_outs, ambiguous_libs
773772

774773
def construct_arguments(
775774
ctx,
@@ -787,7 +786,8 @@ def construct_arguments(
787786
rust_flags,
788787
out_dir,
789788
build_env_files,
790-
build_flags_files,
789+
flags_files,
790+
linker_flags_files,
791791
emit = ["dep-info", "link"],
792792
force_all_deps_direct = False,
793793
rustdoc = False,
@@ -852,7 +852,7 @@ def construct_arguments(
852852
for build_env_file in build_env_files:
853853
process_wrapper_flags.add("--env-file", build_env_file)
854854

855-
process_wrapper_flags.add_all(build_flags_files, before_each = "--arg-file")
855+
process_wrapper_flags.add_all(flags_files, before_each = "--arg-file")
856856

857857
# Certain rust build processes expect to find files from the environment
858858
# variable `$CARGO_MANIFEST_DIR`. Examples of this include pest, tera,
@@ -1011,6 +1011,8 @@ def construct_arguments(
10111011
env.update(link_env)
10121012
rustc_flags.add(ld, format = "--codegen=linker=%s")
10131013
rustc_flags.add_joined("--codegen", link_args, join_with = " ", format_joined = "link-args=%s")
1014+
# TODO pass linkopts (now in linker_flags_files) from cc deps to linker
1015+
# This can be done by propagating linkopts from cc deps to downstream via CcInfo
10141016

10151017
_add_native_link_flags(rustc_flags, dep_info, linkstamp_outs, ambiguous_libs, crate_info.type, toolchain, cc_toolchain, feature_configuration, compilation_mode)
10161018

@@ -1156,7 +1158,7 @@ def rustc_compile_action(
11561158
# Determine if the build is currently running with --stamp
11571159
stamp = is_stamping_enabled(attr)
11581160

1159-
compile_inputs, out_dir, build_env_files, build_flags_files, linkstamp_outs, ambiguous_libs = collect_inputs(
1161+
compile_inputs, out_dir, build_env_files, flags_files, flags_files, linkstamp_outs, ambiguous_libs = collect_inputs(
11601162
ctx = ctx,
11611163
file = ctx.file,
11621164
files = ctx.files,
@@ -1202,7 +1204,8 @@ def rustc_compile_action(
12021204
rust_flags = rust_flags,
12031205
out_dir = out_dir,
12041206
build_env_files = build_env_files,
1205-
build_flags_files = build_flags_files,
1207+
flags_files = flags_files,
1208+
linker_flags_files = linker_flags_files,
12061209
force_all_deps_direct = force_all_deps_direct,
12071210
stamp = stamp,
12081211
use_json_output = bool(build_metadata) or bool(rustc_output) or bool(rustc_rmeta_output),
@@ -1227,8 +1230,8 @@ def rustc_compile_action(
12271230
output_hash = output_hash,
12281231
rust_flags = rust_flags,
12291232
out_dir = out_dir,
1230-
build_env_files = build_env_files,
1231-
build_flags_files = build_flags_files,
1233+
flags_files = flags_files,
1234+
link_flag_files = linker_flags_files,
12321235
force_all_deps_direct = force_all_deps_direct,
12331236
stamp = stamp,
12341237
use_json_output = True,
@@ -1318,8 +1321,8 @@ def rustc_compile_action(
13181321
)
13191322
elif hasattr(ctx.executable, "_bootstrap_process_wrapper"):
13201323
# Run without process_wrapper
1321-
if build_env_files or build_flags_files or stamp or build_metadata:
1322-
fail("build_env_files, build_flags_files, stamp, build_metadata are not supported when building without process_wrapper")
1324+
if build_env_files or flags_files or linker_flags_files or stamp or build_metadata:
1325+
fail("build_env_files, flags_files, linker_flags_files, stamp, build_metadata are not supported when building without process_wrapper")
13231326
ctx.actions.run(
13241327
executable = ctx.executable._bootstrap_process_wrapper,
13251328
inputs = compile_inputs,
@@ -1650,7 +1653,8 @@ def _create_extra_input_args(build_info, dep_info):
16501653
- (depset[File]): A list of all build info `OUT_DIR` File objects
16511654
- (str): The `OUT_DIR` of the current build info
16521655
- (File): An optional generated environment file from a `cargo_build_script` target
1653-
- (depset[File]): All direct and transitive build flag files from the current build info.
1656+
- (depset[File]): All direct and transitive build flag files for compiling from the current build info.
1657+
- (depset[File]): All direct and transitive build flag files for linking from the current build info.
16541658
"""
16551659
input_files = []
16561660
input_depsets = []
@@ -1659,25 +1663,33 @@ def _create_extra_input_args(build_info, dep_info):
16591663
# to create the final command line
16601664
out_dir = None
16611665
build_env_file = None
1662-
build_flags_files = []
1666+
1667+
# We need to make a distinction for flags files here to decide whether to
1668+
# pass them to rustc based on the output crate type
1669+
# linker_flags_files are not needed for bin, dylib, and cdylib crate types
1670+
# because a linker is not invoked to produce such crates
1671+
flags_files = []
1672+
link_flag_files = []
16631673

16641674
if build_info:
16651675
if build_info.out_dir:
16661676
out_dir = build_info.out_dir.path
16671677
input_files.append(build_info.out_dir)
16681678
build_env_file = build_info.rustc_env
16691679
if build_info.flags:
1670-
build_flags_files.append(build_info.flags)
1671-
if build_info.link_flags:
1672-
build_flags_files.append(build_info.link_flags)
1673-
input_files.append(build_info.link_flags)
1680+
flags_files.append(build_info.flags)
1681+
if build_info.linker_flags:
1682+
link_flag_files.append(build_info.linker_flags)
1683+
input_files.append(build_info.linker_flags)
16741684
input_depsets.append(build_info.compile_data)
16751685

16761686
return (
1687+
# TODO: Split input_files into compile_input_files and link_input_files
16771688
depset(input_files, transitive = [dep_info.link_search_path_files] + input_depsets),
16781689
out_dir,
16791690
build_env_file,
1680-
depset(build_flags_files, transitive = [dep_info.link_search_path_files]),
1691+
depset(flags_files, transitive = [dep_info.link_search_path_files]),
1692+
depset(link_flag_files, transitive = [dep_info.link_search_path_files]),
16811693
)
16821694

16831695
def _compute_rpaths(toolchain, output_dir, dep_info, use_pic):

0 commit comments

Comments
 (0)