Skip to content

Commit 1906440

Browse files
committed
Be more strategic about linking to rust crates
Instead of linking directly to the rust crate, try to figure out the location and name of the library from the file name, then call gcc with appropriate -L, -l flags. This will allow dynamic linking to be more forgiving about where it loads the library from at runtime - currently a stage3 compiler can't run correctly from the stage0 directory. Only tested on Linux. Fingers crossed.
1 parent e130e7b commit 1906440

File tree

2 files changed

+28
-1
lines changed

2 files changed

+28
-1
lines changed

src/comp/driver/rustc.rs

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -419,12 +419,37 @@ fn main(vec[str] args) {
419419
}
420420
}
421421

422+
// Converts a library file name into a gcc -l argument
423+
fn unlib(@session::config config, str filename) -> str {
424+
auto rmlib = bind fn(@session::config config,
425+
str filename) -> str {
426+
if (config.os == session::os_macos
427+
|| config.os == session::os_linux
428+
&& str::find(filename, "lib") == 0) {
429+
ret str::slice(filename, 3u, str::byte_len(filename));
430+
} else {
431+
ret filename;
432+
}
433+
} (config, _);
434+
fn rmext(str filename) -> str {
435+
auto parts = str::split(filename, '.' as u8);
436+
vec::pop(parts);
437+
ret str::connect(parts, ".");
438+
}
439+
ret alt (config.os) {
440+
case (session::os_macos) { rmext(rmlib(filename)) }
441+
case (session::os_linux) { rmext(rmlib(filename)) }
442+
case (_) { rmext(filename) }
443+
};
444+
}
445+
422446
for (str cratepath in sess.get_used_crate_files()) {
423447
auto dir = fs::dirname(cratepath);
424448
if (dir != "") {
425449
gcc_args += ["-L" + dir];
426450
}
427-
gcc_args += [fs::basename(cratepath)];
451+
auto libarg = unlib(sess.get_targ_cfg(), fs::basename(cratepath));
452+
gcc_args += ["-l" + libarg];
428453
}
429454

430455
auto used_libs = sess.get_used_libraries();

src/comp/metadata/creader.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,9 @@ fn find_library_crate(&session::session sess, &ast::ident ident,
8383
// manually filtering fs::list_dir here.
8484

8585
for (str library_search_path in library_search_paths) {
86+
log #fmt("searching %s", library_search_path);
8687
for (str path in fs::list_dir(library_search_path)) {
88+
log #fmt("searching %s", path);
8789
let str f = fs::basename(path);
8890
if (!(str::starts_with(f, prefix) &&
8991
str::ends_with(f, nn.suffix))) {

0 commit comments

Comments
 (0)