name: RISC-V none GCC 11.3.0

on: [push]

jobs:
  Build-All-RISCV-none-GCC11:
    runs-on: windows-latest

    steps:
    - uses: actions/checkout@v3

    - name: Install RISC-V none GCC 11.3.0
      shell: pwsh
      run: | 
       Invoke-WebRequest -Uri "https://github.com/xpack-dev-tools/riscv-none-elf-gcc-xpack/releases/download/v11.3.0-1/xpack-riscv-none-elf-gcc-11.3.0-1-win32-x64.zip" -OutFile "xpack-riscv-none-elf-gcc-11.3.0-1-win32-x64.zip" -Verbose
       echo "-----Unpack gcc zip----"
       Expand-Archive xpack-*
       echo "Done"
       echo ""
       ls

       $env:GCC_PATH = "${{github.workspace}}/xpack-riscv-none-elf-gcc-11.3.0-1-win32-x64/xpack-riscv-none-elf-gcc-11.3.0-1" 

       $env:GCC_PATH+"/bin/" | Out-File -FilePath $env:GITHUB_PATH -Encoding utf8 -Append
       $env:GCC_PATH+"/riscv-none-elf/include/" | Out-File -FilePath $env:GITHUB_PATH -Encoding utf8 -Append
       $env:GCC_PATH+"/riscv-none-elf/include/c++/11.3.0/" | Out-File -FilePath $env:GITHUB_PATH -Encoding utf8 -Append
       $env:GCC_PATH+"/riscv-none-elf/include/c++/11.3.0/riscv-none-elf" | Out-File -FilePath $env:GITHUB_PATH -Encoding utf8 -Append
       $env:GCC_PATH+"/riscv-none-elf/include/c++/11.3.0/backward" | Out-File -FilePath $env:GITHUB_PATH -Encoding utf8 -Append
       $env:GCC_PATH+"/lib/gcc/riscv-none-elf/11.3.0/include" | Out-File -FilePath $env:GITHUB_PATH -Encoding utf8 -Append
       $env:GCC_PATH+"/lib/gcc/riscv-none-elf/11.3.0/include-fixed" | Out-File -FilePath $env:GITHUB_PATH -Encoding utf8 -Append
       $env:GCC_PATH+"/riscv-none-elf/lib/rv32im/ilp32/" | Out-File -FilePath $env:GITHUB_PATH -Encoding utf8 -Append

    #
    #  RISC-V
    #

    - name: Check compiler
      run: riscv-none-elf-gcc --version

    - name: Create Build Environment RISC-V
      run: cmake -E make_directory ${{github.workspace}}/build_riscv

    - name: Configure CMake
      shell: bash
      working-directory: ${{github.workspace}}/build_riscv
      run: |
       cmake $GITHUB_WORKSPACE  -G "Unix Makefiles" -DDEBUG=1 -Driscv=1

    - name: Build RISC-V
      working-directory: ${{github.workspace}}/build_riscv
      shell: bash
      run: cmake --build . -j

    - name: Save binaries
      uses: actions/upload-artifact@v3
      with:
        name: riscv-elf
        retention-days: 1
        path: |
          build_riscv/test_riscv.elf

  Run-QEMU:
    runs-on: ubuntu-latest
    needs: Build-All-RISCV-none-GCC11
    timeout-minutes: 5
    steps:
    - name: Set up QEMU
      run: |
        sudo apt-get install qemu-system-riscv32
        qemu-system-riscv32 --version

    - name: Donwload riscv binaries
      uses: actions/download-artifact@v3
      with:
        name: riscv-elf

    - name: Run Test
      working-directory: /home/runner/work/FreeRTOS_cpp11/FreeRTOS_cpp11/
      run: |
        ls -la /home/runner/work/FreeRTOS_cpp11/FreeRTOS_cpp11/
        qemu-system-riscv32 -M virt -semihosting -nographic -bios none -kernel test_riscv.elf
