name: Build raylib - Windows

on:
  workflow_dispatch:
  push:
    paths:
      - 'src/**'
      - 'examples/**'
      - '.github/workflows/windows.yml'
  pull_request:
    paths:
      - 'src/**'
      - 'examples/**'
      - '.github/workflows/windows.yml'
  release:
    types: [published]

permissions:
  contents: read

jobs:
  build:
    permissions:
      contents: write  # for actions/upload-release-asset to upload release asset
    runs-on: windows-latest
    strategy:
      fail-fast: false
      max-parallel: 1
      matrix:
        compiler: [mingw-w64, msvc16]
        ziptarget: [ win32, win64, winarm64 ]
        include:
        - compiler: mingw-w64
          ARCH: "i686"
          WINDRES_ARCH: pe-i386
          ziptarget: "win32"
        - compiler: mingw-w64
          ARCH: "x86_64"
          WINDRES_ARCH: pe-x86-64
          ziptarget: "win64"
        - compiler: msvc16
          ARCH: "x86"
          VSARCHPATH: "Win32"
          ziptarget: "win32"
        - compiler: msvc16
          ARCH: "x64"
          VSARCHPATH: "x64"
          ziptarget: "win64"
        - compiler: msvc16
          ARCH: "ARM64"
          VSARCHPATH: "ARM64"
          ziptarget: "winarm64"
        exclude:
          - compiler: mingw-w64
            ziptarget: winarm64 # We don't build arm64 with mingw-w64 yet

    env:
      RELEASE_NAME: raylib-dev_${{ matrix.ziptarget }}_${{ matrix.compiler }}
      GNUTARGET: default

    steps:
    - name: Checkout
      uses: actions/checkout@master

    - name: Setup Release Version
      run: |
        echo "RELEASE_NAME=raylib-${{ github.event.release.tag_name }}_${{ matrix.ziptarget }}_${{ matrix.compiler }}" >> $GITHUB_ENV
      shell: bash
      if: github.event_name == 'release' && github.event.action == 'published'

    - name: Setup Environment
      run: |
        dir
        mkdir build
        cd build
        mkdir ${{ env.RELEASE_NAME }}
        cd ${{ env.RELEASE_NAME }}
        mkdir include
        mkdir lib
        cd ../../../raylib

    # Setup MSBuild.exe path if required
    - name: Setup MSBuild
      uses: microsoft/setup-msbuild@v2
      if: matrix.compiler == 'msvc16'

    - name: Build Library (MinGW-w64 32bit)
      run: |
        cd src
        x86_64-w64-mingw32-gcc.exe --version
        windres.exe --version
        dir C:\msys64\mingw64\bin
        make PLATFORM=PLATFORM_DESKTOP CC=x86_64-w64-mingw32-gcc.exe RAYLIB_LIBTYPE=STATIC RAYLIB_RELEASE_PATH="../build/${{ env.RELEASE_NAME }}/lib" CUSTOM_CFLAGS=-m32
        //windres.exe -i raylib.dll.rc -o raylib.dll.rc.data -O coff --target=${{ matrix.WINDRES_ARCH }}
        //make PLATFORM=PLATFORM_DESKTOP CC=${{ matrix.ARCH }}-w64-mingw32-gcc.exe RAYLIB_LIBTYPE=SHARED RAYLIB_RELEASE_PATH="../build/${{ env.RELEASE_NAME }}/lib" -B
        cd ..
      shell: cmd
      if: |
        matrix.compiler == 'mingw-w64' &&
        matrix.arch == 'i686'

    - name: Build Library (MinGW-w64 64bit)
      run: |
        cd src
        ${{ matrix.ARCH }}-w64-mingw32-gcc.exe --version
        windres.exe --version
        dir C:\msys64\mingw64\bin
        make PLATFORM=PLATFORM_DESKTOP CC=${{ matrix.ARCH }}-w64-mingw32-gcc.exe RAYLIB_LIBTYPE=STATIC RAYLIB_RELEASE_PATH="../build/${{ env.RELEASE_NAME }}/lib"
        windres.exe -i raylib.dll.rc -o raylib.dll.rc.data -O coff --target=${{ matrix.WINDRES_ARCH }}
        make PLATFORM=PLATFORM_DESKTOP CC=${{ matrix.ARCH }}-w64-mingw32-gcc.exe RAYLIB_LIBTYPE=SHARED RAYLIB_RELEASE_PATH="../build/${{ env.RELEASE_NAME }}/lib" -B
        cd ..
      shell: cmd
      if: |
        matrix.compiler == 'mingw-w64' &&
        matrix.arch == 'x86_64'

    - name: Build Library (MSVC16)
      run: |
        cd projects/VS2022
        msbuild.exe raylib.sln /target:raylib /property:Configuration=Release /property:Platform=${{ matrix.ARCH }}
        copy /Y .\build\raylib\bin\${{ matrix.VSARCHPATH }}\Release\raylib.lib .\..\..\build\${{ env.RELEASE_NAME }}\lib\raylib.lib
        msbuild.exe raylib.sln /target:raylib /property:Configuration=Release.DLL /property:Platform=${{ matrix.ARCH }}
        copy /Y .\build\raylib\bin\${{ matrix.VSARCHPATH }}\Release.DLL\raylib.dll .\..\..\build\${{ env.RELEASE_NAME }}\lib\raylib.dll
        copy /Y .\build\raylib\bin\${{ matrix.VSARCHPATH }}\Release.DLL\raylib.lib .\..\..\build\${{ env.RELEASE_NAME }}\lib\raylibdll.lib
        cd ../..
      shell: cmd
      if: matrix.compiler == 'msvc16'

    - name: Generate Artifacts
      run: |
        copy /Y .\src\raylib.h .\build\${{ env.RELEASE_NAME }}\include\raylib.h
        copy /Y .\src\raymath.h .\build\${{ env.RELEASE_NAME }}\include\raymath.h
        copy /Y .\src\rlgl.h .\build\${{ env.RELEASE_NAME }}\include\rlgl.h
        copy /Y .\CHANGELOG .\build\${{ env.RELEASE_NAME }}\CHANGELOG
        copy /Y .\README.md .\build\${{ env.RELEASE_NAME }}\README.md
        copy /Y .\LICENSE .\build\${{ env.RELEASE_NAME }}\LICENSE
        cd build
        7z a ./${{ env.RELEASE_NAME }}.zip ./${{ env.RELEASE_NAME }}
        dir
      shell: cmd

    - name: Upload Artifacts
      uses: actions/upload-artifact@v4
      with:
        name: ${{ env.RELEASE_NAME }}.zip
        path: ./build/${{ env.RELEASE_NAME }}.zip

    - name: Upload Artifact to Release
      uses: softprops/action-gh-release@v1
      with:
        files: ./build/${{ env.RELEASE_NAME }}.zip
      env:
        GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
      if: github.event_name == 'release' && github.event.action == 'published'
