Skip to content

Commit

Permalink
WIP: adapt cmake to find mosquitto on windows
Browse files Browse the repository at this point in the history
  • Loading branch information
marcothaller committed Nov 8, 2024
1 parent 0986ae1 commit d7d6082
Show file tree
Hide file tree
Showing 5 changed files with 116 additions and 29 deletions.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
61 changes: 61 additions & 0 deletions cmake/FindMosquitto.cmake
Original file line number Diff line number Diff line change
@@ -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()
20 changes: 0 additions & 20 deletions cmake/dependencies.cmake
Original file line number Diff line number Diff line change
@@ -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)
Expand Down
61 changes: 53 additions & 8 deletions cmake/dependencies/mosquitto.cmake
Original file line number Diff line number Diff line change
@@ -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"
# $<TARGET_FILE_DIR:app>)
# add_custom_command(TARGET app POST_BUILD
# COMMAND ${CMAKE_COMMAND} -E copy_if_different
# "C:/Program Files/mosquitto/mosquittopp.dll"
# $<TARGET_FILE_DIR:app>)
# add_custom_command(TARGET app POST_BUILD
# COMMAND ${CMAKE_COMMAND} -E copy_if_different
# "C:/Program Files/mosquitto/libcrypto-3-x64.dll"
# $<TARGET_FILE_DIR:app>)
# add_custom_command(TARGET app POST_BUILD
# COMMAND ${CMAKE_COMMAND} -E copy_if_different
# "C:/Program Files/mosquitto/libssl-3-x64.dll"
# $<TARGET_FILE_DIR:app>)
# add_custom_command(TARGET app POST_BUILD
# COMMAND ${CMAKE_COMMAND} -E copy_if_different
# "C:/Program Files/mosquitto/pthreadVC3.dll"
# $<TARGET_FILE_DIR:app>)
# endif()


file (DOWNLOAD
https://test.mosquitto.org/ssl/mosquitto.org.crt
${CMAKE_BINARY_DIR}/mosquitto.org.crt
)

#find_package(libmosquitto REQUIRED)

Expand Down
2 changes: 1 addition & 1 deletion src/mqtt/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
)

0 comments on commit d7d6082

Please sign in to comment.