name: Build and Test

on:
  push:
    branches:
    - 'main'
    tags:
    - 'v*'
  pull_request:
    branches:
    - '*'


jobs:
  coverage:
    name: Coverage (ubuntu-22.04)
    runs-on: ubuntu-22.04
    env:
      NODE_OPTIONS: --max_old_space_size=4096
    steps:
      - uses: actions/checkout@v4
        with:
          submodules: recursive
      - uses: actions/cache@v4
        id: cache
        with:
          path: ./build/third_party/install
          key: ${{ github.job }}-${{ hashFiles('./third_party/**') }}
      - name: disable superbuild on cache hit
        if: steps.cache.outputs.cache-hit == 'true'
        run: echo "superbuild=-DSUPERBUILD=OFF" >> $GITHUB_ENV && echo "cmake_prefix_path=-DCMAKE_PREFIX_PATH=$(pwd)/build/third_party/install" >> $GITHUB_ENV
      - name: Install dependencies
        run: sudo apt-get update && sudo apt-get install -y lcov python3-future
      - name: configure
        run: cmake $superbuild $cmake_prefix_path -DCMAKE_BUILD_TYPE=Coverage -DASAN=ON -DWERROR=ON -DENABLE_CPPTRACE=On -Bbuild -H.
      - name: build
        run: cmake --build build -j2
      - name: unit tests
        run: ./build/src/unit_tests/unit_tests_runner
      - name: system tests
        run: ./build/src/system_tests/system_tests_runner
      - name: run lcov
        run: lcov --capture --directory . --no-external --exclude "*/third_party/*"  --output-file lcov.info
      - name: check size of lcov.info
        run: |
          ls -l lcov.info
      - name: Coveralls
        uses: coverallsapp/github-action@v2
        with:
          github-token: ${{ secrets.GITHUB_TOKEN }}
          path-to-lcov: "./lcov.info"

  ubuntu-non-superbuild:
    name: ${{ matrix.ubuntu_image }} (non-mavsdk_server, non-superbuild)
    runs-on: ${{ matrix.ubuntu_image }}
    strategy:
      matrix:
        include:
          - ubuntu_image: ubuntu-20.04
          - ubuntu_image: ubuntu-22.04
          - ubuntu_image: ubuntu-24.04
    steps:
      - uses: actions/checkout@v4
        with:
          submodules: recursive
      - name: install
        run: sudo apt-get update && sudo apt-get install -y libjsoncpp-dev libcurl4-openssl-dev libtinyxml2-dev libgtest-dev libgmock-dev liblzma-dev
      - name: install mavlink on the system
        run: |
            cmake -Bthird_party/mavlink/build -Sthird_party/mavlink
            sudo cmake --build third_party/mavlink/build
      - name: configure
        run: cmake -DCMAKE_BUILD_TYPE=RelWithDebInfo -DSUPERBUILD=OFF -DWERROR=ON -DENABLE_CPPTRACE=On -Bbuild/release -H.
      - name: build
        run: cmake --build build/release -j2
      - name: unit tests
        run: ./build/release/src/unit_tests/unit_tests_runner
      - name: system tests
        if: ${{ matrix.ubuntu_image }} == "ubuntu-24.04"
        run: ./build/release/src/system_tests/system_tests_runner

  ubuntu-superbuild:
    name: ${{ matrix.ubuntu_image }} (mavsdk_server, superbuild)
    runs-on: ${{ matrix.ubuntu_image }}
    strategy:
      fail-fast: false
      matrix:
        include:
          - ubuntu_image: ubuntu-20.04
            cc: gcc
            cxx: g++
            description: Ubuntu 20.04 (GCC)
          - ubuntu_image: ubuntu-22.04
            cc: gcc
            cxx: g++
            description: Ubuntu 22.04 (GCC)
          - ubuntu_image: ubuntu-24.04
            cc: gcc-13
            cxx: g++-13
            description: Ubuntu 24.04 (GCC 13)
          - ubuntu_image: ubuntu-24.04
            cc: gcc-14
            cxx: g++-14
            description: Ubuntu 24.04 (GCC 14)
          - ubuntu_image: ubuntu-24.04
            cc: clang-18
            cxx: clang++-18
            description: Ubuntu 24.04 (clang 18)
    steps:
      - uses: actions/checkout@v4
        with:
          submodules: recursive
      - uses: actions/cache@v4
        id: cache
        with:
          path: ./build/release/third_party/install
          key: ${{ github.job }}-${{ matrix.ubuntu_image }}-${{ matrix.cc }}-${{ hashFiles('./third_party/**') }}-1
      - name: disable superbuild on cache hit
        if: steps.cache.outputs.cache-hit == 'true'
        run: echo "superbuild=-DSUPERBUILD=OFF" >> $GITHUB_ENV && echo "cmake_prefix_path=-DCMAKE_PREFIX_PATH=$(pwd)/build/release/third_party/install" >> $GITHUB_ENV
      - name: configure
        run: |
          echo "CC=${{ matrix.cc }}" >> $GITHUB_ENV
          echo "CXX=${{ matrix.cxx }}" >> $GITHUB_ENV
          cmake $superbuild $cmake_prefix_path -DCMAKE_BUILD_TYPE=RelWithDebInfo -DBUILD_MAVSDK_SERVER=ON -DWERROR=ON -DENABLE_CPPTRACE=On -DCMAKE_INSTALL_PREFIX=install -Bbuild/release -H.
      - name: cleanup to save space
        run: |
          rm -rf ./build/release/third_party/absl
          rm -rf ./build/release/third_party/cares
          rm -rf ./build/release/third_party/cpptrace
          rm -rf ./build/release/third_party/curl
          rm -rf ./build/release/third_party/grpc
          rm -rf ./build/release/third_party/gtest
          rm -rf ./build/release/third_party/jsoncpp
          rm -rf ./build/release/third_party/libevents
          rm -rf ./build/release/third_party/liblzma
          rm -rf ./build/release/third_party/mavlink
          rm -rf ./build/release/third_party/openssl
          rm -rf ./build/release/third_party/protobuf
          rm -rf ./build/release/third_party/re2
          rm -rf ./build/release/third_party/tinyxml2
          rm -rf ./build/release/third_party/zlib
          rm -rf ./build/release/third_party/zlib-ng
      - name: build
        run: cmake --build build/release -j2
      - name: install
        run: cmake --build build/release --target install
      - name: temporary workaround for Ubuntu 22.04
        if: ${{ matrix.ubuntu_image }} == "ubuntu-22.04"
        run: |
         echo 'APT::Get::Always-Include-Phased-Updates "true";' | sudo tee -a /etc/apt/apt.conf.d/99-phased-updates
         sudo apt-get update
      - name: install examples dependencies
        run: sudo apt-get install libsdl2-dev ${{ matrix.cc }} ${{ matrix.cxx }}
      - name: configure examples
        run: cmake -DCMAKE_PREFIX_PATH="$(pwd)/install;$(pwd)/build/release/third_party/install" -DCMAKE_BUILD_TYPE=RelWithDebInfo -DWERROR=ON -Bexamples/build -Hexamples
      - name: build examples
        run: cmake --build examples/build -j2
      - name: unit tests
        run: ./build/release/src/unit_tests/unit_tests_runner
       # - name: system tests
       #   run: ./build/release/src/system_tests/system_tests_runner
      - name: test (mavsdk_server)
        run: ./build/release/src/mavsdk_server/test/unit_tests_mavsdk_server

  ubuntu-hunter:
    name: ${{ matrix.ubuntu_image }} (non-mavsdk_server, hunter)
    runs-on: ${{ matrix.ubuntu_image }}
    strategy:
      matrix:
        include:
          - ubuntu_image: ubuntu-20.04
          - ubuntu_image: ubuntu-22.04
          - ubuntu_image: ubuntu-24.04
    steps:
      - uses: actions/checkout@v4
        with:
          submodules: recursive
      - uses: actions/cache@v4
        id: cache
        with:
          path: ~/.hunter
          key: ${{ github.job }}-${{ matrix.ubuntu_image }}-${{ hashFiles('~/.hunter/**') }}-2
      - name: install mavlink on the system
        run: |
            cmake -Bthird_party/mavlink/build -Sthird_party/mavlink
            sudo cmake --build third_party/mavlink/build
      - name: configure
        run: cmake -DSUPERBUILD=OFF -DHUNTER_ENABLED=ON -DCMAKE_TOOLCHAIN_FILE=$(pwd)/src/cmake/fpic_toolchain.cmake  -DCMAKE_BUILD_TYPE=Debug -DBUILD_MAVSDK_SERVER=OFF -DBUILD_SHARED_LIBS=OFF -DENABLE_CPPTRACE=On -Bbuild -H.
      - name: build
        run: cmake --build build -j2
      - name: unit tests
        run: ./build/src/unit_tests/unit_tests_runner
      #- name: system tests
      #  run: ./build/src/system_tests/system_tests_runner

  ubuntu24-style-and-proto-check:
    name: ubuntu-24.04 (style and proto check)
    runs-on: ubuntu-24.04
    steps:
      - uses: actions/checkout@v4
        with:
          submodules: recursive
      - name: install dependencies
        run: sudo apt-get update && sudo apt-get install -y clang-format-18
      - uses: actions/cache@v4
        id: cache
        with:
          path: ./build/default/third_party/install
          key: ${{ github.job }}-${{ hashFiles('./third_party/**') }}
      - name: disable superbuild on cache hit
        if: steps.cache.outputs.cache-hit == 'true'
        run: echo "superbuild=-DSUPERBUILD=OFF" >> $GITHUB_ENV && echo "cmake_prefix_path=-DCMAKE_PREFIX_PATH=$(pwd)/build/default/third_party/install" >> $GITHUB_ENV
      - name: build necessary protoc tooling
        run: cmake $superbuild $cmake_prefix_path -DCMAKE_BUILD_TYPE=Debug -DBUILD_MAVSDK_SERVER=ON -DENABLE_CPPTRACE=On -Bbuild/default -H.
      - name: generate code from protos
        run: PATH="$PATH:$HOME/.local/bin" tools/generate_from_protos.sh
      - name: fix style
        run: tools/fix_style.sh . || true
      - name: check for diff
        run: git diff --exit-code

  deb-package:
    name: ${{ matrix.container_name }} (package, non-mavsdk_server)
    runs-on: ${{ matrix.container_name }}
    strategy:
      fail-fast: false
      matrix:
        container_name: [ubuntu-20.04, ubuntu-22.04, ubuntu-24.04]
    steps:
      - uses: actions/checkout@v4
        with:
          submodules: recursive
      - name: git permission workaround
        run: |
          git config --global --add safe.directory "$GITHUB_WORKSPACE"
          git config --global --add safe.directory /github/workspace
      - name: Install ruby, gem, and fpm
        run: |
          sudo apt-get update
          sudo apt-get install -y ruby-dev
          sudo gem install --no-document fpm
      - name: configure
        run: cmake -DCMAKE_BUILD_TYPE=RelWithDebInfo -DBUILD_MAVSDK_SERVER=OFF -DBUILD_SHARED_LIBS=ON -DCMAKE_INSTALL_PREFIX=install -DWERROR=ON -Bbuild/release -H.
      - name: build
        run: cmake --build build/release --target install -- -j2
      - name: Package
        if: startsWith(github.ref, 'refs/tags/v')
        run: tools/create_packages.sh ./install . amd64 libmavsdk-dev
      - name: Publish artefacts
        if: startsWith(github.ref, 'refs/tags/v')
        uses: svenstaro/upload-release-action@v1-release
        with:
          repo_token: ${{ secrets.GITHUB_TOKEN }}
          file_glob: true
          file: '*.deb'
          tag: ${{ github.ref }}
          overwrite: true

  dockcross-linux-arm:
    name: linux-${{ matrix.docker_name }}
    runs-on: ubuntu-20.04
    strategy:
      fail-fast: false
      matrix:
        include:
          - docker_name: armv6
            arch_name: armv6
          - docker_name: armv7
            arch_name: armv7
          - docker_name: arm64
            arch_name: arm64
          - docker_name: arm64-lts
            arch_name: arm64
            rename_distro: true
    steps:
      - uses: actions/checkout@v4
        with:
          submodules: recursive
      - name: setup dockcross
        run: docker run --rm docker.io/mavsdk/mavsdk-dockcross-linux-${{ matrix.docker_name }}-custom > ./dockcross-linux-${{ matrix.docker_name }}-custom; chmod +x ./dockcross-linux-${{ matrix.docker_name }}-custom
      - uses: actions/cache@v4
        id: cache
        with:
          path: ./build/linux-${{ matrix.docker_name }}/third_party/install
          key: ${{ github.job }}-linux-${{ matrix.docker_name }}-${{ hashFiles('./third_party/**') }}-6
      - name: disable superbuild on cache hit
        if: steps.cache.outputs.cache-hit == 'true'
        run: echo "superbuild=-DSUPERBUILD=OFF" >> $GITHUB_ENV && echo "cmake_prefix_path=-DCMAKE_PREFIX_PATH=/work/build/linux-${{ matrix.docker_name }}/third_party/install" >> $GITHUB_ENV
      - name: configure
        run: ./dockcross-linux-${{ matrix.docker_name }}-custom /bin/bash -c "cmake $superbuild $cmake_prefix_path -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX=build/linux-${{ matrix.docker_name }}/install -DBUILD_MAVSDK_SERVER=OFF -DBUILD_SHARED_LIBS=ON -DWERROR=ON -Bbuild/linux-${{ matrix.docker_name }} -H."
      - name: build
        run: ./dockcross-linux-${{ matrix.docker_name }}-custom cmake --build build/linux-${{ matrix.docker_name }} -j2 --target install
      - name: create deb packages
        run: ./dockcross-linux-${{ matrix.docker_name }}-custom tools/create_packages.sh ./build/linux-${{ matrix.docker_name }}/install . ${{ matrix.arch_name }} libmavsdk-dev
      - if: ${{ matrix.rename_distro }}
        name: Rename LTS versions from debian12 to debian11
        run: |
          sudo apt update && sudo apt install -y rename
          rename 's/debian12_arm64/debian11_arm64/' *.deb
      - name: Publish artefacts
        if: startsWith(github.ref, 'refs/tags/v')
        uses: svenstaro/upload-release-action@v1-release
        with:
          repo_token: ${{ secrets.GITHUB_TOKEN }}
          file_glob: true
          file: '*.deb'
          tag: ${{ github.ref }}
          overwrite: true

  alpine-linux:
    name: alpine 3.19.0 (musl)
    runs-on: ubuntu-20.04
    container: alpine:3.19.0
    steps:
      - name: install tools
        run: apk update && apk add build-base cmake git linux-headers perl tar python3 py3-pip rust cargo
      - uses: actions/checkout@v4
        with:
          submodules: recursive
      - name: git permission workaround
        run: |
          git config --global --add safe.directory "$GITHUB_WORKSPACE"
          git config --global --add safe.directory /github/workspace
      - uses: actions/cache@v4
        id: cache
        with:
          path: ./build/release/third_party/install
          key: ${{ github.job }}-${{ hashFiles('./third_party/**') }}-1
      - name: disable superbuild on cache hit
        if: steps.cache.outputs.cache-hit == 'true'
        run: echo "superbuild=-DSUPERBUILD=OFF" >> $GITHUB_ENV && echo "cmake_prefix_path=-DCMAKE_PREFIX_PATH=$(pwd)/build/release/third_party/install" >> $GITHUB_ENV
      - name: configure
        run: cmake $superbuild $cmake_prefix_path -DCMAKE_BUILD_TYPE=Release -DBUILD_MAVSDK_SERVER=ON -DBUILD_SHARED_LIBS=OFF -DBUILD_STATIC_MAVSDK_SERVER=ON -DCMAKE_INSTALL_PREFIX=install -DWERROR=ON -DENABLE_CPPTRACE=On -Bbuild/release -H.
      - name: build
        run: cmake --build build/release --target install -- -j2
      - name: unit tests
        run: ./build/release/src/unit_tests/unit_tests_runner
      - name: system tests
        run: ./build/release/src/system_tests/system_tests_runner
      - name: test (mavsdk_server)
        run: ./build/release/src/mavsdk_server/test/unit_tests_mavsdk_server
      - name: Upload as artefact
        uses: actions/upload-artifact@v4
        with:
          name: mavsdk_server_musl_x86_64
          path: ./install/bin/mavsdk_server
          retention-days: 2
      - name: Publish artefacts
        if: startsWith(github.ref, 'refs/tags/v')
        uses: svenstaro/upload-release-action@v1-release
        with:
          repo_token: ${{ secrets.GITHUB_TOKEN }}
          file: 'install/bin/mavsdk_server'
          asset_name: 'mavsdk_server_musl_x86_64'
          tag: ${{ github.ref }}
          overwrite: true

  dockcross-linux-musl:
    name: ${{ matrix.arch_name }}
    runs-on: ubuntu-20.04
    strategy:
      fail-fast: false
      matrix:
        arch_name: [linux-armv6-musl, linux-armv7l-musl, linux-arm64-musl]
    steps:
      - uses: actions/checkout@v4
        with:
          submodules: recursive
      - name: setup dockcross
        run: docker run --rm dockcross/${{ matrix.arch_name }} > ./dockcross-${{ matrix.arch_name }}; chmod +x ./dockcross-${{ matrix.arch_name }}
      - uses: actions/cache@v4
        id: cache
        with:
          path: ./build/${{ matrix.arch_name }}/third_party/install
          key: ${{ github.job }}-${{ matrix.arch_name }}-${{ hashFiles('./third_party/**') }}
      - name: disable superbuild on cache hit
        if: steps.cache.outputs.cache-hit == 'true'
        run: echo "superbuild=-DSUPERBUILD=OFF" >> $GITHUB_ENV && echo "cmake_prefix_path=-DCMAKE_PREFIX_PATH=/work/build/${{ matrix.arch_name }}/third_party/install" >> $GITHUB_ENV
      - name: configure
        run: ./dockcross-${{ matrix.arch_name }} /bin/bash -c "cmake $superbuild $cmake_prefix_path -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX=build/${{ matrix.arch_name }}/install -DBUILD_MAVSDK_SERVER=ON -DBUILD_SHARED_LIBS=OFF -DBUILD_STATIC_MAVSDK_SERVER=ON -DWERROR=ON -Bbuild/${{ matrix.arch_name }} -H."
      - name: build
        run: ./dockcross-${{ matrix.arch_name }} cmake --build build/${{ matrix.arch_name }} -j2 --target install
      - name: Publish artefacts
        if: startsWith(github.ref, 'refs/tags/v')
        uses: svenstaro/upload-release-action@v1-release
        with:
          repo_token: ${{ secrets.GITHUB_TOKEN }}
          file: 'build/${{ matrix.arch_name }}/install/bin/mavsdk_server'
          asset_name: 'mavsdk_server_${{ matrix.arch_name }}'
          tag: ${{ github.ref }}
          overwrite: true

  dockcross-android:
    name: ${{ matrix.name }}
    runs-on: ubuntu-20.04
    strategy:
      fail-fast: false
      matrix:
        include:
          - name: android-arm
            arch: armeabi-v7a
          - name: android-arm64
            arch: arm64-v8a
          - name: android-x86
            arch: x86
          - name: android-x86_64
            arch: x86_64
    steps:
      - uses: actions/checkout@v4
        with:
          submodules: recursive
      - name: setup dockcross
        run: docker run --rm dockcross/${{ matrix.name }} > ./dockcross-${{ matrix.name }}; chmod +x ./dockcross-${{ matrix.name }}
      - uses: actions/cache@v4
        id: cache
        with:
          path: ./build/${{ matrix.name }}/third_party/install
          key: ${{ github.job }}-${{ matrix.name }}-${{ hashFiles('./third_party/**') }}
      - name: disable superbuild on cache hit
        if: steps.cache.outputs.cache-hit == 'true'
        run: echo "superbuild=-DSUPERBUILD=OFF" >> $GITHUB_ENV && echo "cmake_prefix_path=-DCMAKE_PREFIX_PATH=/work/build/${{ matrix.name }}/third_party/install" >> $GITHUB_ENV
      - name: configure
        run: ./dockcross-${{ matrix.name }} /bin/bash -c "cmake $superbuild $cmake_prefix_path -DCMAKE_BUILD_TYPE=RelWithDebInfo -DCMAKE_INSTALL_PREFIX=build/${{ matrix.name }}/install -DBUILD_MAVSDK_SERVER=ON -DBUILD_SHARED_LIBS=OFF -DWERROR=ON -Bbuild/${{ matrix.name }} -H."
      - name: build
        run: ./dockcross-${{ matrix.name }} cmake --build build/${{ matrix.name }} -j2 --target install
      - name: create tar with header and library
        run: mkdir -p build/${{ matrix.name }}/export/include; cp build/${{ matrix.name }}/install/include/mavsdk/mavsdk_server/mavsdk_server_api.h build/${{ matrix.name }}/export/include; mkdir -p build/${{ matrix.name }}/export/${{ matrix.arch }}; cp build/${{ matrix.name }}/install/lib/libmavsdk_server.so build/${{ matrix.name }}/export/${{ matrix.arch }}; tar -C build/${{ matrix.name }}/export -cf build/${{ matrix.name }}/export/mavsdk_server_${{ matrix.name }}.tar ${{ matrix.arch }} include;
      - name: Publish artefacts
        if: startsWith(github.ref, 'refs/tags/v')
        uses: svenstaro/upload-release-action@v1-release
        with:
          repo_token: ${{ secrets.GITHUB_TOKEN }}
          file: 'build/${{ matrix.name }}/export/mavsdk_server_${{ matrix.name }}.tar'
          asset_name: 'mavsdk_server_${{ matrix.name }}.tar'
          tag: ${{ github.ref }}
          overwrite: true

  macOS:
    name: ${{ matrix.name }}
    runs-on: ${{ matrix.runner }}
    strategy:
      fail-fast: false
      matrix:
        include:
          - name: macOS x64
            build-framework: OFF
            runner: macos-13
            architecture: 'x64'
          - name: macOS arm64
            build-framework: OFF
            runner: macos-14
            architecture: 'arm64'
          - name: macOS framework x64
            build-framework: ON
            runner: macos-13
            architecture: 'x64'
          - name: macOS framework arm64
            build-framework: ON
            runner: macos-14
            architecture: 'arm64'
    steps:
      - name: Install Python
        uses: actions/setup-python@v5
        with:
          python-version: '3.11'
      - uses: actions/checkout@v4
        with:
          submodules: recursive
          fetch-depth: 0
      - uses: actions/cache@v4
        id: cache
        with:
          path: ./build/macos/third_party/install
          key: ${{ github.job }}-${{ matrix.name }}-${{ hashFiles('./third_party/**') }}-1
      - name: disable superbuild on cache hit
        if: steps.cache.outputs.cache-hit == 'true'
        run: echo "superbuild=-DSUPERBUILD=OFF" >> $GITHUB_ENV && echo "cmake_prefix_path=-DCMAKE_PREFIX_PATH=$(pwd)/build/macos/third_party/install" >> $GITHUB_ENV
      - name: set SDKROOT value
        run: echo "SDKROOT=$(xcrun --sdk macosx --show-sdk-path)" >> $GITHUB_ENV
      - name: configure
        run: cmake $superbuild $cmake_prefix_path -DCMAKE_BUILD_TYPE=RelWithDebInfo -DCMAKE_INSTALL_PREFIX=build/macos/install -DBUILD_MAVSDK_SERVER=ON -DBUILD_SHARED_LIBS=OFF -DMACOS_FRAMEWORK=${{ matrix.build-framework }} -DWERROR=ON -DENABLE_CPPTRACE=On -Bbuild/macos -H.
      - name: build
        run: cmake --build build/macos -j2 --target install
      - name: unit tests
        run: ./build/macos/src/unit_tests/unit_tests_runner
      - name: system tests
        run: ./build/macos/src/system_tests/system_tests_runner
      - name: test (mavsdk_server)
        run: ./build/macos/src/mavsdk_server/test/unit_tests_mavsdk_server
      - name: Upload framework as artefact
        if: ${{ matrix.build-framework == 'ON' }}
        uses: actions/upload-artifact@v4
        with:
          name: mavsdk_server_macos_${{ matrix.architecture }}.framework
          path: ./build/macos/src/mavsdk_server/src/mavsdk_server.framework
          retention-days: 2
      - name: Upload binary as artefact
        if: ${{ matrix.build-framework == 'OFF' }}
        uses: actions/upload-artifact@v4
        with:
          name: mavsdk_server_macos_${{ matrix.architecture }}
          path: build/macos/install/bin/mavsdk_server
          retention-days: 2
      - name: Publish mavsdk_server_macos to github release
        if: ${{ startsWith(github.ref, 'refs/tags/v') && matrix.build-framework == 'OFF' }}
        uses: svenstaro/upload-release-action@v1-release
        with:
          repo_token: ${{ secrets.GITHUB_TOKEN }}
          file: build/macos/install/bin/mavsdk_server
          asset_name: mavsdk_server_macos_${{ matrix.architecture }}
          tag: ${{ github.ref }}
          overwrite: true

  iOS:
    name: ${{ matrix.name }}
    runs-on: ${{ matrix.runner }}
    strategy:
      fail-fast: false
      matrix:
        include:
          - name: ios
            platform: OS64
            sdk: iphoneos
            runner: macos-14
          - name: ios_simulator_x64
            platform: SIMULATOR64
            sdk: iphonesimulator
            runner: macos-13
          - name: ios_simulator_arm64
            platform: SIMULATORARM64
            sdk: iphonesimulator
            runner: macos-14
    steps:
      - name: Install Python
        uses: actions/setup-python@v5
        with:
          python-version: '3.11'
          architecture: 'x64'
      - uses: actions/checkout@v4
        with:
          submodules: recursive
          fetch-depth: 0
      - uses: actions/cache@v4
        id: cache
        with:
          path: ./build/${{ matrix.name }}/third_party/install
          key: ${{ github.job }}-${{ matrix.name }}-${{ hashFiles('./third_party/**', './tools/ios.toolchain.cmake') }}-1
      - name: disable superbuild on cache hit
        if: steps.cache.outputs.cache-hit == 'true'
        run: echo "superbuild=-DSUPERBUILD=OFF" >> $GITHUB_ENV && echo "cmake_prefix_path=-DCMAKE_PREFIX_PATH=$(pwd)/build/${{ matrix.name }}/third_party/install" >> $GITHUB_ENV
      - name: set SDK-related environment variables (for non-cmake deps)
        run: |
          echo "SDKROOT=$(xcrun --sdk ${{ matrix.sdk }} --show-sdk-path)" >> $GITHUB_ENV
      - name: install pymavlink dependencies
        run: |
          python3 -m pip install --upgrade pip
          python3 -m pip install wheel
          python3 -m pip install future
      - name: configure
        run: cmake $superbuild $cmake_prefix_path -DENABLE_STRICT_TRY_COMPILE=ON -DCMAKE_BUILD_TYPE=RelWithDebInfo -DCMAKE_TOOLCHAIN_FILE=$(pwd)/tools/ios.toolchain.cmake -DENABLE_BITCODE=Off -DPLATFORM=${{ matrix.platform }} -DDEPLOYMENT_TARGET=14.0 -DBUILD_MAVSDK_SERVER=ON -DBUILD_SHARED_LIBS=OFF -DWERROR=ON -Bbuild/${{ matrix.name }} -H.
      - name: build
        run: cmake --build build/${{ matrix.name }} -j$(nproc)
      - uses: actions/upload-artifact@v4
        with:
          name: mavsdk_server_${{ matrix.name }}.framework
          path: ./build/${{ matrix.name }}/src/mavsdk_server/src/mavsdk_server.framework
          retention-days: 2

  iOS-XCFramework:
    name: iOS XCFramework
    needs: [macOS, iOS]
    runs-on: macos-13
    steps:
      - uses: actions/checkout@v4
        with:
          submodules: recursive
      - uses: actions/download-artifact@v4
        with:
          name: mavsdk_server_ios.framework
          path: ./build/ios/src/mavsdk_server/src/mavsdk_server.framework
      - uses: actions/download-artifact@v4
        with:
          name: mavsdk_server_ios_simulator_x64.framework
          path: ./build/ios_simulator_x64/src/mavsdk_server/src/mavsdk_server.framework
      - uses: actions/download-artifact@v4
        with:
          name: mavsdk_server_ios_simulator_arm64.framework
          path: ./build/ios_simulator_arm64/src/mavsdk_server/src/mavsdk_server.framework
      - uses: actions/download-artifact@v4
        with:
          name: mavsdk_server_macos_x64.framework
          path: ./build/macos_x64/src/mavsdk_server/src/mavsdk_server.framework
      - uses: actions/download-artifact@v4
        with:
          name: mavsdk_server_macos_arm64.framework
          path: ./build/macos_arm64/src/mavsdk_server/src/mavsdk_server.framework
      - name: Install tree
        run: brew install tree
      - name: Package
        run: bash ./src/mavsdk_server/tools/package_mavsdk_server_framework.bash
      - uses: actions/upload-artifact@v4
        with:
          name: mavsdk_server.xcframework
          path: ./build/mavsdk_server.xcframework
          retention-days: 2
      - name: Publish xcframework
        if: startsWith(github.ref, 'refs/tags/v')
        uses: svenstaro/upload-release-action@v1-release
        with:
          repo_token: ${{ secrets.GITHUB_TOKEN }}
          file: 'build/mavsdk_server.xcframework.zip'
          asset_name: 'mavsdk_server.xcframework.zip'
          tag: ${{ github.ref }}
          overwrite: true
      - name: Publish xcframework checksum
        if: startsWith(github.ref, 'refs/tags/v')
        uses: svenstaro/upload-release-action@v1-release
        with:
          repo_token: ${{ secrets.GITHUB_TOKEN }}
          file: 'build/mavsdk_server.xcframework.zip.sha256'
          asset_name: 'mavsdk_server.xcframework.zip.sha256'
          tag: ${{ github.ref }}
          overwrite: true

  Windows:
    name: Windows
    runs-on: windows-latest
    steps:
      - uses: actions/checkout@v4
        with:
          submodules: recursive
      - uses: actions/cache@v4
        id: cache
        with:
          path: ./build/release/third_party/install
          key: ${{ github.job }}-${{ hashFiles('./third_party/**') }}-4
      - name: disable superbuild on cache hit
        if: steps.cache.outputs.cache-hit == 'true'
        run: |
            echo "superbuild=-DSUPERBUILD=OFF" | Out-File -FilePath $env:GITHUB_ENV -Encoding utf8 -Append
            echo "cmake_prefix_path=-DCMAKE_PREFIX_PATH=$(pwd)/build/release/third_party/install" | Out-File -FilePath $env:GITHUB_ENV -Encoding utf8 -Append
      - name: install packages
        run: |
            choco install nasm
            echo "C:\Program Files\NASM" | Out-File -FilePath $env:GITHUB_PATH -Encoding utf8 -Append
      - name: configure
        run: cmake -G "Visual Studio 17 2022" $env:superbuild $env:cmake_prefix_path -DCMAKE_BUILD_TYPE=RelWithDebInfo -DCMAKE_INSTALL_PREFIX=build/release/install -DBUILD_MAVSDK_SERVER=ON -DBUILD_SHARED_LIBS=OFF -DWERROR=ON -Bbuild/release -S.
      - name: build
        run: cmake --build build/release -j2 --config RelWithDebInfo --target install
      - name: Create zip file mavsdk libraries
        if: startsWith(github.ref, 'refs/tags/v')
        run: cd build/release/install && 7z.exe a -tzip ../../../mavsdk-windows-x64-release.zip . && cd ../../..
      - name: Publish mavsdk libraries
        if: startsWith(github.ref, 'refs/tags/v')
        uses: svenstaro/upload-release-action@v1-release
        with:
          repo_token: ${{ secrets.GITHUB_TOKEN }}
          file: 'mavsdk-windows-x64-release.zip'
          asset_name: 'mavsdk-windows-x64-release.zip'
          tag: ${{ github.ref }}
          overwrite: true
      - name: Publish mavsdk_server binary
        if: startsWith(github.ref, 'refs/tags/v')
        uses: svenstaro/upload-release-action@v1-release
        with:
          repo_token: ${{ secrets.GITHUB_TOKEN }}
          file: 'build/release/install/bin/mavsdk_server_bin.exe'
          asset_name: 'mavsdk_server_win32.exe'
          tag: ${{ github.ref }}
          overwrite: true
