Skip to content

Commit 2920658

Browse files
committed
--print=native-static-libs
1 parent 0314e10 commit 2920658

File tree

3 files changed

+46
-3
lines changed

3 files changed

+46
-3
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 {
@@ -1292,7 +1293,7 @@ pub fn rustc_short_optgroups() -> Vec<RustcOptGroup> {
12921293
print on stdout",
12931294
"[crate-name|file-names|sysroot|cfg|target-list|\
12941295
target-cpus|target-features|relocation-models|\
1295-
code-models|target-spec-json]"),
1296+
code-models|target-spec-json|native-static-deps]"),
12961297
opt::flagmulti_s("g", "", "Equivalent to -C debuginfo=2"),
12971298
opt::flagmulti_s("O", "", "Equivalent to -C opt-level=2"),
12981299
opt::opt_s("o", "", "Write output to <filename>", "FILENAME"),
@@ -1638,6 +1639,7 @@ pub fn build_session_options_and_crate_config(matches: &getopts::Matches)
16381639
"target-features" => PrintRequest::TargetFeatures,
16391640
"relocation-models" => PrintRequest::RelocationModels,
16401641
"code-models" => PrintRequest::CodeModels,
1642+
"native-static-libs" => PrintRequest::NativeStaticLibs,
16411643
"target-spec-json" => {
16421644
if nightly_options::is_unstable_enabled(matches) {
16431645
PrintRequest::TargetSpec

src/librustc_driver/lib.rs

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

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

src/librustc_trans/back/link.rs

+37-1
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,13 +647,20 @@ fn link_staticlib(sess: &Session,
647647
ab.build();
648648

649649
if !all_native_libs.is_empty() {
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
650654
print_native_static_libs_legacy(sess, &all_native_libs);
655+
}
651656
}
652657
}
653658

654659
fn print_native_static_libs_legacy(sess: &Session, all_native_libs: &[NativeLibrary]) {
655660
sess.note_without_error("link against the following native artifacts when linking against \
656661
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");
657664

658665
for lib in all_native_libs.iter().filter(|l| relevant_lib(sess, l)) {
659666
let name = match lib.kind {
@@ -667,6 +674,35 @@ fn print_native_static_libs_legacy(sess: &Session, all_native_libs: &[NativeLibr
667674
}
668675
}
669676

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+
670706
// Create a dynamic library or executable
671707
//
672708
// This will invoke the system linker/cc to create the resulting file. This

0 commit comments

Comments
 (0)