Skip to content

Commit

Permalink
"sandwich" rlibs between native deps in linker order (#1333)
Browse files Browse the repository at this point in the history
 "sandwich" rlibs between two sections of native dependencies in linker order
  • Loading branch information
krasimirgg authored May 13, 2022
1 parent df35490 commit 51f8e30
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
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

0 comments on commit 51f8e30

Please sign in to comment.