name: CI
on:
  push:
    branches: '**'
  pull_request:
    branches: 'master'

jobs:
  unixish:
    name: ${{ matrix.os }} ${{ matrix.flavor }} (cc=${{ matrix.cc }})
    strategy:
      fail-fast: false
      matrix:
        include:
          - flavor: asan
            cc: clang-11
            runner: ubuntu-20.04
            os: linux
          - flavor: lint
            cc: gcc
            runner: ubuntu-20.04
            os: linux
          - flavor: tsan
            cc: clang-11
            runner: ubuntu-20.04
            os: linux
          - cc: clang
            runner: macos-10.15
            os: osx
    runs-on: ${{ matrix.runner }}
    env:
      CC: ${{ matrix.cc }}
      CI_OS_NAME: ${{ matrix.os }}
    steps:
      - uses: actions/checkout@v2

      - name: Setup commom environment variables
        run: ./.github/workflows/env.sh ${{ matrix.flavor }}

      - name: Setup clang repository
        if: matrix.flavor == 'asan' || matrix.flavor == 'tsan'
        run: |
          wget -O - https://apt.llvm.org/llvm-snapshot.gpg.key|sudo apt-key add -
          sudo add-apt-repository 'deb http://apt.llvm.org/focal/ llvm-toolchain-focal-11 main'

      - name: Install apt packages
        if: matrix.os == 'linux'
        run: |
          sudo apt-get update
          sudo apt-get install -y autoconf automake build-essential ccache cmake cpanminus cscope gcc-multilib gdb gettext gperf language-pack-tr libtool-bin locales ninja-build pkg-config python3 python3-pip python3-setuptools unzip valgrind xclip

      - name: Install new clang
        if: matrix.flavor == 'asan' || matrix.flavor == 'tsan'
        run: sudo apt-get install -y clang-11

      - name: Install brew packages
        if: matrix.os == 'osx'
        run: |
          # Workaround brew issues
          rm -f /usr/local/bin/2to3
          brew unlink gcc@8 gcc@9
          brew update >/dev/null
          brew upgrade
          brew install automake ccache perl cpanminus ninja

      - name: Setup interpreter packages
        run: |
          ./ci/before_install.sh
          ./ci/install.sh

      - name: Cache dependencies
        uses: actions/cache@v2
        with:
          path: |
            ${{ env.CACHE_NVIM_DEPS_DIR }}
            ~/.ccache
          key: ${{ runner.os }}-${{ matrix.flavor }}-${{ matrix.cc }}-${{ hashFiles('cmake/*', 'third-party/**', '**/CMakeLists.txt') }}-${{ github.base_ref }}

      - name: Build third-party
        run: ./ci/before_script.sh

      - name: Build and test
        run: ./ci/script.sh

      - name: Cache dependencies
        if: ${{ success() }}
        run: ./ci/before_cache.sh

  windows:
    runs-on: windows-2016
    env:
      DEPS_BUILD_DIR: "C:/projects/nvim-deps"
      DEPS_PREFIX: "C:/projects/nvim-deps/usr"

    strategy:
      fail-fast: false
      matrix:
        config: [ MINGW_64-gcov, MINGW_32, MSVC_64, MSVC_32 ]
    name: windows (${{ matrix.config }})
    steps:
      - uses: actions/checkout@v2

      - uses: actions/cache@v2
        with:
          path: C:\projects\nvim-deps
          key: ${{ matrix.config }}-${{ hashFiles('third-party\**') }}

      - name: Run CI
        run: powershell ci\build.ps1
        env:
          CONFIGURATION: ${{ matrix.config }}
