Skip to content

Commit bcc5a38

Browse files
authored
Prefer clang if linker-plugin-lto specified (#1573)
* Add test-linker-plugin-lto
1 parent 89fb94c commit bcc5a38

File tree

3 files changed

+45
-6
lines changed

3 files changed

+45
-6
lines changed

.github/workflows/main.yml

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,26 @@ jobs:
217217
- name: check clean Git workting tree
218218
uses: ./.github/actions/check-clean-git-working-tree
219219

220+
test-linker-plugin-lto:
221+
name: Test linker-plugin-lto
222+
runs-on: ubuntu-latest
223+
steps:
224+
- uses: actions/checkout@v5
225+
- name: Install Rust (rustup)
226+
run: |
227+
set -euxo pipefail
228+
rustup toolchain install stable --no-self-update --profile minimal
229+
rustup default stable
230+
shell: bash
231+
- run: cargo update
232+
- uses: Swatinem/rust-cache@v2
233+
- run: cargo test --workspace --release
234+
env:
235+
RUSTCFLAGS: -C linker-plugin-lto
236+
# check that there are no uncommitted changes to prevent bugs like https://github.com/rust-lang/cc-rs/issues/1411
237+
- name: check clean Git workting tree
238+
uses: ./.github/actions/check-clean-git-working-tree
239+
220240
# This is separate from the matrix above because there is no prebuilt rust-std component for these targets.
221241
check-build-std:
222242
name: Test build-std
@@ -421,6 +441,7 @@ jobs:
421441
name: Tests pass
422442
needs:
423443
- test
444+
- test-linker-plugin-lto
424445
- check-build-std
425446
- check-wasm
426447
- test-wasi

Cargo.toml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,3 +52,8 @@ cc = { path = "." }
5252

5353
[lints.rust]
5454
unexpected_cfgs = { level = "allow", check-cfg = ['cfg(disable_clang_cl_tests)'] }
55+
56+
[profile.release]
57+
opt-level = 3 # Or "s" or "z" for different optimization goals
58+
lto = true
59+

src/lib.rs

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2808,6 +2808,14 @@ impl Build {
28082808
cmd
28092809
}
28102810

2811+
fn prefer_clang(&self) -> bool {
2812+
if let Some(env) = self.getenv("CARGO_ENCODED_RUSTFLAGS") {
2813+
env.to_string_lossy().contains("linker-plugin-lto")
2814+
} else {
2815+
false
2816+
}
2817+
}
2818+
28112819
fn get_base_compiler(&self) -> Result<Tool, Error> {
28122820
let out_dir = self.get_out_dir().ok();
28132821
let out_dir = out_dir.as_deref();
@@ -2835,14 +2843,19 @@ impl Build {
28352843
("CC", "gcc", "cc", "clang")
28362844
};
28372845

2838-
// On historical Solaris systems, "cc" may have been Sun Studio, which
2839-
// is not flag-compatible with "gcc". This history casts a long shadow,
2840-
// and many modern illumos distributions today ship GCC as "gcc" without
2841-
// also making it available as "cc".
2846+
let fallback = Cow::Borrowed(Path::new(traditional));
28422847
let default = if cfg!(target_os = "solaris") || cfg!(target_os = "illumos") {
2843-
gnu
2848+
// On historical Solaris systems, "cc" may have been Sun Studio, which
2849+
// is not flag-compatible with "gcc". This history casts a long shadow,
2850+
// and many modern illumos distributions today ship GCC as "gcc" without
2851+
// also making it available as "cc".
2852+
Cow::Borrowed(Path::new(gnu))
2853+
} else if self.prefer_clang() {
2854+
self.which(Path::new(clang), None)
2855+
.map(Cow::Owned)
2856+
.unwrap_or(fallback)
28442857
} else {
2845-
traditional
2858+
fallback
28462859
};
28472860

28482861
let cl_exe = self.find_msvc_tools_find_tool(&target, msvc);

0 commit comments

Comments
 (0)