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