name: Ubuntu

on:
  pull_request:
    branches:
      - main
  push:
    branches:
      - main

permissions:
  contents: read

jobs:
  build:
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@v4
      with:
        submodules: true

    - name: Install Packages
      run: |
        sudo apt-get update
        sudo apt-get install meson ninja-build libturbojpeg0-dev libpng-dev libwebp-dev libgles-dev libsdl2-dev

    - name: Build
      run: |
        meson setup build -Dlog=true -Dengines="sw, gl" -Dexamples=true -Dloaders=all -Dsavers=all -Dbindings=capi -Dtools=all
        sudo ninja -C build install

  compact_test:
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@v4
      with:
        submodules: true

    - name: Install Packages
      run: |
        sudo apt-get update
        sudo apt-get install meson ninja-build libgles-dev

    - name: Build
      run: |
        meson setup build -Dlog=true -Dengines="sw, gl" -Dloaders=all -Dsavers=all -Dstatic=true -Dthreads=false
        sudo ninja -C build install

  unit_test:
    runs-on: ubuntu-latest
    permissions:
      contents: read
      pull-requests: write

    steps:
    - uses: actions/checkout@v4
      with:
        submodules: true

    - name: Install Packages
      run: |
        sudo apt-get update
        sudo apt-get install meson ninja-build libgtest-dev libasan5 valgrind curl jq software-properties-common libturbojpeg0-dev libpng-dev libwebp-dev libgles-dev

    - name: Build
      run: |
        meson setup build -Dloaders="all" -Dengines="sw, gl" -Dsavers="all" -Dbindings="capi" -Dtests=true --errorlogs
        sudo ninja -C build install test

    - uses: actions/upload-artifact@v4
      with:
        name: UnitTestReport
        path: build/meson-logs/testlog.txt

    - name: Run memcheck Script(valgrind)
      run: |
        export PATH=$PATH:~/.local/bin/
        chmod +x "${GITHUB_WORKSPACE}/.github/workflows/memcheck_valgrind.sh"
        "${GITHUB_WORKSPACE}/.github/workflows/memcheck_valgrind.sh"
      env:
        GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

    - name: Build & Run memcheck Script(ASAN)
      run: |
        sudo rm -rf ./build
        meson setup build -Db_sanitize="address,undefined" -Dloaders="all" -Dsavers="all" -Dtests="true" -Dbindings="capi"
        sudo ninja -C build install
        export PATH=$PATH:~/.local/bin/
        chmod +x "${GITHUB_WORKSPACE}/.github/workflows/memcheck_asan.sh"
        "${GITHUB_WORKSPACE}/.github/workflows/memcheck_asan.sh"
      env:
        GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
