Skip to content

Commit 22d6598

Browse files
committed
Auto merge of #43067 - pornel:libdeps, r=nrc
Compact display of static lib dependencies Fixes #33173 Instead of displaying one dependency per line, I've changed the format to display them all in one line. As a bonus they're in format of linker flags (`-lfoo`), so the output can be copy&pasted if one is actually going to link as suggested.
2 parents 088216f + 2354089 commit 22d6598

File tree

3 files changed

+52
-7
lines changed

3 files changed

+52
-7
lines changed

src/librustc/session/config.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -340,6 +340,7 @@ pub enum PrintRequest {
340340
RelocationModels,
341341
CodeModels,
342342
TargetSpec,
343+
NativeStaticLibs,
343344
}
344345

345346
pub enum Input {
@@ -1296,7 +1297,7 @@ pub fn rustc_short_optgroups() -> Vec<RustcOptGroup> {
12961297
print on stdout",
12971298
"[crate-name|file-names|sysroot|cfg|target-list|\
12981299
target-cpus|target-features|relocation-models|\
1299-
code-models|target-spec-json]"),
1300+
code-models|target-spec-json|native-static-deps]"),
13001301
opt::flagmulti_s("g", "", "Equivalent to -C debuginfo=2"),
13011302
opt::flagmulti_s("O", "", "Equivalent to -C opt-level=2"),
13021303
opt::opt_s("o", "", "Write output to <filename>", "FILENAME"),
@@ -1642,6 +1643,7 @@ pub fn build_session_options_and_crate_config(matches: &getopts::Matches)
16421643
"target-features" => PrintRequest::TargetFeatures,
16431644
"relocation-models" => PrintRequest::RelocationModels,
16441645
"code-models" => PrintRequest::CodeModels,
1646+
"native-static-libs" => PrintRequest::NativeStaticLibs,
16451647
"target-spec-json" => {
16461648
if nightly_options::is_unstable_enabled(matches) {
16471649
PrintRequest::TargetSpec

src/librustc_driver/lib.rs

+6-1
Original file line numberDiff line numberDiff line change
@@ -741,7 +741,9 @@ impl RustcDefaultCalls {
741741
odir: &Option<PathBuf>,
742742
ofile: &Option<PathBuf>)
743743
-> Compilation {
744-
if sess.opts.prints.is_empty() {
744+
// PrintRequest::NativeStaticLibs is special - printed during linking
745+
// (empty iterator returns true)
746+
if sess.opts.prints.iter().all(|&p| p==PrintRequest::NativeStaticLibs) {
745747
return Compilation::Continue;
746748
}
747749

@@ -851,6 +853,9 @@ impl RustcDefaultCalls {
851853
PrintRequest::TargetCPUs | PrintRequest::TargetFeatures => {
852854
rustc_trans::print(*req, sess);
853855
}
856+
PrintRequest::NativeStaticLibs => {
857+
println!("Native static libs can be printed only during linking");
858+
}
854859
}
855860
}
856861
return Compilation::Stop;

src/librustc_trans/back/link.rs

+43-5
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ use super::linker::Linker;
1515
use super::rpath::RPathConfig;
1616
use super::rpath;
1717
use metadata::METADATA_FILENAME;
18-
use rustc::session::config::{self, NoDebugInfo, OutputFilenames, OutputType};
18+
use rustc::session::config::{self, NoDebugInfo, OutputFilenames, OutputType, PrintRequest};
1919
use rustc::session::filesearch;
2020
use rustc::session::search_paths::PathKind;
2121
use rustc::session::Session;
@@ -647,11 +647,20 @@ fn link_staticlib(sess: &Session,
647647
ab.build();
648648

649649
if !all_native_libs.is_empty() {
650-
sess.note_without_error("link against the following native artifacts when linking against \
651-
this static library");
652-
sess.note_without_error("the order and any duplication can be significant on some \
653-
platforms, and so may need to be preserved");
650+
if sess.opts.prints.contains(&PrintRequest::NativeStaticLibs) {
651+
print_native_static_libs(sess, &all_native_libs);
652+
} else {
653+
// Fallback for backwards compatibility only
654+
print_native_static_libs_legacy(sess, &all_native_libs);
655+
}
654656
}
657+
}
658+
659+
fn print_native_static_libs_legacy(sess: &Session, all_native_libs: &[NativeLibrary]) {
660+
sess.note_without_error("link against the following native artifacts when linking against \
661+
this static library");
662+
sess.note_without_error("This list will not be printed by default. \
663+
Please add --print=native-static-libs if you need this information");
655664

656665
for lib in all_native_libs.iter().filter(|l| relevant_lib(sess, l)) {
657666
let name = match lib.kind {
@@ -665,6 +674,35 @@ fn link_staticlib(sess: &Session,
665674
}
666675
}
667676

677+
fn print_native_static_libs(sess: &Session, all_native_libs: &[NativeLibrary]) {
678+
let lib_args: Vec<_> = all_native_libs.iter()
679+
.filter(|l| relevant_lib(sess, l))
680+
.filter_map(|lib| match lib.kind {
681+
NativeLibraryKind::NativeStaticNobundle |
682+
NativeLibraryKind::NativeUnknown => {
683+
if sess.target.target.options.is_like_msvc {
684+
Some(format!("{}.lib", lib.name))
685+
} else {
686+
Some(format!("-l{}", lib.name))
687+
}
688+
},
689+
NativeLibraryKind::NativeFramework => {
690+
// ld-only syntax, since there are no frameworks in MSVC
691+
Some(format!("-framework {}", lib.name))
692+
},
693+
// These are included, no need to print them
694+
NativeLibraryKind::NativeStatic => None,
695+
})
696+
.collect();
697+
if !lib_args.is_empty() {
698+
sess.note_without_error("Link against the following native artifacts when linking \
699+
against this static library. The order and any duplication \
700+
can be significant on some platforms.");
701+
// Prefix for greppability
702+
sess.note_without_error(&format!("native-static-libs: {}", &lib_args.join(" ")));
703+
}
704+
}
705+
668706
// Create a dynamic library or executable
669707
//
670708
// This will invoke the system linker/cc to create the resulting file. This

0 commit comments

Comments
 (0)