name: Build (cabal)

on:
  push:
    branches:
    - master
    - ci-*
    - release*
    paths: &trigger_path_list
    - '.github/workflows/cabal.yml'
    - 'Agda.cabal'
    - 'Setup.hs'
    - 'src/agda-mode/**'
    - 'src/full/**'
    - 'src/main/**'
    - 'test/**.hs'
  pull_request:
    paths: *trigger_path_list

defaults:
  run:
    shell: bash

jobs:
  auto-cancel:
    if: |
      !contains(github.event.head_commit.message, '[skip ci]')
      && !contains(github.event.head_commit.message, '[ci skip]')
      && !contains(github.event.head_commit.message, '[github skip]')
      && !contains(github.event.head_commit.message, '[skip github]')
    runs-on: Ubuntu-latest # Required, but it can be anything here.

    steps:
    - uses: styfle/cancel-workflow-action@0.6.0
      with:
        access_token: ${{ github.token }}

  cabal:
    needs: auto-cancel

    runs-on: ${{ matrix.os }}
    strategy:
      fail-fast: false
      matrix:
        os: [windows-latest, macos-latest, ubuntu-20.04]
        ghc-ver: [9.2.1]
        cabal-ver: [latest]
    env:
      FLAGS: "-f enable-cluster-counting"
      # ICU_URL: 'https://github.com/unicode-org/icu/releases/download/release-69-1/icu4c-69_1-Win64-MSVC2019.zip'

    steps:
    - uses: actions/checkout@v2
      with:
        submodules: recursive

    - uses: haskell/actions/setup@v1
      id: setup-haskell
      with:
        ghc-version: ${{ matrix.ghc-ver }}
        cabal-version: ${{ matrix.cabal-ver }}

    - name: Environment settings based on the Haskell setup
      run: |
        export GHC_VER=$(ghc --numeric-version)
        export CABAL_VER=$(cabal --numeric-version)
        echo "GHC_VER   = ${GHC_VER}"
        echo "CABAL_VER = ${CABAL_VER}"
        echo "GHC_VER=${GHC_VER}"       >> ${GITHUB_ENV}
        echo "CABAL_VER=${CABAL_VER}"   >> ${GITHUB_ENV}
      # From now on, use env.{GHC|CABAL}_VER rather than matrix.{ghc|cabal}-ver!

    - name: Configure the build plan
      run: |
        cabal update
        cabal configure ${FLAGS} -O0

    - name: Install the ICU library (Windows)
      if: ${{ runner.os == 'Windows' }}
      shell: pwsh
      run: |
        $env:PATH = "C:\msys64\mingw64\bin;C:\msys64\usr\bin;$origPath;$env:PATH"
        pacman --noconfirm -Sy msys2-keyring mingw-w64-x86_64-pkg-config mingw-w64-x86_64-icu

    - name: Set up pkg-config for the ICU library (macOS)
      if: ${{ runner.os == 'macOS' }}
      run: |
        echo "PKG_CONFIG_PATH=/usr/local/opt/icu4c/lib/pkgconfig" >> ${GITHUB_ENV}

    # Andreas, 2022-02-07: This step seem no longer necessary for Ubuntu 20.04.
    # But keep it here commented-out, we might need it in the future again.
    # - name: Install the icu library (Ubuntu)
    #   if: ${{ runner.os == 'Linux' }}
    #   run: |
    #     sudo apt-get update -qq
    #     sudo apt-get install libicu-dev -qq

    # Andreas, 2022-02-07: Installing with pacman is easier.
    # Keep this here commented out, in case we need it again.
    # - name: Download the icu4c library from unicode-org (Windows)
    #   if: ${{ runner.os == 'Windows' }}
    #   env:
    #     ICU_DIR: '/c/icu4c'
    #     ICU_FILE: '/tmp/icu4c.zip'
    #   run: |
    #     curl -sSLo ${ICU_FILE} ${ICU_URL}
    #     7z e ${ICU_FILE} -o${ICU_DIR}
    #     mkdir -p ${ICU_DIR}/include/unicode
    #     mv ${ICU_DIR}/*.h ${ICU_DIR}/include/unicode

    #     cabal user-config update --augment="extra-lib-dirs: $(cygpath -w ${ICU_DIR})" --augment="extra-include-dirs: $(cygpath -w ${ICU_DIR}/include)"

    - uses: actions/cache@v2
      name: Cache dependencies
      id: cache
      with:
        path: |
          ${{ steps.setup-haskell.outputs.cabal-store }}
        # The file `plan.json` contains the build information.
        key: ${{ runner.os }}-cabal-01-${{ env.GHC_VER }}-${{ env.CABAL_VER }}-${{ hashFiles('**/plan.json') }}

    - name: Install dependencies
      if: ${{ !steps.cache.outputs.cache-hit }}
      run: |
        cabal build --only-dependencies

    - name: Build Agda
      run: |
        cabal build

    - name: Build Agda without enable-cluster-counting
      run: |
        cabal configure -O0
        cabal build
