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