name: ci

on:
  workflow_dispatch:
  pull_request:
  push:
    branches:
      - master
    paths-ignore:
      - '**.md'
      - '**.rst'
      - '**.sh'

jobs:
  build:
    name: ${{ matrix.name }}
    runs-on: ${{ matrix.os }}

    strategy:
      fail-fast: false
      matrix:

        name: [ 
          windows-2019-cl,
          windows-2019-mingw,
          ubuntu-20.04-gcc,
          ubuntu-20.04-clang,
          macOS-11-clang,
          macOS-11-gcc
        ]

        include:
          - name: windows-2019-cl
            os: windows-2019
            compiler: cl

          - name: windows-2019-mingw
            os: windows-2019
            compiler: mingw-w64

          - name: ubuntu-20.04-gcc
            os: ubuntu-20.04
            compiler: gcc
            version: "10"

          - name: ubuntu-20.04-clang
            os: ubuntu-20.04
            compiler: clang
            version: "10"

          - name: macOS-11-clang
            os: macOS-11
            compiler: xcode
            version: "12.4"

          - name: macOS-11-gcc
            os: macOS-11
            compiler: gcc
            version: "10"

    steps:
      - uses: actions/checkout@v3

      - name: Install and setup MSYS2 mingw
        if: runner.os == 'Windows' && matrix.compiler == 'mingw-w64'
        uses: msys2/setup-msys2@v2     
        with:
          msystem: MINGW64
          install: mingw-w64-x86_64-toolchain make gcc diffutils

      - name: Build and test windows mingw
        if: runner.os == 'Windows' &&  matrix.compiler == 'mingw-w64'
        shell: msys2 {0}
        run: |
          make

      - name: Install and setup msvc prompt
        if: runner.os == 'Windows'
        uses: ilammy/msvc-dev-cmd@v1

      - name: Build and test windows MSVC
        if: runner.os == 'Windows' &&  matrix.compiler == 'cl'
        run: |
          .\build_msvc.bat --clean

      - name:  Install and setup Linux packages
        if: runner.os == 'Linux'
        run: |
          sudo apt-get -y -qq update
          if [ "${{ matrix.compiler }}" = "gcc" ]; then
            sudo apt-get install -y --no-install-recommends g++-${{ matrix.version }}-multilib
            echo "CC=gcc-${{ matrix.version }}" >> $GITHUB_ENV
            echo "CCC=g++-${{ matrix.version }}" >> $GITHUB_ENV
          else
            sudo apt-get install -y --no-install-recommends g++-multilib
            echo "CC=clang-${{ matrix.version }}" >> $GITHUB_ENV
            echo "CCC=clang++-${{ matrix.version }}" >> $GITHUB_ENV
          fi

      - name: Build and test Linux
        if: runner.os == 'Linux'
        run: |
          make
      
      - name:  Install and setup MacOS packages
        if: runner.os == 'macOS'
        run: |
          if [ "${{ matrix.compiler }}" = "gcc" ]; then
            brew install gcc@${{ matrix.version }}
            export "CC=gcc-${{ matrix.version }}" >> $GITHUB_ENV
            export "CCC=g++-${{ matrix.version }}" >> $GITHUB_ENV
          else
            ls -ls /Applications/
            sudo xcode-select -switch /Applications/Xcode_${{ matrix.version }}.app
            export "CC=clang-${{ matrix.version }}" >> $GITHUB_ENV
            export "CCC=clang++-${{ matrix.version }}" >> $GITHUB_ENV
          fi

      - name: Build and test MacOS clang
        if: runner.os == 'MacOS' &&  matrix.compiler == 'xcode' 
        run: |
          make

      - name: Build and test MacOS gcc
        if: runner.os == 'MacOS' &&  matrix.compiler == 'gcc'
        run: |
          make
