Skip to content

Commit f90c21a

Browse files
committed
When given rustc -C codegen-units=4 -o output --emit=obj, reset units back to 1.
Fix #30063 Note: while this code is careful to handle the case of mutliple emit types (e.g. `--emit=asm,obj`) by reporting all the emit types that conflict with codegen units in its warnings, an invocation with multiple emit types *and* `-o PATH` will continue to ignore the requested target path (with a warning), as it already does today, since the code that checks for that is further downstream.
1 parent 77ed39c commit f90c21a

File tree

1 file changed

+46
-1
lines changed

1 file changed

+46
-1
lines changed

src/librustc/session/config.rs

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,30 @@ pub enum OutputType {
7171
DepInfo,
7272
}
7373

74+
impl OutputType {
75+
fn is_compatible_with_codegen_units_and_single_output_file(&self) -> bool {
76+
match *self {
77+
OutputType::Exe |
78+
OutputType::DepInfo => true,
79+
OutputType::Bitcode |
80+
OutputType::Assembly |
81+
OutputType::LlvmAssembly |
82+
OutputType::Object => false,
83+
}
84+
}
85+
86+
fn shorthand(&self) -> &'static str {
87+
match *self {
88+
OutputType::Bitcode => "llvm-bc",
89+
OutputType::Assembly => "asm",
90+
OutputType::LlvmAssembly => "llvm-ir",
91+
OutputType::Object => "obj",
92+
OutputType::Exe => "link",
93+
OutputType::DepInfo => "dep-info",
94+
}
95+
}
96+
}
97+
7498
#[derive(Clone)]
7599
pub struct Options {
76100
// The crate config requested for the session, which may be combined
@@ -933,7 +957,28 @@ pub fn build_session_options(matches: &getopts::Matches) -> Options {
933957
output_types.insert(OutputType::Exe, None);
934958
}
935959

936-
let cg = build_codegen_options(matches, color);
960+
let mut cg = build_codegen_options(matches, color);
961+
962+
// Issue #30063: if user requests llvm-related output to one
963+
// particular path, disable codegen-units.
964+
if matches.opt_present("o") && cg.codegen_units != 1 {
965+
let incompatible: Vec<_> = output_types.iter()
966+
.map(|ot_path| ot_path.0)
967+
.filter(|ot| {
968+
!ot.is_compatible_with_codegen_units_and_single_output_file()
969+
}).collect();
970+
if !incompatible.is_empty() {
971+
for ot in &incompatible {
972+
early_warn(color, &format!("--emit={} with -o incompatible with \
973+
-C codegen-units=N for N > 1",
974+
ot.shorthand()));
975+
}
976+
early_warn(color, "resetting to default -C codegen-units=1");
977+
cg.codegen_units = 1;
978+
}
979+
}
980+
981+
let cg = cg;
937982

938983
let sysroot_opt = matches.opt_str("sysroot").map(|m| PathBuf::from(&m));
939984
let target = matches.opt_str("target").unwrap_or(

0 commit comments

Comments
 (0)