@@ -1544,6 +1544,24 @@ def _portable_link_flags(lib, use_pic, ambiguous_libs):
1544
1544
# and adding references to these symlinks in the native section A.
1545
1545
# We rely in the behavior of -Clink-arg to put the linker args
1546
1546
# at the end of the linker invocation constructed by rustc.
1547
+
1548
+ # We skip adding `-Clink-arg=-l for libstd and libtest from the standard library, as
1549
+ # these two libraries are present both as an `.rlib` and a `.so` format.
1550
+ # On linux, Rustc adds a -Bdynamic to the linker command line before the libraries specified
1551
+ # with `-Clink-arg`, which leads to us linking agains the `.so`s but not putting the
1552
+ # corresponding value to the runtime library search paths, which results in a
1553
+ # "cannot open shared object file: No such file or directory" error at exectuion time.
1554
+ # We can fix this by adding a `-Clink-arg=-Bstatic` on linux, but we don't have that option for
1555
+ # macos. The proper solution for this issue would be to remove `libtest-{hash}.so` and `libstd-{hash}.so`
1556
+ # from the toolchain. However, it is not enough to change the toolchain's `rust_std_{...}` filegroups
1557
+ # here: https://github.com/bazelbuild/rules_rust/blob/a9d5d894ad801002d007b858efd154e503796b9f/rust/private/repository_utils.bzl#L144
1558
+ # because rustc manages to escape the sandbox and still finds them at linking time.
1559
+ # We need to modify the repository rules to erase those files completely.
1560
+ if "lib/rustlib" in artifact .path and (
1561
+ artifact .basename .startswith ("libtest-" ) or artifact .basename .startswith ("libstd-" ) or
1562
+ artifact .basename .startswith ("test-" ) or artifact .basename .startswith ("std-" )
1563
+ ):
1564
+ return ["-lstatic=%s" % get_lib_name (artifact )]
1547
1565
return [
1548
1566
"-lstatic=%s" % get_lib_name (artifact ),
1549
1567
"-Clink-arg=-l%s" % get_lib_name (artifact ),
0 commit comments