# Copyright (C) Markus Franz Xaver Johannes Oberhumer
# CC CompilationCheck: test llvm-mingw

# see https://github.com/mstorsjo/llvm-mingw

name: 'Weekly CI CC - llvm-mingw'
on:
  schedule: [cron: '30 2 * * 3'] # run weekly Wednesday 02:30 UTC
  workflow_dispatch:
env:
  CMAKE_REQUIRED_QUIET: OFF
  DEBIAN_FRONTEND: noninteractive

jobs:
  job-llvm-mingw: # uses cmake + make
    if: github.repository_owner == 'upx'
    strategy:
      fail-fast: false
      matrix:
        include:
          - name: llvm-mingw-20230614-msvcrt
            llvm_version: 16.0.6
            url: 'https://github.com/mstorsjo/llvm-mingw/releases/download/20230614/llvm-mingw-20230614-msvcrt-ubuntu-20.04-x86_64.tar.xz'
          - name: llvm-mingw-20230614-ucrt
            llvm_version: 16.0.6
            url: 'https://github.com/mstorsjo/llvm-mingw/releases/download/20230614/llvm-mingw-20230614-ucrt-ubuntu-20.04-x86_64.tar.xz'
          - name: llvm-mingw-20231003-msvcrt
            llvm_version: 17.0.2
            url: 'https://github.com/mstorsjo/llvm-mingw/releases/download/20231003/llvm-mingw-20231003-msvcrt-ubuntu-20.04-x86_64.tar.xz'
          - name: llvm-mingw-20231003-ucrt
            llvm_version: 17.0.2
            url: 'https://github.com/mstorsjo/llvm-mingw/releases/download/20231003/llvm-mingw-20231003-ucrt-ubuntu-20.04-x86_64.tar.xz'
    name: ${{ format('{0} {1}', matrix.name, matrix.llvm_version) }}
    runs-on: ubuntu-latest
    steps:
      - name: 'Check out code'
        uses: actions/checkout@v4
        with: { submodules: true }
      - name: ${{ format('Install {0} {1}', matrix.name, matrix.llvm_version) }}
        run: |
          mkdir -p -v ~/.local/bin
          cd ~/.local/bin
          wget -q '${{ matrix.url }}'
          ls -l ./llvm-mingw*.tar.*
          tar -xoaf ./llvm-mingw*.tar.*
          rm ./llvm-mingw*.tar.*
          # update PATH
          cd ./llvm-mingw*/bin
          echo "PATH=$PWD:$PATH" >> $GITHUB_ENV
      - name: 'Build clang aarch64'
        run: |
          export CC="aarch64-w64-mingw32-clang -static" CXX="aarch64-w64-mingw32-clang++ -static"
          make UPX_XTARGET=aarch64-w64-mingw32-clang xtarget/debug xtarget/release
      - name: 'Build clang armv7'
        run: |
          export CC="armv7-w64-mingw32-clang -static" CXX="armv7-w64-mingw32-clang++ -static"
          make UPX_XTARGET=armv7-w64-mingw32-clang xtarget/debug xtarget/release
      - name: 'Build clang i686'
        run: |
          export CC="i686-w64-mingw32-clang -static" CXX="i686-w64-mingw32-clang++ -static"
          make UPX_XTARGET=i686-w64-mingw32-clang xtarget/debug xtarget/release
      - name: 'Build clang x86_64'
        run: |
          export CC="x86_64-w64-mingw32-clang -static" CXX="x86_64-w64-mingw32-clang++ -static"
          make UPX_XTARGET=x86_64-w64-mingw32-clang xtarget/debug xtarget/release
      - name: 'Make artifact'
        run: |
          N=$(echo "upx-${GITHUB_REF_NAME}-${GITHUB_SHA:0:7}-weekly-ci-${{ matrix.name }}-${{ matrix.llvm_version }}" | sed 's/[^0-9a-zA-Z_.-]/-/g')
          mkdir -p "tmp/artifact/$N"
          (cd build && cp -ai --parents */*/*/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
