name: vm1-ci

on:
  - push
  - pull_request

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

    strategy:
      fail-fast: false
      matrix:
        os:
          - name: freebsd
            version: "14.2"
          - name: netbsd
            version: "10.0"
          - name: openbsd
            version: "7.6"
        compiler:
          - clang
          - gcc
        strictness:
          - "normal"
          - "strict"
        exclude:
          - os: {name: netbsd}
            compiler: clang
          - os: {name: openbsd}
            compiler: gcc
          # cross-platform-actions/actions@v0.21.1 works for FreeBSD but hangs often
          #- os: {name: freebsd}

    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
          sudo 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_netbsd_deps.sh <<EOF
          #!/bin/sh
          set -ex
          sudo pkgin -y install \
            bison ccache gmake
          EOF

          tee ~/work/install_openbsd_deps.sh <<EOF
          #!/bin/sh
          set -ex
          sudo pkg_add \
            bison ccache gmake \
            $(case ${{matrix.compiler }} in
              (gcc) echo ${{matrix.compiler}} ;; esac)
          EOF

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

      - name: Checkout code
        uses: actions/checkout@v4
        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="/sbin/sysctl -n hw.ncpu" ;;
            *) _cpus="echo 3" ;;
          esac
          HB_USER_CFLAGS=""
          HB_USER_LDFLAGS=""
          case ${{matrix.strictness}} in
            normal) ;;
            strict) HB_USER_CFLAGS="$HB_USER_CFLAGS -Werror" ;;
          esac
          tee ./.bashrc <<EOENV
          # These contribs do not build on any platform
          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 N_CPUS="\$($_cpus)"
          [ -n "$N_CPUS" ] || export N_CPUS=3
          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
          ${{matrix.compiler}} --version
          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
        timeout-minutes: 30
        uses: cross-platform-actions/action@v0.26.0
        with:
          operating_system: ${{ matrix.os.name }}
          architecture: x86_64
          version: "${{ matrix.os.version }}"
          shell: bash
          memory: 2G
          cpu_count: 3
          sync_files: "true"
          run: |
            echo "::group::Install dependencies"
            set -ex
            ~/work/install_${{ matrix.os.name }}_deps.sh
            . ~/work/run_build.sh
