Skip to content

Commit 9809ee0

Browse files
committed
Print the native static libs as in the other --print
Fixes #61089
1 parent 9503ea1 commit 9809ee0

File tree

3 files changed

+82
-15
lines changed

3 files changed

+82
-15
lines changed

Cargo.lock

+1
Original file line numberDiff line numberDiff line change
@@ -3713,6 +3713,7 @@ dependencies = [
37133713
"libc",
37143714
"rustc_ast",
37153715
"rustc_ast_pretty",
3716+
"rustc_attr",
37163717
"rustc_codegen_ssa",
37173718
"rustc_data_structures",
37183719
"rustc_error_codes",

compiler/rustc_driver/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ rustc_error_codes = { path = "../rustc_error_codes" }
3333
rustc_interface = { path = "../rustc_interface" }
3434
rustc_serialize = { path = "../rustc_serialize" }
3535
rustc_ast = { path = "../rustc_ast" }
36+
rustc_attr = { path = "../rustc_attr" }
3637
rustc_span = { path = "../rustc_span" }
3738

3839
[target.'cfg(windows)'.dependencies]

compiler/rustc_driver/src/lib.rs

+80-15
Original file line numberDiff line numberDiff line change
@@ -263,11 +263,12 @@ fn run_compiler(
263263
}
264264
let should_stop = RustcDefaultCalls::print_crate_info(
265265
&***compiler.codegen_backend(),
266-
compiler.session(),
266+
&compiler,
267267
None,
268268
&odir,
269269
&ofile,
270-
);
270+
)
271+
.unwrap_or(Compilation::Continue);
271272

272273
if should_stop == Compilation::Stop {
273274
return;
@@ -319,11 +320,11 @@ fn run_compiler(
319320
let sess = compiler.session();
320321
let should_stop = RustcDefaultCalls::print_crate_info(
321322
&***compiler.codegen_backend(),
322-
sess,
323+
compiler,
323324
Some(compiler.input()),
324325
compiler.output_dir(),
325326
compiler.output_file(),
326-
)
327+
)?
327328
.and_then(|| {
328329
RustcDefaultCalls::list_metadata(
329330
sess,
@@ -593,7 +594,11 @@ fn handle_explain(registry: Registry, code: &str, output: ErrorOutputType) {
593594

594595
fn show_content_with_pager(content: &str) {
595596
let pager_name = env::var_os("PAGER").unwrap_or_else(|| {
596-
if cfg!(windows) { OsString::from("more.com") } else { OsString::from("less") }
597+
if cfg!(windows) {
598+
OsString::from("more.com")
599+
} else {
600+
OsString::from("less")
601+
}
597602
});
598603

599604
let mut fallback_to_println = false;
@@ -622,6 +627,60 @@ fn show_content_with_pager(content: &str) {
622627
}
623628
}
624629

630+
fn relevant_lib(sess: &Session, lib: &NativeLib) -> bool {
631+
match lib.cfg {
632+
Some(ref cfg) => rustc_attr::cfg_matches(cfg, &sess.parse_sess, None),
633+
None => true,
634+
}
635+
}
636+
637+
use rustc_codegen_ssa::{CrateInfo, NativeLib};
638+
use rustc_session::utils::NativeLibKind;
639+
640+
fn print_native_static_libs(compiler: &interface::Compiler) -> interface::Result<()> {
641+
let sess = compiler.session();
642+
compiler.enter(|queries| {
643+
use rustc_codegen_ssa::back::link::each_linked_rlib;
644+
let mut all_native_libs = vec![];
645+
queries.parse()?;
646+
queries.global_ctxt()?.take().enter(|tcx| {
647+
let crate_info = CrateInfo::new(tcx);
648+
let res = each_linked_rlib(&crate_info, &mut |cnum, _path| {
649+
all_native_libs.extend(crate_info.native_libraries[&cnum].iter().cloned());
650+
});
651+
if let Err(e) = res {
652+
sess.fatal(&e);
653+
}
654+
let lib_args: Vec<_> = all_native_libs
655+
.iter()
656+
.filter(|l| relevant_lib(sess, l))
657+
.filter_map(|lib| {
658+
let name = lib.name?;
659+
match lib.kind {
660+
NativeLibKind::StaticNoBundle
661+
| NativeLibKind::Dylib
662+
| NativeLibKind::Unspecified => {
663+
if sess.target.is_like_msvc {
664+
Some(format!("{}.lib", name))
665+
} else {
666+
Some(format!("-l{}", name))
667+
}
668+
}
669+
NativeLibKind::Framework => {
670+
// ld-only syntax, since there are no frameworks in MSVC
671+
Some(format!("-framework {}", name))
672+
}
673+
// These are included, no need to print them
674+
NativeLibKind::StaticBundle | NativeLibKind::RawDylib => None,
675+
}
676+
})
677+
.collect();
678+
println!("native-static-libs: {}", &lib_args.join(" "));
679+
Ok(())
680+
})
681+
})
682+
}
683+
625684
impl RustcDefaultCalls {
626685
fn process_rlink(sess: &Session, compiler: &interface::Compiler) -> Result<(), ErrorReported> {
627686
if let Input::File(file) = compiler.input() {
@@ -679,16 +738,16 @@ impl RustcDefaultCalls {
679738

680739
fn print_crate_info(
681740
codegen_backend: &dyn CodegenBackend,
682-
sess: &Session,
741+
compiler: &interface::Compiler,
683742
input: Option<&Input>,
684743
odir: &Option<PathBuf>,
685744
ofile: &Option<PathBuf>,
686-
) -> Compilation {
745+
) -> interface::Result<Compilation> {
746+
let sess = compiler.session();
747+
687748
use rustc_session::config::PrintRequest::*;
688-
// PrintRequest::NativeStaticLibs is special - printed during linking
689-
// (empty iterator returns true)
690-
if sess.opts.prints.iter().all(|&p| p == PrintRequest::NativeStaticLibs) {
691-
return Compilation::Continue;
749+
if sess.opts.prints.is_empty() {
750+
return Ok(Compilation::Continue);
692751
}
693752

694753
let attrs = match input {
@@ -699,7 +758,7 @@ impl RustcDefaultCalls {
699758
Ok(attrs) => Some(attrs),
700759
Err(mut parse_error) => {
701760
parse_error.emit();
702-
return Compilation::Stop;
761+
return Ok(Compilation::Stop);
703762
}
704763
}
705764
}
@@ -776,10 +835,12 @@ impl RustcDefaultCalls {
776835
codegen_backend.print(*req, sess);
777836
}
778837
// Any output here interferes with Cargo's parsing of other printed output
779-
PrintRequest::NativeStaticLibs => {}
838+
PrintRequest::NativeStaticLibs => {
839+
print_native_static_libs(compiler)?;
840+
}
780841
}
781842
}
782-
Compilation::Stop
843+
Ok(Compilation::Stop)
783844
}
784845
}
785846

@@ -1147,7 +1208,11 @@ fn extra_compiler_flags() -> Option<(Vec<String>, bool)> {
11471208
}
11481209
}
11491210

1150-
if !result.is_empty() { Some((result, excluded_cargo_defaults)) } else { None }
1211+
if !result.is_empty() {
1212+
Some((result, excluded_cargo_defaults))
1213+
} else {
1214+
None
1215+
}
11511216
}
11521217

11531218
/// Runs a closure and catches unwinds triggered by fatal errors.

0 commit comments

Comments
 (0)