#
# Config build environment
#

# Specifies library name
SET(SHARED_LIBRARY_NAME "pump")
SET(STATIC_LIBRARY_NAME "pump_static")

# Specifies build type
IF(BUILD_DEBUG)
	SET(CMAKE_BUILD_TYPE Debug)
	SET(pump_WITH_DEBUG_LOG "WITH_DEBUG_LOG")
ELSE()
	SET(CMAKE_BUILD_TYPE Release)
	SET(pump_WITH_DEBUG_LOG "WITHOUT_DEBUG_LOG")
	# The macro disable assert
	ADD_DEFINITIONS("-DNDEBUG")
ENDIF()

SET(LIB_SEP " ")
IF("${CMAKE_CXX_COMPILER_ID}" STREQUAL "MSVC")
	SET(LIB_SEP ";")
	SET(CMAKE_CONFIGURATION_TYPES ${CMAKE_BUILD_TYPE} CACHE STRING "" FORCE)
ENDIF()

# Specifies suffix of libraries for finding and linking
IF(WIN32)
	SET(CMAKE_FIND_LIBRARY_SUFFIXES "${CMAKE_FIND_LIBRARY_SUFFIXES};.a")
ENDIF()

# Specifies project prefix path
SET(CMAKE_PREFIX_PATH ${ROOT_DIR}/lib)

# Specifies libraries path
LINK_DIRECTORIES(${ROOT_DIR}/lib)
LINK_DIRECTORIES(${CMAKE_BINARY_DIR})

# Specifies include path
INCLUDE_DIRECTORIES(${ROOT_DIR}/src)
INCLUDE_DIRECTORIES(${ROOT_DIR}/include)
MESSAGE(STATUS "INCLUDE path: ${ROOT_DIR}")

# Specifies unicode character set on windows
IF(WIN32)
	REMOVE_DEFINITIONS("-D_MBCS")
	ADD_DEFINITIONS("-DUNICODE")
	ADD_DEFINITIONS("-D_UNICODE")
	ADD_DEFINITIONS("-DWIN32_LEAN_AND_MEAN")
ENDIF()

IF(MSYS OR CYGWIN)
	# Fix select conflicting declaration to define __USE_W32_SOCKETS
	ADD_DEFINITIONS("-D__USE_W32_SOCKETS")
ENDIF()

TEST_BIG_ENDIAN(IS_BIG_ENDIAN)
IF(IS_BIG_ENDIAN)
	SET(pump_WITH_ENDIAN "WITH_BIG_ENDIAN")
ELSE()
	SET(pump_WITH_ENDIAN "WITH_LITTLE_ENDIAN")
ENDIF()

CHECK_INCLUDE_FILE(winsock2.h HAVE_WS_HEADER)
IF(HAVE_WS_HEADER)
	SET(pump_WITH_WINSOCK "WITH_WINSOCK")
ELSE()
	SET(pump_WITH_WINSOCK "WITHOUT_WINSOCK")
ENDIF()

IF(HAVE_WS_HEADER AND WITH_IOCP)
	SET(pump_WITH_IOCP "WITH_IOCP")
ELSE()
	SET(pump_WITH_IOCP "WITHOUT_IOCP")
ENDIF()

CHECK_INCLUDE_FILE(sys/epoll.h HAVE_EPOLL_HEADER)
IF(HAVE_EPOLL_HEADER AND NOT HAVE_WS_HEADER)
	SET(pump_WITH_EPOLL "WITH_EPOLL")
ELSE()
	SET(pump_WITH_EPOLL "WITHOUT_EPOLL")
ENDIF()

CHECK_INCLUDE_FILE(strings.h FOUND_STRNGS_HEADER)
IF(FOUND_STRNGS_HEADER)
	SET(pump_FOUND_STRNGS_HEADER "FOUND_STRNGS_HEADER")
ELSE()
	SET(pump_FOUND_STRNGS_HEADER "NOT_FOUND_STRNGS_HEADER")
ENDIF()

CHECK_INCLUDE_FILE(iconv.h FOUND_ICONV_HEADER)
IF(FOUND_ICONV_HEADER)
	SET(pump_FOUND_ICONV_HEADER "FOUND_ICONV_HEADER")
ELSE()
	SET(pump_FOUND_ICONV_HEADER "NOT_FOUND_ICONV_HEADER")
ENDIF()

CHECK_FUNCTION_EXISTS(strcpy_s FOUND_STRCPYS_FUNC)
CHECK_FUNCTION_EXISTS(strncpy FOUND_STRNCPY_FUNC)
IF(FOUND_STRCPYS_FUNC)
	SET(pump_FOUND_STRNCPY_FUNC "FOUND_STRCPYS")
ELSEIF(FOUND_STRNCPY_FUNC)
	SET(pump_FOUND_STRNCPY_FUNC "FOUND_STRNCPY")
ENDIF()

CHECK_FUNCTION_EXISTS(sprintf_s FOUND_SPRINTFS_FUNC)
CHECK_FUNCTION_EXISTS(snprintf FOUND_SNPRINTF_FUNC)
IF(FOUND_SPRINTFS_FUNC)
	SET(pump_FOUND_SNPRINTF_FUNC "FOUND_SPRINTFS")
ELSEIF(FOUND_SNPRINTF_FUNC)
	SET(pump_FOUND_SNPRINTF_FUNC "FOUND_SNPRINTF")
ENDIF()

CHECK_FUNCTION_EXISTS(_strnicmp FOUND_STRNICMP_FUNC)
CHECK_FUNCTION_EXISTS(strncasecmp FOUND_STRNCASECMP_FUNC)
IF(FOUND_STRNICMP_FUNC)
	SET(pump_WITH_STRNCASECMP_FUNC "FOUND_STRNICMP")
ELSEIF(FOUND_STRNCASECMP_FUNC)
	SET(pump_WITH_STRNCASECMP_FUNC "FOUND_STRNCASECMP")
ENDIF()

CHECK_FUNCTION_EXISTS(SwitchToThread FOUND_SWTCHTOTHREAD_FUNC)
CHECK_FUNCTION_EXISTS(sched_yield FOUND_SCHEDYIELD_FUNC)
IF(FOUND_SWTCHTOTHREAD_FUNC)
	SET(pump_WITH_SCHEDYIELD_FUNC "FOUND_SWTCHTOTHREAD")
ELSEIF(FOUND_SCHEDYIELD_FUNC)
	SET(pump_WITH_SCHEDYIELD_FUNC "FOUND_SCHEDYIELD")
ELSE()
	SET(pump_WITH_SCHEDYIELD_FUNC "FOUND_SCHEDSLEEP")
ENDIF()