Skip to content

Add link-flags-ld emit option. #31875

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions src/librustc/session/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ pub enum OutputType {
Object,
Exe,
DepInfo,
LinkFlagsLd,
}

#[derive(Clone, Copy, Debug, PartialEq, Eq)]
Expand All @@ -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 |
Expand All @@ -103,6 +105,7 @@ impl OutputType {
OutputType::Object => "obj",
OutputType::Exe => "link",
OutputType::DepInfo => "dep-info",
OutputType::LinkFlagsLd => "link-flags-ld",
}
}
}
Expand Down Expand Up @@ -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,
}
}
Expand Down Expand Up @@ -884,7 +888,7 @@ pub fn rustc_short_optgroups() -> Vec<RustcOptGroup> {
"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]"),
Expand Down Expand Up @@ -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: `{}`",
Expand Down
55 changes: 49 additions & 6 deletions src/librustc_trans/back/link.rs
Original file line number Diff line number Diff line change
Expand Up @@ -822,14 +822,57 @@ 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 \
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");
report_link_line(sess, all_native_libs);
}

fn report_link_line(sess: &Session, native_libs: Vec<(NativeLibraryKind, String)>) {
if native_libs.is_empty() {
return;
}

for &(kind, ref lib) in &all_native_libs {
// 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");
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",
NativeLibraryKind::NativeUnknown => "library",
Expand Down
2 changes: 2 additions & 0 deletions src/librustc_trans/back/write.rs
Original file line number Diff line number Diff line change
Expand Up @@ -676,6 +676,7 @@ pub fn run_passes(sess: &Session,
modules_config.emit_obj = true;
metadata_config.emit_obj = true;
},
OutputType::LinkFlagsLd |
OutputType::DepInfo => {}
}
}
Expand Down Expand Up @@ -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 => {}
}
}
Expand Down