name: build & test

on: [push, pull_request]

jobs:
# This workflow contains 2 jobs build and test

  build:
    name: Build uoscore-uedhoc
    runs-on: ubuntu-latest
    
    steps:
      - name: Install Zephyr tools
        run: |
          wget https://apt.kitware.com/kitware-archive.sh
          sudo bash kitware-archive.sh
          sudo apt install --no-install-recommends git cmake ninja-build gperf ccache dfu-util device-tree-compiler wget python3-dev python3-pip python3-setuptools python3-tk python3-wheel xz-utils file make gcc gcc-multilib g++-multilib libsdl2-dev libmagic1 lcov gdb
          cmake --version
          python3 --version
          dtc --version

          pip3 install tabulate
          sudo apt install python3-venv
          python3 -m venv ~/zephyrproject/.venv
          source ~/zephyrproject/.venv/bin/activate

          pip3 install west
          west init ~/zephyrproject
          cd ~/zephyrproject
          west update

          west zephyr-export

          pip install -r ~/zephyrproject/zephyr/scripts/requirements.txt
          

          cd ~
          wget https://github.com/zephyrproject-rtos/sdk-ng/releases/download/v0.16.3/zephyr-sdk-0.16.3_linux-x86_64.tar.xz
          wget -O - https://github.com/zephyrproject-rtos/sdk-ng/releases/download/v0.16.3/sha256.sum | shasum --check --ignore-missing

          tar xvf zephyr-sdk-0.16.3_linux-x86_64.tar.xz
          cd zephyr-sdk-0.16.3
          /bin/bash -c "yes | ./setup.sh"
      
      - name: Checkout code
        uses: actions/checkout@v2
        with:
          submodules: recursive

### build, test and archive
# native_posix (0x68)
      - name: Build native_posix
        run: |
           source ~/zephyrproject/zephyr/zephyr-env.sh
           source ~/zephyrproject/.venv/bin/activate
           cd test/
           bash ci_scripts/test_build_options.sh

      - name: Archive static lib
        uses: actions/upload-artifact@v2
        with:
          name: uoscore-uedhoc-x86
          path: test/build/uoscore_uedhoc/libuoscore-uedhoc.a

# native_posix_64 
      - name: Build native_posix_64
        run: |
           source ~/zephyrproject/zephyr/zephyr-env.sh
           source ~/zephyrproject/.venv/bin/activate
           cd test/
           rm -rf build
           rm -rf build_lib_test
           west build -b=native_posix_64
           west build -t run

      - name: Archive static lib
        uses: actions/upload-artifact@v2
        with:
          name: uoscore-uedhoc-x86-64
          path: test/build/uoscore_uedhoc/libuoscore-uedhoc.a

### Generate ROM report
      - name: Genrate ROM report
        run: |
           source ~/zephyrproject/zephyr/zephyr-env.sh
           source ~/zephyrproject/.venv/bin/activate
           cd test/  
           bash ci_scripts/flash_report_gen.sh

      - name: Upload rom report with tinycrypt
        uses: actions/upload-artifact@v2
        with: 
          name: rom-report-tinycrypt-crypto  
          path: test/build_reports/rom_report_nrf91_with_tinycrypt.txt

      - name: Upload rom report with mbedtls
        uses: actions/upload-artifact@v2
        with: 
          name: rom-report-mbedtls-crypto  
          path: test/build_reports/rom_report_nrf91_with_mbedtls.txt

### Generate stack report
      - name: Genrate stack report
        run: |
           source ~/zephyrproject/zephyr/zephyr-env.sh
           source ~/zephyrproject/.venv/bin/activate
           cd test/  
           bash ci_scripts/stack_report_gen.sh

      - name: Upload stack report
        uses: actions/upload-artifact@v2
        with: 
          name: stack-report  
          path: test/build_reports/stack_report.html

### Generate test coverage report
      - name: Build native_posix with coverage information
        run: |
           source ~/zephyrproject/zephyr/zephyr-env.sh
           source ~/zephyrproject/.venv/bin/activate
           cd test/
           rm -rf build
           rm -rf build_lib_test
           west build -b native_posix -- -DCONFIG_COVERAGE=y
           west build -t run
           lcov --capture --directory ./ --output-file lcov.info -q --rc lcov_branch_coverage=1
           genhtml lcov.info --output-directory lcov_html -q --ignore-errors source --branch-coverage --highlight --legend
           tar -zcvf github-pages.tar.gz lcov_html

      - name: Upload test coverage report
        uses: actions/upload-pages-artifact@v1
        with: 
          name: github-pages  
          path: test/lcov_html


  # Deploy job
  deploy:
    # Add a dependency to the build job
    needs: build

    # Grant GITHUB_TOKEN the permissions required to make a Pages deployment
    permissions:
      pages: write      # to deploy to Pages
      id-token: write   # to verify the deployment originates from an appropriate source

    # Deploy to the github-pages environment
    environment:
      name: github-pages
      url: ${{ steps.deployment.outputs.page_url }}

    # Specify runner + deployment step
    runs-on: ubuntu-latest
    steps:
      - name: Deploy to GitHub Pages
        id: deployment
        uses: actions/deploy-pages@v1