Skip to content

Commit

Permalink
Modularise CI workflow and validate outputs for binary size checks.
Browse files Browse the repository at this point in the history
  • Loading branch information
detly committed Jul 17, 2023
1 parent 89b2ccd commit e400b73
Show file tree
Hide file tree
Showing 2 changed files with 109 additions and 27 deletions.
45 changes: 45 additions & 0 deletions .github/actions/build-with-patched-std/action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
# Github composite action to build a single-source-file test binary with an
# already-checked-out version of Rust's stdlib, that will be patched with a
# given revision of the backtrace crate.

name: Build with patched std
description: >
Build a binary with a version of std that's had a specific revision of
backtrace patched in.
inputs:
backtrace-commit:
description: The git commit of backtrace to patch in to std
required: true
main-rs:
description: The (single) source code file to compile
required: true
rustc-dir:
description: The root directory of the rustc repo
required: true
outputs:
test-binary-size:
description: The size in bytes of the built test binary
value: ${{ steps.measure.outputs.test-binary-size }}
runs:
using: "composite"
steps:
- shell: bash
id: measure
env:
RUSTC_FLAGS: -Copt-level=s -Cstrip=symbols
RUSTC_BUILD_DIR: build/x86_64-unknown-linux-gnu
working-directory: ${{ inputs.rustc-dir }}
run: |
rm -rf "$RUSTC_BUILD_DIR/stage0-std"
(cd library/backtrace && git checkout ${{ inputs.backtrace-commit }})
git add library/backtrace
python3 x.py build library --stage 0
TEMP_BUILD_OUTPUT=$(mktemp test-binary-XXXXXXXX)
"$RUSTC_BUILD_DIR/stage0-sysroot/bin/rustc" $RUSTC_FLAGS "${{ inputs.main-rs }}" -o "$TEMP_BUILD_OUTPUT"
BINARY_SIZE=$(stat -c '%s' "$TEMP_BUILD_OUTPUT")
rm "$TEMP_BUILD_OUTPUT"
echo "test-binary-size=$BINARY_SIZE" >> "$GITHUB_OUTPUT"
91 changes: 64 additions & 27 deletions .github/workflows/check-binary-size.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,51 +15,88 @@ jobs:
runs-on: ubuntu-latest
permissions:
pull-requests: write
env:
# This cannot be used as a context variable in the 'uses' key later. If it
# changes, update those steps too.
BACKTRACE_DIR: backtrace
RUSTC_DIR: rustc
TEST_MAIN_RS: foo.rs
BASE_COMMIT: ${{ github.event.pull_request.base.sha }}
HEAD_COMMIT: ${{ github.event.pull_request.head.sha }}
steps:
- name: Print info
run: |
echo "Current SHA: ${{ github.event.pull_request.head.sha }}"
echo "Base SHA: ${{ github.event.pull_request.base.sha }}"
echo "Current SHA: $HEAD_COMMIT"
echo "Base SHA: $BASE_COMMIT"
# Note: the backtrace source that's cloned here is NOT the version to be
# patched in to std. It's cloned here to access the Github action for
# building the test binary and measuring its size.
- name: Clone backtrace to access Github action
uses: actions/checkout@v3
with:
path: ${{ env.BACKTRACE_DIR }}
- name: Clone Rustc
uses: actions/checkout@v3
with:
repository: rust-lang/rust
fetch-depth: 1
- name: Fetch backtrace
run: git submodule update --init library/backtrace
- name: Create hello world program that uses backtrace
run: printf "fn main() { panic!(); }" > foo.rs
- name: Build binary with base version of backtrace
path: ${{ env.RUSTC_DIR }}
- name: Set up std repository and backtrace submodule for size test
working-directory: ${{ env.RUSTC_DIR }}
run: |
printf "[llvm]\ndownload-ci-llvm = true\n\n[rust]\nincremental = false\n" > config.toml
# Bootstrap config
cat <<EOF > config.toml
[llvm]
download-ci-llvm = true
[rust]
incremental = false
EOF
# Test program source
cat <<EOF > $TEST_MAIN_RS
fn main() {
panic!();
}
EOF
git submodule update --init library/backtrace
cd library/backtrace
git remote add head-pr https://github.com/${{ github.event.pull_request.head.repo.full_name }}
git fetch --all
git checkout ${{ github.event.pull_request.base.sha }}
cd ../..
git add library/backtrace
python3 x.py build library --stage 0
./build/x86_64-unknown-linux-gnu/stage0-sysroot/bin/rustc -O foo.rs -o binary-reference
- name: Build binary with base version of backtrace
uses: ./backtrace/.github/actions/build-with-patched-std
with:
backtrace-commit: $BASE_COMMIT
main-rs: ${{ env.TEST_MAIN_RS }}
rustc-dir: ${{ env.RUSTC_DIR }}
id: size-reference
- name: Build binary with PR version of backtrace
run: |
cd library/backtrace
git checkout ${{ github.event.pull_request.head.sha }}
cd ../..
git add library/backtrace
rm -rf build/x86_64-unknown-linux-gnu/stage0-std
python3 x.py build library --stage 0
./build/x86_64-unknown-linux-gnu/stage0-sysroot/bin/rustc -O foo.rs -o binary-updated
- name: Display binary size
run: |
ls -la binary-*
echo "SIZE_REFERENCE=$(stat -c '%s' binary-reference)" >> "$GITHUB_ENV"
echo "SIZE_UPDATED=$(stat -c '%s' binary-updated)" >> "$GITHUB_ENV"
uses: ./backtrace/.github/actions/build-with-patched-std
with:
backtrace-commit: $HEAD_COMMIT
main-rs: ${{ env.TEST_MAIN_RS }}
rustc-dir: ${{ env.RUSTC_DIR }}
id: size-updated
- name: Post a PR comment if the size has changed
uses: actions/github-script@v6
env:
SIZE_REFERENCE: ${{ steps.size-reference.outputs.test-binary-size }}
SIZE_UPDATED: ${{ steps.size-updated.outputs.test-binary-size }}
with:
script: |
const reference = process.env.SIZE_REFERENCE;
const updated = process.env.SIZE_UPDATED;
if (!(reference > 0)) {
core.setFailed(`Reference size invalid: ${reference}`);
return;
}
if (!(updated > 0)) {
core.setFailed(`Updated size invalid: ${updated}`);
return;
}
const diff = updated - reference;
const plus = diff > 0 ? "+" : "";
const diff_str = `${plus}${diff}B`;
Expand Down

0 comments on commit e400b73

Please sign in to comment.