From 583805850d04ac8e16b5b72792479a08f5e41f2d Mon Sep 17 00:00:00 2001 From: David Cermak Date: Tue, 16 Dec 2025 07:52:40 +0100 Subject: [PATCH 1/3] feat(mosq): Add support for linux build --- components/mosquitto/CMakeLists.txt | 6 ++++++ components/mosquitto/examples/broker/CMakeLists.txt | 7 +++++++ .../mosquitto/examples/broker/main/example_broker.c | 2 +- .../mosquitto/examples/broker/main/idf_component.yml | 3 +++ components/mosquitto/port/broker.c | 2 ++ components/mosquitto/port/config.c | 11 +++++++++++ components/mosquitto/port/mosq_time.c | 2 +- components/mosquitto/port/priv_include/sys/syslog.h | 3 --- components/mosquitto/port/signals.c | 5 ++++- 9 files changed, 35 insertions(+), 6 deletions(-) diff --git a/components/mosquitto/CMakeLists.txt b/components/mosquitto/CMakeLists.txt index ee071fd69c..9c51863cfc 100644 --- a/components/mosquitto/CMakeLists.txt +++ b/components/mosquitto/CMakeLists.txt @@ -1,3 +1,5 @@ +idf_build_get_property(idf_target IDF_TARGET) + set(m_dir mosquitto) set(m_src_dir ${m_dir}/src) set(m_incl_dir ${m_dir}/include) @@ -94,6 +96,10 @@ target_compile_options(${COMPONENT_LIB} PRIVATE "-Wno-format") # without modifying upstream code target_link_options(${COMPONENT_LIB} INTERFACE "-Wl,--wrap=mosquitto_unpwd_check") +if(${idf_target} STREQUAL "linux") + target_link_options(${COMPONENT_LIB} INTERFACE "-Wl,--wrap=main") +endif() + # Some mosquitto source unconditionally define `_GNU_SOURCE` which collides with IDF build system # producing warning: "_GNU_SOURCE" redefined # This workarounds this issue by undefining the macro for the selected files diff --git a/components/mosquitto/examples/broker/CMakeLists.txt b/components/mosquitto/examples/broker/CMakeLists.txt index 5efb12ce92..04eb43f426 100644 --- a/components/mosquitto/examples/broker/CMakeLists.txt +++ b/components/mosquitto/examples/broker/CMakeLists.txt @@ -3,4 +3,11 @@ cmake_minimum_required(VERSION 3.16) include($ENV{IDF_PATH}/tools/cmake/project.cmake) + +if("${IDF_TARGET}" STREQUAL "linux") + list(APPEND EXTRA_COMPONENT_DIRS "../../../../common_components/linux_compat") +endif() + +idf_build_set_property(MINIMAL_BUILD ON) + project(broker) diff --git a/components/mosquitto/examples/broker/main/example_broker.c b/components/mosquitto/examples/broker/main/example_broker.c index d54073f629..00bfc47320 100644 --- a/components/mosquitto/examples/broker/main/example_broker.c +++ b/components/mosquitto/examples/broker/main/example_broker.c @@ -13,7 +13,7 @@ #include "mosq_broker.h" #include "protocol_examples_common.h" -const static char *TAG = "mqtt_broker"; +__attribute__((unused)) const static char *TAG = "mqtt_broker"; /* Basic auth credentials for the example */ #define EXAMPLE_USERNAME "testuser" diff --git a/components/mosquitto/examples/broker/main/idf_component.yml b/components/mosquitto/examples/broker/main/idf_component.yml index 3856d73b01..0eb21b3114 100644 --- a/components/mosquitto/examples/broker/main/idf_component.yml +++ b/components/mosquitto/examples/broker/main/idf_component.yml @@ -8,3 +8,6 @@ dependencies: rules: - if: idf_version >=6.0 version: ^1.0.0 + espressif/sock_utils: + version: '*' + override_path: '../../../../sock_utils' diff --git a/components/mosquitto/port/broker.c b/components/mosquitto/port/broker.c index d169e221d7..b6308cb600 100644 --- a/components/mosquitto/port/broker.c +++ b/components/mosquitto/port/broker.c @@ -9,6 +9,8 @@ #include "mosquitto_broker_internal.h" #include "memory_mosq.h" #include "mosq_broker.h" +#include +#include static struct mosquitto__listener_sock *listensock = NULL; static int listensock_count = 0; diff --git a/components/mosquitto/port/config.c b/components/mosquitto/port/config.c index 29f927ee22..4f936e449f 100644 --- a/components/mosquitto/port/config.c +++ b/components/mosquitto/port/config.c @@ -14,6 +14,7 @@ #include "utlist.h" #include "lib_load.h" #include "syslog.h" +#include #include "sdkconfig.h" @@ -244,3 +245,13 @@ pid_t fork(void) abort(); return 0; } + +#ifdef CONFIG_IDF_TARGET_LINUX +extern void app_main(void); + +int __wrap_main(int argc, char *argv[]) +{ + app_main(); + return 0; +} +#endif diff --git a/components/mosquitto/port/mosq_time.c b/components/mosquitto/port/mosq_time.c index 56158ac363..c3c8f3824d 100644 --- a/components/mosquitto/port/mosq_time.c +++ b/components/mosquitto/port/mosq_time.c @@ -8,7 +8,7 @@ #include #include "time_mosq.h" - +#include "esp_err.h" #include "esp_timer.h" void mosquitto_time_init(void) diff --git a/components/mosquitto/port/priv_include/sys/syslog.h b/components/mosquitto/port/priv_include/sys/syslog.h index a609fa623b..c29ac14c46 100644 --- a/components/mosquitto/port/priv_include/sys/syslog.h +++ b/components/mosquitto/port/priv_include/sys/syslog.h @@ -6,8 +6,6 @@ #pragma once -#define EAI_SYSTEM 11 /* system error returned in errno */ - #include "esp_log.h" #define LOG_EMERG (0) #define LOG_ALERT (1) @@ -30,6 +28,5 @@ #define syslog(sev, format, ... ) ESP_LOG_LEVEL_LOCAL(sev-2, "mosquitto", format, ##__VA_ARGS__) -#define gai_strerror(x) "gai_strerror() not supported" #define openlog(a, b, c) #define closelog() diff --git a/components/mosquitto/port/signals.c b/components/mosquitto/port/signals.c index f1559b51aa..fbf54b9467 100644 --- a/components/mosquitto/port/signals.c +++ b/components/mosquitto/port/signals.c @@ -6,8 +6,11 @@ * SPDX-FileContributor: 2024 Espressif Systems (Shanghai) CO LTD */ #include "signal.h" +#include "sdkconfig.h" -int sigprocmask (int, const sigset_t *, sigset_t *) +#ifndef CONFIG_IDF_TARGET_LINUX +int sigprocmask(int, const sigset_t *, sigset_t *) { return 0; } +#endif From ef4efa51e8a4ac94013d1e3754e75881d0d28b8f Mon Sep 17 00:00:00 2001 From: David Cermak Date: Tue, 16 Dec 2025 17:50:18 +0100 Subject: [PATCH 2/3] fix(sock_utils): Pass empty cmake for linux target In order to support linux builds, we define this component as a no-op --- components/sock_utils/CMakeLists.txt | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/components/sock_utils/CMakeLists.txt b/components/sock_utils/CMakeLists.txt index f2f39d334b..f34e9fcb18 100644 --- a/components/sock_utils/CMakeLists.txt +++ b/components/sock_utils/CMakeLists.txt @@ -1,3 +1,11 @@ +idf_build_get_property(idf_target IDF_TARGET) + +if(${idf_target} STREQUAL "linux") + # This component is no-op for linux simulator + idf_component_register() + return() +endif() + idf_component_register(SRCS "src/getnameinfo.c" "src/ifaddrs.c" "src/gai_strerror.c" From d7ed8a8534fdc6b252bb15e48a20dc8349d476ce Mon Sep 17 00:00:00 2001 From: David Cermak Date: Tue, 16 Dec 2025 17:58:43 +0100 Subject: [PATCH 3/3] fix(examples): Use local broker for mqtt tests --- .github/workflows/examples_build-host-test.yml | 4 ++++ ci/test_broker/run.sh | 12 ++++++++++++ ci/test_broker/sdkconfig.broker | 5 +++++ examples/mqtt/sdkconfig.ci | 2 +- 4 files changed, 22 insertions(+), 1 deletion(-) create mode 100755 ci/test_broker/run.sh create mode 100644 ci/test_broker/sdkconfig.broker diff --git a/.github/workflows/examples_build-host-test.yml b/.github/workflows/examples_build-host-test.yml index 2919faf0f2..ffe1428b1b 100644 --- a/.github/workflows/examples_build-host-test.yml +++ b/.github/workflows/examples_build-host-test.yml @@ -53,6 +53,8 @@ jobs: steps: - name: Checkout esp-protocols uses: actions/checkout@v4 + with: + submodules: recursive - name: Build with IDF-${{ matrix.idf_ver }} shell: bash run: | @@ -60,8 +62,10 @@ jobs: . ${IDF_PATH}/export.sh python -m pip install idf-build-apps python ./ci/build_apps.py examples/mqtt -l -t linux -r 'sdkconfig.ci' + ./ci/test_broker/run.sh timeout 5 ./examples/mqtt/build_linux/esp_mqtt_demo.elf | tee test.log || true grep 'MQTT_EVENT_DATA' test.log + pkill -f 'broker.elf' run_on_target: # Skip running on forks since it won't have access to secrets diff --git a/ci/test_broker/run.sh b/ci/test_broker/run.sh new file mode 100755 index 0000000000..cb9e2c0dc4 --- /dev/null +++ b/ci/test_broker/run.sh @@ -0,0 +1,12 @@ +#!/bin/bash + +set -e + +BROKER_PATH=components/mosquitto/examples/broker +SCRIPT_DIR=$(dirname $0) +cp ${SCRIPT_DIR}/sdkconfig.broker ${BROKER_PATH}/sdkconfig.broker +idf.py -B build -DSDKCONFIG=sdkconfig.broker -C ${BROKER_PATH} build + +./build/broker.elf & +export MOSQ_PID=$! +echo "Broker started with PID ${MOSQ_PID}" diff --git a/ci/test_broker/sdkconfig.broker b/ci/test_broker/sdkconfig.broker new file mode 100644 index 0000000000..97e24aef4a --- /dev/null +++ b/ci/test_broker/sdkconfig.broker @@ -0,0 +1,5 @@ +CONFIG_IDF_TARGET="linux" +CONFIG_EXAMPLE_BROKER_PORT=1234 +CONFIG_EXAMPLE_BROKER_RUN_LOCAL_MQTT_CLIENT=n +CONFIG_EXAMPLE_BROKER_WITH_TLS=n +CONFIG_ESP_EVENT_POST_FROM_ISR=n diff --git a/examples/mqtt/sdkconfig.ci b/examples/mqtt/sdkconfig.ci index bc8c262f38..51206bbef9 100644 --- a/examples/mqtt/sdkconfig.ci +++ b/examples/mqtt/sdkconfig.ci @@ -1,2 +1,2 @@ # CONFIG_ESP_EVENT_POST_FROM_ISR is not set -CONFIG_BROKER_URL="mqtt://${CI_MQTT_BROKER_URI}" +CONFIG_BROKER_URL="mqtt://127.0.0.1:1234"