1 cmake_minimum_required(VERSION 2.6)
5 set(PACKAGE "libwebsockets")
6 set(PACKAGE_VERSION "1.1")
7 set(PACKAGE_BUGREPORT "andy@warmcat.com")
8 set(PACKAGE_NAME "${PACKAGE}")
9 set(PACKAGE_STRING "${PACKAGE} ${PACKAGE_VERSION}")
10 set(PACKAGE_TARNAME "${PACKAGE}")
11 set(PACKAGE_URL "http://libwebsockets.org")
12 set(VERSION "{PACKAGE_VERSION}")
14 set(LWS_LIBRARY_VERSION ${PACKAGE_VERSION})
16 # Try to find the current Git hash.
20 COMMAND "${GIT_EXECUTABLE}" log -n 1 --pretty=%h
21 OUTPUT_VARIABLE GIT_HASH
22 OUTPUT_STRIP_TRAILING_WHITESPACE
25 set(LWS_BUILD_HASH ${GIT_HASH})
26 message("Git commit hash: ${LWS_BUILD_HASH}")
29 option(WITH_SSL "Include SSL support (default OpenSSL, CyaSSL if USE_CYASSL is set)" ON)
30 option(USE_EXTERNAL_ZLIB "Search the system for ZLib instead of using the included one (on Windows)" OFF)
31 option(USE_CYASSL "Use CyaSSL replacement for OpenSSL" OFF)
32 option(WITH_BUILTIN_GETIFADDRS "Use BSD getifaddrs implementation from libwebsockets... default is your libc provides it" OFF)
33 option(WITHOUT_TESTAPPS "Don't build the libwebsocket-test-apps" OFF)
34 option(WITHOUT_CLIENT "Don't build the client part of the library" OFF)
35 option(WITHOUT_SERVER "Don't build the server part of the library" OFF)
36 option(WITH_LIBCRYPTO "Use libcrypto MD5 and SHA1 implementations" ON)
37 option(WITHOUT_PING "Don't build the ping test application" OFF)
38 option(WITHOUT_DEBUG "Don't compile debug related code" OFF)
39 option(WITHOUT_EXTENSIONS "Don't compile with extensions" OFF)
40 option(WITH_LATENCY "Build latency measuring code into the library" OFF)
41 option(WITHOUT_DAEMONIZE "Don't build the daemonization api" OFF)
43 # The base dir where the SSL dirs should be looked for.
44 set(SSL_CERT_DIR CACHE STRING "")
45 set(SSL_CLIENT_CERT_DIR CACHE STRING "")
47 if ("${SSL_CERT_DIR}" STREQUAL "")
51 if ("${SSL_CLIENT_CERT_DIR}" STREQUAL "")
52 set(LWS_OPENSSL_CLIENT_CERTS ".")
54 set(LWS_OPENSSL_CLIENT_CERTS "${SSL_CLIENT_CERT_DIR}")
57 set(CYASSL_LIB CACHE STRING "")
58 set(CYASSL_INCLUDE_DIRS CACHE STRING "")
61 if ("${CYASSL_LIB}" STREQUAL "" OR "${CYASSL_INCLUDE_DIRS}" STREQUAL "")
62 error("You must set CYASSL_LIB and CYASSL_INCLUDE_DIRS when USE_CYASSL is turned on")
66 if (WITHOUT_EXTENSIONS)
67 set(LWS_NO_EXTENSIONS 1)
71 set(LWS_OPENSSL_SUPPORT 1)
78 if (WITHOUT_DAEMONIZE)
79 set(LWS_NO_DAEMONIZE 1)
83 set(LWS_MINGW_SUPPORT 1)
86 set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${PROJECT_SOURCE_DIR}/cmake/")
87 include_directories(${PROJECT_BINARY_DIR})
89 # Put the libaries and binaries that get built into directories at the
90 # top of the build tree rather than in hard-to-find leaf directories.
91 SET(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${PROJECT_BINARY_DIR}/bin)
92 SET(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${PROJECT_BINARY_DIR}/lib)
93 SET(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${PROJECT_BINARY_DIR}/lib)
95 # So we can include the CMake generated config file only when
96 # building with CMAKE.
97 add_definitions(-DCMAKE_BUILD)
99 include(CheckFunctionExists)
100 include(CheckIncludeFile)
101 include(CheckIncludeFiles)
102 include(CheckLibraryExists)
104 CHECK_FUNCTION_EXISTS(bzero HAVE_BZERO)
105 CHECK_FUNCTION_EXISTS(fork HAVE_FORK)
106 CHECK_FUNCTION_EXISTS(malloc HAVE_MALLOC)
107 CHECK_FUNCTION_EXISTS(memset HAVE_MEMSET)
108 CHECK_FUNCTION_EXISTS(realloc HAVE_REALLOC)
109 CHECK_FUNCTION_EXISTS(socket HAVE_SOCKET)
110 CHECK_FUNCTION_EXISTS(strerror HAVE_STRERROR)
111 CHECK_FUNCTION_EXISTS(vfork HAVE_VFORK)
112 CHECK_FUNCTION_EXISTS(getifaddrs HAVE_GETIFADDRS)
114 if (WITH_BUILTIN_GETIFADDRS)
116 warning("getifaddrs already exists on the system, are you sure you want to build using the BSD version? (This is normally only needed on systems running uclibc)")
118 set(LWS_BUILTIN_GETIFADDRS 1)
121 CHECK_INCLUDE_FILE(dlfcn.h HAVE_DLFCN_H)
122 CHECK_INCLUDE_FILE(fcntl.h HAVE_FCNTL_H)
123 CHECK_INCLUDE_FILE(inttypes.h HAVE_INTTYPES_H)
124 CHECK_INCLUDE_FILE(memory.h HAVE_MEMORY_H)
125 CHECK_INCLUDE_FILE(netinet/in.h HAVE_NETINET_IN_H)
126 CHECK_INCLUDE_FILE(stdint.h HAVE_STDINT_H)
127 CHECK_INCLUDE_FILE(stdlib.h HAVE_STDLIB_H)
128 CHECK_INCLUDE_FILE(strings.h HAVE_STRINGS_H)
129 CHECK_INCLUDE_FILE(string.h HAVE_STRING_H)
130 CHECK_INCLUDE_FILE(sys/prctl.h HAVE_SYS_PRCTL_H)
131 CHECK_INCLUDE_FILE(sys/socket.h HAVE_SYS_SOCKET_H)
132 CHECK_INCLUDE_FILE(sys/stat.h HAVE_SYS_STAT_H)
133 CHECK_INCLUDE_FILE(sys/types.h HAVE_SYS_TYPES_H)
134 CHECK_INCLUDE_FILE(unistd.h HAVE_UNISTD_H)
135 CHECK_INCLUDE_FILE(vfork.h HAVE_VFORK_H)
136 CHECK_INCLUDE_FILE(zlib.h HAVE_ZLIB_H)
138 # TODO: These can be tested if they actually work also...
139 set(HAVE_WORKING_FORK HAVE_FORK)
140 set(HAVE_WORKING_VFORK HAVE_VFORK)
142 CHECK_INCLUDE_FILES("stdlib.h;stdarg.h;string.h;float.h" STDC_HEADERS)
144 if (NOT HAVE_SYS_TYPES_H)
146 set(size_t "unsigned int")
150 set(malloc rpl_malloc)
153 if (NOT HAVE_REALLOC)
154 set(realloc rpl_realloc)
157 # Generate the config.h that includes all the compilation settings.
159 ${PROJECT_SOURCE_DIR}/config.h.cmake
160 ${PROJECT_BINARY_DIR}/lws_config.h)
165 # Turn off stupid microsoft security warnings.
166 add_definitions(-D_CRT_SECURE_NO_DEPRECATE -D_CRT_NONSTDC_NO_DEPRECATE)
169 include_directories(${PROJECT_SOURCE_DIR}/lib)
171 # Group headers and sources.
172 # Some IDEs use this for nicer file structure.
174 lib/private-libwebsockets.h
175 lib/extension-deflate-frame.h
176 lib/extension-deflate-stream.h
177 ${PROJECT_BINARY_DIR}/lws_config.h
187 lib/client-handshake.c
190 lib/extension-deflate-frame.c
191 lib/extension-deflate-stream.c
198 lib/server-handshake.c
202 # Add helper files for Windows.
204 set(WIN32_HELPERS_PATH win32port/win32helpers)
206 list(APPEND HDR_PRIVATE
207 ${WIN32_HELPERS_PATH}/websock-w32.h
208 ${WIN32_HELPERS_PATH}/gettimeofday.h
212 ${WIN32_HELPERS_PATH}/websock-w32.c
213 ${WIN32_HELPERS_PATH}/gettimeofday.c
216 include_directories(${WIN32_HELPERS_PATH})
219 if (NOT WITHOUT_DAEMONIZE)
227 if (!WITH_BUILTIN_GETIFADDRS)
228 list(APPEND HDR_PRIVATE lib/getifaddrs.h)
229 list(APPEND SOURCES lib/getifaddrs.c)
233 source_group("Headers Private" FILES ${HDR_PRIVATE})
234 source_group("Headers Public" FILES ${HDR_PUBLIC})
235 source_group("Sources" FILES ${SOURCES})
240 add_library(websocket STATIC
252 if (WIN32 AND NOT USE_EXTERNAL_ZLIB)
253 message("Using included Zlib version")
255 # Compile ZLib if needed.
256 set(WIN32_ZLIB_PATH "win32port/zlib")
258 ${WIN32_ZLIB_PATH}/adler32.c
259 ${WIN32_ZLIB_PATH}/compress.c
260 ${WIN32_ZLIB_PATH}/crc32.c
261 ${WIN32_ZLIB_PATH}/deflate.c
262 ${WIN32_ZLIB_PATH}/gzclose.c
263 ${WIN32_ZLIB_PATH}/gzio.c
264 ${WIN32_ZLIB_PATH}/gzlib.c
265 ${WIN32_ZLIB_PATH}/gzread.c
266 ${WIN32_ZLIB_PATH}/gzwrite.c
267 ${WIN32_ZLIB_PATH}/infback.c
268 ${WIN32_ZLIB_PATH}/inffast.c
269 ${WIN32_ZLIB_PATH}/inflate.c
270 ${WIN32_ZLIB_PATH}/inftrees.c
271 ${WIN32_ZLIB_PATH}/trees.c
272 ${WIN32_ZLIB_PATH}/uncompr.c
273 ${WIN32_ZLIB_PATH}/zutil.c
276 # Create the library.
277 add_library(ZLIB STATIC ${ZLIB_SRCS})
279 # Set the same variables as find_package would.
280 set(ZLIB_INCLUDE_DIRS ${WIN32_ZLIB_PATH})
281 get_property(ZLIB_LIBRARIES TARGET zlib PROPERTY LOCATION)
284 find_package(ZLIB REQUIRED)
287 message("ZLib include dirs: ${ZLIB_INCLUDE_DIRS}")
288 message("ZLib libraries: ${ZLIB_LIBRARIES}")
289 include_directories(${ZLIB_INCLUDE_DIRS})
290 target_link_libraries(websocket ${ZLIB_LIBRARIES})
296 message("Compiling with SSL support")
299 # Use CyaSSL as OpenSSL replacement.
300 set(OPENSSL_LIBRARIES ${CYASSL_LIB})
301 set(OPENSSL_INCLUDE_DIRS ${CYASSL_INCLUDE_DIRS})
304 # TODO: Add support for STATIC also.
305 find_package(OpenSSL REQUIRED)
307 # TODO: Find OpenSSL binaries and copy them to the output directories for the test-apps so that they can be run out of the box.
310 message("OpenSSL include dir: ${OPENSSL_INCLUDE_DIR}")
311 message("OpenSSL libraries: ${OPENSSL_LIBRARIES}")
312 include_directories(${OPENSSL_INCLUDE_DIR})
313 target_link_libraries(websocket ${OPENSSL_LIBRARIES})
317 # Platform specific libs.
320 target_link_libraries(websocket ws2_32.lib)
324 target_link_libraries(websocket m)
330 if (NOT WITHOUT_TESTAPPS)
332 # Helper function for adding a test app.
334 function(create_test_app TEST_NAME MAIN_SRC WIN32_SRCS WIN32_HDRS)
336 set(TEST_SRCS ${MAIN_SRC})
340 list(APPEND TEST_SRCS
341 ${WIN32_HELPERS_PATH}/getopt.c
342 ${WIN32_HELPERS_PATH}/getopt_long.c
343 ${WIN32_HELPERS_PATH}/gettimeofday.c
347 ${WIN32_HELPERS_PATH}/getopt.h
348 ${WIN32_HELPERS_PATH}/gettimeofday.h
352 source_group("Headers" FILES ${TEST_HDR})
353 source_group("Sources" FILES ${TEST_SRCS})
354 add_executable(${TEST_NAME} ${TEST_SRCS} ${TEST_HDR})
355 target_link_libraries(${TEST_NAME} websocket)
359 PROPERTY COMPILE_DEFINITIONS INSTALL_DATADIR="${SSL_CERT_DIR}"
368 if (NOT WITHOUT_CLIENT)
369 create_test_app(test-client
370 "test-server/test-client.c"
373 endif(NOT WITHOUT_CLIENT)
378 if (NOT WITHOUT_SERVER)
379 create_test_app(test-server
380 "test-server/test-server.c"
382 "${WIN32_HELPERS_PATH}/netdb.h;${WIN32_HELPERS_PATH}/strings.h;${WIN32_HELPERS_PATH}/unistd.h;${WIN32_HELPERS_PATH}/websock-w32.h")
383 endif(NOT WITHOUT_SERVER)
388 if (NOT WITHOUT_FRAGGLE)
389 create_test_app(test-fraggle
390 "test-server/test-fraggle.c"
392 "${WIN32_HELPERS_PATH}/unistd.h;${WIN32_HELPERS_PATH}/sys/time.h")
393 endif(NOT WITHOUT_FRAGGLE)
398 if (NOT WITHOUT_PING)
399 create_test_app(test-ping
400 "test-server/test-ping.c"
402 "${WIN32_HELPERS_PATH}/unistd.h;${WIN32_HELPERS_PATH}/sys/time.h")
403 endif(NOT WITHOUT_PING)
406 # Copy OpenSSL dlls to the output directory on Windows.
407 # (Otherwise we'll get an error when trying to run)
409 if (WIN32 AND WITH_SSL AND NOT USE_CYASSL)
411 message("Searching for OpenSSL dlls")
412 find_package(OpenSSLbins)
414 if(OPENSSL_BIN_FOUND)
415 message("OpenSSL dlls found, copying to output directory")
416 message("Libeay: ${LIBEAY_BIN}")
417 message("SSLeay: ${SSLEAY_BIN}")
426 add_custom_command(TARGET ${TARGET_BIN}
428 COMMAND ${CMAKE_COMMAND} -E copy ${LIBEAY_BIN} $<TARGET_FILE_DIR:${TARGET_BIN}> VERBATIM)
430 add_custom_command(TARGET ${TARGET_BIN}
432 COMMAND ${CMAKE_COMMAND} -E copy ${SSLEAY_BIN} $<TARGET_FILE_DIR:${TARGET_BIN}> VERBATIM)
436 endif(NOT WITHOUT_TESTAPPS)