cmake lib lib64 problem
[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
278         list(APPEND SOURCES 
279                 ${WIN32_HELPERS_PATH}/websock-w32.c
280                 ${WIN32_HELPERS_PATH}/gettimeofday.c
281                 )
282
283         include_directories(${WIN32_HELPERS_PATH})
284 else()
285         # Unix.
286         if (NOT WITHOUT_DAEMONIZE)
287                 list(APPEND SOURCES
288                         lib/daemonize.c
289                         )
290         endif()
291 endif()
292
293 if (UNIX)
294         if (NOT HAVE_GETIFADDRS)
295                 list(APPEND HDR_PRIVATE lib/getifaddrs.h)
296                 list(APPEND SOURCES lib/getifaddrs.c)
297         endif()
298 endif()
299
300 source_group("Headers Private"  FILES ${HDR_PRIVATE})
301 source_group("Headers Public"   FILES ${HDR_PUBLIC})
302 source_group("Sources"          FILES ${SOURCES})
303
304 #
305 # Create the lib.
306 #
307 add_library(websockets STATIC
308                         ${HDR_PRIVATE}
309                         ${HDR_PUBLIC}
310                         ${SOURCES})
311 add_library(websockets_shared SHARED
312                         ${HDR_PRIVATE}
313                         ${HDR_PUBLIC}
314                         ${SOURCES})
315
316 if (WIN32)
317         # On Windows libs have the same file ending (.lib)
318         # for both static and shared libraries, so we
319         # need a unique name for the static one.
320         set_target_properties(websockets 
321                 PROPERTIES
322                 OUTPUT_NAME websockets_static)
323
324         # Compile as DLL (export function declarations)
325         set_property(
326                 TARGET websockets_shared
327                 PROPERTY COMPILE_DEFINITIONS 
328                 LWS_DLL
329                 LWS_INTERNAL
330                 )
331 endif()
332
333 # We want the shared lib to be named "libwebsockets"
334 # not "libwebsocket_shared".
335 set_target_properties(websockets_shared
336                 PROPERTIES 
337                 OUTPUT_NAME websockets)
338
339 # Set the so version of the lib.
340 # Equivalent to LDFLAGS=-version-info 3:0:0
341 if(CMAKE_COMPILER_IS_GNUCC OR CMAKE_COMPILER_IS_GNUCXX)
342         foreach(lib websockets websockets_shared)
343                 set_target_properties(${lib} 
344                         PROPERTIES
345                         SOVERSION ${SOVERSION})
346         endforeach()
347 endif()
348
349 set(LIB_LIST)
350
351 #
352 # Find libraries.
353 #
354
355 #
356 # ZLIB (Only needed for deflate extensions).
357 #
358 if (NOT WITHOUT_EXTENSIONS)
359         if (WIN32 AND NOT USE_EXTERNAL_ZLIB)
360                 message("Using included Zlib version")
361
362                 # Compile ZLib if needed.
363                 set(WIN32_ZLIB_PATH "win32port/zlib")
364                 set(ZLIB_SRCS
365                         ${WIN32_ZLIB_PATH}/adler32.c
366                         ${WIN32_ZLIB_PATH}/compress.c
367                         ${WIN32_ZLIB_PATH}/crc32.c
368                         ${WIN32_ZLIB_PATH}/deflate.c
369                         ${WIN32_ZLIB_PATH}/gzclose.c
370                         ${WIN32_ZLIB_PATH}/gzio.c
371                         ${WIN32_ZLIB_PATH}/gzlib.c
372                         ${WIN32_ZLIB_PATH}/gzread.c
373                         ${WIN32_ZLIB_PATH}/gzwrite.c
374                         ${WIN32_ZLIB_PATH}/infback.c
375                         ${WIN32_ZLIB_PATH}/inffast.c
376                         ${WIN32_ZLIB_PATH}/inflate.c
377                         ${WIN32_ZLIB_PATH}/inftrees.c
378                         ${WIN32_ZLIB_PATH}/trees.c
379                         ${WIN32_ZLIB_PATH}/uncompr.c
380                         ${WIN32_ZLIB_PATH}/zutil.c
381                 )
382
383                 # Create the library.
384                 add_library(ZLIB STATIC ${ZLIB_SRCS})
385
386                 # Set the same variables as find_package would.
387                 set(ZLIB_INCLUDE_DIRS ${WIN32_ZLIB_PATH})
388                 get_property(ZLIB_LIBRARIES TARGET ZLIB PROPERTY LOCATION)
389                 set(ZLIB_FOUND 1)
390         else()
391                 find_package(ZLIB REQUIRED)
392         endif()
393
394         # Make sure ZLib is compiled before the libs.
395         foreach (lib websockets websockets_shared)
396                 add_dependencies(${lib} ZLIB)
397         endforeach()
398
399         message("ZLib include dirs: ${ZLIB_INCLUDE_DIRS}")
400         message("ZLib libraries: ${ZLIB_LIBRARIES}")
401         include_directories(${ZLIB_INCLUDE_DIRS})
402         list(APPEND LIB_LIST ${ZLIB_LIBRARIES})
403 endif(NOT WITHOUT_EXTENSIONS)
404
405 #
406 # OpenSSL
407 #
408 if (WITH_SSL)
409         message("Compiling with SSL support")
410
411         if (USE_CYASSL)
412                 # Use CyaSSL as OpenSSL replacement.
413                 # TODO: Add a find_package command for this also.
414                 message("CyaSSL include dir: ${CYASSL_INCLUDE_DIRS}")
415                 message("CyaSSL libraries: ${CYASSL_LIB}")
416
417                 # Additional to the root directory we need to include
418                 # the cyassl/ subdirectory which contains the OpenSSL
419                 # compatability layer headers.
420                 foreach(inc ${CYASSL_INCLUDE_DIRS})
421                         include_directories(${inc} ${inc}/cyassl)
422                 endforeach()
423
424                 list(APPEND ${LIB_LIST} ${CYASSL_LIB})
425         else()
426                 # TODO: Add support for STATIC also.
427                 find_package(OpenSSL REQUIRED)
428
429                 message("OpenSSL include dir: ${OPENSSL_INCLUDE_DIR}")
430                 message("OpenSSL libraries: ${OPENSSL_LIBRARIES}")
431
432                 include_directories(${OPENSSL_INCLUDE_DIR})
433                 list(APPEND LIB_LIST ${OPENSSL_LIBRARIES})
434         endif()
435 endif(WITH_SSL)
436
437 #
438 # Platform specific libs.
439 #
440 if (WIN32)
441         list(APPEND LIB_LIST ws2_32.lib)
442 endif()
443
444 if (UNIX)
445         list(APPEND LIB_LIST m)
446 endif()
447
448 # Setup the linking for all libs.
449 foreach (lib websockets websockets_shared)
450         target_link_libraries(${lib} ${LIB_LIST})
451 endforeach()
452
453 #
454 # Test applications
455 #
456 set(TEST_APP_LIST)
457 if (NOT WITHOUT_TESTAPPS)
458         #
459         # Helper function for adding a test app.
460         #
461         macro(create_test_app TEST_NAME MAIN_SRC WIN32_SRCS WIN32_HDRS)
462                 
463                 set(TEST_SRCS ${MAIN_SRC})
464                 set(TEST_HDR)
465
466                 if (WIN32)
467                         list(APPEND TEST_SRCS 
468                                 ${WIN32_HELPERS_PATH}/getopt.c
469                                 ${WIN32_HELPERS_PATH}/getopt_long.c
470                                 ${WIN32_HELPERS_PATH}/gettimeofday.c
471                                 ${WIN32_SRCS})
472
473                         list(APPEND TEST_HDR 
474                                 ${WIN32_HELPERS_PATH}/getopt.h
475                                 ${WIN32_HELPERS_PATH}/gettimeofday.h
476                                 ${WIN32_HDRS})
477                 endif(WIN32)
478
479                 source_group("Headers Private"   FILES ${TEST_HDR})
480                 source_group("Sources"   FILES ${TEST_SRCS})
481                 add_executable(${TEST_NAME} ${TEST_SRCS} ${TEST_HDR})
482                 
483                 if (LINK_TESTAPPS_DYNAMIC)
484                         target_link_libraries(${TEST_NAME} websockets_shared)
485                         add_dependencies(${TEST_NAME} websockets_shared)
486                 else()
487                         target_link_libraries(${TEST_NAME} websockets)
488                         add_dependencies(${TEST_NAME} websockets)
489                 endif()
490
491                 # Set test app specific defines.
492                 set_property(TARGET ${TEST_NAME}
493                                         PROPERTY COMPILE_DEFINITIONS 
494                                                 INSTALL_DATADIR="${SSL_CERT_DIR}"
495                                         )
496
497                 # Prefix the binary names with libwebsockets.
498                 set_target_properties(${TEST_NAME} 
499                         PROPERTIES
500                         OUTPUT_NAME libwebsockets-${TEST_NAME})
501
502                 # Add to the list of tests.
503                 list(APPEND TEST_APP_LIST ${TEST_NAME})
504         endmacro()
505
506         if (WITH_SSL AND NOT USE_CYASSL)
507                 message("Searching for OpenSSL executable and dlls")
508                 find_package(OpenSSLbins)
509                 message("OpenSSL executable: ${OPENSSL_EXECUTABLE}")
510         endif()
511
512         if (NOT WITHOUT_SERVER)
513                 #
514                 # test-server
515                 #
516                 if (NOT WITHOUT_TEST_SERVER)
517                         create_test_app(test-server
518                                 "test-server/test-server.c"
519                                 ""
520                                 "${WIN32_HELPERS_PATH}/netdb.h;${WIN32_HELPERS_PATH}/strings.h;${WIN32_HELPERS_PATH}/unistd.h;${WIN32_HELPERS_PATH}/websock-w32.h")
521                 endif()
522
523                 #
524                 # test-server-extpoll
525                 #
526                 if (NOT WITHOUT_TEST_SERVER_EXTPOLL)
527                         create_test_app(test-server-extpoll
528                                 "test-server/test-server.c"
529                                 "win32port/win32helpers/websock-w32.c"
530                                 "${WIN32_HELPERS_PATH}/netdb.h;${WIN32_HELPERS_PATH}/strings.h;${WIN32_HELPERS_PATH}/unistd.h;${WIN32_HELPERS_PATH}/websock-w32.h")
531                         # Set defines for this executable only.
532                         set_property(
533                                 TARGET test-server-extpoll
534                                 PROPERTY COMPILE_DEFINITIONS 
535                                         EXTERNAL_POLL 
536                                         INSTALL_DATADIR="${SSL_CERT_DIR}"
537                                 )
538
539                         # We need to link against winsock code.
540                         if (WIN32)
541                                 target_link_libraries(test-server-extpoll ws2_32.lib)
542                         endif()
543                 endif()
544
545                 # Data files for running the test server.
546                 set(TEST_SERVER_DATA
547                         ${PROJECT_SOURCE_DIR}/test-server/favicon.ico 
548                         ${PROJECT_SOURCE_DIR}/test-server/leaf.jpg
549                         ${PROJECT_SOURCE_DIR}/test-server/libwebsockets.org-logo.png
550                         ${PROJECT_SOURCE_DIR}/test-server/test.html)
551
552                 # Generate self-signed SSL certs for the test-server.
553                 if (WITH_SSL AND OPENSSL_EXECUTABLE)
554                         message("Generating SSL Certificates for the test-server...")
555
556                         set(TEST_SERVER_SSL_KEY ${PROJECT_BINARY_DIR}/libwebsockets-test-server.key.pem)
557                         set(TEST_SERVER_SSL_CERT ${PROJECT_BINARY_DIR}/libwebsockets-test-server.pem)
558
559                         if (WIN32)
560                                 file(WRITE ${PROJECT_BINARY_DIR}/openssl_input.txt
561                                         "GB\n"
562                                         "Erewhon\n"
563                                         "All around\n"
564                                         "libwebsockets-test\n"
565                                         "localhost\n"
566                                         "none@invalid.org\n\n"
567                                         )
568                                 
569                                 # The "type" command is a bit picky with paths.
570                                 file(TO_NATIVE_PATH "${PROJECT_BINARY_DIR}/openssl_input.txt" OPENSSL_INPUT_WIN_PATH)
571
572                                 execute_process(
573                                         COMMAND cmd /c type "${OPENSSL_INPUT_WIN_PATH}"
574                                         COMMAND "${OPENSSL_EXECUTABLE}" req -new -newkey rsa:1024 -days 10000 -nodes -x509 -keyout "${TEST_SERVER_SSL_KEY}" -out "${TEST_SERVER_SSL_CERT}"
575                                         RESULT_VARIABLE OPENSSL_RETURN_CODE)
576                                 
577                                 message("\n")
578
579                                 if (OPENSSL_RETURN_CODE)
580                                         message("!!! Failed to generate SSL certificate:\n${OPENSSL_RETURN_CODE} !!!")
581                                 endif()
582                         else()
583                                 execute_process(
584                                         COMMAND printf "GB\\nErewhon\\nAll around\\nlibwebsockets-test\\n\\nlocalhost\\nnone@invalid.org\\n"
585                                         COMMAND ${OPENSSL_EXECUTABLE} 
586                                                 req -new -newkey rsa:1024 -days 10000 -nodes -x509 -keyout ${TEST_SERVER_SSL_KEY} -out ${TEST_SERVER_SSL_CERT}
587                                         )
588                         endif()
589
590                         list(APPEND TEST_SERVER_DATA 
591                                 ${TEST_SERVER_SSL_KEY} 
592                                 ${TEST_SERVER_SSL_CERT})
593                 endif()
594
595                 # Copy the file needed to run the server so that the test apps can
596                 # reach them from their default output location
597                 foreach (TEST_FILE ${TEST_SERVER_DATA})
598                         add_custom_command(TARGET test-server
599                                                 POST_BUILD 
600                                                 COMMAND ${CMAKE_COMMAND} -E make_directory "$<TARGET_FILE_DIR:test-server>/../share/libwebsockets-test-server"
601                                                 COMMAND ${CMAKE_COMMAND} -E copy ${TEST_FILE} "$<TARGET_FILE_DIR:test-server>/../share/libwebsockets-test-server" VERBATIM)
602                 endforeach()
603         endif(NOT WITHOUT_SERVER)
604
605         if (NOT WITHOUT_CLIENT)
606                 #
607                 # test-client
608                 #
609                 if (NOT WITHOUT_TEST_CLIENT)
610                         create_test_app(test-client
611                                 "test-server/test-client.c"
612                                 ""
613                                 "")
614                 endif()
615
616                 #
617                 # test-fraggle
618                 #
619                 if (NOT WITHOUT_TEST_FRAGGLE)
620                         create_test_app(test-fraggle
621                                 "test-server/test-fraggle.c"
622                                 ""
623                                 "${WIN32_HELPERS_PATH}/unistd.h;${WIN32_HELPERS_PATH}/sys/time.h")
624                 endif()
625
626                 #
627                 # test-ping
628                 #
629                 if (NOT WITHOUT_TEST_PING)
630                         create_test_app(test-ping
631                                 "test-server/test-ping.c"
632                                 ""
633                                 "${WIN32_HELPERS_PATH}/unistd.h;${WIN32_HELPERS_PATH}/sys/time.h")
634                 endif()
635         endif(NOT WITHOUT_CLIENT)
636
637         #
638         # Copy OpenSSL dlls to the output directory on Windows.
639         # (Otherwise we'll get an error when trying to run)
640         #
641         if (WIN32 AND WITH_SSL AND NOT USE_CYASSL)
642                 if(OPENSSL_BIN_FOUND)
643                         message("OpenSSL dlls found:")
644                         message("  Libeay: ${LIBEAY_BIN}")
645                         message("  SSLeay: ${SSLEAY_BIN}")
646
647                         foreach(TARGET_BIN ${TEST_APP_LIST})                    
648                                 add_custom_command(TARGET ${TARGET_BIN}
649                                         POST_BUILD 
650                                         COMMAND ${CMAKE_COMMAND} -E copy ${LIBEAY_BIN} $<TARGET_FILE_DIR:${TARGET_BIN}> VERBATIM)
651                                         
652                                 add_custom_command(TARGET ${TARGET_BIN}
653                                         POST_BUILD 
654                                         COMMAND ${CMAKE_COMMAND} -E copy ${SSLEAY_BIN} $<TARGET_FILE_DIR:${TARGET_BIN}> VERBATIM)
655                         endforeach()
656                 endif()
657         endif()
658 endif(NOT WITHOUT_TESTAPPS)
659
660 if (UNIX)
661         # Generate documentation.
662         # TODO: Fix this on Windows.
663         message("Generating API documentation")
664         file(GLOB C_FILES ${PROJECT_SOURCE_DIR}/lib/*.c)
665         execute_process(COMMAND ${CMAKE_COMMAND} -E make_directory ${PROJECT_BINARY_DIR}/doc/)
666
667         execute_process(
668                 COMMAND ${PROJECT_SOURCE_DIR}/scripts/kernel-doc -html ${C_FILES} ${HDR_PUBLIC} 
669                 OUTPUT_FILE ${PROJECT_BINARY_DIR}/doc/libwebsockets-api-doc.html
670                 ERROR_QUIET)
671
672         execute_process(
673                 COMMAND ${PROJECT_SOURCE_DIR}/scripts/kernel-doc -text ${C_FILES} ${HDR_PUBLIC}
674                 OUTPUT_FILE ${PROJECT_BINARY_DIR}/doc/libwebsockets-api-doc.txt
675                 ERROR_QUIET)
676
677 # Generate and install pkgconfig.
678 # (This is not indented, because the tabs will be part of the output)
679 file(WRITE ${PROJECT_BINARY_DIR}/libwebsockets.pc
680 "prefix=/usr/local
681 exec_prefix=\${prefix}
682 libdir=\${exec_prefix}/lib${LIB_SUFFIX}
683 includedir=\${prefix}/include
684
685 Name: libwebsockets
686 Description: Websockets server and client library
687 Version: ${PACKAGE_VERSION}
688
689 Libs: -L\${libdir} -lwebsockets
690 Cflags: -I\${includedir}"
691 )
692
693         install(FILES ${PROJECT_BINARY_DIR}/libwebsockets.pc
694                 DESTINATION include/pkgconfig)
695 endif()
696
697 # Install headers.
698 install(FILES ${HDR_PUBLIC} 
699                 DESTINATION include
700                 COMPONENT headers)
701 set(CPACK_COMPONENT_HEADERS_DISPLAY_NAME "Header files")
702
703 # Install libs.
704 install(TARGETS websockets websockets_shared
705                 LIBRARY DESTINATION lib${LIB_SUFFIX}
706                 ARCHIVE DESTINATION lib${LIB_SUFFIX}
707                 COMPONENT libraries)
708 set(CPACK_COMPONENT_LIBRARIES_DISPLAY_NAME "Libraries")
709
710 # Install test apps.
711 if (NOT WITHOUT_TESTAPPS)
712         install(TARGETS test-client ${TEST_APP_LIST}
713                         RUNTIME DESTINATION bin
714                         COMPONENT examples)
715         set(CPACK_COMPONENT_EXAMPLES_DISPLAY_NAME "Example Install")
716 endif()
717
718 # Programs shared files used by the test-server.
719 if (NOT WITHOUT_TESTAPPS AND NOT WITHOUT_SERVER)
720         install(FILES ${TEST_SERVER_DATA}
721                         DESTINATION share/libwebsockets-test-server
722                         COMPONENT examples)
723 endif()
724
725 # Most people are more used to "make dist" compared to "make package_source"
726 add_custom_target(dist COMMAND ${CMAKE_MAKE_PROGRAM} package_source)
727
728 # This must always be last!
729 include(CPack)