|
9 | 9 | branches:
|
10 | 10 | - master
|
11 | 11 |
|
| 12 | +# Both the "measure" and "report" jobs need to know this. |
| 13 | +env: |
| 14 | + SIZE_DATA_DIR: sizes |
| 15 | + |
| 16 | +# Responsibility is divided between two jobs "measure" and "report", so that the |
| 17 | +# job that builds (and potentnially runs) untrusted code does not have PR write |
| 18 | +# permission, and vice-versa. |
12 | 19 | jobs:
|
13 |
| - test: |
| 20 | + measure: |
14 | 21 | name: Check binary size
|
15 |
| - runs-on: ubuntu-latest |
| 22 | + strategy: |
| 23 | + matrix: |
| 24 | + platform: [ubuntu-latest, windows-latest] |
| 25 | + runs-on: ${{ matrix.platform }} |
16 | 26 | permissions:
|
17 |
| - pull-requests: write |
| 27 | + contents: read |
| 28 | + env: |
| 29 | + # This cannot be used as a context variable in the 'uses' key later. If it |
| 30 | + # changes, update those steps too. |
| 31 | + BACKTRACE_DIR: backtrace |
| 32 | + RUSTC_DIR: rustc |
| 33 | + TEST_MAIN_RS: foo.rs |
| 34 | + BASE_COMMIT: ${{ github.event.pull_request.base.sha }} |
| 35 | + HEAD_COMMIT: ${{ github.event.pull_request.head.sha }} |
| 36 | + SIZE_DATA_FILE: size-${{ strategy.job-index }}.json |
18 | 37 | steps:
|
19 | 38 | - name: Print info
|
| 39 | + shell: bash |
20 | 40 | run: |
|
21 |
| - echo "Current SHA: ${{ github.event.pull_request.head.sha }}" |
22 |
| - echo "Base SHA: ${{ github.event.pull_request.base.sha }}" |
| 41 | + echo "Current SHA: $HEAD_COMMIT" |
| 42 | + echo "Base SHA: $BASE_COMMIT" |
| 43 | + # Note: the backtrace source that's cloned here is NOT the version to be |
| 44 | + # patched in to std. It's cloned here to access the Github action for |
| 45 | + # building and measuring the test binary. |
| 46 | + - name: Clone backtrace to access Github action |
| 47 | + uses: actions/checkout@v3 |
| 48 | + with: |
| 49 | + path: ${{ env.BACKTRACE_DIR }} |
23 | 50 | - name: Clone Rustc
|
24 | 51 | uses: actions/checkout@v3
|
25 | 52 | with:
|
26 | 53 | 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 |
| 54 | + path: ${{ env.RUSTC_DIR }} |
| 55 | + - name: Set up std repository and backtrace submodule for size test |
| 56 | + shell: bash |
| 57 | + working-directory: ${{ env.RUSTC_DIR }} |
| 58 | + env: |
| 59 | + PR_SOURCE_REPO: ${{ github.event.pull_request.head.repo.full_name }} |
33 | 60 | run: |
|
34 |
| - printf "[llvm]\ndownload-ci-llvm = true\n\n[rust]\nincremental = false\n" > config.toml |
| 61 | + # Bootstrap config |
| 62 | + cat <<EOF > config.toml |
| 63 | + [llvm] |
| 64 | + download-ci-llvm = true |
| 65 | + [rust] |
| 66 | + incremental = false |
| 67 | + EOF |
| 68 | +
|
| 69 | + # Test program source |
| 70 | + cat <<EOF > $TEST_MAIN_RS |
| 71 | + fn main() { |
| 72 | + panic!(); |
| 73 | + } |
| 74 | + EOF |
| 75 | +
|
| 76 | + git submodule update --init library/backtrace |
| 77 | +
|
35 | 78 | cd library/backtrace
|
36 |
| - git remote add head-pr https://github.com/${{ github.event.pull_request.head.repo.full_name }} |
| 79 | + git remote add head-pr "https://github.com/$PR_SOURCE_REPO" |
37 | 80 | 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 |
| 81 | + - name: Build binary with base version of backtrace |
| 82 | + uses: ./backtrace/.github/actions/build-with-patched-std |
| 83 | + with: |
| 84 | + backtrace-commit: ${{ env.BASE_COMMIT }} |
| 85 | + main-rs: ${{ env.TEST_MAIN_RS }} |
| 86 | + rustc-dir: ${{ env.RUSTC_DIR }} |
| 87 | + id: size-reference |
43 | 88 | - 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" |
57 |
| - - name: Post a PR comment if the size has changed |
| 89 | + uses: ./backtrace/.github/actions/build-with-patched-std |
| 90 | + with: |
| 91 | + backtrace-commit: ${{ env.HEAD_COMMIT }} |
| 92 | + main-rs: ${{ env.TEST_MAIN_RS }} |
| 93 | + rustc-dir: ${{ env.RUSTC_DIR }} |
| 94 | + id: size-updated |
| 95 | + # There is no built-in way to "collect" all the outputs of a set of jobs |
| 96 | + # run with a matrix strategy. Subsequent jobs that have a "needs" |
| 97 | + # dependency on this one will be run once, when the last matrix job is |
| 98 | + # run. Appending data to a single file within a matrix is subject to race |
| 99 | + # conditions. So we write the size data to files with distinct names |
| 100 | + # generated from the job index. |
| 101 | + - name: Write sizes to file |
58 | 102 | uses: actions/github-script@v6
|
| 103 | + env: |
| 104 | + SIZE_REFERENCE: ${{ steps.size-reference.outputs.test-binary-size }} |
| 105 | + SIZE_UPDATED: ${{ steps.size-updated.outputs.test-binary-size }} |
| 106 | + PLATFORM: ${{ matrix.platform }} |
59 | 107 | with:
|
60 | 108 | script: |
|
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`; |
| 109 | + const fs = require("fs"); |
| 110 | + const path = require("path"); |
66 | 111 |
|
67 |
| - if (diff !== 0) { |
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. |
| 112 | + fs.mkdirSync(process.env.SIZE_DATA_DIR, {recursive: true}); |
72 | 113 |
|
73 |
| - Original binary size: **${reference}B** |
74 |
| - Updated binary size: **${updated}B** |
75 |
| - Difference: **${diff_str}** (${percent}%)`; |
| 114 | + const output_data = JSON.stringify({ |
| 115 | + platform: process.env.PLATFORM, |
| 116 | + reference: process.env.SIZE_REFERENCE, |
| 117 | + updated: process.env.SIZE_UPDATED, |
| 118 | + }); |
76 | 119 |
|
77 |
| - github.rest.issues.createComment({ |
78 |
| - issue_number: context.issue.number, |
79 |
| - owner: context.repo.owner, |
80 |
| - repo: context.repo.repo, |
81 |
| - body |
82 |
| - }) |
83 |
| - } |
| 120 | + // The "wx" flag makes this fail if the file exists, which we want, |
| 121 | + // because there should be no collisions. |
| 122 | + fs.writeFileSync( |
| 123 | + path.join(process.env.SIZE_DATA_DIR, process.env.SIZE_DATA_FILE), |
| 124 | + output_data, |
| 125 | + { flag: "wx" }, |
| 126 | + ); |
| 127 | + - name: Upload size data |
| 128 | + uses: actions/upload-artifact@v3 |
| 129 | + with: |
| 130 | + name: size-files |
| 131 | + path: ${{ env.SIZE_DATA_DIR }}/${{ env.SIZE_DATA_FILE }} |
| 132 | + retention-days: 1 |
| 133 | + if-no-files-found: error |
| 134 | + report: |
| 135 | + name: Report binary size changes |
| 136 | + runs-on: ubuntu-latest |
| 137 | + needs: measure |
| 138 | + permissions: |
| 139 | + pull-requests: write |
| 140 | + steps: |
| 141 | + # Clone backtrace to access Github composite actions to report size. |
| 142 | + - uses: actions/checkout@v3 |
| 143 | + - name: Download size data |
| 144 | + uses: actions/download-artifact@v3 |
| 145 | + with: |
| 146 | + name: size-files |
| 147 | + path: ${{ env.SIZE_DATA_DIR }} |
| 148 | + - name: Analyze and report size changes |
| 149 | + uses: ./.github/actions/report-code-size-changes |
| 150 | + with: |
| 151 | + data-directory: ${{ env.SIZE_DATA_DIR }} |
0 commit comments