2d5f962685a402e3955606c3e5839108f6ed83dc
[profile/ivi/libwebsockets.git] / CMakeLists.txt
1 cmake_minimum_required(VERSION 2.6)
2
3 project(libwebsockets)
4
5 set(PACKAGE "libwebsockets")
6 set(CPACK_PACKAGE_NAME "${PACKAGE}")
7 set(CPACK_PACKAGE_VERSION_MAJOR "1")
8 set(CPACK_PACKAGE_VERSION_MINOR "2")
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 "3.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 # Try to find the current Git hash.
21 find_package(Git)
22 if(GIT_EXECUTABLE)
23         execute_process(
24     COMMAND "${GIT_EXECUTABLE}" log -n 1 --pretty=%h
25     OUTPUT_VARIABLE GIT_HASH
26     OUTPUT_STRIP_TRAILING_WHITESPACE
27     )
28
29     set(LWS_BUILD_HASH ${GIT_HASH})
30     message("Git commit hash: ${LWS_BUILD_HASH}")
31 endif()
32
33 option(WITH_SSL "Include SSL support (default OpenSSL, CyaSSL if USE_CYASSL is set)" ON)
34 option(USE_EXTERNAL_ZLIB "Search the system for ZLib instead of using the included one (on Windows)" OFF)
35 option(USE_CYASSL "Use CyaSSL replacement for OpenSSL. When settings this, you also need to specify CYASSL_LIB and CYASSL_INCLUDE_DIRS" OFF)
36 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)
37 option(WITHOUT_CLIENT "Don't build the client part of the library" OFF)
38 option(WITHOUT_SERVER "Don't build the server part of the library" OFF)
39 #option(WITH_LIBCRYPTO "Use libcrypto MD5 and SHA1 implementations" ON)
40 option(LINK_TESTAPPS_DYNAMIC "Link the test apps to the shared version of the library. Default is to link statically" OFF)
41 option(WITHOUT_TESTAPPS "Don't build the libwebsocket-test-apps" OFF)
42 option(WITHOUT_TEST_SERVER "Don't build the test server" OFF)
43 option(WITHOUT_TEST_SERVER_EXTPOLL "Don't build the test server version that uses external poll" OFF)
44 option(WITHOUT_TEST_PING "Don't build the ping test application" OFF)
45 option(WITHOUT_TEST_CLIENT "Don't build the client test application" OFF)
46 option(WITHOUT_TEST_FRAGGLE "Don't build the ping test application" OFF)
47 option(WITHOUT_DEBUG "Don't compile debug related code" OFF)
48 option(WITHOUT_EXTENSIONS "Don't compile with extensions" OFF)
49 option(WITH_LATENCY "Build latency measuring code into the library" OFF)
50 option(WITHOUT_DAEMONIZE "Don't build the daemonization api" OFF)
51
52 if (WITHOUT_CLIENT AND WITHOUT_SERVER)
53         message(FATAL_ERROR "Makes no sense to compile without both client or server.")
54 endif()
55
56 # The base dir where the test-apps look for the SSL certs.
57 set(SSL_CERT_DIR CACHE STRING "")
58 set(SSL_CLIENT_CERT_DIR CACHE STRING "")
59
60 if ("${SSL_CERT_DIR}" STREQUAL "")
61         set(SSL_CERT_DIR "../share")
62 endif()
63
64 if ("${SSL_CLIENT_CERT_DIR}" STREQUAL "")
65         if (WIN32)
66                 set(LWS_OPENSSL_CLIENT_CERTS ".")
67         else()
68                 set(LWS_OPENSSL_CLIENT_CERTS "/etc/pki/tls/certs/")
69         endif()
70 else()
71         set(LWS_OPENSSL_CLIENT_CERTS "${SSL_CLIENT_CERT_DIR}")
72 endif()
73
74 set(CYASSL_LIB CACHE STRING "")
75 set(CYASSL_INCLUDE_DIRS CACHE STRING "")
76
77 if (USE_CYASSL)
78         if ("${CYASSL_LIB}" STREQUAL "" OR "${CYASSL_INCLUDE_DIRS}" STREQUAL "")
79                 message(FATAL_ERROR "You must set CYASSL_LIB and CYASSL_INCLUDE_DIRS when USE_CYASSL is turned on")
80         endif()
81 endif()
82
83 if (WITHOUT_EXTENSIONS)
84         set(LWS_NO_EXTENSIONS 1)
85 endif()
86
87 if (WITH_SSL)
88         set(LWS_OPENSSL_SUPPORT 1)
89 endif()
90
91 if (WITH_LATENCY)
92         set(LWS_LATENCY 1)
93 endif()
94
95 if (WITHOUT_DAEMONIZE)
96         set(LWS_NO_DAEMONIZE 1)
97 endif()
98
99 if (WITHOUT_SERVER)
100         set(LWS_NO_SERVER 1)
101 endif()
102
103 if (WITHOUT_CLIENT)
104         set(LWS_NO_CLIENT 1)
105 endif()
106
107 if (WITHOUT_DEBUG)
108         set(_DEBUG 0)
109 else()
110         set(_DEBUG 1)
111 endif()
112
113 if (MINGW)
114         set(LWS_MINGW_SUPPORT 1)
115 endif()
116
117 include_directories(${PROJECT_BINARY_DIR})
118
119 include(CheckCSourceCompiles)
120
121 # Check for different inline keyword versions.
122 foreach(KEYWORD "inline" "__inline__" "__inline")
123         set(CMAKE_REQUIRED_DEFINITIONS "-DKEYWORD=${KEYWORD}")
124         CHECK_C_SOURCE_COMPILES(
125                 "
126                 #include <stdio.h>
127                 KEYWORD void a() {}
128                 int main(int argc, char **argv) { a(); return 0; }
129                 " HAVE_${KEYWORD})
130 endforeach()
131
132 if (NOT HAVE_inline)
133         if (HAVE___inline__)
134                 set(inline __inline__)
135         elseif(HAVE___inline)
136                 set(inline __inline)
137         endif()
138 endif()
139
140 # Put the libaries and binaries that get built into directories at the
141 # top of the build tree rather than in hard-to-find leaf directories. 
142 SET(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${PROJECT_BINARY_DIR}/bin)
143 SET(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${PROJECT_BINARY_DIR}/lib)
144 SET(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${PROJECT_BINARY_DIR}/lib)
145
146 # So we can include the CMake generated config file only when
147 # building with CMAKE.
148 add_definitions(-DCMAKE_BUILD)
149
150 include(CheckFunctionExists)
151 include(CheckIncludeFile)
152 include(CheckIncludeFiles)
153 include(CheckLibraryExists)
154
155 CHECK_FUNCTION_EXISTS(bzero HAVE_BZERO)
156 CHECK_FUNCTION_EXISTS(fork HAVE_FORK)
157 CHECK_FUNCTION_EXISTS(malloc HAVE_MALLOC)
158 CHECK_FUNCTION_EXISTS(memset HAVE_MEMSET)
159 CHECK_FUNCTION_EXISTS(realloc HAVE_REALLOC)
160 CHECK_FUNCTION_EXISTS(socket HAVE_SOCKET)
161 CHECK_FUNCTION_EXISTS(strerror HAVE_STRERROR)
162 CHECK_FUNCTION_EXISTS(vfork HAVE_VFORK)
163 CHECK_FUNCTION_EXISTS(getifaddrs HAVE_GETIFADDRS)
164
165 if (NOT HAVE_GETIFADDRS)
166         if (WITHOUT_BUILTIN_GETIFADDRS)
167                 message(FATAL_ERROR "No getifaddrs was found on the system. Turn off the WITHOUT_BUILTIN_GETIFADDRS compile option to use the supplied BSD version.")
168         endif()
169
170         set(LWS_BUILTIN_GETIFADDRS 1)
171 endif()
172
173 CHECK_INCLUDE_FILE(dlfcn.h HAVE_DLFCN_H)
174 CHECK_INCLUDE_FILE(fcntl.h HAVE_FCNTL_H)
175 CHECK_INCLUDE_FILE(inttypes.h HAVE_INTTYPES_H)
176 CHECK_INCLUDE_FILE(memory.h HAVE_MEMORY_H)
177 CHECK_INCLUDE_FILE(netinet/in.h HAVE_NETINET_IN_H)
178 CHECK_INCLUDE_FILE(stdint.h HAVE_STDINT_H)
179 CHECK_INCLUDE_FILE(stdlib.h HAVE_STDLIB_H)
180 CHECK_INCLUDE_FILE(strings.h HAVE_STRINGS_H)
181 CHECK_INCLUDE_FILE(string.h HAVE_STRING_H)
182 CHECK_INCLUDE_FILE(sys/prctl.h HAVE_SYS_PRCTL_H)
183 CHECK_INCLUDE_FILE(sys/socket.h HAVE_SYS_SOCKET_H)
184 CHECK_INCLUDE_FILE(sys/stat.h HAVE_SYS_STAT_H)
185 CHECK_INCLUDE_FILE(sys/types.h HAVE_SYS_TYPES_H)
186 CHECK_INCLUDE_FILE(unistd.h HAVE_UNISTD_H)
187 CHECK_INCLUDE_FILE(vfork.h HAVE_VFORK_H)
188 CHECK_INCLUDE_FILE(zlib.h HAVE_ZLIB_H)
189
190 # TODO: These can be tested if they actually work also...
191 set(HAVE_WORKING_FORK HAVE_FORK)
192 set(HAVE_WORKING_VFORK HAVE_VFORK)
193
194 CHECK_INCLUDE_FILES("stdlib.h;stdarg.h;string.h;float.h" STDC_HEADERS)
195
196 if (NOT HAVE_SYS_TYPES_H)
197         set(pid_t int)
198         set(size_t "unsigned int")
199 endif()
200
201 if (NOT HAVE_MALLOC)
202         set(malloc rpl_malloc)
203 endif()
204
205 if (NOT HAVE_REALLOC)
206         set(realloc rpl_realloc)
207 endif()
208
209 # Generate the config.h that includes all the compilation settings.
210 configure_file(
211                 ${PROJECT_SOURCE_DIR}/config.h.cmake 
212                 ${PROJECT_BINARY_DIR}/lws_config.h)
213
214 if (MSVC)
215         # Turn off stupid microsoft security warnings.
216         add_definitions(-D_CRT_SECURE_NO_DEPRECATE -D_CRT_NONSTDC_NO_DEPRECATE)
217 endif()
218
219 include_directories(${PROJECT_SOURCE_DIR}/lib)
220
221 # Group headers and sources.
222 # Some IDEs use this for nicer file structure.
223 set(HDR_PRIVATE
224         lib/private-libwebsockets.h
225         ${PROJECT_BINARY_DIR}/lws_config.h
226         )
227
228 set(HDR_PUBLIC  
229         ${PROJECT_SOURCE_DIR}/lib/libwebsockets.h
230         )
231
232 set(SOURCES
233         lib/base64-decode.c
234         lib/handshake.c
235         lib/libwebsockets.c
236         lib/output.c
237         lib/parsers.c
238         lib/sha-1.c
239         )
240
241 if (NOT WITHOUT_CLIENT)
242         list(APPEND SOURCES
243                 lib/client.c
244                 lib/client-handshake.c
245                 lib/client-parser.c
246                 )
247 endif()
248
249 if (NOT WITHOUT_SERVER)
250         list(APPEND SOURCES
251                 lib/server.c
252                 lib/server-handshake.c
253                 )
254 endif()
255
256 if (NOT WITHOUT_EXTENSIONS)
257         list(APPEND HDR_PRIVATE
258                 lib/extension-deflate-frame.h
259                 lib/extension-deflate-stream.h
260                 )
261
262         list(APPEND SOURCES
263                 lib/extension.c
264                 lib/extension-deflate-frame.c
265                 lib/extension-deflate-stream.c
266                 )
267 endif()
268
269 # Add helper files for Windows.
270 if (WIN32)
271         set(WIN32_HELPERS_PATH win32port/win32helpers)
272
273         list(APPEND HDR_PRIVATE
274                 ${WIN32_HELPERS_PATH}/websock-w32.h
275                 ${WIN32_HELPERS_PATH}/gettimeofday.h
276                 )
277 if (MINGW)
278         list(APPEND SOURCES
279                 ${WIN32_HELPERS_PATH}/gettimeofday.c
280                 )
281 else()
282         list(APPEND SOURCES 
283                 ${WIN32_HELPERS_PATH}/websock-w32.c
284                 ${WIN32_HELPERS_PATH}/gettimeofday.c
285                 )
286 endif()
287         include_directories(${WIN32_HELPERS_PATH})
288 else()
289         # Unix.
290         if (NOT WITHOUT_DAEMONIZE)
291                 list(APPEND SOURCES
292                         lib/daemonize.c
293                         )
294         endif()
295 endif()
296
297 if (UNIX)
298         if (NOT HAVE_GETIFADDRS)
299                 list(APPEND HDR_PRIVATE lib/getifaddrs.h)
300                 list(APPEND SOURCES lib/getifaddrs.c)
301         endif()
302 endif()
303
304 source_group("Headers Private"  FILES ${HDR_PRIVATE})
305 source_group("Headers Public"   FILES ${HDR_PUBLIC})
306 source_group("Sources"          FILES ${SOURCES})
307
308 #
309 # Create the lib.
310 #
311 add_library(websockets STATIC
312                         ${HDR_PRIVATE}
313                         ${HDR_PUBLIC}
314                         ${SOURCES})
315 add_library(websockets_shared SHARED
316                         ${HDR_PRIVATE}
317                         ${HDR_PUBLIC}
318                         ${SOURCES})
319
320 if (WIN32)
321         # On Windows libs have the same file ending (.lib)
322         # for both static and shared libraries, so we
323         # need a unique name for the static one.
324         set_target_properties(websockets 
325                 PROPERTIES
326                 OUTPUT_NAME websockets_static)
327
328         # Compile as DLL (export function declarations)
329         set_property(
330                 TARGET websockets_shared
331                 PROPERTY COMPILE_DEFINITIONS 
332                 LWS_DLL
333                 LWS_INTERNAL
334                 )
335 endif()
336
337 # We want the shared lib to be named "libwebsockets"
338 # not "libwebsocket_shared".
339 set_target_properties(websockets_shared
340                 PROPERTIES 
341                 OUTPUT_NAME websockets)
342
343 # Set the so version of the lib.
344 # Equivalent to LDFLAGS=-version-info 3:0:0
345 if(CMAKE_COMPILER_IS_GNUCC OR CMAKE_COMPILER_IS_GNUCXX)
346         foreach(lib websockets websockets_shared)
347                 set_target_properties(${lib} 
348                         PROPERTIES
349                         SOVERSION ${SOVERSION})
350         endforeach()
351 endif()
352
353 set(LIB_LIST)
354
355 #
356 # Find libraries.
357 #
358
359 #
360 # ZLIB (Only needed for deflate extensions).
361 #
362 if (NOT WITHOUT_EXTENSIONS)
363         if (WIN32 AND NOT USE_EXTERNAL_ZLIB)
364                 message("Using included Zlib version")
365
366                 # Compile ZLib if needed.
367                 set(WIN32_ZLIB_PATH "win32port/zlib")
368                 set(ZLIB_SRCS
369                         ${WIN32_ZLIB_PATH}/adler32.c
370                         ${WIN32_ZLIB_PATH}/compress.c
371                         ${WIN32_ZLIB_PATH}/crc32.c
372                         ${WIN32_ZLIB_PATH}/deflate.c
373                         ${WIN32_ZLIB_PATH}/gzclose.c
374                         ${WIN32_ZLIB_PATH}/gzio.c
375                         ${WIN32_ZLIB_PATH}/gzlib.c
376                         ${WIN32_ZLIB_PATH}/gzread.c
377                         ${WIN32_ZLIB_PATH}/gzwrite.c
378                         ${WIN32_ZLIB_PATH}/infback.c
379                         ${WIN32_ZLIB_PATH}/inffast.c
380                         ${WIN32_ZLIB_PATH}/inflate.c
381                         ${WIN32_ZLIB_PATH}/inftrees.c
382                         ${WIN32_ZLIB_PATH}/trees.c
383                         ${WIN32_ZLIB_PATH}/uncompr.c
384                         ${WIN32_ZLIB_PATH}/zutil.c
385                 )
386
387                 # Create the library.
388                 add_library(ZLIB STATIC ${ZLIB_SRCS})
389
390                 # Set the same variables as find_package would.
391                 set(ZLIB_INCLUDE_DIRS ${WIN32_ZLIB_PATH})
392                 get_property(ZLIB_LIBRARIES TARGET ZLIB PROPERTY LOCATION)
393                 set(ZLIB_FOUND 1)
394         else()
395                 find_package(ZLIB REQUIRED)
396         endif()
397
398         # Make sure ZLib is compiled before the libs.
399         foreach (lib websockets websockets_shared)
400                 add_dependencies(${lib} ZLIB)
401         endforeach()
402
403         message("ZLib include dirs: ${ZLIB_INCLUDE_DIRS}")
404         message("ZLib libraries: ${ZLIB_LIBRARIES}")
405         include_directories(${ZLIB_INCLUDE_DIRS})
406         list(APPEND LIB_LIST ${ZLIB_LIBRARIES})
407 endif(NOT WITHOUT_EXTENSIONS)
408
409 #
410 # OpenSSL
411 #
412 if (WITH_SSL)
413         message("Compiling with SSL support")
414
415         if (USE_CYASSL)
416                 # Use CyaSSL as OpenSSL replacement.
417                 # TODO: Add a find_package command for this also.
418                 message("CyaSSL include dir: ${CYASSL_INCLUDE_DIRS}")
419                 message("CyaSSL libraries: ${CYASSL_LIB}")
420
421                 # Additional to the root directory we need to include
422                 # the cyassl/ subdirectory which contains the OpenSSL
423                 # compatability layer headers.
424                 foreach(inc ${CYASSL_INCLUDE_DIRS})
425                         include_directories(${inc} ${inc}/cyassl)
426                 endforeach()
427
428                 list(APPEND ${LIB_LIST} ${CYASSL_LIB})
429         else()
430                 # TODO: Add support for STATIC also.
431                 find_package(OpenSSL REQUIRED)
432
433                 message("OpenSSL include dir: ${OPENSSL_INCLUDE_DIR}")
434                 message("OpenSSL libraries: ${OPENSSL_LIBRARIES}")
435
436                 include_directories(${OPENSSL_INCLUDE_DIR})
437                 list(APPEND LIB_LIST ${OPENSSL_LIBRARIES})
438         endif()
439 endif(WITH_SSL)
440
441 #
442 # Platform specific libs.
443 #
444 if (WIN32)
445         list(APPEND LIB_LIST ws2_32.lib)
446 endif()
447
448 if (UNIX)
449         list(APPEND LIB_LIST m)
450 endif()
451
452 # Setup the linking for all libs.
453 foreach (lib websockets websockets_shared)
454         target_link_libraries(${lib} ${LIB_LIST})
455 endforeach()
456
457 #
458 # Test applications
459 #
460 set(TEST_APP_LIST)
461 if (NOT WITHOUT_TESTAPPS)
462         #
463         # Helper function for adding a test app.
464         #
465         macro(create_test_app TEST_NAME MAIN_SRC WIN32_SRCS WIN32_HDRS)
466                 
467                 set(TEST_SRCS ${MAIN_SRC})
468                 set(TEST_HDR)
469
470                 if (WIN32)
471                         list(APPEND TEST_SRCS 
472                                 ${WIN32_HELPERS_PATH}/getopt.c
473                                 ${WIN32_HELPERS_PATH}/getopt_long.c
474                                 ${WIN32_HELPERS_PATH}/gettimeofday.c
475                                 ${WIN32_SRCS})
476
477                         list(APPEND TEST_HDR 
478                                 ${WIN32_HELPERS_PATH}/getopt.h
479                                 ${WIN32_HELPERS_PATH}/gettimeofday.h
480                                 ${WIN32_HDRS})
481                 endif(WIN32)
482
483                 source_group("Headers Private"   FILES ${TEST_HDR})
484                 source_group("Sources"   FILES ${TEST_SRCS})
485                 add_executable(${TEST_NAME} ${TEST_SRCS} ${TEST_HDR})
486                 
487                 if (LINK_TESTAPPS_DYNAMIC)
488                         target_link_libraries(${TEST_NAME} websockets_shared)
489                         add_dependencies(${TEST_NAME} websockets_shared)
490                 else()
491                         target_link_libraries(${TEST_NAME} websockets)
492                         add_dependencies(${TEST_NAME} websockets)
493                 endif()
494
495                 # Set test app specific defines.
496                 set_property(TARGET ${TEST_NAME}
497                                         PROPERTY COMPILE_DEFINITIONS 
498                                                 INSTALL_DATADIR="${SSL_CERT_DIR}"
499                                         )
500
501                 # Prefix the binary names with libwebsockets.
502                 set_target_properties(${TEST_NAME} 
503                         PROPERTIES
504                         OUTPUT_NAME libwebsockets-${TEST_NAME})
505
506                 # Add to the list of tests.
507                 list(APPEND TEST_APP_LIST ${TEST_NAME})
508         endmacro()
509
510         if (WITH_SSL AND NOT USE_CYASSL)
511                 message("Searching for OpenSSL executable and dlls")
512                 find_package(OpenSSLbins)
513                 message("OpenSSL executable: ${OPENSSL_EXECUTABLE}")
514         endif()
515
516         if (NOT WITHOUT_SERVER)
517                 #
518                 # test-server
519                 #
520                 if (NOT WITHOUT_TEST_SERVER)
521                         create_test_app(test-server
522                                 "test-server/test-server.c"
523                                 ""
524                                 "${WIN32_HELPERS_PATH}/netdb.h;${WIN32_HELPERS_PATH}/strings.h;${WIN32_HELPERS_PATH}/unistd.h;${WIN32_HELPERS_PATH}/websock-w32.h")
525                 endif()
526
527                 #
528                 # test-server-extpoll
529                 #
530                 if (NOT WITHOUT_TEST_SERVER_EXTPOLL)
531                         create_test_app(test-server-extpoll
532                                 "test-server/test-server.c"
533                                 "win32port/win32helpers/websock-w32.c"
534                                 "${WIN32_HELPERS_PATH}/netdb.h;${WIN32_HELPERS_PATH}/strings.h;${WIN32_HELPERS_PATH}/unistd.h;${WIN32_HELPERS_PATH}/websock-w32.h")
535                         # Set defines for this executable only.
536                         set_property(
537                                 TARGET test-server-extpoll
538                                 PROPERTY COMPILE_DEFINITIONS 
539                                         EXTERNAL_POLL 
540                                         INSTALL_DATADIR="${SSL_CERT_DIR}"
541                                 )
542
543                         # We need to link against winsock code.
544                         if (WIN32)
545                                 target_link_libraries(test-server-extpoll ws2_32.lib)
546                         endif()
547                 endif()
548
549                 # Data files for running the test server.
550                 set(TEST_SERVER_DATA
551                         ${PROJECT_SOURCE_DIR}/test-server/favicon.ico 
552                         ${PROJECT_SOURCE_DIR}/test-server/leaf.jpg
553                         ${PROJECT_SOURCE_DIR}/test-server/libwebsockets.org-logo.png
554                         ${PROJECT_SOURCE_DIR}/test-server/test.html)
555
556                 # Generate self-signed SSL certs for the test-server.
557                 if (WITH_SSL AND OPENSSL_EXECUTABLE)
558                         message("Generating SSL Certificates for the test-server...")
559
560                         set(TEST_SERVER_SSL_KEY ${PROJECT_BINARY_DIR}/libwebsockets-test-server.key.pem)
561                         set(TEST_SERVER_SSL_CERT ${PROJECT_BINARY_DIR}/libwebsockets-test-server.pem)
562
563                         if (WIN32)
564                                 file(WRITE ${PROJECT_BINARY_DIR}/openssl_input.txt
565                                         "GB\n"
566                                         "Erewhon\n"
567                                         "All around\n"
568                                         "libwebsockets-test\n"
569                                         "localhost\n"
570                                         "none@invalid.org\n\n"
571                                         )
572                                 
573                                 # The "type" command is a bit picky with paths.
574                                 file(TO_NATIVE_PATH "${PROJECT_BINARY_DIR}/openssl_input.txt" OPENSSL_INPUT_WIN_PATH)
575
576                                 execute_process(
577                                         COMMAND cmd /c type "${OPENSSL_INPUT_WIN_PATH}"
578                                         COMMAND "${OPENSSL_EXECUTABLE}" req -new -newkey rsa:1024 -days 10000 -nodes -x509 -keyout "${TEST_SERVER_SSL_KEY}" -out "${TEST_SERVER_SSL_CERT}"
579                                         RESULT_VARIABLE OPENSSL_RETURN_CODE)
580                                 
581                                 message("\n")
582
583                                 if (OPENSSL_RETURN_CODE)
584                                         message("!!! Failed to generate SSL certificate:\n${OPENSSL_RETURN_CODE} !!!")
585                                 endif()
586                         else()
587                                 execute_process(
588                                         COMMAND printf "GB\\nErewhon\\nAll around\\nlibwebsockets-test\\n\\nlocalhost\\nnone@invalid.org\\n"
589                                         COMMAND ${OPENSSL_EXECUTABLE} 
590                                                 req -new -newkey rsa:1024 -days 10000 -nodes -x509 -keyout ${TEST_SERVER_SSL_KEY} -out ${TEST_SERVER_SSL_CERT}
591                                         )
592                         endif()
593
594                         list(APPEND TEST_SERVER_DATA 
595                                 ${TEST_SERVER_SSL_KEY} 
596                                 ${TEST_SERVER_SSL_CERT})
597                 endif()
598
599                 # Copy the file needed to run the server so that the test apps can
600                 # reach them from their default output location
601                 foreach (TEST_FILE ${TEST_SERVER_DATA})
602                         add_custom_command(TARGET test-server
603                                                 POST_BUILD 
604                                                 COMMAND ${CMAKE_COMMAND} -E make_directory "$<TARGET_FILE_DIR:test-server>/../share/libwebsockets-test-server"
605                                                 COMMAND ${CMAKE_COMMAND} -E copy ${TEST_FILE} "$<TARGET_FILE_DIR:test-server>/../share/libwebsockets-test-server" VERBATIM)
606                 endforeach()
607         endif(NOT WITHOUT_SERVER)
608
609         if (NOT WITHOUT_CLIENT)
610                 #
611                 # test-client
612                 #
613                 if (NOT WITHOUT_TEST_CLIENT)
614                         create_test_app(test-client
615                                 "test-server/test-client.c"
616                                 ""
617                                 "")
618                 endif()
619
620                 #
621                 # test-fraggle
622                 #
623                 if (NOT WITHOUT_TEST_FRAGGLE)
624                         create_test_app(test-fraggle
625                                 "test-server/test-fraggle.c"
626                                 ""
627                                 "${WIN32_HELPERS_PATH}/unistd.h;${WIN32_HELPERS_PATH}/sys/time.h")
628                 endif()
629
630                 #
631                 # test-ping
632                 #
633                 if (NOT WITHOUT_TEST_PING)
634                         create_test_app(test-ping
635                                 "test-server/test-ping.c"
636                                 ""
637                                 "${WIN32_HELPERS_PATH}/unistd.h;${WIN32_HELPERS_PATH}/sys/time.h")
638                 endif()
639         endif(NOT WITHOUT_CLIENT)
640
641         #
642         # Copy OpenSSL dlls to the output directory on Windows.
643         # (Otherwise we'll get an error when trying to run)
644         #
645         if (WIN32 AND WITH_SSL AND NOT USE_CYASSL)
646                 if(OPENSSL_BIN_FOUND)
647                         message("OpenSSL dlls found:")
648                         message("  Libeay: ${LIBEAY_BIN}")
649                         message("  SSLeay: ${SSLEAY_BIN}")
650
651                         foreach(TARGET_BIN ${TEST_APP_LIST})                    
652                                 add_custom_command(TARGET ${TARGET_BIN}
653                                         POST_BUILD 
654                                         COMMAND ${CMAKE_COMMAND} -E copy ${LIBEAY_BIN} $<TARGET_FILE_DIR:${TARGET_BIN}> VERBATIM)
655                                         
656                                 add_custom_command(TARGET ${TARGET_BIN}
657                                         POST_BUILD 
658                                         COMMAND ${CMAKE_COMMAND} -E copy ${SSLEAY_BIN} $<TARGET_FILE_DIR:${TARGET_BIN}> VERBATIM)
659                         endforeach()
660                 endif()
661         endif()
662 endif(NOT WITHOUT_TESTAPPS)
663
664 if (UNIX)
665         # Generate documentation.
666         # TODO: Fix this on Windows.
667         message("Generating API documentation")
668         file(GLOB C_FILES ${PROJECT_SOURCE_DIR}/lib/*.c)
669         execute_process(COMMAND ${CMAKE_COMMAND} -E make_directory ${PROJECT_BINARY_DIR}/doc/)
670
671         execute_process(
672                 COMMAND ${PROJECT_SOURCE_DIR}/scripts/kernel-doc -html ${C_FILES} ${HDR_PUBLIC} 
673                 OUTPUT_FILE ${PROJECT_BINARY_DIR}/doc/libwebsockets-api-doc.html
674                 ERROR_QUIET)
675
676         execute_process(
677                 COMMAND ${PROJECT_SOURCE_DIR}/scripts/kernel-doc -text ${C_FILES} ${HDR_PUBLIC}
678                 OUTPUT_FILE ${PROJECT_BINARY_DIR}/doc/libwebsockets-api-doc.txt
679                 ERROR_QUIET)
680
681 # Generate and install pkgconfig.
682 # (This is not indented, because the tabs will be part of the output)
683 file(WRITE ${PROJECT_BINARY_DIR}/libwebsockets.pc
684 "prefix=/usr/local
685 exec_prefix=\${prefix}
686 libdir=\${exec_prefix}/lib${LIB_SUFFIX}
687 includedir=\${prefix}/include
688
689 Name: libwebsockets
690 Description: Websockets server and client library
691 Version: ${PACKAGE_VERSION}
692
693 Libs: -L\${libdir} -lwebsockets
694 Cflags: -I\${includedir}"
695 )
696
697         install(FILES ${PROJECT_BINARY_DIR}/libwebsockets.pc
698                 DESTINATION include/pkgconfig)
699 endif()
700
701 # Install headers.
702 install(FILES ${HDR_PUBLIC} 
703                 DESTINATION include
704                 COMPONENT headers)
705 set(CPACK_COMPONENT_HEADERS_DISPLAY_NAME "Header files")
706
707 # Install libs.
708 install(TARGETS websockets websockets_shared
709                 LIBRARY DESTINATION lib${LIB_SUFFIX}
710                 ARCHIVE DESTINATION lib${LIB_SUFFIX}
711                 COMPONENT libraries)
712 set(CPACK_COMPONENT_LIBRARIES_DISPLAY_NAME "Libraries")
713
714 # Install test apps.
715 if (NOT WITHOUT_TESTAPPS)
716         install(TARGETS test-client ${TEST_APP_LIST}
717                         RUNTIME DESTINATION bin
718                         COMPONENT examples)
719         set(CPACK_COMPONENT_EXAMPLES_DISPLAY_NAME "Example Install")
720 endif()
721
722 # Programs shared files used by the test-server.
723 if (NOT WITHOUT_TESTAPPS AND NOT WITHOUT_SERVER)
724         install(FILES ${TEST_SERVER_DATA}
725                         DESTINATION share/libwebsockets-test-server
726                         COMPONENT examples)
727 endif()
728
729 # Most people are more used to "make dist" compared to "make package_source"
730 add_custom_target(dist COMMAND ${CMAKE_MAKE_PROGRAM} package_source)
731
732 # This must always be last!
733 include(CPack)