name: vm2-ci

on:
  - push
  - pull_request

jobs:
  vm2-ci:
    runs-on: ubuntu-latest

    strategy:
      fail-fast: false
      matrix:
        os:
          - name: freebsd
            version: "14.0"
          - name: omnios
            version: "r151046"
          - name: solaris
            version: "11.4"
        compiler:
          - clang
          - gcc
        strictness:
          - "normal"
          - "strict"
        exclude:
          - os: {name: omnios}
            compiler: clang
          - os: {name: solaris}
            compiler: clang

    steps:

      - name: Configure environment
        run: |
          echo "Nothing to do for ${{ matrix.os.name }}"

      - name: Install packages
        run: |
          echo "The actual install takes place in the VM in the build step"

          tee ~/work/install_freebsd_deps.sh <<EOF
          #!/bin/sh
          pkg install -y \
            bison ccache gmake \
            $(case ${{matrix.compiler }} in
              (gcc) echo ${{matrix.compiler}} ;; esac) \
            libslang2 \
            cairo \
            freeimage \
            libgd \
            mariadb-connector-c \
            postgresql16-client \
            qt5-buildtools qt5-gui
          EOF

          tee ~/work/install_omnios_deps.sh <<EOF
          #!/usr/xpg4/bin/sh
          set -ex
          pkg install \
            build-essential \
            ccache
          pkg list
          EOF

          tee ~/work/install_solaris_deps.sh <<EOF
          #!/usr/xpg4/bin/sh
          set -ex
          pkgadd -n -d http://get.opencsw.org/now CSWpkgutil || true  # already installed by action
          /opt/csw/bin/pkgutil -U
          /opt/csw/bin/pkgutil -y -i \
            ccache \
            gmake \
            libcurl_dev \
            libssl_dev
          /opt/csw/bin/pkgutil -l
          EOF

          chmod a+x ~/work/install_*_deps.sh

      - name: Checkout code
        uses: actions/checkout@v3
        with:
          ref: ${{ github.event.client_payload.branch }}

      - run: pwd
      - run: ls

      - name: Prepare ccache using action
        uses: hendrikmuhs/ccache-action@v1.2.10
        with:
          key: ${{ matrix.os.name }}-${{ matrix.os.version }}-${{ matrix.compiler }}-${{ matrix.strictness }}-ci
          max-size: "32M"

      - name: Configure build shell
        run: |
          set -ex
          case ${{matrix.os.name }} in
            *bsd) _cpus="sysctl -n hw.ncpu" ;;
            omnios|solaris) _cpus="psrinfo -p" _add_path="/usr/sfw/bin" ;;
            *) _cpus="echo 3" ;;
          esac
          HB_USER_CFLAGS=""
          HB_USER_LDFLAGS=""
          case ${{matrix.strictness}} in
            normal) ;;
            strict) case ${{matrix.compiler}} in
                      (gcc) HB_USER_CFLAGS="$HB_USER_CFLAGS -Werror" ;;
                      (*)   HB_USER_CFLAGS="$HB_USER_CFLAGS -Werror" ;;
                    esac ;;
          esac
          tee ./.bashrc <<EOENV

          export HB_BUILD_TEST="${{matrix.strictness}}"
          export HB_BUILD_VERBOSE="yes"
          export HBMK_WITH_SQLITE3="local"
          export HB_USER_CFLAGS="$HB_USER_CFLAGS"
          export HB_USER_LDFLAGS="$HB_USER_LDFLAGS"
          export HB_CCACHE=ccache

          export PATH="$_add_path:\$PATH"
          export N_CPUS="\$($_cpus)"
          EOENV

      - name: Generate build script
        run: |
          tee ~/work/run_build.sh <<'EOF'
          set -ex
          echo "::group::Configure ccache in VM"
          ccache --set-config="cache_dir=$(pwd)/.ccache"
          ccache --set-config=max_size=32M
          ccache --set-config=compression=true
          ccache -p

          echo ::group::Build Harbour
          set -ex
          . ./.bashrc
          gmake -j$N_CPUS \
            HB_BUILD_CONTRIBS=no \
            HB_COMPILER=${{matrix.compiler}}

          echo "::group::Build contribs"
          set -ex
          . ./.bashrc
          gmake -j$N_CPUS \
            -C contrib \
            HB_COMPILER=${{matrix.compiler}}

          echo "::group::Run tests"
          set -ex
          bin/*/${{matrix.compiler}}/hbtest
          EOF

      - name: Build Harbour and contribs (FreeBSD VM)
        if: matrix.os.name == 'freebsd'
        uses: vmactions/freebsd-vm@v1
        with:
          usesh: true
          mem: 2048
          release: "${{ matrix.os.version }}"
          prepare: |
            echo "::group::Install dependencies"
            set -ex
            ~/work/install_${{ matrix.os.name }}_deps.sh
          run: |
            . ~/work/run_build.sh

      - name: Build Harbour and contribs (OmniOS VM)
        if: matrix.os.name == 'omnios'
        uses: vmactions/omnios-vm@v1
        with:
          usesh: true
          mem: 2048
          prepare: |
            echo "::group::Install dependencies"
            set -ex
            ~/work/install_${{ matrix.os.name }}_deps.sh
          run: |
            . ~/work/run_build.sh

      - name: Build Harbour and contribs (Solaris VM)
        if: matrix.os.name == 'solaris'
        uses: vmactions/solaris-vm@v1
        with:
          release: "11.4-gcc"
          usesh: true
          mem: 4096
          prepare: |
            echo "::group::Install dependencies"
            set -ex
            ~/work/install_${{ matrix.os.name }}_deps.sh
          run: |
            . ~/work/run_build.sh
