Skip to content

Commit ebfc57e

Browse files
committed
Modularise CI workflow and validate outputs for binary size checks.
1 parent a390aa7 commit ebfc57e

File tree

2 files changed

+117
-28
lines changed

2 files changed

+117
-28
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=3 -Cstrip=symbols
30+
# This symlink is made by Build::new() in the bootstrap crate, using a
31+
# symlink on Linux and a junction on Windows, so it will exist on both
32+
# platforms.
33+
RUSTC_BUILD_DIR: build/host
34+
working-directory: ${{ inputs.rustc-dir }}
35+
run: |
36+
rm -rf "$RUSTC_BUILD_DIR/stage0-std"
37+
38+
(cd library/backtrace && git checkout ${{ inputs.backtrace-commit }})
39+
git add library/backtrace
40+
41+
python3 x.py build library --stage 0
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: 69 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -15,51 +15,92 @@ 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
28+
shell: bash
2029
run: |
21-
echo "Current SHA: ${{ github.event.pull_request.head.sha }}"
22-
echo "Base SHA: ${{ github.event.pull_request.base.sha }}"
30+
echo "Current SHA: $HEAD_COMMIT"
31+
echo "Base SHA: $BASE_COMMIT"
32+
# Note: the backtrace source that's cloned here is NOT the version to be
33+
# patched in to std. It's cloned here to access the Github action for
34+
# building the test binary and measuring its size.
35+
- name: Clone backtrace to access Github action
36+
uses: actions/checkout@v3
37+
with:
38+
path: ${{ env.BACKTRACE_DIR }}
2339
- name: Clone Rustc
2440
uses: actions/checkout@v3
2541
with:
2642
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
43+
path: ${{ env.RUSTC_DIR }}
44+
- name: Set up std repository and backtrace submodule for size test
45+
shell: bash
46+
working-directory: ${{ env.RUSTC_DIR }}
47+
env:
48+
PR_SOURCE_REPO: ${{ github.event.pull_request.head.repo.full_name }}
3349
run: |
34-
printf "[llvm]\ndownload-ci-llvm = true\n\n[rust]\nincremental = false\n" > config.toml
50+
# Bootstrap config
51+
cat <<EOF > config.toml
52+
[llvm]
53+
download-ci-llvm = true
54+
[rust]
55+
incremental = false
56+
EOF
57+
58+
# Test program source
59+
cat <<EOF > $TEST_MAIN_RS
60+
fn main() {
61+
panic!();
62+
}
63+
EOF
64+
65+
git submodule update --init library/backtrace
66+
3567
cd library/backtrace
36-
git remote add head-pr https://github.com/${{ github.event.pull_request.head.repo.full_name }}
68+
git remote add head-pr "https://github.com/$PR_SOURCE_REPO"
3769
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-
./build/x86_64-unknown-linux-gnu/stage0-sysroot/bin/rustc -O foo.rs -o binary-reference
70+
- name: Build binary with base version of backtrace
71+
uses: ./backtrace/.github/actions/build-with-patched-std
72+
with:
73+
backtrace-commit: ${{ env.BASE_COMMIT }}
74+
main-rs: ${{ env.TEST_MAIN_RS }}
75+
rustc-dir: ${{ env.RUSTC_DIR }}
76+
id: size-reference
4377
- name: Build binary with PR version of backtrace
44-
run: |
45-
cd library/backtrace
46-
git checkout ${{ github.event.pull_request.head.sha }}
47-
cd ../..
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
53-
run: |
54-
ls -la binary-*
55-
echo "SIZE_REFERENCE=$(stat -c '%s' binary-reference)" >> "$GITHUB_ENV"
56-
echo "SIZE_UPDATED=$(stat -c '%s' binary-updated)" >> "$GITHUB_ENV"
78+
uses: ./backtrace/.github/actions/build-with-patched-std
79+
with:
80+
backtrace-commit: ${{ env.HEAD_COMMIT }}
81+
main-rs: ${{ env.TEST_MAIN_RS }}
82+
rustc-dir: ${{ env.RUSTC_DIR }}
83+
id: size-updated
5784
- name: Post a PR comment if the size has changed
5885
uses: actions/github-script@v6
86+
env:
87+
SIZE_REFERENCE: ${{ steps.size-reference.outputs.test-binary-size }}
88+
SIZE_UPDATED: ${{ steps.size-updated.outputs.test-binary-size }}
5989
with:
6090
script: |
6191
const reference = process.env.SIZE_REFERENCE;
6292
const updated = process.env.SIZE_UPDATED;
93+
94+
if (!(reference > 0)) {
95+
core.setFailed(`Reference size invalid: ${reference}`);
96+
return;
97+
}
98+
99+
if (!(updated > 0)) {
100+
core.setFailed(`Updated size invalid: ${updated}`);
101+
return;
102+
}
103+
63104
const diff = updated - reference;
64105
const plus = diff > 0 ? "+" : "";
65106
const diff_str = `${plus}${diff}B`;

0 commit comments

Comments
 (0)