Skip to content

Commit 6d9640b

Browse files
committed
Search other library paths when loking for link objects
Support the case when link objects are not located in Rust sysroot but in other locations which could be specify through library paths.
1 parent bcf920f commit 6d9640b

File tree

3 files changed

+46
-5
lines changed

3 files changed

+46
-5
lines changed

Diff for: src/librustc_codegen_llvm/back/link.rs

+19-5
Original file line numberDiff line numberDiff line change
@@ -457,6 +457,21 @@ fn print_native_static_libs(sess: &Session, all_native_libs: &[NativeLibrary]) {
457457
}
458458
}
459459

460+
fn get_file_path(sess: &Session, name: &str) -> PathBuf {
461+
let fs = sess.target_filesearch(PathKind::Native);
462+
let file_path = fs.get_lib_path().join(name);
463+
if file_path.exists() {
464+
return file_path
465+
}
466+
for search_path in fs.search_paths() {
467+
let file_path = search_path.dir.join(name);
468+
if file_path.exists() {
469+
return file_path
470+
}
471+
}
472+
PathBuf::from(name)
473+
}
474+
460475
// Create a dynamic library or executable
461476
//
462477
// This will invoke the system linker/cc to create the resulting file. This
@@ -472,7 +487,6 @@ fn link_natively(sess: &Session,
472487
// The invocations of cc share some flags across platforms
473488
let (pname, mut cmd) = get_linker(sess, &linker, flavor);
474489

475-
let root = sess.target_filesearch(PathKind::Native).get_lib_path();
476490
if let Some(args) = sess.target.target.options.pre_link_args.get(&flavor) {
477491
cmd.args(args);
478492
}
@@ -500,12 +514,12 @@ fn link_natively(sess: &Session,
500514
&sess.target.target.options.pre_link_objects_dll
501515
};
502516
for obj in pre_link_objects {
503-
cmd.arg(root.join(obj));
517+
cmd.arg(get_file_path(sess, obj));
504518
}
505519

506520
if crate_type == config::CrateType::Executable && sess.crt_static() {
507521
for obj in &sess.target.target.options.pre_link_objects_exe_crt {
508-
cmd.arg(root.join(obj));
522+
cmd.arg(get_file_path(sess, obj));
509523
}
510524
}
511525

@@ -529,11 +543,11 @@ fn link_natively(sess: &Session,
529543
cmd.args(args);
530544
}
531545
for obj in &sess.target.target.options.post_link_objects {
532-
cmd.arg(root.join(obj));
546+
cmd.arg(get_file_path(sess, obj));
533547
}
534548
if sess.crt_static() {
535549
for obj in &sess.target.target.options.post_link_objects_crt {
536-
cmd.arg(root.join(obj));
550+
cmd.arg(get_file_path(sess, obj));
537551
}
538552
}
539553
if let Some(args) = sess.target.target.options.post_link_args.get(&flavor) {

Diff for: src/test/run-make-fulldeps/libs-search-path/Makefile

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
-include ../tools.mk
2+
3+
ifeq ($(if $(IS_WINDOWS),$(IS_MSVC),no),)
4+
5+
all: empty.rs
6+
cp -r $(shell cygpath -u $(shell $(RUSTC) --print sysroot)) $(TMPDIR)/sysroot
7+
$(RUSTC) --target $(TARGET) --sysroot $(TMPDIR)/sysroot -L$(TMPDIR)/obj -Z print-link-args empty.rs | $(CGREP) 'lib\\crt2.o'
8+
mkdir -p $(TMPDIR)/obj
9+
mv $(TMPDIR)/sysroot/lib/rustlib/$(TARGET)/lib/crt2.o $(TMPDIR)/obj/crt2.o
10+
$(RUSTC) --target $(TARGET) --sysroot $(TMPDIR)/sysroot -L$(TMPDIR)/obj -Z print-link-args empty.rs | $(CGREP) 'obj\\crt2.o'
11+
12+
else
13+
14+
all:
15+
16+
endif

Diff for: src/test/run-make-fulldeps/libs-search-path/empty.rs

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// Copyright 2018 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
fn main() {}

0 commit comments

Comments
 (0)