Fixed linux compilation and added more compile options.
[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.1")
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
14 set(LWS_LIBRARY_VERSION ${PACKAGE_VERSION})
15
16 # Try to find the current Git hash.
17 find_package(Git)
18 if(GIT_EXECUTABLE)
19         execute_process(
20     COMMAND "${GIT_EXECUTABLE}" log -n 1 --pretty=%h
21     OUTPUT_VARIABLE GIT_HASH
22     OUTPUT_STRIP_TRAILING_WHITESPACE
23     )
24
25     set(LWS_BUILD_HASH ${GIT_HASH})
26     message("Git commit hash: ${LWS_BUILD_HASH}")
27 endif()
28
29 option(WITH_SSL "Include SSL support (default OpenSSL, CyaSSL if USE_CYASSL is set)" ON)
30 option(USE_EXTERNAL_ZLIB "Search the system for ZLib instead of using the included one (on Windows)" OFF)
31 option(USE_CYASSL "Use CyaSSL replacement for OpenSSL" OFF)
32 option(WITH_BUILTIN_GETIFADDRS "Use BSD getifaddrs implementation from libwebsockets... default is your libc provides it" OFF)
33 option(WITHOUT_TESTAPPS "Don't build the libwebsocket-test-apps" OFF)
34 option(WITHOUT_CLIENT "Don't build the client part of the library" OFF)
35 option(WITHOUT_SERVER "Don't build the server part of the library" OFF)
36 option(WITH_LIBCRYPTO "Use libcrypto MD5 and SHA1 implementations" ON)
37 option(WITHOUT_PING "Don't build the ping test application" OFF)
38 option(WITHOUT_DEBUG "Don't compile debug related code" OFF)
39 option(WITHOUT_EXTENSIONS "Don't compile with extensions" OFF)
40 option(WITH_LATENCY "Build latency measuring code into the library" OFF)
41 option(WITHOUT_DAEMONIZE "Don't build the daemonization api" OFF)
42
43 # The base dir where the SSL dirs should be looked for.
44 set(SSL_CERT_DIR CACHE STRING "")
45 set(SSL_CLIENT_CERT_DIR CACHE STRING "")
46
47 if ("${SSL_CERT_DIR}" STREQUAL "")
48         set(SSL_CERT_DIR ".")
49 endif()
50
51 if ("${SSL_CLIENT_CERT_DIR}" STREQUAL "")
52         set(LWS_OPENSSL_CLIENT_CERTS ".")
53 else()
54         set(LWS_OPENSSL_CLIENT_CERTS "${SSL_CLIENT_CERT_DIR}")
55 endif()
56
57 set(CYASSL_LIB CACHE STRING "")
58 set(CYASSL_INCLUDE_DIRS CACHE STRING "")
59
60 if (USE_CYASSL)
61         if ("${CYASSL_LIB}" STREQUAL "" OR "${CYASSL_INCLUDE_DIRS}" STREQUAL "")
62                 error("You must set CYASSL_LIB and CYASSL_INCLUDE_DIRS when USE_CYASSL is turned on")
63         endif()
64 endif()
65
66 if (WITHOUT_EXTENSIONS)
67         set(LWS_NO_EXTENSIONS 1)
68 endif()
69
70 if (WITH_SSL)
71         set(LWS_OPENSSL_SUPPORT 1)
72 endif()
73
74 if (WITH_LATENCY)
75         set(LWS_LATENCY 1)
76 endif()
77
78 if (WITHOUT_DAEMONIZE)
79         set(LWS_NO_DAEMONIZE 1)
80 endif()
81
82 if (MINGW)
83         set(LWS_MINGW_SUPPORT 1)
84 endif()
85
86 set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${PROJECT_SOURCE_DIR}/cmake/")
87 include_directories(${PROJECT_BINARY_DIR})
88
89 # Put the libaries and binaries that get built into directories at the
90 # top of the build tree rather than in hard-to-find leaf directories. 
91 SET(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${PROJECT_BINARY_DIR}/bin)
92 SET(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${PROJECT_BINARY_DIR}/lib)
93 SET(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${PROJECT_BINARY_DIR}/lib)
94
95 # So we can include the CMake generated config file only when
96 # building with CMAKE.
97 add_definitions(-DCMAKE_BUILD)
98
99 include(CheckFunctionExists)
100 include(CheckIncludeFile)
101 include(CheckIncludeFiles)
102 include(CheckLibraryExists)
103
104 CHECK_FUNCTION_EXISTS(bzero HAVE_BZERO)
105 CHECK_FUNCTION_EXISTS(fork HAVE_FORK)
106 CHECK_FUNCTION_EXISTS(malloc HAVE_MALLOC)
107 CHECK_FUNCTION_EXISTS(memset HAVE_MEMSET)
108 CHECK_FUNCTION_EXISTS(realloc HAVE_REALLOC)
109 CHECK_FUNCTION_EXISTS(socket HAVE_SOCKET)
110 CHECK_FUNCTION_EXISTS(strerror HAVE_STRERROR)
111 CHECK_FUNCTION_EXISTS(vfork HAVE_VFORK)
112 CHECK_FUNCTION_EXISTS(getifaddrs HAVE_GETIFADDRS)
113
114 if (WITH_BUILTIN_GETIFADDRS)
115         if (HAVE_GETIFADDRS)
116                 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)")
117         endif()
118         set(LWS_BUILTIN_GETIFADDRS 1)
119 endif()
120
121 CHECK_INCLUDE_FILE(dlfcn.h HAVE_DLFCN_H)
122 CHECK_INCLUDE_FILE(fcntl.h HAVE_FCNTL_H)
123 CHECK_INCLUDE_FILE(inttypes.h HAVE_INTTYPES_H)
124 CHECK_INCLUDE_FILE(memory.h HAVE_MEMORY_H)
125 CHECK_INCLUDE_FILE(netinet/in.h HAVE_NETINET_IN_H)
126 CHECK_INCLUDE_FILE(stdint.h HAVE_STDINT_H)
127 CHECK_INCLUDE_FILE(stdlib.h HAVE_STDLIB_H)
128 CHECK_INCLUDE_FILE(strings.h HAVE_STRINGS_H)
129 CHECK_INCLUDE_FILE(string.h HAVE_STRING_H)
130 CHECK_INCLUDE_FILE(sys/prctl.h HAVE_SYS_PRCTL_H)
131 CHECK_INCLUDE_FILE(sys/socket.h HAVE_SYS_SOCKET_H)
132 CHECK_INCLUDE_FILE(sys/stat.h HAVE_SYS_STAT_H)
133 CHECK_INCLUDE_FILE(sys/types.h HAVE_SYS_TYPES_H)
134 CHECK_INCLUDE_FILE(unistd.h HAVE_UNISTD_H)
135 CHECK_INCLUDE_FILE(vfork.h HAVE_VFORK_H)
136 CHECK_INCLUDE_FILE(zlib.h HAVE_ZLIB_H)
137
138 # TODO: These can be tested if they actually work also...
139 set(HAVE_WORKING_FORK HAVE_FORK)
140 set(HAVE_WORKING_VFORK HAVE_VFORK)
141
142 CHECK_INCLUDE_FILES("stdlib.h;stdarg.h;string.h;float.h" STDC_HEADERS)
143
144 if (NOT HAVE_SYS_TYPES_H)
145         set(pid_t int)
146         set(size_t "unsigned int")
147 endif()
148
149 if (NOT HAVE_MALLOC)
150         set(malloc rpl_malloc)
151 endif()
152
153 if (NOT HAVE_REALLOC)
154         set(realloc rpl_realloc)
155 endif()
156
157 # Generate the config.h that includes all the compilation settings.
158 configure_file(
159                 ${PROJECT_SOURCE_DIR}/config.h.cmake 
160                 ${PROJECT_BINARY_DIR}/lws_config.h)
161
162 set(LIB_LIST)
163
164 if (MSVC)
165         # Turn off stupid microsoft security warnings.
166         add_definitions(-D_CRT_SECURE_NO_DEPRECATE -D_CRT_NONSTDC_NO_DEPRECATE)
167 endif()
168
169 include_directories(${PROJECT_SOURCE_DIR}/lib)
170
171 # Group headers and sources.
172 # Some IDEs use this for nicer file structure.
173 set(HDR_PRIVATE
174         lib/private-libwebsockets.h
175         lib/extension-deflate-frame.h
176         lib/extension-deflate-stream.h
177         ${PROJECT_BINARY_DIR}/lws_config.h
178         )
179
180 set(HDR_PUBLIC  
181         lib/libwebsockets.h
182         )
183
184 set(SOURCES
185         lib/base64-decode.c
186         lib/client.c
187         lib/client-handshake.c
188         lib/client-parser.c
189         lib/extension.c
190         lib/extension-deflate-frame.c
191         lib/extension-deflate-stream.c
192         lib/handshake.c
193         lib/libwebsockets.c
194         lib/minilex.c
195         lib/output.c
196         lib/parsers.c
197         lib/server.c
198         lib/server-handshake.c
199         lib/sha-1.c
200         )
201
202 # Add helper files for Windows.
203 if (WIN32)
204         set(WIN32_HELPERS_PATH win32port/win32helpers)
205
206         list(APPEND HDR_PRIVATE
207                 ${WIN32_HELPERS_PATH}/websock-w32.h
208                 ${WIN32_HELPERS_PATH}/gettimeofday.h
209                 )
210
211         list(APPEND SOURCES 
212                 ${WIN32_HELPERS_PATH}/websock-w32.c
213                 ${WIN32_HELPERS_PATH}/gettimeofday.c
214                 )
215
216         include_directories(${WIN32_HELPERS_PATH})
217 else()
218         # Unix.
219         if (NOT WITHOUT_DAEMONIZE)
220                 list(APPEND SOURCES
221                         lib/daemonize.c
222                         )
223         endif()
224 endif()
225
226 if (UNIX)
227         if (!WITH_BUILTIN_GETIFADDRS)
228                 list(APPEND HDR_PRIVATE lib/getifaddrs.h)
229                 list(APPEND SOURCES lib/getifaddrs.c)
230         endif()
231 endif()
232
233 source_group("Headers Private"  FILES ${HDR_PRIVATE})
234 source_group("Headers Public"   FILES ${HDR_PUBLIC})
235 source_group("Sources"          FILES ${SOURCES})
236
237 #
238 # Create the lib.
239 #
240 add_library(websocket STATIC
241                         ${HDR_PRIVATE}
242                         ${HDR_PUBLIC}
243                         ${SOURCES})
244
245 #
246 # Find libraries.
247 #
248
249 #
250 # ZLIB.
251 #
252 if (WIN32 AND NOT USE_EXTERNAL_ZLIB)
253         message("Using included Zlib version")
254
255         # Compile ZLib if needed.
256         set(WIN32_ZLIB_PATH "win32port/zlib")
257         set(ZLIB_SRCS
258                 ${WIN32_ZLIB_PATH}/adler32.c
259                 ${WIN32_ZLIB_PATH}/compress.c
260                 ${WIN32_ZLIB_PATH}/crc32.c
261                 ${WIN32_ZLIB_PATH}/deflate.c
262                 ${WIN32_ZLIB_PATH}/gzclose.c
263                 ${WIN32_ZLIB_PATH}/gzio.c
264                 ${WIN32_ZLIB_PATH}/gzlib.c
265                 ${WIN32_ZLIB_PATH}/gzread.c
266                 ${WIN32_ZLIB_PATH}/gzwrite.c
267                 ${WIN32_ZLIB_PATH}/infback.c
268                 ${WIN32_ZLIB_PATH}/inffast.c
269                 ${WIN32_ZLIB_PATH}/inflate.c
270                 ${WIN32_ZLIB_PATH}/inftrees.c
271                 ${WIN32_ZLIB_PATH}/trees.c
272                 ${WIN32_ZLIB_PATH}/uncompr.c
273                 ${WIN32_ZLIB_PATH}/zutil.c
274         )
275
276         # Create the library.
277         add_library(ZLIB STATIC ${ZLIB_SRCS})
278
279         # Set the same variables as find_package would.
280         set(ZLIB_INCLUDE_DIRS ${WIN32_ZLIB_PATH})
281         get_property(ZLIB_LIBRARIES TARGET zlib PROPERTY LOCATION)
282         set(ZLIB_FOUND 1)
283 else()
284         find_package(ZLIB REQUIRED)
285 endif()
286
287 message("ZLib include dirs: ${ZLIB_INCLUDE_DIRS}")
288 message("ZLib libraries: ${ZLIB_LIBRARIES}")
289 include_directories(${ZLIB_INCLUDE_DIRS})
290 target_link_libraries(websocket ${ZLIB_LIBRARIES})
291
292 #
293 # OpenSSL
294 #
295 if (WITH_SSL)
296         message("Compiling with SSL support")
297
298         if (USE_CYASSL)
299                 # Use CyaSSL as OpenSSL replacement.
300                 set(OPENSSL_LIBRARIES ${CYASSL_LIB})
301                 set(OPENSSL_INCLUDE_DIRS ${CYASSL_INCLUDE_DIRS})
302                 set(OPENSSL_FOUND 1)
303         else()
304                 # TODO: Add support for STATIC also.
305                 find_package(OpenSSL REQUIRED)
306
307                 # TODO: Find OpenSSL binaries and copy them to the output directories for the test-apps so that they can be run out of the box.
308         endif()
309
310         message("OpenSSL include dir: ${OPENSSL_INCLUDE_DIR}")
311         message("OpenSSL libraries: ${OPENSSL_LIBRARIES}")
312         include_directories(${OPENSSL_INCLUDE_DIR})
313         target_link_libraries(websocket ${OPENSSL_LIBRARIES})
314 endif(WITH_SSL)
315
316 #
317 # Platform specific libs.
318 #
319 if (WIN32)
320         target_link_libraries(websocket ws2_32.lib)
321 endif()
322
323 if (UNIX)
324         target_link_libraries(websocket m)
325 endif()
326
327 #
328 # Test applications
329 #
330 if (NOT WITHOUT_TESTAPPS)
331         #
332         # Helper function for adding a test app.
333         #
334         function(create_test_app TEST_NAME MAIN_SRC WIN32_SRCS WIN32_HDRS)
335                 
336                 set(TEST_SRCS ${MAIN_SRC})
337                 set(TEST_HDR)
338
339                 if (WIN32)
340                         list(APPEND TEST_SRCS 
341                                 ${WIN32_HELPERS_PATH}/getopt.c
342                                 ${WIN32_HELPERS_PATH}/getopt_long.c
343                                 ${WIN32_HELPERS_PATH}/gettimeofday.c
344                                 ${WIN32_SRCS})
345
346                         list(APPEND TEST_HDR 
347                                 ${WIN32_HELPERS_PATH}/getopt.h
348                                 ${WIN32_HELPERS_PATH}/gettimeofday.h
349                                 ${WIN32_HDRS})
350                 endif(WIN32)
351
352                 source_group("Headers"   FILES ${TEST_HDR})
353                 source_group("Sources"   FILES ${TEST_SRCS})
354                 add_executable(${TEST_NAME} ${TEST_SRCS} ${TEST_HDR})
355                 target_link_libraries(${TEST_NAME} websocket)
356
357                 set_property(
358                                         TARGET ${TEST_NAME}
359                                         PROPERTY COMPILE_DEFINITIONS INSTALL_DATADIR="${SSL_CERT_DIR}"
360                                         )
361
362
363         endfunction()
364
365         #
366         # test-client
367         #
368         if (NOT WITHOUT_CLIENT)
369                 create_test_app(test-client
370                         "test-server/test-client.c"
371                         ""
372                         "")
373         endif(NOT WITHOUT_CLIENT)
374
375         #
376         # test-server
377         #
378         if (NOT WITHOUT_SERVER)
379                 create_test_app(test-server
380                         "test-server/test-server.c"
381                         ""
382                         "${WIN32_HELPERS_PATH}/netdb.h;${WIN32_HELPERS_PATH}/strings.h;${WIN32_HELPERS_PATH}/unistd.h;${WIN32_HELPERS_PATH}/websock-w32.h")
383         endif(NOT WITHOUT_SERVER)
384
385         #
386         # test-fraggle
387         #
388         if (NOT WITHOUT_FRAGGLE)
389                 create_test_app(test-fraggle
390                         "test-server/test-fraggle.c"
391                         ""
392                         "${WIN32_HELPERS_PATH}/unistd.h;${WIN32_HELPERS_PATH}/sys/time.h")
393         endif(NOT WITHOUT_FRAGGLE)
394
395         #
396         # test-ping
397         #
398         if (NOT WITHOUT_PING)
399                 create_test_app(test-ping
400                         "test-server/test-ping.c"
401                         ""
402                         "${WIN32_HELPERS_PATH}/unistd.h;${WIN32_HELPERS_PATH}/sys/time.h")
403         endif(NOT WITHOUT_PING)
404
405         #
406         # Copy OpenSSL dlls to the output directory on Windows.
407         # (Otherwise we'll get an error when trying to run)
408         #
409         if (WIN32 AND WITH_SSL AND NOT USE_CYASSL)
410
411                 message("Searching for OpenSSL dlls")
412                 find_package(OpenSSLbins)
413
414                 if(OPENSSL_BIN_FOUND)
415                         message("OpenSSL dlls found, copying to output directory")
416                         message("Libeay: ${LIBEAY_BIN}")
417                         message("SSLeay: ${SSLEAY_BIN}")
418
419                         foreach(TARGET_BIN 
420                                         test-client 
421                                         test-server
422                                         test-fraggle
423                                         test-echo
424                                         test-ping
425                                         )                       
426                                 add_custom_command(TARGET ${TARGET_BIN}
427                                         POST_BUILD 
428                                         COMMAND ${CMAKE_COMMAND} -E copy ${LIBEAY_BIN} $<TARGET_FILE_DIR:${TARGET_BIN}> VERBATIM)
429                                         
430                                 add_custom_command(TARGET ${TARGET_BIN}
431                                         POST_BUILD 
432                                         COMMAND ${CMAKE_COMMAND} -E copy ${SSLEAY_BIN} $<TARGET_FILE_DIR:${TARGET_BIN}> VERBATIM)
433                         endforeach()
434                 endif()
435         endif()
436 endif(NOT WITHOUT_TESTAPPS)