Skip to content

Commit 3751f83

Browse files
committed
Auto merge of #56397 - petrhosek:file-search, r=cramertj
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.
2 parents 1137d29 + 22b526e commit 3751f83

File tree

4 files changed

+73
-5
lines changed

4 files changed

+73
-5
lines changed

src/librustc_codegen_llvm/back/link.rs

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -462,6 +462,27 @@ fn print_native_static_libs(sess: &Session, all_native_libs: &[NativeLibrary]) {
462462
}
463463
}
464464

465+
fn get_file_path(sess: &Session, name: &str) -> PathBuf {
466+
let fs = sess.target_filesearch(PathKind::Native);
467+
let search_path = |path: &PathBuf| -> Option<PathBuf> {
468+
let file_path = path.join(name);
469+
if file_path.as_path().exists() {
470+
Some(file_path)
471+
} else {
472+
None
473+
}
474+
};
475+
let mut found_path = search_path(&fs.get_lib_path());
476+
if found_path.is_none() {
477+
fs.for_each_lib_search_path(|path, _| {
478+
if found_path.is_none() {
479+
found_path = search_path(&path.to_path_buf());
480+
}
481+
});
482+
}
483+
found_path.unwrap_or_else(|| PathBuf::from(name))
484+
}
485+
465486
// Create a dynamic library or executable
466487
//
467488
// This will invoke the system linker/cc to create the resulting file. This
@@ -477,7 +498,6 @@ fn link_natively(sess: &Session,
477498
// The invocations of cc share some flags across platforms
478499
let (pname, mut cmd) = get_linker(sess, &linker, flavor);
479500

480-
let root = sess.target_filesearch(PathKind::Native).get_lib_path();
481501
if let Some(args) = sess.target.target.options.pre_link_args.get(&flavor) {
482502
cmd.args(args);
483503
}
@@ -505,12 +525,12 @@ fn link_natively(sess: &Session,
505525
&sess.target.target.options.pre_link_objects_dll
506526
};
507527
for obj in pre_link_objects {
508-
cmd.arg(root.join(obj));
528+
cmd.arg(get_file_path(sess, obj));
509529
}
510530

511531
if crate_type == config::CrateType::Executable && sess.crt_static() {
512532
for obj in &sess.target.target.options.pre_link_objects_exe_crt {
513-
cmd.arg(root.join(obj));
533+
cmd.arg(get_file_path(sess, obj));
514534
}
515535
}
516536

@@ -534,11 +554,11 @@ fn link_natively(sess: &Session,
534554
cmd.args(args);
535555
}
536556
for obj in &sess.target.target.options.post_link_objects {
537-
cmd.arg(root.join(obj));
557+
cmd.arg(get_file_path(sess, obj));
538558
}
539559
if sess.crt_static() {
540560
for obj in &sess.target.target.options.post_link_objects_crt {
541-
cmd.arg(root.join(obj));
561+
cmd.arg(get_file_path(sess, obj));
542562
}
543563
}
544564
if let Some(args) = sess.target.target.options.post_link_args.get(&flavor) {
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
-include ../tools.mk
2+
3+
ifdef IS_MSVC
4+
5+
TARGETS =
6+
ifeq ($(filter x86,$(LLVM_COMPONENTS)),x86)
7+
TARGETS += x86_64-windows-msvc
8+
endif
9+
10+
all: $(TARGETS)
11+
12+
define MK_TARGETS
13+
$(1): empty.rs
14+
mkdir $$(TMPDIR)/obj
15+
$$(call COMPILE_OBJ,$$(TMPDIR)/obj/crt2.o,crt2.c)
16+
$$(RUSTC) --target $(1) -L$$(TMPDIR)/obj -Z print-link-args empty.rs \
17+
| $$(CGREP) $$(TMPDIR)/obj/crt2.o
18+
endef
19+
20+
$(foreach target,$(TARGETS),$(eval $(call MK_TARGETS,$(target))))
21+
22+
else
23+
24+
all:
25+
26+
endif
Lines changed: 11 additions & 0 deletions
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+
void _start() {}
Lines changed: 11 additions & 0 deletions
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)