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