cmake_minimum_required(VERSION 3.0)
cmake_policy(SET CMP0017 NEW) # need include() with .cmake
project(qad)
set(_QAD_MAJOR    1)
set(_QAD_MINOR    3)
set(_QAD_REVISION 2)
set(_QAD_SUFFIX   beta)
set(_QAD_COMPANY  SHS)
set(_QAD_DOMAIN   org.SHS)

if ("x${CMAKE_MODULE_PATH}" STREQUAL "x")
	set(CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake")
endif()
if (NOT LIBPROJECT)
	set(_plugins_default_ 1)
	if (DEFINED ANDROID_PLATFORM)
		set(_plugins_default_ 0)
	endif()
	option(LIB "System install" 1)
	option(DEBUG "Build with -g3" 0)
	option(DESIGNER_PLUGINS "Build qt designer plugins" ${_plugins_default_})
	option(STATIC_LIB OFF)
	set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -O3 -Wall")
	if (DEBUG)
		set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -g3")
	endif ()
endif()

include_directories(${CMAKE_CURRENT_SOURCE_DIR} ${CMAKE_CURRENT_BINARY_DIR})

set(EXPORT_HEADER "${CMAKE_CURRENT_SOURCE_DIR}/qad_export.h")

if(STATIC_LIB)
	set(QAD_LIB_TYPE STATIC)
	add_definitions(-DQAD_STATIC_DEFINE)
	#message(STATUS "Building QAD static libraries")
else()
	set(QAD_LIB_TYPE SHARED)
	#message(STATUS "Building QAD shared libraries")
endif()

if (NOT DEFINED ANDROID_PLATFORM)
	find_package(OpenGL REQUIRED)
endif()
if (MINGW)
	find_package(MinGW REQUIRED)
endif()


# Version
if(EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/qad_version.h")
	file(REMOVE "${CMAKE_CURRENT_SOURCE_DIR}/qad_version.h")
endif()
set(QAD_VERSION_FILE "${CMAKE_CURRENT_BINARY_DIR}/qad_version.h")
set_version(QAD
	MAJOR    "${_QAD_MAJOR}"
	MINOR    "${_QAD_MINOR}"
	REVISION "${_QAD_REVISION}"
	BUILD    "${BUILD_NUMBER}"
	SUFFIX   "${_QAD_SUFFIX}"
	OUTPUT   "${QAD_VERSION_FILE}")
message(STATUS "Building QAD version ${QAD_VERSION} (${QAD_LIB_TYPE})")


if (NOT LIBPROJECT)
	if (LIB)
		if(WIN32)
			if(MINGW)
				set(CMAKE_INSTALL_PREFIX ${MINGW_DIR})
			endif()
		else()
			if (DEFINED ANDROID_PLATFORM)
				set(CMAKE_INSTALL_PREFIX ${ANDROID_SYSTEM_LIBRARY_PATH}/usr)
			else()
				set(CMAKE_INSTALL_PREFIX /usr/local)
			endif()
		endif()
		message(STATUS "Install ${PROJECT_NAME} to system \"${CMAKE_INSTALL_PREFIX}\"")
	else()
		message(STATUS "Install ${PROJECT_NAME} to local \"bin\", \"lib\" and \"include\"")
	endif()
endif()

file(GLOB CMAKES "cmake/*.cmake" "cmake/*.in")

if (LIB)
	install(FILES ${CMAKES} DESTINATION ${CMAKE_ROOT}/Modules)
	if (WIN32)
		install(FILES ${EXPORT_HEADER} DESTINATION ${MINGW_INCLUDE}/qad)
	else()
		install(FILES ${EXPORT_HEADER} DESTINATION ${CMAKE_INSTALL_PREFIX}/include/qad)
	endif()
else()
	install(FILES ${EXPORT_HEADER} DESTINATION include/qad)
endif()

macro(qad_install _TARGET _HDR_VAR _QM_FILES)
	if ("${_TARGET}" STREQUAL "qad_utils")
		list(APPEND ${_HDR_VAR} "${QAD_VERSION_FILE}")
	endif()
	if (LIBPROJECT)
		sdk_install("qad" "${_TARGET}" "${${_HDR_VAR}}" "${_QM_FILES}")
	else()
		if (LIB)
			if (WIN32)
				if(NOT "x${${_HDR_VAR}}" STREQUAL "x")
					install(FILES ${${_HDR_VAR}} DESTINATION ${MINGW_INCLUDE}/qad)
				endif()
				qt_install(TARGETS ${_TARGET} ARCHIVE DESTINATION ${MINGW_LIB})
				qt_install(TARGETS ${_TARGET} RUNTIME DESTINATION ${MINGW_BIN})
				qt_install(TARGETS ${_TARGET} RUNTIME DESTINATION QtBin)
			else()
				if(NOT "x${${_HDR_VAR}}" STREQUAL "x")
					install(FILES ${${_HDR_VAR}} DESTINATION ${CMAKE_INSTALL_PREFIX}/include/qad)
				endif()
				qt_install(TARGETS ${_TARGET} ARCHIVE DESTINATION ${CMAKE_INSTALL_PREFIX}/lib)
			endif()
			if(NOT "x${_QM_FILES}" STREQUAL "x")
				qt_install(LANG ${_QM_FILES} DESTINATION QtLang)
			endif()
		else()
			if(WIN32)
				qt_install(TARGETS ${_TARGET} RUNTIME DESTINATION bin)
				qt_install(TARGETS ${_TARGET} ARCHIVE DESTINATION lib)
			else()
				qt_install(TARGETS ${_TARGET} DESTINATION lib)
			endif()
			if(NOT "x${${_HDR_VAR}}" STREQUAL "x")
				install(FILES ${${_HDR_VAR}} DESTINATION include/qad)
			endif()
			if(NOT "x${_QM_FILES}" STREQUAL "x")
				qt_install(LANG ${_QM_FILES} DESTINATION lang)
			endif()
		endif()
	endif()
endmacro()

macro(qad_plugin NAME _MODULES _LIBS)
	if (NOT STATIC_LIB)
		if(DESIGNER_PLUGINS)
			set(PROJ_NAME qad_${NAME}_plugin)
			include_directories("..")
			add_definitions(-DQT_PLUGIN)
			add_definitions(-DQT_NO_DEBUG)
			add_definitions(-DQT_SHARED)
			add_definitions(-DQDESIGNER_EXPORT_WIDGETS)
			find_qt(${QtVersions} Core Designer ${_MODULES})
			qt_sources(SRC)
			qt_wrap(${SRC} CPPS out_CPP QMS out_QM)
			qt_add_library(${PROJ_NAME} SHARED out_CPP)
			qt_target_link_libraries(${PROJ_NAME} ${_LIBS} qad_${NAME} ${_${NAME}_PLUGIN_LIBS})
			if (WIN32)
				qt_install(TARGETS ${PROJ_NAME} RUNTIME DESTINATION QtPlugins/designer)
			else()
				qt_install(TARGETS ${PROJ_NAME} DESTINATION QtPlugins/designer)
			endif()
		endif()
	endif()
endmacro()

macro(qad_project NAME _MODULES _LIBS)
	set(PROJ_NAME qad_${NAME})
	find_qt(${QtVersions} Core ${_MODULES})
	qt_sources(SRC)
	import_version(${PROJ_NAME} QAD)
	if(NOT STATIC_LIB)
		set_deploy_property(${PROJ_NAME} SHARED
			LABEL 		${PROJ_NAME}
			FULLNAME 	"${_QAD_DOMAIN}.${PROJ_NAME}"
			COMPANY 	${_QAD_COMPANY}
			INFO 		"QAD ${NAME} library")
	else()
		set_deploy_property(${PROJ_NAME} STATIC
			LABEL 		${PROJ_NAME}
			FULLNAME 	"${_QAD_DOMAIN}.${PROJ_NAME}"
			COMPANY 	${_QAD_COMPANY}
			INFO 		"QAD ${NAME} library")
	endif()
	make_rc(${PROJ_NAME} out_RC)
	qt_wrap(${SRC} HDRS out_HDR CPPS out_CPP QMS out_QM ${out_RC})
	qt_add_library(${PROJ_NAME} ${QAD_LIB_TYPE} out_CPP ${EXPORT_HEADER})
	if(NOT STATIC_LIB)
		qt_target_compile_definitions(${PROJ_NAME} PRIVATE QAD_SHARED_DEFINE)
	endif()
	qt_target_link_libraries(${PROJ_NAME} ${_LIBS})
	qad_install(${PROJ_NAME} "out_HDR" "out_QM")
	set(${NAME}_UTILS 1)
	if (LIBPROJECT)
		set(${NAME}_UTILS ${UTILS})
	endif()
	message(STATUS "Building ${PROJ_NAME}")
	if (EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/plugin")
		set(_${NAME}_PLUGIN_LIBS "${_LIBS}")
		add_subdirectory(plugin)
	endif()
endmacro()

set(DIRS utils widgets application blockview graphic sql_table touch_widgets doc)
foreach(D ${DIRS})
	list(APPEND QT_MULTILIB_LIST qad_${D})
endforeach(D)
set(QT_MULTILIB_LIST ${QT_MULTILIB_LIST} PARENT_SCOPE)
foreach(D ${DIRS})
	include_directories(${D})
	add_subdirectory(${D})
endforeach(D)
if (LIB)
	set(_ALL_TARGETS ${_ALL_TARGETS} PARENT_SCOPE)
endif()
