Skip to content

Commit 9347e45

Browse files
authored
Fix rustcflags mapping: require -Clinker-plugin-lto for -flto (#1564)
Only when -Clto and -Clinker-plugin-lto are both specified, would we pass -flto, making sure that we won't break environments which has ar doesn't support llvm ir bitcode and linker that doesn't have llvm lto plugin. Fixed #1463
1 parent 9a2e7ce commit 9347e45

File tree

2 files changed

+13
-11
lines changed

2 files changed

+13
-11
lines changed

.github/workflows/main.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -386,7 +386,7 @@ jobs:
386386
- uses: actions/checkout@v5
387387
- name: Install Rust
388388
run: |
389-
rustup toolchain install stable --no-self-update --profile minimal --component rustfmt
389+
rustup toolchain install stable --no-self-update --profile minimal --component clippy
390390
rustup default stable
391391
shell: bash
392392
- uses: Swatinem/rust-cache@v2

src/flags.rs

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ pub(crate) struct RustcCodegenFlags<'a> {
2020
soft_float: Option<bool>,
2121
dwarf_version: Option<u32>,
2222
stack_protector: Option<&'a str>,
23+
linker_plugin_lto: Option<bool>,
2324
}
2425

2526
impl<'this> RustcCodegenFlags<'this> {
@@ -140,6 +141,8 @@ impl<'this> RustcCodegenFlags<'this> {
140141
"-Ccontrol-flow-guard" => self.control_flow_guard = value.or(Some("true")),
141142
// https://doc.rust-lang.org/rustc/codegen-options/index.html#lto
142143
"-Clto" => self.lto = value.or(Some("true")),
144+
// https://doc.rust-lang.org/rustc/linker-plugin-lto.html
145+
"-Clinker-plugin-lto" => self.linker_plugin_lto = Some(true),
143146
// https://doc.rust-lang.org/rustc/codegen-options/index.html#relocation-model
144147
"-Crelocation-model" => {
145148
self.relocation_model = flag_not_empty(value)?;
@@ -316,16 +319,14 @@ impl<'this> RustcCodegenFlags<'this> {
316319
push_if_supported(format!("-fembed-bitcode={cc_val}").into());
317320
}
318321

319-
// https://clang.llvm.org/docs/ClangCommandLineReference.html#cmdoption-clang-flto
320-
if let Some(value) = self.lto {
321-
let cc_val = match value {
322-
"y" | "yes" | "on" | "true" | "fat" => Some("full"),
323-
"thin" => Some("thin"),
324-
_ => None,
322+
// https://doc.rust-lang.org/rustc/linker-plugin-lto.html
323+
if self.linker_plugin_lto.unwrap_or(false) {
324+
// https://clang.llvm.org/docs/ClangCommandLineReference.html#cmdoption-clang-flto
325+
let cc_val = match self.lto {
326+
Some("thin") => "thin",
327+
_ => "full",
325328
};
326-
if let Some(cc_val) = cc_val {
327-
push_if_supported(format!("-flto={cc_val}").into());
328-
}
329+
push_if_supported(format!("-flto={cc_val}").into());
329330
}
330331
// https://clang.llvm.org/docs/ClangCommandLineReference.html#cmdoption-clang-mguard
331332
if let Some(value) = self.control_flow_guard {
@@ -513,7 +514,7 @@ mod tests {
513514
"-Clink-self-contained=yes",
514515
"-Clinker=lld",
515516
"-Clinker-flavor=ld.lld",
516-
"-Clinker-plugin-lto=yes",
517+
"-Clinker-plugin-lto=/path",
517518
"-Cllvm-args=foo",
518519
"-Cmetadata=foo",
519520
"-Cno-prepopulate-passes",
@@ -553,6 +554,7 @@ mod tests {
553554
branch_protection: Some("bti,pac-ret,leaf"),
554555
dwarf_version: Some(5),
555556
stack_protector: Some("strong"),
557+
linker_plugin_lto: Some(true),
556558
},
557559
);
558560
}

0 commit comments

Comments
 (0)