name: Build Releases
on:
  push:
    tags:
      - v*

env:
  CARGO_TERM_COLOR: always

jobs:
  build-cross:
    runs-on: ubuntu-latest
    env:
      RUST_BACKTRACE: full
    strategy:
      matrix:
        target:
          - i686-unknown-linux-musl
          - x86_64-pc-windows-gnu
          - x86_64-unknown-linux-gnu
          - x86_64-unknown-linux-musl
          - arm-unknown-linux-gnueabi
          - arm-unknown-linux-gnueabihf
          - arm-unknown-linux-musleabi
          - arm-unknown-linux-musleabihf
          - aarch64-unknown-linux-gnu
          - aarch64-unknown-linux-musl
          - mips-unknown-linux-musl
          - mips-unknown-linux-gnu
          - mipsel-unknown-linux-musl

    steps:
      - uses: actions/checkout@v2

      - name: Install cross
        run: cargo install cross

      - name: Build ${{ matrix.target }}
        timeout-minutes: 120
        run: |
          cd build
          ./build-release -t ${{ matrix.target }}

      - name: Upload Github Assets
        uses: softprops/action-gh-release@v1
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        with:
          files: build/release/*
          prerelease: ${{ contains(github.ref, '-') }}

  build-unix:
    runs-on: ${{ matrix.os }}
    env:
      BUILD_EXTRA_FEATURES: "local-redir"
      RUST_BACKTRACE: full
    strategy:
      matrix:
        # os: [ubuntu-latest, macos-latest]
        os: [macos-latest]
    steps:
      - uses: actions/checkout@v2

      - name: Install GNU tar
        if: runner.os == 'macOS'
        run: |
          brew install gnu-tar
          # echo "::add-path::/usr/local/opt/gnu-tar/libexec/gnubin"
          echo "/usr/local/opt/gnu-tar/libexec/gnubin" >> $GITHUB_PATH

      - name: Install Rust nightly
        uses: actions-rs/toolchain@v1
        with:
          profile: minimal

      - name: Build release
        shell: bash
        run: |
          cd build
          ./build-host-release

      - name: Upload Github Assets
        uses: softprops/action-gh-release@v1
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        with:
          files: build/release/*
          prerelease: ${{ contains(github.ref, '-') }}

  build-windows:
    runs-on: windows-latest
    env:
      RUSTFLAGS: "-Ctarget-feature=+crt-static"
      RUST_BACKTRACE: full
    steps:
      - uses: actions/checkout@v2

      - name: Install Rust nightly
        uses: actions-rs/toolchain@v1
        with:
          profile: minimal

      - name: Build release
        run: |
          cd build
          pwsh build-host-release.ps1

      - name: Upload Github Assets
        uses: softprops/action-gh-release@v1
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        with:
          files: build/release/*
          prerelease: ${{ contains(github.ref, '-') }}
