SET(LIBEVSRC	ev.c)

CHECK_INCLUDE_FILES(sys/types.h HAVE_SYS_TYPES_H)
CHECK_INCLUDE_FILES(sys/inotify.h HAVE_SYS_INOTIFY_H)
CHECK_INCLUDE_FILES(sys/epoll.h HAVE_SYS_EPOLL_H)
CHECK_INCLUDE_FILES("sys/types.h;sys/event.h;sys/time.h" HAVE_SYS_EVENT_H)
CHECK_INCLUDE_FILES(sys/queue.h HAVE_SYS_QUEUE_H)
CHECK_INCLUDE_FILES(sys/stat.h HAVE_SYS_STAT_H)
CHECK_INCLUDE_FILES(sys/signalfd.h HAVE_SYS_SIGNALFD_H)
CHECK_INCLUDE_FILES(port.h HAVE_PORT_H)
CHECK_INCLUDE_FILES(poll.h HAVE_POLL_H)
CHECK_INCLUDE_FILES(memory.h HAVE_MEMORY_H)
CHECK_INCLUDE_FILES(sys/select.h HAVE_SYS_SELECT_H)
CHECK_INCLUDE_FILES(sys/eventfd.h HAVE_SYS_EVENTFD_H)
CHECK_INCLUDE_FILES(sys/timerfd.h HAVE_SYS_TIMERFD_H)
CHECK_INCLUDE_FILES(linux/fs.h HAVE_LINUX_FS_H)
CHECK_INCLUDE_FILES(linux/aio_abi.h HAVE_LINUX_AIO_ABI_H)

IF(HAVE_SYS_INOTIFY_H)
	CHECK_SYMBOL_EXISTS(inotify_init "sys/types.h;sys/inotify.h" HAVE_INOTIFY_INIT)
ENDIF()
IF(HAVE_SYS_EPOLL_H)
	CHECK_SYMBOL_EXISTS(epoll_ctl "sys/types.h;sys/epoll.h" HAVE_EPOLL_CTL)
ENDIF()
IF(HAVE_SYS_EVENT_H)
	CHECK_SYMBOL_EXISTS(kqueue "sys/types.h;sys/event.h;sys/time.h" HAVE_KQUEUE)
ENDIF()
IF(HAVE_PORT_H)
	CHECK_SYMBOL_EXISTS(port_create port.h HAVE_PORT_CREATE)
ENDIF()
IF(HAVE_POLL_H)
	CHECK_SYMBOL_EXISTS(poll poll.h HAVE_POLL)
ENDIF()
IF(HAVE_SYS_SELECT_H)
	CHECK_SYMBOL_EXISTS(select sys/select.h HAVE_SELECT)
ENDIF()
IF(HAVE_SYS_EVENTFD_H)
	CHECK_SYMBOL_EXISTS(eventfd sys/eventfd.h HAVE_EVENTFD)
ENDIF()
IF(HAVE_SYS_SIGNALFD_H)
	CHECK_SYMBOL_EXISTS(signalfd sys/signalfd.h HAVE_SIGNALFD)
ENDIF()
IF(HAVE_SYS_TIMERFD_H)
	CHECK_SYMBOL_EXISTS(timerfd_create sys/timerfd.h HAVE_TIMERFD)
ENDIF()
IF(HAVE_LINUX_FS_H)
	CHECK_SYMBOL_EXISTS(RWF_SUPPORTED linux/fs.h HAVE_KERNEL_RWF_T)
ENDIF()
CHECK_SYMBOL_EXISTS(time.h nanosleep HAVE_NANOSLEEP)

# check first without rt
CHECK_FUNCTION_EXISTS(clock_gettime HAVE_CLOCK_GETTIME)

CHECK_LIBRARY_EXISTS(rt clock_gettime "" HAVE_LIBRT)
# then check with rt
CHECK_LIBRARY_EXISTS(rt clock_gettime "" HAVE_CLOCK_GETTIME)
CHECK_LIBRARY_EXISTS(m ceil "" HAVE_LIBM)

CONFIGURE_FILE(config.h.in libev-config.h)

IF(ENABLE_STATIC MATCHES "ON")
	ADD_LIBRARY(rspamd-ev STATIC ${LIBEVSRC})
ELSE()
	ADD_LIBRARY(rspamd-ev SHARED ${LIBEVSRC})
ENDIF()
target_link_libraries(rspamd-ev "${RSPAMD_REQUIRED_LIBRARIES}")
include_directories("${CMAKE_CURRENT_BINARY_DIR}")
ADD_DEFINITIONS("-DEV_CONFIG_H=\"libev-config.h\""
		-DEV_MULTIPLICITY=1
		-DEV_USE_FLOOR=1
		-DEV_NO_THREADS=1 # We do not have threads in Rspamd!
		-DEV_FEATURES=127 # Enable all features
		)
IF(HAVE_EVENTFD)
	ADD_DEFINITIONS(-DEV_USE_EVENTFD=1)
ENDIF()

IF(ENABLE_FULL_DEBUG MATCHES "ON")
	ADD_DEFINITIONS(-DEV_VERIFY=3)
ENDIF()

INSTALL(TARGETS rspamd-ev LIBRARY DESTINATION ${RSPAMD_LIBDIR})
