name: Compile and Run
on:
  workflow_dispatch:
  pull_request:
    branches: [main]
  push:
    branches: [main]

permissions: 
  actions: read
  security-events: write

jobs:
   CI_cpp_test_run: 
    strategy:
      fail-fast: false
      matrix:
        # GCC currently has too many problems with the C++ API
        compiler: [AC6, CLANG]
        core: [M0, M4, M55]
    runs-on: ubuntu-latest    

    steps:
      - name: Checkout
        uses: actions/checkout@v4

      - name: Setup Python 3.10
        uses: actions/setup-python@v5
        with:
          python-version: '3.10'
          cache: 'pip'
          cache-dependency-path: Testing/requirements.txt
      
      - name: Install system packages
        run: |
          sudo add-apt-repository ppa:deadsnakes/ppa
          sudo apt-get install libpython3.9 libtinfo5

      - name: Cache packs
        uses: actions/cache@v4
        with:
          key: packs-cmsis-dsp
          restore-keys: |
              packs-
          path: /home/runner/.cache/arm/packs

      - name: Activate vcpkg
        uses: ARM-software/cmsis-actions/vcpkg@v1
        with:
          cache: "-cmsis_dsp_vcpkg"

      - name: Activate Arm tool license
        uses: ARM-software/cmsis-actions/armlm@v1
  
      - name: Cache boost
        id: cache-boost
        uses: actions/cache@v4
        with:
          key: boost-cmsis-dsp
          restore-keys: |
              boost-cmsis-dsp
          path: ${{ github.workspace }}/boost_1_84_0

      - name: Install boost
        if: steps.cache-boost.outputs.cache-hit != 'true'
        run: |
          echo "Install boost"
          curl -o boost.tar.gz https://archives.boost.io/release/1.84.0/source/boost_1_84_0.tar.gz
          tar -xf boost.tar.gz
          
      - name: Prepare framework
        run: |
          cd dsppp
          echo "Create missing folders"
          mkdir AC6_results
          mkdir CLANG_results
          mkdir GCC_results

          echo "Install missing python packages"
          pip install -r ../Testing/requirements.txt

          echo "Load missing pack"
          csolution list packs -s  test.csolution.yml -m > required_packs.txt
          
          cat required_packs.txt
          cpackget add -a -f required_packs.txt


      - name: Execute
        run: |
          cd dsppp
          echo "Running tests"

          python run_all.py -t -g ${{ matrix.compiler }} -c ${{ matrix.core }} -avh $AVH_FVP_PLUGINS/../bin

      - name: Upload test report
        uses: actions/upload-artifact@v4
        with:
          name: test-cpp-report_${{ matrix.compiler }}_${{ matrix.core }}
          path: dsppp/${{ matrix.compiler }}_results/errors_${{ matrix.core }}.txt

      
      - name: Check error
        run: |
          cd dsppp
          
          echo "Checking output..."
          test "$(cat ${{ matrix.compiler }}_results/errors_${{ matrix.core }}.txt | wc -l)" -eq 0
