From d830c410638577e1759bcccfdec5f17907973bb0 Mon Sep 17 00:00:00 2001 From: Thad House Date: Fri, 12 Dec 2025 19:39:26 -0800 Subject: [PATCH] [wpinet] Add option to use directly linked Avahi from cmake (#8469) --- .clang-tidy | 3 +++ CMakeLists.txt | 1 + wpinet/CMakeLists.txt | 15 ++++++++++++ wpinet/src/main/native/linux/AvahiClient.cpp | 4 ++++ wpinet/src/main/native/linux/AvahiClient.hpp | 24 ++++++++++++++++++-- 5 files changed, 45 insertions(+), 2 deletions(-) diff --git a/.clang-tidy b/.clang-tidy index 33d48c6044..1c902d5b9e 100644 --- a/.clang-tidy +++ b/.clang-tidy @@ -68,4 +68,7 @@ Checks: modernize-use-override, modernize-use-using, readability-braces-around-statements' +CheckOptions: + - key: modernize-use-using.IgnoreExternC + value: 'true' FormatStyle: file diff --git a/CMakeLists.txt b/CMakeLists.txt index e9118a6fe1..d2408b0c36 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -96,6 +96,7 @@ option(WITH_BENCHMARK "Build the benchmark project" ON) option(USE_SYSTEM_FMTLIB "Use system fmtlib" OFF) option(USE_SYSTEM_LIBUV "Use system libuv" OFF) option(USE_SYSTEM_EIGEN "Use system eigen" OFF) +option(USE_LINKED_AVAHI "Use directly linked Avahi instead of loading at runtime" OFF) # Options for location of OpenCV Java. set(OPENCV_JAVA_INSTALL_DIR "" CACHE PATH "Location to search for the OpenCV jar file") diff --git a/wpinet/CMakeLists.txt b/wpinet/CMakeLists.txt index b4b5a62d65..af645178e3 100644 --- a/wpinet/CMakeLists.txt +++ b/wpinet/CMakeLists.txt @@ -128,6 +128,21 @@ target_compile_features(wpinet PUBLIC cxx_std_20) wpilib_target_warnings(wpinet) target_link_libraries(wpinet PUBLIC wpiutil) +if(USE_LINKED_AVAHI) + find_library(AVAHI_COMMON REQUIRED NAMES avahi-common) + if(AVAHI_COMMON) + message(STATUS "Found avahi common: ${AVAHI_COMMON}") + endif() + + find_library(AVAHI_CLIENT REQUIRED NAMES avahi-client) + if(AVAHI_CLIENT) + message(STATUS "Found avahi client: ${AVAHI_CLIENT}") + endif() + + target_link_libraries(wpinet PUBLIC ${AVAHI_COMMON} ${AVAHI_CLIENT}) + target_compile_definitions(wpinet PRIVATE DIRECT_LINK_AVAHI) +endif() + if(NOT USE_SYSTEM_LIBUV) target_sources(wpinet PRIVATE ${uv_native_src}) install( diff --git a/wpinet/src/main/native/linux/AvahiClient.cpp b/wpinet/src/main/native/linux/AvahiClient.cpp index 7eee1c0020..a9a19bcd58 100644 --- a/wpinet/src/main/native/linux/AvahiClient.cpp +++ b/wpinet/src/main/native/linux/AvahiClient.cpp @@ -11,6 +11,9 @@ using namespace wpi::net; +#ifdef DIRECT_LINK_AVAHI +#define AvahiFunctionLoad(snake_name) snake_name = &::avahi_##snake_name +#else #define AvahiFunctionLoad(snake_name) \ do { \ snake_name = \ @@ -19,6 +22,7 @@ using namespace wpi::net; return; \ } \ } while (false) +#endif AvahiFunctionTable::AvahiFunctionTable() { void* lib = dlopen("libavahi-common.so.3", RTLD_LAZY); diff --git a/wpinet/src/main/native/linux/AvahiClient.hpp b/wpinet/src/main/native/linux/AvahiClient.hpp index ede5276411..15fb3d836c 100644 --- a/wpinet/src/main/native/linux/AvahiClient.hpp +++ b/wpinet/src/main/native/linux/AvahiClient.hpp @@ -8,6 +8,20 @@ #include +#ifdef DIRECT_LINK_AVAHI +#include "avahi-client/client.h" +#include "avahi-client/lookup.h" +#include "avahi-client/publish.h" +#include "avahi-common/address.h" +#include "avahi-common/alternative.h" +#include "avahi-common/domain.h" +#include "avahi-common/error.h" +#include "avahi-common/malloc.h" +#include "avahi-common/strlst.h" +#include "avahi-common/thread-watch.h" + +#else + /*** This file is part of avahi. avahi is free software; you can redistribute it and/or modify it @@ -24,6 +38,8 @@ USA. ***/ +extern "C" { + typedef struct AvahiPoll AvahiPoll; typedef enum { @@ -223,6 +239,10 @@ enum { AVAHI_ERR_MAX = -54 }; +} // extern "C" + +#endif + namespace wpi::net { class AvahiFunctionTable { public: @@ -235,8 +255,8 @@ class AvahiFunctionTable { AvahiFunction(threaded_poll_get, const AvahiPoll*, (AvahiThreadedPoll*)); AvahiFunction(threaded_poll_start, int, (AvahiThreadedPoll*)); AvahiFunction(threaded_poll_stop, int, (AvahiThreadedPoll*)); - AvahiFunction(threaded_poll_lock, int, (AvahiThreadedPoll*)); - AvahiFunction(threaded_poll_unlock, int, (AvahiThreadedPoll*)); + AvahiFunction(threaded_poll_lock, void, (AvahiThreadedPoll*)); + AvahiFunction(threaded_poll_unlock, void, (AvahiThreadedPoll*)); AvahiFunction(client_new, AvahiClient*, (const AvahiPoll* poll_api, AvahiClientFlags flags,