# Copyright (C) Markus Franz Xaver Johannes Oberhumer
# BS BuildSystem: build on Windows with 'cmake -G "Unix Makefiles"'

name: 'Weekly CI BS - cmake Windows make'
on:
  schedule: [cron: '20 1 * * 3'] # run weekly Wednesday 01:20 UTC
  workflow_dispatch:
env:
  CMAKE_REQUIRED_QUIET: OFF
  DEBIAN_FRONTEND: noninteractive
  UPX_CMAKE_BUILD_FLAGS: --verbose
  UPX_CMAKE_CONFIG_FLAGS: -G "Unix Makefiles"

jobs:
  job-cmake-windows-make: # uses cmake + make
    if: github.repository_owner == 'upx'
    strategy:
      fail-fast: false
      matrix:
        include:
# INFO: the following don't work with GNU make (probably some quoting problems with spaces or backslashes)
#   => use Ninja generator instead; see weekly-ci-bs-cmake-windows-ninja.yml
#         # cl (msvc)
#         - { os: windows-2019, cc: cl, cxx: cl, vsversion: 2019, arch: amd64 }
#         - { os: windows-2019, cc: cl, cxx: cl, vsversion: 2019, arch: amd64_arm64 }
#         - { os: windows-2019, cc: cl, cxx: cl, vsversion: 2019, arch: amd64_x86 }
#         - { os: windows-2022, cc: cl, cxx: cl, vsversion: 2022, arch: amd64 }
#         - { os: windows-2022, cc: cl, cxx: cl, vsversion: 2022, arch: amd64_arm64 }
#         - { os: windows-2022, cc: cl, cxx: cl, vsversion: 2022, arch: amd64_x86 }
#         # clang-cl
#         - { os: windows-2022, cc: clang-cl, cxx: clang-cl, vsversion: 2022, arch: amd64 }
          # clang
          - { os: windows-2022, cc: clang, cxx: 'clang++', arch: amd64, xflags: '-static' }
          # gcc (mingw-gcc)
          - { os: windows-2022, cc: gcc, cxx: 'g++', arch: amd64, xflags: '-static' }
    env:
      CC: ${{ matrix.cc }} ${{ matrix.xflags }}
      CXX: ${{ matrix.cxx }} ${{ matrix.xflags }}
    name: ${{ format('{0} {1} {2}', matrix.cc, matrix.vsversion, matrix.arch) }}
    runs-on: ${{ matrix.os }}
    steps:
      - name: 'Check out code'
        uses: actions/checkout@v4
        with: { submodules: true }
      - name: 'Set up Developer Command Prompt'
        if: matrix.vsversion
        uses: ilammy/msvc-dev-cmd@v1
        with:
          vsversion: ${{ matrix.vsversion }}
          arch: ${{ matrix.arch }}
      - name: 'Init environment'
        shell: bash
        run: |
          xtarget="windows-make-${{ matrix.cc }}-${{ matrix.vsversion }}-${{ matrix.arch}}"
          echo "xtarget=$xtarget" >> $GITHUB_ENV
      - name: 'Build cmake Make Debug'
        shell: bash
        run: |
          make UPX_XTARGET=$xtarget xtarget/debug
          ls -l build/xtarget/$xtarget/debug/upx.exe
          file  build/xtarget/$xtarget/debug/upx.exe || true
      - name: 'Build cmake Make Release'
        shell: bash
        run: |
          make UPX_XTARGET=$xtarget xtarget/release
          ls -l build/xtarget/$xtarget/release/upx.exe
          file  build/xtarget/$xtarget/release/upx.exe || true
      - name: 'Make artifact'
        shell: bash
        run: |
          N=$(echo "upx-${GITHUB_REF_NAME}-${GITHUB_SHA:0:7}-weekly-ci-make-${xtarget}" | sed 's/[^0-9a-zA-Z_.-]/-/g')
          mkdir -p "tmp/artifact/$N"
          (cd build && cp -ai --parents xtarget/*/*/upx.exe "../tmp/artifact/$N")
          # GitHub Actions magic: set "artifact_name" environment value for use in next step
          echo "artifact_name=$N" >> $GITHUB_ENV
      - name: ${{ format('Upload artifact {0}', env.artifact_name) }}
        uses: actions/upload-artifact@v3
        with:
          name: ${{ env.artifact_name }}
          path: tmp/artifact
      - name: 'Run basic tests'
        if: ${{ matrix.arch != 'amd64_arm64' }}
        shell: bash
        run: |
          ctest --test-dir build/xtarget/$xtarget/debug
          ctest --test-dir build/xtarget/$xtarget/release
      - name: 'Run install tests'
        shell: bash
        run: |
          env DESTDIR=./Install-debug   cmake --install build/xtarget/$xtarget/debug
          env DESTDIR=./Install-release cmake --install build/xtarget/$xtarget/release
