1 cmake_minimum_required(VERSION 2.6)
5 set(PACKAGE "libwebsockets")
6 set(PACKAGE_VERSION "1.2")
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}")
13 set(SOVERSION "3.0.0")
15 set(LWS_LIBRARY_VERSION ${PACKAGE_VERSION})
16 set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${PROJECT_SOURCE_DIR}/cmake/")
18 # Try to find the current Git hash.
22 COMMAND "${GIT_EXECUTABLE}" log -n 1 --pretty=%h
23 OUTPUT_VARIABLE GIT_HASH
24 OUTPUT_STRIP_TRAILING_WHITESPACE
27 set(LWS_BUILD_HASH ${GIT_HASH})
28 message("Git commit hash: ${LWS_BUILD_HASH}")
31 option(WITH_SSL "Include SSL support (default OpenSSL, CyaSSL if USE_CYASSL is set)" ON)
32 option(USE_EXTERNAL_ZLIB "Search the system for ZLib instead of using the included one (on Windows)" OFF)
33 option(USE_CYASSL "Use CyaSSL replacement for OpenSSL. When settings this, you also need to specify CYASSL_LIB and CYASSL_INCLUDE_DIRS" OFF)
34 option(WITHOUT_BUILTIN_GETIFADDRS "Don't use BSD getifaddrs implementation from libwebsockets if it is missing (this will result in a compilation error) ... Default is your libc provides it. On some systems such as uclibc it doesn't exist." OFF)
35 option(WITHOUT_CLIENT "Don't build the client part of the library" OFF)
36 option(WITHOUT_SERVER "Don't build the server part of the library" OFF)
37 #option(WITH_LIBCRYPTO "Use libcrypto MD5 and SHA1 implementations" ON)
38 option(WITHOUT_TESTAPPS "Don't build the libwebsocket-test-apps" OFF)
39 option(WITHOUT_TEST_SERVER "Don't build the test server" OFF)
40 option(WITHOUT_TEST_SERVER_EXTPOLL "Don't build the test server version that uses external poll" OFF)
41 option(WITHOUT_TEST_PING "Don't build the ping test application" OFF)
42 option(WITHOUT_TEST_CLIENT "Don't build the client test application" OFF)
43 option(WITHOUT_TEST_FRAGGLE "Don't build the ping test application" OFF)
44 option(WITHOUT_DEBUG "Don't compile debug related code" OFF)
45 option(WITHOUT_EXTENSIONS "Don't compile with extensions" OFF)
46 option(WITH_LATENCY "Build latency measuring code into the library" OFF)
47 option(WITHOUT_DAEMONIZE "Don't build the daemonization api" OFF)
49 if (WITHOUT_CLIENT AND WITHOUT_SERVER)
50 message(FATAL_ERROR "Makes no sense to compile without both client or server.")
53 # The base dir where the SSL dirs should be looked for.
54 set(SSL_CERT_DIR CACHE STRING "")
55 set(SSL_CLIENT_CERT_DIR CACHE STRING "")
57 if ("${SSL_CERT_DIR}" STREQUAL "")
58 set(SSL_CERT_DIR "$ENV{DESTDIR}/usr/local/share")
61 if ("${SSL_CLIENT_CERT_DIR}" STREQUAL "")
63 set(LWS_OPENSSL_CLIENT_CERTS ".")
65 set(LWS_OPENSSL_CLIENT_CERTS "/etc/pki/tls/certs/")
68 set(LWS_OPENSSL_CLIENT_CERTS "${SSL_CLIENT_CERT_DIR}")
71 set(CYASSL_LIB CACHE STRING "")
72 set(CYASSL_INCLUDE_DIRS CACHE STRING "")
75 if ("${CYASSL_LIB}" STREQUAL "" OR "${CYASSL_INCLUDE_DIRS}" STREQUAL "")
76 message(FATAL_ERROR "You must set CYASSL_LIB and CYASSL_INCLUDE_DIRS when USE_CYASSL is turned on")
80 if (WITHOUT_EXTENSIONS)
81 set(LWS_NO_EXTENSIONS 1)
85 set(LWS_OPENSSL_SUPPORT 1)
92 if (WITHOUT_DAEMONIZE)
93 set(LWS_NO_DAEMONIZE 1)
105 set(LWS_MINGW_SUPPORT 1)
108 include_directories(${PROJECT_BINARY_DIR})
110 include(CheckCSourceCompiles)
112 # Check for different inline keyword versions.
113 foreach(KEYWORD "inline" "__inline__" "__inline")
114 set(CMAKE_REQUIRED_DEFINITIONS "-DKEYWORD=${KEYWORD}")
115 CHECK_C_SOURCE_COMPILES(
119 int main(int argc, char **argv) { a(); return 0; }
125 set(inline __inline__)
126 elseif(HAVE___inline)
131 # Put the libaries and binaries that get built into directories at the
132 # top of the build tree rather than in hard-to-find leaf directories.
133 SET(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${PROJECT_BINARY_DIR}/bin)
134 SET(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${PROJECT_BINARY_DIR}/lib)
135 SET(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${PROJECT_BINARY_DIR}/lib)
137 # So we can include the CMake generated config file only when
138 # building with CMAKE.
139 add_definitions(-DCMAKE_BUILD)
141 include(CheckFunctionExists)
142 include(CheckIncludeFile)
143 include(CheckIncludeFiles)
144 include(CheckLibraryExists)
146 CHECK_FUNCTION_EXISTS(bzero HAVE_BZERO)
147 CHECK_FUNCTION_EXISTS(fork HAVE_FORK)
148 CHECK_FUNCTION_EXISTS(malloc HAVE_MALLOC)
149 CHECK_FUNCTION_EXISTS(memset HAVE_MEMSET)
150 CHECK_FUNCTION_EXISTS(realloc HAVE_REALLOC)
151 CHECK_FUNCTION_EXISTS(socket HAVE_SOCKET)
152 CHECK_FUNCTION_EXISTS(strerror HAVE_STRERROR)
153 CHECK_FUNCTION_EXISTS(vfork HAVE_VFORK)
154 CHECK_FUNCTION_EXISTS(getifaddrs HAVE_GETIFADDRS)
156 if (NOT HAVE_GETIFADDRS)
157 if (WITHOUT_BUILTIN_GETIFADDRS)
158 message(FATAL_ERROR "No getifaddrs was found on the system. Turn off the WITHOUT_BUILTIN_GETIFADDRS compile option to use the supplied BSD version.")
161 set(LWS_BUILTIN_GETIFADDRS 1)
164 CHECK_INCLUDE_FILE(dlfcn.h HAVE_DLFCN_H)
165 CHECK_INCLUDE_FILE(fcntl.h HAVE_FCNTL_H)
166 CHECK_INCLUDE_FILE(inttypes.h HAVE_INTTYPES_H)
167 CHECK_INCLUDE_FILE(memory.h HAVE_MEMORY_H)
168 CHECK_INCLUDE_FILE(netinet/in.h HAVE_NETINET_IN_H)
169 CHECK_INCLUDE_FILE(stdint.h HAVE_STDINT_H)
170 CHECK_INCLUDE_FILE(stdlib.h HAVE_STDLIB_H)
171 CHECK_INCLUDE_FILE(strings.h HAVE_STRINGS_H)
172 CHECK_INCLUDE_FILE(string.h HAVE_STRING_H)
173 CHECK_INCLUDE_FILE(sys/prctl.h HAVE_SYS_PRCTL_H)
174 CHECK_INCLUDE_FILE(sys/socket.h HAVE_SYS_SOCKET_H)
175 CHECK_INCLUDE_FILE(sys/stat.h HAVE_SYS_STAT_H)
176 CHECK_INCLUDE_FILE(sys/types.h HAVE_SYS_TYPES_H)
177 CHECK_INCLUDE_FILE(unistd.h HAVE_UNISTD_H)
178 CHECK_INCLUDE_FILE(vfork.h HAVE_VFORK_H)
179 CHECK_INCLUDE_FILE(zlib.h HAVE_ZLIB_H)
181 # TODO: These can be tested if they actually work also...
182 set(HAVE_WORKING_FORK HAVE_FORK)
183 set(HAVE_WORKING_VFORK HAVE_VFORK)
185 CHECK_INCLUDE_FILES("stdlib.h;stdarg.h;string.h;float.h" STDC_HEADERS)
187 if (NOT HAVE_SYS_TYPES_H)
189 set(size_t "unsigned int")
193 set(malloc rpl_malloc)
196 if (NOT HAVE_REALLOC)
197 set(realloc rpl_realloc)
200 # Generate the config.h that includes all the compilation settings.
202 ${PROJECT_SOURCE_DIR}/config.h.cmake
203 ${PROJECT_BINARY_DIR}/lws_config.h)
206 # Turn off stupid microsoft security warnings.
207 add_definitions(-D_CRT_SECURE_NO_DEPRECATE -D_CRT_NONSTDC_NO_DEPRECATE)
210 include_directories(${PROJECT_SOURCE_DIR}/lib)
212 # Group headers and sources.
213 # Some IDEs use this for nicer file structure.
215 lib/private-libwebsockets.h
216 ${PROJECT_BINARY_DIR}/lws_config.h
220 ${PROJECT_SOURCE_DIR}/lib/libwebsockets.h
232 if (NOT WITHOUT_CLIENT)
235 lib/client-handshake.c
240 if (NOT WITHOUT_SERVER)
243 lib/server-handshake.c
247 if (NOT WITHOUT_EXTENSIONS)
248 list(APPEND HDR_PRIVATE
249 lib/extension-deflate-frame.h
250 lib/extension-deflate-stream.h
255 lib/extension-deflate-frame.c
256 lib/extension-deflate-stream.c
260 # Add helper files for Windows.
262 set(WIN32_HELPERS_PATH win32port/win32helpers)
264 list(APPEND HDR_PRIVATE
265 ${WIN32_HELPERS_PATH}/websock-w32.h
266 ${WIN32_HELPERS_PATH}/gettimeofday.h
270 ${WIN32_HELPERS_PATH}/websock-w32.c
271 ${WIN32_HELPERS_PATH}/gettimeofday.c
274 include_directories(${WIN32_HELPERS_PATH})
277 if (NOT WITHOUT_DAEMONIZE)
285 if (NOT HAVE_GETIFADDRS)
286 list(APPEND HDR_PRIVATE lib/getifaddrs.h)
287 list(APPEND SOURCES lib/getifaddrs.c)
291 source_group("Headers Private" FILES ${HDR_PRIVATE})
292 source_group("Headers Public" FILES ${HDR_PUBLIC})
293 source_group("Sources" FILES ${SOURCES})
298 add_library(websockets STATIC
302 add_library(websockets_shared SHARED
308 # On Windows libs have the same file ending (.lib)
309 # for both static and shared libraries, so we
310 # need a unique name for the static one.
311 set_target_properties(websockets
313 OUTPUT_NAME websockets_static)
315 # Compile as DLL (export function declarations)
317 TARGET websockets_shared
318 PROPERTY COMPILE_DEFINITIONS
324 # We want the shared lib to be named "libwebsockets"
325 # not "libwebsocket_shared".
326 set_target_properties(websockets_shared
328 OUTPUT_NAME websockets)
330 # Set the so version of the lib.
331 # Equivalent to LDFLAGS=-version-info 3:0:0
332 if(CMAKE_COMPILER_IS_GNUCC OR CMAKE_COMPILER_IS_GNUCXX)
333 foreach(lib websockets websockets_shared)
334 set_target_properties(${lib}
336 SOVERSION ${SOVERSION})
347 # ZLIB (Only needed for deflate extensions).
349 if (NOT WITHOUT_EXTENSIONS)
350 if (WIN32 AND NOT USE_EXTERNAL_ZLIB)
351 message("Using included Zlib version")
353 # Compile ZLib if needed.
354 set(WIN32_ZLIB_PATH "win32port/zlib")
356 ${WIN32_ZLIB_PATH}/adler32.c
357 ${WIN32_ZLIB_PATH}/compress.c
358 ${WIN32_ZLIB_PATH}/crc32.c
359 ${WIN32_ZLIB_PATH}/deflate.c
360 ${WIN32_ZLIB_PATH}/gzclose.c
361 ${WIN32_ZLIB_PATH}/gzio.c
362 ${WIN32_ZLIB_PATH}/gzlib.c
363 ${WIN32_ZLIB_PATH}/gzread.c
364 ${WIN32_ZLIB_PATH}/gzwrite.c
365 ${WIN32_ZLIB_PATH}/infback.c
366 ${WIN32_ZLIB_PATH}/inffast.c
367 ${WIN32_ZLIB_PATH}/inflate.c
368 ${WIN32_ZLIB_PATH}/inftrees.c
369 ${WIN32_ZLIB_PATH}/trees.c
370 ${WIN32_ZLIB_PATH}/uncompr.c
371 ${WIN32_ZLIB_PATH}/zutil.c
374 # Create the library.
375 add_library(ZLIB STATIC ${ZLIB_SRCS})
377 # Set the same variables as find_package would.
378 set(ZLIB_INCLUDE_DIRS ${WIN32_ZLIB_PATH})
379 get_property(ZLIB_LIBRARIES TARGET ZLIB PROPERTY LOCATION)
382 find_package(ZLIB REQUIRED)
385 # Make sure ZLib is compiled before the libs.
386 foreach (lib websockets websockets_shared)
387 add_dependencies(${lib} ZLIB)
390 message("ZLib include dirs: ${ZLIB_INCLUDE_DIRS}")
391 message("ZLib libraries: ${ZLIB_LIBRARIES}")
392 include_directories(${ZLIB_INCLUDE_DIRS})
393 list(APPEND LIB_LIST ${ZLIB_LIBRARIES})
394 endif(NOT WITHOUT_EXTENSIONS)
400 message("Compiling with SSL support")
403 # Use CyaSSL as OpenSSL replacement.
404 # TODO: Add a find_package command for this also.
405 message("CyaSSL include dir: ${CYASSL_INCLUDE_DIRS}")
406 message("CyaSSL libraries: ${CYASSL_LIB}")
408 # Additional to the root directory we need to include
409 # the cyassl/ subdirectory which contains the OpenSSL
410 # compatability layer headers.
411 foreach(inc ${CYASSL_INCLUDE_DIRS})
412 include_directories(${inc} ${inc}/cyassl)
415 list(APPEND ${LIB_LIST} ${CYASSL_LIB})
417 # TODO: Add support for STATIC also.
418 find_package(OpenSSL REQUIRED)
420 message("OpenSSL include dir: ${OPENSSL_INCLUDE_DIR}")
421 message("OpenSSL libraries: ${OPENSSL_LIBRARIES}")
423 include_directories(${OPENSSL_INCLUDE_DIR})
424 list(APPEND LIB_LIST ${OPENSSL_LIBRARIES})
429 # Platform specific libs.
432 list(APPEND LIB_LIST ws2_32.lib)
436 list(APPEND LIB_LIST m)
439 # Setup the linking for all libs.
440 foreach (lib websockets websockets_shared)
441 target_link_libraries(${lib} ${LIB_LIST})
448 if (NOT WITHOUT_TESTAPPS)
450 # Helper function for adding a test app.
452 function(create_test_app TEST_NAME MAIN_SRC WIN32_SRCS WIN32_HDRS)
454 set(TEST_SRCS ${MAIN_SRC})
458 list(APPEND TEST_SRCS
459 ${WIN32_HELPERS_PATH}/getopt.c
460 ${WIN32_HELPERS_PATH}/getopt_long.c
461 ${WIN32_HELPERS_PATH}/gettimeofday.c
465 ${WIN32_HELPERS_PATH}/getopt.h
466 ${WIN32_HELPERS_PATH}/gettimeofday.h
470 source_group("Headers Private" FILES ${TEST_HDR})
471 source_group("Sources" FILES ${TEST_SRCS})
472 add_executable(${TEST_NAME} ${TEST_SRCS} ${TEST_HDR})
473 target_link_libraries(${TEST_NAME} websockets)
474 add_dependencies(${TEST_NAME} websockets)
476 # Set test app specific defines.
477 set_property(TARGET ${TEST_NAME}
478 PROPERTY COMPILE_DEFINITIONS
479 INSTALL_DATADIR="${SSL_CERT_DIR}"
482 # Prefix the binary names with libwebsockets.
483 set_target_properties(${TEST_NAME}
485 OUTPUT_NAME libwebsockets-${TEST_NAME})
487 # Add to the list of tests.
488 list(APPEND TEST_APP_LIST ${TEST_NAME})
491 if (WITH_SSL AND NOT USE_CYASSL)
492 message("Searching for OpenSSL executable and dlls")
493 find_package(OpenSSLbins)
494 message("OpenSSL executable: ${OPENSSL_EXECUTABLE}")
497 if (NOT WITHOUT_SERVER)
501 if (NOT WITHOUT_TEST_SERVER)
502 create_test_app(test-server
503 "test-server/test-server.c"
505 "${WIN32_HELPERS_PATH}/netdb.h;${WIN32_HELPERS_PATH}/strings.h;${WIN32_HELPERS_PATH}/unistd.h;${WIN32_HELPERS_PATH}/websock-w32.h")
509 # test-server-extpoll
511 if (NOT WITHOUT_TEST_SERVER_EXTPOLL)
512 create_test_app(test-server-extpoll
513 "test-server/test-server.c"
514 "win32port/win32helpers/websock-w32.c"
515 "${WIN32_HELPERS_PATH}/netdb.h;${WIN32_HELPERS_PATH}/strings.h;${WIN32_HELPERS_PATH}/unistd.h;${WIN32_HELPERS_PATH}/websock-w32.h")
516 # Set defines for this executable only.
518 TARGET test-server-extpoll
519 PROPERTY COMPILE_DEFINITIONS
521 INSTALL_DATADIR="${SSL_CERT_DIR}"
524 # We need to link against winsock code.
526 target_link_libraries(test-server-extpoll ws2_32.lib)
530 # Data files for running the test server.
532 test-server/favicon.ico
534 test-server/libwebsockets.org-logo.png
535 test-server/test.html)
537 # Generate self-signed SSL certs for the test-server.
538 if (WITH_SSL AND OPENSSL_EXECUTABLE)
540 set(TEST_SERVER_SSL_KEY ${PROJECT_BINARY_DIR}/libwebsockets-test-server.key.pem)
541 set(TEST_SERVER_SSL_CERT ${PROJECT_BINARY_DIR}/libwebsockets-test-server.pem)
544 file(WRITE ${PROJECT_BINARY_DIR}/openssl_input.txt
548 "libwebsockets-test\n"
554 COMMAND type ${PROJECT_BINARY_DIR}/openssl_input.txt
555 COMMAND ${OPENSSL_EXECUTABLE} req -new -newkey rsa:1024 -days 10000 -nodes -x509 -keyout ${TEST_SERVER_SSL_KEY} -out ${TEST_SERVER_SSL_CERT}
559 COMMAND printf "GB\\nErewhon\\nAll around\\nlibwebsockets-test\\n\\nlocalhost\\nnone@invalid.org\\n"
560 COMMAND ${OPENSSL_EXECUTABLE}
561 req -new -newkey rsa:1024 -days 10000 -nodes -x509 -keyout ${TEST_SERVER_SSL_KEY} -out ${TEST_SERVER_SSL_CERT}
565 list(APPEND TEST_SERVER_DATA
566 ${TEST_SERVER_SSL_KEY}
567 ${TEST_SERVER_SSL_CERT})
569 endif(NOT WITHOUT_SERVER)
571 if (NOT WITHOUT_CLIENT)
575 if (NOT WITHOUT_TEST_CLIENT)
576 create_test_app(test-client
577 "test-server/test-client.c"
585 if (NOT WITHOUT_TEST_FRAGGLE)
586 create_test_app(test-fraggle
587 "test-server/test-fraggle.c"
589 "${WIN32_HELPERS_PATH}/unistd.h;${WIN32_HELPERS_PATH}/sys/time.h")
595 if (NOT WITHOUT_TEST_PING)
596 create_test_app(test-ping
597 "test-server/test-ping.c"
599 "${WIN32_HELPERS_PATH}/unistd.h;${WIN32_HELPERS_PATH}/sys/time.h")
601 endif(NOT WITHOUT_CLIENT)
604 # Copy OpenSSL dlls to the output directory on Windows.
605 # (Otherwise we'll get an error when trying to run)
607 if (WIN32 AND WITH_SSL AND NOT USE_CYASSL)
608 if(OPENSSL_BIN_FOUND)
609 message("OpenSSL dlls found, copying to output directory")
610 message("Libeay: ${LIBEAY_BIN}")
611 message("SSLeay: ${SSLEAY_BIN}")
613 foreach(TARGET_BIN ${TEST_APP_LIST})
614 add_custom_command(TARGET ${TARGET_BIN}
616 COMMAND ${CMAKE_COMMAND} -E copy ${LIBEAY_BIN} $<TARGET_FILE_DIR:${TARGET_BIN}> VERBATIM)
618 add_custom_command(TARGET ${TARGET_BIN}
620 COMMAND ${CMAKE_COMMAND} -E copy ${SSLEAY_BIN} $<TARGET_FILE_DIR:${TARGET_BIN}> VERBATIM)
624 endif(NOT WITHOUT_TESTAPPS)
627 # Generate documentation.
628 # TODO: Fix this on Windows.
629 message("Generating API documentation")
630 file(GLOB C_FILES ${PROJECT_SOURCE_DIR}/lib/*.c)
631 execute_process(COMMAND ${CMAKE_COMMAND} -E make_directory ${PROJECT_BINARY_DIR}/doc/)
634 COMMAND ${PROJECT_SOURCE_DIR}/scripts/kernel-doc -html ${C_FILES} ${HDR_PUBLIC}
635 OUTPUT_FILE ${PROJECT_BINARY_DIR}/doc/libwebsockets-api-doc.html
639 COMMAND ${PROJECT_SOURCE_DIR}/scripts/kernel-doc -text ${C_FILES} ${HDR_PUBLIC}
640 OUTPUT_FILE ${PROJECT_BINARY_DIR}/doc/libwebsockets-api-doc.txt
643 # Generate and install pkgconfig.
644 # (This is not indented, because the tabs will be part of the output)
645 file(WRITE ${PROJECT_BINARY_DIR}/libwebsockets.pc
647 exec_prefix=\${prefix}
648 libdir=\${exec_prefix}/lib
649 includedir=\${prefix}/include
652 Description: Websockets server and client library
653 Version: ${PACKAGE_VERSION}
655 Libs: -L\${libdir} -lwebsockets
656 Cflags: -I\${includedir}"
659 install(FILES ${PROJECT_BINARY_DIR}/libwebsockets.pc
660 DESTINATION /usr/local/include/pkgconfig)
664 install(FILES ${HDR_PUBLIC}
665 DESTINATION /usr/local/include)
668 install(TARGETS websockets websockets_shared
669 LIBRARY DESTINATION /usr/local/lib
670 ARCHIVE DESTINATION /usr/local/lib)
673 install(TARGETS ${TEST_APP_LIST}
674 RUNTIME DESTINATION /usr/local/bin)
676 # Install shared files used by the test-server.
677 if (NOT WITHOUT_TESTAPPS AND NOT WITHOUT_SERVER)
678 install(FILES ${TEST_SERVER_DATA}
679 DESTINATION /usr/local/share/libwebsockets-test-server)