about summary refs log tree commit diff
path: root/src
diff options
context:
space:
mode:
authorMazdak Farrokhzad <twingoow@gmail.com>2019-03-29 02:40:49 +0100
committerGitHub <noreply@github.com>2019-03-29 02:40:49 +0100
commit3df97f9da8e4a23772f5845bc641adae03e032be (patch)
tree0f8e70dbf49cec5550bda9b668f4be04b58a4371 /src
parentf9262afa4d1a88715ff57907bd17eda4d039cea6 (diff)
parent7d365cf27f4249fc9b61ba8abfc813abe43f1cb7 (diff)
downloadrust-3df97f9da8e4a23772f5845bc641adae03e032be.tar.gz
rust-3df97f9da8e4a23772f5845bc641adae03e032be.zip
Rollup merge of #59401 - japaric:compiler-builtins-stack-sizes, r=alexcrichton
bootstrap: build crates under libtest with -Z emit-stack-sizes

Please see the comment in the diff for the rationale.

This change adds a `.stack_sizes` linker section to `libcompiler_builtins.rlib`
but this section is discarded by the linker by default so it won't affect the
binary size of most programs. It will, however, negatively affect the binary
size of programs that link to a recent release of the `cortex-m-rt` crate
because of the linker script that crate provides, but I have proposed a PR
(rust-embedded/cortex-m-rt#186) to solve the problem (which I originally
introduced :-)).

This change does increase the size of the `libcompiler_builtins.rlib` artifact we
distribute but the increase is in the order of (a few) KBs.

r? @alexcrichton
Diffstat (limited to 'src')
-rw-r--r--src/bootstrap/bin/rustc.rs27
-rw-r--r--src/bootstrap/compile.rs4
2 files changed, 31 insertions, 0 deletions
diff --git a/src/bootstrap/bin/rustc.rs b/src/bootstrap/bin/rustc.rs
index ca86aeb8100..28c8a75a13a 100644
--- a/src/bootstrap/bin/rustc.rs
+++ b/src/bootstrap/bin/rustc.rs
@@ -181,6 +181,33 @@ fn main() {
             cmd.arg("-C").arg(format!("debug-assertions={}", debug_assertions));
         }
 
+        // Build all crates in the `std` facade with `-Z emit-stack-sizes` to add stack usage
+        // information.
+        //
+        // When you use this `-Z` flag with Cargo you get stack usage information on all crates
+        // compiled from source, and when you are using LTO you also get information on pre-compiled
+        // crates like `core` and `std`, even if they were not compiled with `-Z emit-stack-sizes`.
+        // However, there's an exception: `compiler_builtins`. This crate is special and doesn't
+        // participate in LTO because it's always linked as a separate object file. For this reason
+        // it's impossible to get stack usage information about `compiler-builtins` using
+        // `RUSTFLAGS` + Cargo, or `cargo rustc`.
+        //
+        // To make the stack usage information of all crates under the `std` facade available to
+        // Cargo based stack usage analysis tools, in both LTO and non-LTO mode, we compile them
+        // with the `-Z emit-stack-sizes` flag. The `RUSTC_EMIT_STACK_SIZES` var helps us apply this
+        // flag only to the crates in the `std` facade. The `-Z` flag is known to currently work
+        // with targets that produce ELF files so we limit its use flag to those targets.
+        //
+        // NOTE(japaric) if this ever causes problem with an LLVM upgrade or any PR feel free to
+        // remove it or comment it out
+        if env::var_os("RUSTC_EMIT_STACK_SIZES").is_some()
+            && (target.contains("-linux-")
+                || target.contains("-none-eabi")
+                || target.ends_with("-none-elf"))
+        {
+            cmd.arg("-Zemit-stack-sizes");
+        }
+
         if let Ok(s) = env::var("RUSTC_CODEGEN_UNITS") {
             cmd.arg("-C").arg(format!("codegen-units={}", s));
         }
diff --git a/src/bootstrap/compile.rs b/src/bootstrap/compile.rs
index 9498dbb5952..0d51d7c5ef3 100644
--- a/src/bootstrap/compile.rs
+++ b/src/bootstrap/compile.rs
@@ -97,6 +97,8 @@ impl Step for Std {
         let _folder = builder.fold_output(|| format!("stage{}-std", compiler.stage));
         builder.info(&format!("Building stage{} std artifacts ({} -> {})", compiler.stage,
                 &compiler.host, target));
+        // compile with `-Z emit-stack-sizes`; see bootstrap/src/rustc.rs for more details
+        cargo.env("RUSTC_EMIT_STACK_SIZES", "1");
         run_cargo(builder,
                   &mut cargo,
                   &libstd_stamp(builder, compiler, target),
@@ -382,6 +384,8 @@ impl Step for Test {
         let _folder = builder.fold_output(|| format!("stage{}-test", compiler.stage));
         builder.info(&format!("Building stage{} test artifacts ({} -> {})", compiler.stage,
                 &compiler.host, target));
+        // compile with `-Z emit-stack-sizes`; see bootstrap/src/rustc.rs for more details
+        cargo.env("RUSTC_EMIT_STACK_SIZES", "1");
         run_cargo(builder,
                   &mut cargo,
                   &libtest_stamp(builder, compiler, target),