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