Subject: [PATCH] cmake: set the project language
[platform/upstream/libwebsockets.git] / CMakeLists.txt
1 cmake_minimum_required(VERSION 2.8)
2
3 project(libwebsockets C)
4
5 set(PACKAGE "libwebsockets")
6 set(CPACK_PACKAGE_NAME "${PACKAGE}")
7 set(CPACK_PACKAGE_VERSION_MAJOR "1")
8 set(CPACK_PACKAGE_VERSION_MINOR "3")
9 set(CPACK_PACKAGE_VERSION "${CPACK_PACKAGE_VERSION_MAJOR}.${CPACK_PACKAGE_VERSION_MINOR}")
10 set(CPACK_PACKAGE_VENDOR "andy@warmcat.com")
11 set(CPACK_PACKAGE_DESCRIPTION_SUMMARY "${PACKAGE} ${PACKAGE_VERSION}")
12 set(SOVERSION "5.0.0")
13 set(CPACK_SOURCE_GENERATOR "TGZ")
14 set(CPACK_SOURCE_PACKAGE_FILE_NAME "${CPACK_PACKAGE_NAME}-${CPACK_PACKAGE_VERSION}")
15 set(VERSION "${CPACK_PACKAGE_VERSION}")
16
17 set(LWS_LIBRARY_VERSION ${CPACK_PACKAGE_VERSION})
18 set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${PROJECT_SOURCE_DIR}/cmake/")
19
20 message(STATUS "CMAKE_TOOLCHAIN_FILE='${CMAKE_TOOLCHAIN_FILE}'")
21
22 # Try to find the current Git hash.
23 find_package(Git)
24 if(GIT_EXECUTABLE)
25         execute_process(
26     WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}"
27     COMMAND "${GIT_EXECUTABLE}" log -n 1 --pretty=%h
28     OUTPUT_VARIABLE GIT_HASH
29     OUTPUT_STRIP_TRAILING_WHITESPACE
30     )
31
32     set(LWS_BUILD_HASH ${GIT_HASH})
33     message("Git commit hash: ${LWS_BUILD_HASH}")
34 endif()
35
36 option(LWS_WITH_SSL "Include SSL support (default OpenSSL, CyaSSL if LWS_USE_CYASSL is set)" ON)
37 option(LWS_SSL_CLIENT_USE_OS_CA_CERTS "SSL support should make use of OS installed CA root certs" ON)
38 option(LWS_USE_EXTERNAL_ZLIB "Search the system for ZLib instead of using the included one (on Windows)" OFF)
39 option(LWS_USE_CYASSL "Use CyaSSL replacement for OpenSSL. When settings this, you also need to specify LWS_CYASSL_LIB and LWS_CYASSL_INCLUDE_DIRS" OFF)
40 option(LWS_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)
41 option(LWS_WITHOUT_CLIENT "Don't build the client part of the library" OFF)
42 option(LWS_WITHOUT_SERVER "Don't build the server part of the library" OFF)
43 option(LWS_LINK_TESTAPPS_DYNAMIC "Link the test apps to the shared version of the library. Default is to link statically" OFF)
44 option(LWS_WITHOUT_TESTAPPS "Don't build the libwebsocket-test-apps" OFF)
45 option(LWS_WITHOUT_TEST_SERVER "Don't build the test server" OFF)
46 option(LWS_WITHOUT_TEST_SERVER_EXTPOLL "Don't build the test server version that uses external poll" OFF)
47 option(LWS_WITHOUT_TEST_PING "Don't build the ping test application" OFF)
48 option(LWS_WITHOUT_TEST_CLIENT "Don't build the client test application" OFF)
49 option(LWS_WITHOUT_TEST_FRAGGLE "Don't build the ping test application" OFF)
50 option(LWS_WITHOUT_DEBUG "Don't compile debug related code" OFF)
51 option(LWS_WITHOUT_EXTENSIONS "Don't compile with extensions" OFF)
52 option(LWS_WITH_LATENCY "Build latency measuring code into the library" OFF)
53 option(LWS_WITHOUT_DAEMONIZE "Don't build the daemonization api" OFF)
54 option(LWS_WITH_LIBEV "Compile with support for libev" OFF)
55 option(LWS_IPV6 "Compile with support for ipv6" ON)
56 option(LWS_WITH_HTTP2 "Compile with support for http2" OFF)
57
58 # Allow the user to override installation directories.
59 set(LWS_INSTALL_LIB_DIR       lib CACHE PATH "Installation directory for libraries")
60 set(LWS_INSTALL_BIN_DIR       bin CACHE PATH "Installation directory for executables")
61 set(LWS_INSTALL_INCLUDE_DIR   include CACHE PATH "Installation directory for header files")
62 set(LWS_INSTALL_EXAMPLES_DIR  bin CACHE PATH "Installation directory for example files")
63
64 if (LWS_WITHOUT_CLIENT AND LWS_WITHOUT_SERVER)
65         message(FATAL_ERROR "Makes no sense to compile without both client or server.")
66 endif()
67
68 # The base dir where the test-apps look for the SSL certs.
69 set(LWS_OPENSSL_CLIENT_CERTS ../share CACHE PATH "Server SSL certificate directory")
70 if (WIN32)
71         set(LWS_OPENSSL_CLIENT_CERTS . CACHE PATH "Client SSL certificate directory")
72
73         if (LWS_IPV6)
74                 set(LWS_IPV6 OFF)
75                 message(WARNING "IPv6 does currently not work on Windows!")
76         endif()
77 else()
78         set(LWS_OPENSSL_CLIENT_CERTS /etc/pki/tls/certs/ CACHE PATH "Client SSL certificate directory")
79 endif()
80
81 set(LWS_CYASSL_LIB CACHE PATH "Path to the CyaSSL library")
82 set(LWS_CYASSL_INCLUDE_DIRS CACHE PATH "Path to the CyaSSL include directory")
83
84 if (LWS_USE_CYASSL)
85         if ("${LWS_CYASSL_LIB}" STREQUAL "" OR "${LWS_CYASSL_INCLUDE_DIRS}" STREQUAL "")
86                 message(FATAL_ERROR "You must set LWS_CYASSL_LIB and LWS_CYASSL_INCLUDE_DIRS when LWS_USE_CYASSL is turned on")
87         endif()
88         set(USE_CYASSL 1)
89 endif()
90
91 if (LWS_WITHOUT_EXTENSIONS)
92         set(LWS_NO_EXTENSIONS 1)
93 endif()
94
95 if (LWS_WITH_SSL)
96         set(LWS_OPENSSL_SUPPORT 1)
97 endif()
98
99 if (LWS_SSL_CLIENT_USE_OS_CA_CERTS)
100         set(LWS_SSL_CLIENT_USE_OS_CA_CERTS 1)
101 endif()
102
103 if (LWS_WITH_LATENCY)
104         set(LWS_LATENCY 1)
105 endif()
106
107 if (LWS_WITHOUT_DAEMONIZE OR WIN32)
108         set(LWS_NO_DAEMONIZE 1)
109 endif()
110
111 if (LWS_WITHOUT_SERVER)
112         set(LWS_NO_SERVER 1)
113 endif()
114
115 if (LWS_WITHOUT_CLIENT)
116         set(LWS_NO_CLIENT 1)
117 endif()
118
119 if (LWS_WITHOUT_DEBUG)
120         set(_DEBUG 0)
121 else()
122         set(_DEBUG 1)
123 endif()
124
125 if (LWS_WITH_LIBEV)
126         set(LWS_USE_LIBEV 1)
127         set(LWS_NO_EXTERNAL_POLL 1)
128 endif()
129
130 if (LWS_IPV6)
131         set(LWS_USE_IPV6 1)
132 endif()
133
134 if (LWS_WITH_HTTP2)
135         set(LWS_USE_HTTP2 1)
136 endif()
137
138 if (MINGW)
139         set(LWS_MINGW_SUPPORT 1)
140 endif()
141
142 include_directories("${PROJECT_BINARY_DIR}")
143
144 include(CheckCSourceCompiles)
145
146 # Check for different inline keyword versions.
147 foreach(KEYWORD "inline" "__inline__" "__inline")
148         set(CMAKE_REQUIRED_DEFINITIONS "-DKEYWORD=${KEYWORD}")
149         CHECK_C_SOURCE_COMPILES(
150                 "
151                 #include <stdio.h>
152                 KEYWORD void a() {}
153                 int main(int argc, char **argv) { a(); return 0; }
154                 " HAVE_${KEYWORD})
155 endforeach()
156
157 if (NOT HAVE_inline)
158         if (HAVE___inline__)
159                 set(inline __inline__)
160         elseif(HAVE___inline)
161                 set(inline __inline)
162         endif()
163 endif()
164
165 # Put the libaries and binaries that get built into directories at the
166 # top of the build tree rather than in hard-to-find leaf directories. 
167 SET(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${PROJECT_BINARY_DIR}/bin")
168 SET(CMAKE_LIBRARY_OUTPUT_DIRECTORY "${PROJECT_BINARY_DIR}/lib")
169 SET(CMAKE_ARCHIVE_OUTPUT_DIRECTORY "${PROJECT_BINARY_DIR}/lib")
170
171 # Put absolute path of dynamic libraries into the object code. Some
172 # architectures, notably Mac OS X, need this.
173 SET(CMAKE_INSTALL_NAME_DIR "${CMAKE_INSTALL_PREFIX}/${LWS_INSTALL_LIB_DIR}${LIB_SUFFIX}")
174
175 # So we can include the CMake generated config file only when
176 # building with CMAKE.
177 add_definitions(-DCMAKE_BUILD)
178
179 include(CheckFunctionExists)
180 include(CheckIncludeFile)
181 include(CheckIncludeFiles)
182 include(CheckLibraryExists)
183 include(CheckTypeSize)
184
185 CHECK_FUNCTION_EXISTS(bzero HAVE_BZERO)
186 CHECK_FUNCTION_EXISTS(fork HAVE_FORK)
187 CHECK_FUNCTION_EXISTS(getenv HAVE_GETENV)
188 CHECK_FUNCTION_EXISTS(malloc HAVE_MALLOC)
189 CHECK_FUNCTION_EXISTS(memset HAVE_MEMSET)
190 CHECK_FUNCTION_EXISTS(realloc HAVE_REALLOC)
191 CHECK_FUNCTION_EXISTS(socket HAVE_SOCKET)
192 CHECK_FUNCTION_EXISTS(strerror HAVE_STRERROR)
193 CHECK_FUNCTION_EXISTS(vfork HAVE_VFORK)
194 CHECK_FUNCTION_EXISTS(getifaddrs HAVE_GETIFADDRS)
195
196 if (NOT HAVE_GETIFADDRS)
197         if (LWS_WITHOUT_BUILTIN_GETIFADDRS)
198                 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.")
199         endif()
200
201         set(LWS_BUILTIN_GETIFADDRS 1)
202 endif()
203
204 CHECK_INCLUDE_FILE(dlfcn.h HAVE_DLFCN_H)
205 CHECK_INCLUDE_FILE(fcntl.h HAVE_FCNTL_H)
206 CHECK_INCLUDE_FILE(in6addr.h HAVE_IN6ADDR_H)
207 CHECK_INCLUDE_FILE(inttypes.h HAVE_INTTYPES_H)
208 CHECK_INCLUDE_FILE(memory.h HAVE_MEMORY_H)
209 CHECK_INCLUDE_FILE(netinet/in.h HAVE_NETINET_IN_H)
210 CHECK_INCLUDE_FILE(stdint.h HAVE_STDINT_H)
211 CHECK_INCLUDE_FILE(stdlib.h HAVE_STDLIB_H)
212 CHECK_INCLUDE_FILE(strings.h HAVE_STRINGS_H)
213 CHECK_INCLUDE_FILE(string.h HAVE_STRING_H)
214 CHECK_INCLUDE_FILE(sys/prctl.h HAVE_SYS_PRCTL_H)
215 CHECK_INCLUDE_FILE(sys/socket.h HAVE_SYS_SOCKET_H)
216 CHECK_INCLUDE_FILE(sys/stat.h HAVE_SYS_STAT_H)
217 CHECK_INCLUDE_FILE(sys/types.h HAVE_SYS_TYPES_H)
218 CHECK_INCLUDE_FILE(unistd.h HAVE_UNISTD_H)
219 CHECK_INCLUDE_FILE(vfork.h HAVE_VFORK_H)
220 CHECK_INCLUDE_FILE(zlib.h HAVE_ZLIB_H)
221
222 # TODO: These can be tested if they actually work also...
223 set(HAVE_WORKING_FORK HAVE_FORK)
224 set(HAVE_WORKING_VFORK HAVE_VFORK)
225
226 CHECK_INCLUDE_FILES("stdlib.h;stdarg.h;string.h;float.h" STDC_HEADERS)
227
228 CHECK_TYPE_SIZE(pid_t PID_T_SIZE)
229 CHECK_TYPE_SIZE(size_t SIZE_T_SIZE)
230
231 if (NOT PID_T_SIZE)
232         set(pid_t int)
233 endif()
234
235 if (NOT SIZE_T_SIZE)
236         set(size_t "unsigned int")
237 endif()
238
239 if (NOT HAVE_MALLOC)
240         set(malloc rpl_malloc)
241 endif()
242
243 if (NOT HAVE_REALLOC)
244         set(realloc rpl_realloc)
245 endif()
246
247 # Generate the config.h that includes all the compilation settings.
248 configure_file(
249        "${PROJECT_SOURCE_DIR}/config.h.cmake"
250        "${PROJECT_BINARY_DIR}/lws_config.h")
251
252 if (MSVC)
253         # Turn off stupid microsoft security warnings.
254         add_definitions(-D_CRT_SECURE_NO_DEPRECATE -D_CRT_NONSTDC_NO_DEPRECATE)
255 endif(MSVC)
256
257 include_directories("${PROJECT_SOURCE_DIR}/lib")
258
259 # Group headers and sources.
260 # Some IDEs use this for nicer file structure.
261 set(HDR_PRIVATE
262         lib/private-libwebsockets.h
263         )
264
265 set(HDR_PUBLIC  
266         "${PROJECT_SOURCE_DIR}/lib/libwebsockets.h"
267         "${PROJECT_BINARY_DIR}/lws_config.h"
268         )
269
270 set(SOURCES
271         lib/base64-decode.c
272         lib/handshake.c
273         lib/libwebsockets.c
274         lib/service.c
275         lib/pollfd.c
276         lib/output.c
277         lib/parsers.c
278         lib/context.c
279         lib/sha-1.c
280         )
281
282 if (NOT LWS_WITHOUT_CLIENT)
283         list(APPEND SOURCES
284                 lib/client.c
285                 lib/client-handshake.c
286                 lib/client-parser.c
287                 )
288 endif()
289
290 if (LWS_WITH_SSL)
291         list(APPEND SOURCES
292                 lib/ssl.c
293                 )
294 endif()
295
296 if (LWS_WITH_HTTP2)
297         list(APPEND SOURCES
298                 lib/http2.c
299                 lib/hpack.c
300                 lib/ssl-http2.c
301                 )
302 endif()
303 # select the active platform files
304
305 if (WIN32)
306         list(APPEND SOURCES
307                 lib/lws-plat-win.c
308                 )
309 else()
310         list(APPEND SOURCES
311                 lib/lws-plat-unix.c
312                 )
313 endif()
314
315 if (NOT LWS_WITHOUT_SERVER)
316         list(APPEND SOURCES
317                 lib/server.c
318                 lib/server-handshake.c
319                 )
320 endif()
321
322 if (NOT LWS_WITHOUT_EXTENSIONS)
323         list(APPEND HDR_PRIVATE
324                 lib/extension-deflate-frame.h
325                 lib/extension-deflate-stream.h
326                 )
327
328         list(APPEND SOURCES
329                 lib/extension.c
330                 lib/extension-deflate-frame.c
331                 lib/extension-deflate-stream.c
332                 )
333 endif()
334
335 if (LWS_WITH_LIBEV)
336         list(APPEND SOURCES
337                 lib/libev.c
338                 )
339 endif(LWS_WITH_LIBEV)
340
341 # Add helper files for Windows.
342 if (WIN32)
343         set(WIN32_HELPERS_PATH win32port/win32helpers)
344         include_directories(${WIN32_HELPERS_PATH})
345 else(WIN32)
346         # Unix.
347         if (NOT LWS_WITHOUT_DAEMONIZE)
348                 list(APPEND SOURCES
349                         lib/daemonize.c
350                         )
351         endif()
352 endif(WIN32)
353
354 if (UNIX)
355         if (NOT HAVE_GETIFADDRS)
356                 list(APPEND HDR_PRIVATE lib/getifaddrs.h)
357                 list(APPEND SOURCES lib/getifaddrs.c)
358         endif()
359 endif(UNIX)
360
361
362 if (CMAKE_COMPILER_IS_GNUCC OR CMAKE_COMPILER_IS_GNUCXX)
363     if (UNIX)
364                 set( CMAKE_C_FLAGS "-Wall -Werror -O3 -fvisibility=hidden ${CMAKE_C_FLAGS}" )
365     else(UNIX)
366                 set( CMAKE_C_FLAGS "-Wall -O3 -fvisibility=hidden ${CMAKE_C_FLAGS}" )
367     endif(UNIX)
368 endif ()
369
370 source_group("Headers Private"  FILES ${HDR_PRIVATE})
371 source_group("Headers Public"   FILES ${HDR_PUBLIC})
372 source_group("Sources"          FILES ${SOURCES})
373
374 #
375 # Create the lib.
376 #
377 add_library(websockets STATIC
378                         ${HDR_PRIVATE}
379                         ${HDR_PUBLIC}
380                         ${SOURCES})
381 add_library(websockets_shared SHARED
382                         ${HDR_PRIVATE}
383                         ${HDR_PUBLIC}
384                         ${SOURCES})
385
386 if (WIN32)
387         # On Windows libs have the same file ending (.lib)
388         # for both static and shared libraries, so we
389         # need a unique name for the static one.
390         set_target_properties(websockets 
391                 PROPERTIES
392                 OUTPUT_NAME websockets_static)
393
394         # Compile as DLL (export function declarations)
395         set_property(
396                 TARGET websockets_shared
397                 PROPERTY COMPILE_DEFINITIONS 
398                 LWS_DLL
399                 LWS_INTERNAL
400                 )
401 endif(WIN32)
402
403 # We want the shared lib to be named "libwebsockets"
404 # not "libwebsocket_shared".
405 set_target_properties(websockets_shared
406                 PROPERTIES 
407                 OUTPUT_NAME websockets)
408
409 # Set the so version of the lib.
410 # Equivalent to LDFLAGS=-version-info x:x:x
411 if(CMAKE_COMPILER_IS_GNUCC OR CMAKE_COMPILER_IS_GNUCXX)
412         foreach(lib websockets websockets_shared)
413                 set_target_properties(${lib} 
414                         PROPERTIES
415                         SOVERSION ${SOVERSION})
416         endforeach()
417 endif()
418
419 set(LIB_LIST)
420
421 #
422 # Find libraries.
423 #
424
425 #
426 # ZLIB (Only needed for deflate extensions).
427 #
428 if (NOT LWS_WITHOUT_EXTENSIONS)
429         if (WIN32 AND NOT LWS_USE_EXTERNAL_ZLIB)
430                 message("Using included Zlib version")
431
432                 # Compile ZLib if needed.
433                 set(WIN32_ZLIB_PATH "win32port/zlib")
434                 set(ZLIB_SRCS
435                         ${WIN32_ZLIB_PATH}/adler32.c
436                         ${WIN32_ZLIB_PATH}/compress.c
437                         ${WIN32_ZLIB_PATH}/crc32.c
438                         ${WIN32_ZLIB_PATH}/deflate.c
439                         ${WIN32_ZLIB_PATH}/gzclose.c
440                         ${WIN32_ZLIB_PATH}/gzio.c
441                         ${WIN32_ZLIB_PATH}/gzlib.c
442                         ${WIN32_ZLIB_PATH}/gzread.c
443                         ${WIN32_ZLIB_PATH}/gzwrite.c
444                         ${WIN32_ZLIB_PATH}/infback.c
445                         ${WIN32_ZLIB_PATH}/inffast.c
446                         ${WIN32_ZLIB_PATH}/inflate.c
447                         ${WIN32_ZLIB_PATH}/inftrees.c
448                         ${WIN32_ZLIB_PATH}/trees.c
449                         ${WIN32_ZLIB_PATH}/uncompr.c
450                         ${WIN32_ZLIB_PATH}/zutil.c
451                 )
452
453                 # Create the library.
454                 add_library(ZLIB STATIC ${ZLIB_SRCS})
455
456                 # Set the same variables as find_package would.
457                 set(ZLIB_INCLUDE_DIRS ${WIN32_ZLIB_PATH})
458                 get_property(ZLIB_LIBRARIES TARGET ZLIB PROPERTY LOCATION)
459                 set(ZLIB_FOUND 1)
460         else()
461                 find_package(ZLIB REQUIRED)
462         endif()
463
464         # Make sure ZLib is compiled before the libs.
465         foreach (lib websockets websockets_shared)
466                 add_dependencies(${lib} ZLIB)
467         endforeach()
468
469         message("ZLib include dirs: ${ZLIB_INCLUDE_DIRS}")
470         message("ZLib libraries: ${ZLIB_LIBRARIES}")
471         include_directories(${ZLIB_INCLUDE_DIRS})
472         list(APPEND LIB_LIST ${ZLIB_LIBRARIES})
473 endif(NOT LWS_WITHOUT_EXTENSIONS)
474
475 #
476 # OpenSSL
477 #
478 if (LWS_WITH_SSL)
479         message("Compiling with SSL support")
480
481         if (LWS_USE_CYASSL)
482                 # Use CyaSSL as OpenSSL replacement.
483                 # TODO: Add a find_package command for this also.
484                 message("CyaSSL include dir: ${LWS_CYASSL_INCLUDE_DIRS}")
485                 message("CyaSSL libraries: ${LWS_CYASSL_LIB}")
486
487                 # Additional to the root directory we need to include
488                 # the cyassl/ subdirectory which contains the OpenSSL
489                 # compatability layer headers.
490                 foreach(inc ${LWS_CYASSL_INCLUDE_DIRS})
491                         include_directories("${inc}" "${inc}/cyassl")
492                 endforeach()
493
494                 list(APPEND LIB_LIST "${LWS_CYASSL_LIB}")
495         else()
496                 if (OPENSSL_ROOT_DIR)
497                         set(OPENSSL_INCLUDE_DIR "${OPENSSL_ROOT_DIR}/include")
498                         set(OPENSSL_LIBRARIES "${OPENSSL_ROOT_DIR}/lib/libssl.so" "${OPENSSL_ROOT_DIR}/lib/libcrypto.so")
499                 else(OPENSSL_ROOT_DIR)
500                 
501                 # TODO: Add support for STATIC also.
502                 find_package(OpenSSL REQUIRED)
503                 endif(OPENSSL_ROOT_DIR)
504
505                 message("OpenSSL include dir: ${OPENSSL_INCLUDE_DIR}")
506                 message("OpenSSL libraries: ${OPENSSL_LIBRARIES}")
507
508                 include_directories("${OPENSSL_INCLUDE_DIR}")
509                 list(APPEND LIB_LIST ${OPENSSL_LIBRARIES})
510
511                 # Link against dynamic linking functions.
512                 # (Don't link directly to libdl since it is not needed on all platforms, it's now a part of libc).
513                 list(APPEND LIB_LIST ${CMAKE_DL_LIBS})
514         endif()
515 endif(LWS_WITH_SSL)
516
517 if (LWS_WITH_LIBEV)
518         list(APPEND LIB_LIST "ev")
519 endif(LWS_WITH_LIBEV)
520
521 #
522 # Platform specific libs.
523 #
524 if (WINCE)
525         list(APPEND LIB_LIST ws2.lib)
526 elseif (WIN32)
527         list(APPEND LIB_LIST ws2_32.lib)
528 endif()
529
530 if (UNIX)
531         list(APPEND LIB_LIST m)
532 endif()
533
534 # Setup the linking for all libs.
535 foreach (lib websockets websockets_shared)
536         target_link_libraries(${lib} ${LIB_LIST})
537 endforeach()
538
539 #
540 # Test applications
541 #
542 set(TEST_APP_LIST)
543 if (NOT LWS_WITHOUT_TESTAPPS)
544         #
545         # Helper function for adding a test app.
546         #
547         macro(create_test_app TEST_NAME MAIN_SRC)
548                 
549                 set(TEST_SRCS ${MAIN_SRC})
550                 set(TEST_HDR)
551
552                 if (WIN32)
553                         list(APPEND TEST_SRCS 
554                                 ${WIN32_HELPERS_PATH}/getopt.c
555                                 ${WIN32_HELPERS_PATH}/getopt_long.c
556                                 ${WIN32_HELPERS_PATH}/gettimeofday.c
557                         )
558
559                         list(APPEND TEST_HDR 
560                                 ${WIN32_HELPERS_PATH}/getopt.h
561                                 ${WIN32_HELPERS_PATH}/gettimeofday.h
562                         )
563                 endif(WIN32)
564
565                 source_group("Headers Private"   FILES ${TEST_HDR})
566                 source_group("Sources"   FILES ${TEST_SRCS})
567                 add_executable(${TEST_NAME} ${TEST_SRCS} ${TEST_HDR})
568                 
569                 if (LWS_LINK_TESTAPPS_DYNAMIC)
570                         target_link_libraries(${TEST_NAME} websockets_shared)
571                         add_dependencies(${TEST_NAME} websockets_shared)
572                 else(LWS_LINK_TESTAPPS_DYNAMIC)
573                         target_link_libraries(${TEST_NAME} websockets)
574                         add_dependencies(${TEST_NAME} websockets)
575                 endif(LWS_LINK_TESTAPPS_DYNAMIC)
576
577                 # Set test app specific defines.
578                 set_property(TARGET ${TEST_NAME}
579                                         PROPERTY COMPILE_DEFINITIONS 
580                                                 INSTALL_DATADIR="${CMAKE_INSTALL_PREFIX}/share"
581                                         )
582
583                 # Prefix the binary names with libwebsockets.
584                 set_target_properties(${TEST_NAME} 
585                         PROPERTIES
586                         OUTPUT_NAME libwebsockets-${TEST_NAME})
587
588                 # Add to the list of tests.
589                 list(APPEND TEST_APP_LIST ${TEST_NAME})
590         endmacro()
591
592         if (LWS_WITH_SSL AND NOT LWS_USE_CYASSL)
593                 message("Searching for OpenSSL executable and dlls")
594                 find_package(OpenSSLbins)
595                 message("OpenSSL executable: ${OPENSSL_EXECUTABLE}")
596         endif()
597
598         if (NOT LWS_WITHOUT_SERVER)
599                 #
600                 # test-server
601                 #
602                 if (NOT LWS_WITHOUT_TEST_SERVER)
603                         create_test_app(test-server "test-server/test-server.c")
604                 endif()
605
606                 #
607                 # test-server-extpoll
608                 #
609                 if (NOT LWS_WITHOUT_TEST_SERVER_EXTPOLL)
610                         create_test_app(test-server-extpoll "test-server/test-server.c")
611                         # Set defines for this executable only.
612                         set_property(
613                                 TARGET test-server-extpoll
614                                 PROPERTY COMPILE_DEFINITIONS 
615                                         EXTERNAL_POLL 
616                                         INSTALL_DATADIR="${CMAKE_INSTALL_PREFIX}/share"
617                                 )
618
619                         # We need to link against winsock code.
620                         if (WIN32)
621                                 target_link_libraries(test-server-extpoll ws2_32.lib)
622                         endif(WIN32)
623                 endif()
624
625                 # Data files for running the test server.
626                 set(TEST_SERVER_DATA
627                         "${PROJECT_SOURCE_DIR}/test-server/favicon.ico"
628                         "${PROJECT_SOURCE_DIR}/test-server/leaf.jpg"
629                         "${PROJECT_SOURCE_DIR}/test-server/libwebsockets.org-logo.png"
630                         "${PROJECT_SOURCE_DIR}/test-server/test.html")
631
632                 # Generate self-signed SSL certs for the test-server.
633                 if (LWS_WITH_SSL AND OPENSSL_EXECUTABLE AND NOT LWS_WITHOUT_TEST_SERVER)
634                         message("Generating SSL Certificates for the test-server...")
635
636                         set(TEST_SERVER_SSL_KEY "${PROJECT_BINARY_DIR}/libwebsockets-test-server.key.pem")
637                         set(TEST_SERVER_SSL_CERT "${PROJECT_BINARY_DIR}/libwebsockets-test-server.pem")
638
639                         if (WIN32)
640                                 file(WRITE "${PROJECT_BINARY_DIR}/openssl_input.txt"
641                                         "GB\n"
642                                         "Erewhon\n"
643                                         "All around\n"
644                                         "libwebsockets-test\n"
645                                         "localhost\n"
646                                         "none@invalid.org\n\n"
647                                         )
648
649                                 # The "type" command is a bit picky with paths.
650                                 file(TO_NATIVE_PATH "${PROJECT_BINARY_DIR}/openssl_input.txt" OPENSSL_INPUT_WIN_PATH)
651                                 message("OPENSSL_INPUT_WIN_PATH = ${OPENSSL_INPUT_WIN_PATH}")
652                                 message("cmd = \"${OPENSSL_EXECUTABLE}\" req -new -newkey rsa:1024 -days 10000 -nodes -x509 -keyout \"${TEST_SERVER_SSL_KEY}\" -out \"${TEST_SERVER_SSL_CERT}\"")
653
654                                 execute_process(
655                                         COMMAND cmd /c type "${OPENSSL_INPUT_WIN_PATH}"
656                                         COMMAND "${OPENSSL_EXECUTABLE}" req -new -newkey rsa:1024 -days 10000 -nodes -x509 -keyout "${TEST_SERVER_SSL_KEY}" -out "${TEST_SERVER_SSL_CERT}"
657                                         RESULT_VARIABLE OPENSSL_RETURN_CODE)
658
659                                 message("\n")
660
661                                 if (OPENSSL_RETURN_CODE)
662                                         message(WARNING "!!! Failed to generate SSL certificate for Test Server using cmd.exe !!!:\nOpenSSL return code = ${OPENSSL_RETURN_CODE}")
663                                 else()
664                                         message("SUCCSESFULLY generated SSL certificate")
665                                 endif()
666                         else()
667                                 # Unix.
668                                 execute_process(
669                                         COMMAND printf "GB\\nErewhon\\nAll around\\nlibwebsockets-test\\n\\nlocalhost\\nnone@invalid.org\\n"
670                                         COMMAND "${OPENSSL_EXECUTABLE}"
671                                                 req -new -newkey rsa:1024 -days 10000 -nodes -x509 -keyout "${TEST_SERVER_SSL_KEY}" -out "${TEST_SERVER_SSL_CERT}"
672                                         RESULT_VARIABLE OPENSSL_RETURN_CODE)
673
674                                 if (OPENSSL_RETURN_CODE)
675                                         message(WARNING "!!! Failed to generate SSL certificate for Test Server!!!:\nOpenSSL return code = ${OPENSSL_RETURN_CODE}")
676                                 else()
677                                         message("SUCCSESFULLY generated SSL certificate")
678                                 endif()
679                         endif()
680
681                         list(APPEND TEST_SERVER_DATA 
682                                 "${TEST_SERVER_SSL_KEY}"
683                                 "${TEST_SERVER_SSL_CERT}")
684                 endif()
685
686                 add_custom_command(TARGET test-server
687                                                 POST_BUILD 
688                                                 COMMAND "${CMAKE_COMMAND}" -E make_directory "$<TARGET_FILE_DIR:test-server>/../share/libwebsockets-test-server")
689
690                 # Copy the file needed to run the server so that the test apps can
691                 # reach them from their default output location
692                 foreach (TEST_FILE ${TEST_SERVER_DATA})
693                         if (EXISTS ${TEST_FILE})
694                                 add_custom_command(TARGET test-server
695                                                         POST_BUILD 
696                                                         COMMAND "${CMAKE_COMMAND}" -E copy "${TEST_FILE}" "$<TARGET_FILE_DIR:test-server>/../share/libwebsockets-test-server" VERBATIM)
697                         endif()
698                 endforeach()
699         endif(NOT LWS_WITHOUT_SERVER)
700
701         if (NOT LWS_WITHOUT_CLIENT)
702                 #
703                 # test-client
704                 #
705                 if (NOT LWS_WITHOUT_TEST_CLIENT)
706                         create_test_app(test-client "test-server/test-client.c")
707                 endif()
708
709                 #
710                 # test-fraggle
711                 #
712                 if (NOT LWS_WITHOUT_TEST_FRAGGLE)
713                         create_test_app(test-fraggle "test-server/test-fraggle.c")
714                 endif()
715
716                 #
717                 # test-ping
718                 #
719                 if (NOT LWS_WITHOUT_TEST_PING)
720                         create_test_app(test-ping "test-server/test-ping.c")
721                 endif()
722                 #
723                 # test-echo
724                 #
725                 if (NOT WITHOUT_TEST_ECHO)
726                         create_test_app(test-echo "test-server/test-echo.c")
727                 endif()
728
729         endif(NOT LWS_WITHOUT_CLIENT)
730
731         #
732         # Copy OpenSSL dlls to the output directory on Windows.
733         # (Otherwise we'll get an error when trying to run)
734         #
735         if (WIN32 AND LWS_WITH_SSL AND NOT LWS_USE_CYASSL)
736                 if(OPENSSL_BIN_FOUND)
737                         message("OpenSSL dlls found:")
738                         message("  Libeay: ${LIBEAY_BIN}")
739                         message("  SSLeay: ${SSLEAY_BIN}")
740
741                         foreach(TARGET_BIN ${TEST_APP_LIST})                    
742                                 add_custom_command(TARGET ${TARGET_BIN}
743                                         POST_BUILD 
744                                         COMMAND "${CMAKE_COMMAND}" -E copy "${LIBEAY_BIN}" "$<TARGET_FILE_DIR:${TARGET_BIN}>" VERBATIM)
745                                         
746                                 add_custom_command(TARGET ${TARGET_BIN}
747                                         POST_BUILD 
748                                         COMMAND "${CMAKE_COMMAND}" -E copy "${SSLEAY_BIN}" "$<TARGET_FILE_DIR:${TARGET_BIN}>" VERBATIM)
749                         endforeach()
750                 endif()
751         endif()
752 endif(NOT LWS_WITHOUT_TESTAPPS)
753
754 if (UNIX)
755         # Generate documentation.
756         # TODO: Fix this on Windows.
757         message("Generating API documentation")
758         file(GLOB C_FILES "${PROJECT_SOURCE_DIR}/lib/*.c")
759         execute_process(COMMAND "${CMAKE_COMMAND}" -E make_directory "${PROJECT_BINARY_DIR}/doc/")
760
761         execute_process(
762                 COMMAND "${PROJECT_SOURCE_DIR}/scripts/kernel-doc" -html ${C_FILES} ${HDR_PUBLIC}
763                 OUTPUT_FILE "${PROJECT_BINARY_DIR}/doc/libwebsockets-api-doc.html"
764                 ERROR_QUIET)
765
766         execute_process(
767                 COMMAND "${PROJECT_SOURCE_DIR}/scripts/kernel-doc" -text ${C_FILES} ${HDR_PUBLIC}
768                 OUTPUT_FILE "${PROJECT_BINARY_DIR}/doc/libwebsockets-api-doc.txt"
769                 ERROR_QUIET)
770
771 # Generate and install pkgconfig.
772 # (This is not indented, because the tabs will be part of the output)
773 file(WRITE "${PROJECT_BINARY_DIR}/libwebsockets.pc"
774 "prefix=\"${CMAKE_INSTALL_PREFIX}\"
775 exec_prefix=\${prefix}
776 libdir=\${exec_prefix}/lib${LIB_SUFFIX}
777 includedir=\${prefix}/include
778
779 Name: libwebsockets
780 Description: Websockets server and client library
781 Version: ${CPACK_PACKAGE_VERSION_MAJOR}.${CPACK_PACKAGE_VERSION_MINOR}
782
783 Libs: -L\${libdir} -lwebsockets
784 Cflags: -I\${includedir}"
785 )
786
787         install(FILES "${PROJECT_BINARY_DIR}/libwebsockets.pc"
788                 DESTINATION lib${LIB_SUFFIX}/pkgconfig)
789 endif(UNIX)
790
791 #
792 # Installation preparations.
793 #
794
795 if(WIN32 AND NOT CYGWIN)
796   set(DEF_INSTALL_CMAKE_DIR cmake)
797 else()
798   set(DEF_INSTALL_CMAKE_DIR lib/cmake/libwebsockets)
799 endif()
800
801 set(LWS_INSTALL_CMAKE_DIR ${DEF_INSTALL_CMAKE_DIR} CACHE PATH "Installation directory for CMake files")
802
803 # Export targets (This is used for other CMake projects to easily find the libraries and include files).
804 export(TARGETS websockets websockets_shared
805         FILE "${PROJECT_BINARY_DIR}/LibwebsocketsTargets.cmake")
806 export(PACKAGE libwebsockets)
807
808 # Generate the config file for the build-tree.
809 set(LWS__INCLUDE_DIRS 
810     "${PROJECT_SOURCE_DIR}/lib"
811     "${PROJECT_BINARY_DIR}")
812 set(LIBWEBSOCKETS_INCLUDE_DIRS ${LWS__INCLUDE_DIRS} CACHE PATH "Libwebsockets include directories")
813 configure_file(${PROJECT_SOURCE_DIR}/cmake/LibwebsocketsConfig.cmake.in
814                 ${PROJECT_BINARY_DIR}/LibwebsocketsConfig.cmake 
815                 @ONLY)
816
817 # Generate the config file for the installation tree.
818 get_filename_component(LWS_ABSOLUTE_INSTALL_CMAKE_DIR ${LWS_INSTALL_CMAKE_DIR} ABSOLUTE)
819 get_filename_component(LWS_ABSOLUTE_INSTALL_INCLUDE_DIR ${LWS_INSTALL_INCLUDE_DIR} ABSOLUTE)
820 file(RELATIVE_PATH 
821     REL_INCLUDE_DIR 
822     "${LWS_ABSOLUTE_INSTALL_CMAKE_DIR}"
823     "${LWS_ABSOLUTE_INSTALL_INCLUDE_DIR}") # Calculate the relative directory from the cmake dir.
824
825 # Note the EVENT_CMAKE_DIR is defined in JanssonConfig.cmake.in, 
826 # we escape it here so it's evaluated when it is included instead
827 # so that the include dirs are given relative to where the 
828 # config file is located.
829 set(LWS__INCLUDE_DIRS 
830     "\${LWS_CMAKE_DIR}/${REL_INCLUDE_DIR}") 
831 configure_file(${PROJECT_SOURCE_DIR}/cmake/LibwebsocketsConfig.cmake.in
832                 ${PROJECT_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/LibwebsocketsConfig.cmake 
833                 @ONLY)
834
835 # Generate version info for both build-tree and install-tree.
836 configure_file(${PROJECT_SOURCE_DIR}/cmake/LibwebsocketsConfigVersion.cmake.in
837                 ${PROJECT_BINARY_DIR}/LibwebsocketsConfigVersion.cmake 
838                 @ONLY)
839
840 set_target_properties(websockets websockets_shared 
841                                         PROPERTIES PUBLIC_HEADER "${HDR_PUBLIC}")
842
843 #
844 # Installation.
845 #
846
847 # Install libs and headers.
848 install(TARGETS websockets websockets_shared
849                 EXPORT LibwebsocketsTargets
850                 LIBRARY DESTINATION "${LWS_INSTALL_LIB_DIR}${LIB_SUFFIX}" COMPONENT libraries
851                 ARCHIVE DESTINATION "${LWS_INSTALL_LIB_DIR}${LIB_SUFFIX}" COMPONENT libraries
852                 RUNTIME DESTINATION "${LWS_INSTALL_BIN_DIR}" COMPONENT libraries # Windows DLLs
853                 PUBLIC_HEADER DESTINATION "${LWS_INSTALL_INCLUDE_DIR}" COMPONENT dev)
854 set(CPACK_COMPONENT_LIBRARIES_DISPLAY_NAME "Libraries")
855 set(CPACK_COMPONENT_DEV_DISPLAY_NAME "Development files")
856
857 # Install test apps.
858 if (NOT LWS_WITHOUT_TESTAPPS AND NOT LWS_WITHOUT_CLIENT)
859         install(TARGETS test-client ${TEST_APP_LIST}
860                         RUNTIME DESTINATION ${LWS_INSTALL_EXAMPLES_DIR}
861                         COMPONENT examples)
862         set(CPACK_COMPONENT_EXAMPLES_DISPLAY_NAME "Example files")
863 endif()
864
865 # Programs shared files used by the test-server.
866 if (NOT LWS_WITHOUT_TESTAPPS AND NOT LWS_WITHOUT_SERVER)
867         install(FILES ${TEST_SERVER_DATA}
868                         DESTINATION share/libwebsockets-test-server
869                         COMPONENT examples)
870 endif()
871
872 # Install exports for the install-tree.
873 install(EXPORT LibwebsocketsTargets
874                 DESTINATION "${LWS_INSTALL_CMAKE_DIR}" COMPONENT dev)
875
876 # build subdir is not part of sources
877 set(CPACK_SOURCE_IGNORE_FILES $(CPACK_SOURCE_IGNORE_FILES) ".git" "build" "tgz" "tar.gz")
878
879 # Most people are more used to "make dist" compared to "make package_source"
880 add_custom_target(dist COMMAND "${CMAKE_MAKE_PROGRAM}" package_source)
881
882 include(UseRPMTools)
883 if (RPMTools_FOUND)
884         RPMTools_ADD_RPM_TARGETS(libwebsockets libwebsockets.spec)
885 endif()
886
887 message("---------------------------------------------------------------------")
888 message("  Settings:  (For more help do cmake -LH <srcpath>")
889 message("---------------------------------------------------------------------")
890 message(" LWS_WITH_SSL = ${LWS_WITH_SSL}  (SSL Support)")
891 message(" LWS_SSL_CLIENT_USE_OS_CA_CERTS = ${LWS_SSL_CLIENT_USE_OS_CA_CERTS}")
892 message(" LWS_USE_CYASSL = ${LWS_USE_CYASSL} (CyaSSL replacement for OpenSSL)")
893 if (LWS_USE_CYASSL)
894         message("   LWS_CYASSL_LIB = ${LWS_CYASSL_LIB}")
895         message("   LWS_CYASSL_INCLUDE_DIRS = ${LWS_CYASSL_INCLUDE_DIRS}")
896 endif()
897 message(" LWS_WITHOUT_BUILTIN_GETIFADDRS = ${LWS_WITHOUT_BUILTIN_GETIFADDRS}")
898 message(" LWS_WITHOUT_CLIENT = ${LWS_WITHOUT_CLIENT}")
899 message(" LWS_WITHOUT_SERVER = ${LWS_WITHOUT_SERVER}")
900 message(" LWS_LINK_TESTAPPS_DYNAMIC = ${LWS_LINK_TESTAPPS_DYNAMIC}")
901 message(" LWS_WITHOUT_TESTAPPS = ${LWS_WITHOUT_TESTAPPS}")
902 message(" LWS_WITHOUT_TEST_SERVER = ${LWS_WITHOUT_TEST_SERVER}")
903 message(" LWS_WITHOUT_TEST_SERVER_EXTPOLL = ${LWS_WITHOUT_TEST_SERVER_EXTPOLL}")
904 message(" LWS_WITHOUT_TEST_PING = ${LWS_WITHOUT_TEST_PING}")
905 message(" LWS_WITHOUT_TEST_CLIENT = ${LWS_WITHOUT_TEST_CLIENT}")
906 message(" LWS_WITHOUT_TEST_FRAGGLE = ${LWS_WITHOUT_TEST_FRAGGLE}")
907 message(" LWS_WITHOUT_DEBUG = ${LWS_WITHOUT_DEBUG}")
908 message(" LWS_WITHOUT_EXTENSIONS = ${LWS_WITHOUT_EXTENSIONS}")
909 message(" LWS_WITH_LATENCY = ${LWS_WITH_LATENCY}")
910 message(" LWS_WITHOUT_DAEMONIZE = ${LWS_WITHOUT_DAEMONIZE}")
911 message(" LWS_USE_LIBEV = ${LWS_USE_LIBEV}")
912 message(" LWS_IPV6 = ${LWS_IPV6}")
913 message(" LWS_WITH_HTTP2 = ${LWS_WITH_HTTP2}")
914 message("---------------------------------------------------------------------")
915
916 # These will be available to parent projects including libwebsockets using add_subdirectory()
917 set(LIBWEBSOCKETS_LIBRARIES websocket websockets_shared CACHE STRING "Libwebsocket libraries")
918 set(LIBWEBSOCKETS_LIBRARIES_STATIC websocket CACHE STRING "Libwebsocket static library")
919 set(LIBWEBSOCKETS_LIBRARIES_SHARED websockets_shared CACHE STRING "Libwebsocket shared library")
920
921 # This must always be last!
922 include(CPack)