name: Build Binaries

on:
    push:
        branches-ignore:
        - nightly
    pull_request:
        branches-ignore:
        - nightly

env:
    # Customize the CMake build type here (Release, Debug, RelWithDebInfo, etc.)
    BUILD_TYPE: Release
    GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}

jobs:
    build_windows:
        runs-on: windows-latest

        steps:
        - uses: actions/checkout@v3
        
        - name: Create Build Environment
          run: cmake -E make_directory ${{runner.workspace}}/build

        - name: Download PothosSDR
          run: Invoke-WebRequest -Uri "https://downloads.myriadrf.org/builds/PothosSDR/PothosSDR-2020.01.26-vc14-x64.exe" -OutFile ${{runner.workspace}}/pothos.exe

        - name: Install PothosSDR
          run: mkdir "C:/Program Files/PothosSDR" ; 7z x ${{runner.workspace}}/pothos.exe -o"C:/Program Files/PothosSDR/"

        - name: Download libusb
          run: Invoke-WebRequest -Uri "https://github.com/libusb/libusb/releases/download/v1.0.23/libusb-1.0.23.7z" -OutFile ${{runner.workspace}}/libusb.7z

        - name: Patch Pothos with earlier libusb version
          working-directory: ${{runner.workspace}}
          run: 7z x libusb.7z -olibusb_old ; rm "C:/Program Files/PothosSDR/bin/libusb-1.0.dll" ; cp "libusb_old/MS64/dll/libusb-1.0.dll" "C:/Program Files/PothosSDR/bin/"
 
        - name: Download SDRPlay API
          run: Invoke-WebRequest -Uri "https://drive.google.com/uc?id=12UHPMwkfa67A11QZDmpCT4iwHnyJHWuu" -OutFile ${{runner.workspace}}/SDRPlay.zip

        - name: Install SDRPlay API
          run: 7z x ${{runner.workspace}}/SDRPlay.zip -o"C:/Program Files/"

        - name: Download codec2
          run: git clone https://github.com/AlexandreRouma/codec2

        - name: Prepare MinGW
          run: C:/msys64/msys2_shell.cmd -defterm -here -no-start -mingw64 -c "pacman --noconfirm -S --needed base-devel mingw-w64-x86_64-toolchain mingw-w64-x86_64-cmake mingw-w64-x86_64-ninja"

        - name: Prepare build for codec2
          run: cd codec2 ; mkdir build ; cd build ; C:/msys64/msys2_shell.cmd -defterm -here -no-start -mingw64 -c "cmake .. -DCMAKE_GNUtoMS=ON -DCMAKE_BUILD_TYPE=Release -DCMAKE_C_FLAGS=-static-libgcc"

        - name: Build codec2
          run: cd codec2/build ; C:/msys64/msys2_shell.cmd -defterm -here -no-start -mingw64 -c "ninja"

        - name: Install codec2
          run: mkdir "C:/Program Files/codec2" ; mkdir "C:/Program Files/codec2/include" ; mkdir "C:/Program Files/codec2/include/codec2" ; mkdir "C:/Program Files/codec2/lib" ; cd "codec2" ; xcopy "src" "C:/Program Files/codec2/include" ; cd "build" ; xcopy "src" "C:/Program Files/codec2/lib" ; xcopy "codec2" "C:/Program Files/codec2/include/codec2"

        - name: Install vcpkg dependencies
          run: vcpkg install fftw3:x64-windows glfw3:x64-windows portaudio:x64-windows zstd:x64-windows

        - name: Install rtaudio
          run: git clone https://github.com/thestk/rtaudio ; cd rtaudio ; git checkout 2f2fca4502d506abc50f6d4473b2836d24cfb1e3 ; mkdir build ; cd build ; cmake .. ; cmake --build . --config Release ; cmake --install .

        - name: Prepare CMake
          working-directory: ${{runner.workspace}}/build
          run: cmake "$Env:GITHUB_WORKSPACE" "-DCMAKE_TOOLCHAIN_FILE=C:/vcpkg/scripts/buildsystems/vcpkg.cmake" -DOPT_BUILD_BLADERF_SOURCE=ON -DOPT_BUILD_LIMESDR_SOURCE=ON -DOPT_BUILD_SDRPLAY_SOURCE=ON -DOPT_BUILD_NEW_PORTAUDIO_SINK=ON -DOPT_BUILD_M17_DECODER=ON

        - name: Build
          working-directory: ${{runner.workspace}}/build
          run: cmake --build . --config Release --verbose

        - name: Create Archive
          working-directory: ${{runner.workspace}}
          run: '&($Env:GITHUB_WORKSPACE + "/make_windows_package.ps1") ./build ($Env:GITHUB_WORKSPACE + "/root")'

        - name: Save Archive
          uses: actions/upload-artifact@v3
          with:
              name: sdrpp_windows_x64
              path: ${{runner.workspace}}/sdrpp_windows_x64.zip

    build_macos:
        runs-on: macos-11

        steps:
        - uses: actions/checkout@v3
        
        - name: Create Build Environment
          run: cmake -E make_directory ${{runner.workspace}}/build
          
        - name: Update brew repositories
          run: brew update

        - name: Fix stuff
          run: rm -f /usr/local/bin/2to3* /usr/local/bin/idle3* /usr/local/bin/pydoc3* /usr/local/bin/python3* /usr/local/bin/python3-config* && brew reinstall gettext

        - name: Install dependencies
          run: brew install pkg-config libusb fftw glfw airspy airspyhf portaudio hackrf rtl-sdr libbladerf codec2 zstd && pip3 install mako

        - name: Install volk
          run: git clone --recursive https://github.com/gnuradio/volk && cd volk && mkdir build && cd build && cmake -DCMAKE_BUILD_TYPE=Release .. && make -j3 && sudo make install && cd ../../

        - name: Install SDRplay API
          run: wget https://www.sdrplay.com/software/SDRplay_RSP_API-MacOSX-3.07.3.pkg && sudo installer -pkg SDRplay_RSP_API-MacOSX-3.07.3.pkg -target /

        - name: Install libiio
          run: git clone https://github.com/analogdevicesinc/libiio && cd libiio && mkdir build && cd build && cmake -DCMAKE_BUILD_TYPE=Release .. && make -j3 && sudo make install && cd ../../

        - name: Install libad9361
          run: git clone https://github.com/analogdevicesinc/libad9361-iio && cd libad9361-iio && mkdir build && cd build && cmake -DCMAKE_BUILD_TYPE=Release .. && make -j3 && sudo make install && cd ../../

        - name: Install LimeSuite
          run: git clone https://github.com/myriadrf/LimeSuite && cd LimeSuite && mkdir builddir && cd builddir && cmake -DCMAKE_BUILD_TYPE=Release .. && make -j3 && sudo make install && cd ../../

        - name: Prepare CMake
          working-directory: ${{runner.workspace}}/build
          run: cmake $GITHUB_WORKSPACE -DOPT_BUILD_PLUTOSDR_SOURCE=ON -DOPT_BUILD_SOAPY_SOURCE=OFF -DOPT_BUILD_BLADERF_SOURCE=ON -DOPT_BUILD_SDRPLAY_SOURCE=ON -DOPT_BUILD_LIMESDR_SOURCE=ON -DOPT_BUILD_AUDIO_SINK=OFF -DOPT_BUILD_PORTAUDIO_SINK=ON -DOPT_BUILD_NEW_PORTAUDIO_SINK=ON -DOPT_BUILD_M17_DECODER=ON -DOPT_BUILD_AUDIO_SOURCE=OFF -DUSE_BUNDLE_DEFAULTS=ON -DCMAKE_BUILD_TYPE=Release

        - name: Build
          working-directory: ${{runner.workspace}}/build
          run: make VERBOSE=1 -j3

        - name: Create Archive
          working-directory: ${{runner.workspace}}
          run: cd $GITHUB_WORKSPACE && sh make_macos_bundle.sh ${{runner.workspace}}/build ./SDR++.app && zip -r ${{runner.workspace}}/sdrpp_macos_intel.zip SDR++.app

        - name: Save Archive
          uses: actions/upload-artifact@v3
          with:
              name: sdrpp_macos_intel
              path: ${{runner.workspace}}/sdrpp_macos_intel.zip
  
    build_debian_buster:
        runs-on: ubuntu-latest

        steps:
        - uses: actions/checkout@v3
        
        - name: Create Docker Image
          run: cd $GITHUB_WORKSPACE/docker_builds/debian_buster && docker build . --tag sdrpp_build

        - name: Run Container
          run: docker run --name build -v $GITHUB_WORKSPACE:/root/SDRPlusPlus --env BUILD_NO="-$GITHUB_RUN_NUMBER" sdrpp_build /root/do_build.sh

        - name: Recover Deb Archive
          working-directory: ${{runner.workspace}}
          run: docker cp build:/root/SDRPlusPlus/sdrpp_debian_amd64.deb ./

        - name: Save Deb Archive
          uses: actions/upload-artifact@v3
          with:
              name: sdrpp_debian_buster_amd64
              path: ${{runner.workspace}}/sdrpp_debian_amd64.deb

    build_debian_bullseye:
        runs-on: ubuntu-latest

        steps:
        - uses: actions/checkout@v3
        
        - name: Create Docker Image
          run: cd $GITHUB_WORKSPACE/docker_builds/debian_bullseye && docker build . --tag sdrpp_build

        - name: Run Container
          run: docker run --name build -v $GITHUB_WORKSPACE:/root/SDRPlusPlus --env BUILD_NO="-$GITHUB_RUN_NUMBER" sdrpp_build /root/do_build.sh

        - name: Recover Deb Archive
          working-directory: ${{runner.workspace}}
          run: docker cp build:/root/SDRPlusPlus/sdrpp_debian_amd64.deb ./

        - name: Save Deb Archive
          uses: actions/upload-artifact@v3
          with:
              name: sdrpp_debian_bullseye_amd64
              path: ${{runner.workspace}}/sdrpp_debian_amd64.deb

    build_debian_sid:
        runs-on: ubuntu-latest

        steps:
        - uses: actions/checkout@v3
        
        - name: Create Docker Image
          run: cd $GITHUB_WORKSPACE/docker_builds/debian_sid && docker build . --tag sdrpp_build

        - name: Run Container
          run: docker run --name build -v $GITHUB_WORKSPACE:/root/SDRPlusPlus --env BUILD_NO="-$GITHUB_RUN_NUMBER" sdrpp_build /root/do_build.sh

        - name: Recover Deb Archive
          working-directory: ${{runner.workspace}}
          run: docker cp build:/root/SDRPlusPlus/sdrpp_debian_amd64.deb ./

        - name: Save Deb Archive
          uses: actions/upload-artifact@v3
          with:
              name: sdrpp_debian_sid_amd64
              path: ${{runner.workspace}}/sdrpp_debian_amd64.deb

    # build_ubuntu_bionic:
    #     runs-on: ubuntu-latest

    #     steps:
    #     - uses: actions/checkout@v3
        
    #     - name: Create Docker Image
    #       run: cd $GITHUB_WORKSPACE/docker_builds/ubuntu_bionic && docker build . --tag sdrpp_build

    #     - name: Run Container
    #       run: docker run --name build -v $GITHUB_WORKSPACE:/root/SDRPlusPlus --env BUILD_NO="-$GITHUB_RUN_NUMBER" sdrpp_build /root/do_build.sh

    #     - name: Recover Deb Archive
    #       working-directory: ${{runner.workspace}}
    #       run: docker cp build:/root/SDRPlusPlus/sdrpp_debian_amd64.deb ./

    #     - name: Save Deb Archive
    #       uses: actions/upload-artifact@v3
    #       with:
    #           name: sdrpp_ubuntu_bionic_amd64
    #           path: ${{runner.workspace}}/sdrpp_debian_amd64.deb

    build_ubuntu_focal:
        runs-on: ubuntu-latest

        steps:
        - uses: actions/checkout@v3
        
        - name: Create Docker Image
          run: cd $GITHUB_WORKSPACE/docker_builds/ubuntu_focal && docker build . --tag sdrpp_build

        - name: Run Container
          run: docker run --name build -v $GITHUB_WORKSPACE:/root/SDRPlusPlus --env BUILD_NO="-$GITHUB_RUN_NUMBER" sdrpp_build /root/do_build.sh

        - name: Recover Deb Archive
          working-directory: ${{runner.workspace}}
          run: docker cp build:/root/SDRPlusPlus/sdrpp_debian_amd64.deb ./

        - name: Save Deb Archive
          uses: actions/upload-artifact@v3
          with:
              name: sdrpp_ubuntu_focal_amd64
              path: ${{runner.workspace}}/sdrpp_debian_amd64.deb

    build_ubuntu_jammy:
        runs-on: ubuntu-latest

        steps:
        - uses: actions/checkout@v3
        
        - name: Create Docker Image
          run: cd $GITHUB_WORKSPACE/docker_builds/ubuntu_jammy && docker build . --tag sdrpp_build

        - name: Run Container
          run: docker run --name build -v $GITHUB_WORKSPACE:/root/SDRPlusPlus --env BUILD_NO="-$GITHUB_RUN_NUMBER" sdrpp_build /root/do_build.sh

        - name: Recover Deb Archive
          working-directory: ${{runner.workspace}}
          run: docker cp build:/root/SDRPlusPlus/sdrpp_debian_amd64.deb ./

        - name: Save Deb Archive
          uses: actions/upload-artifact@v3
          with:
              name: sdrpp_ubuntu_jammy_amd64
              path: ${{runner.workspace}}/sdrpp_debian_amd64.deb

    build_raspios_bullseye_armhf:
        runs-on: ARM

        steps:
        - uses: actions/checkout@v3

        - name: Create Build Environment
          run: rm -rf ${{runner.workspace}}/build && cmake -E make_directory ${{runner.workspace}}/build
        
        - name: Prepare CMake
          working-directory: ${{runner.workspace}}/build
          run: cmake $GITHUB_WORKSPACE -DOPT_BUILD_BLADERF_SOURCE=ON -DOPT_BUILD_LIMESDR_SOURCE=ON -DOPT_BUILD_SDRPLAY_SOURCE=ON -DOPT_BUILD_NEW_PORTAUDIO_SINK=ON -DOPT_BUILD_M17_DECODER=ON -DOPT_BUILD_USRP_SOURCE=ON

        - name: Build
          working-directory: ${{runner.workspace}}/build
          run: make VERBOSE=1 -j3

        - name: Create Dev Archive
          working-directory: ${{runner.workspace}}
          run: sh $GITHUB_WORKSPACE/make_debian_package.sh ./build 'libfftw3-dev, libglfw3-dev, libvolk2-dev, librtaudio-dev' && mv sdrpp_debian_amd64.deb sdrpp_debian_armhf.deb
        
        - name: Save Deb Archive
          uses: actions/upload-artifact@v3
          with:
              name: sdrpp_raspios_bullseye_armhf
              path: ${{runner.workspace}}/sdrpp_debian_armhf.deb

    build_android:
        runs-on: ubuntu-latest

        steps:
        - uses: actions/checkout@v3

        - name: Fetch container
          working-directory: ${{runner.workspace}}
          run: git clone https://github.com/AlexandreRouma/android-sdr-kit
        
        - name: Build container
          working-directory: ${{runner.workspace}}/android-sdr-kit
          run: docker build --progress=plain -t android-sdr-kit .

        - name: Build
          run: docker run --name build -v $GITHUB_WORKSPACE:/root/SDRPlusPlus android-sdr-kit /bin/bash -l -c "cd /root/SDRPlusPlus/android && gradle --info assembleDebug"

        - name: Recover APK
          working-directory: ${{runner.workspace}}
          run: docker cp build:/root/SDRPlusPlus/android/app/build/outputs/apk/debug/app-debug.apk ./ && mv app-debug.apk sdrpp.apk

        - name: Save APK
          uses: actions/upload-artifact@v3
          with:
              name: sdrpp_android
              path: ${{runner.workspace}}/sdrpp.apk

    create_full_archive:
        needs: ['build_windows', 'build_macos', 'build_debian_buster', 'build_debian_bullseye', 'build_debian_sid', 'build_ubuntu_focal', 'build_ubuntu_jammy', 'build_raspios_bullseye_armhf', 'build_android']
        runs-on: ubuntu-latest

        steps:
        - name: Download All Builds
          uses: actions/download-artifact@v3

        - name: Create Archive
          run: >
            mkdir sdrpp_all && 
            mv sdrpp_windows_x64/sdrpp_windows_x64.zip sdrpp_all/ && 
            mv sdrpp_macos_intel/sdrpp_macos_intel.zip sdrpp_all/ && 
            mv sdrpp_debian_buster_amd64/sdrpp_debian_amd64.deb sdrpp_all/sdrpp_debian_buster_amd64.deb && 
            mv sdrpp_debian_bullseye_amd64/sdrpp_debian_amd64.deb sdrpp_all/sdrpp_debian_bullseye_amd64.deb && 
            mv sdrpp_debian_sid_amd64/sdrpp_debian_amd64.deb sdrpp_all/sdrpp_debian_sid_amd64.deb && 
            mv sdrpp_ubuntu_focal_amd64/sdrpp_debian_amd64.deb sdrpp_all/sdrpp_ubuntu_focal_amd64.deb &&
            mv sdrpp_ubuntu_jammy_amd64/sdrpp_debian_amd64.deb sdrpp_all/sdrpp_ubuntu_jammy_amd64.deb &&
            mv sdrpp_raspios_bullseye_armhf/sdrpp_debian_armhf.deb sdrpp_all/sdrpp_raspios_bullseye_armhf.deb &&
            mv sdrpp_android/sdrpp.apk sdrpp_all/sdrpp.apk

        - uses: actions/upload-artifact@v3
          with:
            name: sdrpp_all
            path: sdrpp_all/

    update_nightly_release:
        needs: [create_full_archive]
        runs-on: ubuntu-latest
        if: ${{ github.event_name == 'push' && github.ref == 'refs/heads/master' }}

        steps:
        - name: Download All Builds
          uses: actions/download-artifact@v3

        - name: Update Nightly
          run: gh release upload nightly sdrpp_all/* -R ${{github.repository}} --clobber

    check_spelling:
        runs-on: ubuntu-latest

        steps:
        - uses: actions/checkout@v3

        - name: Install codespell
          run: sudo apt update -y && sudo apt install -y codespell
        
        - name: Running codespell
          run: cd $GITHUB_WORKSPACE && codespell -q 2 || true

    check_formatting:
        runs-on: ubuntu-latest

        steps:
        - uses: actions/checkout@v3
        
        - name: Run check_clang_format
          run: cd $GITHUB_WORKSPACE && chmod +x ./check_clang_format.sh && ./check_clang_format.sh || true
