|
| 1 | +# ############################################################################## |
| 2 | +# apps/netutils/paho_mqtt/CMakeLists.txt |
| 3 | +# |
| 4 | +# Licensed to the Apache Software Foundation (ASF) under one or more contributor |
| 5 | +# license agreements. See the NOTICE file distributed with this work for |
| 6 | +# additional information regarding copyright ownership. The ASF licenses this |
| 7 | +# file to you under the Apache License, Version 2.0 (the "License"); you may not |
| 8 | +# use this file except in compliance with the License. You may obtain a copy of |
| 9 | +# the License at |
| 10 | +# |
| 11 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | +# |
| 13 | +# Unless required by applicable law or agreed to in writing, software |
| 14 | +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 15 | +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 16 | +# License for the specific language governing permissions and limitations under |
| 17 | +# the License. |
| 18 | +# |
| 19 | +# ############################################################################## |
| 20 | + |
| 21 | +if(CONFIG_LIB_MQTT5) |
| 22 | + |
| 23 | + # ############################################################################ |
| 24 | + # Config and Fetch Paho MQTT C library |
| 25 | + # ############################################################################ |
| 26 | + |
| 27 | + set(PAHO_MQTT_DIR ${CMAKE_CURRENT_LIST_DIR}/paho_mqtt) |
| 28 | + |
| 29 | + if(NOT EXISTS ${PAHO_MQTT_DIR}) |
| 30 | + # Default version if not specified in config |
| 31 | + if(DEFINED CONFIG_NETUTILS_PAHO_MQTT_VERSION) |
| 32 | + set(PAHO_MQTT_VERSION ${CONFIG_NETUTILS_PAHO_MQTT_VERSION}) |
| 33 | + else() |
| 34 | + set(PAHO_MQTT_VERSION "1.3.15") |
| 35 | + endif() |
| 36 | + |
| 37 | + set(PAHO_MQTT_URL "https://github.com/eclipse-paho/paho.mqtt.c/archive") |
| 38 | + FetchContent_Declare( |
| 39 | + paho_mqtt_fetch |
| 40 | + URL ${PAHO_MQTT_URL}/v${PAHO_MQTT_VERSION}.zip |
| 41 | + SOURCE_DIR ${PAHO_MQTT_DIR} BINARY_DIR |
| 42 | + ${CMAKE_BINARY_DIR}/apps/netutils/paho_mqtt/paho_mqtt |
| 43 | + DOWNLOAD_NO_PROGRESS true |
| 44 | + TIMEOUT 30) |
| 45 | + |
| 46 | + FetchContent_GetProperties(paho_mqtt_fetch) |
| 47 | + |
| 48 | + if(NOT paho_mqtt_fetch_POPULATED) |
| 49 | + FetchContent_Populate(paho_mqtt_fetch) |
| 50 | + |
| 51 | + # GitHub ZIP files extract with a versioned top-level directory |
| 52 | + # Move contents from paho.mqtt.c-<version> to paho_mqtt if needed |
| 53 | + file(GLOB extracted_dirs "${PAHO_MQTT_DIR}/paho.mqtt.c-*") |
| 54 | + if(extracted_dirs) |
| 55 | + list(GET extracted_dirs 0 extracted_dir) |
| 56 | + # Move all contents from the versioned directory to paho_mqtt |
| 57 | + file(GLOB extracted_contents "${extracted_dir}/*") |
| 58 | + foreach(item ${extracted_contents}) |
| 59 | + get_filename_component(item_name ${item} NAME) |
| 60 | + execute_process( |
| 61 | + COMMAND ${CMAKE_COMMAND} -E rename ${item} ${PAHO_MQTT_DIR}/${item_name} |
| 62 | + WORKING_DIRECTORY ${CMAKE_CURRENT_LIST_DIR}) |
| 63 | + endforeach() |
| 64 | + # Remove the empty versioned directory |
| 65 | + execute_process( |
| 66 | + COMMAND ${CMAKE_COMMAND} -E remove_directory ${extracted_dir} |
| 67 | + WORKING_DIRECTORY ${CMAKE_CURRENT_LIST_DIR}) |
| 68 | + message("Moved contents from versioned directory to paho_mqtt") |
| 69 | + endif() |
| 70 | + |
| 71 | + # Remove downloaded zip file if exists in current directory |
| 72 | + file(GLOB zip_files "${CMAKE_CURRENT_LIST_DIR}/v${PAHO_MQTT_VERSION}.zip") |
| 73 | + if(zip_files) |
| 74 | + file(REMOVE ${zip_files}) |
| 75 | + message("Removed downloaded zip file") |
| 76 | + endif() |
| 77 | + endif() |
| 78 | + |
| 79 | + # Apply paho_mqtt_01 patch if exists |
| 80 | + if(EXISTS ${CMAKE_CURRENT_LIST_DIR}/paho_mqtt_01.patch) |
| 81 | + execute_process( |
| 82 | + COMMAND sh -c "patch -p1 --forward --ignore-whitespace < ${CMAKE_CURRENT_LIST_DIR}/paho_mqtt_01.patch || true" |
| 83 | + WORKING_DIRECTORY ${PAHO_MQTT_DIR}) |
| 84 | + message("paho_mqtt_01 patching done") |
| 85 | + endif() |
| 86 | + |
| 87 | + message("paho_mqtt download done") |
| 88 | + endif() |
| 89 | + |
| 90 | + configure_file(paho_mqtt/src/VersionInfo.h.in |
| 91 | + ${CMAKE_CURRENT_BINARY_DIR}/VersionInfo.h @ONLY) |
| 92 | + |
| 93 | + set(MQTT5_INCDIR ${CMAKE_CURRENT_LIST_DIR}/paho_mqtt/src) |
| 94 | + list(APPEND MQTT5_INCDIR ${CMAKE_CURRENT_BINARY_DIR}) |
| 95 | + |
| 96 | + file(GLOB CSRCS paho_mqtt/src/*.c) |
| 97 | + |
| 98 | + if(CONFIG_UTILS_MQTT5) |
| 99 | + list(APPEND CSRCS |
| 100 | + ${CMAKE_CURRENT_LIST_DIR}/paho_mqtt/src/samples/pubsub_opts.c) |
| 101 | + endif() |
| 102 | + |
| 103 | + if(CONFIG_OPENSSL_MBEDTLS_WRAPPER) |
| 104 | + list(REMOVE_ITEM CSRCS ${CMAKE_CURRENT_LIST_DIR}/paho_mqtt/src/SHA1.c) |
| 105 | + endif() |
| 106 | + |
| 107 | + list(REMOVE_ITEM CSRCS ${CMAKE_CURRENT_LIST_DIR}/paho_mqtt/src/MQTTClient.c |
| 108 | + ${CMAKE_CURRENT_LIST_DIR}/paho_mqtt/src/MQTTVersion.c) |
| 109 | + |
| 110 | + nuttx_add_library(mqtt5) |
| 111 | + |
| 112 | + target_sources(mqtt5 PRIVATE ${CSRCS}) |
| 113 | + |
| 114 | + target_include_directories(mqtt5 PRIVATE ${MQTT5_INCDIR}) |
| 115 | + |
| 116 | + target_compile_options(mqtt5 PRIVATE ${MQTT5_FLAGS}) |
| 117 | + |
| 118 | + if(CONFIG_UTILS_MQTT5) |
| 119 | + |
| 120 | + set(MQTT_PUB_FLAGS |
| 121 | + -DmessageArrived=mqtt_pub_messageArrived |
| 122 | + -DonDisconnect=mqtt_pub_onDisconnect |
| 123 | + -DonConnectFailure5=mqtt_pub_onConnectFailure5 |
| 124 | + -DonConnectFailure=mqtt_pub_onConnectFailure |
| 125 | + -DonConnect5=mqtt_pub_onConnect5 |
| 126 | + -DonConnect=mqtt_pub_onConnect |
| 127 | + -Dmysleep=mqtt_pub_mysleep |
| 128 | + -DtoStop=mqtt_pub_toStop |
| 129 | + -Dopts=mqtt_pub_opts |
| 130 | + -Dmyconnect=mqtt_pub_myconnect |
| 131 | + -Dcfinish=mqtt_pub_cfinish |
| 132 | + -Dtrace_callback=mqtt_pub_trace_callback) |
| 133 | + |
| 134 | + nuttx_add_application( |
| 135 | + NAME |
| 136 | + mqtt_pub |
| 137 | + SRCS |
| 138 | + paho_mqtt/src/samples/paho_c_pub.c |
| 139 | + DEPENDS |
| 140 | + mqtt5 |
| 141 | + INCLUDE_DIRECTORIES |
| 142 | + ${MQTT5_INCDIR} |
| 143 | + COMPILE_FLAGS |
| 144 | + ${MQTT_PUB_FLAGS} |
| 145 | + STACKSIZE |
| 146 | + ${CONFIG_UTILS_MQTT5_STACKSIZE} |
| 147 | + PRIORITY |
| 148 | + ${CONFIG_UTILS_MQTT5_PRIORITY}) |
| 149 | + |
| 150 | + nuttx_add_application( |
| 151 | + NAME |
| 152 | + mqtt_sub |
| 153 | + SRCS |
| 154 | + paho_mqtt/src/samples/paho_c_sub.c |
| 155 | + DEPENDS |
| 156 | + mqtt5 |
| 157 | + INCLUDE_DIRECTORIES |
| 158 | + ${MQTT5_INCDIR} |
| 159 | + STACKSIZE |
| 160 | + ${CONFIG_UTILS_MQTT5_STACKSIZE} |
| 161 | + PRIORITY |
| 162 | + ${CONFIG_UTILS_MQTT5_PRIORITY}) |
| 163 | + endif() |
| 164 | +endif() |
0 commit comments