name: compile_windows

on: [push]

env:
  BUILD_TYPE: Release
  WXVERSION: 3.2.4

permissions:
  contents: read

jobs:
  compile_windows:
    name: Compile using minGW
    permissions:
      contents: write  # for actions/create-release to create a release
    runs-on: windows-latest
    steps:
      - name: create_release
        if: startsWith(github.ref, 'refs/tags/Version')
        id: create_release
        uses: softprops/action-gh-release@v1
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} # This token is provided by Actions, you do not need to create your own token
        with:
          tag_name: ${{ github.ref }}
          release_name: Release ${{ github.ref }}
          body: |
            Automatic compilation on a new release
          draft: false
          prerelease: false
      - name: Checkout wxMaxima
        uses: actions/checkout@v4
        with:
          # We must fetch at least the immediate parents so that if this is
          # a pull request then we can checkout the head.
          fetch-depth: 15
          path: wxMaxima

      - name: Install Ninja
        run: |
            choco install ninja

      - name: Cache wxWidgets
        uses: actions/cache@v4
        env:
          cache-name: cache-wxWidgets
        with:
          path: wxWidgets-${{env.WXVERSION}}
          key: ${{ runner.os }}-build-${{ env.cache-name }}-3

      # multi-job compilation is broken in wxWidgets, see https://trac.wxwidgets.org/ticket/19029
      # compiling wxWidgets via cmake works, but the result isn't detected by cmake any more.
      - name: Compile wxWidgets
        run: |
            if (Test-Path wxWidgets-${{env.WXVERSION}}) { echo "wxWidgets already present"} else {
               Invoke-WebRequest -Uri https://github.com/wxWidgets/wxWidgets/releases/download/v${{env.WXVERSION}}/wxWidgets-${{env.WXVERSION}}.zip -OutFile wxWidgets-${{env.WXVERSION}}.zip
               Expand-Archive -LiteralPath wxWidgets-${{env.WXVERSION}}.zip -DestinationPath wxWidgets-${{env.WXVERSION}}
               cd wxWidgets-${{env.WXVERSION}}
               cd build
               cd msw
               mingw32-make -f makefile.gcc SHELL=cmd.exe SHARED=0 BUILD=release
               cd ..
               cd ..
               cd ..
            }

      - name: Configure wxMaxima
        run: |
            cd wxMaxima
            new-item build -itemtype directory
            new-item build\Release -itemtype directory
            cd build
            cmake -DwxWidgets_ROOT_DIR="../../wxWidgets-${{env.WXVERSION}}" -DwxWidgets_LIB_DIR="../../wxWidgets-${{env.WXVERSION}}/lib/gcc_lib" -DwxWidgets_CONFIGURATION=mswu -DCMAKE_BUILD_TYPE=release -G "MinGW Makefiles" -DwxWidgets_USE_DEBUG=false -DwxWidgets_USE_UNICODE=true -DwxWidgets_USE_STATIC=true -G Ninja ..
            cd ..
            cd ..
      - name: Compile wxMaxima
        run: |
            cd wxMaxima
            cd build
            cmake --build .
            cd ..
            cd ..
      - name: Package wxMaxima
        run: |
            cd wxMaxima
            cd build
            cpack
            cd ..
            cd ..

  MSVC:
    permissions:
      contents: read # for actions/checkout to fetch code
      actions: read # only required for a private repository by github/codeql-action/upload-sarif to get the Action run status
    name: Compile using MSVC
    runs-on: windows-latest

    steps:
      - name: Checkout repository
        uses: actions/checkout@v4
      - name: Cache wxWidgets
        uses: actions/cache@v4
        env:
          cache-name: cache-wxWidgets
        with:
          path: wxwidgets-source
          key: ${{ runner.os }}-build-msvc-${{ env.cache-name }}-${env:WXVERSION}-1
      - name: wxWidgets
        run: |
             if (Test-Path wxwidgets-source) { echo "wxWidgets already present"} else {
                 echo "Downloading wxWidgets"
                 curl "https://github.com/wxWidgets/wxWidgets/releases/download/v${env:WXVERSION}/wxWidgets-${env:WXVERSION}.7z" -L --output wxWidgets.7z
                 echo "Unpacking wxWidgets sources"
                 7z x "wxWidgets.7z" "-owxwidgets-source" -y
                 echo "Building wxWidgets from sources"
                 cd "wxwidgets-source"
                 cmake "." -DwxUSE_WEBVIEW_EDGE=true
                 cmake --build .
                 cd .. }
      - name: Configure wxMaxima
        run: |
             mkdir build
             cd build
             cmake -DwxWidgets_ROOT_DIR="../wxwidgets-source" -DwxWidgets_LIB_DIR="../wxwidgets-source/lib/vc_x64_dll" -DwxWidgets_CONFIGURATION="mswud" -DCMAKE_BUILD_TYPE=${{ env.config}} ..
             cd ..
      - name: Compile wxMaxima
        run: |
             cd build
             cmake --build .
             cd ..
