1project(lws-minimal-ws-server-threads-smp)
2cmake_minimum_required(VERSION 2.8)
3include(CheckIncludeFile)
4include(CheckCSourceCompiles)
5
6set(SAMP lws-minimal-ws-server-threads-smp)
7set(SRCS minimal-ws-server.c)
8
9MACRO(require_pthreads result)
10	CHECK_INCLUDE_FILE(pthread.h LWS_HAVE_PTHREAD_H)
11	if (NOT LWS_HAVE_PTHREAD_H)
12		if (LWS_WITH_MINIMAL_EXAMPLES)
13			set(result 0)
14		else()
15			message(FATAL_ERROR "threading support requires pthreads")
16		endif()
17	endif()
18ENDMACRO()
19
20# If we are being built as part of lws, confirm current build config supports
21# reqconfig, else skip building ourselves.
22#
23# If we are being built externally, confirm installed lws was configured to
24# support reqconfig, else error out with a helpful message about the problem.
25#
26MACRO(require_lws_config reqconfig _val result)
27
28	if (DEFINED ${reqconfig})
29	if (${reqconfig})
30		set (rq 1)
31	else()
32		set (rq 0)
33	endif()
34	else()
35		set(rq 0)
36	endif()
37
38	if (${_val} EQUAL ${rq})
39		set(SAME 1)
40	else()
41		set(SAME 0)
42	endif()
43
44	if (LWS_WITH_MINIMAL_EXAMPLES AND NOT ${SAME})
45		if (${_val})
46			message("${SAMP}: skipping as lws being built without ${reqconfig}")
47		else()
48			message("${SAMP}: skipping as lws built with ${reqconfig}")
49		endif()
50		set(${result} 0)
51	else()
52		if (LWS_WITH_MINIMAL_EXAMPLES)
53			set(MET ${SAME})
54		else()
55			CHECK_C_SOURCE_COMPILES("#include <libwebsockets.h>\nint main(void) {\n#if defined(${reqconfig})\n return 0;\n#else\n fail;\n#endif\n return 0;\n}\n" HAS_${reqconfig})
56			if (NOT DEFINED HAS_${reqconfig} OR NOT HAS_${reqconfig})
57				set(HAS_${reqconfig} 0)
58			else()
59				set(HAS_${reqconfig} 1)
60			endif()
61			if ((HAS_${reqconfig} AND ${_val}) OR (NOT HAS_${reqconfig} AND NOT ${_val}))
62				set(MET 1)
63			else()
64				set(MET 0)
65			endif()
66		endif()
67		if (NOT MET)
68			if (${_val})
69				message(FATAL_ERROR "This project requires lws must have been configured with ${reqconfig}")
70			else()
71				message(FATAL_ERROR "Lws configuration of ${reqconfig} is incompatible with this project")
72			endif()
73		endif()
74
75	endif()
76ENDMACRO()
77
78set(requirements 1)
79require_pthreads(requirements)
80require_lws_config(LWS_ROLE_WS 1 requirements)
81require_lws_config(LWS_WITH_SERVER 1 requirements)
82
83if (requirements)
84	add_executable(${SAMP} ${SRCS})
85
86	if (websockets_shared)
87		target_link_libraries(${SAMP} websockets_shared pthread)
88		add_dependencies(${SAMP} websockets_shared)
89	else()
90		target_link_libraries(${SAMP} websockets pthread)
91	endif()
92endif()
93