#
# Export functions:
#	build_pump_library()
#
# Run:
#   build_pump_library()
#

MACRO(build_pump_library)
	CONFIGURE_FILE(
		"${ROOT_DIR}/include/pump/config.h.in"
		"${ROOT_DIR}/include/pump/config.h"
	)

	set_compile_flags(${LIB_COMPILE_FLAGS})

	FILE(GLOB_RECURSE LIB_SOURCES ${ROOT_DIR}/src/*.*)
	FILE(GLOB_RECURSE LIB_HEADERS ${ROOT_DIR}/include/pump/*.h)
	SET(LIB_ALL_FILES ${LIB_SOURCES} ${LIB_HEADERS} ${COM_SOURCES})

	IF("${CMAKE_CXX_COMPILER_ID}" STREQUAL "MSVC")
		FOREACH(FILE_NAME ${LIB_SOURCES})
			FILE(RELATIVE_PATH REL_FILE_NAME "${ROOT_DIR}/src" ${FILE_NAME})
			GET_FILENAME_COMPONENT(REL_FILE_PATH ${REL_FILE_NAME} DIRECTORY)
			IF("${REL_FILE_PATH}" STREQUAL "")
				SOURCE_GROUP("" FILES ${FILE_NAME})
			ELSEIF("${REL_FILE_PATH}" STREQUAL "..")
				SOURCE_GROUP("" FILES ${FILE_NAME})
			ELSE()
				STRING(REPLACE "/" "\\" REL_FILE_PATH "${REL_FILE_PATH}")
				SOURCE_GROUP(${REL_FILE_PATH} FILES ${FILE_NAME})
			ENDIF()
			#MESSAGE(STATUS "file: ${FILE_NAME} path: ${REL_FILE_PATH}")
		ENDFOREACH()
		
		FOREACH(FILE_NAME ${LIB_HEADERS})
			FILE(RELATIVE_PATH REL_FILE_NAME "${ROOT_DIR}/include/pump" ${FILE_NAME})
			GET_FILENAME_COMPONENT(REL_FILE_PATH ${REL_FILE_NAME} DIRECTORY)
			IF("${REL_FILE_PATH}" STREQUAL "")
				SOURCE_GROUP("" FILES ${FILE_NAME})
			ELSEIF("${REL_FILE_PATH}" STREQUAL "..")
				SOURCE_GROUP("" FILES ${FILE_NAME})
			ELSE()
				STRING(REPLACE "/" "\\" REL_FILE_PATH "${REL_FILE_PATH}")
				SOURCE_GROUP(${REL_FILE_PATH} FILES ${FILE_NAME})
			ENDIF()
			#MESSAGE(STATUS "file: ${FILE_NAME} path: ${REL_FILE_PATH}")
		ENDFOREACH()
	ENDIF()
	
	ADD_LIBRARY(${SHARED_LIBRARY_NAME} SHARED ${LIB_ALL_FILES})
	ADD_LIBRARY(${STATIC_LIBRARY_NAME} STATIC ${LIB_ALL_FILES})
	
	IF(BUILD_DEBUG)
		SET(OUTPUT_SHARED_LIB "${SHARED_LIBRARY_NAME}d")
		IF("${CMAKE_CXX_COMPILER_ID}" STREQUAL "MSVC")
			SET(OUTPUT_STATIC_LIB "${STATIC_LIBRARY_NAME}d")
		ELSE()
			SET(OUTPUT_STATIC_LIB "${SHARED_LIBRARY_NAME}d")
		ENDIF()
	ELSE()
		SET(OUTPUT_SHARED_LIB "${SHARED_LIBRARY_NAME}")
		IF("${CMAKE_CXX_COMPILER_ID}" STREQUAL "MSVC")
			SET(OUTPUT_STATIC_LIB "${STATIC_LIBRARY_NAME}")
		ELSE()
			SET(OUTPUT_STATIC_LIB "${SHARED_LIBRARY_NAME}")
		ENDIF()
	ENDIF()
	SET_TARGET_PROPERTIES(${SHARED_LIBRARY_NAME} PROPERTIES 
		OUTPUT_NAME ${OUTPUT_SHARED_LIB}
		CLEAN_DIRECT_OUTPUT 1)
	SET_TARGET_PROPERTIES(${STATIC_LIBRARY_NAME} PROPERTIES 
		OUTPUT_NAME ${OUTPUT_STATIC_LIB}
		CLEAN_DIRECT_OUTPUT 1)
	
	IF(WIN32)
		SET(LINK_LIBS "ws2_32.lib")
	ELSEIF(MSYS OR CYGWIN)
		SET(LINK_LIBS "ws2_32")
	ELSEIF(UNIX)
		SET(LINK_LIBS "pthread")
	ENDIF()
	IF(TLS_LIBRARY)
		SET(LINK_LIBS "${LINK_LIBS}${LIB_SEP}${TLS_LIBRARY}")
	ENDIF()
	IF(CRYPTO_LIBRARY)
		SET(LINK_LIBS "${LINK_LIBS}${LIB_SEP}${CRYPTO_LIBRARY}")
	ENDIF()
	IF(JEMALLOC_LIBRARY)
		SET(LINK_LIBS "${LINK_LIBS}${LIB_SEP}${JEMALLOC_LIBRARY}")
	ENDIF()
	MESSAGE(STATUS "LINK_LIBS: ${LINK_LIBS}")
	TARGET_LINK_LIBRARIES(${SHARED_LIBRARY_NAME} ${LINK_LIBS})
	TARGET_LINK_LIBRARIES(${STATIC_LIBRARY_NAME} ${LINK_LIBS})

	IF(UNIX)
		SET(CMAKE_INSTALL_PREFIX /usr/local)
		INSTALL(DIRECTORY ${ROOT_DIR}/include/pump DESTINATION include)
		INSTALL(TARGETS ${SHARED_LIBRARY_NAME}
				RUNTIME DESTINATION bin
				LIBRARY DESTINATION lib64
				ARCHIVE DESTINATION lib64)
		INSTALL(TARGETS ${STATIC_LIBRARY_NAME}
				RUNTIME DESTINATION bin
				LIBRARY DESTINATION lib64
				ARCHIVE DESTINATION lib64)
	ENDIF()
ENDMACRO()

build_pump_library()
