Skip to content

Commit 7da5d18

Browse files
Merge pull request #542 from Kobzol/ci-binary-size
CI: add workflow for checking binary size
2 parents e1c49fb + ae229a6 commit 7da5d18

File tree

1 file changed

+86
-0
lines changed

1 file changed

+86
-0
lines changed
+86
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
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
6+
7+
on:
8+
pull_request:
9+
branches:
10+
- master
11+
12+
jobs:
13+
test:
14+
name: Check binary size
15+
runs-on: ubuntu-latest
16+
permissions:
17+
pull-requests: write
18+
steps:
19+
- name: Print info
20+
run: |
21+
echo "Current SHA: ${{ github.event.pull_request.head.sha }}"
22+
echo "Base SHA: ${{ github.event.pull_request.base.sha }}"
23+
- name: Clone Rustc
24+
uses: actions/checkout@v3
25+
with:
26+
repository: rust-lang/rust
27+
fetch-depth: 1
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
33+
run: |
34+
printf "[llvm]\ndownload-ci-llvm = true\n\n[rust]\nincremental = false\n" > config.toml
35+
cd library/backtrace
36+
git remote add kobzol https://github.com/kobzol/backtrace-rs
37+
git fetch --all
38+
git checkout ${{ github.event.pull_request.base.sha }}
39+
cd ../..
40+
git add library/backtrace
41+
python3 x.py build library --stage 0
42+
cp -r ./build/x86_64-unknown-linux-gnu/stage0/bin ./build/x86_64-unknown-linux-gnu/stage0-sysroot/bin
43+
cp -r ./build/x86_64-unknown-linux-gnu/stage0/lib/*.so ./build/x86_64-unknown-linux-gnu/stage0-sysroot/lib
44+
./build/x86_64-unknown-linux-gnu/stage0-sysroot/bin/rustc -O foo.rs -o binary-reference
45+
- name: Build binary with PR version of backtrace
46+
run: |
47+
cd library/backtrace
48+
git checkout ${{ github.event.pull_request.head.sha }}
49+
cd ../..
50+
git add library/backtrace
51+
rm -rf build/x86_64-unknown-linux-gnu/stage0-std
52+
python3 x.py build library --stage 0
53+
cp -r ./build/x86_64-unknown-linux-gnu/stage0/bin ./build/x86_64-unknown-linux-gnu/stage0-sysroot/bin
54+
cp -r ./build/x86_64-unknown-linux-gnu/stage0/lib/*.so ./build/x86_64-unknown-linux-gnu/stage0-sysroot/lib
55+
./build/x86_64-unknown-linux-gnu/stage0-sysroot/bin/rustc -O foo.rs -o binary-updated
56+
- name: Display binary size
57+
run: |
58+
ls -la binary-*
59+
echo "SIZE_REFERENCE=$(stat -c '%s' binary-reference)" >> "$GITHUB_ENV"
60+
echo "SIZE_UPDATED=$(stat -c '%s' binary-updated)" >> "$GITHUB_ENV"
61+
- name: Post a PR comment if the size has changed
62+
uses: actions/github-script@v6
63+
with:
64+
script: |
65+
const reference = process.env.SIZE_REFERENCE;
66+
const updated = process.env.SIZE_UPDATED;
67+
const diff = updated - reference;
68+
const plus = diff > 0 ? "+" : "";
69+
const diff_str = `${plus}${diff}B`;
70+
71+
if (diff !== 0) {
72+
// The body is created here and wrapped so "weirdly" to avoid whitespace at the start of the lines,
73+
// which is interpreted as a code block by Markdown.
74+
const body = `Below is the size of a hello-world Rust program linked with libstd with backtrace.
75+
76+
Original binary size: **${reference}B**
77+
Updated binary size: **${updated}B**
78+
Difference: **${diff_str}**`;
79+
80+
github.rest.issues.createComment({
81+
issue_number: context.issue.number,
82+
owner: context.repo.owner,
83+
repo: context.repo.repo,
84+
body
85+
})
86+
}

0 commit comments

Comments
 (0)