From d7d60823a7cc9e0aaedf44b7db42e84630be5c28 Mon Sep 17 00:00:00 2001 From: Marco Thaller Date: Fri, 8 Nov 2024 07:38:20 +0100 Subject: [PATCH] WIP: adapt cmake to find mosquitto on windows --- CMakeLists.txt | 1 + cmake/FindMosquitto.cmake | 61 ++++++++++++++++++++++++++++++ cmake/dependencies.cmake | 20 ---------- cmake/dependencies/mosquitto.cmake | 61 ++++++++++++++++++++++++++---- src/mqtt/CMakeLists.txt | 2 +- 5 files changed, 116 insertions(+), 29 deletions(-) create mode 100644 cmake/FindMosquitto.cmake diff --git a/CMakeLists.txt b/CMakeLists.txt index 4018a00..af43d13 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -15,6 +15,7 @@ option(BUILD_INTEGRATION_KDGUI_SLINT "Build KDGui/Slint Integration" ON) option(BUILD_INTEGRATION_MQTT "Build MQTT Integration" ON) # this will be overriden in dependencies.cmake for now option(BUILD_TESTS "Build Tests" ON) +list(APPEND CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}/cmake") list(APPEND CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}/cmake/ECM/find-modules") include(CTest) diff --git a/cmake/FindMosquitto.cmake b/cmake/FindMosquitto.cmake new file mode 100644 index 0000000..c28c1be --- /dev/null +++ b/cmake/FindMosquitto.cmake @@ -0,0 +1,61 @@ +# FindMosquitto.cmake - Locate the Mosquitto library and headers +# This module defines the following variables: +# Mosquitto_FOUND - Set to TRUE if the library and headers are found +# Mosquitto_INCLUDE_DIRS - Path to the Mosquitto headers +# Mosquitto_LIBRARIES - Path to the Mosquitto library +# It also defines an imported target `Mosquitto::Mosquitto` if found + +# Check for platform-specific library name +if (WIN32) + set(MOSQUITTO_LIB_NAMES mosquitto) # On Windows, the library is usually named `mosquitto.lib` +else () + set(MOSQUITTO_LIB_NAMES mosquitto) # On Linux, it is usually named `libmosquitto.so` or `libmosquitto.a` +endif () + +# Locate the Mosquitto include directory +find_path(Mosquitto_INCLUDE_DIR + NAMES mosquitto.h + HINTS + # Custom hint paths for common installation locations on Linux + /usr/include + /usr/local/include + # Custom hint paths for Windows (assuming installation under Program Files or a common directory) + $ENV{PROGRAMFILES}/mosquitto/devel + #$ENV{PROGRAMFILES(X86)}/mosquitto/devel + PATH_SUFFIXES include + DOC "Path to the Mosquitto include directory" +) + +# Locate the Mosquitto library +find_library(Mosquitto_LIBRARY + NAMES ${MOSQUITTO_LIB_NAMES} + HINTS + # Custom hint paths for common library locations on Linux + /usr/lib + /usr/local/lib + # Custom hint paths for Windows + $ENV{PROGRAMFILES}/mosquitto/devel + #$ENV{PROGRAMFILES(X86)}/mosquitto/devel + PATH_SUFFIXES lib + DOC "Path to the Mosquitto library" +) + +# Use standard package handling to verify both library and include paths were found +include(FindPackageHandleStandardArgs) +find_package_handle_standard_args(Mosquitto + REQUIRED_VARS Mosquitto_LIBRARY Mosquitto_INCLUDE_DIR + VERSION_VAR Mosquitto_VERSION +) + +# Define convenient variables for include directories and library paths +set(Mosquitto_INCLUDE_DIRS ${Mosquitto_INCLUDE_DIR}) +set(Mosquitto_LIBRARIES ${Mosquitto_LIBRARY}) + +# If Mosquitto is found, define an imported target for linking +if (Mosquitto_FOUND) + add_library(Mosquitto::Mosquitto UNKNOWN IMPORTED) + set_target_properties(Mosquitto::Mosquitto PROPERTIES + INTERFACE_INCLUDE_DIRECTORIES "${Mosquitto_INCLUDE_DIRS}" + IMPORTED_LOCATION "${Mosquitto_LIBRARIES}" + ) +endif() diff --git a/cmake/dependencies.cmake b/cmake/dependencies.cmake index 356c8f9..46bcb9b 100644 --- a/cmake/dependencies.cmake +++ b/cmake/dependencies.cmake @@ -1,23 +1,3 @@ -########################################################################### -# TODO -# check here if mosquitto is present for now. -# override BUILD_INTEGRATION_MQTT accordingly. -# remove this as soon as we support build of libmosquitto on all OSes -# and we are always able to build mqtt integration in case -# BUILD_INTEGRATION_MQTT is set to ON. -find_package(PkgConfig) - -if (UNIX AND PKG_CONFIG_FOUND) - pkg_check_modules(Mosquitto IMPORTED_TARGET libmosquitto REQUIRED) -endif() - -if (Mosquitto_FOUND AND BUILD_INTEGRATION_MQTT) - set(BUILD_INTEGRATION_MQTT ON) -else () - set(BUILD_INTEGRATION_MQTT OFF) -endif() -########################################################################### - include(FetchContent) include(cmake/dependencies/kdutils.cmake) diff --git a/cmake/dependencies/mosquitto.cmake b/cmake/dependencies/mosquitto.cmake index 24705b3..0a749aa 100644 --- a/cmake/dependencies/mosquitto.cmake +++ b/cmake/dependencies/mosquitto.cmake @@ -1,14 +1,59 @@ +find_package(Mosquitto REQUIRED) + +if (Mosquitto_FOUND) + file (DOWNLOAD + https://test.mosquitto.org/ssl/mosquitto.org.crt + ${CMAKE_BINARY_DIR}/mosquitto.org.crt + ) +else() + message("Mosquitto could not be located. We don't support building mosquitto from souce (yet). Please install mosquitto manually.") +endif () + #include(ExternalProject) -#find_package(PkgConfig) -#if(UNIX AND PKG_CONFIG_FOUND) - #pkg_check_modules(Mosquitto IMPORTED_TARGET libmosquittopp REQUIRED) -#endif() +# if(UNIX AND PKG_CONFIG_FOUND) +# find_package(PkgConfig) +# pkg_check_modules(Mosquitto IMPORTED_TARGET libmosquittopp REQUIRED) +# elseif(WIN32) +# # Look for libmosquitto library +# find_library(MOSQUITTO_LIB mosquitto HINTS "C:/Program Files/mosquitto/devel") + +# # Look for mosquitto header files +# find_path(MOSQUITTO_INCLUDE mosquitto.h HINTS "C:/Program Files/mosquitto/devel") + +# if (MOSQUITTO_LIB AND MOSQUITTO_INCLUDE) +# message(STATUS "libmosquitto found on Windows!") +# # include_directories(${MOSQUITTO_INCLUDE}) +# # target_link_libraries(app ${MOSQUITTO_LIB}) +# else() +# message(FATAL_ERROR "libmosquitto not found on Windows!") +# endif() +# endif() + +# if (WIN32) +# add_custom_command(TARGET app POST_BUILD +# COMMAND ${CMAKE_COMMAND} -E copy_if_different +# "C:/Program Files/mosquitto/mosquitto.dll" +# $) +# add_custom_command(TARGET app POST_BUILD +# COMMAND ${CMAKE_COMMAND} -E copy_if_different +# "C:/Program Files/mosquitto/mosquittopp.dll" +# $) +# add_custom_command(TARGET app POST_BUILD +# COMMAND ${CMAKE_COMMAND} -E copy_if_different +# "C:/Program Files/mosquitto/libcrypto-3-x64.dll" +# $) +# add_custom_command(TARGET app POST_BUILD +# COMMAND ${CMAKE_COMMAND} -E copy_if_different +# "C:/Program Files/mosquitto/libssl-3-x64.dll" +# $) +# add_custom_command(TARGET app POST_BUILD +# COMMAND ${CMAKE_COMMAND} -E copy_if_different +# "C:/Program Files/mosquitto/pthreadVC3.dll" +# $) +# endif() + -file (DOWNLOAD - https://test.mosquitto.org/ssl/mosquitto.org.crt - ${CMAKE_BINARY_DIR}/mosquitto.org.crt -) #find_package(libmosquitto REQUIRED) diff --git a/src/mqtt/CMakeLists.txt b/src/mqtt/CMakeLists.txt index 5b9ba1c..1dfa53c 100644 --- a/src/mqtt/CMakeLists.txt +++ b/src/mqtt/CMakeLists.txt @@ -5,5 +5,5 @@ add_library(mecaps::${TARGET_NAME} ALIAS ${TARGET_NAME}) target_link_libraries(${TARGET_NAME} PUBLIC KDUtils::KDFoundation - PUBLIC PkgConfig::Mosquitto + PUBLIC Mosquitto::Mosquitto )