@@ -181,6 +181,30 @@ fn main() {
181181 cmd. arg ( "-C" ) . arg ( format ! ( "debug-assertions={}" , debug_assertions) ) ;
182182 }
183183
184+ // Build `compiler_builtins` with `-Z emit-stack-sizes` to add stack usage information.
185+ //
186+ // When you use this flag with Cargo you get stack usage information on all crates compiled
187+ // from source, and when you are using LTO you also get information on pre-compiled crates
188+ // like `core` and `std`. However, there's an exception: `compiler_builtins`. This crate
189+ // is special and doesn't participate in LTO because it's always linked as a separate object
190+ // file. Due to this it's impossible to get information about this crate using `RUSTFLAGS`
191+ // + Cargo, or `cargo rustc`.
192+ //
193+ // To make the stack usage information of this crate available to Cargo based stack usage
194+ // analysis tools we compile `compiler_builtins` with the `-Z emit-stack-sizes` flag. The
195+ // flag is known to currently work with targets that produce ELF files so we limit the use
196+ // of the flag to those targets.
197+ //
198+ // NOTE(japaric) if this ever causes problem with an LLVM upgrade or any PR feel free to
199+ // remove it or comment it out
200+ if crate_name == "compiler_builtins"
201+ && ( target. contains ( "-linux-" )
202+ || target. contains ( "-none-eabi" )
203+ || target. ends_with ( "-none-elf" ) )
204+ {
205+ cmd. arg ( "-Z" ) . arg ( "emit-stack-sizes" ) ;
206+ }
207+
184208 if let Ok ( s) = env:: var ( "RUSTC_CODEGEN_UNITS" ) {
185209 cmd. arg ( "-C" ) . arg ( format ! ( "codegen-units={}" , s) ) ;
186210 }
0 commit comments