Skip to content

Commit cdcc1a4

Browse files
committed
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.
1 parent a6a9194 commit cdcc1a4

File tree

3 files changed

+44
-2
lines changed

3 files changed

+44
-2
lines changed

src/librustc/session/config.rs

+7-2
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ pub enum OutputType {
6969
Object,
7070
Exe,
7171
DepInfo,
72+
LinkFlagsLd,
7273
}
7374

7475
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
@@ -87,7 +88,8 @@ impl OutputType {
8788
fn is_compatible_with_codegen_units_and_single_output_file(&self) -> bool {
8889
match *self {
8990
OutputType::Exe |
90-
OutputType::DepInfo => true,
91+
OutputType::DepInfo |
92+
OutputType::LinkFlagsLd => true,
9193
OutputType::Bitcode |
9294
OutputType::Assembly |
9395
OutputType::LlvmAssembly |
@@ -103,6 +105,7 @@ impl OutputType {
103105
OutputType::Object => "obj",
104106
OutputType::Exe => "link",
105107
OutputType::DepInfo => "dep-info",
108+
OutputType::LinkFlagsLd => "link-flags-ld",
106109
}
107110
}
108111
}
@@ -210,6 +213,7 @@ impl OutputFilenames {
210213
OutputType::LlvmAssembly => base.with_extension("ll"),
211214
OutputType::Object => base.with_extension("o"),
212215
OutputType::DepInfo => base.with_extension("d"),
216+
OutputType::LinkFlagsLd => base.with_extension("ldflags"),
213217
OutputType::Exe => base,
214218
}
215219
}
@@ -884,7 +888,7 @@ pub fn rustc_short_optgroups() -> Vec<RustcOptGroup> {
884888
"NAME"),
885889
opt::multi_s("", "emit", "Comma separated list of types of output for \
886890
the compiler to emit",
887-
"[asm|llvm-bc|llvm-ir|obj|link|dep-info]"),
891+
"[asm|llvm-bc|llvm-ir|obj|link|link-flags-ld|dep-info]"),
888892
opt::multi_s("", "print", "Comma separated list of compiler information to \
889893
print on stdout",
890894
"[crate-name|file-names|sysroot|target-list]"),
@@ -1059,6 +1063,7 @@ pub fn build_session_options(matches: &getopts::Matches) -> Options {
10591063
"llvm-bc" => OutputType::Bitcode,
10601064
"obj" => OutputType::Object,
10611065
"link" => OutputType::Exe,
1066+
"link-flags-ld" => OutputType::LinkFlagsLd,
10621067
"dep-info" => OutputType::DepInfo,
10631068
part => {
10641069
early_error(error_format, &format!("unknown emission type: `{}`",

src/librustc_trans/back/link.rs

+35
Original file line numberDiff line numberDiff line change
@@ -830,6 +830,41 @@ fn report_link_line(sess: &Session, native_libs: Vec<(NativeLibraryKind, String)
830830
return;
831831
}
832832

833+
// Write out link flags to a file if requested.
834+
match sess.opts.output_types.get(&OutputType::LinkFlagsLd) {
835+
Some(path) => {
836+
let mut ldflags = String::new();
837+
for &(kind, ref lib) in &native_libs {
838+
let prefix = match kind {
839+
NativeLibraryKind::NativeStatic => "-l",
840+
NativeLibraryKind::NativeUnknown => "-l",
841+
NativeLibraryKind::NativeFramework => "-f ",
842+
};
843+
ldflags.push_str(&format!(" {}{}", prefix, *lib));
844+
}
845+
ldflags.push('\n');
846+
match *path {
847+
Some(ref path) => {
848+
match fs::File::create(&path).and_then(|mut f| {
849+
f.write_all(ldflags.trim_left().as_bytes())
850+
}) {
851+
Ok(..) => {}
852+
Err(e) => sess.fatal(
853+
&format!("failed to write {}: {}",
854+
path.display(), e))
855+
}
856+
},
857+
None => sess.note_without_error(
858+
&format!("ldflags: {}", ldflags.trim()))
859+
};
860+
return;
861+
},
862+
None => {
863+
// Link flag output not requested, continue.
864+
},
865+
};
866+
867+
// Otherwise, warn about needed link lines in the build output.
833868
sess.note_without_error(
834869
"link against the following native artifacts when linking against \
835870
this static library");

src/librustc_trans/back/write.rs

+2
Original file line numberDiff line numberDiff line change
@@ -676,6 +676,7 @@ pub fn run_passes(sess: &Session,
676676
modules_config.emit_obj = true;
677677
metadata_config.emit_obj = true;
678678
},
679+
OutputType::LinkFlagsLd |
679680
OutputType::DepInfo => {}
680681
}
681682
}
@@ -780,6 +781,7 @@ pub fn run_passes(sess: &Session,
780781
copy_if_one_unit("0.o", OutputType::Object, true);
781782
}
782783
OutputType::Exe |
784+
OutputType::LinkFlagsLd |
783785
OutputType::DepInfo => {}
784786
}
785787
}

0 commit comments

Comments
 (0)