Skip to content

"sandwich" rlibs between native deps in linker order #1333

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

Merged
merged 4 commits into from
May 13, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions rust/private/rustc.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -1301,8 +1301,26 @@ def _portable_link_flags(lib, use_pic, ambiguous_libs, experimental_use_whole_ar
modifiers = ""
if experimental_use_whole_archive_for_native_deps:
modifiers = ":+whole-archive"

# To ensure appropriate linker library argument order, in the presence
# of both native libraries that depend on rlibs and rlibs that depend
# on native libraries, we use an approach where we "sandwich" the
# rust libraries between two similar sections of all of native
# libraries:
# n1 n2 ... r1 r2 ... n1 n2 ...
# A B C
# This way any dependency from a native library to a rust library
# is resolved from A to B, and any dependency from a rust library to
# a native one is resolved from B to C.
# The question of resolving dependencies from a native library from A
# to any rust library is addressed in a different place, where we
# create symlinks to the rlibs, pretending they are native libraries,
# and adding references to these symlinks in the native section A.
# We rely in the behavior of -Clink-arg to put the linker args
# at the end of the linker invocation constructed by rustc.
return [
"-lstatic%s=%s" % (modifiers, get_lib_name(artifact)),
"-Clink-arg=-l%s" % get_lib_name(artifact),
]
elif _is_dylib(lib):
return [
Expand Down
4 changes: 4 additions & 0 deletions test/unit/native_deps/native_deps_test.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ def _cdylib_has_native_libs_test_impl(ctx):
assert_argv_contains_prefix_suffix(env, action, "-Lnative=", "/native_deps")
assert_argv_contains(env, action, "--crate-type=cdylib")
assert_argv_contains(env, action, "-lstatic=native_dep")
assert_argv_contains(env, action, "-Clink-arg=-lnative_dep")
assert_argv_contains_prefix(env, action, "--codegen=linker=")
return analysistest.end(env)

Expand All @@ -40,6 +41,7 @@ def _staticlib_has_native_libs_test_impl(ctx):
assert_argv_contains_prefix_suffix(env, action, "-Lnative=", "/native_deps")
assert_argv_contains(env, action, "--crate-type=staticlib")
assert_argv_contains(env, action, "-lstatic=native_dep")
assert_argv_contains(env, action, "-Clink-arg=-lnative_dep")
assert_argv_contains_prefix(env, action, "--codegen=linker=")
return analysistest.end(env)

Expand All @@ -51,6 +53,7 @@ def _proc_macro_has_native_libs_test_impl(ctx):
assert_argv_contains_prefix_suffix(env, action, "-Lnative=", "/native_deps")
assert_argv_contains(env, action, "--crate-type=proc-macro")
assert_argv_contains(env, action, "-lstatic=native_dep")
assert_argv_contains(env, action, "-Clink-arg=-lnative_dep")
assert_argv_contains_prefix(env, action, "--codegen=linker=")
return analysistest.end(env)

Expand All @@ -60,6 +63,7 @@ def _bin_has_native_libs_test_impl(ctx):
action = tut.actions[0]
assert_argv_contains_prefix_suffix(env, action, "-Lnative=", "/native_deps")
assert_argv_contains(env, action, "-lstatic=native_dep")
assert_argv_contains(env, action, "-Clink-arg=-lnative_dep")
assert_argv_contains_prefix(env, action, "--codegen=linker=")
return analysistest.end(env)

Expand Down