<commit_before>#
# Rspamd - rambler antispam system
#
# Cmake configuration file
#

############################# INITIAL SECTION #############################################

PROJECT(rspamd C)

SET(RSPAMD_VERSION_MAJOR 0)
SET(RSPAMD_VERSION_MINOR 6)
SET(RSPAMD_VERSION_PATCH 0)


SET(RSPAMD_VERSION         "${RSPAMD_VERSION_MAJOR}.${RSPAMD_VERSION_MINOR}.${RSPAMD_VERSION_PATCH}")
SET(RSPAMD_MASTER_SITE_URL "http://bitbucket.org/vstakhov/rspamd")

IF(NOT RSPAMD_USER)
	SET(RSPAMD_USER "nobody")
	SET(RSPAMD_GROUP "nobody")
ENDIF(NOT RSPAMD_USER)

CMAKE_MINIMUM_REQUIRED(VERSION 2.6.0 FATAL_ERROR)

############################# OPTIONS SECTION #############################################

OPTION(DEBUG_MODE          "Enable debug output [default: ON]"                  ON)
OPTION(ENABLE_OPTIMIZATION "Enable optimization [default: OFF]"                 OFF)
OPTION(SKIP_RELINK_RPATH   "Skip relinking and full RPATH for the install tree" OFF)
OPTION(ENABLE_REDIRECTOR   "Enable redirector install [default: OFF]"           OFF)
OPTION(ENABLE_PROFILING    "Enable profiling [default: OFF]"                    OFF)
OPTION(ENABLE_GPERF_TOOLS  "Enable google perftools [default: OFF]"             OFF)
OPTION(ENABLE_STATIC       "Enable static compiling [default: OFF]"             OFF)
OPTION(ENABLE_LUAJIT       "Link with libluajit [default: OFF]"                 OFF)
OPTION(ENABLE_JUDY         "Find and link with Judy library [default: ON]"      ON)
OPTION(ENABLE_DB           "Find and link with DB library [default: OFF]"       OFF)
OPTION(ENABLE_SQLITE       "Find and link with sqlite3 library [default: OFF]"  OFF)
OPTION(ENABLE_HIREDIS      "Find and link with external redis library [default: OFF]"  OFF)
OPTION(NO_SHARED           "Build internal libs static [default: OFF]"          OFF)
OPTION(FORCE_GMIME24       "Link with gmime2.4 [default: OFF]"                  OFF)

# Build optimized code for following CPU (default i386)
#SET(CPU_TUNE               "i686")

############################# INCLUDE SECTION #############################################

INCLUDE(CheckIncludeFiles)
INCLUDE(CheckFunctionExists)
INCLUDE(CheckSymbolExists)
INCLUDE(CheckCSourceCompiles)
INCLUDE(CheckLibraryExists)
INCLUDE(FindPkgConfig)
INCLUDE(CheckCCompilerFlag)
INCLUDE(FindPerl)
INCLUDE(FindOpenSSL)

############################# MODULES SECTION #############################################

MACRO(_AddModulesForced MLIST WLIST)
# Generate unique string for this build
	STRING(RANDOM LENGTH 8 _MODULES_ID)
	SET(MODULES_ID ${_MODULES_ID} CACHE INTERNAL "Modules ID" FORCE)
	FILE(WRITE "src/modules.c" "/* ${MODULES_ID} */\n#include \"config.h\"\n")
	
	# Handle even old cmake
	LIST(LENGTH ${MLIST} MLIST_COUNT)
	LIST(LENGTH ${WLIST} WLIST_COUNT)
	MATH(EXPR MLIST_MAX ${MLIST_COUNT}-1)
	MATH(EXPR WLIST_MAX ${WLIST_COUNT}-1)
	
	FOREACH(MOD_IDX RANGE ${MLIST_MAX})
		LIST(GET ${MLIST} ${MOD_IDX} MOD)
		FILE(APPEND "src/modules.c" "extern module_t ${MOD}_module;\n")
	ENDFOREACH(MOD_IDX RANGE ${MLIST_MAX})
		 
	FILE(APPEND "src/modules.c" "\n\nmodule_t *modules[] = {\n")
	
	FOREACH(MOD_IDX RANGE ${MLIST_MAX})
		LIST(GET ${MLIST} ${MOD_IDX} MOD)
		FILE(APPEND "src/modules.c" "&${MOD}_module,\n")
	ENDFOREACH(MOD_IDX RANGE ${MLIST_MAX})
	
	FILE(APPEND "src/modules.c" "NULL\n};\n")
	
	FOREACH(MOD_IDX RANGE ${WLIST_MAX})
		LIST(GET ${WLIST} ${MOD_IDX} WRK)
		FILE(APPEND "src/modules.c" "extern worker_t ${WRK}_worker;\n")
	ENDFOREACH(MOD_IDX RANGE ${WLIST_MAX})
	 
	FILE(APPEND "src/modules.c" "\n\nworker_t *workers[] = {\n")
	
	FOREACH(MOD_IDX RANGE ${WLIST_MAX})
		LIST(GET ${WLIST} ${MOD_IDX} WRK)
		FILE(APPEND "src/modules.c" "&${WRK}_worker,\n")
	ENDFOREACH(MOD_IDX RANGE ${WLIST_MAX})
	FILE(APPEND "src/modules.c" "NULL\n};\n")
ENDMACRO(_AddModulesForced MLIST WLIST)

MACRO(AddModules MLIST WLIST)
	_AddModulesForced(${MLIST} ${WLIST})
	#IF(NOT EXISTS "src/modules.c")
	#	_AddModulesForced(${MLIST} ${WLIST})
	#ELSE(NOT EXISTS "src/modules.c")
	#	FILE(STRINGS "src/modules.c" FILE_ID_RAW REGEX "^/.*[a-zA-Z0-9]+.*/$")
	#	STRING(REGEX MATCH "[a-zA-Z0-9]+" FILE_ID "${FILE_ID_RAW}")
	#	IF(NOT FILE_ID STREQUAL MODULES_ID)
	#		MESSAGE("Regenerate modules info")
	#		_AddModulesForced(${MLIST} ${WLIST})
	#	ENDIF(NOT FILE_ID STREQUAL MODULES_ID)
	#ENDIF(NOT EXISTS "src/modules.c")
ENDMACRO(AddModules MLIST WLIST)

############################# CONFIG SECTION #############################################
# Initial set

IF(CMAKE_INSTALL_PREFIX)
    SET(PREFIX ${CMAKE_INSTALL_PREFIX})
ENDIF(CMAKE_INSTALL_PREFIX)

LIST(APPEND CMAKE_REQUIRED_LIBRARIES m)

# Try to detect platform for further configuration
CHECK_C_SOURCE_COMPILES ("#if !defined(__x86_64) && !defined(__amd64) && !defined(_M_X64)
					#error assume 32 bit arch
					#endif
					int main() { return 0;}" AMD_64)
IF(NOT BUILD_CPU_MODE)
	IF(AMD_64)
		SET (BUILD_CPU_MODE "64")
	ELSE(AMD_64)
		SET (BUILD_CPU_MODE "32")
	ENDIF(AMD_64)
ENDIF(NOT BUILD_CPU_MODE)

# Platform specific configuration
IF(CMAKE_SYSTEM_NAME STREQUAL "FreeBSD")
	SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -D_BSD_SOURCE -DFREEBSD")
	CONFIGURE_FILE(freebsd/rspamd.sh.in freebsd/rspamd @ONLY)

	IF(ENABLE_REDIRECTOR MATCHES "ON")
		CONFIGURE_FILE(freebsd/redirector.sh.in freebsd/redirector @ONLY)
	ENDIF(ENABLE_REDIRECTOR MATCHES "ON")
	IF(CMAKE_INSTALL_PREFIX)
    	IF(NOT ETC_PREFIX)
	    	SET(ETC_PREFIX "${CMAKE_INSTALL_PREFIX}/etc")
    	ENDIF(NOT ETC_PREFIX)
    	IF(NOT MAN_PREFIX)
	    	SET(MAN_PREFIX "${CMAKE_INSTALL_PREFIX}/man")
    	ENDIF(NOT MAN_PREFIX)
    	IF(NOT LOCALSTATES_PREFIX)
	    	SET(LOCALSTATES_PREFIX "${CMAKE_INSTALL_PREFIX}/var/run/rspamd")
    	ENDIF(NOT LOCALSTATES_PREFIX)
	    SET(PREFIX "${CMAKE_INSTALL_PREFIX}")
	ELSE(CMAKE_INSTALL_PREFIX)
    	IF(NOT ETC_PREFIX)
    		SET(ETC_PREFIX /usr/local/etc)
    	ENDIF(NOT ETC_PREFIX)
    	IF(NOT MAN_PREFIX)
    		SET(MAN_PREFIX /usr/local/man)
    	ENDIF(NOT MAN_PREFIX)
    	IF(NOT LOCALSTATES_PREFIX)
	    	SET(LOCALSTATES_PREFIX "/var/run/rspamd")
    	ENDIF(NOT LOCALSTATES_PREFIX)
    	IF(NOT PREFIX)
    		SET(PREFIX /usr/local)
    	ENDIF(NOT PREFIX)
	ENDIF(CMAKE_INSTALL_PREFIX)
    FIND_LIBRARY(LIBINTL_LIBRARY NAMES intl PATHS   /lib
                                               /opt/lib
                                               /usr/lib
                                               /usr/local/lib
                 DOC "Path where the libintl library can be found")
    LIST(APPEND CMAKE_REQUIRED_LIBRARIES ${LIBINTL_LIBRARY})
	MESSAGE(STATUS "Configuring for FreeBSD")
 
ENDIF(CMAKE_SYSTEM_NAME STREQUAL "FreeBSD")

IF(CMAKE_SYSTEM_NAME STREQUAL "Darwin")
	SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -D_BSD_SOURCE -DDARWIN")
    
    IF(CMAKE_INSTALL_PREFIX)
    	IF(NOT ETC_PREFIX)
	    	SET(ETC_PREFIX "${CMAKE_INSTALL_PREFIX}/etc")
	    ENDIF(NOT ETC_PREFIX)
	    IF(NOT MAN_PREFIX)
	    	SET(MAN_PREFIX "${CMAKE_INSTALL_PREFIX}/man")
	   	ENDIF(NOT MAN_PREFIX)
	    IF(NOT LOCALSTATES_PREFIX)
	    	SET(LOCALSTATES_PREFIX "${CMAKE_INSTALL_PREFIX}/var/run/rspamd")
    	ENDIF(NOT LOCALSTATES_PREFIX)
	    SET(PREFIX "${CMAKE_INSTALL_PREFIX}")
	ELSE(CMAKE_INSTALL_PREFIX)    	
    	IF(NOT ETC_PREFIX)
    		SET(ETC_PREFIX /usr/local/etc)
    	ENDIF(NOT ETC_PREFIX)
    	IF(NOT MAN_PREFIX)
    		SET(MAN_PREFIX /usr/local/man)
    	ENDIF(NOT MAN_PREFIX)
    	IF(NOT LOCALSTATES_PREFIX)
	    	SET(LOCALSTATES_PREFIX "/var/run/rspamd")
    	ENDIF(NOT LOCALSTATES_PREFIX)
    	IF(NOT PREFIX)
    		SET(PREFIX /usr/local)
    	ENDIF(NOT PREFIX)
    ENDIF(CMAKE_INSTALL_PREFIX)

    FIND_LIBRARY(LIBINTL_LIBRARY NAMES intl PATHS   /lib
                                               /opt/lib
                                               /usr/lib
                                               /usr/local/lib
                 DOC "Path where the libintl library can be found")
    LIST(APPEND CMAKE_REQUIRED_LIBRARIES ${LIBINTL_LIBRARY})
	MESSAGE(STATUS "Configuring for Darwin")

ENDIF(CMAKE_SYSTEM_NAME STREQUAL "Darwin")

IF(CMAKE_SYSTEM_NAME STREQUAL "Linux")
	SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -D_GNU_SOURCE -DLINUX")
	IF(CMAKE_INSTALL_PREFIX)
		IF(NOT ETC_PREFIX)
	    	SET(ETC_PREFIX "${CMAKE_INSTALL_PREFIX}/etc")
	    ENDIF(NOT ETC_PREFIX)
	    
	    IF(NOT MAN_PREFIX)
	    	SET(MAN_PREFIX "${CMAKE_INSTALL_PREFIX}/share/man")
	    ENDIF(NOT MAN_PREFIX)
	    IF(NOT LOCALSTATES_PREFIX)
	    	SET(LOCALSTATES_PREFIX "${CMAKE_INSTALL_PREFIX}/var/run/rspamd")
    	ENDIF(NOT LOCALSTATES_PREFIX)
	    
	    SET(PREFIX "${CMAKE_INSTALL_PREFIX}")
	ELSE(CMAKE_INSTALL_PREFIX)    	
    	IF(NOT ETC_PREFIX)
    		SET(ETC_PREFIX /etc)
    	ENDIF(NOT ETC_PREFIX)
    	IF(NOT PREFIX)
    		SET(PREFIX /usr)
    	ENDIF(NOT PREFIX)
    	IF(NOT MAN_PREFIX)
    		SET(MAN_PREFIX /usr/share/man)
    	ENDIF(NOT MAN_PREFIX)
    	IF(NOT LOCALSTATES_PREFIX)
	    	SET(LOCALSTATES_PREFIX "/var/run/rspamd")
    	ENDIF(NOT LOCALSTATES_PREFIX)
    ENDIF(CMAKE_INSTALL_PREFIX)
    IF(BUILD_CPU_MODE STREQUAL "32")
    	SET(CMAKE_C_FLAGS  "-D_FILE_OFFSET_BITS=64 -D_LARGEFILE64_SOURCE ${CMAKE_C_FLAGS}")
    ENDIF(BUILD_CPU_MODE STREQUAL "32")
    # Workaround with architecture specific includes
    IF(IS_DIRECTORY "/usr/include/${CMAKE_SYSTEM_PROCESSOR}-linux-gnu/") 
    	INCLUDE_DIRECTORIES("/usr/include/${CMAKE_SYSTEM_PROCESSOR}-linux-gnu/")
    	SET(CMAKE_REQUIRED_INCLUDES "/usr/include/${CMAKE_SYSTEM_PROCESSOR}-linux-gnu/")
    ENDIF(IS_DIRECTORY "/usr/include/${CMAKE_SYSTEM_PROCESSOR}-linux-gnu/")
    
	LIST(APPEND CMAKE_REQUIRED_LIBRARIES rt)
	LIST(APPEND CMAKE_REQUIRED_LIBRARIES dl)
	MESSAGE(STATUS "Configuring for Linux")
	IF(EXISTS "/etc/debian_version")
		SET(LINUX_START_SCRIPT "rspamd_debian.in")
	ELSE(EXISTS "/etc/debian_version")
		SET(LINUX_START_SCRIPT "rspamd_rh.in")
	ENDIF(EXISTS "/etc/debian_version")
ENDIF(CMAKE_SYSTEM_NAME STREQUAL "Linux")

IF(CMAKE_SYSTEM_NAME STREQUAL "SunOS")
 	IF(NOT BUILD_CPU_MODE)
	 	SET (BUILD_CPU_MODE "32")
	ENDIF(NOT BUILD_CPU_MODE)
	
	IF("${CMAKE_C_COMPILER_ID}" MATCHES SunPro)
		SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Xa -xregs=no%frameptr -xstrconst -xc99")
		SET(SUN_BUILD32 "-xtarget=generic")
		SET(SUN_BUILD64 "-xtarget=generic64")
		IF(ENABLE_OPTIMIZATION MATCHES "ON")
			SET(CMAKE_C_OPT_FLAGS "-fast -xdepend")
		ELSE(ENABLE_OPTIMIZATION MATCHES "ON")
			SET(CMAKE_C_OPT_FLAGS "-xO0")
		ENDIF(ENABLE_OPTIMIZATION MATCHES "ON")
	ELSE("${CMAKE_C_COMPILER_ID}" MATCHES SunPro)
		set(SUN_BUILD32 "-m32")
		set(SUN_BUILD64 "-m64")		
	ENDIF("${CMAKE_C_COMPILER_ID}" MATCHES SunPro)

 	IF (BUILD_CPU_MODE STREQUAL "32")
       message (STATUS "Building 32-bit mode on Solaris")
       # If we are asked to build 32 bit mode
       	set(CMAKE_C_FLAGS  "${SUN_BUILD32} ${CMAKE_C_FLAGS}")
       	set(CMAKE_SHARED_LINKER_FLAGS  "${CMAKE_SHARED_LINKER_FLAGS} ${SUN_BUILD32}")
 	ELSE (BUILD_CPU_MODE STREQUAL "32")
       # solaris 64-bit mode
		message (STATUS "Building 64-bit mode on Solaris")
       	set(CMAKE_C_FLAGS  "${SUN_BUILD64} ${CMAKE_C_FLAGS}")
       	set(CMAKE_SHARED_LINKER_FLAGS  "${CMAKE_SHARED_LINKER_FLAGS} ${SUN_BUILD64}")
    ENDIF (BUILD_CPU_MODE STREQUAL "32")

 	SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -D__EXTENSIONS__ -DSOLARIS -D_POSIX_SOURCE -D_POSIX_C_SOURCE=200112L")

	IF(CMAKE_INSTALL_PREFIX)
		IF(NOT ETC_PREFIX)
	    	SET(ETC_PREFIX "${CMAKE_INSTALL_PREFIX}/etc")
	    ENDIF(NOT ETC_PREFIX)
	    IF(NOT MAN_PREFIX)
	    	SET(MAN_PREFIX "${CMAKE_INSTALL_PREFIX}/man")
	    ENDIF(NOT MAN_PREFIX)
	    IF(NOT LOCALSTATES_PREFIX)
	    	SET(LOCALSTATES_PREFIX "/var/run/rspamd")
    	ENDIF(NOT LOCALSTATES_PREFIX)
	    SET(PREFIX "${CMAKE_INSTALL_PREFIX}")
	ELSE(CMAKE_INSTALL_PREFIX)    
    	IF(NOT ETC_PREFIX)
    		SET(ETC_PREFIX /etc)
    	ENDIF(NOT ETC_PREFIX)
    	IF(NOT PREFIX)
    		SET(PREFIX /usr)
    	ENDIF(NOT PREFIX)
    	IF(NOT MAN_PREFIX)
    		SET(MAN_PREFIX /usr/share/man)
    	ENDIF(NOT MAN_PREFIX)
    	IF(NOT LOCALSTATES_PREFIX)
	    	SET(LOCALSTATES_PREFIX "/var/run/rspamd")
    	ENDIF(NOT LOCALSTATES_PREFIX)
    ENDIF(CMAKE_INSTALL_PREFIX)
    
	LIST(APPEND CMAKE_REQUIRED_LIBRARIES rt)
	LIST(APPEND CMAKE_REQUIRED_LIBRARIES dl)
	LIST(APPEND CMAKE_REQUIRED_LIBRARIES resolv)
 	LIST(APPEND CMAKE_REQUIRED_LIBRARIES nsl)
 	LIST(APPEND CMAKE_REQUIRED_LIBRARIES socket)
 	LIST(APPEND CMAKE_REQUIRED_LIBRARIES umem)
 	# Ugly hack, but FindOpenSSL on Solaris does not link with libcrypto
 	LIST(APPEND CMAKE_REQUIRED_LIBRARIES crypto)
  	SET(CMAKE_VERBOSE_MAKEFILE ON)
  	SET(CMAKE_INSTALL_RPATH_USE_LINK_PATH FALSE)
  	SET(CMAKE_INSTALL_RPATH "${PREFIX}/lib")	
ENDIF(CMAKE_SYSTEM_NAME STREQUAL "SunOS")

# Set libdir
IF(NOT LIBDIR)
	SET(LIBDIR lib/rspamd)
	IF(NOT CMAKE_INSTALL_RPATH)
		SET(CMAKE_INSTALL_RPATH ${PREFIX}/${LIBDIR})
	ENDIF(NOT CMAKE_INSTALL_RPATH)
ENDIF(NOT LIBDIR)

# Set includedir
IF(NOT INCLUDEDIR)
	SET(INCLUDEDIR include/rspamd)
ENDIF(NOT INCLUDEDIR)

# Check prefixes
IF(NOT ETC_PREFIX)
	IF(CMAKE_INSTALL_PREFIX)
		MESSAGE(WARNING "Your system is unknown for rspamd, assume defining ETC_PREFIX and MAN_PREFIX by -D option, now ETC_PREFIX and MAN_PREFIX are relative to ${CMAKE_INSTALL_PREFIX}")
		SET(ETC_PREFIX "${CMAKE_INSTALL_PREFIX}/etc")
	ELSE(CMAKE_INSTALL_PREFIX)
		MESSAGE(FATAL_ERROR "Your system is unknown for rspamd and no CMAKE_INSTALL_PREFIX specified, assume defining CMAKE_INSTALL_PREFIX")
	ENDIF(CMAKE_INSTALL_PREFIX) 
	MESSAGE(STATUS "Configuring for ${CMAKE_SYSTEM_NAME}")
ENDIF(NOT ETC_PREFIX)
IF(NOT MAN_PREFIX)
	IF(CMAKE_INSTALL_PREFIX)
		SET(MAN_PREFIX "${CMAKE_INSTALL_PREFIX}/man")
	ENDIF(CMAKE_INSTALL_PREFIX) 
ENDIF(NOT MAN_PREFIX)
IF(NOT LOCALSTATES_PREFIX)
	IF(CMAKE_INSTALL_PREFIX)
		SET(LOCALSTATES_PREFIX "${CMAKE_INSTALL_PREFIX}/var/run/rspamd")
	ENDIF(CMAKE_INSTALL_PREFIX) 
ENDIF(NOT LOCALSTATES_PREFIX)

# Now CMAKE_INSTALL_PREFIX is a base prefix for everything
# ETC_PREFIX - for configuration
# MAN_PREFIX - for manual pages
# LOCALSTATES_PREFIX - for runtime files

# Now find libraries and headers

# Find lua libraries
IF(NOT LUA_VERSION_MAJOR OR NOT LUA_VERSION_MINOR)
	SET(LUA_VERSION_MAJOR "5")
	SET(LUA_VERSION_MINOR "1")
ENDIF(NOT LUA_VERSION_MAJOR OR NOT LUA_VERSION_MINOR)

FIND_PATH(LUA_INCLUDE_DIR lua.h
  HINTS
  $ENV{LUA_DIR}
  PATH_SUFFIXES "include/lua${LUA_VERSION_MAJOR}${LUA_VERSION_MINOR}" 
  				"include/lua${LUA_VERSION_MAJOR}.${LUA_VERSION_MINOR}" 
  				include/lua include
  PATHS
  /usr/local
  /usr
  ~/Library/Frameworks
  /Library/Frameworks
  /sw # Fink
  /opt/local # DarwinPorts
  /opt/csw # Blastwave
  /opt
)

FIND_LIBRARY(LUA_LIBRARY 
  NAMES lua 
  		"lua${LUA_VERSION_MAJOR}${LUA_VERSION_MINOR}"
  		"lua${LUA_VERSION_MAJOR}.${LUA_VERSION_MINOR}" 
  		"lua-${LUA_VERSION_MAJOR}.${LUA_VERSION_MINOR}"
  HINTS
  $ENV{LUA_DIR}
  PATH_SUFFIXES lib64 lib
  PATHS
  /usr/local
  /usr
  ~/Library/Frameworks
  /Library/Frameworks
  /sw
  /opt/local
  /opt/csw
  /opt
  DOC "Lua library"
)

IF(LUA_LIBRARY AND LUA_INCLUDE_DIR)
	SET(LUA_FOUND 1)
ENDIF(LUA_LIBRARY AND LUA_INCLUDE_DIR)

# Check for luajit
IF(ENABLE_LUAJIT MATCHES "ON")
    FIND_LIBRARY(LUAJIT_LIBRARY NAMES
    			"luajit${LUA_VERSION_MAJOR}${LUA_VERSION_MINOR}"
    			"luajit${LUA_VERSION_MAJOR}.${LUA_VERSION_MINOR}"
    			"luajit-${LUA_VERSION_MAJOR}.${LUA_VERSION_MINOR}"
    			 luajit
                  HINTS
                  $ENV{LUA_DIR}
                  PATH_SUFFIXES lib64 lib
                  PATHS
                      ~/Library/Frameworks
                      /Library/Frameworks
                      /usr/local
                      /usr
                      /sw
                      /opt/local
                      /opt/csw
                      /opt
                )
     IF(NOT LUAJIT_LIBRARY)
         MESSAGE(FATAL_ERROR "Luajit not found but enabled in configuration")
     ENDIF(NOT LUAJIT_LIBRARY)
ENDIF(ENABLE_LUAJIT MATCHES "ON")

IF(NOT LUA_FOUND)
	MESSAGE(FATAL_ERROR "Lua not found, lua support is required for working")
ELSE(NOT LUA_FOUND)
    SET(WITH_LUA 1)
    INCLUDE_DIRECTORIES("${LUA_INCLUDE_DIR}")
ENDIF(NOT LUA_FOUND)

# Check and link to pcre
pkg_check_modules(PCRE REQUIRED libpcre)
IF(PCRE_INCLUDE_DIRS)
	INCLUDE_DIRECTORIES("${PCRE_INCLUDE_DIRS}")
ENDIF(PCRE_INCLUDE_DIRS)
IF(PCRE_LIBRARY_DIRS)
	LINK_DIRECTORIES("${PCRE_LIBRARY_DIRS}")
ENDIF(PCRE_LIBRARY_DIRS)

IF(ENABLE_SQLITE MATCHES "ON")
	# Find optional sqlite3 support
	pkg_check_modules(SQLITE sqlite3>=3.6.0)
	IF(SQLITE_FOUND)
		SET(WITH_SQLITE 1)
	ENDIF(SQLITE_FOUND)
	IF(SQLITE_INCLUDE_DIRS)
		INCLUDE_DIRECTORIES("${SQLITE_INCLUDE_DIRS}")
	ENDIF(SQLITE_INCLUDE_DIRS)
	IF(SQLITE_LIBRARY_DIRS)
		LINK_DIRECTORIES("${SQLITE_LIBRARY_DIRS}")
	ENDIF(SQLITE_LIBRARY_DIRS)
ENDIF(ENABLE_SQLITE MATCHES "ON")

#Check for openssl (required for dkim)
IF(OPENSSL_FOUND)
	SET(HAVE_OPENSSL 1)
	INCLUDE_DIRECTORIES("${OPENSSL_INCLUDE_DIR}")
ENDIF(OPENSSL_FOUND)

IF(ENABLE_STATIC MATCHES "ON")
	pkg_check_modules(GLIB2 REQUIRED glib-2.0>=2.12 gthread-2.0 gmodule-2.0)
	SET(LINK_TYPE "STATIC")
ELSE(ENABLE_STATIC MATCHES "ON")
	pkg_check_modules(GLIB2 REQUIRED glib-2.0>=2.12 gthread-2.0 gmodule-2.0)
	IF(NO_SHARED MATCHES "OFF")
		SET(LINK_TYPE "SHARED")
	ELSE(NO_SHARED MATCHES "OFF")
		SET(LINK_TYPE "STATIC")
	ENDIF(NO_SHARED MATCHES "OFF")
ENDIF(ENABLE_STATIC MATCHES "ON")
SET(GLIB2_VERSION "${GLIB2_glib-2.0_VERSION}")

IF(GLIB2_VERSION VERSION_GREATER "2.30.0")
	pkg_check_modules(LIBFFI libffi)
	IF(LIBFFI_FOUND)
		SET(GLIB2_LDFLAGS "${GLIB2_LDFLAGS};${LIBFFI_LDFLAGS}")
		SET(GLIB2_LIBRARIES "${GLIB2_LIBRARIES};${LIBFFI_LIBRARIES}")
		SET(GLIB2_STATIC_LDFLAGS "${GLIB2_STATIC_LDFLAGS};${LIBFFI_STATIC_LDFLAGS}")
		SET(GLIB2_CFLAGS "${GLIB2_CFLAGS};${LIBFFI_CFLAGS}")
	ENDIF(LIBFFI_FOUND)
ENDIF(GLIB2_VERSION VERSION_GREATER "2.30.0") 
pkg_check_modules(GMIME2 gmime-2.0)

IF(ENABLE_HIREDIS MATCHES "ON")
	# Try to find hiredis library
	pkg_check_modules(HIREDIS REQUIRED libhiredis)
	IF(HIREDIS_INCLUDE_DIRS)
		INCLUDE_DIRECTORIES("${HIREDIS_INCLUDE_DIRS}")
	ENDIF(HIREDIS_INCLUDE_DIRS)
	IF(HIREDIS_LIBRARY_DIRS)
		LINK_DIRECTORIES("${HIREDIS_LIBRARY_DIRS}")
	ENDIF(HIREDIS_LIBRARY_DIRS)
	IF(HIREDIS_FOUND)
		SET(WITH_SYSTEM_HIREDIS 1)
	ENDIF(HIREDIS_FOUND)
ENDIF(ENABLE_HIREDIS MATCHES "ON")

# Try to link with gmime24
IF(NOT GMIME2_FOUND OR FORCE_GMIME24)
	pkg_check_modules(GMIME24 gmime-2.4)
	IF(NOT GMIME24_FOUND)
		pkg_check_modules(GMIME24 REQUIRED gmime-2.6)
	ENDIF(NOT GMIME24_FOUND)
	SET(GMIME24 "yes")
	# Gmime2
	FOREACH(arg ${GMIME24_CFLAGS})
		SET(GMIME_CFLAGS "${GMIME_CFLAGS} ${arg}")
	ENDFOREACH(arg ${GMIME24_CFLAGS})

	IF(ENABLE_STATIC MATCHES "ON")
		FOREACH(arg ${GMIME24_STATIC_LDFLAGS})
			SET(GMIME_LDFLAGS "${GMIME_LDFLAGS} ${arg}")
		ENDFOREACH(arg ${GMIME24_LDFLAGS})
	ELSE(ENABLE_STATIC MATCHES "ON")
		FOREACH(arg ${GMIME24_LDFLAGS})
			SET(GMIME_LDFLAGS "${GMIME_LDFLAGS} ${arg}")
		ENDFOREACH(arg ${GMIME24_LDFLAGS})
	ENDIF(ENABLE_STATIC MATCHES "ON")
	IF(GMIME24_INCLUDE_DIRS)
		INCLUDE_DIRECTORIES(${GMIME24_INCLUDE_DIRS})
	ENDIF(GMIME24_INCLUDE_DIRS)
	IF(GMIME24_LIBRARY_DIRS)
		LINK_DIRECTORIES(${GMIME24_LIBRARY_DIRS})
	ENDIF(GMIME24_LIBRARY_DIRS)
ELSE(NOT GMIME2_FOUND OR FORCE_GMIME24)
	# Gmime2
	FOREACH(arg ${GMIME2_CFLAGS})
		SET(GMIME_CFLAGS "${GMIME_CFLAGS} ${arg}")
	ENDFOREACH(arg ${GMIME2_CFLAGS})

	IF(ENABLE_STATIC MATCHES "ON")
		FOREACH(arg ${GMIME2_STATIC_LDFLAGS})
			SET(GMIME_LDFLAGS "${GMIME_LDFLAGS} ${arg}")
		ENDFOREACH(arg ${GMIME2_LDFLAGS})
	ELSE(ENABLE_STATIC MATCHES "ON")
		FOREACH(arg ${GMIME2_LDFLAGS})
			SET(GMIME_LDFLAGS "${GMIME_LDFLAGS} ${arg}")
		ENDFOREACH(arg ${GMIME2_LDFLAGS})
	ENDIF(ENABLE_STATIC MATCHES "ON")
	IF(GMIME2_INCLUDE_DIRS)
		INCLUDE_DIRECTORIES(${GMIME2_INCLUDE_DIRS})
	ENDIF(GMIME2_INCLUDE_DIRS)
	IF(GMIME2_LIBRARY_DIRS)
		LINK_DIRECTORIES(${GMIME2_LIBRARY_DIRS})
	ENDIF(GMIME2_LIBRARY_DIRS)
ENDIF(NOT GMIME2_FOUND OR FORCE_GMIME24)

# Make from ; separated list normal space separated list
# Glib2
FOREACH(arg ${GLIB2_CFLAGS})
	SET(GLIB_CFLAGS "${GLIB_CFLAGS} ${arg}")
ENDFOREACH(arg ${GLIB2_CFLAGS})

IF(ENABLE_STATIC MATCHES "ON")
	FOREACH(arg ${GLIB2_STATIC_LDFLAGS})
		SET(GLIB_LDFLAGS "${GLIB_LDFLAGS} ${arg}")
	ENDFOREACH(arg ${GLIB2_LDFLAGS})
ELSE(ENABLE_STATIC MATCHES "ON")
	FOREACH(arg ${GLIB2_LDFLAGS})
		SET(GLIB_LDFLAGS "${GLIB_LDFLAGS} ${arg}")
	ENDFOREACH(arg ${GLIB2_LDFLAGS})
ENDIF(ENABLE_STATIC MATCHES "ON")


INCLUDE_DIRECTORIES(${GLIB2_INCLUDE_DIRS})
LINK_DIRECTORIES(${GLIB2_LIBRARY_DIRS})


FIND_LIBRARY(LIBZ_LIBRARY NAMES z PATH_SUFFIXES lib64 lib
                  PATHS
                      ~/Library/Frameworks
                      /Library/Frameworks
                      /usr/local
                      /usr
                      /sw
                      /opt/local
                      /opt/csw
                      /opt
                 DOC "Path where the libz library can be found")
IF(NOT LIBZ_LIBRARY)
	MESSAGE(FATAL_ERROR "libz is required for libgmime")
ENDIF(NOT LIBZ_LIBRARY)
# Check for libevent

FIND_LIBRARY(LIBEVENT_LIBRARY NAMES event PATH_SUFFIXES lib64 lib
                  PATHS
                      ~/Library/Frameworks
                      /Library/Frameworks
                      /usr/local
                      /usr
                      /sw
                      /opt/local
                      /opt/csw
                      /opt
                 DOC "Path where the libevent library can be found")
IF(NOT LIBEVENT_LIBRARY)
	MESSAGE(FATAL_ERROR "libevent is required for building rspamd")
ENDIF(NOT LIBEVENT_LIBRARY)

FIND_PATH(LIBEVENT_INCLUDE event.h PATHS 	/opt/include
										/usr/include
										/usr/local/include
										DOC "Path where the libevent header files can be found")

FIND_PATH(LIBEVENT_EVHTTP evhttp.h PATHS /opt/include
										/usr/include
										/usr/local/include
										DOC "Path where the libevhttp header files can be found")

GET_FILENAME_COMPONENT(LIBEVENT_PATH "${LIBEVENT_LIBRARY}" PATH)
INCLUDE_DIRECTORIES("${LIBEVENT_INCLUDE}")
LINK_DIRECTORIES("${LIBEVENT_PATH}")

IF(ENABLE_JUDY MATCHES "ON")
	# Find libjudy
	FIND_LIBRARY(LIBJUDY_LIBRARY NAMES Judy PATH_SUFFIXES lib64 lib
	                  PATHS
	                      ~/Library/Frameworks
	                      /Library/Frameworks
	                      /usr/local
	                      /usr
	                      /sw
	                      /opt/local
	                      /opt/csw
	                      /opt
	                 DOC "Path where the libjudy library can be found")
	IF(LIBJUDY_LIBRARY)
		FIND_PATH(LIBJUDY_INCLUDE Judy.h PATHS 	/opt/include
												/usr/include
												/usr/local/include
												DOC "Path where the judy header files can be found")
	
		GET_FILENAME_COMPONENT(LIBJUDY_PATH "${LIBJUDY_LIBRARY}" PATH)
		INCLUDE_DIRECTORIES("${LIBJUDY_INCLUDE}")
		LINK_DIRECTORIES("${LIBJUDY_PATH}")
		SET(WITH_JUDY 1)
	ENDIF(LIBJUDY_LIBRARY)
ENDIF(ENABLE_JUDY MATCHES "ON")

# Find libbd
IF(ENABLE_DB MATCHES "ON")
	FIND_LIBRARY(LIBDB_LIBRARY NAMES db PATHS PATH_SUFFIXES lib64 lib
	                  PATHS
	                      ~/Library/Frameworks
	                      /Library/Frameworks
	                      /usr/local
	                      /usr
	                      /sw
	                      /opt/local
	                      /opt/csw
	                      /opt
	                 DOC "Path where the libdb library can be found")
	IF(LIBDB_LIBRARY)
		FIND_PATH(LIBDB_INCLUDE db.h PATHS 	/opt/include
												/usr/include
												/usr/local/include
												DOC "Path where the bdb header files can be found")
	
		GET_FILENAME_COMPONENT(LIBDB_PATH "${LIBDB_LIBRARY}" PATH)
		INCLUDE_DIRECTORIES("${LIBDB_INCLUDE}")
		LINK_DIRECTORIES("${LIBDB_PATH}")
		SET(WITH_DB 1)
	ENDIF(LIBDB_LIBRARY)
ENDIF(ENABLE_DB MATCHES "ON")

IF(ENABLE_PROFILING MATCHES "ON")
	SET(WITH_PROFILER 1)
	SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -pg")
	SET(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -pg")
ENDIF(ENABLE_PROFILING MATCHES "ON")

# Static build

IF(ENABLE_STATIC MATCHES "ON")
	SET(BUILD_STATIC 1)
	SET(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -static -pthread")
	MESSAGE(STATUS "Static build of rspamd, no custom plugins support")
ENDIF(ENABLE_STATIC MATCHES "ON")

# Google performance tools

IF(ENABLE_GPERF_TOOLS MATCHES "ON")
	FIND_PATH(GPERF_INCLUDE google/profiler.h PATHS /opt/include
													/usr/include
													/usr/local/include
											  DOC "Path where google perftools includes can be found")
	INCLUDE_DIRECTORIES("${GPERF_INCLUDE}")
	FIND_LIBRARY(GPERF_LIBRARY NAMES profiler PATH_SUFFIXES lib64 lib
                  PATHS
                      ~/Library/Frameworks
                      /Library/Frameworks
                      /usr/local
                      /usr
                      /sw
                      /opt/local
                      /opt/csw
                      /opt
					DOC "Path where the gperf library can be found")
	IF(NOT GPERF_LIBRARY)
		MESSAGE(FATAL_ERROR "gperf tools support is enabled but not found in system")
	ENDIF(NOT GPERF_LIBRARY)
	GET_FILENAME_COMPONENT(GPERF_PATH "${GPERF_LIBRARY}" PATH)
	LINK_DIRECTORIES("${GPERF_PATH}")
	SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fno-omit-frame-pointer")
	SET(WITH_GPERF_TOOLS 1)
ENDIF(ENABLE_GPERF_TOOLS MATCHES "ON")

# Find util library

FIND_LIBRARY(LIBUTIL_LIBRARY NAMES util PATHS	PATH_SUFFIXES lib64 lib
                  PATHS
                      ~/Library/Frameworks
                      /Library/Frameworks
                      /usr/local
                      /usr
                      /sw
                      /opt/local
                      /opt/csw
                      /opt
                 DOC "Path where the libutil library can be found")
IF(LIBUTIL_LIBRARY)
	LIST(APPEND CMAKE_REQUIRED_LIBRARIES util)
	CHECK_FUNCTION_EXISTS(pidfile_open HAVE_PIDFILE)
	CHECK_FUNCTION_EXISTS(pidfile_fileno HAVE_PIDFILE_FILENO)

	FIND_FILE(HAVE_LIBUTIL_H NAMES libutil.h PATHS /usr/include
											   /opt/include
											   /usr/local/include
				DOC "Path to libutil header")
ENDIF(LIBUTIL_LIBRARY)

# Find libfetch (for FreeBSD)

FIND_LIBRARY(LIBFETCH_LIBRARY NAMES fetch PATHS	PATH_SUFFIXES lib64 lib
                  PATHS
                      ~/Library/Frameworks
                      /Library/Frameworks
                      /usr/local
                      /usr
                      /sw
                      /opt/local
                      /opt/csw
                      /opt
                 DOC "Path where the libfetch library can be found")
IF(LIBFETCH_LIBRARY)
	FIND_FILE(HAVE_FETCH_H NAMES fetch.h PATHS /usr/include
											   /opt/include
											   /usr/local/include
				DOC "Path to libfetch header")
ELSE(LIBFETCH_LIBRARY)
	# Try to find libcurl
	INCLUDE(FindCURL)
	IF(NOT CURL_FOUND)
		MESSAGE(WARNING "Neither libcurl nor libfetch were found, no support of URL includes in configuration")
	ELSE(NOT CURL_FOUND)
		INCLUDE_DIRECTORIES("${CURL_INCLUDE_DIRS}")
	ENDIF(NOT CURL_FOUND)
ENDIF(LIBFETCH_LIBRARY)

# Process with warn flags
SET(CMAKE_C_WARN_FLAGS "")
CHECK_C_COMPILER_FLAG(-Wall SUPPORT_WALL)
CHECK_C_COMPILER_FLAG(-W SUPPORT_W)
CHECK_C_COMPILER_FLAG(-Wpointer-arith SUPPORT_WPOINTER)
CHECK_C_COMPILER_FLAG(-Wno-unused-parameter SUPPORT_WPARAM)
CHECK_C_COMPILER_FLAG(-Wno-unused-function SUPPORT_WFUNCTION)
CHECK_C_COMPILER_FLAG(-Wunused-variable SUPPORT_WUNUSED_VAR)
CHECK_C_COMPILER_FLAG(-Wno-pointer-sign SUPPORT_WPOINTER_SIGN)
CHECK_C_COMPILER_FLAG(-Wstrict-prototypes SUPPORT_WSTRICT_PROTOTYPES)
CHECK_C_COMPILER_FLAG(-pedantic SUPPORT_PEDANTIC_FLAG)
IF(NOT "${CMAKE_C_COMPILER_ID}" MATCHES SunPro)
	CHECK_C_COMPILER_FLAG("-std=c99" SUPPORT_STD_FLAG)
ENDIF(NOT "${CMAKE_C_COMPILER_ID}" MATCHES SunPro)
IF(SUPPORT_W)
	SET(CMAKE_C_WARN_FLAGS "${CMAKE_C_WARN_FLAGS} -W")
ENDIF(SUPPORT_W)
IF(SUPPORT_WALL)
	SET(CMAKE_C_WARN_FLAGS "${CMAKE_C_WARN_FLAGS} -Wall")
ENDIF(SUPPORT_WALL)
IF(SUPPORT_WPOINTER)
	SET(CMAKE_C_WARN_FLAGS "${CMAKE_C_WARN_FLAGS} -Wpointer-arith")
ENDIF(SUPPORT_WPOINTER)
IF(SUPPORT_WPARAM)
	SET(CMAKE_C_WARN_FLAGS "${CMAKE_C_WARN_FLAGS} -Wno-unused-parameter")
ENDIF(SUPPORT_WPARAM)
IF(SUPPORT_WFUNCTION)
	SET(CMAKE_C_WARN_FLAGS "${CMAKE_C_WARN_FLAGS} -Wno-unused-function")
ENDIF(SUPPORT_WFUNCTION)
IF(SUPPORT_WUNUSED_VAR)
	SET(CMAKE_C_WARN_FLAGS "${CMAKE_C_WARN_FLAGS} -Wunused-variable")
ENDIF(SUPPORT_WUNUSED_VAR)
IF(SUPPORT_WPOINTER_SIGN)
	SET(CMAKE_C_WARN_FLAGS "${CMAKE_C_WARN_FLAGS} -Wno-pointer-sign")
ENDIF(SUPPORT_WPOINTER_SIGN)
IF(SUPPORT_WSTRICT_PROTOTYPES)
	SET(CMAKE_C_WARN_FLAGS "${CMAKE_C_WARN_FLAGS} -Wstrict-prototypes")
ENDIF(SUPPORT_WSTRICT_PROTOTYPES)
IF(SUPPORT_PEDANTIC_FLAG)
	SET(CMAKE_C_WARN_FLAGS "${CMAKE_C_WARN_FLAGS} -pedantic")
ENDIF(SUPPORT_PEDANTIC_FLAG)
IF(SUPPORT_STD_FLAG)
	SET(CMAKE_C_WARN_FLAGS "${CMAKE_C_WARN_FLAGS} -std=c99")
ENDIF(SUPPORT_STD_FLAG)
 
CHECK_C_COMPILER_FLAG(-fPIC SUPPORT_FPIC)
IF(SUPPORT_FPIC)
	SET(CMAKE_EXE_LINKER_FLAGS "${CMAKE_C_FLAGS} -fPIC")
ENDIF(SUPPORT_FPIC)
CHECK_C_COMPILER_FLAG(-fpic SUPPORT_Fpic)
IF(SUPPORT_Fpic)
	SET(CMAKE_EXE_LINKER_FLAGS"${CMAKE_C_FLAGS} -fpic")
ENDIF(SUPPORT_Fpic)
 
 # Optimization flags
IF(NOT CMAKE_C_OPT_FLAGS)
	IF(ENABLE_OPTIMIZATION MATCHES "ON")
		SET(CMAKE_C_OPT_FLAGS "-O3 -fstrict-aliasing")
	ELSE(ENABLE_OPTIMIZATION MATCHES "ON")
		SET(CMAKE_C_OPT_FLAGS "-O0 -fstrict-aliasing")
	ENDIF(ENABLE_OPTIMIZATION MATCHES "ON")<commit_msg>Revert PIC change, as it is required for compiling.<commit_after>#
# Rspamd - rambler antispam system
#
# Cmake configuration file
#

############################# INITIAL SECTION #############################################

PROJECT(rspamd C)

SET(RSPAMD_VERSION_MAJOR 0)
SET(RSPAMD_VERSION_MINOR 6)
SET(RSPAMD_VERSION_PATCH 0)


SET(RSPAMD_VERSION         "${RSPAMD_VERSION_MAJOR}.${RSPAMD_VERSION_MINOR}.${RSPAMD_VERSION_PATCH}")
SET(RSPAMD_MASTER_SITE_URL "http://bitbucket.org/vstakhov/rspamd")

IF(NOT RSPAMD_USER)
	SET(RSPAMD_USER "nobody")
	SET(RSPAMD_GROUP "nobody")
ENDIF(NOT RSPAMD_USER)

CMAKE_MINIMUM_REQUIRED(VERSION 2.6.0 FATAL_ERROR)

############################# OPTIONS SECTION #############################################

OPTION(DEBUG_MODE          "Enable debug output [default: ON]"                  ON)
OPTION(ENABLE_OPTIMIZATION "Enable optimization [default: OFF]"                 OFF)
OPTION(SKIP_RELINK_RPATH   "Skip relinking and full RPATH for the install tree" OFF)
OPTION(ENABLE_REDIRECTOR   "Enable redirector install [default: OFF]"           OFF)
OPTION(ENABLE_PROFILING    "Enable profiling [default: OFF]"                    OFF)
OPTION(ENABLE_GPERF_TOOLS  "Enable google perftools [default: OFF]"             OFF)
OPTION(ENABLE_STATIC       "Enable static compiling [default: OFF]"             OFF)
OPTION(ENABLE_LUAJIT       "Link with libluajit [default: OFF]"                 OFF)
OPTION(ENABLE_JUDY         "Find and link with Judy library [default: ON]"      ON)
OPTION(ENABLE_DB           "Find and link with DB library [default: OFF]"       OFF)
OPTION(ENABLE_SQLITE       "Find and link with sqlite3 library [default: OFF]"  OFF)
OPTION(ENABLE_HIREDIS      "Find and link with external redis library [default: OFF]"  OFF)
OPTION(NO_SHARED           "Build internal libs static [default: OFF]"          OFF)
OPTION(FORCE_GMIME24       "Link with gmime2.4 [default: OFF]"                  OFF)

# Build optimized code for following CPU (default i386)
#SET(CPU_TUNE               "i686")

############################# INCLUDE SECTION #############################################

INCLUDE(CheckIncludeFiles)
INCLUDE(CheckFunctionExists)
INCLUDE(CheckSymbolExists)
INCLUDE(CheckCSourceCompiles)
INCLUDE(CheckLibraryExists)
INCLUDE(FindPkgConfig)
INCLUDE(CheckCCompilerFlag)
INCLUDE(FindPerl)
INCLUDE(FindOpenSSL)

############################# MODULES SECTION #############################################

MACRO(_AddModulesForced MLIST WLIST)
# Generate unique string for this build
	STRING(RANDOM LENGTH 8 _MODULES_ID)
	SET(MODULES_ID ${_MODULES_ID} CACHE INTERNAL "Modules ID" FORCE)
	FILE(WRITE "src/modules.c" "/* ${MODULES_ID} */\n#include \"config.h\"\n")
	
	# Handle even old cmake
	LIST(LENGTH ${MLIST} MLIST_COUNT)
	LIST(LENGTH ${WLIST} WLIST_COUNT)
	MATH(EXPR MLIST_MAX ${MLIST_COUNT}-1)
	MATH(EXPR WLIST_MAX ${WLIST_COUNT}-1)
	
	FOREACH(MOD_IDX RANGE ${MLIST_MAX})
		LIST(GET ${MLIST} ${MOD_IDX} MOD)
		FILE(APPEND "src/modules.c" "extern module_t ${MOD}_module;\n")
	ENDFOREACH(MOD_IDX RANGE ${MLIST_MAX})
		 
	FILE(APPEND "src/modules.c" "\n\nmodule_t *modules[] = {\n")
	
	FOREACH(MOD_IDX RANGE ${MLIST_MAX})
		LIST(GET ${MLIST} ${MOD_IDX} MOD)
		FILE(APPEND "src/modules.c" "&${MOD}_module,\n")
	ENDFOREACH(MOD_IDX RANGE ${MLIST_MAX})
	
	FILE(APPEND "src/modules.c" "NULL\n};\n")
	
	FOREACH(MOD_IDX RANGE ${WLIST_MAX})
		LIST(GET ${WLIST} ${MOD_IDX} WRK)
		FILE(APPEND "src/modules.c" "extern worker_t ${WRK}_worker;\n")
	ENDFOREACH(MOD_IDX RANGE ${WLIST_MAX})
	 
	FILE(APPEND "src/modules.c" "\n\nworker_t *workers[] = {\n")
	
	FOREACH(MOD_IDX RANGE ${WLIST_MAX})
		LIST(GET ${WLIST} ${MOD_IDX} WRK)
		FILE(APPEND "src/modules.c" "&${WRK}_worker,\n")
	ENDFOREACH(MOD_IDX RANGE ${WLIST_MAX})
	FILE(APPEND "src/modules.c" "NULL\n};\n")
ENDMACRO(_AddModulesForced MLIST WLIST)

MACRO(AddModules MLIST WLIST)
	_AddModulesForced(${MLIST} ${WLIST})
	#IF(NOT EXISTS "src/modules.c")
	#	_AddModulesForced(${MLIST} ${WLIST})
	#ELSE(NOT EXISTS "src/modules.c")
	#	FILE(STRINGS "src/modules.c" FILE_ID_RAW REGEX "^/.*[a-zA-Z0-9]+.*/$")
	#	STRING(REGEX MATCH "[a-zA-Z0-9]+" FILE_ID "${FILE_ID_RAW}")
	#	IF(NOT FILE_ID STREQUAL MODULES_ID)
	#		MESSAGE("Regenerate modules info")
	#		_AddModulesForced(${MLIST} ${WLIST})
	#	ENDIF(NOT FILE_ID STREQUAL MODULES_ID)
	#ENDIF(NOT EXISTS "src/modules.c")
ENDMACRO(AddModules MLIST WLIST)

############################# CONFIG SECTION #############################################
# Initial set

IF(CMAKE_INSTALL_PREFIX)
    SET(PREFIX ${CMAKE_INSTALL_PREFIX})
ENDIF(CMAKE_INSTALL_PREFIX)

LIST(APPEND CMAKE_REQUIRED_LIBRARIES m)

# Try to detect platform for further configuration
CHECK_C_SOURCE_COMPILES ("#if !defined(__x86_64) && !defined(__amd64) && !defined(_M_X64)
					#error assume 32 bit arch
					#endif
					int main() { return 0;}" AMD_64)
IF(NOT BUILD_CPU_MODE)
	IF(AMD_64)
		SET (BUILD_CPU_MODE "64")
	ELSE(AMD_64)
		SET (BUILD_CPU_MODE "32")
	ENDIF(AMD_64)
ENDIF(NOT BUILD_CPU_MODE)

# Platform specific configuration
IF(CMAKE_SYSTEM_NAME STREQUAL "FreeBSD")
	SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -D_BSD_SOURCE -DFREEBSD")
	CONFIGURE_FILE(freebsd/rspamd.sh.in freebsd/rspamd @ONLY)

	IF(ENABLE_REDIRECTOR MATCHES "ON")
		CONFIGURE_FILE(freebsd/redirector.sh.in freebsd/redirector @ONLY)
	ENDIF(ENABLE_REDIRECTOR MATCHES "ON")
	IF(CMAKE_INSTALL_PREFIX)
    	IF(NOT ETC_PREFIX)
	    	SET(ETC_PREFIX "${CMAKE_INSTALL_PREFIX}/etc")
    	ENDIF(NOT ETC_PREFIX)
    	IF(NOT MAN_PREFIX)
	    	SET(MAN_PREFIX "${CMAKE_INSTALL_PREFIX}/man")
    	ENDIF(NOT MAN_PREFIX)
    	IF(NOT LOCALSTATES_PREFIX)
	    	SET(LOCALSTATES_PREFIX "${CMAKE_INSTALL_PREFIX}/var/run/rspamd")
    	ENDIF(NOT LOCALSTATES_PREFIX)
	    SET(PREFIX "${CMAKE_INSTALL_PREFIX}")
	ELSE(CMAKE_INSTALL_PREFIX)
    	IF(NOT ETC_PREFIX)
    		SET(ETC_PREFIX /usr/local/etc)
    	ENDIF(NOT ETC_PREFIX)
    	IF(NOT MAN_PREFIX)
    		SET(MAN_PREFIX /usr/local/man)
    	ENDIF(NOT MAN_PREFIX)
    	IF(NOT LOCALSTATES_PREFIX)
	    	SET(LOCALSTATES_PREFIX "/var/run/rspamd")
    	ENDIF(NOT LOCALSTATES_PREFIX)
    	IF(NOT PREFIX)
    		SET(PREFIX /usr/local)
    	ENDIF(NOT PREFIX)
	ENDIF(CMAKE_INSTALL_PREFIX)
    FIND_LIBRARY(LIBINTL_LIBRARY NAMES intl PATHS   /lib
                                               /opt/lib
                                               /usr/lib
                                               /usr/local/lib
                 DOC "Path where the libintl library can be found")
    LIST(APPEND CMAKE_REQUIRED_LIBRARIES ${LIBINTL_LIBRARY})
	MESSAGE(STATUS "Configuring for FreeBSD")
 
ENDIF(CMAKE_SYSTEM_NAME STREQUAL "FreeBSD")

IF(CMAKE_SYSTEM_NAME STREQUAL "Darwin")
	SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -D_BSD_SOURCE -DDARWIN")
    
    IF(CMAKE_INSTALL_PREFIX)
    	IF(NOT ETC_PREFIX)
	    	SET(ETC_PREFIX "${CMAKE_INSTALL_PREFIX}/etc")
	    ENDIF(NOT ETC_PREFIX)
	    IF(NOT MAN_PREFIX)
	    	SET(MAN_PREFIX "${CMAKE_INSTALL_PREFIX}/man")
	   	ENDIF(NOT MAN_PREFIX)
	    IF(NOT LOCALSTATES_PREFIX)
	    	SET(LOCALSTATES_PREFIX "${CMAKE_INSTALL_PREFIX}/var/run/rspamd")
    	ENDIF(NOT LOCALSTATES_PREFIX)
	    SET(PREFIX "${CMAKE_INSTALL_PREFIX}")
	ELSE(CMAKE_INSTALL_PREFIX)    	
    	IF(NOT ETC_PREFIX)
    		SET(ETC_PREFIX /usr/local/etc)
    	ENDIF(NOT ETC_PREFIX)
    	IF(NOT MAN_PREFIX)
    		SET(MAN_PREFIX /usr/local/man)
    	ENDIF(NOT MAN_PREFIX)
    	IF(NOT LOCALSTATES_PREFIX)
	    	SET(LOCALSTATES_PREFIX "/var/run/rspamd")
    	ENDIF(NOT LOCALSTATES_PREFIX)
    	IF(NOT PREFIX)
    		SET(PREFIX /usr/local)
    	ENDIF(NOT PREFIX)
    ENDIF(CMAKE_INSTALL_PREFIX)

    FIND_LIBRARY(LIBINTL_LIBRARY NAMES intl PATHS   /lib
                                               /opt/lib
                                               /usr/lib
                                               /usr/local/lib
                 DOC "Path where the libintl library can be found")
    LIST(APPEND CMAKE_REQUIRED_LIBRARIES ${LIBINTL_LIBRARY})
	MESSAGE(STATUS "Configuring for Darwin")

ENDIF(CMAKE_SYSTEM_NAME STREQUAL "Darwin")

IF(CMAKE_SYSTEM_NAME STREQUAL "Linux")
	SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -D_GNU_SOURCE -DLINUX")
	IF(CMAKE_INSTALL_PREFIX)
		IF(NOT ETC_PREFIX)
	    	SET(ETC_PREFIX "${CMAKE_INSTALL_PREFIX}/etc")
	    ENDIF(NOT ETC_PREFIX)
	    
	    IF(NOT MAN_PREFIX)
	    	SET(MAN_PREFIX "${CMAKE_INSTALL_PREFIX}/share/man")
	    ENDIF(NOT MAN_PREFIX)
	    IF(NOT LOCALSTATES_PREFIX)
	    	SET(LOCALSTATES_PREFIX "${CMAKE_INSTALL_PREFIX}/var/run/rspamd")
    	ENDIF(NOT LOCALSTATES_PREFIX)
	    
	    SET(PREFIX "${CMAKE_INSTALL_PREFIX}")
	ELSE(CMAKE_INSTALL_PREFIX)    	
    	IF(NOT ETC_PREFIX)
    		SET(ETC_PREFIX /etc)
    	ENDIF(NOT ETC_PREFIX)
    	IF(NOT PREFIX)
    		SET(PREFIX /usr)
    	ENDIF(NOT PREFIX)
    	IF(NOT MAN_PREFIX)
    		SET(MAN_PREFIX /usr/share/man)
    	ENDIF(NOT MAN_PREFIX)
    	IF(NOT LOCALSTATES_PREFIX)
	    	SET(LOCALSTATES_PREFIX "/var/run/rspamd")
    	ENDIF(NOT LOCALSTATES_PREFIX)
    ENDIF(CMAKE_INSTALL_PREFIX)
    IF(BUILD_CPU_MODE STREQUAL "32")
    	SET(CMAKE_C_FLAGS  "-D_FILE_OFFSET_BITS=64 -D_LARGEFILE64_SOURCE ${CMAKE_C_FLAGS}")
    ENDIF(BUILD_CPU_MODE STREQUAL "32")
    # Workaround with architecture specific includes
    IF(IS_DIRECTORY "/usr/include/${CMAKE_SYSTEM_PROCESSOR}-linux-gnu/") 
    	INCLUDE_DIRECTORIES("/usr/include/${CMAKE_SYSTEM_PROCESSOR}-linux-gnu/")
    	SET(CMAKE_REQUIRED_INCLUDES "/usr/include/${CMAKE_SYSTEM_PROCESSOR}-linux-gnu/")
    ENDIF(IS_DIRECTORY "/usr/include/${CMAKE_SYSTEM_PROCESSOR}-linux-gnu/")
    
	LIST(APPEND CMAKE_REQUIRED_LIBRARIES rt)
	LIST(APPEND CMAKE_REQUIRED_LIBRARIES dl)
	MESSAGE(STATUS "Configuring for Linux")
	IF(EXISTS "/etc/debian_version")
		SET(LINUX_START_SCRIPT "rspamd_debian.in")
	ELSE(EXISTS "/etc/debian_version")
		SET(LINUX_START_SCRIPT "rspamd_rh.in")
	ENDIF(EXISTS "/etc/debian_version")
ENDIF(CMAKE_SYSTEM_NAME STREQUAL "Linux")

IF(CMAKE_SYSTEM_NAME STREQUAL "SunOS")
 	IF(NOT BUILD_CPU_MODE)
	 	SET (BUILD_CPU_MODE "32")
	ENDIF(NOT BUILD_CPU_MODE)
	
	IF("${CMAKE_C_COMPILER_ID}" MATCHES SunPro)
		SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Xa -xregs=no%frameptr -xstrconst -xc99")
		SET(SUN_BUILD32 "-xtarget=generic")
		SET(SUN_BUILD64 "-xtarget=generic64")
		IF(ENABLE_OPTIMIZATION MATCHES "ON")
			SET(CMAKE_C_OPT_FLAGS "-fast -xdepend")
		ELSE(ENABLE_OPTIMIZATION MATCHES "ON")
			SET(CMAKE_C_OPT_FLAGS "-xO0")
		ENDIF(ENABLE_OPTIMIZATION MATCHES "ON")
	ELSE("${CMAKE_C_COMPILER_ID}" MATCHES SunPro)
		set(SUN_BUILD32 "-m32")
		set(SUN_BUILD64 "-m64")		
	ENDIF("${CMAKE_C_COMPILER_ID}" MATCHES SunPro)

 	IF (BUILD_CPU_MODE STREQUAL "32")
       message (STATUS "Building 32-bit mode on Solaris")
       # If we are asked to build 32 bit mode
       	set(CMAKE_C_FLAGS  "${SUN_BUILD32} ${CMAKE_C_FLAGS}")
       	set(CMAKE_SHARED_LINKER_FLAGS  "${CMAKE_SHARED_LINKER_FLAGS} ${SUN_BUILD32}")
 	ELSE (BUILD_CPU_MODE STREQUAL "32")
       # solaris 64-bit mode
		message (STATUS "Building 64-bit mode on Solaris")
       	set(CMAKE_C_FLAGS  "${SUN_BUILD64} ${CMAKE_C_FLAGS}")
       	set(CMAKE_SHARED_LINKER_FLAGS  "${CMAKE_SHARED_LINKER_FLAGS} ${SUN_BUILD64}")
    ENDIF (BUILD_CPU_MODE STREQUAL "32")

 	SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -D__EXTENSIONS__ -DSOLARIS -D_POSIX_SOURCE -D_POSIX_C_SOURCE=200112L")

	IF(CMAKE_INSTALL_PREFIX)
		IF(NOT ETC_PREFIX)
	    	SET(ETC_PREFIX "${CMAKE_INSTALL_PREFIX}/etc")
	    ENDIF(NOT ETC_PREFIX)
	    IF(NOT MAN_PREFIX)
	    	SET(MAN_PREFIX "${CMAKE_INSTALL_PREFIX}/man")
	    ENDIF(NOT MAN_PREFIX)
	    IF(NOT LOCALSTATES_PREFIX)
	    	SET(LOCALSTATES_PREFIX "/var/run/rspamd")
    	ENDIF(NOT LOCALSTATES_PREFIX)
	    SET(PREFIX "${CMAKE_INSTALL_PREFIX}")
	ELSE(CMAKE_INSTALL_PREFIX)    
    	IF(NOT ETC_PREFIX)
    		SET(ETC_PREFIX /etc)
    	ENDIF(NOT ETC_PREFIX)
    	IF(NOT PREFIX)
    		SET(PREFIX /usr)
    	ENDIF(NOT PREFIX)
    	IF(NOT MAN_PREFIX)
    		SET(MAN_PREFIX /usr/share/man)
    	ENDIF(NOT MAN_PREFIX)
    	IF(NOT LOCALSTATES_PREFIX)
	    	SET(LOCALSTATES_PREFIX "/var/run/rspamd")
    	ENDIF(NOT LOCALSTATES_PREFIX)
    ENDIF(CMAKE_INSTALL_PREFIX)
    
	LIST(APPEND CMAKE_REQUIRED_LIBRARIES rt)
	LIST(APPEND CMAKE_REQUIRED_LIBRARIES dl)
	LIST(APPEND CMAKE_REQUIRED_LIBRARIES resolv)
 	LIST(APPEND CMAKE_REQUIRED_LIBRARIES nsl)
 	LIST(APPEND CMAKE_REQUIRED_LIBRARIES socket)
 	LIST(APPEND CMAKE_REQUIRED_LIBRARIES umem)
 	# Ugly hack, but FindOpenSSL on Solaris does not link with libcrypto
 	LIST(APPEND CMAKE_REQUIRED_LIBRARIES crypto)
  	SET(CMAKE_VERBOSE_MAKEFILE ON)
  	SET(CMAKE_INSTALL_RPATH_USE_LINK_PATH FALSE)
  	SET(CMAKE_INSTALL_RPATH "${PREFIX}/lib")	
ENDIF(CMAKE_SYSTEM_NAME STREQUAL "SunOS")

# Set libdir
IF(NOT LIBDIR)
	SET(LIBDIR lib/rspamd)
	IF(NOT CMAKE_INSTALL_RPATH)
		SET(CMAKE_INSTALL_RPATH ${PREFIX}/${LIBDIR})
	ENDIF(NOT CMAKE_INSTALL_RPATH)
ENDIF(NOT LIBDIR)

# Set includedir
IF(NOT INCLUDEDIR)
	SET(INCLUDEDIR include/rspamd)
ENDIF(NOT INCLUDEDIR)

# Check prefixes
IF(NOT ETC_PREFIX)
	IF(CMAKE_INSTALL_PREFIX)
		MESSAGE(WARNING "Your system is unknown for rspamd, assume defining ETC_PREFIX and MAN_PREFIX by -D option, now ETC_PREFIX and MAN_PREFIX are relative to ${CMAKE_INSTALL_PREFIX}")
		SET(ETC_PREFIX "${CMAKE_INSTALL_PREFIX}/etc")
	ELSE(CMAKE_INSTALL_PREFIX)
		MESSAGE(FATAL_ERROR "Your system is unknown for rspamd and no CMAKE_INSTALL_PREFIX specified, assume defining CMAKE_INSTALL_PREFIX")
	ENDIF(CMAKE_INSTALL_PREFIX) 
	MESSAGE(STATUS "Configuring for ${CMAKE_SYSTEM_NAME}")
ENDIF(NOT ETC_PREFIX)
IF(NOT MAN_PREFIX)
	IF(CMAKE_INSTALL_PREFIX)
		SET(MAN_PREFIX "${CMAKE_INSTALL_PREFIX}/man")
	ENDIF(CMAKE_INSTALL_PREFIX) 
ENDIF(NOT MAN_PREFIX)
IF(NOT LOCALSTATES_PREFIX)
	IF(CMAKE_INSTALL_PREFIX)
		SET(LOCALSTATES_PREFIX "${CMAKE_INSTALL_PREFIX}/var/run/rspamd")
	ENDIF(CMAKE_INSTALL_PREFIX) 
ENDIF(NOT LOCALSTATES_PREFIX)

# Now CMAKE_INSTALL_PREFIX is a base prefix for everything
# ETC_PREFIX - for configuration
# MAN_PREFIX - for manual pages
# LOCALSTATES_PREFIX - for runtime files

# Now find libraries and headers

# Find lua libraries
IF(NOT LUA_VERSION_MAJOR OR NOT LUA_VERSION_MINOR)
	SET(LUA_VERSION_MAJOR "5")
	SET(LUA_VERSION_MINOR "1")
ENDIF(NOT LUA_VERSION_MAJOR OR NOT LUA_VERSION_MINOR)

FIND_PATH(LUA_INCLUDE_DIR lua.h
  HINTS
  $ENV{LUA_DIR}
  PATH_SUFFIXES "include/lua${LUA_VERSION_MAJOR}${LUA_VERSION_MINOR}" 
  				"include/lua${LUA_VERSION_MAJOR}.${LUA_VERSION_MINOR}" 
  				include/lua include
  PATHS
  /usr/local
  /usr
  ~/Library/Frameworks
  /Library/Frameworks
  /sw # Fink
  /opt/local # DarwinPorts
  /opt/csw # Blastwave
  /opt
)

FIND_LIBRARY(LUA_LIBRARY 
  NAMES lua 
  		"lua${LUA_VERSION_MAJOR}${LUA_VERSION_MINOR}"
  		"lua${LUA_VERSION_MAJOR}.${LUA_VERSION_MINOR}" 
  		"lua-${LUA_VERSION_MAJOR}.${LUA_VERSION_MINOR}"
  HINTS
  $ENV{LUA_DIR}
  PATH_SUFFIXES lib64 lib
  PATHS
  /usr/local
  /usr
  ~/Library/Frameworks
  /Library/Frameworks
  /sw
  /opt/local
  /opt/csw
  /opt
  DOC "Lua library"
)

IF(LUA_LIBRARY AND LUA_INCLUDE_DIR)
	SET(LUA_FOUND 1)
ENDIF(LUA_LIBRARY AND LUA_INCLUDE_DIR)

# Check for luajit
IF(ENABLE_LUAJIT MATCHES "ON")
    FIND_LIBRARY(LUAJIT_LIBRARY NAMES
    			"luajit${LUA_VERSION_MAJOR}${LUA_VERSION_MINOR}"
    			"luajit${LUA_VERSION_MAJOR}.${LUA_VERSION_MINOR}"
    			"luajit-${LUA_VERSION_MAJOR}.${LUA_VERSION_MINOR}"
    			 luajit
                  HINTS
                  $ENV{LUA_DIR}
                  PATH_SUFFIXES lib64 lib
                  PATHS
                      ~/Library/Frameworks
                      /Library/Frameworks
                      /usr/local
                      /usr
                      /sw
                      /opt/local
                      /opt/csw
                      /opt
                )
     IF(NOT LUAJIT_LIBRARY)
         MESSAGE(FATAL_ERROR "Luajit not found but enabled in configuration")
     ENDIF(NOT LUAJIT_LIBRARY)
ENDIF(ENABLE_LUAJIT MATCHES "ON")

IF(NOT LUA_FOUND)
	MESSAGE(FATAL_ERROR "Lua not found, lua support is required for working")
ELSE(NOT LUA_FOUND)
    SET(WITH_LUA 1)
    INCLUDE_DIRECTORIES("${LUA_INCLUDE_DIR}")
ENDIF(NOT LUA_FOUND)

# Check and link to pcre
pkg_check_modules(PCRE REQUIRED libpcre)
IF(PCRE_INCLUDE_DIRS)
	INCLUDE_DIRECTORIES("${PCRE_INCLUDE_DIRS}")
ENDIF(PCRE_INCLUDE_DIRS)
IF(PCRE_LIBRARY_DIRS)
	LINK_DIRECTORIES("${PCRE_LIBRARY_DIRS}")
ENDIF(PCRE_LIBRARY_DIRS)

IF(ENABLE_SQLITE MATCHES "ON")
	# Find optional sqlite3 support
	pkg_check_modules(SQLITE sqlite3>=3.6.0)
	IF(SQLITE_FOUND)
		SET(WITH_SQLITE 1)
	ENDIF(SQLITE_FOUND)
	IF(SQLITE_INCLUDE_DIRS)
		INCLUDE_DIRECTORIES("${SQLITE_INCLUDE_DIRS}")
	ENDIF(SQLITE_INCLUDE_DIRS)
	IF(SQLITE_LIBRARY_DIRS)
		LINK_DIRECTORIES("${SQLITE_LIBRARY_DIRS}")
	ENDIF(SQLITE_LIBRARY_DIRS)
ENDIF(ENABLE_SQLITE MATCHES "ON")

#Check for openssl (required for dkim)
IF(OPENSSL_FOUND)
	SET(HAVE_OPENSSL 1)
	INCLUDE_DIRECTORIES("${OPENSSL_INCLUDE_DIR}")
ENDIF(OPENSSL_FOUND)

IF(ENABLE_STATIC MATCHES "ON")
	pkg_check_modules(GLIB2 REQUIRED glib-2.0>=2.12 gthread-2.0 gmodule-2.0)
	SET(LINK_TYPE "STATIC")
ELSE(ENABLE_STATIC MATCHES "ON")
	pkg_check_modules(GLIB2 REQUIRED glib-2.0>=2.12 gthread-2.0 gmodule-2.0)
	IF(NO_SHARED MATCHES "OFF")
		SET(LINK_TYPE "SHARED")
	ELSE(NO_SHARED MATCHES "OFF")
		SET(LINK_TYPE "STATIC")
	ENDIF(NO_SHARED MATCHES "OFF")
ENDIF(ENABLE_STATIC MATCHES "ON")
SET(GLIB2_VERSION "${GLIB2_glib-2.0_VERSION}")

IF(GLIB2_VERSION VERSION_GREATER "2.30.0")
	pkg_check_modules(LIBFFI libffi)
	IF(LIBFFI_FOUND)
		SET(GLIB2_LDFLAGS "${GLIB2_LDFLAGS};${LIBFFI_LDFLAGS}")
		SET(GLIB2_LIBRARIES "${GLIB2_LIBRARIES};${LIBFFI_LIBRARIES}")
		SET(GLIB2_STATIC_LDFLAGS "${GLIB2_STATIC_LDFLAGS};${LIBFFI_STATIC_LDFLAGS}")
		SET(GLIB2_CFLAGS "${GLIB2_CFLAGS};${LIBFFI_CFLAGS}")
	ENDIF(LIBFFI_FOUND)
ENDIF(GLIB2_VERSION VERSION_GREATER "2.30.0") 
pkg_check_modules(GMIME2 gmime-2.0)

IF(ENABLE_HIREDIS MATCHES "ON")
	# Try to find hiredis library
	pkg_check_modules(HIREDIS REQUIRED libhiredis)
	IF(HIREDIS_INCLUDE_DIRS)
		INCLUDE_DIRECTORIES("${HIREDIS_INCLUDE_DIRS}")
	ENDIF(HIREDIS_INCLUDE_DIRS)
	IF(HIREDIS_LIBRARY_DIRS)
		LINK_DIRECTORIES("${HIREDIS_LIBRARY_DIRS}")
	ENDIF(HIREDIS_LIBRARY_DIRS)
	IF(HIREDIS_FOUND)
		SET(WITH_SYSTEM_HIREDIS 1)
	ENDIF(HIREDIS_FOUND)
ENDIF(ENABLE_HIREDIS MATCHES "ON")

# Try to link with gmime24
IF(NOT GMIME2_FOUND OR FORCE_GMIME24)
	pkg_check_modules(GMIME24 gmime-2.4)
	IF(NOT GMIME24_FOUND)
		pkg_check_modules(GMIME24 REQUIRED gmime-2.6)
	ENDIF(NOT GMIME24_FOUND)
	SET(GMIME24 "yes")
	# Gmime2
	FOREACH(arg ${GMIME24_CFLAGS})
		SET(GMIME_CFLAGS "${GMIME_CFLAGS} ${arg}")
	ENDFOREACH(arg ${GMIME24_CFLAGS})

	IF(ENABLE_STATIC MATCHES "ON")
		FOREACH(arg ${GMIME24_STATIC_LDFLAGS})
			SET(GMIME_LDFLAGS "${GMIME_LDFLAGS} ${arg}")
		ENDFOREACH(arg ${GMIME24_LDFLAGS})
	ELSE(ENABLE_STATIC MATCHES "ON")
		FOREACH(arg ${GMIME24_LDFLAGS})
			SET(GMIME_LDFLAGS "${GMIME_LDFLAGS} ${arg}")
		ENDFOREACH(arg ${GMIME24_LDFLAGS})
	ENDIF(ENABLE_STATIC MATCHES "ON")
	IF(GMIME24_INCLUDE_DIRS)
		INCLUDE_DIRECTORIES(${GMIME24_INCLUDE_DIRS})
	ENDIF(GMIME24_INCLUDE_DIRS)
	IF(GMIME24_LIBRARY_DIRS)
		LINK_DIRECTORIES(${GMIME24_LIBRARY_DIRS})
	ENDIF(GMIME24_LIBRARY_DIRS)
ELSE(NOT GMIME2_FOUND OR FORCE_GMIME24)
	# Gmime2
	FOREACH(arg ${GMIME2_CFLAGS})
		SET(GMIME_CFLAGS "${GMIME_CFLAGS} ${arg}")
	ENDFOREACH(arg ${GMIME2_CFLAGS})

	IF(ENABLE_STATIC MATCHES "ON")
		FOREACH(arg ${GMIME2_STATIC_LDFLAGS})
			SET(GMIME_LDFLAGS "${GMIME_LDFLAGS} ${arg}")
		ENDFOREACH(arg ${GMIME2_LDFLAGS})
	ELSE(ENABLE_STATIC MATCHES "ON")
		FOREACH(arg ${GMIME2_LDFLAGS})
			SET(GMIME_LDFLAGS "${GMIME_LDFLAGS} ${arg}")
		ENDFOREACH(arg ${GMIME2_LDFLAGS})
	ENDIF(ENABLE_STATIC MATCHES "ON")
	IF(GMIME2_INCLUDE_DIRS)
		INCLUDE_DIRECTORIES(${GMIME2_INCLUDE_DIRS})
	ENDIF(GMIME2_INCLUDE_DIRS)
	IF(GMIME2_LIBRARY_DIRS)
		LINK_DIRECTORIES(${GMIME2_LIBRARY_DIRS})
	ENDIF(GMIME2_LIBRARY_DIRS)
ENDIF(NOT GMIME2_FOUND OR FORCE_GMIME24)

# Make from ; separated list normal space separated list
# Glib2
FOREACH(arg ${GLIB2_CFLAGS})
	SET(GLIB_CFLAGS "${GLIB_CFLAGS} ${arg}")
ENDFOREACH(arg ${GLIB2_CFLAGS})

IF(ENABLE_STATIC MATCHES "ON")
	FOREACH(arg ${GLIB2_STATIC_LDFLAGS})
		SET(GLIB_LDFLAGS "${GLIB_LDFLAGS} ${arg}")
	ENDFOREACH(arg ${GLIB2_LDFLAGS})
ELSE(ENABLE_STATIC MATCHES "ON")
	FOREACH(arg ${GLIB2_LDFLAGS})
		SET(GLIB_LDFLAGS "${GLIB_LDFLAGS} ${arg}")
	ENDFOREACH(arg ${GLIB2_LDFLAGS})
ENDIF(ENABLE_STATIC MATCHES "ON")


INCLUDE_DIRECTORIES(${GLIB2_INCLUDE_DIRS})
LINK_DIRECTORIES(${GLIB2_LIBRARY_DIRS})


FIND_LIBRARY(LIBZ_LIBRARY NAMES z PATH_SUFFIXES lib64 lib
                  PATHS
                      ~/Library/Frameworks
                      /Library/Frameworks
                      /usr/local
                      /usr
                      /sw
                      /opt/local
                      /opt/csw
                      /opt
                 DOC "Path where the libz library can be found")
IF(NOT LIBZ_LIBRARY)
	MESSAGE(FATAL_ERROR "libz is required for libgmime")
ENDIF(NOT LIBZ_LIBRARY)
# Check for libevent

FIND_LIBRARY(LIBEVENT_LIBRARY NAMES event PATH_SUFFIXES lib64 lib
                  PATHS
                      ~/Library/Frameworks
                      /Library/Frameworks
                      /usr/local
                      /usr
                      /sw
                      /opt/local
                      /opt/csw
                      /opt
                 DOC "Path where the libevent library can be found")
IF(NOT LIBEVENT_LIBRARY)
	MESSAGE(FATAL_ERROR "libevent is required for building rspamd")
ENDIF(NOT LIBEVENT_LIBRARY)

FIND_PATH(LIBEVENT_INCLUDE event.h PATHS 	/opt/include
										/usr/include
										/usr/local/include
										DOC "Path where the libevent header files can be found")

FIND_PATH(LIBEVENT_EVHTTP evhttp.h PATHS /opt/include
										/usr/include
										/usr/local/include
										DOC "Path where the libevhttp header files can be found")

GET_FILENAME_COMPONENT(LIBEVENT_PATH "${LIBEVENT_LIBRARY}" PATH)
INCLUDE_DIRECTORIES("${LIBEVENT_INCLUDE}")
LINK_DIRECTORIES("${LIBEVENT_PATH}")

IF(ENABLE_JUDY MATCHES "ON")
	# Find libjudy
	FIND_LIBRARY(LIBJUDY_LIBRARY NAMES Judy PATH_SUFFIXES lib64 lib
	                  PATHS
	                      ~/Library/Frameworks
	                      /Library/Frameworks
	                      /usr/local
	                      /usr
	                      /sw
	                      /opt/local
	                      /opt/csw
	                      /opt
	                 DOC "Path where the libjudy library can be found")
	IF(LIBJUDY_LIBRARY)
		FIND_PATH(LIBJUDY_INCLUDE Judy.h PATHS 	/opt/include
												/usr/include
												/usr/local/include
												DOC "Path where the judy header files can be found")
	
		GET_FILENAME_COMPONENT(LIBJUDY_PATH "${LIBJUDY_LIBRARY}" PATH)
		INCLUDE_DIRECTORIES("${LIBJUDY_INCLUDE}")
		LINK_DIRECTORIES("${LIBJUDY_PATH}")
		SET(WITH_JUDY 1)
	ENDIF(LIBJUDY_LIBRARY)
ENDIF(ENABLE_JUDY MATCHES "ON")

# Find libbd
IF(ENABLE_DB MATCHES "ON")
	FIND_LIBRARY(LIBDB_LIBRARY NAMES db PATHS PATH_SUFFIXES lib64 lib
	                  PATHS
	                      ~/Library/Frameworks
	                      /Library/Frameworks
	                      /usr/local
	                      /usr
	                      /sw
	                      /opt/local
	                      /opt/csw
	                      /opt
	                 DOC "Path where the libdb library can be found")
	IF(LIBDB_LIBRARY)
		FIND_PATH(LIBDB_INCLUDE db.h PATHS 	/opt/include
												/usr/include
												/usr/local/include
												DOC "Path where the bdb header files can be found")
	
		GET_FILENAME_COMPONENT(LIBDB_PATH "${LIBDB_LIBRARY}" PATH)
		INCLUDE_DIRECTORIES("${LIBDB_INCLUDE}")
		LINK_DIRECTORIES("${LIBDB_PATH}")
		SET(WITH_DB 1)
	ENDIF(LIBDB_LIBRARY)
ENDIF(ENABLE_DB MATCHES "ON")

IF(ENABLE_PROFILING MATCHES "ON")
	SET(WITH_PROFILER 1)
	SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -pg")
	SET(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -pg")
ENDIF(ENABLE_PROFILING MATCHES "ON")

# Static build

IF(ENABLE_STATIC MATCHES "ON")
	SET(BUILD_STATIC 1)
	SET(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -static -pthread")
	MESSAGE(STATUS "Static build of rspamd, no custom plugins support")
ENDIF(ENABLE_STATIC MATCHES "ON")

# Google performance tools

IF(ENABLE_GPERF_TOOLS MATCHES "ON")
	FIND_PATH(GPERF_INCLUDE google/profiler.h PATHS /opt/include
													/usr/include
													/usr/local/include
											  DOC "Path where google perftools includes can be found")
	INCLUDE_DIRECTORIES("${GPERF_INCLUDE}")
	FIND_LIBRARY(GPERF_LIBRARY NAMES profiler PATH_SUFFIXES lib64 lib
                  PATHS
                      ~/Library/Frameworks
                      /Library/Frameworks
                      /usr/local
                      /usr
                      /sw
                      /opt/local
                      /opt/csw
                      /opt
					DOC "Path where the gperf library can be found")
	IF(NOT GPERF_LIBRARY)
		MESSAGE(FATAL_ERROR "gperf tools support is enabled but not found in system")
	ENDIF(NOT GPERF_LIBRARY)
	GET_FILENAME_COMPONENT(GPERF_PATH "${GPERF_LIBRARY}" PATH)
	LINK_DIRECTORIES("${GPERF_PATH}")
	SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fno-omit-frame-pointer")
	SET(WITH_GPERF_TOOLS 1)
ENDIF(ENABLE_GPERF_TOOLS MATCHES "ON")

# Find util library

FIND_LIBRARY(LIBUTIL_LIBRARY NAMES util PATHS	PATH_SUFFIXES lib64 lib
                  PATHS
                      ~/Library/Frameworks
                      /Library/Frameworks
                      /usr/local
                      /usr
                      /sw
                      /opt/local
                      /opt/csw
                      /opt
                 DOC "Path where the libutil library can be found")
IF(LIBUTIL_LIBRARY)
	LIST(APPEND CMAKE_REQUIRED_LIBRARIES util)
	CHECK_FUNCTION_EXISTS(pidfile_open HAVE_PIDFILE)
	CHECK_FUNCTION_EXISTS(pidfile_fileno HAVE_PIDFILE_FILENO)

	FIND_FILE(HAVE_LIBUTIL_H NAMES libutil.h PATHS /usr/include
											   /opt/include
											   /usr/local/include
				DOC "Path to libutil header")
ENDIF(LIBUTIL_LIBRARY)

# Find libfetch (for FreeBSD)

FIND_LIBRARY(LIBFETCH_LIBRARY NAMES fetch PATHS	PATH_SUFFIXES lib64 lib
                  PATHS
                      ~/Library/Frameworks
                      /Library/Frameworks
                      /usr/local
                      /usr
                      /sw
                      /opt/local
                      /opt/csw
                      /opt
                 DOC "Path where the libfetch library can be found")
IF(LIBFETCH_LIBRARY)
	FIND_FILE(HAVE_FETCH_H NAMES fetch.h PATHS /usr/include
											   /opt/include
											   /usr/local/include
				DOC "Path to libfetch header")
ELSE(LIBFETCH_LIBRARY)
	# Try to find libcurl
	INCLUDE(FindCURL)
	IF(NOT CURL_FOUND)
		MESSAGE(WARNING "Neither libcurl nor libfetch were found, no support of URL includes in configuration")
	ELSE(NOT CURL_FOUND)
		INCLUDE_DIRECTORIES("${CURL_INCLUDE_DIRS}")
	ENDIF(NOT CURL_FOUND)
ENDIF(LIBFETCH_LIBRARY)

# Process with warn flags
SET(CMAKE_C_WARN_FLAGS "")
CHECK_C_COMPILER_FLAG(-Wall SUPPORT_WALL)
CHECK_C_COMPILER_FLAG(-W SUPPORT_W)
CHECK_C_COMPILER_FLAG(-Wpointer-arith SUPPORT_WPOINTER)
CHECK_C_COMPILER_FLAG(-Wno-unused-parameter SUPPORT_WPARAM)
CHECK_C_COMPILER_FLAG(-Wno-unused-function SUPPORT_WFUNCTION)
CHECK_C_COMPILER_FLAG(-Wunused-variable SUPPORT_WUNUSED_VAR)
CHECK_C_COMPILER_FLAG(-Wno-pointer-sign SUPPORT_WPOINTER_SIGN)
CHECK_C_COMPILER_FLAG(-Wstrict-prototypes SUPPORT_WSTRICT_PROTOTYPES)
CHECK_C_COMPILER_FLAG(-pedantic SUPPORT_PEDANTIC_FLAG)
IF(NOT "${CMAKE_C_COMPILER_ID}" MATCHES SunPro)
	CHECK_C_COMPILER_FLAG("-std=c99" SUPPORT_STD_FLAG)
ENDIF(NOT "${CMAKE_C_COMPILER_ID}" MATCHES SunPro)
IF(SUPPORT_W)
	SET(CMAKE_C_WARN_FLAGS "${CMAKE_C_WARN_FLAGS} -W")
ENDIF(SUPPORT_W)
IF(SUPPORT_WALL)
	SET(CMAKE_C_WARN_FLAGS "${CMAKE_C_WARN_FLAGS} -Wall")
ENDIF(SUPPORT_WALL)
IF(SUPPORT_WPOINTER)
	SET(CMAKE_C_WARN_FLAGS "${CMAKE_C_WARN_FLAGS} -Wpointer-arith")
ENDIF(SUPPORT_WPOINTER)
IF(SUPPORT_WPARAM)
	SET(CMAKE_C_WARN_FLAGS "${CMAKE_C_WARN_FLAGS} -Wno-unused-parameter")
ENDIF(SUPPORT_WPARAM)
IF(SUPPORT_WFUNCTION)
	SET(CMAKE_C_WARN_FLAGS "${CMAKE_C_WARN_FLAGS} -Wno-unused-function")
ENDIF(SUPPORT_WFUNCTION)
IF(SUPPORT_WUNUSED_VAR)
	SET(CMAKE_C_WARN_FLAGS "${CMAKE_C_WARN_FLAGS} -Wunused-variable")
ENDIF(SUPPORT_WUNUSED_VAR)
IF(SUPPORT_WPOINTER_SIGN)
	SET(CMAKE_C_WARN_FLAGS "${CMAKE_C_WARN_FLAGS} -Wno-pointer-sign")
ENDIF(SUPPORT_WPOINTER_SIGN)
IF(SUPPORT_WSTRICT_PROTOTYPES)
	SET(CMAKE_C_WARN_FLAGS "${CMAKE_C_WARN_FLAGS} -Wstrict-prototypes")
ENDIF(SUPPORT_WSTRICT_PROTOTYPES)
IF(SUPPORT_PEDANTIC_FLAG)
	SET(CMAKE_C_WARN_FLAGS "${CMAKE_C_WARN_FLAGS} -pedantic")
ENDIF(SUPPORT_PEDANTIC_FLAG)
IF(SUPPORT_STD_FLAG)
	SET(CMAKE_C_WARN_FLAGS "${CMAKE_C_WARN_FLAGS} -std=c99")
ENDIF(SUPPORT_STD_FLAG)
 
CHECK_C_COMPILER_FLAG(-fPIC SUPPORT_FPIC)
IF(SUPPORT_FPIC)
	SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fPIC")
ENDIF(SUPPORT_FPIC)
 
 # Optimization flags
IF(NOT CMAKE_C_OPT_FLAGS)
	IF(ENABLE_OPTIMIZATION MATCHES "ON")
		SET(CMAKE_C_OPT_FLAGS "-O3 -fstrict-aliasing")
	ELSE(ENABLE_OPTIMIZATION MATCHES "ON")
		SET(CMAKE_C_OPT_FLAGS "-O0 -fstrict-aliasing")
	ENDIF(ENABLE_OPTIMIZATION MATCHES "ON")
{'fraction_non_alphanumeric': 0.11220381379889321, 'fraction_numerical': 0.00890914386554046, 'mean_word_length': 3.6042439062869764, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 24, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3459657', 'n_tokens_mistral': 24641, 'n_tokens_neox': 21797, 'n_words': 3851}