Error: lto can only be run for executables, cdylibs and static library outputs

Hello all, I'm running Gentoo and thought of trying to build packages with RUSTFLAGS="-C lto=fat" (also tried thin) and have been getting this error.
I've tried to compile a few packages now. After searching online I believe this is due to trying to build an rlib with LTO? I'm not too good with rust so I don't know. Is there any way around this? Is there a way to "prefer" LTO but ignore if it's an rlib?

This is the error, during the middle of a compile.
error: lto can only be run for executables, cdylibs and static library outputs

build.rs should ignore RUSTFLAGS · Issue #6375 · rust-lang/cargo · GitHub I saw this here recommending something called profiles? In this case I don't really have easy access to what args are given to cargo, if this is the only way can I use an environment variable instead of giving cargo an argument? Otherwise this would require a solution on an eclass/ebuild level....

I'd appreciate any suggestions on how to get as most as possible built with LTO, thanks for your time.

You are probably trying to build a rust dylib with LTO. This doesn't work as LTO needs the fast majority of the symbols to be private for it to be effective I think. A rust dylib exports every public symbol of all of it's dependencies.

Is there a way to not apply LTO to dylibs?

I don't think this only happens when the package I'm trying to build is a dylib as it also did this when building Alacritty..... But I guess I'll try a few more to be sure. The other ones being the rust compiler and librsvg as of right now. I'll try a few more in the meantime.

EDIT: this is the particular crate alacritty failed with https://crates.io/crates/vte_generate_state_changes

If I understand correctly, the problem is that Rust tries to apply LTO when compiling dependencies, while it should be only applied at the last stage (since only then there's a "link-time" stage). RUSTFLAGS, on the other side, are used on every compilation step, so they're not what you want.

The thing you probably want is, as you've already noted, the Cargo profile configuration, specifically, the lto option. That is, you should try and add the following to Cargo.toml:

[profile.release]
lto = "fat"

...and then Cargo will apply necessary flags to necessary cases only.

Cargo also passes -Clto=yes when compiling rlib dependencies as this skips object file generation for rlibs, leaving just the llvm bitcode for LTO consumption.

1 Like

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.