# Copyright (C) Markus Franz Xaver Johannes Oberhumer
# build with "zig cc"; see https://ziglang.org/download/

name: 'Weekly CI - zigcc'
on:
  schedule: [cron: '40 4 * * 3'] # run weekly Wednesday 04:40 UTC
  workflow_dispatch:
env:
  CMAKE_REQUIRED_QUIET: OFF
  DEBIAN_FRONTEND: noninteractive
  # 2023-07-30
  ZIG_DIST_VERSION: 0.11.0-dev.4320+6f0a613b6

jobs:
  job-linux-zigcc: # uses cmake + make
    name: ${{ format('zigcc {0} {1}', matrix.zig_target, matrix.zig_pic) }}
    runs-on: ubuntu-latest
    container: 'alpine:3.18' # older versions such as alpine:3.12 also work; no-container also works
    strategy:
      fail-fast: false
      matrix:
        include:
          - { zig_target: aarch64-linux-musl }
          - { zig_target: aarch64-linux-musl, zig_pic: -fPIE }
          - { zig_target: aarch64-macos-none }
          - { zig_target: aarch64-macos.11.0-none }
          - { zig_target: aarch64-macos.12.0-none }
          - { zig_target: aarch64-macos.13.0-none }
          - { zig_target: aarch64-windows-gnu }
          - { zig_target: arm-linux-musleabihf }
          - { zig_target: arm-linux-musleabihf, zig_pic: -fPIE }
          - { zig_target: i386-linux-musl }
          - { zig_target: i386-windows-gnu }
          - { zig_target: mips-linux-musl }
          - { zig_target: mipsel-linux-musl }
          - { zig_target: powerpc-linux-musl }
          - { zig_target: powerpc64-linux-musl }
          - { zig_target: powerpc64le-linux-musl }
          - { zig_target: x86_64-linux-musl }
          - { zig_target: x86_64-linux-musl, zig_pic: -fPIE }
          - { zig_target: x86_64-macos-none }
          - { zig_target: x86_64-macos.11.0-none }
          - { zig_target: x86_64-macos.12.0-none }
          - { zig_target: x86_64-macos.13.0-none }
          - { zig_target: x86_64-windows-gnu }
    env:
      # for zig-cc wrapper scripts (see below):
      ZIG_CPPFLAGS: -DUPX_DOCTEST_CONFIG_MULTITHREADING
      ZIG_FLAGS: ${{ matrix.zig_flags }}
      ZIG_PIC: ${{ matrix.zig_pic }}
      ZIG_TARGET: ${{ matrix.zig_target }}
    steps:
      - name: 'Install Alpine Linux container packages'
        if: ${{ job.container }}
        shell: sh
        run: |
          apk update && apk upgrade && apk add bash cmake file git make tar xz
          # set PATH like in Ubuntu
          echo "PATH=$HOME/.local/bin:$PATH" >> $GITHUB_ENV
          # this seems to be needed when running in a container (beause of UID mismatch??)
          git config --global --add safe.directory '*'
      - name: 'Check out code'
        uses: actions/checkout@v3
        with: { submodules: true }
      - name: ${{ format('Install Zig {0}', env.ZIG_DIST_VERSION) }}
        shell: bash
        run: |
          # GitHub Actions magic: set "UPX_GITREV_SHORT" environment value for use in steps below
          rev=$(git rev-parse --short=7 HEAD)
          echo "UPX_GITREV_SHORT=$rev" >> $GITHUB_ENV
          # update ZIG_TARGET (i386 => x86)
          ZIG_TARGET=${ZIG_TARGET/i386-/x86-}
          echo "ZIG_TARGET=$ZIG_TARGET" >> $GITHUB_ENV
          # install zig; note that ~/.local/bin is included in the default $PATH on Ubuntu
          mkdir -p -v ~/.local/bin
          cd ~/.local/bin
          ZIG_DIST_NAME=zig-linux-x86_64-${ZIG_DIST_VERSION}
          wget -q https://ziglang.org/builds/${ZIG_DIST_NAME}.tar.xz
          ls -l ${ZIG_DIST_NAME}.tar.xz
          tar -xoJf ${ZIG_DIST_NAME}.tar.xz
          rm ${ZIG_DIST_NAME}.tar.xz
          ln -s -v ${ZIG_DIST_NAME}/zig zig
          #echo "PATH=$PATH" && which zig
          echo -n 'zig version: '; zig version
          # create wrapper scripts (needed for CMake)
          echo -e '#!/bin/sh\nexec zig ar "$@"' > zig-ar
          echo -e '#!/bin/sh\nexec zig cc  -target $ZIG_TARGET $ZIG_PIC $ZIG_FLAGS $ZIG_CPPFLAGS $ZIG_CFLAGS   "$@"' > zig-cc
          echo -e '#!/bin/sh\nexec zig c++ -target $ZIG_TARGET $ZIG_PIC $ZIG_FLAGS $ZIG_CPPFLAGS $ZIG_CXXFLAGS "$@"' > zig-cxx
          echo -e '#!/bin/sh\nexec zig ranlib "$@"' > zig-ranlib
          chmod +x zig-ar zig-cc zig-cxx zig-ranlib
          ls -la; head zig-ar zig-cc zig-cxx zig-ranlib
      - name: ${{ format('Build Release with zig-cc -target {0} {1}', env.ZIG_TARGET, env.ZIG_PIC) }}
        run: |
          mkdir -p build/zig/${ZIG_TARGET}${ZIG_PIC}/release
          cd       build/zig/${ZIG_TARGET}${ZIG_PIC}/release
          cmake ../../../.. -DCMAKE_BUILD_TYPE=Release -DCMAKE_AR=$HOME/.local/bin/zig-ar -DCMAKE_C_COMPILER=zig-cc -DCMAKE_CXX_COMPILER=zig-cxx -DCMAKE_RANLIB=$HOME/.local/bin/zig-ranlib $EXTRA_CMAKE_CONFIG_FLAGS_RELEASE
          cmake --build . --config Release --parallel --verbose
          file ./upx*
      - name: ${{ format('Build Debug with zig-cc -target {0} {1}', env.ZIG_TARGET, env.ZIG_PIC) }}
        run: |
          mkdir -p build/zig/${ZIG_TARGET}${ZIG_PIC}/debug
          cd       build/zig/${ZIG_TARGET}${ZIG_PIC}/debug
          cmake ../../../.. -DCMAKE_BUILD_TYPE=Debug -DCMAKE_AR=$HOME/.local/bin/zig-ar -DCMAKE_C_COMPILER=zig-cc -DCMAKE_CXX_COMPILER=zig-cxx -DCMAKE_RANLIB=$HOME/.local/bin/zig-ranlib $EXTRA_CMAKE_CONFIG_FLAGS_DEBUG
          cmake --build . --config Debug --parallel --verbose
          file ./upx*
      - name: ${{ format('Make artifact from upx-{0}-{1}', github.ref_name, env.UPX_GITREV_SHORT) }}
        shell: bash
        run: |
          N=$(echo "upx-${GITHUB_REF_NAME}-${GITHUB_SHA:0:7}-weekly-ci-zigcc-${{ matrix.zig_target }}${ZIG_PIC}" | sed 's/[^0-9a-zA-Z_.-]/-/g')
          mkdir -p "tmp/artifact/$N"
          (cd build && shopt -s nullglob && cp -ai --parents */upx{,.exe} */*/*/upx{,.exe} "../tmp/artifact/$N")
          (cd tmp/artifact && tar --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 install tests'
        if: ${{ contains(matrix.zig_target, '-linux') }}
        run: |
          (cd build/zig/${ZIG_TARGET}${ZIG_PIC}/release && DESTDIR=$PWD/Install-with-cmake cmake --install .)
          (cd build/zig/${ZIG_TARGET}${ZIG_PIC}/release && DESTDIR=$PWD/Install-with-make  make install)

# vim:set ts=2 sw=2 et:
