on: [push, pull_request]
jobs:
  crosscompile:
    strategy:
      fail-fast: false
      matrix:
        go: [ "1.24.x", "1.25.x" ]
    runs-on: ${{ fromJSON(vars['CROSS_COMPILE_RUNNER_UBUNTU'] || '"ubuntu-latest"') }}
    name: "Cross Compilation (Go ${{matrix.go}})"
    timeout-minutes: 30
    steps:
      - uses: actions/checkout@v5
      - uses: actions/setup-go@v6
        with:
          go-version: ${{ matrix.go }}
      - name: Get Date
        id: get-date
        run: echo "date=$(/bin/date -u "+%Y%m%d")" >> $GITHUB_OUTPUT
      - name: Load Go build cache
        id: load-go-cache
        uses: actions/cache/restore@v4
        with:
          path: ~/.cache/go-build
          key: go-${{ matrix.go }}-crosscompile-${{ steps.get-date.outputs.date }}
          restore-keys: go-${{ matrix.go }}-crosscompile-
      - name: Install build utils
        run: |
          sudo apt-get update
          sudo apt-get install -y gcc-multilib
      - name: Install dependencies
        run: go build example/main.go
      - name: Run cross compilation
        # run in parallel on as many cores as are available on the machine
        run: go tool dist list | xargs -I % -P "$(nproc)" .github/workflows/cross-compile.sh %
      - name: Save Go build cache
        # only store cache when on master
        if: github.event_name == 'push' && github.ref_name == 'master'
        uses: actions/cache/save@v4
        with:
          path: ~/.cache/go-build
          # Caches are immutable, so we only update it once per day (at most).
          # See https://github.com/actions/cache/blob/main/tips-and-workarounds.md#update-a-cache
          key: go-${{ matrix.go }}-crosscompile-${{ steps.get-date.outputs.date }}
