xref: /freebsd/contrib/libucl/CMakeLists.txt (revision a0ee8cc636cd5c2374ec44ca71226564ea0bca95)
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		src/xxhash.c)
210
211
212SET (LIB_TYPE STATIC)
213IF (BUILD_SHARED_LIBS)
214  SET (LIB_TYPE SHARED)
215ENDIF (BUILD_SHARED_LIBS)
216ADD_LIBRARY(ucl ${LIB_TYPE} ${UCLSRC})
217SET_TARGET_PROPERTIES(ucl PROPERTIES VERSION ${LIBUCL_VERSION} SOVERSION ${LIBUCL_VERSION_MAJOR})
218
219IF(ENABLE_LUA MATCHES "ON")
220	IF(ENABLE_LUAJIT MATCHES "ON")
221		FindLua(VERSION_MAJOR "5" VERSION_MINOR "1" ROOT "${LUA_ROOT}")
222		IF(NOT LUA_FOUND)
223			MESSAGE(FATAL_ERROR "Lua not found, lua support is required")
224		ELSE(NOT LUA_FOUND)
225			INCLUDE_DIRECTORIES("${LUA_INCLUDE_DIR}")
226		ENDIF(NOT LUA_FOUND)
227	ELSE(ENABLE_LUAJIT MATCHES "ON")
228		FindLua(VERSION_MAJOR "5" VERSION_MINOR "2" ROOT "${LUA_ROOT}")
229		IF(NOT LUA_FOUND)
230			FindLua(VERSION_MAJOR "5" VERSION_MINOR "1" ROOT "${LUA_ROOT}")
231		ENDIF(NOT LUA_FOUND)
232		IF(NOT LUA_FOUND)
233			MESSAGE(FATAL_ERROR "Lua not found, lua support is required")
234		ELSE(NOT LUA_FOUND)
235			INCLUDE_DIRECTORIES("${LUA_INCLUDE_DIR}")
236		ENDIF(NOT LUA_FOUND)
237	ENDIF(ENABLE_LUAJIT MATCHES "ON")
238	SET(UCL_LUA_SRC lua/lua_ucl.c)
239	ADD_LIBRARY(lua-ucl ${LIB_TYPE} ${UCL_LUA_SRC})
240	IF(ENABLE_LUAJIT MATCHES "ON")
241		TARGET_LINK_LIBRARIES(lua-ucl "${LUAJIT_LIBRARY}")
242	ELSE(ENABLE_LUAJIT MATCHES "ON")
243		TARGET_LINK_LIBRARIES(lua-ucl "${LUA_LIBRARY}")
244	ENDIF(ENABLE_LUAJIT MATCHES "ON")
245	TARGET_LINK_LIBRARIES(lua-ucl ucl)
246	SET_TARGET_PROPERTIES(lua-ucl PROPERTIES VERSION ${LIBUCL_VERSION} SOVERSION ${LIBUCL_VERSION_MAJOR})
247ENDIF()
248
249IF(HAVE_FETCH_H)
250    TARGET_LINK_LIBRARIES(ucl fetch)
251ELSE(HAVE_FETCH_H)
252    IF(CURL_FOUND)
253        TARGET_LINK_LIBRARIES(ucl ${CURL_LIBRARIES})
254    ENDIF(CURL_FOUND)
255ENDIF(HAVE_FETCH_H)
256IF(ENABLE_URL_SIGN MATCHES "ON")
257	IF(OPENSSL_FOUND)
258		TARGET_LINK_LIBRARIES(ucl ${OPENSSL_LIBRARIES})
259	ENDIF(OPENSSL_FOUND)
260ENDIF(ENABLE_URL_SIGN MATCHES "ON")
261