cmake_minimum_required(VERSION 3.8)
project(sayonara)

include(FindPkgConfig)
include(CheckIncludeFileCXX)
include(CMake/Compiler.cmake)
if(LINK_GSTREAMER_PLUGINS)
	include(CMake/GStreamer.cmake)
endif()

# absolute linker paths are no longer supoorted in -l parameter
# use -L for path and -l for library itself
if(POLICY CMP0003)
	cmake_policy(SET CMP0003 NEW)
endif()

# no more escaping of preprocessor definitions
if(POLICY CMP0005)
	cmake_policy(SET CMP0005 NEW)
endif()

# Run Automoc/Autouic over autogenerated source files (e.g. from qrc files)
if(POLICY CMP0071)
	cmake_policy(SET CMP0071 NEW)
endif()

if(WITH_COTIRE)
	include(CMake/Cotire.cmake)
	message("Compiling with Cotire")
	add_definitions(-D__STRICT_ANSI__)
else()
	message("Compiling without Cotire")
endif()

if(WIN32)
	pkg_check_modules(GIO gio-windows-2.0 REQUIRED)
endif()


set(TAGLIB_FOUND 0)

if(WITH_SYSTEM_TAGLIB)
	message("Looking for system taglib")
	# overwrites TAGLIB_FOUND, TAGLIB_LIBRARY_DIRS, TAGLIB_LIBRARIES
	pkg_check_modules(TAGLIB taglib>=1.10 REQUIRED)

	# Some distributions do not clean the taglib include path
	# or the pkg-config .pc file for taglib after uninstalling
	find_path(TAGLIB_INCLUDE_FOUND "audioproperties.h" PATHS ${TAGLIB_INCLUDE_DIRS})
	if(NOT TAGLIB_INCLUDE_FOUND)
		message(FATAL_ERROR "Taglib does not seem to be fully installed" )
	endif()
endif()

if(NOT TAGLIB_FOUND)
	set(WITH_SYSTEM_TAGLIB 0)

	set(TAGLIB_INCLUDE_DIRS
		${CMAKE_SOURCE_DIR}/src/3rdParty
		${CMAKE_CURRENT_BINARY_DIR}/3rdParty
		${CMAKE_CURRENT_BINARY_DIR}/3rdParty/taglib
	)

	set(TAGLIB_LIBARARY_DIRS
		${CMAKE_CURRENT_BINARY_DIR}/3rdParty/taglib
	)

	set(TAGLIB_LIBRARY_DIRS ${CMAKE_CURRENT_BINARY_DIR}/3rdParty/taglib/taglib)
	set(TAGLIB_LIBRARIES tag)
	
	# Macros.h.in
	set(SAYONARA_INTERNAL_TAGLIB 1)
else()
	# Macros.h.in
	set(SAYONARA_SYSTEM_TAGLIB 1)
endif()

pkg_check_modules(GLIB glib-2.0 REQUIRED)
pkg_check_modules(GOBJECT gobject-2.0 REQUIRED)
pkg_check_modules(GSTREAMER gstreamer-1.0 REQUIRED)
pkg_check_modules(GSTREAMER_BASE gstreamer-base-1.0 REQUIRED)
pkg_check_modules(GSTREAMER_BASE_APP gstreamer-app-1.0 REQUIRED)
pkg_check_modules(ZLIB REQUIRED zlib REQUIRED)

set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)

if( NOT DEFINED CMAKE_BUILD_TYPE OR NOT CMAKE_BUILD_TYPE )
	message("Use standard build type \"Release\"")
	set(CMAKE_BUILD_TYPE "Release")
endif()

# in order to choose another qt installation set CMAKE_PREFIX_PATH
find_package(Qt5 5.8 COMPONENTS Core Widgets Sql Network DBus Xml Svg REQUIRED)
find_package(Qt5LinguistTools REQUIRED)

include_directories(${Qt5Core_INCLUDE_DIRS})
include_directories(${Qt5Widgets_INCLUDE_DIRS})
include_directories(${Qt5Xml_INCLUDE_DIRS})
include_directories(${Qt5Sql_INCLUDE_DIRS})
include_directories(${Qt5DBus_INCLUDE_DIRS})
include_directories(${Qt5Network_INCLUDE_DIRS})
include_directories(${Qt5Svg_INCLUDE_DIRS})
include_directories(${GLIB_INCLUDE_DIRS})
include_directories(${GOBJECT_INCLUDE_DIRS})
include_directories(${GSTREAMER_INCLUDE_DIRS})
include_directories(${GSTREAMER_BASE_INCLUDE_DIRS})
include_directories(${GSTREAMER_BASE_APP_INCLUDE_DIRS})
include_directories(${TAGLIB_INCLUDE_DIRS})
include_directories(${ZLIB_INCLUDE_DIRS})
include_directories(${CMAKE_CURRENT_SOURCE_DIR})
include_directories(${CMAKE_CURRENT_BINARY_DIR})

add_definitions(${Qt5Core_DEFINITIONS})
add_definitions(${QT_DEFINITIONS})

link_directories(${TAGLIB_LIBRARY_DIRS})
link_directories(${GLIB_LIBRARY_DIRS})
link_directories(${GSTREAMER_LIBRARY_DIRS})

# DBus and Shutdown
if(WIN32)
	message("Windows: host Will compile without dbus and shutdown support")
else()
	message("No Windows: Will compile with dbus and shutdown support")
	# Macros.h.in
	set(SAYONARA_WITH_DBUS 1)
	set(SAYONARA_WITH_SHUTDOWN 1)
	set(WIN32 0)
endif()

# check execinfo for FreeBSD
if(WIN32)
	link_directories(${GIO_LIBRARY_DIRS})
elseif(${CMAKE_SYSTEM_NAME} MATCHES "FreeBSD")
	find_library(execinfo_LIB execinfo)
	if(NOT execinfo_LIB)
		message(FATAL_ERROR "FreeBSD requires execinfo library")
	endif()
	set(OS_LIBRARIES ${execinfo_LIB})
endif()


# Check demangle
check_include_file_cxx("cxxabi.h" HAVE_CXX_ABI)
if(${HAVE_CXX_ABI})
	# Macros.h.in
	set(SAYONARA_HAS_CXX_ABI 1)
	message("Compile with demangle")
else()
	message("Demangle not found")
endif()

configure_file(Macros.h.in "${CMAKE_CURRENT_BINARY_DIR}/Utils/Macros.h" @ONLY)

set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTOUIC OFF)
set(CMAKE_INCLUDE_CURRENT_DIR ON)

add_subdirectory(3rdParty)
add_subdirectory(Application)
add_subdirectory(Components)
add_subdirectory(Database)
add_subdirectory(DBus)
add_subdirectory(Interfaces)
add_subdirectory(Gui)
add_subdirectory(Languages)
add_subdirectory(Utils)

qt5_add_resources(DATABASE_RCC
	${CMAKE_CURRENT_SOURCE_DIR}/../resources/Database.qrc
	${CMAKE_CURRENT_SOURCE_DIR}/../resources/Resources.qrc
)

add_executable(${PROJECT_NAME} Main.cpp ${QM_FILES} ${DATABASE_RCC})

if(WIN32)
	set(OS_LIBRARIES
		api-ms-win-core-winrt-l1-1-0
		api-ms-win-core-winrt-string-l1-1-0
		${GIO_LIBRARIES}
	)
endif()

target_link_libraries(${PROJECT_NAME}
	sayonara_application
	sayonara_languages

	${OS_LIBRARIES}
)

add_executable("create_db" CreateDB.cpp ${DATABASE_RCC})
target_link_libraries("create_db"
	sayonara_utils
	sayonara_database
	Qt5::Widgets
)

if(WIN32)
	install(TARGETS ${PROJECT_NAME} DESTINATION .)
else()
	install(TARGETS ${PROJECT_NAME} DESTINATION bin)
endif()

if(GSTREAMER_PLUGIN_LIBRARIES)
	message("Will install gstreamer plugins")
	install(FILES ${GSTREAMER_PLUGIN_LIBRARIES} DESTINATION lib/gstreamer-1.0)
	install(FILES ${GSTREAMER_PLUGIN_SCANNER} DESTINATION lib/gstreamer1.0/gstreamer-1.0)
endif()
