name: CMake multiplatform

on:
  push:
    branches: [ "main" ]
  pull_request:
    branches: [ "main" ]
  
permissions:
  contents: write

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

    strategy:
      matrix:
        os: [ubuntu-latest, windows-latest]
        build_type: [Release]
        include:
          - os: windows-latest
            c_compiler: clang
            cpp_compiler: clang++
          - os: ubuntu-latest
            c_compiler: clang
            cpp_compiler: clang++

    steps:
    - uses: actions/checkout@v4
    - name: Set reusable strings
      # Turn repeated input strings (such as the build output directory) into step outputs. These step outputs can be used throughout the workflow file.
      id: strings
      shell: bash
      run: |
        echo "build-output-dir=${{ github.workspace }}/build" >> "$GITHUB_OUTPUT"

    - name: Setup Environment
      if: ${{ matrix.os == 'ubuntu-latest' }}
      run: |
        sudo apt-get update -qq
        sudo apt-get install gcc-multilib
        sudo apt-get install -y --no-install-recommends build-essential libgl-dev libwayland-dev xorg-dev libxkbcommon-dev
    
    - name: Configure CMake
      run: >
        cmake -B ${{ steps.strings.outputs.build-output-dir }}
        -DCMAKE_CXX_COMPILER=${{ matrix.cpp_compiler }}
        -DCMAKE_C_COMPILER=${{ matrix.c_compiler }}
        -DCMAKE_BUILD_TYPE=${{ matrix.build_type }}
        -S ${{ github.workspace }}

    - name: Build
      run: cmake --build ${{ steps.strings.outputs.build-output-dir }} --config ${{ matrix.build_type }}
      
    - name: Install
      run: cmake --install ${{ steps.strings.outputs.build-output-dir }} --config ${{ matrix.build_type }}

    - name: Pack
      run: |
        cd ${{ steps.strings.outputs.build-output-dir }}
        cpack -C ${{ matrix.build_type }}

    - name: Create or Update Development Release
      uses: softprops/action-gh-release@v1
      with:
        name: Development Build
        tag_name: dev
        body: |
          ${{ github.event.head_commit.timestamp }}
        draft: false
        prerelease: true
        generate_release_notes: false
        files: 'build/package/*.zip'
      env:
        GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}