Screenplay: Integrating libmosquitto (MQTT - Handwritten Find-Module)

First: Clean Up Toplevel CMakeLists.txt

  • Cram SQLite3 stuff into into cmake/sqlite3.cmake

    find_package(SQLite3)
    
    # to be substituted as C macros
    if (SQLite3_FOUND)
      set(CONFIG_HAVE_SQLITE3 1)
    else()
      set(CONFIG_HAVE_SQLITE3 0)
    endif()
    
  • Include from tolevel

  • Leave configure_file() in place, toplevel (because of its prominence)

Add MQTT Capability To Datalogger

External libmosquitto Dependency

  • No find module that comes with CMake

  • ⟶ write one myself

    # Usage:
    #
    # if (Mosquitto_FOUND)
    #    target_link_libraries(MyTarget Mosquitto::Mosquitto)
    # endif()
    
    find_path(Mosquitto_INCLUDE_DIR mosquitto.h)
    find_library(Mosquitto_LIBRARY NAMES mosquitto)
    include(FindPackageHandleStandardArgs)
    
    find_package_handle_standard_args(
      Mosquitto 
      FOUND_VAR Mosquitto_FOUND
      REQUIRED_VARS Mosquitto_LIBRARY Mosquitto_INCLUDE_DIR
    )
    
    set(Mosquitto_LIBRARIES ${Mosquitto_LIBRARY})
    
    mark_as_advanced(Mosquitto_INCLUDE_DIR Mosquitto_LIBRARY)
    
    add_library(Mosquitto::Mosquitto INTERFACE IMPORTED)
    target_link_libraries(Mosquitto::Mosquitto INTERFACE ${Mosquitto_LIBRARIES})
    target_include_directories(Mosquitto::Mosquitto INTERFACE ${Mosquitto_INCLUDE_DIR})
    
  • add_library(... INTERFACE IMPORTED) (see add_library)

  • Glue all together in cmake/mosquitto.cmake, and include from toplevel CMakeLists.txt

    find_package(Mosquitto)     # <--- finds FindMosquitto.cmake in ${CMAKE_MODULE_PATH}
    
    # to be substituted as C macros
    if (Mosquitto_FOUND)
      set(CONFIG_HAVE_MOSQUITTO 1)
    else()
      set(CONFIG_HAVE_MOSQUITTO 0)
    endif()
    
  • Add HAVE_MOSQUITTO to project-config.h.in

  • Et voila!