1 # This workflow checks if a PR commit has changed the size of a hello world Rust program.
2 # It downloads Rustc and compiles two versions of a stage0 compiler - one using the base commit
3 # of the PR, and one using the latest commit in the PR.
4 # If the size of the hello world program has changed, it posts a comment to the PR.
5 name: Check binary size
14 name: Check binary size
15 runs-on: ubuntu-latest
21 echo "Current SHA: ${{ github.event.pull_request.head.sha }}"
22 echo "Base SHA: ${{ github.event.pull_request.base.sha }}"
24 uses: actions/checkout@v3
26 repository: rust-lang/rust
28 - name: Fetch backtrace
29 run: git submodule update --init library/backtrace
30 - name: Create hello world program that uses backtrace
31 run: printf "fn main() { panic!(); }" > foo.rs
32 - name: Build binary with base version of backtrace
34 printf "[llvm]\ndownload-ci-llvm = true\n\n[rust]\nincremental = false\n" > config.toml
36 git remote add head-pr https://github.com/${{ github.event.pull_request.head.repo.full_name }}
38 git checkout ${{ github.event.pull_request.base.sha }}
40 git add library/backtrace
41 python3 x.py build library --stage 0
42 ./build/x86_64-unknown-linux-gnu/stage0-sysroot/bin/rustc -O foo.rs -o binary-reference
43 - name: Build binary with PR version of backtrace
46 git checkout ${{ github.event.pull_request.head.sha }}
48 git add library/backtrace
49 rm -rf build/x86_64-unknown-linux-gnu/stage0-std
50 python3 x.py build library --stage 0
51 ./build/x86_64-unknown-linux-gnu/stage0-sysroot/bin/rustc -O foo.rs -o binary-updated
52 - name: Display binary size
55 echo "SIZE_REFERENCE=$(stat -c '%s' binary-reference)" >> "$GITHUB_ENV"
56 echo "SIZE_UPDATED=$(stat -c '%s' binary-updated)" >> "$GITHUB_ENV"
57 - name: Post a PR comment if the size has changed
58 uses: actions/github-script@v6
61 const reference = process.env.SIZE_REFERENCE;
62 const updated = process.env.SIZE_UPDATED;
63 const diff = updated - reference;
64 const plus = diff > 0 ? "+" : "";
65 const diff_str = `${plus}${diff}B`;
68 const percent = (((updated / reference) - 1) * 100).toFixed(2);
69 // The body is created here and wrapped so "weirdly" to avoid whitespace at the start of the lines,
70 // which is interpreted as a code block by Markdown.
71 const body = `Below is the size of a hello-world Rust program linked with libstd with backtrace.
73 Original binary size: **${reference}B**
74 Updated binary size: **${updated}B**
75 Difference: **${diff_str}** (${percent}%)`;
77 github.rest.issues.createComment({
78 issue_number: context.issue.number,
79 owner: context.repo.owner,
80 repo: context.repo.repo,