1# 2# libwebsockets - small server side websockets and web server implementation 3# 4# Copyright (C) 2010 - 2020 Andy Green <andy@warmcat.com> 5# 6# Permission is hereby granted, free of charge, to any person obtaining a copy 7# of this software and associated documentation files (the "Software"), to 8# deal in the Software without restriction, including without limitation the 9# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or 10# sell copies of the Software, and to permit persons to whom the Software is 11# furnished to do so, subject to the following conditions: 12# 13# The above copyright notice and this permission notice shall be included in 14# all copies or substantial portions of the Software. 15# 16# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 21# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 22# IN THE SOFTWARE. 23# 24 25include_directories(.) 26 27macro(add_subdir_include_dirs arg1) 28 add_subdirectory(${arg1}) 29 list(APPEND LWS_LIB_BUILD_INC_PATHS ${_CMAKE_INC_LIST}) 30endmacro() 31 32set(LWS_LIB_INCLUDES "") 33 34# 35# Plat specific build items 36# 37 38if (LWS_PLAT_FREERTOS) 39 add_subdir_include_dirs(plat/freertos) 40 if (ESP_PLATFORM) 41 list(APPEND LWS_ESP_IDF_DIRS 42 $ENV{IDF_PATH}/components/freertos/include 43 $ENV{IDF_PATH}/components/esp_hw_support/include/soc/ 44 $ENV{IDF_PATH}/components/esp_common/include 45 $ENV{IDF_PATH}/components/esp_timer/include 46 $ENV{IDF_PATH}/components/soc/include 47 $ENV{IDF_PATH}/components/soc/src/esp32/include 48 $ENV{IDF_PATH}/components/lwip/port/esp32/include 49 $ENV{IDF_PATH}/components/lwip/lwip/src/include 50 $ENV{IDF_PATH}/components/lwip/port/esp32/include 51 ${CMAKE_BINARY_DIR}/config 52 $ENV{IDF_PATH}/components/esp_rom/include 53 $ENV{IDF_PATH}/components/esp_system/include 54 $ENV{IDF_PATH}/components/lwip/include/apps/sntp 55 $ENV{IDF_PATH}/components/soc/soc/esp32/include 56 $ENV{IDF_PATH}/components/heap/include 57 $ENV{IDF_PATH}/components/mbedtls/mbedtls/include 58 $ENV{IDF_PATH}/components/mbedtls/port/include 59 $ENV{IDF_PATH}/components/esp_wifi/include 60 $ENV{IDF_PATH}/components/esp_event/include 61 $ENV{IDF_PATH}/components/esp_netif/include 62 $ENV{IDF_PATH}/components/esp_eth/include 63 $ENV{IDF_PATH}/components/driver/include 64 $ENV{IDF_PATH}/components/soc/soc/include 65 $ENV{IDF_PATH}/components/tcpip_adapter/include 66 $ENV{IDF_PATH}/components/lwip/include/apps 67 $ENV{IDF_PATH}/components/nvs_flash/include 68 $ENV{IDF_PATH}/components/esp32/include 69 $ENV{IDF_PATH}/components/spi_flash/include 70 $ENV{IDF_PATH}/components/mdns/include 71 $ENV{IDF_PATH}/components/lwip/lwip/src/include/lwip 72 $ENV{IDF_PATH}/components/lwip/lwip/src/include 73 $ENV{IDF_PATH}/components/lwip/lwip/src/include/lwip 74 $ENV{IDF_PATH}/components/newlib/platform_include ) 75 76 include_directories(${LWS_ESP_IDF_DIRS}) 77 78 list(APPEND CMAKE_REQUIRED_INCLUDES ${LWS_ESP_IDF_DIRS}) 79 endif() 80 81 82else() 83 if (LWS_PLAT_OPTEE) 84 add_subdir_include_dirs(plat/optee) 85 else() 86 if (WIN32) 87 add_subdir_include_dirs(plat/windows) 88 else() 89 add_subdir_include_dirs(plat/unix) 90 endif() 91 endif() 92endif() 93 94if (LIB_LIST) 95 set(CMAKE_REQUIRED_LIBRARIES ${LIB_LIST} ${CMAKE_REQUIRED_LIBRARIES}) 96endif() 97 98if (LWS_WITH_ZLIB) 99 if (LWS_WITH_BUNDLED_ZLIB) 100 if (WIN32) 101 # it's trying to delete internal zlib entry 102 LIST(REMOVE_AT CMAKE_REQUIRED_LIBRARIES 0 ) 103 endif() 104 endif() 105endif() 106 107 108# ideally we want to use pipe2() 109 110CHECK_C_SOURCE_COMPILES(" 111 #ifndef _GNU_SOURCE 112 #define _GNU_SOURCE 113 #endif 114 #include <unistd.h> 115 int main(void) { 116 int fd[2]; 117 return pipe2(fd, 0); 118 }" LWS_HAVE_PIPE2) 119 120# tcp keepalive needs this on linux to work practically... but it only exists 121# after kernel 2.6.37 122 123CHECK_C_SOURCE_COMPILES("#include <netinet/tcp.h>\nint main(void) { return TCP_USER_TIMEOUT; }\n" LWS_HAVE_TCP_USER_TIMEOUT) 124set(LWS_PUBLIC_INCLUDES "") 125if (LWS_WITH_TLS) 126 add_subdir_include_dirs(tls) 127endif() 128 129# Generate the lws_config.h that includes all the private compilation settings. 130configure_file( 131 "${PROJECT_SOURCE_DIR}/cmake/lws_config_private.h.in" 132 "${PROJECT_BINARY_DIR}/lws_config_private.h") 133 134add_subdir_include_dirs(core) 135add_subdir_include_dirs(misc) 136add_subdir_include_dirs(system) 137 138if (LWS_WITH_DRIVERS) 139 add_subdir_include_dirs(drivers) 140endif() 141 142if (LWS_WITH_NETWORK) 143 add_subdir_include_dirs(core-net) 144 if (LWS_WITH_ABSTRACT) 145 add_subdir_include_dirs(abstract) 146 endif() 147 add_subdir_include_dirs(roles) 148endif() 149 150if (LWS_WITH_JOSE) 151 add_subdir_include_dirs(jose) 152endif() 153if (LWS_WITH_COSE) 154 add_subdir_include_dirs(cose) 155endif() 156 157 158if (LWS_WITH_SECURE_STREAMS) 159 add_subdir_include_dirs(secure-streams) 160endif() 161 162add_subdir_include_dirs(event-libs) 163 164if (LWS_WITH_STATIC) 165 if (LWS_STATIC_PIC) 166 set(CMAKE_POSITION_INDEPENDENT_CODE ON) 167 endif() 168 169 add_library(websockets STATIC ${SOURCES})# ${HDR_PUBLIC}) 170 set_target_properties(websockets PROPERTIES LINKER_LANGUAGE C) 171 list(APPEND LWS_LIBRARIES websockets) 172 target_include_directories(websockets INTERFACE 173 $<INSTALL_INTERFACE:${LWS_INSTALL_INCLUDE_DIR}> 174 $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/../include> 175 $<BUILD_INTERFACE:${CMAKE_CURRENT_BINARY_DIR}/../include> 176 ) 177 target_include_directories(websockets PRIVATE ${LWS_LIB_BUILD_INC_PATHS}) 178 target_compile_definitions(websockets PRIVATE LWS_BUILDING_STATIC) 179 target_include_directories(websockets PUBLIC ${LWS_PUBLIC_INCLUDES}) 180 set(LWS_PUBLIC_INCLUDES ${LWS_PUBLIC_INCLUDES} PARENT_SCOPE) 181 182 if (WIN32) 183 # Windows uses the same .lib ending for static libraries and shared 184 # library linker files, so rename the static library. 185 set_target_properties(websockets 186 PROPERTIES 187 OUTPUT_NAME websockets_static) 188 endif() 189 190endif() 191 192if (LWS_WITH_SHARED) 193 if (NOT RESOURCES) 194 set(RESOURCES "") 195 endif() 196 197 add_library(websockets_shared SHARED ${SOURCES} ${RESOURCES})# ${HDR_PUBLIC}) 198 set_target_properties(websockets_shared PROPERTIES LINKER_LANGUAGE C) 199 list(APPEND LWS_LIBRARIES websockets_shared) 200 target_include_directories(websockets_shared INTERFACE 201 $<INSTALL_INTERFACE:${LWS_INSTALL_INCLUDE_DIR}> 202 $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/../include> 203 $<BUILD_INTERFACE:${CMAKE_CURRENT_BINARY_DIR}/../include> 204 ) 205 target_include_directories(websockets_shared PRIVATE ${LWS_LIB_BUILD_INC_PATHS}) 206 target_compile_definitions(websockets_shared PRIVATE LWS_BUILDING_SHARED) 207 target_include_directories(websockets_shared PUBLIC ${LWS_PUBLIC_INCLUDES}) 208 set(LWS_PUBLIC_INCLUDES ${LWS_PUBLIC_INCLUDES} PARENT_SCOPE) 209 210 # We want the shared lib to be named "libwebsockets" 211 # not "libwebsocket_shared". 212 set_target_properties(websockets_shared 213 PROPERTIES 214 OUTPUT_NAME websockets) 215 216 if (WIN32) 217 # Compile as DLL (export function declarations) 218 set_property( 219 TARGET websockets_shared 220 PROPERTY COMPILE_DEFINITIONS 221 LWS_DLL 222 LWS_INTERNAL) 223 endif() 224 225 if (APPLE) 226 set_property(TARGET websockets_shared PROPERTY MACOSX_RPATH YES) 227 endif() 228 229 if (UNIX AND LWS_WITH_PLUGINS_API) 230 set (CMAKE_POSITION_INDEPENDENT_CODE ON) 231 if (NOT((${CMAKE_SYSTEM_NAME} MATCHES "FreeBSD") OR 232 (${CMAKE_SYSTEM_NAME} MATCHES "OpenBSD") OR 233 (${CMAKE_SYSTEM_NAME} MATCHES "QNX"))) 234 if (LWS_WITH_SHARED) 235 target_link_libraries(websockets_shared dl) 236 endif() 237 endif() 238 endif() 239 240endif() 241 242# 243# expose the library private include dirs to plugins, test apps etc that are 244# part of the lib build but different targets 245# 246 247if (LWS_WITH_SHARED) 248 get_target_property(LWS_LIB_INCLUDES websockets_shared INCLUDE_DIRECTORIES) 249else() 250 get_target_property(LWS_LIB_INCLUDES websockets INCLUDE_DIRECTORIES) 251endif() 252 253 254# Set the so version of the lib. 255# Equivalent to LDFLAGS=-version-info x:x:x 256 257if (CMAKE_COMPILER_IS_GNUCC OR CMAKE_COMPILER_IS_GNUCXX OR COMPILER_IS_CLANG) 258 foreach(lib ${LWS_LIBRARIES}) 259 set_target_properties(${lib} 260 PROPERTIES 261 SOVERSION ${SOVERSION}) 262 endforeach() 263endif() 264 265 266# Setup the linking for all libs. 267foreach (lib ${LWS_LIBRARIES}) 268 target_link_libraries(${lib} ${LIB_LIST}) 269endforeach() 270 271# 272# These will be available to parent projects including libwebsockets 273# using add_subdirectory() 274# 275set(LIBWEBSOCKETS_LIBRARIES ${LWS_LIBRARIES} CACHE STRING "Libwebsocket libraries") 276if (LWS_WITH_STATIC) 277 set(LIBWEBSOCKETS_LIBRARIES_STATIC websockets CACHE STRING "Libwebsocket static library") 278endif() 279if (LWS_WITH_SHARED) 280 set(LIBWEBSOCKETS_LIBRARIES_SHARED websockets_shared CACHE STRING "Libwebsocket shared library") 281endif() 282 283# Install libs and headers. 284install(TARGETS ${LWS_LIBRARIES} 285 EXPORT LibwebsocketsTargets 286 LIBRARY DESTINATION "${LWS_INSTALL_LIB_DIR}${LIB_SUFFIX}" COMPONENT core 287 ARCHIVE DESTINATION "${LWS_INSTALL_LIB_DIR}${LIB_SUFFIX}" COMPONENT core 288 RUNTIME DESTINATION "${LWS_INSTALL_BIN_DIR}" COMPONENT core # Windows DLLs 289 PUBLIC_HEADER DESTINATION "${LWS_INSTALL_INCLUDE_DIR}" COMPONENT dev) 290 291 #set(CPACK_COMPONENT_LIBRARIES_DISPLAY_NAME "Libraries" PARENT_SCOPE) 292set(CPACK_COMPONENT_DEV_DISPLAY_NAME "Development files" PARENT_SCOPE) 293 294 295if (UNIX OR MINGW) 296 297# figure out pkfcfg required libs here 298 299set(lws_requires "") 300if (LWS_HAVE_LIBCAP) 301 if (NOT lws_requires STREQUAL "") 302 set(lws_requires "${lws_requires},libcap") 303 else() 304 set(lws_requires "libcap") 305 endif() 306endif() 307 308 309# Generate and install pkgconfig. 310# (This is not indented, because the tabs will be part of the output) 311file(WRITE "${PROJECT_BINARY_DIR}/libwebsockets.pc" 312"prefix=\"${CMAKE_INSTALL_PREFIX}\" 313exec_prefix=\${prefix} 314libdir=\${exec_prefix}/lib${LIB_SUFFIX} 315includedir=\${prefix}/include 316 317Name: libwebsockets 318Description: Websockets server and client library 319Version: ${CPACK_PACKAGE_VERSION_MAJOR}.${CPACK_PACKAGE_VERSION_MINOR}.${CPACK_PACKAGE_VERSION_PATCH} 320 321Libs: -L\${libdir} -lwebsockets 322Cflags: -I\${includedir} 323" 324) 325if (NOT ${lws_requires} STREQUAL "") 326 file(APPEND "${PROJECT_BINARY_DIR}/libwebsockets.pc" "Requires: ${lws_requires}") 327endif() 328 329 330 install(FILES "${PROJECT_BINARY_DIR}/libwebsockets.pc" 331 DESTINATION lib${LIB_SUFFIX}/pkgconfig) 332 333file(WRITE "${PROJECT_BINARY_DIR}/libwebsockets_static.pc" 334"prefix=\"${CMAKE_INSTALL_PREFIX}\" 335exec_prefix=\${prefix} 336libdir=\${exec_prefix}/lib${LIB_SUFFIX} 337includedir=\${prefix}/include 338 339Name: libwebsockets_static 340Description: Websockets server and client static library 341Version: ${CPACK_PACKAGE_VERSION_MAJOR}.${CPACK_PACKAGE_VERSION_MINOR}.${CPACK_PACKAGE_VERSION_PATCH} 342 343Libs: -L\${libdir} -l:libwebsockets${CMAKE_STATIC_LIBRARY_SUFFIX} 344Libs.private: 345Cflags: -I\${includedir} 346" 347) 348 349if (NOT ${lws_requires} STREQUAL "") 350 file(APPEND "${PROJECT_BINARY_DIR}/libwebsockets_static.pc" "Requires: ${lws_requires}") 351endif() 352 353 354 install(FILES "${PROJECT_BINARY_DIR}/libwebsockets_static.pc" 355 DESTINATION lib${LIB_SUFFIX}/pkgconfig) 356 357endif(UNIX OR MINGW) 358 359 360# Keep explicit parent scope exports at end 361# 362 363export_to_parent_intermediate() 364if (DEFINED LWS_PLAT_UNIX) 365 set(LWS_PLAT_UNIX ${LWS_PLAT_UNIX} PARENT_SCOPE) 366 if (ILLUMOS) 367 add_definitions("-D__illumos__") 368 endif() 369endif() 370set(LWS_HAVE_MBEDTLS_NET_SOCKETS ${LWS_HAVE_MBEDTLS_NET_SOCKETS} PARENT_SCOPE) 371set(LWS_HAVE_MBEDTLS_SSL_NEW_SESSION_TICKET ${LWS_HAVE_MBEDTLS_SSL_NEW_SESSION_TICKET} PARENT_SCOPE) 372set(LWS_HAVE_mbedtls_ssl_conf_alpn_protocols ${LWS_HAVE_mbedtls_ssl_conf_alpn_protocols} PARENT_SCOPE) 373set(TEST_SERVER_SSL_KEY "${TEST_SERVER_SSL_KEY}" PARENT_SCOPE) 374set(TEST_SERVER_SSL_CERT "${TEST_SERVER_SSL_CERT}" PARENT_SCOPE) 375set(TEST_SERVER_DATA ${TEST_SERVER_DATA} PARENT_SCOPE) 376set(LWS_HAVE_PIPE2 ${LWS_HAVE_PIPE2} PARENT_SCOPE) 377set(LWS_LIBRARIES ${LWS_LIBRARIES} PARENT_SCOPE) 378if (DEFINED WIN32_HELPERS_PATH) 379 set(WIN32_HELPERS_PATH ${WIN32_HELPERS_PATH} PARENT_SCOPE) 380endif() 381if (DEFINED HDR_PRIVATE) 382set(HDR_PRIVATE ${HDR_PRIVATE} PARENT_SCOPE) 383endif() 384if (DEFINED ZLIB_FOUND) 385 set(ZLIB_FOUND ${ZLIB_FOUND} PARENT_SCOPE) 386endif() 387if (DEFINED LIB_LIST_AT_END) 388set(LIB_LIST_AT_END ${LIB_LIST_AT_END} PARENT_SCOPE) 389endif() 390set(USE_WOLFSSL ${USE_WOLFSSL} PARENT_SCOPE) 391set(LWS_DEPS_LIB_PATHS ${LWS_DEPS_LIB_PATHS} PARENT_SCOPE) 392 393