Skip to content

Commit 25c8274

Browse files
committed
Modularise CI workflow and validate outputs for binary size checks.
1 parent f3af31c commit 25c8274

File tree

2 files changed

+112
-31
lines changed

2 files changed

+112
-31
lines changed
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
# Github composite action to build a single-source-file test binary with an
2+
# already-checked-out version of Rust's stdlib, that will be patched with a
3+
# given revision of the backtrace crate.
4+
5+
name: Build with patched std
6+
description: >
7+
Build a binary with a version of std that's had a specific revision of
8+
backtrace patched in.
9+
inputs:
10+
backtrace-commit:
11+
description: The git commit of backtrace to patch in to std
12+
required: true
13+
main-rs:
14+
description: The (single) source code file to compile
15+
required: true
16+
rustc-dir:
17+
description: The root directory of the rustc repo
18+
required: true
19+
outputs:
20+
test-binary-size:
21+
description: The size in bytes of the built test binary
22+
value: ${{ steps.measure.outputs.test-binary-size }}
23+
runs:
24+
using: "composite"
25+
steps:
26+
- shell: bash
27+
id: measure
28+
env:
29+
RUSTC_FLAGS: -Copt-level=s -Cstrip=symbols
30+
RUSTC_BUILD_DIR: build/x86_64-unknown-linux-gnu
31+
working-directory: ${{ inputs.rustc-dir }}
32+
run: |
33+
rm -rf "$RUSTC_BUILD_DIR/stage0-std"
34+
35+
(cd library/backtrace && git checkout ${{ inputs.backtrace-commit }})
36+
git add library/backtrace
37+
38+
python3 x.py build library --stage 0
39+
40+
cp -r "$RUSTC_BUILD_DIR/stage0/bin" "$RUSTC_BUILD_DIR/stage0-sysroot/bin"
41+
cp -r $RUSTC_BUILD_DIR/stage0/lib/*.so "$RUSTC_BUILD_DIR/stage0-sysroot/lib"
42+
43+
TEMP_BUILD_OUTPUT=$(mktemp test-binary-XXXXXXXX)
44+
"$RUSTC_BUILD_DIR/stage0-sysroot/bin/rustc" $RUSTC_FLAGS "${{ inputs.main-rs }}" -o "$TEMP_BUILD_OUTPUT"
45+
BINARY_SIZE=$(stat -c '%s' "$TEMP_BUILD_OUTPUT")
46+
rm "$TEMP_BUILD_OUTPUT"
47+
48+
echo "test-binary-size=$BINARY_SIZE" >> "$GITHUB_OUTPUT"

.github/workflows/check-binary-size.yml

Lines changed: 64 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -15,55 +15,88 @@ jobs:
1515
runs-on: ubuntu-latest
1616
permissions:
1717
pull-requests: write
18+
env:
19+
# This cannot be used as a context variable in the 'uses' key later. If it
20+
# changes, update those steps too.
21+
BACKTRACE_DIR: backtrace
22+
RUSTC_DIR: rustc
23+
TEST_MAIN_RS: foo.rs
24+
BASE_COMMIT: ${{ github.event.pull_request.base.sha }}
25+
HEAD_COMMIT: ${{ github.event.pull_request.head.sha }}
1826
steps:
1927
- name: Print info
2028
run: |
21-
echo "Current SHA: ${{ github.event.pull_request.head.sha }}"
22-
echo "Base SHA: ${{ github.event.pull_request.base.sha }}"
29+
echo "Current SHA: $HEAD_COMMIT"
30+
echo "Base SHA: $BASE_COMMIT"
31+
# Note: the backtrace source that's cloned here is NOT the version to be
32+
# patched in to std. It's cloned here to access the Github action for
33+
# building the test binary and measuring its size.
34+
- name: Clone backtrace to access Github action
35+
uses: actions/checkout@v3
36+
with:
37+
path: ${{ env.BACKTRACE_DIR }}
2338
- name: Clone Rustc
2439
uses: actions/checkout@v3
2540
with:
2641
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
42+
path: ${{ env.RUSTC_DIR }}
43+
- name: Set up std repository and backtrace submodule for size test
44+
working-directory: ${{ env.RUSTC_DIR }}
3345
run: |
34-
printf "[llvm]\ndownload-ci-llvm = true\n\n[rust]\nincremental = false\n" > config.toml
46+
# Bootstrap config
47+
cat <<EOF > config.toml
48+
[llvm]
49+
download-ci-llvm = true
50+
[rust]
51+
incremental = false
52+
EOF
53+
54+
# Test program source
55+
cat <<EOF > $TEST_MAIN_RS
56+
fn main() {
57+
panic!();
58+
}
59+
EOF
60+
61+
git submodule update --init library/backtrace
62+
3563
cd library/backtrace
3664
git remote add head-pr https://github.com/${{ github.event.pull_request.head.repo.full_name }}
3765
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
66+
- name: Build binary with base version of backtrace
67+
uses: ./backtrace/.github/actions/build-with-patched-std
68+
with:
69+
backtrace-commit: $BASE_COMMIT
70+
main-rs: ${{ env.TEST_MAIN_RS }}
71+
rustc-dir: ${{ env.RUSTC_DIR }}
72+
id: size-reference
4573
- 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"
74+
uses: ./backtrace/.github/actions/build-with-patched-std
75+
with:
76+
backtrace-commit: $HEAD_COMMIT
77+
main-rs: ${{ env.TEST_MAIN_RS }}
78+
rustc-dir: ${{ env.RUSTC_DIR }}
79+
id: size-updated
6180
- name: Post a PR comment if the size has changed
6281
uses: actions/github-script@v6
82+
env:
83+
SIZE_REFERENCE: ${{ steps.size-reference.outputs.test-binary-size }}
84+
SIZE_UPDATED: ${{ steps.size-updated.outputs.test-binary-size }}
6385
with:
6486
script: |
6587
const reference = process.env.SIZE_REFERENCE;
6688
const updated = process.env.SIZE_UPDATED;
89+
90+
if (!(reference > 0)) {
91+
core.setFailed(`Reference size invalid: ${reference}`);
92+
return;
93+
}
94+
95+
if (!(updated > 0)) {
96+
core.setFailed(`Updated size invalid: ${updated}`);
97+
return;
98+
}
99+
67100
const diff = updated - reference;
68101
const plus = diff > 0 ? "+" : "";
69102
const diff_str = `${plus}${diff}B`;

0 commit comments

Comments
 (0)