diff options
author | Kevin King <kcking@users.noreply.github.com> | 2024-02-12 02:49:38 -0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-02-12 10:49:38 +0000 |
commit | d0d5a861f3e31ca7d29244d66a9496ca8d6124a6 (patch) | |
tree | 519b84c8b26b784bb123514d6e2052aab469eced | |
parent | 63a73ac7f003bf72b6a109eb2634ddb9ae3ad3fb (diff) | |
download | bazelbuild-rules_rust-d0d5a861f3e31ca7d29244d66a9496ca8d6124a6.tar.gz |
Use -C link-arg= instead of link-args to handle nested spaces (#2471)
Co-authored-by: scentini <rosica@google.com>
-rw-r--r-- | rust/private/rustc.bzl | 5 | ||||
-rw-r--r-- | test/unit/native_deps/native_deps_test.bzl | 86 |
2 files changed, 51 insertions, 40 deletions
diff --git a/rust/private/rustc.bzl b/rust/private/rustc.bzl index b4f03b89..5c11f762 100644 --- a/rust/private/rustc.bzl +++ b/rust/private/rustc.bzl @@ -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) diff --git a/test/unit/native_deps/native_deps_test.bzl b/test/unit/native_deps/native_deps_test.bzl index a086afb3..b38748da 100644 --- a/test/unit/native_deps/native_deps_test.bzl +++ b/test/unit/native_deps/native_deps_test.bzl @@ -113,13 +113,21 @@ 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=").removeprefix("-C").removeprefix("link-arg=").removeprefix("link-args=") + for a in argv + if ( + a.startswith("--codegen=link-arg=") or + a.startswith("--codegen=link-args=") or + a.startswith("-Clink-args=") or + a.startswith("-Clink-arg=") or + a.startswith("link-args=") or + a.startswith("link-arg=") 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) @@ -129,39 +137,39 @@ def _bin_has_native_dep_and_alwayslink_test_impl(ctx): toolchain = _get_toolchain(ctx) compilation_mode = ctx.var["COMPILATION_MODE"] workspace_prefix = "" if ctx.workspace_name == "rules_rust" else "external/rules_rust/" - individual_link_args = [ - arg - for arg in _extract_linker_args(action.argv) - if arg.startswith("link-arg=") or arg.startswith("-lstatic=") - ] + link_args = _extract_linker_args(action.argv) if toolchain.target_os == "darwin": - darwin_component = _get_darwin_component(individual_link_args[-1]) + darwin_component = _get_darwin_component(link_args[-1]) want = [ "-lstatic=native_dep", - "link-arg=-Wl,-force_load,bazel-out/{}-{}/bin/{}test/unit/native_deps/libalwayslink.lo".format(darwin_component, compilation_mode, workspace_prefix), + "-lnative_dep", + "-Wl,-force_load,bazel-out/{}-{}/bin/{}test/unit/native_deps/libalwayslink.lo".format(darwin_component, compilation_mode, workspace_prefix), ] - asserts.equals(env, want, individual_link_args) + assert_list_contains_adjacent_elements(env, link_args, want) elif toolchain.target_os == "windows": if toolchain.target_triple.abi == "msvc": want = [ "-lstatic=native_dep", - "link-arg=/WHOLEARCHIVE:bazel-out/x64_windows-{}/bin/{}test/unit/native_deps/alwayslink.lo.lib".format(compilation_mode, workspace_prefix), + "native_dep.lib", + "/WHOLEARCHIVE:bazel-out/x64_windows-{}/bin/{}test/unit/native_deps/alwayslink.lo.lib".format(compilation_mode, workspace_prefix), ] else: want = [ "-lstatic=native_dep", - "link-arg=-Wl,--whole-archive", - "link-arg=bazel-out/x64_windows-{}/bin/{}test/unit/native_deps/alwayslink.lo.lib".format(compilation_mode, workspace_prefix), - "link-arg=-Wl,--no-whole-archive", + "native_dep.lib", + "-Wl,--whole-archive", + "bazel-out/x64_windows-{}/bin/{}test/unit/native_deps/alwayslink.lo.lib".format(compilation_mode, workspace_prefix), + "-Wl,--no-whole-archive", ] else: want = [ "-lstatic=native_dep", - "link-arg=-Wl,--whole-archive", - "link-arg=bazel-out/k8-{}/bin/{}test/unit/native_deps/libalwayslink.lo".format(compilation_mode, workspace_prefix), - "link-arg=-Wl,--no-whole-archive", + "-lnative_dep", + "-Wl,--whole-archive", + "bazel-out/k8-{}/bin/{}test/unit/native_deps/libalwayslink.lo".format(compilation_mode, workspace_prefix), + "-Wl,--no-whole-archive", ] - asserts.equals(env, want, individual_link_args) + assert_list_contains_adjacent_elements(env, link_args, want) return analysistest.end(env) def _cdylib_has_native_dep_and_alwayslink_test_impl(ctx): @@ -171,8 +179,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"] @@ -182,29 +189,33 @@ def _cdylib_has_native_dep_and_alwayslink_test_impl(ctx): darwin_component = _get_darwin_component(linker_args[-1]) want = [ "-lstatic=native_dep{}".format(pic_suffix), - "link-arg=-Wl,-force_load,bazel-out/{}-{}/bin/{}test/unit/native_deps/libalwayslink{}.lo".format(darwin_component, compilation_mode, workspace_prefix, pic_suffix), + "-lnative_dep{}".format(pic_suffix), + "-Wl,-force_load,bazel-out/{}-{}/bin/{}test/unit/native_deps/libalwayslink{}.lo".format(darwin_component, compilation_mode, workspace_prefix, pic_suffix), ] elif toolchain.target_os == "windows": if toolchain.target_triple.abi == "msvc": want = [ "-lstatic=native_dep", - "link-arg=/WHOLEARCHIVE:bazel-out/x64_windows-{}/bin/{}test/unit/native_deps/alwayslink.lo.lib".format(compilation_mode, workspace_prefix), + "native_dep.lib", + "/WHOLEARCHIVE:bazel-out/x64_windows-{}/bin/{}test/unit/native_deps/alwayslink.lo.lib".format(compilation_mode, workspace_prefix), ] else: want = [ "-lstatic=native_dep", - "link-arg=-Wl,--whole-archive", - "link-arg=bazel-out/x64_windows-{}/bin/{}test/unit/native_deps/alwayslink.lo.lib".format(compilation_mode, workspace_prefix), - "link-arg=-Wl,--no-whole-archive", + "native_dep.lib", + "-Wl,--whole-archive", + "bazel-out/x64_windows-{}/bin/{}test/unit/native_deps/alwayslink.lo.lib".format(compilation_mode, workspace_prefix), + "-Wl,--no-whole-archive", ] else: want = [ "-lstatic=native_dep{}".format(pic_suffix), - "link-arg=-Wl,--whole-archive", - "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", + "-lnative_dep{}".format(pic_suffix), + "-Wl,--whole-archive", + "bazel-out/k8-{}/bin/{}test/unit/native_deps/libalwayslink{}.lo".format(compilation_mode, workspace_prefix, pic_suffix), + "-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): @@ -334,11 +345,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, |