# Copyright (C) Markus Franz Xaver Johannes Oberhumer
# CC CompilationCheck: test various clang and gcc versions

# Build under various Alpine Linux versions with clang and gcc, and
# also test building with C++20, C++23 and LTO.

# And also uses a subdirectory "upx with space" that contains whitespace in order
# to detect possible quoting issues.

# info: Alpine Linux 3.9 (released Jan 2019) has clang-5, cmake-3.13.0
#   and gcc-8, which nicely matches our minimal build requirements

name: 'Weekly CI CC - Alpine Linux C++23'
on:
  schedule: [cron: '00 2 * * 3'] # run weekly Wednesday 02:00 UTC
  workflow_dispatch:
env:
  CMAKE_REQUIRED_QUIET: OFF
  DEBIAN_FRONTEND: noninteractive
  UPX_CMAKE_CONFIG_FLAGS: -DCMAKE_VERBOSE_MAKEFILE=ON
  UPX_TESTSUITE_LEVEL: 4

jobs:
  job-alpine-cmake: # uses cmake + make
    if: github.repository_owner == 'upx'
    strategy:
      fail-fast: false
      matrix:
        include:
          - { container: 'alpine:3.9' , use_cxx20: 0, use_cxx2b: 0, use_lto: 0 } # Jan 2019: C++17; clang-5 & gcc-8
          - { container: 'alpine:3.10', use_cxx20: 0, use_cxx2b: 0, use_lto: 0 }
          - { container: 'alpine:3.11', use_cxx20: 0, use_cxx2b: 0, use_lto: 0 }
          - { container: 'alpine:3.12', use_cxx20: 0, use_cxx2b: 0, use_lto: 0 }
          - { container: 'alpine:3.13', use_cxx20: 1, use_cxx2b: 0, use_lto: 0 } # Jan 2021: C++20; clang-10 & gcc-10
          - { container: 'alpine:3.14', use_cxx20: 1, use_cxx2b: 0, use_lto: 0 }
          - { container: 'alpine:3.15', use_cxx20: 1, use_cxx2b: 0, use_lto: 1 } # Nov 2021: LTO;   clang-12 & gcc-10 & binutils-2.37
          - { container: 'alpine:3.16', use_cxx20: 1, use_cxx2b: 1, use_lto: 1 } # May 2022: C++2b; clang-13 & gcc-11
          - { container: 'alpine:3.17', use_cxx20: 1, use_cxx2b: 1, use_lto: 1 }
          - { container: 'alpine:3.18', use_cxx20: 1, use_cxx2b: 1, use_lto: 1 }
          - { container: 'alpine:edge', use_cxx20: 1, use_cxx2b: 1, use_lto: 1 }
          - { container: 'i386/alpine:edge', use_cxx20: 1, use_cxx2b: 1, use_lto: 1 }
    name: ${{ format('container {0}', matrix.container) }}
    runs-on: ubuntu-latest
    container: ${{ matrix.container }}
    steps:
      - name: ${{ format('Install packages {0}', matrix.container) }}
        run: |
          apk update && apk upgrade && apk add clang cmake g++ git make
          case ${{ matrix.container }} in
            # clang-dev is needed on Alpine versions <= 3.11 for clang headers like <emmintrin.h>
            *:3.[0-9] | *:3.1[0-1]) apk add clang-dev ;;
          esac
          case ${{ matrix.container }}-${{ matrix.use_lto }} in
            # llvm-dev is needed on Alpine versions <= 3.18 for -flto
            *:3.[0-9]-1 | *:3.1[0-8]-1) apk add llvm-dev ;;
          esac
          # set environment vars
          x="$(apk list -I "$(apk info -Wq "$(which clang)")")"; echo "clang_package=${x%% *}" >> $GITHUB_ENV
          x="$(apk list -I "$(apk info -Wq "$(which gcc)")")"; echo "gcc_package=${x%% *}" >> $GITHUB_ENV
          N=$(echo "upx-${GITHUB_REF_NAME}-${GITHUB_SHA:0:7}-weekly-ci-alpine-${{ matrix.container }}" | sed 's/[^0-9a-zA-Z_.-]/-/g')
          echo "artifact_name=$N" >> $GITHUB_ENV
          # create user upx:upx 2000:2000 for file system tests below ("sudo")
          adduser upx -u 2000 -D && cd /home/upx && chmod 00700 . && chown -R upx:upx .
      - name: ${{ format('Check out UPX {0} source code', github.ref_name) }}
        run: |
          git clone --branch "$GITHUB_REF_NAME" --depth 1 https://github.com/upx/upx "upx with space"
          git -C "upx with space" submodule update --init

      # build with default C11 and C++17
      - name: ${{ format('Build clang Release with {0}', env.clang_package) }}
        run: 'make -C "upx with space" UPX_XTARGET=clang-static CC="clang -static" CXX="clang++ -static"'
      - name: ${{ format('Build clang Debug with {0}', env.clang_package) }}
        run: 'make -C "upx with space" UPX_XTARGET=clang-static CC="clang -static" CXX="clang++ -static" xtarget/debug'
      - name: ${{ format('Build gcc Release with {0}', env.gcc_package) }}
        run: 'make -C "upx with space" UPX_XTARGET=gcc-static   CC="gcc -static"   CXX="g++ -static"'
      - name: ${{ format('Build gcc Debug with {0}', env.gcc_package) }}
        run: 'make -C "upx with space" UPX_XTARGET=gcc-static   CC="gcc -static"   CXX="g++ -static" xtarget/debug'

      # build with C17 and C++20
      - name: ${{ format('Build clang C++20 Release with {0}', env.clang_package) }}
        if: matrix.use_cxx20
        run: |
          export UPX_CONFIG_DISABLE_C_STANDARD=ON UPX_CONFIG_DISABLE_CXX_STANDARD=ON
          make -C "upx with space" UPX_XTARGET=clang-static-cxx20 CC="clang -static -std=gnu17" CXX="clang++ -static -std=gnu++20"
      - name: ${{ format('Build clang C++20 Debug with {0}', env.clang_package) }}
        if: matrix.use_cxx20
        run: |
          export UPX_CONFIG_DISABLE_C_STANDARD=ON UPX_CONFIG_DISABLE_CXX_STANDARD=ON
          make -C "upx with space" UPX_XTARGET=clang-static-cxx20 CC="clang -static -std=gnu17" CXX="clang++ -static -std=gnu++20" xtarget/debug
      - name: ${{ format('Build gcc C++20 Release with {0}', env.gcc_package) }}
        if: matrix.use_cxx20
        run: |
          export UPX_CONFIG_DISABLE_C_STANDARD=ON UPX_CONFIG_DISABLE_CXX_STANDARD=ON
          make -C "upx with space" UPX_XTARGET=gcc-static-cxx20   CC="gcc -static -std=gnu17"   CXX="g++ -static -std=gnu++20"
      - name: ${{ format('Build gcc C++20 Debug with {0}', env.gcc_package) }}
        if: matrix.use_cxx20
        run: |
          export UPX_CONFIG_DISABLE_C_STANDARD=ON UPX_CONFIG_DISABLE_CXX_STANDARD=ON
          make -C "upx with space" UPX_XTARGET=gcc-static-cxx20   CC="gcc -static -std=gnu17"   CXX="g++ -static -std=gnu++20" xtarget/debug

      # build with C23 and C++23
      - name: ${{ format('Build clang C++23 Release with {0}', env.clang_package) }}
        if: matrix.use_cxx23 || matrix.use_cxx2b
        run: |
          export UPX_CONFIG_DISABLE_C_STANDARD=ON UPX_CONFIG_DISABLE_CXX_STANDARD=ON
          a=gnu23; b=gnu++23; if test "X${{ matrix.use_cxx2b }}" = X1; then a=gnu2x; b=gnu++2b; fi
          make -C "upx with space" UPX_XTARGET=clang-static-cxx23 CC="clang -static -std=$a" CXX="clang++ -static -std=$b"
      - name: ${{ format('Build clang C++23 Debug with {0}', env.clang_package) }}
        if: matrix.use_cxx23 || matrix.use_cxx2b
        run: |
          export UPX_CONFIG_DISABLE_C_STANDARD=ON UPX_CONFIG_DISABLE_CXX_STANDARD=ON
          a=gnu23; b=gnu++23; if test "X${{ matrix.use_cxx2b }}" = X1; then a=gnu2x; b=gnu++2b; fi
          make -C "upx with space" UPX_XTARGET=clang-static-cxx23 CC="clang -static -std=$a" CXX="clang++ -static -std=$b" xtarget/debug
      - name: ${{ format('Build gcc C++23 Release with {0}', env.gcc_package) }}
        if: matrix.use_cxx23 || matrix.use_cxx2b
        run: |
          export UPX_CONFIG_DISABLE_C_STANDARD=ON UPX_CONFIG_DISABLE_CXX_STANDARD=ON
          a=gnu23; b=gnu++23; if test "X${{ matrix.use_cxx2b }}" = X1; then a=gnu2x; b=gnu++2b; fi
          make -C "upx with space" UPX_XTARGET=gcc-static-cxx23   CC="gcc -static -std=$a"   CXX="g++ -static -std=$b"
      - name: ${{ format('Build gcc C++23 Debug with {0}', env.gcc_package) }}
        if: matrix.use_cxx23 || matrix.use_cxx2b
        run: |
          export UPX_CONFIG_DISABLE_C_STANDARD=ON UPX_CONFIG_DISABLE_CXX_STANDARD=ON
          a=gnu23; b=gnu++23; if test "X${{ matrix.use_cxx2b }}" = X1; then a=gnu2x; b=gnu++2b; fi
          make -C "upx with space" UPX_XTARGET=gcc-static-cxx23   CC="gcc -static -std=$a"   CXX="g++ -static -std=$b" xtarget/debug

      # build with -flto
      - name: ${{ format('Build clang LTO Release with {0}', env.clang_package) }}
        if: matrix.use_lto
        run: |
          make -C "upx with space" UPX_XTARGET=clang-static-cxxlto CC="clang -static -flto" CXX="clang++ -static -flto"
      - name: ${{ format('Build clang LTO Debug with {0}', env.clang_package) }}
        if: matrix.use_lto
        run: |
          make -C "upx with space" UPX_XTARGET=clang-static-cxxlto CC="clang -static -flto" CXX="clang++ -static -flto" xtarget/debug
      - name: ${{ format('Build gcc LTO Release with {0}', env.gcc_package) }}
        if: matrix.use_lto
        run: |
          make -C "upx with space" UPX_XTARGET=gcc-static-cxxlto   CC="gcc -static -flto"   CXX="g++ -static -flto"
      - name: ${{ format('Build gcc LTO Debug with {0}', env.gcc_package) }}
        if: matrix.use_lto
        run: |
          make -C "upx with space" UPX_XTARGET=gcc-static-cxxlto   CC="gcc -static -flto"   CXX="g++ -static -flto" xtarget/debug

      - { name: 'Strip release binaries', run: 'strip -p --strip-unneeded "upx with space"/build/*/*/release/upx' }

      - 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 with space*/build/*/*/*/upx'

      - name: 'Run install tests'
        run: |
          (cd "upx with space"/build/xtarget/clang-static/debug   && DESTDIR="$PWD/Install with cmake" cmake --install .)
          (cd "upx with space"/build/xtarget/clang-static/debug   && DESTDIR="$PWD/Install with make"  make install)
          (cd "upx with space"/build/xtarget/clang-static/release && DESTDIR="$PWD/Install with cmake" cmake --install .)
          (cd "upx with space"/build/xtarget/clang-static/release && DESTDIR="$PWD/Install with make"  make install)
          (cd "upx with space"/build/xtarget/gcc-static/debug     && DESTDIR="$PWD/Install with cmake" cmake --install .)
          (cd "upx with space"/build/xtarget/gcc-static/debug     && DESTDIR="$PWD/Install with make"  make install)
          (cd "upx with space"/build/xtarget/gcc-static/release   && DESTDIR="$PWD/Install with cmake" cmake --install .)
          (cd "upx with space"/build/xtarget/gcc-static/release   && DESTDIR="$PWD/Install with make"  make install)

      - { name: 'Run basic tests clang Debug',   run: 'make -C "upx with space"/build/xtarget/clang-static/debug test' }
      - { name: 'Run basic tests clang Release', run: 'make -C "upx with space"/build/xtarget/clang-static/release test' }
      - { name: 'Run basic tests gcc Debug',     run: 'make -C "upx with space"/build/xtarget/gcc-static/debug test' }
      - { name: 'Run basic tests gcc Release',   run: 'make -C "upx with space"/build/xtarget/gcc-static/release test' }

      - name: 'Run basic tests C++20, C++23 and LTO'
        if: matrix.use_cxx20 || matrix.use_cxx23 || matrix.use_cxx2b || matrix.use_lto
        run: |
          for dir in "upx with space"/build/xtarget/*-cxx*/*; do
             echo "===== $dir"
             make -C "$dir" test
          done

      - name: 'Run file system test suite (busybox)'
        run: |
          apk add bash sudo
          testsuite="$(readlink -fn "upx with space"/misc/testsuite/test_symlinks.sh)"
          cd "upx with space"/build/xtarget/gcc-static/release
          # IMPORTANT: do NOT run as user root!
          chmod a+w . && sudo -u upx bash "$testsuite"

      # test suite
      - name: ${{ format('Run test suite level {0}', env.UPX_TESTSUITE_LEVEL) }}
        run: |
          # testsuite needs bash and working "readlink -en" and "sha256sum -b"
          apk add bash coreutils
          # use a directory that contains whitespace to detect possible quoting issues
          git clone --depth=1 https://github.com/upx/upx-testsuite "upx-testsuite with space"
          export upx_testsuite_SRCDIR="$(readlink -fn "upx-testsuite with space")"
          testsuite_1="$(readlink -fn "upx with space"/misc/testsuite/upx_testsuite_1.sh)"
          (cd "upx with space"/build/xtarget/gcc-static/release && upx_exe=./upx bash "$testsuite_1")

      - name: 'Run file system test suite (coreutils)'
        run: |
          apk add bash coreutils sudo
          testsuite="$(readlink -fn "upx with space"/misc/testsuite/test_symlinks.sh)"
          cd "upx with space"/build/xtarget/gcc-static/release
          # IMPORTANT: do NOT run as user root!
          chmod a+w . && sudo -u upx bash "$testsuite"
