[solved] How to enable LTO only in final build step?

I’d like to enable link time optimization.

Unfortunately, the dependency proc_macro_hack_impl fails to build when I enable LTO via RUSTFLAGS.

I can run cargo build with -v and re-run the last rustc build/link via manual copy/paste and added -C lto=thin. This results in a significant performance improvement in the resulting binary.

But, is there a way to do this cleanly without re-running rustc manually?

The solution is to use the correct [profile] section to enable LTO. In my case I need to use [profile.bench] and everything works fine.

1 Like