CMakeLists.txt 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. cmake_minimum_required(VERSION 3.9)
  2. project( qmqtt VERSION 1.0.0 )
  3. include( GNUInstallDirs ) # needed to define vars used in install() directives.
  4. # ===================================================================
  5. # Configurable options
  6. option( ${PROJECT_NAME}_SHARED "Build a shared library. Turn off for static." ON )
  7. option( ${PROJECT_NAME}_WEBSOCKETS "Enable WebSockets for MQTT" OFF )
  8. option( ${PROJECT_NAME}_SSL "Enable SSL support for MQTT" ON )
  9. if ( ${PROJECT_NAME}_SHARED )
  10. set( library_build_type SHARED )
  11. set( library_install_component Library )
  12. else()
  13. set( library_build_type STATIC )
  14. set( library_install_component Devel )
  15. endif()
  16. set( ws_component )
  17. set( ws_libname )
  18. set( qt5_min_version "5.3.0" )
  19. if ( ${PROJECT_NAME}_WEBSOCKETS )
  20. set( ws_component WebSockets )
  21. set( ws_libname Qt5::WebSockets )
  22. set( qt5_min_version "5.7.0" )
  23. endif()
  24. if ( NOT ${PROJECT_NAME}_SSL)
  25. set( ssl_defs QT_NO_SSL )
  26. endif()
  27. find_package( Qt5 ${qt5_min_version} COMPONENTS Core Network ${ws_component} CONFIG REQUIRED )
  28. set( CMAKE_AUTOMOC ON )
  29. cmake_policy( SET CMP0020 NEW ) # Automatically link Qt executables to qtmain target on Windows.
  30. # ===================================================================
  31. # Project files
  32. set( PUBLIC_HEADERS
  33. src/mqtt/qmqtt_global.h
  34. src/mqtt/qmqtt.h
  35. src/mqtt/qmqtt_client.h
  36. src/mqtt/qmqtt_frame.h
  37. src/mqtt/qmqtt_message.h
  38. src/mqtt/qmqtt_routesubscription.h
  39. src/mqtt/qmqtt_routedmessage.h
  40. src/mqtt/qmqtt_router.h
  41. src/mqtt/qmqtt_networkinterface.h
  42. src/mqtt/qmqtt_socketinterface.h
  43. src/mqtt/qmqtt_timerinterface.h
  44. )
  45. set( PRIVATE_HEADERS
  46. src/mqtt/qmqtt_client_p.h
  47. src/mqtt/qmqtt_message_p.h
  48. src/mqtt/qmqtt_network_p.h
  49. src/mqtt/qmqtt_socket_p.h
  50. src/mqtt/qmqtt_timer_p.h
  51. )
  52. set( SOURCES
  53. src/mqtt/qmqtt_client_p.cpp
  54. src/mqtt/qmqtt_client.cpp
  55. src/mqtt/qmqtt_frame.cpp
  56. src/mqtt/qmqtt_message.cpp
  57. src/mqtt/qmqtt_network.cpp
  58. src/mqtt/qmqtt_routesubscription.cpp
  59. src/mqtt/qmqtt_router.cpp
  60. src/mqtt/qmqtt_socket.cpp
  61. src/mqtt/qmqtt_timer.cpp
  62. )
  63. if ( ${PROJECT_NAME}_WEBSOCKETS )
  64. list( APPEND PRIVATE_HEADERS
  65. src/mqtt/qmqtt_websocket_p.h
  66. src/mqtt/qmqtt_websocketiodevice_p.h
  67. )
  68. list( APPEND SOURCES
  69. src/mqtt/qmqtt_websocket.cpp
  70. src/mqtt/qmqtt_websocketiodevice.cpp
  71. )
  72. endif()
  73. if ( ${PROJECT_NAME}_SSL)
  74. list( APPEND PRIVATE_HEADERS
  75. src/mqtt/qmqtt_ssl_socket_p.h
  76. )
  77. list( APPEND SOURCES
  78. src/mqtt/qmqtt_ssl_socket.cpp
  79. )
  80. endif()
  81. # Mark public headers as such
  82. set_source_files_properties( ${PUBLIC_HEADERS} PROPERTIES PUBLIC_HEADER 1 )
  83. # ===================================================================
  84. # Library target
  85. # Library has the same name as the project
  86. add_library( ${PROJECT_NAME} ${library_build_type} ${SOURCES} ${PUBLIC_HEADERS} ${PRIVATE_HEADERS} )
  87. target_link_libraries( ${PROJECT_NAME} PUBLIC Qt5::Core Qt5::Network ${ws_libname} )
  88. target_compile_definitions( ${PROJECT_NAME}
  89. PRIVATE
  90. QT_NO_CAST_FROM_ASCII
  91. QT_NO_CAST_TO_ASCII
  92. QT_BUILD_QMQTT_LIB
  93. ${ssl_defs}
  94. )
  95. # Where to look for headers while compiling the target or when compiling against
  96. # the target.
  97. target_include_directories( ${PROJECT_NAME}
  98. PUBLIC
  99. $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/src/mqtt>
  100. $<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>
  101. )
  102. set_target_properties( ${PROJECT_NAME}
  103. PROPERTIES
  104. VERSION ${PROJECT_VERSION}
  105. SOVERSION ${PROJECT_VERSION_MAJOR}
  106. CXX_STANDARD 11
  107. CXX_STANDARD_REQUIRED OFF # Whether CXX_STANDARD is enforced
  108. )
  109. if ( ${CMAKE_HOST_WIN32} )
  110. # On Windows, libraries are not generally prefixed with "lib".
  111. # If left unchanged, cmake will still add this prefix.
  112. set_target_properties( ${PROJECT_NAME}
  113. PROPERTIES
  114. PREFIX ""
  115. IMPORT_PREFIX ""
  116. )
  117. endif()
  118. # ===================================================================
  119. # Installation
  120. # Rule to install runtime components (ie: the shared library)
  121. install(
  122. TARGETS ${PROJECT_NAME}
  123. EXPORT ${PROJECT_NAME}
  124. COMPONENT ${library_install_component}
  125. RUNTIME DESTINATION "${CMAKE_INSTALL_BINDIR}"
  126. LIBRARY DESTINATION "${CMAKE_INSTALL_LIBDIR}"
  127. ARCHIVE DESTINATION "${CMAKE_INSTALL_LIBDIR}"
  128. )
  129. install(
  130. EXPORT ${PROJECT_NAME}
  131. DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME}"
  132. FILE ${PROJECT_NAME}Targets.cmake
  133. COMPONENT Devel
  134. )
  135. install(
  136. FILES ${PUBLIC_HEADERS}
  137. DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}
  138. COMPONENT Devel
  139. OPTIONAL
  140. )
  141. # Generate a CMake file into the installation, to easily use the library
  142. install(
  143. EXPORT ${PROJECT_NAME}
  144. DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME}"
  145. COMPONENT Devel
  146. OPTIONAL
  147. )
  148. include( CMakePackageConfigHelpers )
  149. file( WRITE "${CMAKE_CURRENT_BINARY_DIR}/qmqttConfig.cmake.in"
  150. "@PACKAGE_INIT@\ninclude( \${CMAKE_CURRENT_LIST_DIR}/qmqttTargets.cmake )" )
  151. configure_package_config_file(
  152. "${CMAKE_CURRENT_BINARY_DIR}/qmqttConfig.cmake.in"
  153. "qmqttConfig.cmake"
  154. INSTALL_DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME}
  155. PATH_VARS CMAKE_INSTALL_LIBDIR
  156. )
  157. write_basic_package_version_file(
  158. "qmqttConfigVersion.cmake"
  159. VERSION ${PROJECT_VERSION}
  160. COMPATIBILITY SameMajorVersion
  161. )
  162. install(
  163. FILES
  164. ${CMAKE_CURRENT_BINARY_DIR}/qmqttConfig.cmake
  165. ${CMAKE_CURRENT_BINARY_DIR}/qmqttConfigVersion.cmake
  166. DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME}
  167. COMPONENT Devel
  168. )
  169. # ===================================================================
  170. # Package creation
  171. set( CPACK_PACKAGE_NAME ${PROJECT_NAME} )
  172. set( CPACK_PACKAGE_VENDOR "emqtt" ) # Github project owner
  173. set( CPACK_PACKAGE_DESCRIPTION_SUMMARY "mqtt client for Qt" )
  174. set( CPACK_PACKAGE_HOMEPAGE_URL "https://github.com/emqtt/qmqtt" )
  175. set( CPACK_PACKAGE_CONTACT "https://github.com/emqtt/qmqtt" )
  176. set( CPACK_PACKAGE_VERSION_MAJOR ${PROJECT_VERSION_MAJOR} )
  177. set( CPACK_PACKAGE_VERSION_MINOR ${PROJECT_VERSION_MINOR} )
  178. set( CPACK_PACKAGE_VERSION_PATCH ${PROJECT_VERSION_PATCH} )
  179. set( CPACK_PACKAGE_VERSION ${CPACK_PACKAGE_VERSION_MAJOR}.${CPACK_PACKAGE_VERSION_MINOR}.${CPACK_PACKAGE_VERSION_PATCH} )
  180. set( CPACK_PACKAGE_INSTALL_DIRECTORY ${PROJECT_NAME} )
  181. set( CPACK_RESOURCE_FILE_LICENSE "${CMAKE_CURRENT_SOURCE_DIR}/edl-v10" )
  182. set( CPACK_RESOURCE_FILE_README "${CMAKE_CURRENT_SOURCE_DIR}/README.md" )
  183. set( CPACK_COMPONENT_Library_DISPLAY_NAME "QMQTT Library" )
  184. set( CPACK_COMPONENT_Library_DESCRIPTION "The QMQTT binary library." )
  185. set( CPACK_COMPONENT_Library_REQUIRED 1 )
  186. set( CPACK_COMPONENT_Devel_DISPLAY_NAME "QMQTT Development Files" )
  187. set( CPACK_COMPONENT_Devel_DESCRIPTION "Development files for compiling against QMQTT." )
  188. set( CPACK_COMPONENT_Devel_REQUIRED 0 )
  189. if( ${CMAKE_SYSTEM_NAME} STREQUAL "Linux" )
  190. if ( "${CPACK_PACKAGE_ARCHITECTURE}" STREQUAL "" )
  191. # Note: the architecture should default to the local architecture, but it
  192. # in fact comes up empty. We call `uname -m` to ask the kernel instead.
  193. EXECUTE_PROCESS( COMMAND uname -m COMMAND tr -d '\n' OUTPUT_VARIABLE CPACK_PACKAGE_ARCHITECTURE )
  194. endif()
  195. set( CPACK_INCLUDE_TOPLEVEL_DIRECTORY 1 )
  196. set( CPACK_PACKAGE_RELEASE 1 )
  197. # RPM - https://cmake.org/cmake/help/latest/cpack_gen/rpm.html
  198. set( CPACK_RPM_PACKAGE_RELEASE ${CPACK_PACKAGE_RELEASE} )
  199. set( CPACK_RPM_PACKAGE_ARCHITECTURE ${CPACK_PACKAGE_ARCHITECTURE} )
  200. set( CPACK_RPM_PACKAGE_DESCRIPTION ${CPACK_PACKAGE_DESCRIPTION_SUMMARY} )
  201. set( CPACK_RPM_PACKAGE_URL ${CPACK_PACKAGE_HOMEPAGE_URL} )
  202. set( CPACK_RPM_PACKAGE_LICENSE "EPL-1 AND EDL-1" )
  203. set( CPACK_RPM_COMPONENT_INSTALL 1 )
  204. set( CPACK_RPM_COMPRESSION_TYPE "xz" )
  205. set( CPACK_RPM_PACKAGE_AUTOPROV 1 )
  206. set( CPACK_RPM_Library_PACKAGE_SUMMARY ${CPACK_COMPONENT_Library_DESCRIPTION} )
  207. set( CPACK_RPM_Library_PACKAGE_ARCHITECTURE ${CPACK_RPM_PACKAGE_ARCHITECTURE} )
  208. set( CPACK_RPM_Library_PACKAGE_NAME ${CPACK_PACKAGE_NAME} )
  209. set( CPACK_RPM_Library_FILE_NAME "RPM-DEFAULT" )
  210. set( CPACK_RPM_Devel_PACKAGE_REQUIRES "cmake >= ${CMAKE_MINIMUM_REQUIRED_VERSION},qmqtt >= ${CPACK_PACKAGE_VERSION}" )
  211. set( CPACK_RPM_Devel_PACKAGE_SUMMARY ${CPACK_COMPONENT_Devel_DESCRIPTION} )
  212. set( CPACK_RPM_Devel_PACKAGE_ARCHITECTURE ${CPACK_RPM_PACKAGE_ARCHITECTURE} )
  213. set( CPACK_RPM_Devel_PACKAGE_NAME "${CPACK_PACKAGE_NAME}-devel" )
  214. set( CPACK_RPM_Devel_FILE_NAME "RPM-DEFAULT" )
  215. # DEB - https://cmake.org/cmake/help/latest/cpack_gen/deb.html
  216. set( CPACK_DEBIAN_PACKAGE_RELEASE ${CPACK_PACKAGE_RELEASE} )
  217. set( CPACK_DEBIAN_PACKAGE_HOMEPAGE ${CPACK_PACKAGE_HOMEPAGE_URL} )
  218. set( CPACK_DEB_COMPONENT_INSTALL 1 )
  219. set( CPACK_DEBIAN_COMPRESSION_TYPE "xz")
  220. if ( ${CPACK_PACKAGE_ARCHITECTURE} STREQUAL "x86_64" )
  221. set( CPACK_DEBIAN_PACKAGE_ARCHITECTURE "amd64" ) # DEB doesn't always use the kernel's arch name
  222. else()
  223. set( CPACK_DEBIAN_PACKAGE_ARCHITECTURE ${CPACK_PACKAGE_ARCHITECTURE} )
  224. endif()
  225. set( CPACK_DEBIAN_FILE_NAME "DEB-DEFAULT" ) # Use default naming scheme
  226. set( CPACK_DEBIAN_LIBRARY_PACKAGE_NAME ${CPACK_PACKAGE_NAME} )
  227. set( CPACK_DEBIAN_LIBRARY_PACKAGE_SHLIBDEPS 1 )
  228. set( CPACK_DEBIAN_DEVEL_PACKAGE_DEPENDS "cmake (>= ${CMAKE_MINIMUM_REQUIRED_VERSION}), qmqtt (>= ${CPACK_PACKAGE_VERSION})" )
  229. set( CPACK_DEBIAN_DEVEL_PACKAGE_NAME "${CPACK_PACKAGE_NAME}-dev" )
  230. elseif( ${CMAKE_HOST_WIN32} )
  231. set( CPACK_NSIS_ENABLE_UNINSTALL_BEFORE_INSTALL ON )
  232. set( CPACK_NSIS_DISPLAY_NAME ${PROJECT_NAME} )
  233. set( CPACK_NSIS_PACKAGE_NAME ${PROJECT_NAME} )
  234. set( CPACK_NSIS_URL_INFO_ABOUT ${CPACK_PACKAGE_HOMEPAGE_URL} )
  235. endif()
  236. # This must always be last!
  237. include(CPack)