# Paho.cmake - provide Eclipse Paho MQTT C++ (and its bundled C library) via
# FetchContent.
#
# Rationale (security): we build Paho from its OFFICIAL upstream Git repo rather
# than from the AUR. This removes the anonymous AUR packager from the trust
# chain - you trust only the Eclipse project source. The C library is pulled in
# as paho.mqtt.cpp's own pinned git submodule (PAHO_WITH_MQTT_C=ON), which is
# likewise official Eclipse source.
#
# HARDENING (recommended): the tag below is a mutable ref. For maximum integrity
# pin PAHO_CPP_TAG to a full commit SHA (immutable), or switch to URL + URL_HASH
# (SHA256) of the signed release tarball and verify the hash out-of-band. Bump
# the version deliberately, not silently.
#
# Design note: we let paho.mqtt.cpp build its own bundled C submodule
# (PAHO_WITH_MQTT_C=ON) rather than fetching paho.mqtt.c separately. The
# separate-fetch approach forces the C++ wrapper down its find_package() path,
# whose unconditional build-tree export() then drags the in-tree C target into
# an export set it does not belong to and fails at generation time. The bundled
# path keeps the C target inside the C++ wrapper's own export set, sidestepping
# that conflict entirely.

include(FetchContent)

set(PAHO_CPP_TAG "v1.4.1" CACHE STRING "Eclipse paho.mqtt.cpp git tag/commit")

# ---- Build options for the bundled Paho C and the C++ wrapper ----
set(PAHO_WITH_MQTT_C    ON  CACHE BOOL "" FORCE)  # build C from cpp's submodule
set(PAHO_BUILD_SHARED   OFF CACHE BOOL "" FORCE)
set(PAHO_BUILD_STATIC   ON  CACHE BOOL "" FORCE)
set(PAHO_WITH_SSL       OFF CACHE BOOL "" FORCE)  # current code uses plain tcp://
set(PAHO_ENABLE_TESTING OFF CACHE BOOL "" FORCE)
set(PAHO_BUILD_SAMPLES  OFF CACHE BOOL "" FORCE)
set(PAHO_BUILD_DOCUMENTATION OFF CACHE BOOL "" FORCE)
set(PAHO_BUILD_CPP_SHARED OFF CACHE BOOL "" FORCE)
set(PAHO_BUILD_CPP_STATIC ON  CACHE BOOL "" FORCE)

# Toolchain-compatibility shims for these pinned upstream releases:
#   * The bundled paho.mqtt.c declares cmake_minimum_required below the floor
#     that current CMake (>= 4.0) still accepts; allow it to configure anyway.
#   * paho.mqtt.c has `typedef unsigned int bool;`, which is illegal under C23
#     (the default on GCC >= 14 / Clang >= 18, where `bool` is a keyword). Pin
#     the C dialect to C17 for the duration of the fetch so it compiles.
set(_fgc_c_std_save "${CMAKE_C_STANDARD}")
set(CMAKE_C_STANDARD 17)
set(CMAKE_POLICY_VERSION_MINIMUM 3.5)

FetchContent_Declare(paho_mqtt_cpp
  GIT_REPOSITORY https://github.com/eclipse-paho/paho.mqtt.cpp.git
  GIT_TAG        ${PAHO_CPP_TAG}
  GIT_SHALLOW    TRUE
)
FetchContent_MakeAvailable(paho_mqtt_cpp)

unset(CMAKE_POLICY_VERSION_MINIMUM)
set(CMAKE_C_STANDARD "${_fgc_c_std_save}")

# Provide a stable alias the top-level CMakeLists links against, regardless of
# the exact target name the upstream version exports.
if(NOT TARGET PahoMqttCpp::paho-mqttpp3-static)
  if(TARGET paho-mqttpp3-static)
    add_library(PahoMqttCpp::paho-mqttpp3-static ALIAS paho-mqttpp3-static)
  elseif(TARGET paho-mqttpp3)
    add_library(PahoMqttCpp::paho-mqttpp3-static ALIAS paho-mqttpp3)
  else()
    message(FATAL_ERROR "Paho C++ static target not found after FetchContent; "
                        "check PAHO_CPP_TAG and upstream target names.")
  endif()
endif()
