Skip to content

Commit

Permalink
Use -C link-arg= instead of link-args to handle nested spaces
Browse files Browse the repository at this point in the history
  • Loading branch information
kcking committed Feb 9, 2024
1 parent 9f68ed2 commit 60d83f3
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 16 deletions.
5 changes: 4 additions & 1 deletion rust/private/rustc.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -1018,7 +1018,10 @@ def construct_arguments(

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")

# Split link args into individual "--codegen=link-arg=" flags to handle nested spaces.
# Additional context: https://github.com/rust-lang/rust/pull/36574
rustc_flags.add_all(link_args, format_each = "--codegen=link-arg=%s")

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

Expand Down
30 changes: 15 additions & 15 deletions test/unit/native_deps/native_deps_test.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -113,13 +113,17 @@ def _bin_has_native_libs_test_impl(ctx):
return analysistest.end(env)

def _extract_linker_args(argv):
return [a for a in argv if (
a.startswith("link-arg=") or
a.startswith("link-args=") or
a.startswith("-l") or
a.endswith(".lo") or
a.endswith(".o")
)]
return [
a.removeprefix("--codegen=link-arg=").removeprefix("--codegen=link-args=")
for a in argv
if (
a.startswith("--codegen=link-arg=") or
a.startswith("--codegen=link-args=") or
a.startswith("-l") or
a.endswith(".lo") or
a.endswith(".o")
)
]

def _bin_has_native_dep_and_alwayslink_test_impl(ctx):
env = analysistest.begin(ctx)
Expand Down Expand Up @@ -171,8 +175,7 @@ def _cdylib_has_native_dep_and_alwayslink_test_impl(ctx):
tut = analysistest.target_under_test(env)
action = tut.actions[0]

# skipping first link-arg since it contains unrelated linker flags
linker_args = _extract_linker_args(action.argv)[1:]
linker_args = _extract_linker_args(action.argv)

toolchain = _get_toolchain(ctx)
compilation_mode = ctx.var["COMPILATION_MODE"]
Expand Down Expand Up @@ -204,7 +207,7 @@ def _cdylib_has_native_dep_and_alwayslink_test_impl(ctx):
"link-arg=bazel-out/k8-{}/bin/{}test/unit/native_deps/libalwayslink{}.lo".format(compilation_mode, workspace_prefix, pic_suffix),
"link-arg=-Wl,--no-whole-archive",
]
asserts.equals(env, want, linker_args)
assert_list_contains_adjacent_elements(env, linker_args, want)
return analysistest.end(env)

def _get_pic_suffix(ctx, compilation_mode):
Expand Down Expand Up @@ -334,11 +337,8 @@ def _linkopts_propagate_test_impl(ctx):
# Consistently with cc rules, dependency linkopts take precedence over
# dependent linkopts (i.e. dependency linkopts appear later in the command
# line).
linkopt_args = [
arg
for arg in _extract_linker_args(action.argv)
if arg.startswith("link-args")
][0].split(" ")

linkopt_args = _extract_linker_args(action.argv)
assert_list_contains_adjacent_elements(
env,
linkopt_args,
Expand Down

0 comments on commit 60d83f3

Please sign in to comment.