name: Release
on:
  push:
    tags: ["v*"]

jobs:
  build:
    strategy:
      matrix:
        include:
          - target: x86_64-unknown-linux-gnu
            os: ubuntu-latest
          - target: aarch64-unknown-linux-gnu
            os: ubuntu-latest
            cross: true
          - target: x86_64-apple-darwin
            os: macos-latest
          - target: aarch64-apple-darwin
            os: macos-latest
    runs-on: ${{ matrix.os }}
    steps:
      - uses: actions/checkout@v4
      - uses: dtolnay/rust-toolchain@stable
        with:
          targets: ${{ matrix.target }}
      - name: Install cross-compilation tools
        if: matrix.cross
        run: sudo apt-get update && sudo apt-get install -y gcc-aarch64-linux-gnu
      - name: Set cross-linker
        if: matrix.cross
        run: |
          mkdir -p .cargo
          echo '[target.aarch64-unknown-linux-gnu]' >> .cargo/config.toml
          echo 'linker = "aarch64-linux-gnu-gcc"' >> .cargo/config.toml
      - run: cargo build --release --features jj --target ${{ matrix.target }}
      - name: Rename binary with target triple
        run: |
          mkdir -p dist
          cp target/${{ matrix.target }}/release/lx dist/lx-${{ matrix.target }}
      - uses: actions/upload-artifact@v4
        with:
          name: lx-${{ matrix.target }}
          path: dist/lx-${{ matrix.target }}

  release:
    needs: build
    runs-on: ubuntu-latest
    permissions:
      contents: write
    steps:
      - uses: actions/download-artifact@v4
        with:
          merge-multiple: true
      - uses: softprops/action-gh-release@v2
        with:
          files: lx-*
          generate_release_notes: true
