Skip to content
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

Require static native libraries when linking static executables #55566

Closed
wants to merge 1 commit into from
Closed
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
18 changes: 14 additions & 4 deletions src/librustc_codegen_llvm/back/link.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1412,9 +1412,7 @@ fn add_upstream_rust_crates(cmd: &mut dyn Linker,
}
}

// Link in all of our upstream crates' native dependencies. Remember that
// all of these upstream native dependencies are all non-static
// dependencies. We've got two cases then:
// Link in all of our upstream crates' native dependencies. We have two cases:
//
// 1. The upstream crate is an rlib. In this case we *must* link in the
// native dependency because the rlib is just an archive.
Expand Down Expand Up @@ -1457,7 +1455,19 @@ fn add_upstream_native_libraries(cmd: &mut dyn Linker,
continue
}
match lib.kind {
NativeLibraryKind::NativeUnknown => cmd.link_dylib(&name.as_str()),
NativeLibraryKind::NativeUnknown => {
// On some targets, like Linux, linking a static executable inhibits using
// dylibs at all. Force native libraries to be static, even if for example
// an upstream rlib was originally linked against a native shared library.
if crate_type == config::CrateType::Executable
&& sess.crt_static()
&& !sess.target.target.options.crt_static_allows_dylibs
{
cmd.link_staticlib(&name.as_str())
} else {
cmd.link_dylib(&name.as_str())
}
},
NativeLibraryKind::NativeFramework => cmd.link_framework(&name.as_str()),
NativeLibraryKind::NativeStaticNobundle => {
// Link "static-nobundle" native libs only if the crate they originate from
Expand Down