name: Linux builds

on: [push, pull_request]

concurrency:
  group: ${{ github.workflow }}-${{ github.ref }}
  cancel-in-progress: true

env:
  CCACHE_COMPRESS:      "true"
  CCACHE_COMPRESSLEVEL: "6"
  CCACHE_DIR:           "/dev/shm/.ccache"
  CCACHE_DIRECT:        "true"
  CCACHE_MAXSIZE:       "128M"
  CCACHE_SLOPPINESS:    "pch_defines,time_macros"
  CCACHE_UMASK:         "002"

  CLAMDB_DIR:      "/var/lib/clamav"

  DOCKER_RUN: >-
    docker run --rm
    -u "$(id --user):$(id --group)"
    -v "$PWD:$PWD"
    -v "$CCACHE_DIR:$CCACHE_DIR"
    -e HOME
    -e CCACHE_COMPRESS
    -e CCACHE_COMPRESSLEVEL
    -e CCACHE_DIR
    -e CCACHE_DIRECT
    -e CCACHE_MAXSIZE
    -e CCACHE_SLOPPINESS
    -e CCACHE_UMASK
    --workdir "$PWD"
    ghcr.io/dosbox-staging/debian-cross

jobs:
  cache_subprojects:
    name: Cache subprojects
    runs-on: ubuntu-latest
    if: github.event_name == 'push' || github.event.pull_request.head.repo.full_name != github.repository
    steps:
      - name: Checkout repository
        uses: actions/checkout@v4
        with:
          submodules: false

      - name: Install Meson
        run:  |
          export DEBIAN_FRONTEND=noninteractive
          sudo apt-get update
          sudo apt-get install -y python3-setuptools
          sudo pip3 install --upgrade meson

      - name: Cache subprojects
        id:   cache-subprojects
        uses: actions/cache@v3.3.2
        with:
          path: subprojects.tar
          key: subprojects-${{ hashFiles('subprojects/*.wrap') }}-1
          enableCrossOsArchive: true

      - if:   steps.cache-subprojects.outputs.cache-hit != 'true'
        name: Generate subprojects cache
        run:  scripts/fetch-and-tar-subprojects.sh

  build_ubuntu:
    name: ${{ matrix.conf.name }}
    runs-on: ${{ matrix.conf.os }}
    if: github.event_name == 'push' || github.event.pull_request.head.repo.full_name != github.repository
    needs: cache_subprojects
    strategy:
      matrix:
        conf:

          - name: GCC 10, Ubuntu 22.04
            os: ubuntu-22.04
            packages: g++-12
            build_flags: -Dbuildtype=debug --native-file=.github/meson/native-gcc-12.ini
            max_warnings: 0
            needs_all_deps: true
            run_tests: true

          - name: Clang 12, Ubuntu 22.04
            os: ubuntu-22.04
            packages: clang-14
            build_flags: -Dbuildtype=debug --native-file=.github/meson/native-clang-14.ini
            max_warnings: 0
            needs_all_deps: true
            run_tests: true

          - name: GCC 12, +debugger
            os: ubuntu-22.04
            packages: g++-12
            build_flags: -Denable_debugger=heavy --native-file=.github/meson/native-gcc-12.ini
            max_warnings: 0
            needs_all_deps: true

          - name: GCC, Debian 11, ARMv7
            os: ubuntu-20.04
            build_flags: -Dbuildtype=debug -Duse_zlib_ng=false --cross-file dosbox-cross
            max_warnings: 0
            run_tests: true
            cross: true
            arch: armhf
            deb_vers: 11
            needs_min_deps: false

          - name: GCC, Debian 11, aarch64
            os: ubuntu-20.04
            build_flags: -Dbuildtype=debug --cross-file dosbox-cross
            max_warnings: 0
            run_tests: true
            cross: true
            arch: arm64
            deb_vers: 11
            needs_min_deps: false

          - name: GCC, Debian 11, ppc64le
            os: ubuntu-20.04
            build_flags: -Dbuildtype=debug --cross-file dosbox-cross
            max_warnings: 21
            run_tests: true
            cross: true
            arch: ppc64el
            deb_vers: 11
            needs_min_deps: false

          - name: GCC 9, minimum build
            os: ubuntu-20.04
            packages: g++-9
            build_flags: >-
              -Dbuildtype=minsize
              -Dunit_tests=disabled
              -Duse_alsa=false
              -Duse_fluidsynth=false
              -Duse_mt32emu=false
              -Duse_opengl=false
              -Duse_sdl2_net=false
              -Duse_slirp=false
              --native-file=.github/meson/native-gcc-9.ini
            needs_min_deps: true
            max_warnings: -1

    steps:
      - name: Checkout repository
        uses: actions/checkout@v4
        with:
          submodules: false

#      - name: Update host repository
#        run: |
          # use the following to force full upgrades
          # sudo apt-get -y upgrade
          # sudo apt-get -y --with-new-pkgs upgrade
          # sudo apt-get -y install -y aptitude
          # echo -e "n\ny\ny" | sudo aptitude -y -f full-upgrade
          # sudo apt-get update
          # sudo apt-get -y upgrade

      - name: Install minimum dependencies
        if:   matrix.conf.needs_min_deps
        run: |
          export DEBIAN_FRONTEND=noninteractive
          sudo apt-get update
          sudo apt-get install -y build-essential ccache libsdl2-dev libopusfile-dev libspeexdsp-dev python3-setuptools
          sudo pip3 install --upgrade meson ninja

      - name: Install all dependencies
        if:   matrix.conf.needs_all_deps
        run: |
          export DEBIAN_FRONTEND=noninteractive
          sudo apt-get update
          sudo apt-get install -y \
            ${{ matrix.conf.packages }} \
            $(cat packages/${{ matrix.conf.os }}-apt.txt)
          sudo pip3 install --upgrade meson ninja

      - name:  Prepare compiler cache
        id:    prep-ccache
        shell: bash
        run: |
          mkdir -p "${CCACHE_DIR}"
          echo "dir=$CCACHE_DIR"                                                 >> $GITHUB_OUTPUT
          echo "today=$(date -I)"                                                >> $GITHUB_OUTPUT
          echo "yesterday=$(date --date=yesterday -I)"                           >> $GITHUB_OUTPUT
          echo "name_hash=$(echo '${{ matrix.conf.name }}' | shasum | cut -b-8)" >> $GITHUB_OUTPUT

      - uses:  actions/cache@v3.3.2
        id:    cache-ccache
        with:
          path: ${{ steps.prep-ccache.outputs.dir }}
          key:  ccache-${{ matrix.conf.os }}-${{ steps.prep-ccache.outputs.name_hash }}-${{ steps.prep-ccache.outputs.today }}-1
          restore-keys: |
            ccache-${{ matrix.conf.os }}-${{ steps.prep-ccache.outputs.name_hash }}-${{ steps.prep-ccache.outputs.yesterday }}-1

      - name: Log environment
        run:  ./scripts/log-env.sh

      - name: Set binary names
        id: bins
        run: |
          set -x
          git fetch --prune --unshallow
          git fetch --all --tags --force
          if [ -z "${{matrix.conf.cross}}" ]; then
            echo "MESON_BIN=meson" >> $GITHUB_OUTPUT
            echo "CCACHE_BIN=ccache" >> $GITHUB_OUTPUT
          else
            if $(git describe | grep -qs '-'); then
              vers="${{matrix.conf.deb_vers}}-$(git describe | cut -d '-' -f 1)-dev-${{matrix.conf.arch}}"
            else
              vers="${{matrix.conf.deb_vers}}-$(git describe)-${{matrix.conf.arch}}"
            fi
            echo "MESON_BIN=${{env.DOCKER_RUN}}:${vers} meson" >> $GITHUB_OUTPUT
            echo "CCACHE_BIN=${{env.DOCKER_RUN}}:${vers} ccache" >> $GITHUB_OUTPUT
          fi

      - name: Restore subprojects cache
        id:   cache-subprojects
        uses: actions/cache@v3.3.2
        with:
          path: subprojects.tar
          key: subprojects-${{ hashFiles('subprojects/*.wrap') }}-1
          enableCrossOsArchive: true

      - name: Extract subprojects cache
        run:  scripts/extract-subprojects-tar.sh

      - name: Meson setup
        run:  |
          ${{steps.bins.outputs.MESON_BIN}} setup ${{ matrix.conf.build_flags }} build \
          || ( cat build/meson-logs/meson-log.txt ; exit 1 )

      - name: Build
        run: |
          set -xo pipefail
          ${{steps.bins.outputs.CCACHE_BIN}} -z
          ${{steps.bins.outputs.MESON_BIN}} compile -C build |& tee build.log
          ${{steps.bins.outputs.CCACHE_BIN}} -s

      - name: Run tests
        if:   matrix.conf.run_tests
        run:  ${{steps.bins.outputs.MESON_BIN}} test --num-processes 128 -t 0 -C build --print-errorlogs

      - name: Summarize warnings
        env:
          MAX_WARNINGS: ${{ matrix.conf.max_warnings }}
        run:  ./scripts/count-warnings.py -lf build.log


  build_linux_release:
    name: Release build
    runs-on: ubuntu-20.04
    if: github.event_name == 'push' || github.event.pull_request.head.repo.full_name != github.repository
    needs: cache_subprojects
    steps:
      - name: Checkout repository
        uses: actions/checkout@v4
        with:
          submodules: false

      - run:  sudo apt-get update

      - name: Install dependencies
        run: |
          sudo apt-get install -y tree \
            $(cat packages/ubuntu-20.04-apt.txt)
          sudo pip3 install --upgrade meson ninja

      - name: Restore subprojects cache
        id:   cache-subprojects
        uses: actions/cache@v3.3.2
        with:
          path: subprojects.tar
          key: subprojects-${{ hashFiles('subprojects/*.wrap') }}-1
          enableCrossOsArchive: true

      - name: Extract subprojects cache
        run:  scripts/extract-subprojects-tar.sh

      - name:  Prepare compiler cache
        id:    prep-ccache
        shell: bash
        run: |
          mkdir -p "${CCACHE_DIR}"
          echo "dir=$CCACHE_DIR"                       >> $GITHUB_OUTPUT
          echo "today=$(date -I)"                      >> $GITHUB_OUTPUT
          echo "yesterday=$(date --date=yesterday -I)" >> $GITHUB_OUTPUT

      - uses:  actions/cache@v3.3.2
        id:    cache-ccache
        with:
          path: ${{ steps.prep-ccache.outputs.dir }}
          key:  ccache-linux-release-${{ steps.prep-ccache.outputs.today }}-1
          restore-keys: |
            ccache-linux-release-${{ steps.prep-ccache.outputs.yesterday }}-1

      - name: Log environment
        run:  ./scripts/log-env.sh

      - name: Inject version string
        run: |
          set -x
          git fetch --prune --unshallow
          git fetch --all --tags --force
          export VERSION=$(git describe --abbrev=0)
          echo "VERSION=$VERSION" >> $GITHUB_ENV

      - name: Setup release build
        run: |
          meson setup \
            -Duse_xinput2=false \
            -Duse_zlib_ng=sse2,ssse3,neon \
            -Ddefault_library=static \
            --wrap-mode=forcefallback \
            -Db_lto=true -Db_lto_threads=$(nproc) \
            --native-file=.github/meson/native-gcc-9.ini \
            build \
          || ( cat build/meson-logs/meson-log.txt ; exit 1 )


      - name: Build
        run:  meson compile -C build

      - name: Package
        run: |
          ./scripts/create-package.sh \
            -p linux \
            build \
            "dosbox-staging-linux-$VERSION"

      - name: Create tarball
        run: tar -cJf "dosbox-staging-linux-$VERSION.tar.xz" "dosbox-staging-linux-$VERSION"

      - name:  Prepare Clam AV DB cache
        id:    prep-clamdb
        shell: bash
        run: |
          sudo mkdir -p "${CLAMDB_DIR}"
          sudo chmod 777 "${CLAMDB_DIR}"
          echo "today=$(date -I)"                      >> $GITHUB_OUTPUT
          echo "yesterday=$(date --date=yesterday -I)" >> $GITHUB_OUTPUT
      - uses:  actions/cache@v3.3.2
        id:    cache-clamdb
        with:
          path: ${{ env.CLAMDB_DIR }}/*.cvd
          key:  clamdb-linux-${{ steps.prep-clamdb.outputs.today }}-1
          restore-keys: |
            clamdb-linux-${{ steps.prep-clamdb.outputs.yesterday }}-1

      - name: Clam AV scan
        run: |
          set -x
          sudo apt-get install clamav
          sudo systemctl stop clamav-freshclam
          sudo sed -i 's/30/20000/g' /etc/clamav/freshclam.conf
          sudo freshclam --foreground
          clamscan --heuristic-scan-precedence=yes --recursive --infected .

      - name: Upload tarball
        uses: actions/upload-artifact@v4.3.1
        # GitHub automatically zips the artifacts (there's no way to create
        # a tarball), and it removes all executable flags while zipping.
        # Letting it zip a tarball preserves flags in the compressed files.
        with:
          name: dosbox-staging-linux-x86_64
          path: dosbox-staging-linux-${{ env.VERSION }}.tar.xz
          overwrite: true


  # This job exists only to publish an artifact with version info when building
  # from main branch, so snapshot build version will be visible on:
  # https://dosbox-staging.github.io/downloads/devel/
  #
  publish_additional_artifacts:
    name: Publish additional artifacts
    needs: build_linux_release
    runs-on: ubuntu-22.04
    if: github.event_name == 'push' && github.ref == 'refs/heads/main'
    steps:
      - uses: actions/checkout@v4
        with:
          submodules: false

      - name: Generate changelog
        run: |
          set +x
          git fetch --unshallow
          git fetch --all --tags --force
          VERSION=$(git describe --abbrev=0)
          echo "VERSION=$VERSION" >> $GITHUB_ENV
          NEWEST_TAG=$(git describe --abbrev=0)
          git log "$NEWEST_TAG..HEAD" > changelog-$VERSION.txt

      - uses: actions/upload-artifact@v4.3.1
        with:
          # Keep exactly this artifact name; it's being used to propagate
          # version info via GitHub REST API
          name: changelog-${{ env.VERSION }}.txt
          path: changelog-${{ env.VERSION }}.txt
          overwrite: true
