in gcc/clang lto requires you to pass
-flto to both the compiler and linker.
but when I put
lto = true in the Cargo.toml and compile with
--verbose I see
-Clto only in the last rustc invocation and not in all of the deps invocations. how does this work?
When compiling an rlib, the LLVM bitcode is included by default in the resulting rlib in addition to the object files.
-Clto is only used to chose between linking the object files and performing LTO using the bitcode.
On nightly you can use
-Cembed-bitcode=no to avoid generating LLVM bitcode for rlibs. I believe cargo will use this flag when it lands on stable if lto is disabled to save disk space and a bit of compilation time.
-Clinker-plugin-lto disables the generation of object files in the rlibs I believe. I am not sure though.
Just to expand on what bjorn3 said:
Default rlib behavior = both object code and llvm bitcode
-Clinker-plugin-lto = bitcode only (artifact will only be used with LTO)
-Cembed-bitcode=no = object code only (will never be used with LTO)
I think, usually, with a regular rlib,
-C lto would just be ignored. Cargo avoids passing it for libraries because the flag is incompatible with other things like
This has all recently changed in nightly (1.45), and the details are in https://github.com/rust-lang/cargo/blob/master/src/cargo/core/compiler/lto.rs