cmake_minimum_required(VERSION 2.6.3)

IF(DEFINED CMAKE_BUILD_TYPE)
     SET(CMAKE_BUILD_TYPE ${CMAKE_BUILD_TYPE} CACHE STRING "Choose the type of build, options are: None Debug Release RelWithDebInfo MinSizeRel")
ELSE(DEFINED CMAKE_BUILD_TYPE)
    SET(CMAKE_BUILD_TYPE Debug CACHE STRING "Choose the type of build, options are: None Debug Release RelWithDebInfo MinSizeRel")
ENDIF(DEFINED CMAKE_BUILD_TYPE)

PROJECT(all)

IF(NOT DEFINED CRTMPSERVER_SOURCES_ROOT)
	SET(CRTMPSERVER_SOURCES_ROOT "../../sources")
ENDIF(NOT DEFINED CRTMPSERVER_SOURCES_ROOT)
GET_FILENAME_COMPONENT(CRTMPSERVER_SOURCES_ROOT "${CRTMPSERVER_SOURCES_ROOT}" ABSOLUTE)
#MESSAGE("CRTMPSERVER_SOURCES_ROOT: ${CRTMPSERVER_SOURCES_ROOT}")

IF(NOT DEFINED CRTMPSERVER_CONSTANTS_ROOT)
    SET(CRTMPSERVER_CONSTANTS_ROOT "../../constants")
ENDIF(NOT DEFINED CRTMPSERVER_CONSTANTS_ROOT)
GET_FILENAME_COMPONENT(CRTMPSERVER_CONSTANTS_ROOT "${CRTMPSERVER_CONSTANTS_ROOT}" ABSOLUTE)
#MESSAGE("CRTMPSERVER_CONSTANTS_ROOT: ${CRTMPSERVER_CONSTANTS_ROOT}")

IF(NOT DEFINED CRTMPSERVER_3RDPARTY_ROOT)
	SET(CRTMPSERVER_3RDPARTY_ROOT "../../3rdparty")
ENDIF(NOT DEFINED CRTMPSERVER_3RDPARTY_ROOT)
GET_FILENAME_COMPONENT(CRTMPSERVER_3RDPARTY_ROOT "${CRTMPSERVER_3RDPARTY_ROOT}" ABSOLUTE)
#MESSAGE("CRTMPSERVER_3RDPARTY_ROOT: ${CRTMPSERVER_3RDPARTY_ROOT}")

SET(CRTMPSERVER_ROOT "lib/crtmpserver")

IF(DEFINED CRTMPSERVER_INSTALL_PREFIX)
	SET(CMAKE_INSTALL_PREFIX ${CRTMPSERVER_INSTALL_PREFIX})
	SET(CMAKE_INSTALL_RPATH_USE_LINK_PATH TRUE)
	SET(CMAKE_BUILD_WITH_INSTALL_RPATH TRUE)
	SET(CMAKE_INSTALL_RPATH "${CMAKE_INSTALL_PREFIX}/${CRTMPSERVER_ROOT}")
	SET(CMAKE_INSTALL_NAME_DIR "${CMAKE_INSTALL_PREFIX}/${CRTMPSERVER_ROOT}")
ELSE(DEFINED CRTMPSERVER_INSTALL_PREFIX)
	SET(CMAKE_INSTALL_PREFIX /tmp/crtmpserver)
ENDIF(DEFINED CRTMPSERVER_INSTALL_PREFIX)

#sample commit to trigger HUDSON

#detect additional libraries
SET(OpenSSL_FIND_REQUIRED true)
SET(CRYPTO_FIND_REQUIRED true)
INCLUDE(cmake_find_modules/Find_openssl.cmake)
INCLUDE(cmake_find_modules/Find_dl.cmake)
INCLUDE(cmake_find_modules/pch_support.cmake)
INCLUDE(CMakeDetermineSystem)
INCLUDE(TestBigEndian)


#setup the endianess if not already defined in toolchain file
if(NOT DEFINED NO_ENDIANESS)
	TEST_BIG_ENDIAN(ENDIANESS)
	IF(ENDIANESS)
		ADD_DEFINITIONS(-DBIG_ENDIAN_BYTE_ALIGNED)
	ELSE(ENDIANESS)
		ADD_DEFINITIONS(-DLITTLE_ENDIAN_BYTE_ALIGNED)
	ENDIF(ENDIANESS)
ENDIF(NOT DEFINED NO_ENDIANESS)

#supported features
ADD_DEFINITIONS(-DHAS_PROTOCOL_HTTP)
ADD_DEFINITIONS(-DHAS_PROTOCOL_RTMP)
ADD_DEFINITIONS(-DHAS_PROTOCOL_LIVEFLV)
ADD_DEFINITIONS(-DHAS_PROTOCOL_RTP)
ADD_DEFINITIONS(-DHAS_PROTOCOL_TS)
ADD_DEFINITIONS(-DHAS_PROTOCOL_VAR)
ADD_DEFINITIONS(-DHAS_PROTOCOL_CLI)
#ADD_DEFINITIONS(-DHAS_PROTOCOL_MMS)
ADD_DEFINITIONS(-DHAS_PROTOCOL_HLS)
#ADD_DEFINITIONS(-DHAS_PROTOCOL_RAWHTTPSTREAM)
ADD_DEFINITIONS(-DHAS_MEDIA_MP3)
ADD_DEFINITIONS(-DHAS_MEDIA_MP4)
ADD_DEFINITIONS(-DHAS_MEDIA_FLV)
#ADD_DEFINITIONS(-DHAS_MEDIA_NSV)
#ADD_DEFINITIONS(-DHAS_SYSLOG)
IF($ENV{COMPILE_STATIC} MATCHES "1")
	MESSAGE(STATUS "Compile static")
	ADD_DEFINITIONS(-DCOMPILE_STATIC)
	SET(LIB_TYPE STATIC)
	ADD_SUBDIRECTORY(lua lua)
	ADD_SUBDIRECTORY(tinyxml tinyxml)
	SET(LUA_LIBRARY_PATH "lua")
	SET(LUA_INCLUDE_PATH "${CRTMPSERVER_3RDPARTY_ROOT}/lua-dev")
	SET(TINYXML_LIBRARY_PATH "tinyxml")
	SET(TINYXML_INCLUDE_PATH "${CRTMPSERVER_3RDPARTY_ROOT}/tinyxml")
#	SET(TEMP_EXE_LINK_C_FLAGS "${CMAKE_EXE_LINK_STATIC_C_FLAGS}")
#	SET(TEMP_EXE_LINK_CXX_FLAGS "${CMAKE_EXE_LINK_STATIC_CXX_FLAGS}")
#	SET(CMAKE_EXE_LINK_DYNAMIC_C_FLAGS ${TEMP_EXE_LINK_C_FLAGS})
#	SET(CMAKE_EXE_LINK_DYNAMIC_CXX_FLAGS ${TEMP_EXE_LINK_CXX_FLAGS})
#	SET(CMAKE_EXE_LINK_STATIC_C_FLAGS ${TEMP_EXE_LINK_C_FLAGS})
#	SET(CMAKE_EXE_LINK_STATIC_CXX_FLAGS ${TEMP_EXE_LINK_CXX_FLAGS})
ELSE($ENV{COMPILE_STATIC} MATCHES "1")
	MESSAGE(STATUS "Compile dynamic")
	SET(LIB_TYPE SHARED)
	
	INCLUDE(cmake_find_modules/Find_lua.cmake)
	IF(NOT LUA_FOUND)
		ADD_SUBDIRECTORY(lua lua)
	ENDIF(NOT LUA_FOUND)

	INCLUDE(cmake_find_modules/Find_tinyxml.cmake)
	IF(NOT TINYXML_FOUND)
		ADD_SUBDIRECTORY(tinyxml tinyxml)
	ENDIF(NOT TINYXML_FOUND)
ENDIF($ENV{COMPILE_STATIC} MATCHES "1")

#fine tunning
ADD_DEFINITIONS(-DCreateRTCPPacket=CreateRTCPPacket_mystyle_only_once)
ADD_DEFINITIONS(-DFeedDataAudioMPEG4Generic=FeedDataAudioMPEG4Generic_one_by_one)
ADD_DEFINITIONS(-DHandleTSVideoData=HandleVideoData_version3)

ADD_DEFINITIONS(-DHAS_LUA)

#INCLUDE(cmake_find_modules/Find_v8.cmake)
#IF(V8_FOUND)
#    ADD_DEFINITIONS(-DHAS_V8)
#ENDIF(V8_FOUND)

#memory pool
#ADD_DEFINITIONS(-DUSE_MEM_POOL)
#ADD_DEFINITIONS(-DDEBUG_MEM_POOL)

SET(GENERIC_FLAGS "-Wall -Werror")

#add 64 bit support for 32 but platforms
ADD_DEFINITIONS(-D__USE_FILE_OFFSET64)
ADD_DEFINITIONS(-D_FILE_OFFSET_BITS=64)
MESSAGE(STATUS "We have a ${CMAKE_SYSTEM} system")
IF(CMAKE_SYSTEM_NAME MATCHES "FreeBSD")
	SET(GENERIC_FLAGS "${GENERIC_FLAGS} -fPIC")
	IF(DEFINED SOCKREACTOR)
		ADD_DEFINITIONS(-D${SOCKREACTOR})
	ELSE(DEFINED SOCKREACTOR)
		ADD_DEFINITIONS(-DNET_KQUEUE)
	ENDIF(DEFINED SOCKREACTOR)
	STRING(TOLOWER ${CMAKE_SYSTEM} TEMP_CMAKE_SYSTEM)
	STRING(REGEX MATCH "kfreebsd*" TEMP_CMAKE_SYSTEM_MATCH ${TEMP_CMAKE_SYSTEM})
	IF(TEMP_CMAKE_SYSTEM_MATCH MATCHES "kfreebsd")
		ADD_DEFINITIONS(-DDFREEBSD)
	ELSE(TEMP_CMAKE_SYSTEM_MATCH MATCHES "kfreebsd")
		ADD_DEFINITIONS(-DFREEBSD)
		INCLUDE(cmake_find_modules/Find_execinfo.cmake)
	ENDIF(TEMP_CMAKE_SYSTEM_MATCH MATCHES "kfreebsd")
ELSEIF(CMAKE_SYSTEM_NAME MATCHES "Darwin")
	SET(GENERIC_FLAGS "${GENERIC_FLAGS} -mmacosx-version-min=10.5")
	ADD_DEFINITIONS(-DOSX)
	IF(DEFINED SOCKREACTOR)
		ADD_DEFINITIONS(-D${SOCKREACTOR})
	ELSE(DEFINED SOCKREACTOR)
		ADD_DEFINITIONS(-DNET_KQUEUE)
	ENDIF(DEFINED SOCKREACTOR)
ELSEIF(CMAKE_SYSTEM_NAME MATCHES "Linux")
	SET(GENERIC_FLAGS "${GENERIC_FLAGS} -fPIC")
	ADD_DEFINITIONS(-DLINUX)
	IF(DEFINED SOCKREACTOR)
		ADD_DEFINITIONS(-D${SOCKREACTOR})
	ELSE(DEFINED SOCKREACTOR)
		ADD_DEFINITIONS(-DNET_EPOLL)
	ENDIF(DEFINED SOCKREACTOR)
ELSEIF(CMAKE_SYSTEM_NAME MATCHES "SunOS")
	SET(GENERIC_FLAGS "${GENERIC_FLAGS} -fPIC")
	ADD_DEFINITIONS(-DSOLARIS)
	ADD_DEFINITIONS(-DDONT_DEFINE_HTONLL)
	ADD_DEFINITIONS(-DNET_SELECT)
	SET(SOCKET_LIB "-lsocket")
ELSE(CMAKE_SYSTEM_NAME MATCHES "FreeBSD")
	MESSAGE(FATAL_ERROR "Platform ${CMAKE_SYSTEM_NAME} not supported yet")
ENDIF(CMAKE_SYSTEM_NAME MATCHES "FreeBSD")

#debug/release
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${GENERIC_FLAGS}")
SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${GENERIC_FLAGS}")
SET(CMAKE_CXX_FLAGS_DEBUG "-g -O0 -DWITH_SANITY_INPUT_BUFFER -DVALIDATE_FROMAT_SPECIFIERS -Wformat=2")
SET(CMAKE_CXX_FLAGS_RELEASE "-O3 -fno-strict-aliasing")
#SET(CMAKE_VERBOSE_MAKEFILE TRUE)

#generate defines.h
IF (EXISTS ${CRTMPSERVER_SOURCES_ROOT}/common/include/defines.h)
	MESSAGE(STATUS "Defines are in place")
ELSE (EXISTS ${CRTMPSERVER_SOURCES_ROOT}/common/include/defines.h)
	MESSAGE(STATUS "We need to rebuild the defines")
	EXECUTE_PROCESS(COMMAND lua ${CRTMPSERVER_CONSTANTS_ROOT}/constants.lua cpp ${CRTMPSERVER_SOURCES_ROOT}/common/include/defines.h)
ENDIF(EXISTS ${CRTMPSERVER_SOURCES_ROOT}/common/include/defines.h)


#ADD_DEFINITIONS(-DHAS_SAFE_LOGGER)
ADD_SUBDIRECTORY(common common)
ADD_SUBDIRECTORY(thelib thelib)
#ADD_SUBDIRECTORY(vm vm)
ADD_SUBDIRECTORY(applications applications)
ADD_SUBDIRECTORY(crtmpserver crtmpserver)
#ADD_SUBDIRECTORY(vmtests vmtests)
ADD_SUBDIRECTORY(tests)
#ADD_SUBDIRECTORY(androidapplestreaming androidapplestreaming)

