name: Cataclysm Windows build

on:
  push:
    branches:
    - master
    paths-ignore:
    - 'android/**'
    - 'build-data/osx/**'
    - 'doc/**'
    - 'doxygen_doc/**'
    - 'gfx/**'
    - 'lang/**'
    - 'lgtm/**'
    - 'tools/**'
    - '!tools/format/**'
    - 'utilities/**'
  pull_request:
    branches:
    - master
    paths-ignore:
    - 'android/**'
    - 'build-data/osx/**'
    - 'doc/**'
    - 'doxygen_doc/**'
    - 'gfx/**'
    - 'lang/**'
    - 'lgtm/**'
    - 'tools/**'
    - '!tools/format/**'
    - 'utilities/**'

# We only care about the latest revision of a PR, so cancel previous instances.
concurrency:
  group: msvc-build-${{ github.event.pull_request.number || github.ref_name }}
  cancel-in-progress: true

env:
  # Compressed size ~1GB based on observations
  CCACHE_LIMIT: 8GB
  CDDA_CCACHE_PATH: ${{ github.workspace }}\ccache\
  CDDA_USE_CCACHE: true
  # There's not enough disk space to build both release and debug versions of
  # our dependencies, so we hack the triplet file to build only release versions
  # Have to use github.workspace because runner namespace isn't available yet.
  VCPKG_OVERLAY_TRIPLETS: ${{ github.workspace }}\.github\vcpkg_triplets
  # Also conveniently disable ccache internal compression
  CCACHE_HARDLINK: true
  # vcpkg with object_creator deps totals ~500MB at this compression level
  ZSTD_CLEVEL: 17

jobs:
  build_catatclysm:
    name: Build
    runs-on: windows-2019

    steps:
    - name: checkout repository
      uses: actions/checkout@v2
      with:
        fetch-depth: 1

    - name: Add msbuild to PATH
      uses: microsoft/setup-msbuild@v1.0.2

    - name: Use GNU tar to enable zstd for actions/cache
      run: |
        echo "Adding GNU tar to PATH"
        echo C:\Program Files\Git\usr\bin>>"%GITHUB_PATH%"
      shell: cmd

    - name: Restore artifacts, or run vcpkg, build and cache artifacts
      uses: lukka/run-vcpkg@v10.4
      id: runvcpkg
      with:
        # run-vcpkg tries to hash vcpkg.json but complans if it finds more than one.
        # That said, we also have our custom vcpkg_triplets to hash, so we keep everything the same.
        appendedCacheKey: ${{ hashFiles( 'msvc-full-features/vcpkg.json', '.github/vcpkg_triplets/**' ) }}-x64
        vcpkgDirectory: '${{ runner.workspace }}/b/vcpkg'
        # We have to use at least this version of vcpkg to include fixes for yasm-tool's
        # availability only as an x86 host tool. Keep it in sync with the builtin-baseline
        # field in vcpkg.json. Caching happens as a post-action which runs at the end of
        # the whole workflow, after vcpkg install happens during msbuild run.
        vcpkgGitCommitId: '12b7cfe6109a9d68319334b56a01c44a302a13b6'

    - name: Integrate vcpkg
      run: |
        vcpkg integrate install --vcpkg-root '${{ runner.workspace }}\b\vcpkg'

    - name: Download ccache
      uses: robinraju/release-downloader@v1.4
      with:
        repository: 'ccache/ccache'
        tag: 'v4.6.1'
        fileName: 'ccache-4.6.1-windows-x86_64.zip'

    - name: Install ccache
      id: install-ccache
      run: |
        unzip ccache-4.6.1-windows-x86_64.zip
        cp ccache-4.6.1-windows-x86_64/ccache.exe ccache-4.6.1-windows-x86_64/cl.exe
        cp ccache-4.6.1-windows-x86_64/ccache.exe ccache-4.6.1-windows-x86_64/clang-cl.exe
        mv ccache-4.6.1-windows-x86_64 ${{ env.CDDA_CCACHE_PATH }}

    - name: Get ccache vars
      id: get-vars
      run: |
        echo "datetime=$(/bin/date -u "+%Y%m%d%H%M")" >> $GITHUB_OUTPUT
        echo "ccache-path=$(echo "$APPDATA\\ccache")" >> $GITHUB_OUTPUT
      shell: bash

    - name: ccache cache files
      uses: actions/cache@v3
      with:
        path: ${{ steps.get-vars.outputs.ccache-path }}
        # double-dash after compiler is not a typo, it is to disambiguate between g++-<date> and g++-11-<date> for restore key prefix matching
        key: ccache-${{ github.ref_name }}-${{ runner.os }}-msvc--${{ steps.get-vars.outputs.datetime }}
        restore-keys: |
          ccache-master-${{ runner.os }}-msvc--

    - name: Configure ccache
      run: |
        ${{ env.CDDA_CCACHE_PATH }}\ccache.exe -M 10G
        ${{ env.CDDA_CCACHE_PATH }}\ccache.exe -s -v
        ${{ env.CDDA_CCACHE_PATH }}\ccache.exe -z

    - name: Symlink intermediates to C
      run: |
        mkdir -p "$env:TEMP\objwin"
        New-Item -ItemType SymbolicLink -Target "$env:TEMP\objwin" objwin

    - name: Build
      run: |
          msbuild -m -p:Configuration=Release -p:Platform=x64 "-target:Cataclysm-vcpkg-static;Cataclysm-test-vcpkg-static;JsonFormatter-vcpkg-static" msvc-full-features/Cataclysm-vcpkg-static.sln
          msbuild -m -p:Configuration=Release -p:Platform=x64 "-target:ObjectCreator-vcpkg-static" msvc-object_creator/ObjectCreator-vcpkg-static.sln

    - name: Post-build ccache manipulation
      if: ${{ !failure() }}
      run: |
        ${{ env.CDDA_CCACHE_PATH }}\ccache.exe -s -v
        ${{ env.CDDA_CCACHE_PATH }}\ccache.exe -M ${{ env.CCACHE_LIMIT }}
        ${{ env.CDDA_CCACHE_PATH }}\ccache.exe -c
        ${{ env.CDDA_CCACHE_PATH }}\ccache.exe -s -v

    - name: clear ccache on PRs
      if: ${{ github.ref_name != 'master' }}
      run: |
        ${{ env.CDDA_CCACHE_PATH }}\ccache.exe -C

    - name: Dump vcpkg logs if build failed
      if: failure()
      run: |
        echo =================================================
        Get-ChildItem "${{ runner.workspace }}/Cataclysm-DDA/msvc-full-features/vcpkg_installed" -Recurse
        echo =================================================
        Get-ChildItem "${{ runner.workspace }}/Cataclysm-DDA/msvc-object_creator/vcpkg_installed" -Recurse
        echo =================================================
        Get-ChildItem "${{ runner.workspace }}/b/vcpkg/buildtrees" |
        Foreach-Object {
            Get-ChildItem $_.FullName -Filter *.log |
            Foreach-Object {
              echo =================================================
              echo $_.FullName
              echo =================================================
              type $_.FullName
            }
        }

    - name: Compile .mo files for localization
      run: |
          & "C:\msys64\mingw64\bin\mingw32-make" -C lang -j2
          mkdir -p ./data/mods/TEST_DATA/lang/mo/ru/LC_MESSAGES -ErrorAction SilentlyContinue
          msgfmt -f -o ./data/mods/TEST_DATA/lang/mo/ru/LC_MESSAGES/TEST_DATA.mo ./data/mods/TEST_DATA/lang/po/ru.po

    - name: Enable GitHub Actions problem matchers
      run: |
          Write-Output "::add-matcher::build-scripts/problem-matchers/catch2.json"
          Write-Output "::add-matcher::build-scripts/problem-matchers/debugmsg.json"

    - name: Run tests
      run: |
          .\Cataclysm-test-vcpkg-static-Release-x64.exe --min-duration 0.2 --rng-seed time

    - name: Dump disk usage logs if job failed
      if: failure()
      run: |
        echo =================================================
        df
        echo =================================================
        du -sh *
        echo =================================================
        du -sh msvc-full-features/*
        echo =================================================
        du -sh msvc-object_creator/*
        echo =================================================
        du -sh tools/*
        echo =================================================
      shell: bash

    - name: Don't upload vcpkg cache on failure or PRs
      if: ${{ failure() || cancelled() || github.ref_name != 'master' }}
      run: |
        echo "RUNVCPKG_NO_CACHE=1" >> $GITHUB_ENV
      shell: bash
