name: Code analysis

on: [push, pull_request]

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

env:
  CCACHE_DIR:        "/dev/shm/.ccache"
  CCACHE_MAXSIZE:    "128M"
  CCACHE_COMPRESS:   "true"
  CCACHE_SLOPPINESS: "pch_defines,time_macros"

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

      - run:  sudo apt-get update
      - name: Run shellcheck
        run:  ./scripts/verify-bash.sh
      - name: Install pylint
        run: |
          sudo apt-get install python3-setuptools
          sudo pip3 install pylint beautifulsoup4 html5lib
      - name: Run pylint
        run:  ./scripts/verify-python.sh
      - name: Install markdownlint
        run: |
          sudo apt-get install ruby-full
          ruby --version
          sudo gem install mdl
      - name: Run markdownlint
        run:  ./scripts/verify-markdown.sh
      - name: Install appstream-util
        run:  sudo apt-get install appstream-util
      - name: Verify metainfo.xml
        run:  appstream-util validate-relax --nonet contrib/linux/dosbox-staging.metainfo.xml

  build_clang_static_analyser:
    name: Clang static analyzer
    runs-on: ubuntu-20.04
    needs: run_linters
    steps:
      - name: Checkout repository
        uses: actions/checkout@v4
        with:
          submodules: false

      - run:  sudo apt-get update

      - name: Install dependencies
        run: |
          sudo apt-get install clang-tools python3-bs4 $(cat packages/ubuntu-20.04-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

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

      - 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

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

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

      - run: |
          meson setup \
          -Dbuildtype=debug \
          -Duse_alsa=false \
          -Duse_fluidsynth=false \
          -Duse_mt32emu=false \
          -Duse_opengl=false \
          -Duse_sdl2_net=false \
          -Duse_slirp=false \
          -Duse_zlib_ng=false \
          -Dunit_tests=disabled \
          build

      - name: Build and run scan-build
        run: |
          set -x
          ninja -C build scan-build
          mv build/meson-logs/scanbuild report

      - name: Upload report
        uses: actions/upload-artifact@v4.3.1
        with:
          name: clang-analysis-report
          path: report

      - name: Summarize report
        env:
          MAX_BUGS: 3
        run: |
          # summary
          echo "Full report is included in build Artifacts"
          echo
          ./scripts/count-clang-bugs.py report/*/index.html
