|
| 1 | +--- |
| 2 | +name: Cross-Platform Build |
| 3 | + |
| 4 | +on: |
| 5 | + push: |
| 6 | + branches: [main] |
| 7 | + |
| 8 | + pull_request: |
| 9 | + branches: [main] |
| 10 | + |
| 11 | + workflow_dispatch: |
| 12 | + |
| 13 | +jobs: |
| 14 | + matrix-setup: |
| 15 | + name: Setup Build Matrix |
| 16 | + runs-on: ubuntu-latest |
| 17 | + outputs: |
| 18 | + matrix: ${{ steps.set-matrix.outputs.matrix }} |
| 19 | + steps: |
| 20 | + - name: Checkout repository |
| 21 | + uses: actions/checkout@v4 |
| 22 | + |
| 23 | + - name: Generate build matrix for all supported platforms |
| 24 | + id: set-matrix |
| 25 | + run: | |
| 26 | + matrix=$(cat <<EOF |
| 27 | + { |
| 28 | + "include": [ |
| 29 | + {"target": "aarch64-apple-darwin", "os": "macos-latest", "cross": false}, |
| 30 | + {"target": "x86_64-apple-darwin", "os": "macos-latest", "cross": false}, |
| 31 | + {"target": "aarch64-unknown-linux-gnu", "os": "ubuntu-latest", "cross": true}, |
| 32 | + {"target": "aarch64-unknown-linux-musl", "os": "ubuntu-latest", "cross": true}, |
| 33 | + {"target": "armv7-unknown-linux-gnueabihf", "os": "ubuntu-latest", "cross": true}, |
| 34 | + {"target": "armv7-unknown-linux-musleabihf", "os": "ubuntu-latest", "cross": true}, |
| 35 | + {"target": "x86_64-pc-windows-gnu", "os": "ubuntu-latest", "cross": true}, |
| 36 | + {"target": "x86_64-pc-windows-msvc", "os": "windows-latest", "cross": false}, |
| 37 | + {"target": "x86_64-unknown-linux-gnu", "os": "ubuntu-latest", "cross": false}, |
| 38 | + {"target": "x86_64-unknown-linux-musl", "os": "ubuntu-latest", "cross": true} |
| 39 | + ] |
| 40 | + } |
| 41 | + EOF |
| 42 | + ) |
| 43 | + echo "matrix=$(echo $matrix | jq -c .)" >> $GITHUB_OUTPUT |
| 44 | +
|
| 45 | + cross-build: |
| 46 | + name: Build ${{ matrix.target }} |
| 47 | + needs: matrix-setup |
| 48 | + strategy: |
| 49 | + fail-fast: false |
| 50 | + matrix: ${{ fromJson(needs.matrix-setup.outputs.matrix) }} |
| 51 | + runs-on: ${{ matrix.os }} |
| 52 | + steps: |
| 53 | + - name: Checkout repository |
| 54 | + uses: actions/checkout@v4 |
| 55 | + |
| 56 | + - name: Install Rust toolchain for ${{ matrix.target }} |
| 57 | + uses: dtolnay/rust-toolchain@v1 |
| 58 | + with: |
| 59 | + toolchain: stable |
| 60 | + targets: ${{ matrix.target }} |
| 61 | + |
| 62 | + # Ensure target is installed (workaround for cross-compilation on macOS) |
| 63 | + - name: Ensure target is installed |
| 64 | + run: rustup target add ${{ matrix.target }} |
| 65 | + |
| 66 | + - name: Cache cross-compilation dependencies |
| 67 | + uses: Swatinem/rust-cache@v2 |
| 68 | + with: |
| 69 | + shared-key: "cross-${{ matrix.target }}-${{ hashFiles('**/Cargo.lock') }}" |
| 70 | + cache-on-failure: true |
| 71 | + cache-all-crates: true |
| 72 | + save-if: ${{ github.ref == 'refs/heads/main' }} |
| 73 | + |
| 74 | + # Use cross for Linux cross-compilation |
| 75 | + - name: Install cross for cross-compilation |
| 76 | + if: matrix.cross && matrix.os == 'ubuntu-latest' |
| 77 | + run: | |
| 78 | + cargo install cross --git https://github.com/cross-rs/cross |
| 79 | +
|
| 80 | + # Build with cross |
| 81 | + - name: Build with cross for ${{ matrix.target }} |
| 82 | + if: matrix.cross |
| 83 | + run: | |
| 84 | + cross build --target ${{ matrix.target }} --release --bin recast-cli |
| 85 | +
|
| 86 | + # Build natively |
| 87 | + - name: Build natively for ${{ matrix.target }} |
| 88 | + if: '!matrix.cross' |
| 89 | + run: | |
| 90 | + cargo build --target ${{ matrix.target }} --release --bin recast-cli |
| 91 | +
|
| 92 | + # Test build artifacts exist |
| 93 | + - name: Verify build artifacts exist |
| 94 | + shell: bash |
| 95 | + run: | |
| 96 | + if [[ "${{ matrix.os }}" == "windows-latest" ]]; then |
| 97 | + ls -la target/${{ matrix.target }}/release/*.exe || true |
| 98 | + else |
| 99 | + ls -la target/${{ matrix.target }}/release/recast-cli || true |
| 100 | + fi |
| 101 | +
|
| 102 | + # Upload artifacts |
| 103 | + - name: Upload cross-compilation artifacts for ${{ matrix.target }} |
| 104 | + uses: actions/upload-artifact@v4 |
| 105 | + with: |
| 106 | + name: build-${{ matrix.target }} |
| 107 | + path: | |
| 108 | + target/${{ matrix.target }}/release/recast-cli* |
| 109 | + if-no-files-found: error |
0 commit comments