Skip to content

Skip adding -lstatic to libtest and libstd on Darwin #1620

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
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
8 changes: 4 additions & 4 deletions rust/private/rustc.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -1668,7 +1668,7 @@ def _get_crate_dirname(crate):
"""
return crate.output.dirname

def _portable_link_flags(lib, use_pic, ambiguous_libs, get_lib_name, for_windows):
def _portable_link_flags(lib, use_pic, ambiguous_libs, get_lib_name, for_windows = False, for_darwin = False):
artifact = get_preferred_artifact(lib, use_pic)
if ambiguous_libs and artifact.path in ambiguous_libs:
artifact = ambiguous_libs[artifact.path]
Expand Down Expand Up @@ -1706,7 +1706,7 @@ def _portable_link_flags(lib, use_pic, ambiguous_libs, get_lib_name, for_windows
artifact.basename.startswith("libtest-") or artifact.basename.startswith("libstd-") or
artifact.basename.startswith("test-") or artifact.basename.startswith("std-")
):
return ["-lstatic=%s" % get_lib_name(artifact)]
return [] if for_darwin else ["-lstatic=%s" % get_lib_name(artifact)]
return [
"-lstatic=%s" % get_lib_name(artifact),
"-Clink-arg=-l%s" % (get_lib_name(artifact) if not for_windows else artifact.basename),
Expand Down Expand Up @@ -1738,7 +1738,7 @@ def _make_link_flags_darwin(linker_input_and_use_pic_and_ambiguous_libs):
("link-arg=-Wl,-force_load,%s" % get_preferred_artifact(lib, use_pic).path),
])
else:
ret.extend(_portable_link_flags(lib, use_pic, ambiguous_libs, get_lib_name_default, for_windows = False))
ret.extend(_portable_link_flags(lib, use_pic, ambiguous_libs, get_lib_name_default, for_darwin = True))
return ret

def _make_link_flags_default(linker_input_and_use_pic_and_ambiguous_libs):
Expand All @@ -1755,7 +1755,7 @@ def _make_link_flags_default(linker_input_and_use_pic_and_ambiguous_libs):
"link-arg=-Wl,--no-whole-archive",
])
else:
ret.extend(_portable_link_flags(lib, use_pic, ambiguous_libs, get_lib_name_default, for_windows = False))
ret.extend(_portable_link_flags(lib, use_pic, ambiguous_libs, get_lib_name_default))
return ret

def _libraries_dirnames(linker_input_and_use_pic_and_ambiguous_libs):
Expand Down
31 changes: 31 additions & 0 deletions test/portable_link_flags_darwin/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
load("@rules_cc//cc:defs.bzl", "cc_library")
load("@rules_rust//rust:defs.bzl", "rust_binary", "rust_library")

rust_library(
name = "rust-lib",
srcs = ["lib.rs"],
edition = "2021",
)

cc_library(
name = "c-lib",
srcs = ["api.c"],
deps = [":rust-lib"],
)

rust_binary(
name = "app",
srcs = ["main.rs"],
edition = "2021",
deps = [":c-lib"],
)

sh_test(
name = "test",
srcs = ["test.sh"],
args = ["$(location :app)"],
data = [":app"],
target_compatible_with = [
"@platforms//os:macos",
],
)
5 changes: 5 additions & 0 deletions test/portable_link_flags_darwin/api.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#include <stdio.h>

void lib() {
printf("Hello, C lib!");
}
3 changes: 3 additions & 0 deletions test/portable_link_flags_darwin/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
pub fn lib() {
println!("Hello, Rust lib!");
}
1 change: 1 addition & 0 deletions test/portable_link_flags_darwin/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
fn main() {}
12 changes: 12 additions & 0 deletions test/portable_link_flags_darwin/test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#!/bin/sh
set -e

if otool -L $1 | grep -q libtest; then
echo "error: rust_binary is dynamically linked against libtest!"
exit 1
fi

if otool -L $1 | grep -q libstd; then
echo "error: rust_binary is dynamically linked against libstd!"
exit 1
fi