trim proxy ads plus hostname stg
[profile/ivi/libwebsockets.git] / CMakeLists.txt
1 cmake_minimum_required(VERSION 2.6)
2
3 project(libwebsockets)
4
5 set(PACKAGE "libwebsockets")
6 set(PACKAGE_VERSION "1.2")
7 set(PACKAGE_BUGREPORT "andy@warmcat.com")
8 set(PACKAGE_NAME "${PACKAGE}")
9 set(PACKAGE_STRING "${PACKAGE} ${PACKAGE_VERSION}")
10 set(PACKAGE_TARNAME "${PACKAGE}")
11 set(PACKAGE_URL "http://libwebsockets.org")
12 set(VERSION "{PACKAGE_VERSION}")
13 set(SOVERSION "3.0.0")
14
15 set(LWS_LIBRARY_VERSION ${PACKAGE_VERSION})
16
17 # Try to find the current Git hash.
18 find_package(Git)
19 if(GIT_EXECUTABLE)
20         execute_process(
21     COMMAND "${GIT_EXECUTABLE}" log -n 1 --pretty=%h
22     OUTPUT_VARIABLE GIT_HASH
23     OUTPUT_STRIP_TRAILING_WHITESPACE
24     )
25
26     set(LWS_BUILD_HASH ${GIT_HASH})
27     message("Git commit hash: ${LWS_BUILD_HASH}")
28 endif()
29
30 option(WITH_SSL "Include SSL support (default OpenSSL, CyaSSL if USE_CYASSL is set)" ON)
31 option(USE_EXTERNAL_ZLIB "Search the system for ZLib instead of using the included one (on Windows)" OFF)
32 option(USE_CYASSL "Use CyaSSL replacement for OpenSSL. When settings this, you also need to specify CYASSL_LIB and CYASSL_INCLUDE_DIRS" OFF)
33 option(WITH_BUILTIN_GETIFADDRS "Use BSD getifaddrs implementation from libwebsockets... default is your libc provides it" OFF)
34 option(WITHOUT_TESTAPPS "Don't build the libwebsocket-test-apps" OFF)
35 option(WITHOUT_CLIENT "Don't build the client part of the library" OFF)
36 option(WITHOUT_SERVER "Don't build the server part of the library" OFF)
37 option(WITHOUT_SERVER_EXTPOLL "Don't build a server version that uses external poll" OFF)
38 option(WITH_LIBCRYPTO "Use libcrypto MD5 and SHA1 implementations" ON)
39 option(WITHOUT_PING "Don't build the ping test application" OFF)
40 option(WITHOUT_DEBUG "Don't compile debug related code" OFF)
41 option(WITHOUT_EXTENSIONS "Don't compile with extensions" OFF)
42 option(WITH_LATENCY "Build latency measuring code into the library" OFF)
43 option(WITHOUT_DAEMONIZE "Don't build the daemonization api" OFF)
44
45 # The base dir where the SSL dirs should be looked for.
46 set(SSL_CERT_DIR CACHE STRING "")
47 set(SSL_CLIENT_CERT_DIR CACHE STRING "")
48
49 if ("${SSL_CERT_DIR}" STREQUAL "")
50         set(SSL_CERT_DIR ".")
51 endif()
52
53 if ("${SSL_CLIENT_CERT_DIR}" STREQUAL "")
54         set(LWS_OPENSSL_CLIENT_CERTS ".")
55 else()
56         set(LWS_OPENSSL_CLIENT_CERTS "${SSL_CLIENT_CERT_DIR}")
57 endif()
58
59 set(CYASSL_LIB CACHE STRING "")
60 set(CYASSL_INCLUDE_DIRS CACHE STRING "")
61
62 if (USE_CYASSL)
63         if ("${CYASSL_LIB}" STREQUAL "" OR "${CYASSL_INCLUDE_DIRS}" STREQUAL "")
64                 message(FATAL_ERROR "You must set CYASSL_LIB and CYASSL_INCLUDE_DIRS when USE_CYASSL is turned on")
65         endif()
66 endif()
67
68 if (WITHOUT_EXTENSIONS)
69         set(LWS_NO_EXTENSIONS 1)
70 endif()
71
72 if (WITH_SSL)
73         set(LWS_OPENSSL_SUPPORT 1)
74 endif()
75
76 if (WITH_LATENCY)
77         set(LWS_LATENCY 1)
78 endif()
79
80 if (WITHOUT_DAEMONIZE)
81         set(LWS_NO_DAEMONIZE 1)
82 endif()
83
84 if (WITHOUT_SERVER)
85         set(LWS_NO_SERVER 1)
86 endif()
87
88 if (WITHOUT_CLIENT)
89         set(LWS_NO_CLIENT 1)
90 endif()
91
92 if (MINGW)
93         set(LWS_MINGW_SUPPORT 1)
94 endif()
95
96 set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${PROJECT_SOURCE_DIR}/cmake/")
97 include_directories(${PROJECT_BINARY_DIR})
98
99 include(CheckCSourceCompiles)
100
101 # Check for different inline keyword versions.
102 foreach(KEYWORD "inline" "__inline__" "__inline")
103         set(CMAKE_REQUIRED_DEFINITIONS "-DKEYWORD=${KEYWORD}")
104         CHECK_C_SOURCE_COMPILES(
105                 "
106                 #include <stdio.h>
107                 KEYWORD void a() {}
108                 int main(int argc, char **argv) { a(); return 0; }
109                 " HAVE_${KEYWORD})
110 endforeach()
111
112 if (NOT HAVE_inline)
113         if (HAVE___inline__)
114                 set(inline __inline__)
115         elseif(HAVE___inline)
116                 set(inline __inline)
117         endif()
118 endif()
119
120 # Put the libaries and binaries that get built into directories at the
121 # top of the build tree rather than in hard-to-find leaf directories. 
122 SET(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${PROJECT_BINARY_DIR}/bin)
123 SET(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${PROJECT_BINARY_DIR}/lib)
124 SET(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${PROJECT_BINARY_DIR}/lib)
125
126 # So we can include the CMake generated config file only when
127 # building with CMAKE.
128 add_definitions(-DCMAKE_BUILD)
129
130 include(CheckFunctionExists)
131 include(CheckIncludeFile)
132 include(CheckIncludeFiles)
133 include(CheckLibraryExists)
134
135 CHECK_FUNCTION_EXISTS(bzero HAVE_BZERO)
136 CHECK_FUNCTION_EXISTS(fork HAVE_FORK)
137 CHECK_FUNCTION_EXISTS(malloc HAVE_MALLOC)
138 CHECK_FUNCTION_EXISTS(memset HAVE_MEMSET)
139 CHECK_FUNCTION_EXISTS(realloc HAVE_REALLOC)
140 CHECK_FUNCTION_EXISTS(socket HAVE_SOCKET)
141 CHECK_FUNCTION_EXISTS(strerror HAVE_STRERROR)
142 CHECK_FUNCTION_EXISTS(vfork HAVE_VFORK)
143 CHECK_FUNCTION_EXISTS(getifaddrs HAVE_GETIFADDRS)
144
145 if (WITH_BUILTIN_GETIFADDRS)
146         if (HAVE_GETIFADDRS)
147                 warning("getifaddrs already exists on the system, are you sure you want to build using the BSD version? (This is normally only needed on systems running uclibc)")
148         endif()
149         set(LWS_BUILTIN_GETIFADDRS 1)
150 endif()
151
152 CHECK_INCLUDE_FILE(dlfcn.h HAVE_DLFCN_H)
153 CHECK_INCLUDE_FILE(fcntl.h HAVE_FCNTL_H)
154 CHECK_INCLUDE_FILE(inttypes.h HAVE_INTTYPES_H)
155 CHECK_INCLUDE_FILE(memory.h HAVE_MEMORY_H)
156 CHECK_INCLUDE_FILE(netinet/in.h HAVE_NETINET_IN_H)
157 CHECK_INCLUDE_FILE(stdint.h HAVE_STDINT_H)
158 CHECK_INCLUDE_FILE(stdlib.h HAVE_STDLIB_H)
159 CHECK_INCLUDE_FILE(strings.h HAVE_STRINGS_H)
160 CHECK_INCLUDE_FILE(string.h HAVE_STRING_H)
161 CHECK_INCLUDE_FILE(sys/prctl.h HAVE_SYS_PRCTL_H)
162 CHECK_INCLUDE_FILE(sys/socket.h HAVE_SYS_SOCKET_H)
163 CHECK_INCLUDE_FILE(sys/stat.h HAVE_SYS_STAT_H)
164 CHECK_INCLUDE_FILE(sys/types.h HAVE_SYS_TYPES_H)
165 CHECK_INCLUDE_FILE(unistd.h HAVE_UNISTD_H)
166 CHECK_INCLUDE_FILE(vfork.h HAVE_VFORK_H)
167 CHECK_INCLUDE_FILE(zlib.h HAVE_ZLIB_H)
168
169 # TODO: These can be tested if they actually work also...
170 set(HAVE_WORKING_FORK HAVE_FORK)
171 set(HAVE_WORKING_VFORK HAVE_VFORK)
172
173 CHECK_INCLUDE_FILES("stdlib.h;stdarg.h;string.h;float.h" STDC_HEADERS)
174
175 if (NOT HAVE_SYS_TYPES_H)
176         set(pid_t int)
177         set(size_t "unsigned int")
178 endif()
179
180 if (NOT HAVE_MALLOC)
181         set(malloc rpl_malloc)
182 endif()
183
184 if (NOT HAVE_REALLOC)
185         set(realloc rpl_realloc)
186 endif()
187
188 # Generate the config.h that includes all the compilation settings.
189 configure_file(
190                 ${PROJECT_SOURCE_DIR}/config.h.cmake 
191                 ${PROJECT_BINARY_DIR}/lws_config.h)
192
193 if (MSVC)
194         # Turn off stupid microsoft security warnings.
195         add_definitions(-D_CRT_SECURE_NO_DEPRECATE -D_CRT_NONSTDC_NO_DEPRECATE)
196 endif()
197
198 include_directories(${PROJECT_SOURCE_DIR}/lib)
199
200 # Group headers and sources.
201 # Some IDEs use this for nicer file structure.
202 set(HDR_PRIVATE
203         lib/private-libwebsockets.h
204         lib/extension-deflate-frame.h
205         lib/extension-deflate-stream.h
206         ${PROJECT_BINARY_DIR}/lws_config.h
207         )
208
209 set(HDR_PUBLIC  
210         lib/libwebsockets.h
211         )
212
213 set(SOURCES
214         lib/base64-decode.c
215         lib/client.c
216         lib/client-handshake.c
217         lib/client-parser.c
218         lib/extension.c
219         lib/extension-deflate-frame.c
220         lib/extension-deflate-stream.c
221         lib/handshake.c
222         lib/libwebsockets.c
223         lib/output.c
224         lib/parsers.c
225         lib/server.c
226         lib/server-handshake.c
227         lib/sha-1.c
228         )
229
230 # Add helper files for Windows.
231 if (WIN32)
232         set(WIN32_HELPERS_PATH win32port/win32helpers)
233
234         list(APPEND HDR_PRIVATE
235                 ${WIN32_HELPERS_PATH}/websock-w32.h
236                 ${WIN32_HELPERS_PATH}/gettimeofday.h
237                 )
238
239         list(APPEND SOURCES 
240                 ${WIN32_HELPERS_PATH}/websock-w32.c
241                 ${WIN32_HELPERS_PATH}/gettimeofday.c
242                 )
243
244         include_directories(${WIN32_HELPERS_PATH})
245 else()
246         # Unix.
247         if (NOT WITHOUT_DAEMONIZE)
248                 list(APPEND SOURCES
249                         lib/daemonize.c
250                         )
251         endif()
252 endif()
253
254 if (UNIX)
255         if (!WITH_BUILTIN_GETIFADDRS)
256                 list(APPEND HDR_PRIVATE lib/getifaddrs.h)
257                 list(APPEND SOURCES lib/getifaddrs.c)
258         endif()
259 endif()
260
261 source_group("Headers Private"  FILES ${HDR_PRIVATE})
262 source_group("Headers Public"   FILES ${HDR_PUBLIC})
263 source_group("Sources"          FILES ${SOURCES})
264
265 #
266 # Create the lib.
267 #
268 add_library(websockets STATIC
269                         ${HDR_PRIVATE}
270                         ${HDR_PUBLIC}
271                         ${SOURCES})
272 add_library(websockets_shared SHARED
273                         ${HDR_PRIVATE}
274                         ${HDR_PUBLIC}
275                         ${SOURCES})
276
277 if (WIN32)
278         # On Windows libs have the same file ending (.lib)
279         # for both static and shared libraries, so we
280         # need a unique name for the static one.
281         set_target_properties(websockets 
282                 PROPERTIES
283                 OUTPUT_NAME websockets_static)
284
285         # Compile as DLL (export function declarations)
286         set_property(
287                 TARGET websockets_shared
288                 PROPERTY COMPILE_DEFINITIONS 
289                 LWS_DLL
290                 LWS_INTERNAL
291                 )
292 endif()
293
294 # We want the shared lib to be named "libwebsockets"
295 # not "libwebsocket_shared".
296 set_target_properties(websockets_shared
297                 PROPERTIES 
298                 OUTPUT_NAME websockets)
299
300 # Set the so version of the lib.
301 # Equivalent to LDFLAGS=-version-info 3:0:0
302 if(CMAKE_COMPILER_IS_GNUCC OR CMAKE_COMPILER_IS_GNUCXX)
303         foreach(lib websockets websockets_shared)
304                 set_target_properties(${lib} 
305                         PROPERTIES
306                         SOVERSION ${SOVERSION})
307         endforeach()
308 endif()
309
310 set(LIB_LIST)
311
312 #
313 # Find libraries.
314 #
315
316 #
317 # ZLIB.
318 #
319 if (WIN32 AND NOT USE_EXTERNAL_ZLIB)
320         message("Using included Zlib version")
321
322         # Compile ZLib if needed.
323         set(WIN32_ZLIB_PATH "win32port/zlib")
324         set(ZLIB_SRCS
325                 ${WIN32_ZLIB_PATH}/adler32.c
326                 ${WIN32_ZLIB_PATH}/compress.c
327                 ${WIN32_ZLIB_PATH}/crc32.c
328                 ${WIN32_ZLIB_PATH}/deflate.c
329                 ${WIN32_ZLIB_PATH}/gzclose.c
330                 ${WIN32_ZLIB_PATH}/gzio.c
331                 ${WIN32_ZLIB_PATH}/gzlib.c
332                 ${WIN32_ZLIB_PATH}/gzread.c
333                 ${WIN32_ZLIB_PATH}/gzwrite.c
334                 ${WIN32_ZLIB_PATH}/infback.c
335                 ${WIN32_ZLIB_PATH}/inffast.c
336                 ${WIN32_ZLIB_PATH}/inflate.c
337                 ${WIN32_ZLIB_PATH}/inftrees.c
338                 ${WIN32_ZLIB_PATH}/trees.c
339                 ${WIN32_ZLIB_PATH}/uncompr.c
340                 ${WIN32_ZLIB_PATH}/zutil.c
341         )
342
343         # Create the library.
344         add_library(ZLIB STATIC ${ZLIB_SRCS})
345
346         # Set the same variables as find_package would.
347         set(ZLIB_INCLUDE_DIRS ${WIN32_ZLIB_PATH})
348         get_property(ZLIB_LIBRARIES TARGET ZLIB PROPERTY LOCATION)
349         set(ZLIB_FOUND 1)
350 else()
351         find_package(ZLIB REQUIRED)
352 endif()
353
354 # Make sure ZLib is compiled before the libs.
355 foreach (lib websockets websockets_shared)
356         add_dependencies(${lib} ZLIB)
357 endforeach()
358
359 message("ZLib include dirs: ${ZLIB_INCLUDE_DIRS}")
360 message("ZLib libraries: ${ZLIB_LIBRARIES}")
361 include_directories(${ZLIB_INCLUDE_DIRS})
362 list(APPEND LIB_LIST ${ZLIB_LIBRARIES})
363
364 #
365 # OpenSSL
366 #
367 if (WITH_SSL)
368         message("Compiling with SSL support")
369
370         if (USE_CYASSL)
371                 # Use CyaSSL as OpenSSL replacement.
372                 # TODO: Add a find_package command for this also.
373                 message("CyaSSL include dir: ${CYASSL_INCLUDE_DIRS}")
374                 message("CyaSSL libraries: ${CYASSL_LIB}")
375
376                 # Additional to the root directory we need to include
377                 # the cyassl/ subdirectory which contains the OpenSSL
378                 # compatability layer headers.
379                 foreach(inc ${CYASSL_INCLUDE_DIRS})
380                         include_directories(${inc} ${inc}/cyassl)
381                 endforeach()
382
383                 list(APPEND ${LIB_LIST} ${CYASSL_LIB})
384         else()
385                 # TODO: Add support for STATIC also.
386                 find_package(OpenSSL REQUIRED)
387
388                 message("OpenSSL include dir: ${OPENSSL_INCLUDE_DIR}")
389                 message("OpenSSL libraries: ${OPENSSL_LIBRARIES}")
390
391                 include_directories(${OPENSSL_INCLUDE_DIR})
392                 list(APPEND LIB_LIST ${OPENSSL_LIBRARIES})
393         endif()
394 endif(WITH_SSL)
395
396 #
397 # Platform specific libs.
398 #
399 if (WIN32)
400         list(APPEND LIB_LIST ws2_32.lib)
401 endif()
402
403 if (UNIX)
404         list(APPEND LIB_LIST m)
405 endif()
406
407 # Setup the linking for all libs.
408 foreach (lib websockets websockets_shared)
409         target_link_libraries(${lib} ${LIB_LIST})
410 endforeach()
411
412 #
413 # Test applications
414 #
415 if (NOT WITHOUT_TESTAPPS)
416         #
417         # Helper function for adding a test app.
418         #
419         function(create_test_app TEST_NAME MAIN_SRC WIN32_SRCS WIN32_HDRS)
420                 
421                 set(TEST_SRCS ${MAIN_SRC})
422                 set(TEST_HDR)
423
424                 if (WIN32)
425                         list(APPEND TEST_SRCS 
426                                 ${WIN32_HELPERS_PATH}/getopt.c
427                                 ${WIN32_HELPERS_PATH}/getopt_long.c
428                                 ${WIN32_HELPERS_PATH}/gettimeofday.c
429                                 ${WIN32_SRCS})
430
431                         list(APPEND TEST_HDR 
432                                 ${WIN32_HELPERS_PATH}/getopt.h
433                                 ${WIN32_HELPERS_PATH}/gettimeofday.h
434                                 ${WIN32_HDRS})
435                 endif(WIN32)
436
437                 source_group("Headers Private"   FILES ${TEST_HDR})
438                 source_group("Sources"   FILES ${TEST_SRCS})
439                 add_executable(${TEST_NAME} ${TEST_SRCS} ${TEST_HDR})
440                 target_link_libraries(${TEST_NAME} websockets)
441                 add_dependencies(${TEST_NAME} websockets)
442                 set_property(
443                                         TARGET ${TEST_NAME}
444                                         PROPERTY COMPILE_DEFINITIONS 
445                                                 INSTALL_DATADIR="${SSL_CERT_DIR}"
446                                         )
447         endfunction()
448
449         #
450         # test-client
451         #
452         if (NOT WITHOUT_CLIENT)
453                 create_test_app(test-client
454                         "test-server/test-client.c"
455                         ""
456                         "")
457         endif()
458
459         #
460         # test-server
461         #
462         if (NOT WITHOUT_SERVER)
463                 create_test_app(test-server
464                         "test-server/test-server.c"
465                         ""
466                         "${WIN32_HELPERS_PATH}/netdb.h;${WIN32_HELPERS_PATH}/strings.h;${WIN32_HELPERS_PATH}/unistd.h;${WIN32_HELPERS_PATH}/websock-w32.h")
467         endif()
468
469         #
470         # test-server-extpoll
471         #
472         if (NOT WITHOUT_SERVER AND NOT WITHOUT_SERVER_EXTPOLL)
473                 create_test_app(test-server-extpoll
474                         "test-server/test-server.c"
475                         "win32port/win32helpers/websock-w32.c"
476                         "${WIN32_HELPERS_PATH}/netdb.h;${WIN32_HELPERS_PATH}/strings.h;${WIN32_HELPERS_PATH}/unistd.h;${WIN32_HELPERS_PATH}/websock-w32.h")
477                         
478                 # Set defines for this executable only.
479                 set_property(
480                         TARGET test-server-extpoll
481                         PROPERTY COMPILE_DEFINITIONS 
482                                 EXTERNAL_POLL 
483                                 INSTALL_DATADIR="${SSL_CERT_DIR}"
484                         )
485
486                 # We need to link against winsock code.
487                 if (WIN32)
488                         target_link_libraries(test-server-extpoll ws2_32.lib)
489                 endif()
490         endif()
491
492         #
493         # test-fraggle
494         #
495         if (NOT WITHOUT_FRAGGLE)
496                 create_test_app(test-fraggle
497                         "test-server/test-fraggle.c"
498                         ""
499                         "${WIN32_HELPERS_PATH}/unistd.h;${WIN32_HELPERS_PATH}/sys/time.h")
500         endif()
501
502         #
503         # test-ping
504         #
505         if (NOT WITHOUT_PING)
506                 create_test_app(test-ping
507                         "test-server/test-ping.c"
508                         ""
509                         "${WIN32_HELPERS_PATH}/unistd.h;${WIN32_HELPERS_PATH}/sys/time.h")
510         endif()
511
512         #
513         # Copy OpenSSL dlls to the output directory on Windows.
514         # (Otherwise we'll get an error when trying to run)
515         #
516         if (WIN32 AND WITH_SSL AND NOT USE_CYASSL)
517
518                 message("Searching for OpenSSL dlls")
519                 find_package(OpenSSLbins)
520
521                 if(OPENSSL_BIN_FOUND)
522                         message("OpenSSL dlls found, copying to output directory")
523                         message("Libeay: ${LIBEAY_BIN}")
524                         message("SSLeay: ${SSLEAY_BIN}")
525
526                         foreach(TARGET_BIN 
527                                         test-client 
528                                         test-server
529                                         test-server-extpoll
530                                         test-fraggle
531                                         test-echo
532                                         test-ping
533                                         )                       
534                                 add_custom_command(TARGET ${TARGET_BIN}
535                                         POST_BUILD 
536                                         COMMAND ${CMAKE_COMMAND} -E copy ${LIBEAY_BIN} $<TARGET_FILE_DIR:${TARGET_BIN}> VERBATIM)
537                                         
538                                 add_custom_command(TARGET ${TARGET_BIN}
539                                         POST_BUILD 
540                                         COMMAND ${CMAKE_COMMAND} -E copy ${SSLEAY_BIN} $<TARGET_FILE_DIR:${TARGET_BIN}> VERBATIM)
541                         endforeach()
542                 endif()
543         endif()
544 endif(NOT WITHOUT_TESTAPPS)