name: binary-releases

on:
  push:
    branches: [master]
  workflow_dispatch:

jobs:
  create-release:
    runs-on: ubuntu-latest
    outputs:
      upload_url: ${{ steps.create-new-release.outputs.upload_url }}
    steps:
      - name: Create GitHub release
        id: create-new-release
        uses: actions/create-release@v1
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        with:
          tag_name: ${{ github.run_number }}
          release_name: Release ${{ github.run_number }}

  linux-build:
    # The type of runner that the job will run on
    runs-on: ubuntu-latest
    needs: create-release

    # Steps represent a sequence of tasks that will be executed as part of the job
    steps:
      # Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it
      - uses: actions/checkout@v2

      - name: Opam modules cache
        uses: actions/cache@v1
        env:
          cache-name: cache-opam-modules
        with:
          # OCaml cache files are stored in `~/.opam` on Linux/macOS
          path: ~/.opam
          key: ${{ runner.os }}-build-${{ env.cache-name }}-${{ hashFiles('mlang.opam', 'Makefile') }}
          restore-keys: |
            ${{ runner.os }}-build-${{ env.cache-name }}-
            ${{ runner.os }}-build-
            ${{ runner.os }}-

      # TODO : Cache gmp and mpfr builds

      - name: Set up OCaml
        uses: ocaml/setup-ocaml@v2
        with:
          # Version of the OCaml compiler to initialise
          ocaml-compiler: 4.11.2

      - name: Install dependencies
        run: |
          sudo apt update
          sudo apt install m4 perl python3 clang git build-essential lzip libgmp-dev libmpfr-dev
          eval $(opam env)
          opam update
          make init-without-switch

      - name: Make mlang binary
        run: |
          eval $(opam env)
          make build-static

      - name: Upload release asset
        uses: actions/upload-release-asset@v1
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        with:
          upload_url: ${{ needs.create-release.outputs.upload_url }}
          asset_path: ./_build/default/src/main.exe
          asset_name: mlang-linux-v${{ github.run_number }}.exe
          asset_content_type: application/octet-stream
