name: CMake

on:

  push:
    paths:
    - src/**
    - "**/CMakeLists.txt"
    - "CMakePresets.json"
    - .github/workflows/cmake.yml
    - vcpkg.json
    - "!**/Makefile*"

  pull_request:
    paths:
    - src/**
    - "**/CMakeLists.txt"
    - "CMakePresets.json"
    - .github/workflows/**
    - .github/workflows/cmake.yml
    - vcpkg.json
    - "!**/Makefile*"

jobs:
  mingw:
    name: MSYS2 ${{ matrix.build.name }} build (${{ matrix.environment.msystem }})

    runs-on: windows-latest

    defaults:
      run:
        shell: msys2 {0}

    strategy:
      fail-fast: false
      matrix:
        build:
        - name: Regular
          preset: regular
          target: install/strip
        - name: Debug
          preset: debug
          target: install
        - name: Dev
          preset: experimental
          target: install
        environment:
          - msystem: MINGW32
            prefix: mingw-w64-i686
          - msystem: MINGW64
            prefix: mingw-w64-x86_64
          - msystem: UCRT64
            prefix: mingw-w64-ucrt-x86_64
          - msystem: CLANG64
            prefix: mingw-w64-clang-x86_64

    steps:
    - uses: msys2/setup-msys2@v2
      with:
        path-type: inherit
        update: true
        msystem: ${{ matrix.environment.msystem }}
        install: >-
          ${{ matrix.environment.prefix }}-ninja
          ${{ matrix.environment.prefix }}-gcc
          ${{ matrix.environment.prefix }}-clang
          ${{ matrix.environment.prefix }}-pkg-config
          ${{ matrix.environment.prefix }}-openal
          ${{ matrix.environment.prefix }}-freetype
          ${{ matrix.environment.prefix }}-SDL2
          ${{ matrix.environment.prefix }}-zlib
          ${{ matrix.environment.prefix }}-libpng
          ${{ matrix.environment.prefix }}-libvncserver
          ${{ matrix.environment.prefix }}-winpthreads
    - uses: actions/checkout@v2
    - name: Configure CMake
      run: >-
        cmake -S . -B build
        --preset ${{ matrix.build.preset }}
        -D CMAKE_INSTALL_PREFIX=./build/artifacts
        -D VNC=OFF
    - name: Build
      run: cmake --build build --target ${{ matrix.build.target }}
    - uses: actions/upload-artifact@v2
      with:
        name: '86Box-${{ matrix.build.name }}-MSYS2-${{ matrix.environment.msystem }}-${{ github.sha }}'
        path: build/artifacts/bin/**

  vs2019:
    name: VS2019 ${{ matrix.build.name }} ${{ matrix.target-arch }} build (${{ matrix.toolset }})

    runs-on: windows-latest

    strategy:
      fail-fast: false
      matrix:
        build:
        - name: Debug
          dev-build: off
          new-dynarec: off
          type: Debug
        - name: Dev
          dev-build: on
          new-dynarec: on
          type: Debug
        target-arch: ['Win32', 'x64', 'ARM64']
        toolset: ['clangcl']
        exclude:
          - target-arch: 'ARM64'
            build:
              new-dynarec: off

    steps:
    - uses: actions/checkout@v2
    - uses: actions/cache@v2
      with:
        path: build/vcpkg_installed
        key: vcpkg-${{ hashFiles('vcpkg.json') }}-${{ matrix.target-arch }}
    - name: Configure CMake
      run: >-
        cmake -S . -B build
        -G "Visual Studio 16 2019" -A ${{ matrix.target-arch }} -T ${{ matrix.toolset }}
        -D CMAKE_TOOLCHAIN_FILE=C:\vcpkg\scripts\buildsystems\vcpkg.cmake
        -D CMAKE_INSTALL_PREFIX=./build/artifacts
        -D DEV_BRANCH=${{ matrix.build.dev-build }}
        -D NEW_DYNAREC=${{ matrix.build.new-dynarec }}
        -D VNC=OFF
    - name: Build
      run: cmake --build build --config ${{ matrix.build.type }} --target install
    - uses: actions/upload-artifact@v2
      with:
        name: '86Box-${{ matrix.build.name }}-VS2019-${{ matrix.target-arch }}-${{ matrix.toolset }}-${{ github.sha }}'
        path: build/artifacts/bin/**

  linux:
    name: "Linux GCC 11"

    runs-on: ubuntu-latest
    strategy:
      fail-fast: false
      matrix:
        build:
        - name: Debug
          dev-build: off
          new-dynarec: off
          type: Debug
        - name: Dev
          dev-build: on
          new-dynarec: on
          type: Debug

    steps:
      - uses: actions/checkout@v2
      - name: Install dependencies
        run: sudo apt update && sudo apt install gcc-11 g++-11 libfreetype-dev libsdl2-dev libpng-dev libopenal-dev libc6-dev
      - name: Configure CMake
        run: >-
          cmake -S . -B build
          -D CMAKE_INSTALL_PREFIX=./build/artifacts
          -D DEV_BRANCH=${{ matrix.build.dev-build }}
          -D NEW_DYNAREC=${{ matrix.build.new-dynarec }}
          -D VNC=OFF
          -D CMAKE_BUILD_TYPE=${{ matrix.build.type }}
          -D CMAKE_C_COMPILER=gcc-11 -D CMAKE_CXX_COMPILER=g++-11
      - name: Build
        run: cmake --build build --target install

  macos:
    name: "macOS 11"

    runs-on: macos-11
    strategy:
      fail-fast: false
      matrix:
        build:
        - name: Debug
          dev-build: off
          new-dynarec: off
          type: Debug
        - name: Dev
          dev-build: on
          new-dynarec: on
          type: Debug

    steps:
      - uses: actions/checkout@v2
      - name: Install dependencies
        run: brew install freetype sdl2 libpng openal-soft
      - name: Configure CMake
        run: >-
          cmake -S . -B build
          -D CMAKE_INSTALL_PREFIX=./build/artifacts
          -D DEV_BRANCH=${{ matrix.build.dev-build }}
          -D NEW_DYNAREC=${{ matrix.build.new-dynarec }}
          -D VNC=OFF
          -D CMAKE_BUILD_TYPE=${{ matrix.build.type }}
      - name: Build
        run: cmake --build build --target install
