# Copyright (C) Markus Franz Xaver Johannes Oberhumer
# test minimal number of packages required for building UPX under Alpine Linux
#   clang: apk add clang cmake make
#   gcc:   apk add cmake g++ make

name: 'Test - Minimal Alpine build'
on: [workflow_dispatch]
env:
  CMAKE_REQUIRED_QUIET: OFF
  DEBIAN_FRONTEND: noninteractive

jobs:
  job-alpine-clang: # uses cmake + make
    strategy: { matrix: { container: ['alpine:edge','i386/alpine:edge'] } }
    name: ${{ format('clang {0}', matrix.container) }}
    runs-on: ubuntu-latest
    container: ${{ matrix.container }}
    steps:
      - name: ${{ format('Build clang {0}', matrix.container) }}
        run: |
          apk update && apk upgrade && apk add clang cmake make
          wget https://github.com/upx/upx/releases/download/v4.0.2/upx-4.0.2-src.tar.xz
          tar -xoaf upx-*-src.tar.xz
          cd upx-*-src
          make    build/debug   CC="clang -static" CXX="clang++ -static"
          make    build/release CC="clang -static" CXX="clang++ -static"
          make -C build/debug   test
          make -C build/release test
          make -C build/release install DESTDIR="$PWD/Install"
          N=$(echo "upx-test-alpine-linux-clang-${{ matrix.container }}" | sed 's/[^0-9a-zA-Z_.-]/-/g')
          echo "artifact_name=$N" >> $GITHUB_ENV
      - name: ${{ format('Upload artifact {0}', env.artifact_name) }}
        if: ${{ !startsWith(matrix.container, 'i386/') }} # i386: missing nodejs on host
        uses: actions/upload-artifact@v3
        with:
          name: ${{ env.artifact_name }}
          path: 'upx*/build/*/upx'

  job-alpine-gcc: # uses cmake + make
    strategy: { matrix: { container: ['alpine:edge','i386/alpine:edge'] } }
    name: ${{ format('gcc {0}', matrix.container) }}
    runs-on: ubuntu-latest
    container: ${{ matrix.container }}
    steps:
      - name: ${{ format('Build gcc {0}', matrix.container) }}
        run: |
          apk update && apk upgrade && apk add cmake g++ make
          wget https://github.com/upx/upx/releases/download/v4.0.2/upx-4.0.2-src.tar.xz
          tar -xoaf upx-*-src.tar.xz
          cd upx-*-src
          make    build/debug   CC="gcc -static" CXX="g++ -static"
          make    build/release CC="gcc -static" CXX="g++ -static"
          make -C build/debug   test
          make -C build/release test
          make -C build/release install DESTDIR="$PWD/Install"
          N=$(echo "upx-test-alpine-linux-gcc-${{ matrix.container }}" | sed 's/[^0-9a-zA-Z_.-]/-/g')
          echo "artifact_name=$N" >> $GITHUB_ENV
      - name: ${{ format('Upload artifact {0}', env.artifact_name) }}
        if: ${{ !startsWith(matrix.container, 'i386/') }} # i386: missing nodejs on host
        uses: actions/upload-artifact@v3
        with:
          name: ${{ env.artifact_name }}
          path: 'upx*/build/*/upx'

  job-alpine-by-hand: # uses a POSIX-compliant shell
    # ...and also uses a subdirectory "upx with space" in order to detect possible quoting issues
    # ...and also uses ccache as we are running the same build-script again and again
    if: ${{ true }}
    strategy: { matrix: { container: ['alpine:3.9','alpine:edge'] } }
    name: ${{ format('gcc by-hand {0}', matrix.container) }}
    runs-on: ubuntu-latest
    container: ${{ matrix.container }}
    steps:
      - name: ${{ format('Install packages {0}', matrix.container) }}
        run: |
          # install ccache, g++, git and various POSIX shells
          shells="bash dash loksh mksh zsh"
          case ${{ matrix.container }} in
            *:edge) shells="$shells oksh yash" ;;
          esac
          echo "installing shells: $shells"
          apk update && apk upgrade && apk add ccache g++ git $shells
          # enable ccache
          echo -e "CC=ccache gcc\nCXX=ccache g++ -std=gnu++17" >> $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
          path: 'upx with space'
      - name: 'Build by-hand with bash'
        run: 'bash "./upx with space/misc/scripts/build_upx_by_hand.sh"'
      - name: 'Build by-hand with bash --posix'
        run: 'bash --posix "./upx with space/misc/scripts/build_upx_by_hand.sh"'
      - name: 'Build by-hand with busybox ash'
        run: 'busybox ash "./upx with space/misc/scripts/build_upx_by_hand.sh"'
      - name: 'Build by-hand with busybox sh'
        run: 'busybox sh "./upx with space/misc/scripts/build_upx_by_hand.sh"'
      - name: 'Build by-hand with dash'
        run: 'dash "./upx with space/misc/scripts/build_upx_by_hand.sh"'
      - name: 'Build by-hand with loksh'
        run: 'ksh "./upx with space/misc/scripts/build_upx_by_hand.sh"'
      - name: 'Build by-hand with loksh -o posix'
        run: 'ksh -o posix "./upx with space/misc/scripts/build_upx_by_hand.sh"'
      - name: 'Build by-hand with loksh -o sh'
        run: 'ksh -o sh "./upx with space/misc/scripts/build_upx_by_hand.sh"'
      - name: 'Build by-hand with mksh'
        run: 'mksh "./upx with space/misc/scripts/build_upx_by_hand.sh"'
      - name: 'Build by-hand with mksh -o posix'
        run: 'mksh -o posix "./upx with space/misc/scripts/build_upx_by_hand.sh"'
      - name: 'Build by-hand with mksh -o sh'
        run: 'mksh -o sh "./upx with space/misc/scripts/build_upx_by_hand.sh"'
      - name: 'Build by-hand with oksh'
        if: ${{ contains(matrix.container, ':edge') }}
        run: 'oksh "./upx with space/misc/scripts/build_upx_by_hand.sh"'
      - name: 'Build by-hand with oksh -o posix'
        if: ${{ contains(matrix.container, ':edge') }}
        run: 'oksh -o posix "./upx with space/misc/scripts/build_upx_by_hand.sh"'
      - name: 'Build by-hand with oksh -o sh'
        if: ${{ contains(matrix.container, ':edge') }}
        run: 'oksh -o sh "./upx with space/misc/scripts/build_upx_by_hand.sh"'
      - name: 'Build by-hand with yash'
        if: ${{ contains(matrix.container, ':edge') }}
        run: 'yash "./upx with space/misc/scripts/build_upx_by_hand.sh"'
      - name: 'Build by-hand with zsh'
        run: 'zsh "./upx with space/misc/scripts/build_upx_by_hand.sh"'
      - name: 'Build by-hand with zsh --emulate ksh'
        run: 'zsh --emulate ksh "./upx with space/misc/scripts/build_upx_by_hand.sh"'
      - name: 'Build by-hand with zsh --emulate sh'
        run: 'zsh --emulate sh "./upx with space/misc/scripts/build_upx_by_hand.sh"'
      - name: 'Show ccache stats'
        run: |
          ccache -s
          ccache -p
