Skip to content

Commit

Permalink
rustc: Hint to the linker about static/shared libs
Browse files Browse the repository at this point in the history
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)
  • Loading branch information
alexcrichton committed Apr 17, 2014
1 parent acdee8b commit ad3de7f
Showing 1 changed file with 16 additions and 0 deletions.
16 changes: 16 additions & 0 deletions src/librustc/back/link.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 => {
Expand All @@ -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
Expand Down

4 comments on commit ad3de7f

@bors
Copy link
Contributor

@bors bors commented on ad3de7f Apr 17, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

saw approval from brson
at alexcrichton@ad3de7f

@bors
Copy link
Contributor

@bors bors commented on ad3de7f Apr 17, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

merging alexcrichton/rust/up-llvm = ad3de7f into auto

@bors
Copy link
Contributor

@bors bors commented on ad3de7f Apr 17, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

alexcrichton/rust/up-llvm = ad3de7f merged ok, testing candidate = cd064a87

Please sign in to comment.