From 8308b0f5ec7b0a37b5044275e8d224d2478afb60 Mon Sep 17 00:00:00 2001 From: Ralph Giles Date: Fri, 19 Feb 2016 15:03:32 -0800 Subject: [PATCH 1/3] Move native artifact notification to a helper. --- src/librustc_trans/back/link.rs | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/src/librustc_trans/back/link.rs b/src/librustc_trans/back/link.rs index 76360dcc1b972..84372a5686e70 100644 --- a/src/librustc_trans/back/link.rs +++ b/src/librustc_trans/back/link.rs @@ -822,14 +822,20 @@ fn link_staticlib(sess: &Session, objects: &[PathBuf], out_filename: &Path, ab.update_symbols(); ab.build(); - if !all_native_libs.is_empty() { - sess.note_without_error("link against the following native artifacts when linking against \ + report_link_line(sess, all_native_libs); +} + +fn report_link_line(sess: &Session, native_libs: Vec<(NativeLibraryKind, String)>) { + if !native_libs.is_empty() { + sess.note_without_error( + "link against the following native artifacts when linking against \ this static library"); - sess.note_without_error("the order and any duplication can be significant on some \ + sess.note_without_error( + "the order and any duplication can be significant on some \ platforms, and so may need to be preserved"); } - for &(kind, ref lib) in &all_native_libs { + for &(kind, ref lib) in &native_libs { let name = match kind { NativeLibraryKind::NativeStatic => "static library", NativeLibraryKind::NativeUnknown => "library", From a6a9194e09df2da40369487bf8494064c6a7795c Mon Sep 17 00:00:00 2001 From: Ralph Giles Date: Fri, 19 Feb 2016 15:09:31 -0800 Subject: [PATCH 2/3] Use an early return. If there's nothing to do, returning early saves an indentation level and reduces the amount of state one needs to track reading the code. --- src/librustc_trans/back/link.rs | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/src/librustc_trans/back/link.rs b/src/librustc_trans/back/link.rs index 84372a5686e70..da0d96edef0de 100644 --- a/src/librustc_trans/back/link.rs +++ b/src/librustc_trans/back/link.rs @@ -826,15 +826,17 @@ fn link_staticlib(sess: &Session, objects: &[PathBuf], out_filename: &Path, } fn report_link_line(sess: &Session, native_libs: Vec<(NativeLibraryKind, String)>) { - if !native_libs.is_empty() { - sess.note_without_error( - "link against the following native artifacts when linking against \ - this static library"); - sess.note_without_error( - "the order and any duplication can be significant on some \ - platforms, and so may need to be preserved"); + if native_libs.is_empty() { + return; } + sess.note_without_error( + "link against the following native artifacts when linking against \ + this static library"); + sess.note_without_error( + "the order and any duplication can be significant on some \ + platforms, and so may need to be preserved"); + for &(kind, ref lib) in &native_libs { let name = match kind { NativeLibraryKind::NativeStatic => "static library", From cdcc1a4df217d9c7bf47380585ee6c8715c82faf Mon Sep 17 00:00:00 2001 From: Ralph Giles Date: Sat, 20 Feb 2016 16:45:03 -0800 Subject: [PATCH 3/3] Add link-flags-ld emit switch. Generate an unix-style ldflags line when building with --crate-type staticlib --emit link-flags-ld. If a filename is given, write the link line there for later use by an external build system in linking the static library. If none is given, just modify the normal informative message to print the flags line instead of the library list. Issue #31471. --- src/librustc/session/config.rs | 9 ++++++-- src/librustc_trans/back/link.rs | 35 ++++++++++++++++++++++++++++++++ src/librustc_trans/back/write.rs | 2 ++ 3 files changed, 44 insertions(+), 2 deletions(-) diff --git a/src/librustc/session/config.rs b/src/librustc/session/config.rs index ea08bf021fbba..dfd7aadc11bff 100644 --- a/src/librustc/session/config.rs +++ b/src/librustc/session/config.rs @@ -69,6 +69,7 @@ pub enum OutputType { Object, Exe, DepInfo, + LinkFlagsLd, } #[derive(Clone, Copy, Debug, PartialEq, Eq)] @@ -87,7 +88,8 @@ impl OutputType { fn is_compatible_with_codegen_units_and_single_output_file(&self) -> bool { match *self { OutputType::Exe | - OutputType::DepInfo => true, + OutputType::DepInfo | + OutputType::LinkFlagsLd => true, OutputType::Bitcode | OutputType::Assembly | OutputType::LlvmAssembly | @@ -103,6 +105,7 @@ impl OutputType { OutputType::Object => "obj", OutputType::Exe => "link", OutputType::DepInfo => "dep-info", + OutputType::LinkFlagsLd => "link-flags-ld", } } } @@ -210,6 +213,7 @@ impl OutputFilenames { OutputType::LlvmAssembly => base.with_extension("ll"), OutputType::Object => base.with_extension("o"), OutputType::DepInfo => base.with_extension("d"), + OutputType::LinkFlagsLd => base.with_extension("ldflags"), OutputType::Exe => base, } } @@ -884,7 +888,7 @@ pub fn rustc_short_optgroups() -> Vec { "NAME"), opt::multi_s("", "emit", "Comma separated list of types of output for \ the compiler to emit", - "[asm|llvm-bc|llvm-ir|obj|link|dep-info]"), + "[asm|llvm-bc|llvm-ir|obj|link|link-flags-ld|dep-info]"), opt::multi_s("", "print", "Comma separated list of compiler information to \ print on stdout", "[crate-name|file-names|sysroot|target-list]"), @@ -1059,6 +1063,7 @@ pub fn build_session_options(matches: &getopts::Matches) -> Options { "llvm-bc" => OutputType::Bitcode, "obj" => OutputType::Object, "link" => OutputType::Exe, + "link-flags-ld" => OutputType::LinkFlagsLd, "dep-info" => OutputType::DepInfo, part => { early_error(error_format, &format!("unknown emission type: `{}`", diff --git a/src/librustc_trans/back/link.rs b/src/librustc_trans/back/link.rs index da0d96edef0de..f630d0948b39d 100644 --- a/src/librustc_trans/back/link.rs +++ b/src/librustc_trans/back/link.rs @@ -830,6 +830,41 @@ fn report_link_line(sess: &Session, native_libs: Vec<(NativeLibraryKind, String) return; } + // Write out link flags to a file if requested. + match sess.opts.output_types.get(&OutputType::LinkFlagsLd) { + Some(path) => { + let mut ldflags = String::new(); + for &(kind, ref lib) in &native_libs { + let prefix = match kind { + NativeLibraryKind::NativeStatic => "-l", + NativeLibraryKind::NativeUnknown => "-l", + NativeLibraryKind::NativeFramework => "-f ", + }; + ldflags.push_str(&format!(" {}{}", prefix, *lib)); + } + ldflags.push('\n'); + match *path { + Some(ref path) => { + match fs::File::create(&path).and_then(|mut f| { + f.write_all(ldflags.trim_left().as_bytes()) + }) { + Ok(..) => {} + Err(e) => sess.fatal( + &format!("failed to write {}: {}", + path.display(), e)) + } + }, + None => sess.note_without_error( + &format!("ldflags: {}", ldflags.trim())) + }; + return; + }, + None => { + // Link flag output not requested, continue. + }, + }; + + // Otherwise, warn about needed link lines in the build output. sess.note_without_error( "link against the following native artifacts when linking against \ this static library"); diff --git a/src/librustc_trans/back/write.rs b/src/librustc_trans/back/write.rs index 92d8b928ef428..70d674766673a 100644 --- a/src/librustc_trans/back/write.rs +++ b/src/librustc_trans/back/write.rs @@ -676,6 +676,7 @@ pub fn run_passes(sess: &Session, modules_config.emit_obj = true; metadata_config.emit_obj = true; }, + OutputType::LinkFlagsLd | OutputType::DepInfo => {} } } @@ -780,6 +781,7 @@ pub fn run_passes(sess: &Session, copy_if_one_unit("0.o", OutputType::Object, true); } OutputType::Exe | + OutputType::LinkFlagsLd | OutputType::DepInfo => {} } }