rustc: Hint to the linker about static/shared libs

If a linker finds both a static and a dynamic version of the same library, then
the linker often chooses the dynamic version. This is surprising when a native
library is specified as being "static" in rust source. This modifies the linker
command line to obey the hints given in rust source files and instructing the
linker to prefer a particular version of a found library.

Unfortunately, this patch has no effect on osx because the linker supports
no such hint, and it also has no effect on windows because the linker apparently
just ignores it. For now this is predominately used to enable the previous patch
of linking to libstdc++ statically, but more support would need to be added for
this in the future if we wanted to officially support it.

cc #12557 (doesn't close because it doesn't support OSX and windows)
This commit is contained in:
Alex Crichton 2014-04-16 16:31:45 -07:00
parent acdee8b904
commit ad3de7fdb5

View File

@ -1298,9 +1298,22 @@ fn add_local_native_libraries(args: &mut Vec<~str>, sess: &Session) {
args.push("-L" + path.as_str().unwrap().to_owned());
}
// Some platforms take hints about whether a library is static or dynamic.
// For those that support this, we ensure we pass the option if the library
// was flagged "static" (most defaults are dynamic) to ensure that if
// libfoo.a and libfoo.so both exist that the right one is chosen.
let takes_hints = sess.targ_cfg.os != abi::OsMacos;
for &(ref l, kind) in sess.cstore.get_used_libraries().borrow().iter() {
match kind {
cstore::NativeUnknown | cstore::NativeStatic => {
if takes_hints {
if kind == cstore::NativeStatic {
args.push("-Wl,-Bstatic".to_owned());
} else {
args.push("-Wl,-Bdynamic".to_owned());
}
}
args.push("-l" + *l);
}
cstore::NativeFramework => {
@ -1309,6 +1322,9 @@ fn add_local_native_libraries(args: &mut Vec<~str>, sess: &Session) {
}
}
}
if takes_hints {
args.push("-Wl,-Bdynamic".to_owned());
}
}
// # Rust Crate linking