project(qad)
cmake_minimum_required(VERSION 2.6)
#cmake_policy(SET CMP0017 OLD)
#find_package(Qt4 REQUIRED)
if (LIBPROJECT)
	include(../SDKMacros.cmake)
else()
	option(LIB "System install" 1)
	option(DEBUG "Build with -g3" 0)
	set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -O3 -Wall")
	if (DEBUG)
		set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -g3")
	endif ()
endif()

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

if (NOT LIBPROJECT)
	if (LIB)
		if (WIN32)
			set(CMAKE_INSTALL_PREFIX ${MINGW_DIR})
		else()
			if(APPLE)
				set(CMAKE_INSTALL_PREFIX /usr/local)
			else()
				set(CMAKE_INSTALL_PREFIX /usr)
			endif()
		endif()
		message(STATUS "Install ${PROJECT_NAME} to system \"${CMAKE_INSTALL_PREFIX}\"")
	else()
		message(STATUS "Install ${PROJECT_NAME} to local \"bin\"")
	endif()
endif()
set(CMAKES "FindQAD.cmake" "QtWraps.cmake")
install(FILES ${CMAKES} DESTINATION ${CMAKE_ROOT}/Modules)

macro(qad_install _TARGET _H_FILES)
	if (LIBPROJECT)
		sdk_install("qad" "${_TARGET}" "${_H_FILES}")
	else()
		if (LIB)
			if (WIN32)
				if (NOT _H_FILES STREQUAL "")
					#message("HFILES ${_H_FILES}")
					install(FILES ${_H_FILES} DESTINATION ${MINGW_INCLUDE}/qad)
				endif()
				qt_install(TARGETS ${_TARGET} DESTINATION ${MINGW_LIB})
				qt_install(TARGETS ${_TARGET} DESTINATION ${MINGW_BIN})
				qt_install(TARGETS ${_TARGET} DESTINATION QtBin)
			else()
				if(NOT _H_FILES STREQUAL "")
					install(FILES ${_H_FILES} DESTINATION ${CMAKE_INSTALL_PREFIX}/include/qad)
				endif()
				qt_install(TARGETS ${_TARGET} DESTINATION ${CMAKE_INSTALL_PREFIX}/lib)
			endif()
		else()
			qt_install(TARGETS ${_TARGET} DESTINATION bin)
		endif()
	endif()
endmacro()

macro(qad_plugin NAME _MODULES _LIBS)
	set(PROJ_NAME qad_${NAME}_plugin)
	include_directories("..")
	#message(${_${NAME}_PLUGIN_LIBS})
	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})
	qt_install(TARGETS ${PROJ_NAME} DESTINATION QtPlugins/designer)
endmacro()

macro(qad_project NAME _MODULES _LIBS)
	set(PROJ_NAME qad_${NAME})
	find_qt(${QtVersions} Core ${_MODULES})
	qt_sources(SRC)
	qt_wrap(${SRC} HDRS out_HDR CPPS out_CPP QMS out_QM)
	qt_add_library(${PROJ_NAME} SHARED out_CPP)
	qt_target_link_libraries(${PROJ_NAME} ${_LIBS})
	qad_install(${PROJ_NAME} "${out_HDR}")
	message(STATUS "Building ${PROJ_NAME}")
	if (NOT DEFINED ANDROID_PLATFORM)
		if (EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/plugin")
			set(_${NAME}_PLUGIN_LIBS "${_LIBS}")
			add_subdirectory(plugin)
		endif()
	endif()
endmacro()

set(DIRS utils widgets application blockview graphic sql_table touch_widgets)
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)

