Skip to content

Commit f466a2f

Browse files
committed
rustc: -L also indicates the location of native libraries
-L currently specifies paths to search for Rust crates Building crates that use native libraries is difficult. When the library is located somewhere unexpected there is no way to tell rustc additional paths to look in. If libclang is located at `.` then rustc is not going to know that and linking will fail. To get around that I often end up inserting #[link_args = "-L."] native mod m { } into other crates to get them to build. Now you just `rustc -L .` and it builds. This doesn't do any rpathing so it's still up to somebody else to put the library somewhere it will be found or use LD_LIBRARY_PATH This feature comes with a single, XFAILed test, because I could not think of a way to test it. Odd.
1 parent 903cb0e commit f466a2f

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed

src/rustc/back/link.rs

+16
Original file line numberDiff line numberDiff line change
@@ -580,6 +580,8 @@ fn link_binary(sess: session,
580580
lib_cmd = "-dynamiclib";
581581
} else { lib_cmd = "-shared"; }
582582

583+
// # Crate linking
584+
583585
let cstore = sess.cstore;
584586
for cstore::get_used_crate_files(cstore).each {|cratepath|
585587
if str::ends_with(cratepath, ".rlib") {
@@ -596,6 +598,18 @@ fn link_binary(sess: session,
596598
let ula = cstore::get_used_link_args(cstore);
597599
for ula.each {|arg| cc_args += [arg]; }
598600

601+
// # Native library linking
602+
603+
// User-supplied library search paths (-L on the cammand line) These are
604+
// the same paths used to find Rust crates, so some of them may have been
605+
// added already by the previous crate linking code. This only allows them
606+
// to be found at compile time so it is still entirely up to outside
607+
// forces to make sure that library can be found at runtime.
608+
609+
let addl_paths = sess.opts.addl_lib_search_paths;
610+
for addl_paths.each {|path| cc_args += ["-L" + path]; }
611+
612+
// The names of the native libraries
599613
let used_libs = cstore::get_used_libraries(cstore);
600614
for used_libs.each {|l| cc_args += ["-l" + l]; }
601615

@@ -639,6 +653,8 @@ fn link_binary(sess: session,
639653
// Stack growth requires statically linking a __morestack function
640654
cc_args += ["-lmorestack"];
641655

656+
// FIXME: At some point we want to rpath our guesses as to where
657+
// native libraries might live, based on the addl_lib_search_paths
642658
cc_args += rpath::get_rpath_flags(sess, output);
643659

644660
#debug("%s link args: %s", cc_prog, str::connect(cc_args, " "));

src/test/run-pass/native-lib-path.rs

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// xfail-test FIXME I don't know how to test this
2+
// compile-flags:-L.
3+
// The -L flag is also used for linking native libraries
4+
5+
// FIXME: I want to name a mod that would not link successfully
6+
// wouthout providing a -L argument to the compiler, and that
7+
// will also be found successfully at runtime.
8+
native mod WHATGOESHERE {
9+
fn IDONTKNOW() -> u32;
10+
}
11+
12+
fn main() {
13+
assert IDONTKNOW() == 0x_BAD_DOOD_u32;
14+
}

0 commit comments

Comments
 (0)