Imported Upstream version 4.3.2
[platform/upstream/libwebsockets.git] / CMakeLists.txt
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
25 cmake_minimum_required(VERSION 2.8.12)
26 include(CheckFunctionExists)
27 include(CheckSymbolExists)
28 include(CheckIncludeFile)
29 include(CheckIncludeFiles)
30 include(CheckLibraryExists)
31 include(CheckTypeSize)
32 include(CheckCSourceCompiles)
33
34 if (POLICY CMP0048)
35         cmake_policy(SET CMP0048 NEW)
36 endif()
37
38 #if (POLICY CMP0024)
39 #       cmake_policy(SET CMP0024 NEW)
40 #endif()
41
42 if (POLICY CMP0075)
43         cmake_policy(SET CMP0075 NEW)
44 endif()
45
46 # General Advice
47 #
48 # For selecting between DEBUG / RELEASE, use -DCMAKE_BUILD_TYPE=DEBUG or =RELEASE
49 #   debug builds include source level debug info and extra logging
50
51 set(LWS_WITH_BUNDLED_ZLIB_DEFAULT OFF)
52 if(WIN32)
53         set(LWS_WITH_BUNDLED_ZLIB_DEFAULT ON)
54 endif()
55
56 set(LWS_ROLE_RAW 1)
57 set(LWS_WITH_POLL 1)
58
59 if (ESP_PLATFORM)
60         set(LWS_ESP_PLATFORM 1)
61         #set(CMAKE_TOOLCHAIN_FILE contrib/cross-esp32.cmake)
62         set(LWIP_PROVIDE_ERRNO 1)
63 endif()
64
65 # it's at this point any toolchain file is brought in
66 project(libwebsockets C)
67 if (LWS_WITH_SECURE_STREAMS_CPP)
68         enable_language(CXX)
69 endif()
70 include(CTest)
71
72 if (ESP_PLATFORM)
73        include_directories(
74                 $ENV{IDF_PATH}/components/esp_hw_support/include/soc/
75                 $ENV{IDF_PATH}/components/freertos/include/
76                 $ENV{IDF_PATH}/components/freertos/esp_additions/include/
77                 $ENV{IDF_PATH}/components/freertos/esp_additions/include/freertos/
78                 $ENV{IDF_PATH}/components/freertos/FreeRTOS-Kernel/include/
79                 $ENV{IDF_PATH}/components/freertos/FreeRTOS-Kernel/portable/linux/include/
80                 $ENV{IDF_PATH}/components/xtensa/${CONFIG_IDF_TARGET}/include/
81                 $ENV{IDF_PATH}/components/freertos/include/esp_additions
82                 $ENV{IDF_PATH}/components/hal/include
83                 $ENV{IDF_PATH}/components/soc/${CONFIG_IDF_TARGET}/include/
84                 $ENV{IDF_PATH}/components/soc/include/
85                 $ENV{IDF_PATH}/components/esp_hw_support/include
86                 $ENV{IDF_PATH}/components/hal/${CONFIG_IDF_TARGET}/include/
87         )
88
89         if (CONFIG_IDF_TARGET_ARCH_RISCV)
90                 include_directories(
91                        $ENV{IDF_PATH}/components/freertos/port/riscv/include
92                        $ENV{IDF_PATH}/components/riscv/include)
93         else()
94                 include_directories(
95                        $ENV{IDF_PATH}/components/freertos/port/xtensa/include
96                        $ENV{IDF_PATH}/components/xtensa/include)
97         endif()
98
99 endif()
100
101
102 #
103 # Select features recommended for PC distro packaging
104 #
105 option(LWS_WITH_DISTRO_RECOMMENDED "Enable features recommended for distro packaging" OFF)
106 option(LWS_FOR_GITOHASHI "Enable features recommended for use with gitohashi" OFF)
107
108 #
109 # Compiler features
110 #
111 option(DISABLE_WERROR "Avoid treating compiler warnings as fatal errors" OFF)
112
113 #
114 # Major individual features
115 #
116 option(LWS_WITH_NETWORK "Compile with network-related code" ON)
117 option(LWS_ROLE_H1 "Compile with support for http/1 (needed for ws)" ON)
118 option(LWS_ROLE_WS "Compile with support for websockets" ON)
119 option(LWS_ROLE_MQTT "Build with support for MQTT client" OFF)
120 option(LWS_ROLE_DBUS "Compile with support for DBUS" OFF)
121 option(LWS_ROLE_RAW_PROXY "Raw packet proxy" OFF)
122 option(LWS_ROLE_RAW_FILE "Compile with support for raw files" ON)
123 option(LWS_WITH_HTTP2 "Compile with server support for HTTP/2" ON)
124 option(LWS_WITH_LWSWS "Libwebsockets Webserver" OFF)
125 option(LWS_WITH_CGI "Include CGI (spawn process with network-connected stdin/out/err) APIs" OFF)
126 option(LWS_IPV6 "Compile with support for ipv6" OFF)
127 option(LWS_UNIX_SOCK "Compile with support for UNIX domain socket if OS supports it" ON)
128 option(LWS_WITH_PLUGINS "Support plugins for protocols and extensions (implies LWS_WITH_PLUGINS_API)" OFF)
129 option(LWS_WITH_PLUGINS_BUILTIN "Build the plugin protocols directly into lws library" OFF)
130 option(LWS_WITH_HTTP_PROXY "Support for active HTTP proxying" OFF)
131 option(LWS_WITH_ZIP_FOPS "Support serving pre-zipped files" OFF)
132 option(LWS_WITH_SOCKS5 "Allow use of SOCKS5 proxy on client connections" OFF)
133 option(LWS_WITH_PEER_LIMITS "Track peers and restrict resources a single peer can allocate" OFF)
134 option(LWS_WITH_ACCESS_LOG "Support generating Apache-compatible access logs" OFF)
135 option(LWS_WITH_RANGES "Support http ranges (RFC7233)" OFF)
136 option(LWS_WITH_THREADPOOL "Managed worker thread pool support (relies on pthreads)" OFF)
137 option(LWS_WITH_HTTP_STREAM_COMPRESSION "Support HTTP stream compression" OFF)
138 option(LWS_WITH_HTTP_BROTLI "Also offer brotli http stream compression (requires LWS_WITH_HTTP_STREAM_COMPRESSION)" OFF)
139 option(LWS_WITH_ACME "Enable support for ACME automatic cert acquisition + maintenance (letsencrypt etc)" OFF)
140 option(LWS_WITH_HUBBUB "Enable libhubbub rewriting support" OFF)
141 option(LWS_WITH_ALSA "Enable alsa audio example" OFF)
142 option(LWS_WITH_GTK "Enable gtk example" OFF)
143 option(LWS_WITH_FTS "Full Text Search support" OFF)
144 option(LWS_WITH_SYS_ASYNC_DNS "Nonblocking internal IPv4 + IPv6 DNS resolver" OFF)
145 option(LWS_WITH_SYS_NTPCLIENT "Build in tiny ntpclient good for tls date validation and run via lws_system" OFF)
146 option(LWS_WITH_SYS_DHCP_CLIENT "Build in tiny DHCP client" OFF)
147 option(LWS_WITH_HTTP_BASIC_AUTH "Support Basic Auth" ON)
148 option(LWS_WITH_HTTP_UNCOMMON_HEADERS "Include less common http header support" ON)
149 option(LWS_WITH_SYS_STATE "lws_system state support" ON)
150 option(LWS_WITH_SYS_SMD "Lws System Message Distribution" ON)
151 option(LWS_WITH_SYS_FAULT_INJECTION "Enable fault injection support" OFF)
152 option(LWS_WITH_SYS_METRICS "Lws Metrics API" OFF)
153
154 #
155 # Secure Streams
156 #
157 option(LWS_WITH_SECURE_STREAMS "Secure Streams protocol-agnostic API" OFF)
158 option(LWS_WITH_SECURE_STREAMS_CPP "Secure Streams C++ classes" OFF)
159 option(LWS_WITH_SECURE_STREAMS_PROXY_API "Secure Streams support to work across processes" OFF)
160 option(LWS_WITH_SECURE_STREAMS_SYS_AUTH_API_AMAZON_COM "Auth support for api.amazon.com" OFF)
161 option(LWS_WITH_SECURE_STREAMS_STATIC_POLICY_ONLY "Secure Streams Policy is hardcoded only" OFF)
162 option(LWS_WITH_SECURE_STREAMS_AUTH_SIGV4 "Secure Streams Auth support for AWS Sigv4" OFF)
163 option(LWS_WITH_SECURE_STREAMS_BUFFER_DUMP "Secure Streams protocol buffer dump" OFF)
164 option(LWS_WITH_SS_DIRECT_PROTOCOL_STR "Secure Streams directly set/get metadata w/o policy" OFF)
165
166 #
167 # CTest options
168 #
169 #
170 # If you build with LWS_WITH_MINIMAL_EXAMPLES, you can use CTest / make test to run
171 # examples that can give a pass/fail response.  By default it runs tests both against
172 # a local server peer and warmcat.com, if your CI wants to do the tests but does not
173 # have internet routing, then you can still run a subset of tests with CTest / make
174 # test that only does local tests by disabling this option.
175 #
176 option(LWS_CTEST_INTERNET_AVAILABLE "CTest will performs tests that need the Internet" ON)
177
178 #
179 # TLS library options... all except mbedTLS are basically OpenSSL variants.
180 #
181 option(LWS_WITH_SSL "Include SSL support (defaults to OpenSSL or similar, mbedTLS if LWS_WITH_MBEDTLS is set)" ON)
182 option(LWS_WITH_MBEDTLS "Use mbedTLS (>=2.0) replacement for OpenSSL. When setting this, you also may need to specify LWS_MBEDTLS_LIBRARIES and LWS_MBEDTLS_INCLUDE_DIRS" OFF)
183 option(LWS_WITH_BORINGSSL "Use BoringSSL replacement for OpenSSL" OFF)
184 option(LWS_WITH_CYASSL "Use CyaSSL replacement for OpenSSL. When setting this, you also need to specify LWS_CYASSL_LIBRARIES and LWS_CYASSL_INCLUDE_DIRS" OFF)
185 option(LWS_WITH_WOLFSSL "Use wolfSSL replacement for OpenSSL. When setting this, you also may need to specify LWS_WOLFSSL_LIBRARIES and LWS_WOLFSSL_INCLUDE_DIRS" OFF)
186 option(LWS_SSL_CLIENT_USE_OS_CA_CERTS "SSL support should make use of the OS-installed CA root certs" ON)
187 option(LWS_TLS_LOG_PLAINTEXT_RX "For debugging log the received plaintext as soon as decrypted" OFF)
188 option(LWS_TLS_LOG_PLAINTEXT_TX "For debugging log the transmitted plaintext just before encryption" OFF)
189 option(LWS_WITH_TLS_SESSIONS "Enable persistent, resumable TLS sessions" ON)
190 option(LWS_WITH_TLS_JIT_TRUST "Enable dynamically computing which trusted TLS CA is needed to be instantiated" OFF)
191
192 #
193 # Event library options (may select multiple, or none for default poll()
194 #
195 option(LWS_WITH_LIBEV "Compile with support for libev" OFF)
196 option(LWS_WITH_LIBUV "Compile with support for libuv" OFF)
197 option(LWS_WITH_LIBEVENT "Compile with support for libevent" OFF)
198 option(LWS_WITH_GLIB "Compile with support for glib event loop" OFF)
199 option(LWS_WITH_SDEVENT "Compile with support for sd-event loop" OFF)
200 option(LWS_WITH_ULOOP "Compile with support for uloop" OFF)
201
202 if (UNIX)
203 # since v4.1, on unix platforms default is build any event libs as runtime plugins
204 option(LWS_WITH_EVLIB_PLUGINS "Compile event lib support into runtime-selected plugins" ON)
205 else()
206 # otherwise default to linking the event lib(s) to libwebsockets.so
207 option(LWS_WITH_EVLIB_PLUGINS "Compile event lib support into runtime-selected plugins" OFF)
208 endif()
209 #
210 # LWS Drivers
211 #
212
213 option(LWS_WITH_DRIVERS "With generic drivers for gpio, i2c, display etc" OFF)
214
215 #
216 # Static / Dynamic build options
217 #
218 option(LWS_WITH_STATIC "Build the static version of the library" ON)
219 option(LWS_WITH_SHARED "Build the shared version of the library" ON)
220 option(LWS_LINK_TESTAPPS_DYNAMIC "Link the test apps to the shared version of the library. Default is to link statically" OFF)
221 option(LWS_STATIC_PIC "Build the static version of the library with position-independent code" OFF)
222 option(LWS_SUPPRESS_DEPRECATED_API_WARNINGS "Turn off complaints about, eg, openssl 3 deprecated api usage" ON)
223
224 #
225 # Specific platforms
226 #
227 option(LWS_WITH_ESP32 "Build for ESP32" OFF)
228 option(LWS_PLAT_OPTEE "Build for OPTEE" OFF)
229 option(LWS_PLAT_FREERTOS "Build for FreeRTOS" OFF)
230 option(LWS_PLAT_ANDROID "Android flavour of unix platform" OFF)
231
232 #
233 # Client / Server / Test Apps build control
234 #
235 option(LWS_WITHOUT_CLIENT "Don't build the client part of the library" OFF)
236 option(LWS_WITHOUT_SERVER "Don't build the server part of the library" OFF)
237 option(LWS_WITHOUT_TESTAPPS "Don't build the libwebsocket-test-apps" OFF)
238 option(LWS_WITHOUT_TEST_SERVER "Don't build the test server" OFF)
239 option(LWS_WITHOUT_TEST_SERVER_EXTPOLL "Don't build the test server version that uses external poll" OFF)
240 option(LWS_WITHOUT_TEST_PING "Don't build the ping test application" OFF)
241 option(LWS_WITHOUT_TEST_CLIENT "Don't build the client test application" OFF)
242 #
243 # Extensions (permessage-deflate)
244 #
245 option(LWS_WITHOUT_EXTENSIONS "Don't compile with extensions" ON)
246 #
247 # Helpers + misc
248 #
249 option(LWS_WITHOUT_BUILTIN_GETIFADDRS "Don't use the BSD getifaddrs implementation from libwebsockets if it is missing (this will result in a compilation error) ... The default is to assume that your libc provides it. On some systems such as uclibc it doesn't exist." OFF)
250 option(LWS_FALLBACK_GETHOSTBYNAME "Also try to do dns resolution using gethostbyname if getaddrinfo fails" OFF)
251 option(LWS_WITHOUT_BUILTIN_SHA1 "Don't build the lws sha-1 (eg, because openssl will provide it" OFF)
252 option(LWS_WITHOUT_DAEMONIZE "Don't build the daemonization api" ON)
253 option(LWS_SSL_SERVER_WITH_ECDH_CERT "Include SSL server use ECDH certificate" OFF)
254 option(LWS_WITH_LEJP "With the Lightweight JSON Parser" ON)
255 option(LWS_WITH_CBOR "With the Lightweight LECP CBOR Parser" OFF)
256 option(LWS_WITH_CBOR_FLOAT "Build floating point types if building CBOR LECP" ON)
257 option(LWS_WITH_SQLITE3 "Require SQLITE3 support" OFF)
258 option(LWS_WITH_STRUCT_JSON "Generic struct serialization to and from JSON" OFF)
259 option(LWS_WITH_STRUCT_SQLITE3 "Generic struct serialization to and from SQLITE3" OFF)
260 # broken atm
261 #option(LWS_WITH_SMTP "Provide SMTP support" OFF)
262 if (LWS_WITH_ESP32)
263 option(LWS_WITH_DIR "Directory scanning api support" OFF)
264 option(LWS_WITH_LEJP_CONF "With LEJP configuration parser as used by lwsws" OFF)
265 else()
266 option(LWS_WITH_DIR "Directory scanning api support" ON)
267 option(LWS_WITH_LEJP_CONF "With LEJP configuration parser as used by lwsws" ON)
268 endif()
269 option(LWS_WITH_NO_LOGS "Disable all logging other than _err and _user from being compiled in" OFF)
270 set(LWS_LOGGING_BITFIELD_SET 0 CACHE STRING "Bitfield describing which log levels to force included into the build")
271 set(LWS_LOGGING_BITFIELD_CLEAR 0 CACHE STRING "Bitfield describing which log levels to force removed from the build")
272 option(LWS_LOGS_TIMESTAMP "Timestamp at start of logs" ON)
273 option(LWS_LOG_TAG_LIFECYCLE "Log tagged object lifecycle as NOTICE" ON)
274 option(LWS_AVOID_SIGPIPE_IGN "Android 7+ reportedly needs this" OFF)
275 option(LWS_WITH_JOSE "JOSE JSON Web Signature / Encryption / Keys (RFC7515/6/) API" OFF)
276 option(LWS_WITH_COSE "COSE CBOR Signature / Encryption / Keys (RFC8152) API" OFF)
277 option(LWS_WITH_GENCRYPTO "Enable support for Generic Crypto apis independent of TLS backend" OFF)
278 option(LWS_WITH_SELFTESTS "Selftests run at context creation" OFF)
279 option(LWS_WITH_GCOV "Build with gcc gcov coverage instrumentation" OFF)
280 option(LWS_WITH_EXPORT_LWSTARGETS "Export libwebsockets CMake targets.  Disable if they conflict with an outer cmake project." ON)
281 option(LWS_REPRODUCIBLE "Build libwebsockets reproducible. It removes the build user and hostname from the build" ON)
282 option(LWS_WITH_MINIMAL_EXAMPLES "Also build the normally standalone minimal examples, for QA" OFF)
283 option(LWS_WITH_LWSAC "lwsac Chunk Allocation api" ON)
284 option(LWS_WITH_CUSTOM_HEADERS "Store and allow querying custom HTTP headers (H1 only)" ON)
285 option(LWS_WITH_DISKCACHE "Hashed cache directory with lazy LRU deletion to size limit (unrelated to lws_cache_ttl)" OFF)
286 option(LWS_WITH_ASAN "Build with gcc runtime sanitizer options enabled (needs libasan)" OFF)
287 option(LWS_WITH_LEJP_CONF "With LEJP configuration parser as used by lwsws" OFF)
288 option(LWS_WITH_ZLIB "Include zlib support (required for extensions)" OFF)
289 option(LWS_WITH_BUNDLED_ZLIB "Use bundled zlib version (Windows only)" ${LWS_WITH_BUNDLED_ZLIB_DEFAULT})
290 option(LWS_WITH_MINIZ "Use miniz instead of zlib" OFF)
291 option(LWS_WITH_SEQUENCER "lws_seq_t support" OFF)
292 option(LWS_WITH_EXTERNAL_POLL "Support external POLL integration using callback messages (not recommended)" OFF)
293 option(LWS_WITH_LWS_DSH "Support lws_dsh_t Disordered Shared Heap" OFF)
294 option(LWS_CLIENT_HTTP_PROXYING "Support external http proxies for client connections" ON)
295 option(LWS_WITH_FILE_OPS "Support file operations vfs" ON)
296 option(LWS_WITH_UDP "Platform supports UDP" ON)
297 option(LWS_WITH_SPAWN "Spawn subprocesses with piped stdin/out/stderr" OFF)
298 option(LWS_WITH_FSMOUNT "Overlayfs and fallback mounting apis" OFF)
299 option(LWS_WITH_FANALYZER "Enable gcc -fanalyzer if compiler supports" OFF)
300 option(LWS_HTTP_HEADERS_ALL "Override header reduction optimization and include all like older lws versions" OFF)
301 option(LWS_WITH_SUL_DEBUGGING "Enable zombie lws_sul checking on object deletion" OFF)
302 option(LWS_WITH_PLUGINS_API "Build generic lws_plugins apis (see LWS_WITH_PLUGINS to also build protocol plugins)" OFF)
303 option(LWS_WITH_CONMON "Collect introspectable connection latency stats on individual client connections" ON)
304 option(LWS_WITHOUT_EVENTFD "Force using pipe instead of eventfd" OFF)
305 if (UNIX OR WIN32)
306         option(LWS_WITH_CACHE_NSCOOKIEJAR "Build file-backed lws-cache-ttl that uses netscape cookie jar format (linux-only)" ON)
307 else()
308         option(LWS_WITH_CACHE_NSCOOKIEJAR "Build file-backed lws-cache-ttl that uses netscape cookie jar format (linux-only)" OFF)
309 endif()
310
311 if (${CMAKE_SYSTEM_NAME} MATCHES "Linux")
312         option(LWS_WITH_NETLINK "Monitor Netlink for Routing Table changes" ON)
313 else()
314         set(LWS_WITH_NETLINK 0)
315 endif()
316
317 if (${CMAKE_SYSTEM_NAME} MATCHES "SunOS")
318        # its openssl has md5 deprecated
319        set(LWS_SUPPRESS_DEPRECATED_API_WARNINGS 1)
320 endif()
321
322
323 #
324 # to use miniz, enable both LWS_WITH_ZLIB and LWS_WITH_MINIZ
325 #
326 # End of user settings
327 #
328
329 # sets of sub-options implied by other options
330 #
331 set(LIB_LIST "")
332 set(LIB_LIST_AT_END)
333 set(LWS_LIBRARIES)
334 set(LWS_OPENSSL_SUPPORT 0)
335 include(CMakeLists-implied-options.txt)
336
337 #
338 # Structural helpers for cmake in subdirs
339 #
340
341 macro(add_subdir_include_directories arg1)
342         add_subdirectory(${arg1})
343         include_directories(${_CMAKE_INC_LIST})
344 endmacro()
345
346 macro(exports_to_parent_scope)
347         set(SOURCES ${SOURCES} PARENT_SCOPE)
348         if (LIB_LIST)
349                 set(LIB_LIST ${LIB_LIST} PARENT_SCOPE)
350         endif()
351         get_property(_CURR DIRECTORY PROPERTY INCLUDE_DIRECTORIES) 
352         set(_CMAKE_INC_LIST ${_CURR} PARENT_SCOPE)
353         if (LWS_LIB_BUILD_INC_PATHS)
354                 set(LWS_LIB_BUILD_INC_PATHS ${LWS_LIB_BUILD_INC_PATHS} PARENT_SCOPE)
355         endif()
356 endmacro()
357
358 macro(export_to_parent_intermediate)
359         set(SOURCES ${SOURCES} PARENT_SCOPE)
360         if (LIB_LIST)
361                 set(LIB_LIST ${LIB_LIST} PARENT_SCOPE)
362         endif()
363         set(_CMAKE_INC_LIST ${_CMAKE_INC_LIST} PARENT_SCOPE)
364         if (LWS_LIB_BUILD_INC_PATHS)
365                 set(LWS_LIB_BUILD_INC_PATHS ${LWS_LIB_BUILD_INC_PATHS} PARENT_SCOPE)
366         endif()
367 endmacro()
368
369 #
370 # Try to find the current Git hash
371 #
372
373 find_package(Git)
374 if(GIT_EXECUTABLE)
375         execute_process(
376                 WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}"
377                 COMMAND "${GIT_EXECUTABLE}" describe --tags --always
378                 OUTPUT_VARIABLE GIT_HASH
379                 OUTPUT_STRIP_TRAILING_WHITESPACE
380                 )
381         set(LWS_BUILD_HASH ${GIT_HASH})
382
383         # append the build user and hostname
384         if (NOT LWS_REPRODUCIBLE)
385                 execute_process(
386                         WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}"
387                         COMMAND "whoami"
388                         OUTPUT_VARIABLE GIT_USER
389                         OUTPUT_STRIP_TRAILING_WHITESPACE
390                         )
391                 execute_process(
392                         WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}"
393                         COMMAND "hostname"
394                         OUTPUT_VARIABLE GIT_HOST
395                         OUTPUT_STRIP_TRAILING_WHITESPACE
396                         )
397                 string(REGEX REPLACE "([^\\])[\\]([^\\])" "\\1\\\\\\\\\\2" GIT_USER ${GIT_USER})
398                 set(LWS_BUILD_HASH ${GIT_USER}@${GIT_HOST}-${GIT_HASH})
399         endif()
400
401         message("Git commit hash: ${LWS_BUILD_HASH}")
402 endif()
403
404 if ("${LWS_BUILD_HASH}" STREQUAL "")
405         set(LWS_BUILD_HASH "unknown")
406 endif()
407
408 set(PACKAGE "libwebsockets")
409 set(CPACK_RPM_PACKAGE_LICENSE "MIT")
410 set(CPACK_PACKAGE_NAME "${PACKAGE}")
411 set(CPACK_PACKAGE_VERSION_MAJOR "4")
412 set(CPACK_PACKAGE_VERSION_MINOR "3")
413 set(CPACK_PACKAGE_VERSION_PATCH_NUMBER "2")
414
415 set(CPACK_PACKAGE_VERSION_PATCH "${CPACK_PACKAGE_VERSION_PATCH_NUMBER}-${LWS_BUILD_HASH}")
416 set(CPACK_PACKAGE_RELEASE 1)
417
418 set(CPACK_PACKAGE_VERSION "${CPACK_PACKAGE_VERSION_MAJOR}.${CPACK_PACKAGE_VERSION_MINOR}.${CPACK_PACKAGE_VERSION_PATCH}")
419 set(CPACK_PACKAGE_VENDOR "andy@warmcat.com")
420 set(CPACK_PACKAGE_CONTACT "andy@warmcat.com")
421 set(CPACK_PACKAGE_DESCRIPTION_SUMMARY "${PACKAGE} ${CPACK_PACKAGE_VERSION}")
422 set(SOVERSION "19")
423 if(NOT CPACK_GENERATOR)
424     if(UNIX)
425         set(CPACK_GENERATOR "TGZ")
426     else()
427         set(CPACK_GENERATOR "ZIP")
428     endif()
429 endif()
430 set(CPACK_SOURCE_GENERATOR "TGZ")
431 set(CPACK_SOURCE_PACKAGE_FILE_NAME "${CPACK_PACKAGE_NAME}-${CPACK_PACKAGE_VERSION}")
432 set(VERSION "${CPACK_PACKAGE_VERSION}")
433
434 set(CPACK_RPM_PACKAGE_RELEASE_DIST ON)
435 set(CPACK_RPM_FILE_NAME "RPM-DEFAULT")
436 # below makes path length problems in CI
437 set(CPACK_RPM_DEBUGINFO_PACKAGE OFF)
438 # below makes some kind of chimera rpm with binaries and sources
439 set(CPACK_RPM_PACKAGE_SOURCES OFF)
440 set(CPACK_RPM_INSTALL_WITH_EXEC ON)
441 set(CPACK_RPM_COMPONENT_INSTALL ON)
442
443 set(CPACK_DEBIAN_FILE_NAME "DEB-DEFAULT")
444 set(CPACK_DEBIAN_PACKAGE_SHLIBDEPS ON)
445 set(CPACK_DEBIAN_DEBUGINFO_PACKAGE ON)
446 set(CPACK_DEBIAN_PACKAGE_SOURCE ON)
447 set(CPACK_DEBIAN_COMPONENT_INSTALL ON)
448
449
450 set(LWS_LIBRARY_VERSION ${CPACK_PACKAGE_VERSION})
451 set(LWS_LIBRARY_VERSION_MAJOR ${CPACK_PACKAGE_VERSION_MAJOR})
452 set(LWS_LIBRARY_VERSION_MINOR ${CPACK_PACKAGE_VERSION_MINOR})
453 set(LWS_LIBRARY_VERSION_PATCH ${CPACK_PACKAGE_VERSION_PATCH_NUMBER})
454 set(LWS_LIBRARY_VERSION_PATCH_ELABORATED ${CPACK_PACKAGE_VERSION_PATCH})
455
456 if (NOT CMAKE_MODULE_PATH)
457         set(CMAKE_MODULE_PATH "")
458 endif()
459 set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${PROJECT_SOURCE_DIR}/cmake/")
460
461
462 if (CMAKE_TOOLCHAIN_FILE)
463         message(STATUS "CMAKE_TOOLCHAIN_FILE='${CMAKE_TOOLCHAIN_FILE}'")
464 endif()
465
466 if (NOT LIB_SUFFIX)
467         set(LIB_SUFFIX "")
468 endif()
469
470 if (WIN32)
471         configure_file(${CMAKE_CURRENT_SOURCE_DIR}/win32port/version.rc.in ${CMAKE_CURRENT_BINARY_DIR}/win32port/version.rc @ONLY)
472         set(RESOURCES ${CMAKE_CURRENT_BINARY_DIR}/win32port/version.rc)
473 endif()
474
475 include_directories(include)
476
477 # Allow the user to override installation directories.
478 set(LWS_INSTALL_LIB_DIR       lib CACHE PATH "Installation directory for libraries")
479 set(LWS_INSTALL_BIN_DIR       bin CACHE PATH "Installation directory for executables")
480 set(LWS_INSTALL_INCLUDE_DIR   include CACHE PATH "Installation directory for header files")
481 set(LWS_INSTALL_EXAMPLES_DIR  bin CACHE PATH "Installation directory for example files")
482
483 # if you gave LWS_WITH_MINIZ, point to MINIZ here if not found
484 # automatically
485
486 set(LWS_ZLIB_LIBRARIES CACHE PATH "Path to the zlib/miniz library")
487 set(LWS_ZLIB_INCLUDE_DIRS CACHE PATH "Path to the zlib/miniz include directory")
488 set(LWS_SQLITE3_LIBRARIES CACHE PATH "Path to the sqlite3 library")
489 set(LWS_SQLITE3_INCLUDE_DIRS CACHE PATH "Path to the sqlite3 include directory")
490 set(LWS_LIBMOUNT_INCLUDE_DIRS CACHE PATH "Path to the libmount include directory")
491 set(LWS_LIBMOUNT_LIBRARIES CACHE PATH "Path to the libmount library")
492 # on unix, these are in the toolchain.  On win32 you have to put them somewhere
493 # yourself and point to them here
494 set(LWS_EXT_PTHREAD_INCLUDE_DIR CACHE PATH "Path to an external pthreads include directory")
495 set(LWS_EXT_PTHREAD_LIBRARIES CACHE PATH "Path to an external pthreads library")
496
497
498 if (LWS_WITH_HTTP_STREAM_COMPRESSION)
499         set(LWS_WITH_ZLIB 1)
500 endif()
501
502 if (LWS_WITH_ZLIB AND NOT LWS_WITH_BUNDLED_ZLIB)
503         if ("${LWS_ZLIB_LIBRARIES}" STREQUAL "" OR "${LWS_ZLIB_INCLUDE_DIRS}" STREQUAL "")
504         else()
505                 set(ZLIB_LIBRARIES ${LWS_ZLIB_LIBRARIES})
506                 set(ZLIB_INCLUDE_DIRS ${LWS_ZLIB_INCLUDE_DIRS})
507                 set(ZLIB_FOUND 1)
508         endif()
509 endif()
510
511
512 if (LWS_WITH_SQLITE3)
513         if ("${LWS_SQLITE3_LIBRARIES}" STREQUAL "" OR "${LWS_SQLITE3_INCLUDE_DIRS}" STREQUAL "")
514         else()
515                 set(SQLITE3_LIBRARIES ${LWS_SQLITE3_LIBRARIES})
516                 set(SQLITE3_INCLUDE_DIRS ${LWS_SQLITE3_INCLUDE_DIRS})
517                 set(SQLITE3_FOUND 1)
518         endif()
519 endif()
520
521 include_directories("${PROJECT_BINARY_DIR}")
522
523 # Check for different inline keyword versions.
524 foreach(KEYWORD "inline" "__inline__" "__inline")
525         set(CMAKE_REQUIRED_DEFINITIONS "-DKEYWORD=${KEYWORD}")
526         CHECK_C_SOURCE_COMPILES(
527                 "
528                 #include <stdio.h>
529                 static KEYWORD void a() {}
530                 int main(int argc, char **argv) { a(); return 0; }
531                 " LWS_HAVE_${KEYWORD})
532 endforeach()
533
534 if (NOT LWS_HAVE_inline)
535         if (LWS_HAVE___inline__)
536                 set(inline __inline__)
537         elseif(LWS_HAVE___inline)
538                 set(inline __inline)
539         endif()
540 endif()
541
542 # Put the libraries and binaries that get built into directories at the
543 # top of the build tree rather than in hard-to-find leaf directories. 
544 SET(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${PROJECT_BINARY_DIR}/bin")
545 SET(CMAKE_LIBRARY_OUTPUT_DIRECTORY "${PROJECT_BINARY_DIR}/lib")
546 SET(CMAKE_ARCHIVE_OUTPUT_DIRECTORY "${PROJECT_BINARY_DIR}/lib")
547
548 SET(LWS_INSTALL_PATH "${CMAKE_INSTALL_PREFIX}")
549
550 # Put absolute path of dynamic libraries into the object code. Some
551 # architectures, notably Mac OS X, need this.
552 SET(CMAKE_INSTALL_NAME_DIR "${CMAKE_INSTALL_PREFIX}/${LWS_INSTALL_LIB_DIR}${LIB_SUFFIX}")
553
554 if (LWS_WITHOUT_BUILTIN_SHA1)
555         set(LWS_SHA1_USE_OPENSSL_NAME 1)
556 endif()
557
558 CHECK_C_SOURCE_COMPILES(
559         "#include <malloc.h>
560         int main(int argc, char **argv) { return malloc_trim(0); }
561         " LWS_HAVE_MALLOC_TRIM)
562 CHECK_C_SOURCE_COMPILES(
563         "#include <malloc.h>
564         int main(int argc, char **argv) { return (int)malloc_usable_size((void *)0); }
565         " LWS_HAVE_MALLOC_USABLE_SIZE)
566
567 CHECK_FUNCTION_EXISTS(fork LWS_HAVE_FORK)
568 CHECK_FUNCTION_EXISTS(getenv LWS_HAVE_GETENV)
569 CHECK_FUNCTION_EXISTS(malloc LWS_HAVE_MALLOC)
570 CHECK_FUNCTION_EXISTS(memset LWS_HAVE_MEMSET)
571 CHECK_FUNCTION_EXISTS(realloc LWS_HAVE_REALLOC)
572 CHECK_FUNCTION_EXISTS(socket LWS_HAVE_SOCKET)
573 CHECK_FUNCTION_EXISTS(strerror LWS_HAVE_STRERROR)
574 CHECK_FUNCTION_EXISTS(vfork LWS_HAVE_VFORK)
575 CHECK_FUNCTION_EXISTS(execvpe LWS_HAVE_EXECVPE)
576 CHECK_FUNCTION_EXISTS(getifaddrs LWS_HAVE_GETIFADDRS)
577 CHECK_FUNCTION_EXISTS(snprintf LWS_HAVE_SNPRINTF)
578 CHECK_FUNCTION_EXISTS(_snprintf LWS_HAVE__SNPRINTF)
579 CHECK_FUNCTION_EXISTS(_vsnprintf LWS_HAVE__VSNPRINTF)
580 CHECK_FUNCTION_EXISTS(getloadavg LWS_HAVE_GETLOADAVG)
581 CHECK_FUNCTION_EXISTS(atoll LWS_HAVE_ATOLL)
582 CHECK_FUNCTION_EXISTS(_atoi64 LWS_HAVE__ATOI64)
583 CHECK_FUNCTION_EXISTS(_stat32i64 LWS_HAVE__STAT32I64)
584 CHECK_FUNCTION_EXISTS(clock_gettime LWS_HAVE_CLOCK_GETTIME)
585 CHECK_FUNCTION_EXISTS(localtime_r LWS_HAVE_LOCALTIME_R)
586 CHECK_FUNCTION_EXISTS(gmtime_r LWS_HAVE_GMTIME_R)
587 CHECK_FUNCTION_EXISTS(ctime_r LWS_HAVE_CTIME_R)
588 CHECK_FUNCTION_EXISTS(getgrgid_r LWS_HAVE_GETGRGID_R)
589 CHECK_FUNCTION_EXISTS(getgrnam_r LWS_HAVE_GETGRNAM_R)
590 CHECK_FUNCTION_EXISTS(getpwuid_r LWS_HAVE_GETPWUID_R)
591 CHECK_FUNCTION_EXISTS(getpwnam_r LWS_HAVE_GETPWNAM_R)
592 CHECK_FUNCTION_EXISTS(timegm LWS_HAVE_TIMEGM)
593
594 if(CMAKE_SYSTEM_NAME MATCHES "Darwin")
595         if(CMAKE_OSX_DEPLOYMENT_TARGET LESS "10.12")
596                 message("No clock_gettime found on macOS ${CMAKE_OSX_DEPLOYMENT_TARGET}. Disabling LWS_HAVE_CLOCK_GETTIME.")
597                 set(LWS_HAVE_CLOCK_GETTIME 0)
598         endif()
599 endif()
600
601 if (NOT LWS_HAVE_GETIFADDRS)
602         if (LWS_WITHOUT_BUILTIN_GETIFADDRS)
603                 message(FATAL_ERROR "No getifaddrs was found on the system. Turn off the LWS_WITHOUT_BUILTIN_GETIFADDRS compile option to use the supplied BSD version.")
604         endif()
605         set(LWS_BUILTIN_GETIFADDRS 1)
606 endif()
607
608 if (LWS_EXT_PTHREAD_INCLUDE_DIR)
609         set(CMAKE_REQUIRED_INCLUDES ${CMAKE_REQUIRED_INCLUDES};${LWS_EXT_PTHREAD_INCLUDE_DIR})
610         include_directories(${LWS_EXT_PTHREAD_INCLUDE_DIR})
611
612         list(APPEND LIB_LIST_AT_END ${LWS_EXT_PTHREAD_LIBRARIES})
613         set(CMAKE_REQUIRED_FLAGS ${CMAKE_REQUIRED_FLAGS} " -DHAVE_STRUCT_TIMESPEC=1")
614 endif()
615
616 #
617 # add libs here that need to be at the end of the link order
618 #
619
620 if (LWS_EXT_PTHREAD_INCLUDE_DIR)
621         list(APPEND LIB_LIST_AT_END ${LWS_EXT_PTHREAD_LIBRARIES})
622 endif()
623
624 if (LWS_WITH_ZLIB AND NOT LWS_WITH_BUNDLED_ZLIB)
625         list(APPEND LIB_LIST_AT_END "${ZLIB_LIBRARIES}")
626 endif()
627
628 if (LWS_WITH_PLUGINS_API AND UNIX AND CMAKE_DL_LIBS AND NOT (${CMAKE_SYSTEM_NAME} MATCHES "QNX"))
629         list(APPEND LIB_LIST_AT_END ${CMAKE_DL_LIBS})
630 endif()
631
632
633 CHECK_INCLUDE_FILE(in6addr.h LWS_HAVE_IN6ADDR_H)
634 CHECK_INCLUDE_FILE(memory.h LWS_HAVE_MEMORY_H)
635 CHECK_INCLUDE_FILE(netinet/in.h LWS_HAVE_NETINET_IN_H)
636 CHECK_INCLUDE_FILE(stdint.h LWS_HAVE_STDINT_H)
637 CHECK_INCLUDE_FILE(stdlib.h LWS_HAVE_STDLIB_H)
638 CHECK_INCLUDE_FILE(strings.h LWS_HAVE_STRINGS_H)
639 CHECK_INCLUDE_FILE(string.h LWS_HAVE_STRING_H)
640 CHECK_INCLUDE_FILE(sys/prctl.h LWS_HAVE_SYS_PRCTL_H)
641 CHECK_INCLUDE_FILE(sys/socket.h LWS_HAVE_SYS_SOCKET_H)
642 CHECK_INCLUDE_FILE(sys/sockio.h LWS_HAVE_SYS_SOCKIO_H)
643 CHECK_INCLUDE_FILE(sys/stat.h LWS_HAVE_SYS_STAT_H)
644 CHECK_INCLUDE_FILE(sys/types.h LWS_HAVE_SYS_TYPES_H)
645 CHECK_INCLUDE_FILE(unistd.h LWS_HAVE_UNISTD_H)
646 CHECK_INCLUDE_FILE(vfork.h LWS_HAVE_VFORK_H)
647 CHECK_INCLUDE_FILE(sys/capability.h LWS_HAVE_SYS_CAPABILITY_H)
648 CHECK_INCLUDE_FILE(malloc.h LWS_HAVE_MALLOC_H)
649 CHECK_INCLUDE_FILE(pthread.h LWS_HAVE_PTHREAD_H)
650 CHECK_INCLUDE_FILE(inttypes.h LWS_HAVE_INTTYPES_H)
651 CHECK_INCLUDE_FILE(sys/resource.h LWS_HAVE_SYS_RESOURCE_H)
652
653 if (WIN32 OR MSVC)
654         CHECK_C_SOURCE_COMPILES("#include <winsock2.h>
655                                  #include <afunix.h>
656                                  int main() { return 0; }" LWS_HAVE_WIN32_AFUNIX_H)
657
658         if (LWS_UNIX_SOCK AND NOT LWS_HAVE_WIN32_AFUNIX_H)
659                 message("No afunix.h found. Disabling LWS_UNIX_SOCK.")
660                 set(LWS_WITH_UNIX_SOCK OFF)
661         endif()
662 endif()
663
664 CHECK_LIBRARY_EXISTS(cap cap_set_flag "" LWS_HAVE_LIBCAP)
665
666
667 if (LWS_WITH_ZLIB AND NOT LWS_WITH_BUNDLED_ZLIB)
668         if (LWS_WITH_MINIZ)
669                 CHECK_INCLUDE_FILE(miniz.h LWS_HAVE_ZLIB_H)
670         else()
671                 CHECK_INCLUDE_FILE(zlib.h LWS_HAVE_ZLIB_H)
672         endif()
673 endif()
674
675 CHECK_INCLUDE_FILES("stdlib.h;stdarg.h;string.h" STDC_HEADERS)
676
677 if (NOT CMAKE_REQUIRED_FLAGS)
678         set(CMAKE_REQUIRED_FLAGS "")
679 endif()
680 if (NOT CMAKE_REQUIRED_INCLUDES)
681         set(CMAKE_REQUIRED_INCLUDES "")
682 endif()
683 if (NOT CMAKE_REQUIRED_LIBRARIES)
684         set(CMAKE_REQUIRED_LIBRARIES "")
685 endif()
686
687 CHECK_C_SOURCE_COMPILES("#include <stdint.h> 
688         int main(void) {
689                 intptr_t test = 1;
690                 return 0;
691         }" LWS_HAS_INTPTR_T)
692
693 if ((CMAKE_C_COMPILER_ID MATCHES "Clang") OR
694     (CMAKE_CXX_COMPILER_ID MATCHES "Clang"))
695         set(COMPILER_IS_CLANG ON)
696 endif()
697
698 if (LWS_HAVE_PTHREAD_H AND NOT LWS_PLAT_FREERTOS)
699         CHECK_C_SOURCE_COMPILES("
700                 #ifndef _GNU_SOURCE
701                 #define _GNU_SOURCE
702                 #endif
703                 #include <pthread.h>
704                 int main(void) {
705                 #ifdef __PTW32_H
706                         pthread_t th = {0,0};
707                 #else
708                         pthread_t th = 0;
709                 #endif
710                         pthread_setname_np(th, NULL);
711                         return 0;
712                 }" LWS_HAS_PTHREAD_SETNAME_NP)
713 endif()
714
715 CHECK_C_SOURCE_COMPILES("#include <stddef.h>
716         #include <getopt.h>
717         int main(void) {
718                 void *p = (void *)getopt_long;
719                 return p != NULL;
720         }" LWS_HAS_GETOPT_LONG)
721
722 CHECK_C_SOURCE_COMPILES("#include <linux/rtnetlink.h>
723         int main(void) {
724                 int test = RTA_PREF;
725                 return 0;
726         }" LWS_HAVE_RTA_PREF)
727
728 CHECK_C_SOURCE_COMPILES("#include <sys/types.h>
729         int main(void) {
730                 suseconds_t x = 0;
731                 return (int)x;
732         }" LWS_HAVE_SUSECONDS_T)
733
734 if (NOT PID_T_SIZE)
735         set(pid_t int)
736 endif()
737
738 if (NOT SIZE_T_SIZE)
739         set(size_t "unsigned int")
740 endif()
741
742 if (NOT LWS_HAVE_MALLOC)
743         set(malloc rpl_malloc)
744 endif()
745
746 if (NOT LWS_HAVE_REALLOC)
747         set(realloc rpl_realloc)
748 endif()
749
750
751
752
753 if (CMAKE_COMPILER_IS_GNUCC OR CMAKE_COMPILER_IS_GNUCXX OR COMPILER_IS_CLANG)
754     include (CheckCCompilerFlag)
755     CHECK_C_COMPILER_FLAG(-fvisibility=hidden LWS_HAVE_VISIBILITY)
756     if (LWS_WITH_FANALYZER)
757             CHECK_C_COMPILER_FLAG(-fanalyzer LWS_HAVE_FANALYZER)
758     endif()
759     if (LWS_HAVE_VISIBILITY)
760                 set(VISIBILITY_FLAG -fvisibility=hidden)
761     endif()
762     if (LWS_WITH_GCOV)
763             set (GCOV_FLAGS "-fprofile-arcs -ftest-coverage ")
764     else()
765             set(GCOV_FLAGS "")
766     endif()
767
768         if (LWS_WITH_ASAN)
769                 set (ASAN_FLAGS "-fsanitize=address -fsanitize=undefined -fsanitize-address-use-after-scope -fsanitize-undefined-trap-on-error")
770                 if (NOT COMPILER_IS_CLANG)
771                         set (ASAN_FLAGS "${ASAN_FLAGS} -fsanitize=pointer-compare -fsanitize=pointer-subtract -fsanitize=leak")
772                 endif()
773                 message("Enabling ASAN")
774         else()
775                 set(ASAN_FLAGS "")
776         endif()
777
778         check_c_compiler_flag("-Wignored-qualifiers" LWS_GCC_HAS_IGNORED_QUALIFIERS)
779         check_c_compiler_flag("-Wtype-limits" LWS_GCC_HAS_TYPE_LIMITS)
780         check_c_compiler_flag("-Wno-deprecated-declarations" LWS_GCC_HAS_NO_DEPRECATED_DECLARATIONS)
781
782         if (LWS_GCC_HAS_IGNORED_QUALIFIERS)
783                 set(CMAKE_C_FLAGS "-Wignored-qualifiers ${CMAKE_C_FLAGS}" )
784         endif()
785
786         if (LWS_GCC_HAS_TYPE_LIMITS)
787                 set(CMAKE_C_FLAGS "-Wtype-limits ${CMAKE_C_FLAGS}" )
788         endif()
789
790         if (LWS_WITH_FANALYZER AND LWS_HAVE_FANALYZER)
791                 set(CMAKE_C_FLAGS "-fanalyzer ${CMAKE_C_FLAGS}" )
792         endif()
793
794         if (CMAKE_COMPILER_IS_CLANG OR CMAKE_C_COMPILER_VERSION VERSION_GREATER 4.4)
795                 set(CMAKE_C_FLAGS "-Wuninitialized ${CMAKE_C_FLAGS}")
796         endif()
797
798         # always warn all and generate debug info
799         if (UNIX AND NOT LWS_PLAT_FREERTOS)
800                 set(CMAKE_C_FLAGS "-Wall -Wextra -Wno-unused-parameter -Wconversion -Wsign-compare -Wstrict-aliasing ${VISIBILITY_FLAG} -Wundef ${GCOV_FLAGS} ${CMAKE_C_FLAGS} ${ASAN_FLAGS}" )
801         else()
802                 set(CMAKE_C_FLAGS "-Wall -Wsign-compare ${VISIBILITY_FLAG} ${GCOV_FLAGS} ${CMAKE_C_FLAGS}" )
803         endif()
804
805         if (PICO_SDK_PATH)
806                 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall -Wextra -Wno-unused-parameter -Wconversion -Wsign-compare -Wstrict-aliasing -Wundef -nolibc")
807         endif()
808
809         if (ESP_PLATFORM AND (CONFIG_IDF_TARGET_ESP32 OR CONFIG_IDF_TARGET_ESP32S2 OR CONFIG_IDF_TARGET_ESP32S3))
810                 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -mlongcalls")
811         endif()
812
813         if ("${DISABLE_WERROR}" STREQUAL "OFF")
814                 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Werror")
815         endif()
816
817         if (LWS_SUPPRESS_DEPRECATED_API_WARNINGS)
818                 set(CMAKE_C_FLAGS "-Wno-deprecated ${CMAKE_C_FLAGS}")
819                 if (LWS_GCC_HAS_NO_DEPRECATED_DECLARATIONS)
820                         set(CMAKE_C_FLAGS "-Wno-deprecated-declarations ${CMAKE_C_FLAGS}")
821                 endif()
822         endif()
823 endif ()
824
825 if ((CMAKE_COMPILER_IS_GNUCC OR CMAKE_COMPILER_IS_GNUCXX) AND NOT LWS_WITHOUT_TESTAPPS)
826         if (UNIX AND LWS_HAVE_PTHREAD_H AND NOT (${CMAKE_SYSTEM_NAME} MATCHES "QNX"))
827         # jeez clang understands -pthread but dies if he sees it at link time!
828         # http://stackoverflow.com/questions/2391194/what-is-gs-pthread-equiv-in-clang
829         set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -pthread" )
830         list(APPEND LIB_LIST_AT_END -lpthread)
831     endif()
832 endif()
833
834 if (COMPILER_IS_CLANG)
835
836         # otherwise osx blows a bunch of openssl deprecated api errors
837         set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wno-deprecated-declarations" )
838         if (UNIX AND LWS_HAVE_PTHREAD_H)
839                 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -pthread -Wno-error=unused-command-line-argument" )
840         endif()
841 endif()
842
843 if (WINCE)
844         list(APPEND LIB_LIST_AT_END ws2.lib)
845 elseif (WIN32)
846         list(APPEND LIB_LIST_AT_END ws2_32.lib userenv.lib psapi.lib iphlpapi.lib crypt32.lib)
847 endif()
848
849 if (MSVC)
850         # Turn off pointless microsoft security warnings.
851         add_definitions(-D_CRT_SECURE_NO_DEPRECATE -D_CRT_NONSTDC_NO_DEPRECATE)
852         # Fail the build if any warnings
853         add_compile_options(/W3 /WX)
854         # Unbreak MSVC broken preprocessor __VA_ARGS__ behaviour
855         if (MSVC_VERSION GREATER 1925)
856                 add_compile_options(/Zc:preprocessor /wd5105)
857         else()
858                 add_compile_options(/experimental:preprocessor /wd5105)
859         endif()
860 endif(MSVC)
861
862 if (MINGW)
863         set(LWS_MINGW_SUPPORT 1)
864         set(CMAKE_C_FLAGS "-D__USE_MINGW_ANSI_STDIO ${CMAKE_C_FLAGS}")
865         add_definitions(-DWINVER=0x0601 -D_WIN32_WINNT=0x0601)
866 endif()
867
868 if (HDR_PRIVATE)
869         source_group("Headers Private"  FILES ${HDR_PRIVATE})
870 endif()
871 if (HDR_PUBLIC)
872         source_group("Headers Public"   FILES ${HDR_PUBLIC})
873 endif()
874 if (SOURCES)
875         source_group("Sources"          FILES ${SOURCES})
876 endif()
877 if (RESOURCES)
878         source_group("Resources"        FILES ${RESOURCES})
879 endif()
880
881
882 #
883 # ZLIB (needed for deflate extension and if LWS_WITH_HTTP_STREAM_COMPRESSION)
884 #
885 if (LWS_WITH_ZLIB)
886         if (NOT ZLIB_FOUND)
887                 if (LWS_WITH_MINIZ)
888                         find_package(Miniz REQUIRED)
889                         set(ZLIB_INCLUDE_DIRS ${MINIZ_INCLUDE_DIR})
890                         set(ZLIB_LIBRARIES ${MINIZ_LIBRARIES})
891                 else()
892                         find_package(ZLIB REQUIRED)
893                 endif()
894         endif()
895         message("zlib/miniz include dirs: ${ZLIB_INCLUDE_DIRS}")
896         message("zlib/miniz libraries: ${ZLIB_LIBRARIES}")
897         include_directories(${ZLIB_INCLUDE_DIRS})
898         # done later at end of link list
899         # list(APPEND LIB_LIST ${ZLIB_LIBRARIES})
900         set(CMAKE_REQUIRED_LIBRARIES ${CMAKE_REQUIRED_LIBRARIES} ${ZLIB_LIBRARIES})
901         list(APPEND LIB_LIST_AT_END ${ZLIB_LIBRARIES})
902 endif()
903
904
905 if (LWS_WITH_FSMOUNT AND ${CMAKE_SYSTEM_NAME} STREQUAL "Linux")
906         if (NOT LWS_LIBMOUNT_INCLUDE_DIRS STREQUAL "")
907                 include_directories(${LWS_LIBMOUNT_INCLUDE_DIRS})
908                 message("libmount include dir: ${LWS_LIBMOUNT_INCLUDE_DIRS}")
909         endif()
910         if (NOT LWS_LIBMOUNT_LIBRARIES STREQUAL "")
911                 message("libmount libraries: ${LWS_LIBMOUNT_LIBRARIES}")
912                 list(APPEND LIB_LIST ${LWS_LIBMOUNT_LIBRARIES})
913         else()
914                 list(APPEND LIB_LIST mount)
915         endif()
916 endif()
917
918
919 if (LWS_WITH_SQLITE3)
920         if (NOT SQLITE3_FOUND)
921                 find_path(SQLITE3_INCLUDE_DIRS NAMES sqlite3.h)
922                 find_library(SQLITE3_LIBRARIES NAMES sqlite3)
923                 if(SQLITE3_INCLUDE_DIRS AND SQLITE3_LIBRARIES)
924                         set(SQLITE3_FOUND 1)
925                 endif()
926         endif()
927         message("sqlite3 include dir: ${SQLITE3_INCLUDE_DIRS}")
928         message("sqlite3 libraries: ${SQLITE3_LIBRARIES}")
929         include_directories("${SQLITE3_INCLUDE_DIRS}")
930         list(APPEND LIB_LIST ${SQLITE3_LIBRARIES})
931 endif()
932
933
934 if (LWS_WITH_HUBBUB)
935         find_library(LIBHUBBUB_LIBRARIES NAMES hubbub)
936         list(APPEND LIB_LIST ${LIBHUBBUB_LIBRARIES} )
937 endif()
938
939 if (LWS_HAVE_LIBCAP)
940         find_library(LIBCAP_LIBRARIES NAMES cap)
941         list(APPEND LIB_LIST ${LIBCAP_LIBRARIES} )
942 endif()
943
944 if (LWS_WITH_PLUGINS_BUILTIN)
945         add_subdirectory(plugins)
946 endif()
947
948
949 #
950 # Append the "at end" pieces to the lib list
951 #
952 list(APPEND LIB_LIST ${LIB_LIST_AT_END})
953
954 #
955 # Second-level CMakeLists
956 #
957
958 include_directories("${PROJECT_SOURCE_DIR}/lib")
959
960 add_subdirectory(lib)
961
962
963 if(WIN32 AND NOT CYGWIN)
964   set(DEF_INSTALL_CMAKE_DIR cmake)
965 else()
966   set(DEF_INSTALL_CMAKE_DIR lib${LIB_SUFFIX}/cmake/libwebsockets)
967 endif()
968                 
969 configure_file(${PROJECT_SOURCE_DIR}/cmake/LwsCheckRequirements.cmake
970                 ${PROJECT_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/LwsCheckRequirements.cmake
971                 @ONLY)
972                 
973 configure_file(${PROJECT_SOURCE_DIR}/cmake/LwsCheckRequirements.cmake
974                 ${PROJECT_BINARY_DIR}/LwsCheckRequirements.cmake
975                 @ONLY)  
976
977 # Generate version info for both build-tree and install-tree.
978 configure_file(${PROJECT_SOURCE_DIR}/cmake/libwebsockets-config-version.cmake.in
979                 ${PROJECT_BINARY_DIR}/libwebsockets-config-version.cmake 
980                 @ONLY)
981
982 # Generate the config file for the build-tree.
983 set(LWS__INCLUDE_DIRS 
984     "${PROJECT_SOURCE_DIR}/lib"
985     "${PROJECT_BINARY_DIR}")
986 set(LIBWEBSOCKETS_INCLUDE_DIRS ${LWS__INCLUDE_DIRS} CACHE PATH "Libwebsockets include directories")
987 configure_file(${PROJECT_SOURCE_DIR}/cmake/libwebsockets-config.cmake.in
988                 ${PROJECT_BINARY_DIR}/libwebsockets-config.cmake 
989                 @ONLY)
990 set(LWS_INSTALL_CMAKE_DIR ${DEF_INSTALL_CMAKE_DIR} CACHE PATH "Installation directory for CMake files")
991
992 # Export targets (This is used for other CMake projects to easily find the libraries and include files).
993 if (LWS_WITH_EXPORT_LWSTARGETS)
994     export(TARGETS ${LWS_LIBRARIES}
995             FILE "${PROJECT_BINARY_DIR}/LibwebsocketsTargets.cmake")
996 endif()
997
998
999
1000 set(libwebsockets_DIR ${PROJECT_BINARY_DIR})
1001 set(CMAKE_MODULE_PATH "${PROJECT_SOURCE_DIR}/cmake")
1002 message("DIR ${libwebsockets_DIR} CMP ${CMAKE_MODULE_PATH}")
1003
1004 if (LWS_WITH_MINIMAL_EXAMPLES)
1005         add_subdirectory(minimal-examples)
1006 endif()
1007
1008 if (NOT LWS_WITHOUT_TESTAPPS)
1009         add_subdirectory(test-apps)
1010 endif()
1011
1012 if (NOT LWS_WITH_PLUGINS_BUILTIN)
1013 add_subdirectory(plugins)
1014 endif()
1015 add_subdirectory(lwsws)
1016
1017 # Generate the lws_config.h that includes all the public compilation settings.
1018 configure_file(
1019        "${PROJECT_SOURCE_DIR}/cmake/lws_config.h.in"
1020        "${PROJECT_BINARY_DIR}/lws_config.h")
1021        
1022 add_custom_command(
1023                 OUTPUT ${PROJECT_BINARY_DIR}/include/lws_config.h
1024                         ${PROJECT_BINARY_DIR}/include/libwebsockets
1025                         ${PROJECT_BINARY_DIR}/include/libwebsockets.h
1026                 COMMENT "Creating build include dir"
1027                 COMMAND ${CMAKE_COMMAND} -E copy ${CMAKE_CURRENT_SOURCE_DIR}/include/libwebsockets.h
1028                         ${CMAKE_CURRENT_BINARY_DIR}/include/libwebsockets.h
1029                 COMMAND ${CMAKE_COMMAND} -E copy_directory ${CMAKE_CURRENT_SOURCE_DIR}/include/libwebsockets/
1030                         ${CMAKE_CURRENT_BINARY_DIR}/include/libwebsockets
1031                 COMMAND ${CMAKE_COMMAND} -E copy ${PROJECT_BINARY_DIR}/lws_config.h
1032                         ${CMAKE_CURRENT_BINARY_DIR}/include/lws_config.h
1033                 MAIN_DEPENDENCY ${PROJECT_BINARY_DIR}/lws_config.h
1034 )
1035
1036 add_custom_target(GENHDR DEPENDS  ${PROJECT_BINARY_DIR}/include/lws_config.h)
1037
1038 file(GLOB HDR_PUBLIC1 RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} include/libwebsockets/*.h)
1039 file(GLOB HDR_PUBLIC2 RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} include/libwebsockets.h)
1040 file(GLOB HDR_PUBLIC3 RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} ${CMAKE_CURRENT_BINARY_DIR}/include/lws_config.h)
1041 list(APPEND HDR_PUBLIC ${HDR_PUBLIC1} ${HDR_PUBLIC2} ${HDR_PUBLIC3})
1042
1043 set_source_files_properties(${HDR_PUBLIC} PROPERTIES GENERATED 1)
1044
1045 if (LWS_WITH_STATIC)
1046         add_dependencies(websockets GENHDR)
1047 endif()
1048 if (LWS_WITH_SHARED)
1049         add_dependencies(websockets_shared GENHDR)
1050 endif()
1051
1052
1053
1054 #
1055 #
1056 # Installation preparations.
1057 #
1058
1059 export(PACKAGE libwebsockets)
1060
1061 install(DIRECTORY include/libwebsockets
1062         DESTINATION "${LWS_INSTALL_INCLUDE_DIR}" COMPONENT dev)
1063 install(FILES ${PROJECT_BINARY_DIR}/include/libwebsockets.h ${PROJECT_BINARY_DIR}/include/lws_config.h
1064         DESTINATION "${LWS_INSTALL_INCLUDE_DIR}" COMPONENT dev)
1065
1066 # Generate the config file for the installation tree.
1067 get_filename_component(LWS_ABSOLUTE_INSTALL_CMAKE_DIR ${LWS_INSTALL_CMAKE_DIR} ABSOLUTE)
1068 get_filename_component(LWS_ABSOLUTE_INSTALL_INCLUDE_DIR ${LWS_INSTALL_INCLUDE_DIR} ABSOLUTE)
1069 file(RELATIVE_PATH 
1070     REL_INCLUDE_DIR 
1071     "${LWS_ABSOLUTE_INSTALL_CMAKE_DIR}"
1072     "${LWS_ABSOLUTE_INSTALL_INCLUDE_DIR}") # Calculate the relative directory from the cmake dir.
1073
1074 if (DEFINED REL_INCLUDE_DIR)
1075     set(LWS__INCLUDE_DIRS "\${LWS_CMAKE_DIR}/${REL_INCLUDE_DIR}")
1076 endif()
1077 if (DEFINED OPENSSL_INCLUDE_DIRS)
1078         set(LWS__INCLUDE_DIRS "${LWS__INCLUDE_DIRS};${OPENSSL_INCLUDE_DIRS}")
1079 endif()
1080
1081 configure_file(${PROJECT_SOURCE_DIR}/cmake/libwebsockets-config.cmake.in
1082                 ${PROJECT_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/libwebsockets-config.cmake
1083                 @ONLY)
1084
1085 set_target_properties(${LWS_LIBRARIES}
1086                 PROPERTIES PUBLIC_HEADER "${HDR_PUBLIC}")
1087
1088 # Install the LibwebsocketsConfig.cmake and LibwebsocketsConfigVersion.cmake
1089 install(FILES
1090                "${PROJECT_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/libwebsockets-config.cmake"
1091                "${PROJECT_BINARY_DIR}/libwebsockets-config-version.cmake"
1092                "${PROJECT_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/LwsCheckRequirements.cmake"
1093                DESTINATION "${LWS_INSTALL_CMAKE_DIR}" COMPONENT dev)
1094
1095 # Install exports for the install-tree.
1096 if (LWS_WITH_EXPORT_LWSTARGETS)
1097     install(EXPORT LibwebsocketsTargets
1098             DESTINATION "${LWS_INSTALL_CMAKE_DIR}" COMPONENT dev)
1099 endif()
1100
1101 # build subdir is not part of sources
1102 set(CPACK_SOURCE_IGNORE_FILES $(CPACK_SOURCE_IGNORE_FILES) "/.git/" "/build/" "\\\\.tgz$" "\\\\.tar\\\\.gz$")
1103
1104 # Most people are more used to "make dist" compared to "make package_source"
1105 add_custom_target(dist COMMAND "${CMAKE_MAKE_PROGRAM}" package_source)
1106
1107
1108
1109 # This must always be last!
1110 include(CPack)