xref: /freebsd/contrib/libucl/CMakeLists.txt (revision eda14cbc264d6969b02f2b1994cef11148e914f1)
1PROJECT(libucl C)
2CMAKE_MINIMUM_REQUIRED(VERSION 2.6.0 FATAL_ERROR)
3
4SET(LIBUCL_VERSION_MAJOR 0)
5SET(LIBUCL_VERSION_MINOR 5)
6SET(LIBUCL_VERSION_PATCH 0)
7
8SET(LIBUCL_VERSION
9        "${LIBUCL_VERSION_MAJOR}.${LIBUCL_VERSION_MINOR}.${LIBUCL_VERSION_PATCH}")
10
11INCLUDE(CheckCCompilerFlag)
12INCLUDE(FindOpenSSL)
13
14OPTION(ENABLE_URL_INCLUDE  "Enable urls in ucl includes (requires libcurl or libfetch) [default: OFF]" OFF)
15OPTION(ENABLE_URL_SIGN  "Enable signatures check in ucl includes (requires openssl) [default: OFF]" OFF)
16OPTION(BUILD_SHARED_LIBS "Build Shared Libraries [default: OFF]" OFF)
17OPTION(ENABLE_LUA "Enable lua support [default: OFF]" OFF)
18OPTION(ENABLE_LUAJIT "Enable luajit support [default: OFF]" OFF)
19
20# Find lua installation
21MACRO(FindLua)
22	# Find lua libraries
23	UNSET(LUA_INCLUDE_DIR CACHE)
24	UNSET(LUA_LIBRARY CACHE)
25	CMAKE_PARSE_ARGUMENTS(LUA "" "VERSION_MAJOR;VERSION_MINOR;ROOT" "" ${ARGN})
26
27	IF(NOT LUA_VERSION_MAJOR OR NOT LUA_VERSION_MINOR)
28		MESSAGE(FATAL_ERROR "Invalid FindLua invocation: ${ARGN}")
29	ENDIF()
30
31	IF(ENABLE_LUAJIT MATCHES "ON")
32		MESSAGE(STATUS "Check for luajit ${LUA_VERSION_MAJOR}.${LUA_VERSION_MINOR}")
33		FIND_PATH(LUA_INCLUDE_DIR luajit.h
34				HINTS
35				"${RSPAMD_SEARCH_PATH}" "${LUA_ROOT}"
36				$ENV{LUA_DIR}
37				PATH_SUFFIXES "include/luajit-2.0"
38				"include/luajit${LUA_VERSION_MAJOR}${LUA_VERSION_MINOR}"
39				"include/luajit${LUA_VERSION_MAJOR}.${LUA_VERSION_MINOR}"
40				"include/luajit-${LUA_VERSION_MAJOR}.${LUA_VERSION_MINOR}"
41				"include/luajit"
42				"include/lua${LUA_VERSION_MAJOR}${LUA_VERSION_MINOR}"
43				"include/lua${LUA_VERSION_MAJOR}.${LUA_VERSION_MINOR}"
44				"include/lua-${LUA_VERSION_MAJOR}.${LUA_VERSION_MINOR}"
45				include/lua include
46				PATHS ${RSPAMD_DEFAULT_INCLUDE_PATHS}
47		)
48		FIND_LIBRARY(LUA_LIBRARY
49				NAMES luajit
50				"luajit-2.0"
51				"luajit2.0"
52				"luajit${LUA_VERSION_MAJOR}${LUA_VERSION_MINOR}"
53				"luajit${LUA_VERSION_MAJOR}.${LUA_VERSION_MINOR}"
54				"luajit-${LUA_VERSION_MAJOR}.${LUA_VERSION_MINOR}"
55				HINTS
56				"${RSPAMD_SEARCH_PATH}" "${LUA_ROOT}"
57				$ENV{LUA_DIR}
58				PATH_SUFFIXES lib64 lib
59				PATHS ${RSPAMD_DEFAULT_LIBRARY_PATHS}
60				DOC "Lua library"
61		)
62
63		IF(NOT LUA_LIBRARY OR NOT LUA_INCLUDE_DIR)
64			MESSAGE(STATUS "Fallback from luajit to plain lua")
65			SET(ENABLE_LUAJIT "OFF")
66			MESSAGE(STATUS "Check for lua ${LUA_VERSION_MAJOR}.${LUA_VERSION_MINOR}")
67			FIND_PATH(LUA_INCLUDE_DIR lua.h
68					HINTS
69					"${RSPAMD_SEARCH_PATH}" "${LUA_ROOT}"
70					$ENV{LUA_DIR}
71					PATH_SUFFIXES "include/lua${LUA_VERSION_MAJOR}${LUA_VERSION_MINOR}"
72					"include/lua${LUA_VERSION_MAJOR}.${LUA_VERSION_MINOR}"
73					"include/lua-${LUA_VERSION_MAJOR}.${LUA_VERSION_MINOR}"
74					include/lua include
75					PATHS ${RSPAMD_DEFAULT_INCLUDE_PATHS}
76			)
77			FIND_LIBRARY(LUA_LIBRARY
78					NAMES lua
79					"lua${LUA_VERSION_MAJOR}${LUA_VERSION_MINOR}"
80					"lua${LUA_VERSION_MAJOR}.${LUA_VERSION_MINOR}"
81					"lua-${LUA_VERSION_MAJOR}.${LUA_VERSION_MINOR}"
82					HINTS
83					"${RSPAMD_SEARCH_PATH}" "${LUA_ROOT}"
84					$ENV{LUA_DIR}
85					PATH_SUFFIXES lib64 lib
86					PATHS ${RSPAMD_DEFAULT_LIBRARY_PATHS}
87					DOC "Lua library"
88			)
89		ENDIF()
90	ELSE(ENABLE_LUAJIT MATCHES "ON")
91		MESSAGE(STATUS "Check for lua ${LUA_VERSION_MAJOR}.${LUA_VERSION_MINOR}")
92		FIND_PATH(LUA_INCLUDE_DIR lua.h
93				HINTS
94				"${RSPAMD_SEARCH_PATH}" "${LUA_ROOT}"
95				$ENV{LUA_DIR}
96				PATH_SUFFIXES "include/lua${LUA_VERSION_MAJOR}${LUA_VERSION_MINOR}"
97				"include/lua${LUA_VERSION_MAJOR}.${LUA_VERSION_MINOR}"
98				"include/lua-${LUA_VERSION_MAJOR}.${LUA_VERSION_MINOR}"
99				include/lua include
100				PATHS ${RSPAMD_DEFAULT_INCLUDE_PATHS}
101		)
102		FIND_LIBRARY(LUA_LIBRARY
103				NAMES lua
104				"lua${LUA_VERSION_MAJOR}${LUA_VERSION_MINOR}"
105				"lua${LUA_VERSION_MAJOR}.${LUA_VERSION_MINOR}"
106				"lua-${LUA_VERSION_MAJOR}.${LUA_VERSION_MINOR}"
107				HINTS
108				"${RSPAMD_SEARCH_PATH}" "${LUA_ROOT}"
109				$ENV{LUA_DIR}
110				PATH_SUFFIXES lib64 lib
111				PATHS ${RSPAMD_DEFAULT_LIBRARY_PATHS}
112				DOC "Lua library"
113		)
114	ENDIF(ENABLE_LUAJIT MATCHES "ON")
115
116	IF(LUA_LIBRARY AND LUA_INCLUDE_DIR)
117		SET(LUA_FOUND 1)
118		IF(NOT LUA_VERSION_MAJOR OR NOT LUA_VERSION_MINOR)
119			SET(LUA_VERSION_MAJOR ${LUA_VERSION_MAJOR})
120			SET(LUA_VERSION_MINOR ${LUA_VERSION_MINOR})
121		ENDIF(NOT LUA_VERSION_MAJOR OR NOT LUA_VERSION_MINOR)
122		IF(ENABLE_LUAJIT MATCHES "ON")
123			MESSAGE(STATUS "Found luajit ${LUA_VERSION_MAJOR}.${LUA_VERSION_MINOR}")
124		ELSE(ENABLE_LUAJIT MATCHES "ON")
125			MESSAGE(STATUS "Found lua ${LUA_VERSION_MAJOR}.${LUA_VERSION_MINOR}")
126		ENDIF(ENABLE_LUAJIT MATCHES "ON")
127	ENDIF(LUA_LIBRARY AND LUA_INCLUDE_DIR)
128ENDMACRO()
129
130IF(CMAKE_SYSTEM_NAME STREQUAL "Linux")
131	LIST(APPEND CMAKE_REQUIRED_LIBRARIES rt)
132ENDIF(CMAKE_SYSTEM_NAME STREQUAL "Linux")
133
134IF(ENABLE_URL_INCLUDE MATCHES "ON")
135    FIND_LIBRARY(LIBFETCH_LIBRARY NAMES fetch PATHS	PATH_SUFFIXES lib64 lib
136                      PATHS
137                          ~/Library/Frameworks
138                          /Library/Frameworks
139                          /usr/local
140                          /usr
141                          /sw
142                          /opt/local
143                          /opt/csw
144                          /opt
145                     DOC "Path where the libfetch library can be found")
146    IF(LIBFETCH_LIBRARY)
147    	FIND_FILE(HAVE_FETCH_H NAMES fetch.h PATHS /usr/include
148    											   /opt/include
149    											   /usr/local/include
150    				DOC "Path to libfetch header")
151    ELSE(LIBFETCH_LIBRARY)
152    	# Try to find libcurl
153    	ProcessPackage(CURL libcurl)
154    	IF(NOT CURL_FOUND)
155    		MESSAGE(WARNING "Neither libcurl nor libfetch were found, no support of URL includes in configuration")
156    	ENDIF(NOT CURL_FOUND)
157    ENDIF(LIBFETCH_LIBRARY)
158ENDIF(ENABLE_URL_INCLUDE MATCHES "ON")
159
160SET(CMAKE_C_WARN_FLAGS "")
161CHECK_C_COMPILER_FLAG(-Wall SUPPORT_WALL)
162CHECK_C_COMPILER_FLAG(-W SUPPORT_W)
163CHECK_C_COMPILER_FLAG(-Wno-unused-parameter SUPPORT_WPARAM)
164CHECK_C_COMPILER_FLAG(-Wno-pointer-sign SUPPORT_WPOINTER_SIGN)
165CHECK_C_COMPILER_FLAG(-Wstrict-prototypes SUPPORT_WSTRICT_PROTOTYPES)
166IF(NOT "${CMAKE_C_COMPILER_ID}" MATCHES SunPro)
167	CHECK_C_COMPILER_FLAG("-std=c99" SUPPORT_STD_FLAG)
168ENDIF(NOT "${CMAKE_C_COMPILER_ID}" MATCHES SunPro)
169IF(SUPPORT_W)
170	SET(CMAKE_C_WARN_FLAGS "${CMAKE_C_WARN_FLAGS} -W")
171ENDIF(SUPPORT_W)
172IF(SUPPORT_WALL)
173	SET(CMAKE_C_WARN_FLAGS "${CMAKE_C_WARN_FLAGS} -Wall")
174ENDIF(SUPPORT_WALL)
175IF(SUPPORT_WPARAM)
176	SET(CMAKE_C_WARN_FLAGS "${CMAKE_C_WARN_FLAGS} -Wno-unused-parameter")
177ENDIF(SUPPORT_WPARAM)
178IF(SUPPORT_WPOINTER_SIGN)
179	SET(CMAKE_C_WARN_FLAGS "${CMAKE_C_WARN_FLAGS} -Wno-pointer-sign")
180ENDIF(SUPPORT_WPOINTER_SIGN)
181IF(SUPPORT_WSTRICT_PROTOTYPES)
182	SET(CMAKE_C_WARN_FLAGS "${CMAKE_C_WARN_FLAGS} -Wstrict-prototypes")
183ENDIF(SUPPORT_WSTRICT_PROTOTYPES)
184IF(SUPPORT_STD_FLAG)
185	SET(CMAKE_C_WARN_FLAGS "${CMAKE_C_WARN_FLAGS} -std=c99")
186ENDIF(SUPPORT_STD_FLAG)
187
188IF(ENABLE_URL_SIGN MATCHES "ON")
189	IF(OPENSSL_FOUND)
190		SET(HAVE_OPENSSL 1)
191		INCLUDE_DIRECTORIES("${OPENSSL_INCLUDE_DIR}")
192	ENDIF(OPENSSL_FOUND)
193ENDIF(ENABLE_URL_SIGN MATCHES "ON")
194
195INCLUDE_DIRECTORIES("src")
196INCLUDE_DIRECTORIES("include")
197INCLUDE_DIRECTORIES("uthash")
198INCLUDE_DIRECTORIES("klib")
199
200SET(UCLSRC src/ucl_util.c
201		src/ucl_parser.c
202		src/ucl_emitter.c
203		src/ucl_emitter_streamline.c
204		src/ucl_emitter_utils.c
205		src/ucl_hash.c
206		src/ucl_schema.c
207		src/ucl_msgpack.c
208		src/ucl_sexp.c)
209
210
211SET (LIB_TYPE STATIC)
212IF (BUILD_SHARED_LIBS)
213  SET (LIB_TYPE SHARED)
214ENDIF (BUILD_SHARED_LIBS)
215ADD_LIBRARY(ucl ${LIB_TYPE} ${UCLSRC})
216SET_TARGET_PROPERTIES(ucl PROPERTIES VERSION ${LIBUCL_VERSION} SOVERSION ${LIBUCL_VERSION_MAJOR})
217
218IF(ENABLE_LUA MATCHES "ON")
219	IF(ENABLE_LUAJIT MATCHES "ON")
220		FindLua(VERSION_MAJOR "5" VERSION_MINOR "1" ROOT "${LUA_ROOT}")
221		IF(NOT LUA_FOUND)
222			MESSAGE(FATAL_ERROR "Lua not found, lua support is required")
223		ELSE(NOT LUA_FOUND)
224			INCLUDE_DIRECTORIES("${LUA_INCLUDE_DIR}")
225		ENDIF(NOT LUA_FOUND)
226	ELSE(ENABLE_LUAJIT MATCHES "ON")
227		FindLua(VERSION_MAJOR "5" VERSION_MINOR "2" ROOT "${LUA_ROOT}")
228		IF(NOT LUA_FOUND)
229			FindLua(VERSION_MAJOR "5" VERSION_MINOR "1" ROOT "${LUA_ROOT}")
230		ENDIF(NOT LUA_FOUND)
231		IF(NOT LUA_FOUND)
232			MESSAGE(FATAL_ERROR "Lua not found, lua support is required")
233		ELSE(NOT LUA_FOUND)
234			INCLUDE_DIRECTORIES("${LUA_INCLUDE_DIR}")
235		ENDIF(NOT LUA_FOUND)
236	ENDIF(ENABLE_LUAJIT MATCHES "ON")
237	SET(UCL_LUA_SRC lua/lua_ucl.c)
238	ADD_LIBRARY(lua-ucl ${LIB_TYPE} ${UCL_LUA_SRC})
239	IF(ENABLE_LUAJIT MATCHES "ON")
240		TARGET_LINK_LIBRARIES(lua-ucl "${LUAJIT_LIBRARY}")
241	ELSE(ENABLE_LUAJIT MATCHES "ON")
242		TARGET_LINK_LIBRARIES(lua-ucl "${LUA_LIBRARY}")
243	ENDIF(ENABLE_LUAJIT MATCHES "ON")
244	TARGET_LINK_LIBRARIES(lua-ucl ucl)
245	SET_TARGET_PROPERTIES(lua-ucl PROPERTIES VERSION ${LIBUCL_VERSION} SOVERSION ${LIBUCL_VERSION_MAJOR})
246ENDIF()
247
248IF(HAVE_FETCH_H)
249    TARGET_LINK_LIBRARIES(ucl fetch)
250ELSE(HAVE_FETCH_H)
251    IF(CURL_FOUND)
252        TARGET_LINK_LIBRARIES(ucl ${CURL_LIBRARIES})
253    ENDIF(CURL_FOUND)
254ENDIF(HAVE_FETCH_H)
255IF(ENABLE_URL_SIGN MATCHES "ON")
256	IF(OPENSSL_FOUND)
257		TARGET_LINK_LIBRARIES(ucl ${OPENSSL_LIBRARIES})
258	ENDIF(OPENSSL_FOUND)
259ENDIF(ENABLE_URL_SIGN MATCHES "ON")
260