name: "Build and test"
on:
  workflow_dispatch:
  push:
  pull_request:

env:
  RADIO_TOOL_VERSION: 0.2.2

jobs:
  build:
    runs-on: ubuntu-24.04
    steps:
      - name: Checkout
        uses: actions/checkout@v4
        with:
          submodules: recursive
      - name: Compile using devcontainer
        uses: devcontainers/ci@v0.3
        with:
          runCmd: |
            meson setup build_linux
            meson setup --cross-file cross_arm.txt build_arm
            meson setup --cross-file cross_cm7.txt build_cm7
            meson compile -C build_linux openrtx_linux
            meson compile -C build_linux openrtx_linux_smallscreen
            meson compile -C build_linux openrtx_linux_mod17
            meson compile -C build_arm openrtx_cs7000_bin
            meson compile -C build_arm openrtx_cs7000_dfu
            meson compile -C build_arm openrtx_dm1701_wrap
            meson compile -C build_arm openrtx_dm1801_wrap
            meson compile -C build_arm openrtx_md3x0_wrap
            meson compile -C build_arm openrtx_md9600_wrap
            meson compile -C build_arm openrtx_mduv3x0_wrap
            meson compile -C build_arm openrtx_mod17_wrap
            meson compile -C build_arm openrtx_gd77_wrap
            meson compile -C build_cm7 openrtx_cs7000p_wrap
            meson compile -C build_cm7 openrtx_cs7000p_dfu
            chmod -R 777 *
      - uses: actions/upload-artifact@v4
        with:
          name: release-bins
          path: |
            ${{github.workspace}}/build_cm7/openrtx_cs7000p_wrap.bin
            ${{github.workspace}}/build_cm7/openrtx_cs7000p_dfu
            ${{github.workspace}}/build_arm/openrtx_cs7000_bin
            ${{github.workspace}}/build_arm/openrtx_cs7000_dfu
            ${{github.workspace}}/build_arm/openrtx_dm1701_wrap
            ${{github.workspace}}/build_arm/openrtx_dm1801_wrap.sgl
            ${{github.workspace}}/build_arm/openrtx_gd77_wrap.sgl
            ${{github.workspace}}/build_linux/openrtx_linux
            ${{github.workspace}}/build_arm/openrtx_md3x0_wrap
            ${{github.workspace}}/build_arm/openrtx_md9600_wrap
            ${{github.workspace}}/build_arm/openrtx_mduv3x0_wrap
            ${{github.workspace}}/build_arm/openrtx_mod17_wrap
          if-no-files-found: error
  build-zephyr:
    runs-on: ubuntu-22.04
    steps:
      - name: Checkout
        uses: actions/checkout@v4
        with:
          submodules: recursive
          path: app
      - name: install-deps
        run: |
          sudo apt update
          sudo apt install -y git pkg-config build-essential meson libsdl2-dev libreadline-dev dfu-util cmake libusb-1.0-0 libusb-1.0-0-dev libcodec2-dev codec2
      - name: Set up Python
        uses: actions/setup-python@v5
        with:
          python-version: 3.11
      - name: Setup Zephyr project
        uses: zephyrproject-rtos/action-zephyr-setup@v1
        with:
          app-path: app
          toolchains: xtensa-espressif_esp32s3_zephyr-elf
          sdk-version: 0.17.0
      - name: Compile zephyr
        run: |
          cd app
          meson setup build
          meson compile -C build openrtx_ttwrplus_uf2
        shell: bash
      - uses: actions/upload-artifact@v4
        with:
          name: zephyr-release-bins
          path: |
            app/build/openrtx_*.uf2
  unit-test:
    runs-on: ubuntu-22.04
    steps:
      - name: Checkout
        uses: actions/checkout@v4
        with:
          submodules: recursive
      - name: install-deps
        run: |
          sudo apt update
          sudo apt install -y git pkg-config build-essential meson libsdl2-dev libreadline-dev dfu-util cmake libusb-1.0-0 libusb-1.0-0-dev libcodec2-dev codec2
      - name: setup meson
        run: |
          cd ${{github.workspace}}
          meson setup build
      - name: M17 Viterbi Unit Test
        run: meson test -C build "M17 Viterbi Unit Test"
      - name: M17 Golay Unit Test
        run: meson test -C build "M17 Golay Unit Test"
      - name: M17 RRC Test
        run: meson test -C build "M17 RRC Test"
      - name: Codeplug Test
        run: meson test -C build "Codeplug Test"
      - name: minmea Conversion Test
        run: meson test -C build "minmea conversion Test"
      # The following tests are disabled because they appear to be flakey when run in CI
      # - name: Sine Test
      #   run: meson test -C build "Sine Test"
      # - name: Linux InputStream Test
      #   run: meson test -C build "Linux InputStream Test"
