# Copyright (C) Markus Franz Xaver Johannes Oberhumer
# build with 'cmake -G Xcode'

name: 'Weekly CI - cmake macOS Xcode'
on:
  schedule: [cron: '20 4 * * 3'] # run weekly Wednesday 04:20 UTC
  workflow_dispatch:
env:
  CMAKE_REQUIRED_QUIET: OFF
  DEBIAN_FRONTEND: noninteractive

jobs:
  job-cmake-macos-xcode: # uses cmake + xcodebuild
    name: ${{ format('{0} cmake Xcode', matrix.os) }}
    runs-on: ${{ matrix.os }}
    strategy:
      fail-fast: false
      matrix:
        include:
          - { os: macos-11, run_test: true }
          - { os: macos-12, run_test: true }
          - { os: macos-13, run_test: false } # TODO: enable once UPX fully works on macos-13
    steps:
      - name: 'Check out code'
        uses: actions/checkout@v3
        with: { submodules: true }
      - { name: 'Config cmake Xcode',        run: 'cmake -S . -B build/xcode -G Xcode' }
      - { name: 'Build cmake Xcode default', run: 'cmake --build build/xcode' }
      - { name: 'Build cmake Xcode Debug',   run: 'cmake --build build/xcode --config Debug' }
      - { name: 'Build cmake Xcode Release', run: 'cmake --build build/xcode --config Release' }
      - name: 'Make artifact'
        run: |
          N=$(echo "upx-${GITHUB_REF_NAME}-${GITHUB_SHA:0:7}-weekly-ci-xcode-${{ matrix.os }}" | sed 's/[^0-9a-zA-Z_.-]/-/g')
          mkdir -p "tmp/artifact/$N"
          (cd build && rsync -R -a */*/upx "../tmp/artifact/$N/")
          (cd tmp/artifact && gtar --sort=name -czf "$N.tar.gz" "$N" && rm -rf "./$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.run_test }}
        run: |
          ctest --test-dir build/xcode -C Debug
          ctest --test-dir build/xcode -C Release
      - name: 'Run install tests'
        run: |
          (cd build/xcode && DESTDIR=$PWD/Install-default cmake --install .)
          (cd build/xcode && DESTDIR=$PWD/Install-debug   cmake --install . --config Debug)
          (cd build/xcode && DESTDIR=$PWD/Install-release cmake --install . --config Release)
          ls -l build/xcode/Install-*/usr/local/bin/upx
