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. When settings this, you also need to specify CYASSL_LIB and CYASSL_INCLUDE_DIRS" 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(WITHOUT_SERVER_EXTPOLL "Don't build a server version that uses external poll" OFF)
37 option(WITH_LIBCRYPTO "Use libcrypto MD5 and SHA1 implementations" ON)
38 option(WITHOUT_PING "Don't build the ping test application" OFF)
39 option(WITHOUT_DEBUG "Don't compile debug related code" OFF)
40 option(WITHOUT_EXTENSIONS "Don't compile with extensions" OFF)
41 option(WITH_LATENCY "Build latency measuring code into the library" OFF)
42 option(WITHOUT_DAEMONIZE "Don't build the daemonization api" OFF)
44 # The base dir where the SSL dirs should be looked for.
45 set(SSL_CERT_DIR CACHE STRING "")
46 set(SSL_CLIENT_CERT_DIR CACHE STRING "")
48 if ("${SSL_CERT_DIR}" STREQUAL "")
52 if ("${SSL_CLIENT_CERT_DIR}" STREQUAL "")
53 set(LWS_OPENSSL_CLIENT_CERTS ".")
55 set(LWS_OPENSSL_CLIENT_CERTS "${SSL_CLIENT_CERT_DIR}")
58 set(CYASSL_LIB CACHE STRING "")
59 set(CYASSL_INCLUDE_DIRS CACHE STRING "")
62 if ("${CYASSL_LIB}" STREQUAL "" OR "${CYASSL_INCLUDE_DIRS}" STREQUAL "")
63 message(FATAL_ERROR "You must set CYASSL_LIB and CYASSL_INCLUDE_DIRS when USE_CYASSL is turned on")
67 if (WITHOUT_EXTENSIONS)
68 set(LWS_NO_EXTENSIONS 1)
72 set(LWS_OPENSSL_SUPPORT 1)
79 if (WITHOUT_DAEMONIZE)
80 set(LWS_NO_DAEMONIZE 1)
92 set(LWS_MINGW_SUPPORT 1)
95 set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${PROJECT_SOURCE_DIR}/cmake/")
96 include_directories(${PROJECT_BINARY_DIR})
98 include(CheckCSourceCompiles)
100 # Check for different inline keyword versions.
101 foreach(KEYWORD "inline" "__inline__" "__inline")
102 set(CMAKE_REQUIRED_DEFINITIONS "-DKEYWORD=${KEYWORD}")
103 CHECK_C_SOURCE_COMPILES(
107 int main(int argc, char **argv) { a(); return 0; }
113 set(inline __inline__)
114 elseif(HAVE___inline)
119 # Put the libaries and binaries that get built into directories at the
120 # top of the build tree rather than in hard-to-find leaf directories.
121 SET(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${PROJECT_BINARY_DIR}/bin)
122 SET(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${PROJECT_BINARY_DIR}/lib)
123 SET(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${PROJECT_BINARY_DIR}/lib)
125 # So we can include the CMake generated config file only when
126 # building with CMAKE.
127 add_definitions(-DCMAKE_BUILD)
129 include(CheckFunctionExists)
130 include(CheckIncludeFile)
131 include(CheckIncludeFiles)
132 include(CheckLibraryExists)
134 CHECK_FUNCTION_EXISTS(bzero HAVE_BZERO)
135 CHECK_FUNCTION_EXISTS(fork HAVE_FORK)
136 CHECK_FUNCTION_EXISTS(malloc HAVE_MALLOC)
137 CHECK_FUNCTION_EXISTS(memset HAVE_MEMSET)
138 CHECK_FUNCTION_EXISTS(realloc HAVE_REALLOC)
139 CHECK_FUNCTION_EXISTS(socket HAVE_SOCKET)
140 CHECK_FUNCTION_EXISTS(strerror HAVE_STRERROR)
141 CHECK_FUNCTION_EXISTS(vfork HAVE_VFORK)
142 CHECK_FUNCTION_EXISTS(getifaddrs HAVE_GETIFADDRS)
144 if (WITH_BUILTIN_GETIFADDRS)
146 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)")
148 set(LWS_BUILTIN_GETIFADDRS 1)
151 CHECK_INCLUDE_FILE(dlfcn.h HAVE_DLFCN_H)
152 CHECK_INCLUDE_FILE(fcntl.h HAVE_FCNTL_H)
153 CHECK_INCLUDE_FILE(inttypes.h HAVE_INTTYPES_H)
154 CHECK_INCLUDE_FILE(memory.h HAVE_MEMORY_H)
155 CHECK_INCLUDE_FILE(netinet/in.h HAVE_NETINET_IN_H)
156 CHECK_INCLUDE_FILE(stdint.h HAVE_STDINT_H)
157 CHECK_INCLUDE_FILE(stdlib.h HAVE_STDLIB_H)
158 CHECK_INCLUDE_FILE(strings.h HAVE_STRINGS_H)
159 CHECK_INCLUDE_FILE(string.h HAVE_STRING_H)
160 CHECK_INCLUDE_FILE(sys/prctl.h HAVE_SYS_PRCTL_H)
161 CHECK_INCLUDE_FILE(sys/socket.h HAVE_SYS_SOCKET_H)
162 CHECK_INCLUDE_FILE(sys/stat.h HAVE_SYS_STAT_H)
163 CHECK_INCLUDE_FILE(sys/types.h HAVE_SYS_TYPES_H)
164 CHECK_INCLUDE_FILE(unistd.h HAVE_UNISTD_H)
165 CHECK_INCLUDE_FILE(vfork.h HAVE_VFORK_H)
166 CHECK_INCLUDE_FILE(zlib.h HAVE_ZLIB_H)
168 # TODO: These can be tested if they actually work also...
169 set(HAVE_WORKING_FORK HAVE_FORK)
170 set(HAVE_WORKING_VFORK HAVE_VFORK)
172 CHECK_INCLUDE_FILES("stdlib.h;stdarg.h;string.h;float.h" STDC_HEADERS)
174 if (NOT HAVE_SYS_TYPES_H)
176 set(size_t "unsigned int")
180 set(malloc rpl_malloc)
183 if (NOT HAVE_REALLOC)
184 set(realloc rpl_realloc)
187 # Generate the config.h that includes all the compilation settings.
189 ${PROJECT_SOURCE_DIR}/config.h.cmake
190 ${PROJECT_BINARY_DIR}/lws_config.h)
195 # Turn off stupid microsoft security warnings.
196 add_definitions(-D_CRT_SECURE_NO_DEPRECATE -D_CRT_NONSTDC_NO_DEPRECATE)
199 include_directories(${PROJECT_SOURCE_DIR}/lib)
201 # Group headers and sources.
202 # Some IDEs use this for nicer file structure.
204 lib/private-libwebsockets.h
205 lib/extension-deflate-frame.h
206 lib/extension-deflate-stream.h
207 ${PROJECT_BINARY_DIR}/lws_config.h
217 lib/client-handshake.c
220 lib/extension-deflate-frame.c
221 lib/extension-deflate-stream.c
228 lib/server-handshake.c
232 # Add helper files for Windows.
234 set(WIN32_HELPERS_PATH win32port/win32helpers)
236 list(APPEND HDR_PRIVATE
237 ${WIN32_HELPERS_PATH}/websock-w32.h
238 ${WIN32_HELPERS_PATH}/gettimeofday.h
242 ${WIN32_HELPERS_PATH}/websock-w32.c
243 ${WIN32_HELPERS_PATH}/gettimeofday.c
246 include_directories(${WIN32_HELPERS_PATH})
249 if (NOT WITHOUT_DAEMONIZE)
257 if (!WITH_BUILTIN_GETIFADDRS)
258 list(APPEND HDR_PRIVATE lib/getifaddrs.h)
259 list(APPEND SOURCES lib/getifaddrs.c)
263 source_group("Headers Private" FILES ${HDR_PRIVATE})
264 source_group("Headers Public" FILES ${HDR_PUBLIC})
265 source_group("Sources" FILES ${SOURCES})
270 add_library(websockets STATIC
282 if (WIN32 AND NOT USE_EXTERNAL_ZLIB)
283 message("Using included Zlib version")
285 # Compile ZLib if needed.
286 set(WIN32_ZLIB_PATH "win32port/zlib")
288 ${WIN32_ZLIB_PATH}/adler32.c
289 ${WIN32_ZLIB_PATH}/compress.c
290 ${WIN32_ZLIB_PATH}/crc32.c
291 ${WIN32_ZLIB_PATH}/deflate.c
292 ${WIN32_ZLIB_PATH}/gzclose.c
293 ${WIN32_ZLIB_PATH}/gzio.c
294 ${WIN32_ZLIB_PATH}/gzlib.c
295 ${WIN32_ZLIB_PATH}/gzread.c
296 ${WIN32_ZLIB_PATH}/gzwrite.c
297 ${WIN32_ZLIB_PATH}/infback.c
298 ${WIN32_ZLIB_PATH}/inffast.c
299 ${WIN32_ZLIB_PATH}/inflate.c
300 ${WIN32_ZLIB_PATH}/inftrees.c
301 ${WIN32_ZLIB_PATH}/trees.c
302 ${WIN32_ZLIB_PATH}/uncompr.c
303 ${WIN32_ZLIB_PATH}/zutil.c
306 # Create the library.
307 add_library(ZLIB STATIC ${ZLIB_SRCS})
309 # Set the same variables as find_package would.
310 set(ZLIB_INCLUDE_DIRS ${WIN32_ZLIB_PATH})
311 get_property(ZLIB_LIBRARIES TARGET ZLIB PROPERTY LOCATION)
314 find_package(ZLIB REQUIRED)
317 message("ZLib include dirs: ${ZLIB_INCLUDE_DIRS}")
318 message("ZLib libraries: ${ZLIB_LIBRARIES}")
319 include_directories(${ZLIB_INCLUDE_DIRS})
320 target_link_libraries(websockets ${ZLIB_LIBRARIES})
326 message("Compiling with SSL support")
329 # Use CyaSSL as OpenSSL replacement.
330 # TODO: Add a find_package command for this also.
331 message("CyaSSL include dir: ${CYASSL_INCLUDE_DIRS}")
332 message("CyaSSL libraries: ${CYASSL_LIB}")
334 # Additional to the root directory we need to include
335 # the cyassl/ subdirectory which contains the OpenSSL
336 # compatability layer headers.
337 foreach(inc ${CYASSL_INCLUDE_DIRS})
338 include_directories(${inc} ${inc}/cyassl)
341 target_link_libraries(websockets ${CYASSL_LIB})
343 # TODO: Add support for STATIC also.
344 find_package(OpenSSL REQUIRED)
346 message("OpenSSL include dir: ${OPENSSL_INCLUDE_DIR}")
347 message("OpenSSL libraries: ${OPENSSL_LIBRARIES}")
349 include_directories(${OPENSSL_INCLUDE_DIR})
350 target_link_libraries(websockets ${OPENSSL_LIBRARIES})
355 # Platform specific libs.
358 target_link_libraries(websockets ws2_32.lib)
362 target_link_libraries(websockets m)
368 if (NOT WITHOUT_TESTAPPS)
370 # Helper function for adding a test app.
372 function(create_test_app TEST_NAME MAIN_SRC WIN32_SRCS WIN32_HDRS)
374 set(TEST_SRCS ${MAIN_SRC})
378 list(APPEND TEST_SRCS
379 ${WIN32_HELPERS_PATH}/getopt.c
380 ${WIN32_HELPERS_PATH}/getopt_long.c
381 ${WIN32_HELPERS_PATH}/gettimeofday.c
385 ${WIN32_HELPERS_PATH}/getopt.h
386 ${WIN32_HELPERS_PATH}/gettimeofday.h
390 source_group("Headers" FILES ${TEST_HDR})
391 source_group("Sources" FILES ${TEST_SRCS})
392 add_executable(${TEST_NAME} ${TEST_SRCS} ${TEST_HDR})
393 target_link_libraries(${TEST_NAME} websockets)
397 PROPERTY COMPILE_DEFINITIONS INSTALL_DATADIR="${SSL_CERT_DIR}"
404 if (NOT WITHOUT_CLIENT)
405 create_test_app(test-client
406 "test-server/test-client.c"
414 if (NOT WITHOUT_SERVER)
415 create_test_app(test-server
416 "test-server/test-server.c"
418 "${WIN32_HELPERS_PATH}/netdb.h;${WIN32_HELPERS_PATH}/strings.h;${WIN32_HELPERS_PATH}/unistd.h;${WIN32_HELPERS_PATH}/websock-w32.h")
422 # test-server-extpoll
424 if (NOT WITHOUT_SERVER AND NOT WITHOUT_SERVER_EXTPOLL)
425 create_test_app(test-server-extpoll
426 "test-server/test-server.c"
428 "${WIN32_HELPERS_PATH}/netdb.h;${WIN32_HELPERS_PATH}/strings.h;${WIN32_HELPERS_PATH}/unistd.h;${WIN32_HELPERS_PATH}/websock-w32.h")
430 # Set defines for this executable only.
432 TARGET test-server-extpoll
433 PROPERTY COMPILE_DEFINITIONS EXTERNAL_POLL INSTALL_DATADIR="${SSL_CERT_DIR}"
440 if (NOT WITHOUT_FRAGGLE)
441 create_test_app(test-fraggle
442 "test-server/test-fraggle.c"
444 "${WIN32_HELPERS_PATH}/unistd.h;${WIN32_HELPERS_PATH}/sys/time.h")
450 if (NOT WITHOUT_PING)
451 create_test_app(test-ping
452 "test-server/test-ping.c"
454 "${WIN32_HELPERS_PATH}/unistd.h;${WIN32_HELPERS_PATH}/sys/time.h")
458 # Copy OpenSSL dlls to the output directory on Windows.
459 # (Otherwise we'll get an error when trying to run)
461 if (WIN32 AND WITH_SSL AND NOT USE_CYASSL)
463 message("Searching for OpenSSL dlls")
464 find_package(OpenSSLbins)
466 if(OPENSSL_BIN_FOUND)
467 message("OpenSSL dlls found, copying to output directory")
468 message("Libeay: ${LIBEAY_BIN}")
469 message("SSLeay: ${SSLEAY_BIN}")
478 add_custom_command(TARGET ${TARGET_BIN}
480 COMMAND ${CMAKE_COMMAND} -E copy ${LIBEAY_BIN} $<TARGET_FILE_DIR:${TARGET_BIN}> VERBATIM)
482 add_custom_command(TARGET ${TARGET_BIN}
484 COMMAND ${CMAKE_COMMAND} -E copy ${SSLEAY_BIN} $<TARGET_FILE_DIR:${TARGET_BIN}> VERBATIM)
488 endif(NOT WITHOUT_TESTAPPS)