Skip to content

Commit fd10963

Browse files
authored
Skip adding -lstatic to libtest and libstd on Darwin (#1620)
This is one way to fix #1573, which I have a feeling can be wrong. The binaries produced with this patch contain all required symbols and are working fine on macOS.
1 parent b3314b4 commit fd10963

File tree

6 files changed

+56
-4
lines changed

6 files changed

+56
-4
lines changed

rust/private/rustc.bzl

+4-4
Original file line numberDiff line numberDiff line change
@@ -1668,7 +1668,7 @@ def _get_crate_dirname(crate):
16681668
"""
16691669
return crate.output.dirname
16701670

1671-
def _portable_link_flags(lib, use_pic, ambiguous_libs, get_lib_name, for_windows):
1671+
def _portable_link_flags(lib, use_pic, ambiguous_libs, get_lib_name, for_windows = False, for_darwin = False):
16721672
artifact = get_preferred_artifact(lib, use_pic)
16731673
if ambiguous_libs and artifact.path in ambiguous_libs:
16741674
artifact = ambiguous_libs[artifact.path]
@@ -1706,7 +1706,7 @@ def _portable_link_flags(lib, use_pic, ambiguous_libs, get_lib_name, for_windows
17061706
artifact.basename.startswith("libtest-") or artifact.basename.startswith("libstd-") or
17071707
artifact.basename.startswith("test-") or artifact.basename.startswith("std-")
17081708
):
1709-
return ["-lstatic=%s" % get_lib_name(artifact)]
1709+
return [] if for_darwin else ["-lstatic=%s" % get_lib_name(artifact)]
17101710
return [
17111711
"-lstatic=%s" % get_lib_name(artifact),
17121712
"-Clink-arg=-l%s" % (get_lib_name(artifact) if not for_windows else artifact.basename),
@@ -1738,7 +1738,7 @@ def _make_link_flags_darwin(linker_input_and_use_pic_and_ambiguous_libs):
17381738
("link-arg=-Wl,-force_load,%s" % get_preferred_artifact(lib, use_pic).path),
17391739
])
17401740
else:
1741-
ret.extend(_portable_link_flags(lib, use_pic, ambiguous_libs, get_lib_name_default, for_windows = False))
1741+
ret.extend(_portable_link_flags(lib, use_pic, ambiguous_libs, get_lib_name_default, for_darwin = True))
17421742
return ret
17431743

17441744
def _make_link_flags_default(linker_input_and_use_pic_and_ambiguous_libs):
@@ -1755,7 +1755,7 @@ def _make_link_flags_default(linker_input_and_use_pic_and_ambiguous_libs):
17551755
"link-arg=-Wl,--no-whole-archive",
17561756
])
17571757
else:
1758-
ret.extend(_portable_link_flags(lib, use_pic, ambiguous_libs, get_lib_name_default, for_windows = False))
1758+
ret.extend(_portable_link_flags(lib, use_pic, ambiguous_libs, get_lib_name_default))
17591759
return ret
17601760

17611761
def _libraries_dirnames(linker_input_and_use_pic_and_ambiguous_libs):
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
load("@rules_cc//cc:defs.bzl", "cc_library")
2+
load("@rules_rust//rust:defs.bzl", "rust_binary", "rust_library")
3+
4+
rust_library(
5+
name = "rust-lib",
6+
srcs = ["lib.rs"],
7+
edition = "2021",
8+
)
9+
10+
cc_library(
11+
name = "c-lib",
12+
srcs = ["api.c"],
13+
deps = [":rust-lib"],
14+
)
15+
16+
rust_binary(
17+
name = "app",
18+
srcs = ["main.rs"],
19+
edition = "2021",
20+
deps = [":c-lib"],
21+
)
22+
23+
sh_test(
24+
name = "test",
25+
srcs = ["test.sh"],
26+
args = ["$(location :app)"],
27+
data = [":app"],
28+
target_compatible_with = [
29+
"@platforms//os:macos",
30+
],
31+
)

test/portable_link_flags_darwin/api.c

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#include <stdio.h>
2+
3+
void lib() {
4+
printf("Hello, C lib!");
5+
}
+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
pub fn lib() {
2+
println!("Hello, Rust lib!");
3+
}
+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
fn main() {}
+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#!/bin/sh
2+
set -e
3+
4+
if otool -L $1 | grep -q libtest; then
5+
echo "error: rust_binary is dynamically linked against libtest!"
6+
exit 1
7+
fi
8+
9+
if otool -L $1 | grep -q libstd; then
10+
echo "error: rust_binary is dynamically linked against libstd!"
11+
exit 1
12+
fi

0 commit comments

Comments
 (0)