forked from wolfSSL/wolfMQTT
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CMakeLists.txt
284 lines (239 loc) · 8.64 KB
/
CMakeLists.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
# CMakeList.txt
#
# Copyright (C) 2006-2024 wolfSSL Inc.
#
# This file is part of wolfSSL. (formerly known as CyaSSL)
#
# Usage:
# $ mkdir build
# $ cd build
# $ cmake ..
# $ cmake --build .
#
# To build with debugging use:
# $ cmake .. -DCMAKE_BUILD_TYPE=Debug
#
# See "CMake" in README.md for more.
####################################################
# Project
####################################################
cmake_minimum_required(VERSION 3.16)
project(wolfMQTT VERSION 1.19.1 LANGUAGES C)
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
include(${CMAKE_CURRENT_SOURCE_DIR}/cmake/functions.cmake)
set(MQTT_SOURCES
src/mqtt_client.c
src/mqtt_packet.c
src/mqtt_socket.c
src/mqtt_sn_client.c
src/mqtt_sn_packet.c
)
# default to build shared library
option(BUILD_SHARED_LIBS "Build shared libraries (.dll/.so) instead of static ones (.lib/.a)" ON)
add_library(wolfmqtt ${MQTT_SOURCES})
if (WITH_WOLFSSL)
target_link_libraries(wolfmqtt PUBLIC wolfssl)
target_include_directories(wolfmqtt PUBLIC ${WITH_WOLFSSL}/include)
target_link_directories(wolfmqtt PUBLIC ${WITH_WOLFSSL}/lib)
list(APPEND WOLFMQTT_DEFINITIONS "-DENABLE_MQTT_TLS")
elseif (WITH_WOLFSSL_TREE)
set(WOLFSSL_EXAMPLES "no" CACHE STRING "")
set(WOLFSSL_CRYPT_TESTS "no" CACHE STRING "")
add_subdirectory(${WITH_WOLFSSL_TREE} wolfssl)
target_link_libraries(wolfmqtt PUBLIC wolfssl)
list(APPEND WOLFMQTT_DEFINITIONS "-DENABLE_MQTT_TLS")
else()
find_package(PkgConfig)
pkg_check_modules(WOLFSSL wolfssl)
if (WOLFSSL_FOUND)
list(APPEND WOLFMQTT_DEFINITIONS "-DENABLE_MQTT_TLS")
target_link_libraries(wolfmqtt PUBLIC ${WOLFSSL_LIBRARIES})
target_include_directories(wolfmqtt PUBLIC ${WOLFSSL_INCLUDE_DIRS})
target_link_directories(wolfmqtt PUBLIC ${WOLFSSL_LIBRARY_DIRS})
target_compile_options(wolfmqtt PUBLIC ${WOLFSSL_CFLAGS_OTHER})
else()
# For support with vcpkg
find_package(wolfssl CONFIG REQUIRED)
if (wolfssl_FOUND)
list(APPEND WOLFMQTT_DEFINITIONS "-DENABLE_MQTT_TLS")
target_link_libraries(wolfmqtt PUBLIC wolfssl::wolfssl)
endif()
endif()
endif()
set(WOLFMQTT_EXAMPLES "yes" CACHE BOOL
"Build examples")
target_compile_definitions(wolfmqtt PRIVATE
"BUILDING_WOLFMQTT"
)
# Tell mqtt_types.h we are using a ./configure like output / options.h
target_compile_definitions(wolfmqtt PRIVATE
"BUILDING_CMAKE"
)
add_option(WOLFMQTT_TLS
"Enable TLS support with wolfSSL"
"yes" "yes;no")
if (WOLFMQTT_TLS)
list(APPEND WOLFMQTT_DEFINITIONS "-DENABLE_MQTT_TLS")
endif()
add_option("WOLFMQTT_SN"
"Enable MQTT-SN support"
"no" "yes;no")
if (WOLFMQTT_SN)
list(APPEND WOLFMQTT_DEFINITIONS "-DWOLFMQTT_SN")
endif()
add_option("WOLFMQTT_NONBLOCK"
"Enable non-blocking support"
"no" "yes;no")
if (WOLFMQTT_NONBLOCK)
list(APPEND WOLFMQTT_DEFINITIONS "-DWOLFMQTT_NONBLOCK")
endif()
add_option(WOLFMQTT_TIMEOUT
"Enable timeout support"
"yes" "yes;no")
if (NOT WOLFMQTT_TIMEOUT)
list(APPEND WOLFMQTT_DEFINITIONS "-DWOLFMQTT_NO_TIMEOUT")
endif()
add_option(WOLFMQTT_ERRORSTRINGS
"Enable error strings"
"yes" "yes;no")
if (NOT WOLFMQTT_ERRORSTRINGS)
list(APPEND WOLFMQTT_DEFINITIONS "-DWOLFMQTT_NO_ERROR_STRINGS")
endif()
add_option(WOLFMQTT_STDINCAP
"Enable examples STDIN capture"
"yes" "yes;no")
if (NOT WOLFMQTT_STDINCAP)
list(APPEND WOLFMQTT_DEFINITIONS "-DWOLFMQTT_NO_STDIN_CAP")
endif()
add_option("WOLFMQTT_V5"
"Enable MQTT v5.0 support"
"no" "yes;no")
if (WOLFMQTT_V5)
list(APPEND WOLFMQTT_DEFINITIONS "-DWOLFMQTT_V5")
endif()
add_option(WOLFMQTT_DISCB
"Enable disconnect callback"
"yes" "yes;no")
if (WOLFMQTT_DISCB)
list(APPEND WOLFMQTT_DEFINITIONS "-DWOLFMQTT_DISCONNECT_CB")
endif()
add_option(WOLFMQTT_MT
"Enable multiple thread support"
"no" "yes;no")
if (WOLFMQTT_MT)
list(APPEND WOLFMQTT_DEFINITIONS "-DWOLFMQTT_MULTITHREAD")
find_package(Threads REQUIRED)
endif()
add_option(WOLFMQTT_CURL
"Enable curl easy socket backend"
"no" "yes;no")
if (WOLFMQTT_CURL)
list(APPEND WOLFMQTT_DEFINITIONS "-DENABLE_MQTT_CURL")
endif()
# Note: not adding stress option to cmake build as of yet. stress is for
# testing only and requires the scripts/ dir to be useful.
# generate options file
message("Generating user options header...")
if (${CMAKE_DISABLE_SOURCE_CHANGES})
set(WOLFMQTT_BUILD_OUT_OF_TREE_DEFAULT "${CMAKE_DISABLE_SOURCE_CHANGES}")
else()
set(WOLFMQTT_BUILD_OUT_OF_TREE_DEFAULT "no")
endif()
set(WOLFMQTT_BUILD_OUT_OF_TREE "${WOLFMQTT_BUILD_OUT_OF_TREE_DEFAULT}" CACHE STRING
"Don't generate files in the source tree (default: ${WOLFMQTT_BUILD_OUT_OF_TREE_DEFAULT})")
set_property(CACHE WOLFMQTT_BUILD_OUT_OF_TREE
PROPERTY STRINGS "yes;no")
if (${WOLFMQTT_BUILD_OUT_OF_TREE})
set(WOLFMQTT_OUTPUT_BASE ${CMAKE_CURRENT_BINARY_DIR})
else()
set(WOLFMQTT_OUTPUT_BASE ${CMAKE_CURRENT_SOURCE_DIR})
endif()
set(OPTION_FILE "${WOLFMQTT_OUTPUT_BASE}/wolfmqtt/options.h")
file(REMOVE ${OPTION_FILE})
file(APPEND ${OPTION_FILE} "/* wolfmqtt options.h\n")
file(APPEND ${OPTION_FILE} " * generated from cmake configure options\n")
file(APPEND ${OPTION_FILE} " *\n")
file(APPEND ${OPTION_FILE} " * Copyright (C) 2006-2024 wolfSSL Inc.\n")
file(APPEND ${OPTION_FILE} " *\n")
file(APPEND ${OPTION_FILE} " * This file is part of wolfSSL.\n")
file(APPEND ${OPTION_FILE} " *\n")
file(APPEND ${OPTION_FILE} " */\n\n")
file(APPEND ${OPTION_FILE} "#ifndef WOLFMQTT_OPTIONS_H\n")
file(APPEND ${OPTION_FILE} "#define WOLFMQTT_OPTIONS_H\n\n\n")
file(APPEND ${OPTION_FILE} "#ifdef __cplusplus\n")
file(APPEND ${OPTION_FILE} "extern \"C\" {\n")
file(APPEND ${OPTION_FILE} "#endif\n\n")
add_to_options_file("${WOLFMQTT_DEFINITIONS}" "${OPTION_FILE}")
# CMAKE_C_FLAGS is just a string of space-separated flags to pass to the C
# compiler. We need to replace those spaces with semicolons in order to treat it
# as a CMake list.
string(REPLACE " " ";" CMAKE_C_FLAGS_LIST "${CMAKE_C_FLAGS}")
add_to_options_file("${CMAKE_C_FLAGS_LIST}" "${OPTION_FILE}")
file(APPEND ${OPTION_FILE} "\n#ifdef __cplusplus\n")
file(APPEND ${OPTION_FILE} "}\n")
file(APPEND ${OPTION_FILE} "#endif\n\n\n")
file(APPEND ${OPTION_FILE} "#endif /* WOLFMQTT_OPTIONS_H */\n\n")
if(WIN32)
target_compile_definitions(wolfmqtt PRIVATE
"_WINDLL"
)
endif(WIN32)
target_include_directories(wolfmqtt
PUBLIC
$<INSTALL_INTERFACE:include>
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}>
$<BUILD_INTERFACE:${CMAKE_CURRENT_BINARY_DIR}>
)
if (WOLFMQTT_EXAMPLES)
add_library(mqtt_test_lib STATIC
examples/mqttexample.c
examples/mqttnet.c
)
if (WOLFMQTT_MT)
target_link_libraries(mqtt_test_lib wolfmqtt pthread)
else()
target_link_libraries(mqtt_test_lib wolfmqtt)
endif()
function(add_mqtt_example name src)
add_executable(${name}
examples/${src}
)
target_link_libraries(${name} wolfmqtt mqtt_test_lib)
endfunction()
add_mqtt_example(mqttclient mqttclient/mqttclient.c)
add_mqtt_example(mqttsimple mqttsimple/mqttsimple.c)
add_mqtt_example(nbclient nbclient/nbclient.c)
#add_mqtt_example(mqttuart mqttuart.c)
add_mqtt_example(multithread multithread/multithread.c)
add_mqtt_example(sn-client sn-client/sn-client.c)
add_mqtt_example(sn-client_qos-1 sn-client/sn-client_qos-1.c)
add_mqtt_example(sn-multithread sn-client/sn-multithread.c)
add_mqtt_example(awsiot aws/awsiot.c)
add_mqtt_example(wiot wiot/wiot.c)
add_mqtt_example(azureiothub azure/azureiothub.c)
add_mqtt_example(fwpush firmware/fwpush.c)
add_mqtt_example(fwclient firmware/fwclient.c)
add_mqtt_example(mqtt-pub pub-sub/mqtt-pub.c)
add_mqtt_example(mqtt-sub pub-sub/mqtt-sub.c)
endif()
####################################################
# Installation
####################################################
include(GNUInstallDirs)
install(TARGETS wolfmqtt
EXPORT wolfmqtt-targets
LIBRARY DESTINATION lib
ARCHIVE DESTINATION lib
RUNTIME DESTINATION bin
)
# Install the export set
install(EXPORT wolfmqtt-targets
DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/wolfmqtt
FILE wolfmqtt-config.cmake)
# Install the headers
install(DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/wolfmqtt/
DESTINATION include/wolfmqtt
FILES_MATCHING PATTERN "*.h")
install(DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/wolfmqtt/
DESTINATION include/wolfmqtt
FILES_MATCHING PATTERN "*.h")