Skip to content

Commit 74ef87d

Browse files
committed
1 parent 867fc37 commit 74ef87d

File tree

7 files changed

+59
-0
lines changed

7 files changed

+59
-0
lines changed

rust/private/rustc.bzl

+18
Original file line numberDiff line numberDiff line change
@@ -1544,6 +1544,24 @@ def _portable_link_flags(lib, use_pic, ambiguous_libs):
15441544
# and adding references to these symlinks in the native section A.
15451545
# We rely in the behavior of -Clink-arg to put the linker args
15461546
# 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)]
15471565
return [
15481566
"-lstatic=%s" % get_lib_name(artifact),
15491567
"-Clink-arg=-l%s" % get_lib_name(artifact),

test/native_deps/BUILD.bazel

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
load("@rules_cc//cc:defs.bzl", "cc_library")
2+
load(
3+
"@rules_rust//rust:defs.bzl",
4+
"rust_library",
5+
"rust_test",
6+
)
7+
8+
rust_library(
9+
name = "transitive",
10+
srcs = ["transitive.rs"],
11+
edition = "2018",
12+
)
13+
14+
cc_library(
15+
name = "direct",
16+
srcs = ["direct.cc"],
17+
hdrs = ["direct.h"],
18+
deps = ["transitive"],
19+
)
20+
21+
rust_test(
22+
name = "main",
23+
srcs = ["main.rs"],
24+
edition = "2018",
25+
deps = ["direct"],
26+
)

test/native_deps/direct.cc

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#include "direct.h"
2+
3+
RustStruct MakeRustStruct() {
4+
RustStruct result;
5+
return result;
6+
}

test/native_deps/direct.h

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
struct RustStruct{};
2+
3+
extern "C" RustStruct MakeRustStruct();
4+

test/native_deps/main.rs

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
fn main() {
2+
println!("Done!");
3+
}

test/native_deps/transitive.rs

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
#[repr(C)]
2+
pub struct RustStruct {}

test/native_deps/user.rs

Whitespace-only changes.

0 commit comments

Comments
 (0)