# This file is based on:
# https://github.com/Ifsttar/I-Simpa/blob/master/appveyor.yml


version: '20.09.0-DevelopmentSnapshot_{build}'

image:
 - Visual Studio 2019 # For MSVC 2019 builds only

# clone directory
clone_folder: c:\projects\wxMaxima
# Do not clone the entire git repository
clone_depth: 15

configuration:
 - debug
 
environment:
  # The wxWidgets version as referred to in the source package
  WXVERSION: 3.1.3
  # The wxWidgets version as referred to in the build folder
  # Typically it'd be the wxWidgets version with an optional suffix
  # to indicate build "epoch". The epoch changes are used to invalidate
  # a cached wxWidgets build and force a rebuild.
  WXBUILDVER: 3.1.3_3
  CMAKEGEN: "Ninja"
  matrix:
    - TOOLKIT_INIT: ".appveyor\\MSVC_2019_x64.ps1"
      WXW_LIB: vc_x64_lib
      EXTRA_CMAKE_ARGS: ""
    - TOOLKIT_INIT: ".appveyor\\MINGW_8.1.0_x64.ps1"
      WXW_LIB: gcc_x64_lib
      EXTRA_CMAKE_ARGS: "-DCMAKE_AR=C:/mingw-w64/x86_64-8.1.0-posix-seh-rt_v6-rev0/mingw64/bin/ar.exe"

matrix:
  exclude:
  - configuration: debug
    TOOLKIT_INIT: ".appveyor\\MINGW_8.1.0_x64.ps1"
    WXW_LIB: vc_x64_lib
  - configuration: debug
    TOOLKIT_INIT: ".appveyor\\MSVC_2019_x64.ps1"
    WXW_LIB: gcc_x64_lib

cache:
  - 'C:\wxwidgets-install_%WXBUILDVER%'
  - 'C:\wxwidgets-build_%WXBUILDVER%'
  # The build directory is only cached for the .pdb files. Other files are removed.
  # - C:\ProgramData\chocolatey\bin -> appveyor.yml
  # - C:\ProgramData\chocolatey\lib -> appveyor.yml
  # Do not cache the build itself - we only care about the install!

# branches to build
branches:
  except:
  - gh_pages
  - DebianPackaging
  - DebianPackaging_NightlyBuild

install:
  - ps: |
      echo '- Starting the build'
      echo "    Job:    $env:APPVEYOR_JOB_NAME"
      echo "    Branch: $env:APPVEYOR_REPO_BRANCH"
      echo ''
      echo '- Installing pandoc...'
      choco install -y --no-progress pandoc

before_build:
  - ps: |
      iex $env:TOOLKIT_INIT
      refreshenv
      $SRC_ARCHIVE="wxWidgets-${env:WXVERSION}.7z"
      $BUILD_DIR="C:\wxwidgets-build_${env:WXBUILDVER}"
      $INSTALL_DIR="C:\wxwidgets-install_${env:WXBUILDVER}"
      $INSTALL_LIB_DIR="$INSTALL_DIR\lib\${env:WXW_LIB}"
      $SOURCE_DIR="C:\wxwidgets-source_${env:WXVERSION}"
      copy C:/mingw-w64/x86_64-8.1.0-posix-seh-rt_v6-rev0/mingw64/bin/windres.exe C:/mingw-w64/x86_64-8.1.0-posix-seh-rt_v6-rev0/mingw64/bin/x86_64-w64-mingw32-windres.exe
      # If there's no installation of wxWidgets available, we have to build it
      if (!(Test-Path $INSTALL_DIR)) {
          echo '- No wxWidgets installation $INSTALL_DIR was found'
          
          echo '* Downloading wxWidgets sources'
          Start-FileDownload "https://github.com/wxWidgets/wxWidgets/releases/download/v${env:WXVERSION}/$SRC_ARCHIVE"
          
          echo '* Unpacking wxWidgets sources'
          7z x "$SRC_ARCHIVE" "-o$SOURCE_DIR" -y
          
          echo '* Building wxWidgets from sources'
          mkdir $BUILD_DIR | Out-Null
          pushd $BUILD_DIR
          # Note: wxWidgets unity builds fail at least for version 3.1.3. This is a fixable problem but would
          # require patching the CMakeLists there. So for now it's disabled, since we don'y rebuild wxWidgets
          # often.
          echo cmake -G "$env:CMAKEGEN" "$env:EXTRA_CMAKE_ARGS" --config Release  `
            -DwxBUILD_SHARED=NO  `
            -DwxUSE_STC=NO  `
            -DwxUSE_RIBBON=NO  `
            "-DCMAKE_INSTALL_PREFIX=$INSTALL_DIR" -Wno-dev `
            "$SOURCE_DIR"
          cmake -G "$env:CMAKEGEN" "$env:EXTRA_CMAKE_ARGS" --config Release  `
            -DwxBUILD_SHARED=NO  `
            -DwxUSE_STC=NO  `
            -DwxUSE_RIBBON=NO  `
            "-DCMAKE_INSTALL_PREFIX=$INSTALL_DIR" -Wno-dev `
            "$SOURCE_DIR"
          cmake --build . --config Release -v
          cmake --build . --config Release --target install
          # Remove all files other than .pdb files - we do need those so that the build is quiet
          Get-ChildItem -Recurse -File | Where {($_.Extension -ne ".pdb")} | Remove-Item
          # List the PDB files for diagnostic purposes
          Get-ChildItem -Recurse -File | Select-Object FullName
          popd
      }
      $env:INSTALL_DIR = $INSTALL_DIR
      $env:INSTALL_LIB_DIR = $INSTALL_LIB_DIR

  - ps: |
      $INSTALL_DIR = $env:INSTALL_DIR -replace '\\', '/'
      $INSTALL_LIB_DIR = $env:INSTALL_LIB_DIR -replace '\\', '/'

      echo '- Configuring wxMaxima'
      echo "    Job:    $env:APPVEYOR_JOB_NAME"
      echo "    Branch: $env:APPVEYOR_REPO_BRANCH"
      mkdir build | Out-Null
      pushd build
      # WX_ROOT_DIR is a workaround for a nasty bug in FindwxWidgets. Without it, the value
      # of wxWidgets_LIB_DIR gets cleared (!) in FindwxWidgets.cmake(495)
      # cmake issue report: https://gitlab.kitware.com/cmake/cmake/-/issues/19176
      cmake -G "$env:CMAKEGEN" --config Release  `
        -DAPPVEYOR_BUILD=1 -DCMAKE_UNITY_BUILD=YES  `
        -DWX_ROOT_DIR="$INSTALL_DIR"  `
        -DwxWidgets_ROOT_DIR="$INSTALL_DIR"  `
        -DwxWidgets_LIB_DIR="$INSTALL_LIB_DIR"  `
        -DwxWidgets_CONFIGURATION=mswu  `
        $env:TOOLKIT_CMAKE_ARGS ..
      popd

build_script:
  - ps: |
      echo '- Building wxMaxima'
      echo "    Job:    $env:APPVEYOR_JOB_NAME"
      echo "    Branch: $env:APPVEYOR_REPO_BRANCH"
      iex $env:TOOLKIT_INIT
      pushd build
      cmake --build . --config Release
      if (! $?) { throw "CMake failed with exit code $LastExitCode" }
      popd

test_script:
  - ps: |
      pushd build
      #ctest -C Release
      if (! $?) { throw "CTest failed with exit code $LastExitCode" }
      popd

after_test:
  - ps: |
      echo '* Packing the installer'
      pushd build
      cpack -G NSIS
      if (! $?) { throw "CPack failed with exit code $LastExitCode" }
      popd
#      echo '* Compressing the executable'
#      7z a wxMaxima.exe.7z build/src/*.exe


for:
  -
    matrix:
      only:
        - WXW_LIB: gcc_x64_lib
#    artifacts:
#    - path: 'wxMaxima.exe.7z'
#      name: wxMaxima.exe.7z
    deploy:
      - provider: Environment
        name: GithubReleases
        draft: false
        prerelease: false
        on:
          APPVEYOR_REPO_TAG: true
