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:
  msys2:
    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 ODR
          slug: -ODR
          preset: regular
          target: install/strip
        - name: Debug ODR
          slug: -ODR-Debug
          preset: debug
          target: install
        - name: Dev ODR
          slug: -ODR-Dev
          preset: experimental
          target: install
        - name: Regular NDR
          slug: -NDR
          preset: regularndr
          target: install/strip
        - name: Debug NDR
          slug: -NDR-Debug
          preset: debugndr
          target: install
        - name: Dev NDR
          slug: -NDR-Dev
          preset: experimentalndr
          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: CLANG32
#            prefix: mingw-w64-clang-i686
          - 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 }}-cc
          ${{ 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 }}-rtmidi
    - 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.slug }}-Windows-${{ matrix.environment.msystem }}-gha${{ github.run_number }}'
        path: build/artifacts/**

  llvm-windows:
    name: "Windows vcpkg/LLVM (${{ matrix.build.name }} ${{ matrix.target.name }})"

    runs-on: windows-2022

    env:
      VCPKG_BINARY_SOURCES: 'clear;nuget,GitHub,readwrite'

    strategy:
      fail-fast: false
      matrix:
        build:
        - name: Regular ODR
          slug: -ODR
          type: Release
          dev-build: off
          new-dynarec: off
          strip: --strip
        - name: Debug ODR
          slug: -ODR-Debug
          type: Debug
          dev-build: off
          new-dynarec: off
        - name: Dev ODR
          slug: -ODR-Dev
          type: Debug
          dev-build: on
          new-dynarec: off
        - name: Regular NDR
          slug: -NDR
          type: Release
          strip: --strip
          dev-build: off
          new-dynarec: on
        - name: Debug NDR
          slug: -NDR-Debug
          type: Debug
          dev-build: off
          new-dynarec: on
        - name: Dev NDR
          slug: -NDR-Dev
          type: Debug
          dev-build: on
          new-dynarec: on
        target:
        - name: x86
          triplet: x86-windows-static
          toolchain: cmake/llvm-win32-i686.cmake
          vcvars: x64_x86
        - name: x64
          triplet: x64-windows-static
          toolchain: cmake/llvm-win32-x86_64.cmake
          vcvars: x64
        - name: ARM64
          triplet: arm64-windows-static
          toolchain: cmake/llvm-win32-aarch64.cmake
          vcvars: x64_arm64
        exclude:
        - build: 
            new-dynarec: off
          target:
            name: ARM64

    steps:
    - uses: actions/checkout@v2
    - name: Download Ninja
      run: >
        Invoke-WebRequest https://github.com/ninja-build/ninja/releases/download/v1.10.2/ninja-win.zip -OutFile ninja-win.zip && 
        Expand-Archive ninja-win.zip -DestinationPath .
    - name: Setup NuGet Credentials
      run: >
        & (C:/vcpkg/vcpkg fetch nuget | tail -n 2)
        sources add
        -source "https://nuget.pkg.github.com/86Box/index.json"
        -storepasswordincleartext
        -name "GitHub"
        -username "86Box"
        -password "${{ secrets.GITHUB_TOKEN }}"
    - name: vcpkg package restore
      if: false
      run: vcpkg install freetype libpng openal-soft sdl2 rtmidi --triplet ${{ matrix.target.triplet }}
    - name: Configure CMake
      run: >
        call "C:/Program Files/Microsoft Visual Studio/2022/Enterprise/VC/Auxiliary/Build/vcvarsall.bat" ${{ matrix.target.vcvars }}
  
        set PATH=C:/Program Files/LLVM/bin;%PATH%

        cmake -S . -B build -G Ninja -D CMAKE_BUILD_TYPE=${{ matrix.build.type }}
        -D NEW_DYNAREC=${{ matrix.build.new-dynarec }}
        -D CMAKE_TOOLCHAIN_FILE=C:/vcpkg/scripts/buildsystems/vcpkg.cmake
        -D VCPKG_CHAINLOAD_TOOLCHAIN_FILE=${{ github.workspace }}/${{ matrix.target.toolchain }}
        -D VCPKG_TARGET_TRIPLET=${{ matrix.target.triplet }}
      shell: cmd
    - name: Build
      run: |
        call "C:/Program Files/Microsoft Visual Studio/2022/Enterprise/VC/Auxiliary/Build/vcvarsall.bat" ${{ matrix.target.vcvars }}
        cmake --build build
      shell: cmd
    - name: Generate package
      run: cmake --install build --prefix ./build/artifacts ${{ matrix.build.strip }}
    - uses: actions/upload-artifact@v2
      with:
        name: '86Box${{ matrix.build.slug }}-Windows-LLVM-${{ matrix.target.name }}-gha${{ github.run_number }}'
        path: build/artifacts/**

  linux:
    name: "Linux GCC 11 (${{ matrix.build.name }} x86_64)"

    runs-on: ubuntu-latest

    strategy:
      fail-fast: false
      matrix:
        build:
        - name: Regular ODR
          slug: -ODR
          type: Release
          dev-build: off
          new-dynarec: off
          strip: --strip
        - name: Debug ODR
          slug: -ODR-Debug
          type: Debug
          dev-build: off
          new-dynarec: off
        - name: Dev ODR
          slug: -ODR-Dev
          type: Debug
          dev-build: on
          new-dynarec: off
        - name: Regular NDR
          slug: -NDR
          type: Release
          strip: --strip
          dev-build: off
          new-dynarec: on
        - name: Debug NDR
          slug: -NDR-Debug
          type: Debug
          dev-build: off
          new-dynarec: on
        - name: Dev NDR
          slug: -NDR-Dev
          type: Debug
          dev-build: on
          new-dynarec: on

    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 librtmidi-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 QT=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
      - name: Generate package
        run: cmake --install build --prefix ./build/artifacts ${{ matrix.build.strip }}
      - uses: actions/upload-artifact@v2
        with:
          name: '86Box${{ matrix.build.slug }}-Linux-x86_64-gha${{ github.run_number }}'
          path: build/artifacts/**

  macos11:
    name: "macOS 11 (${{ matrix.build.name }} x86_64)"

    runs-on: macos-11

    strategy:
      fail-fast: false
      matrix:
        build:
        - name: Regular ODR
          slug: -ODR
          type: Release
          dev-build: off
          new-dynarec: off
          strip: --strip
        - name: Debug ODR
          slug: -ODR-Debug
          type: Debug
          dev-build: off
          new-dynarec: off
        - name: Dev ODR
          slug: -ODR-Dev
          type: Debug
          dev-build: on
          new-dynarec: off
        - name: Regular NDR
          slug: -NDR
          type: Release
          strip: --strip
          dev-build: off
          new-dynarec: on
        - name: Debug NDR
          slug: -NDR-Debug
          type: Debug
          dev-build: off
          new-dynarec: on
        - name: Dev NDR
          slug: -NDR-Dev
          type: Debug
          dev-build: on
          new-dynarec: on

    steps:
      - uses: actions/checkout@v2
      - name: Install dependencies
        run: brew install freetype sdl2 libpng openal-soft rtmidi qt@5
      - name: Configure CMake
        run: >-
          cmake -S . -B build
          --toolchain cmake/flags-gcc-x86_64.cmake
          -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 QT=ON
          -D Qt5_DIR=/usr/local/Cellar/qt@5/5.15.2_1/lib/cmake/Qt5
          -D Qt5LinguistTools_DIR=/usr/local/Cellar/qt@5/5.15.2_1/lib/cmake/Qt5LinguistTools/
      - name: Build
        run: cmake --build build
      - name: Generate package
        run: cmake --install build --prefix ./build/artifacts ${{ matrix.build.strip }}
      - uses: actions/upload-artifact@v2
        with:
          name: '86Box${{ matrix.build.slug }}-macOS-x86_64-gha${{ github.run_number }}'
          path: build/artifacts/**
