cmake_minimum_required(VERSION 3.0)
project(qglengine)
find_package(QAD REQUIRED)
find_qt(Qt5 Core Gui OpenGL Xml)
if (NOT Qt5)
	message(WARNING "Building ${PROJECT_NAME} available only on Qt5!")
else()
	if (LIBPROJECT)
		include(SDKMacros)
	else()
		option(LIB "System install" 0)
		option(DEBUG "Build with -g3" 0)
		option(DESIGNER_PLUGINS "Build qt designer plugins" 1)
		set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -O3 -Wall")
		if (DEBUG)
			set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -g3")
		endif()
		include(DeployMacros)
	endif()
	if (MINGW)
		find_package(MinGW REQUIRED)
	endif()
	find_package(OpenGL REQUIRED)
	set_version(qglengine
		MAJOR    1
		MINOR    0
		REVISION 0
		BUILD    "${BUILD_NUMBER}"
		SUFFIX   rc
		OUTPUT   "${CMAKE_CURRENT_SOURCE_DIR}/core/qglengine_version.h")

	qt_sources(SRC)
	qt_sources(FSRC DIR "formats")
	list(APPEND SRC ${FSRC})
	qt_sources(FSRC DIR "core")
	list(APPEND SRC ${FSRC})
	qt_wrap(${SRC} HDRS out_HDR CPPS out_CPP QMS out_QM)
	file(GLOB PHS "*_p.h" "formats/*_p.h" "core/*_p.h")
	list(REMOVE_ITEM out_HDR "${PHS}")
	import_version(qglengine_core qglengine)
	set_deploy_property(qglengine_core SHARED
		LABEL "QGLEngine core library"
		FULLNAME "${_DOMAIN}.qglengine_core"
		COMPANY "${_COMPANY}"
		INFO "QGLEngine core library")
	make_rc(qglengine_core _RC)
	qt_add_library(qglengine_core SHARED out_CPP ${_RC})
	qt_target_include_directories(qglengine_core PRIVATE ${QAD_INCLUDES} "${CMAKE_CURRENT_SOURCE_DIR}" "${CMAKE_CURRENT_SOURCE_DIR}/core")
	qt_target_link_libraries(qglengine_core qad_utils qad_widgets assimp ${OPENGL_LIBRARIES})
	message(STATUS "Building QGLEngine version ${qglengine_VERSION} (SHARED)")
	list(APPEND QT_MULTILIB_LIST qglengine_core)
	add_subdirectory(widgets)
	set(QT_MULTILIB_LIST ${QT_MULTILIB_LIST} PARENT_SCOPE)
	if (LIBPROJECT)
		sdk_install("qglengine" "qglengine_core" "${out_HDR}" "${out_QM}")
	else()
		if (LIB)
			if (WIN32)
				qt_install(FILES ${out_HDR} DESTINATION ${MINGW_INCLUDE}/qglengine)
				qt_install(TARGETS qglengine_core ARCHIVE DESTINATION ${MINGW_LIB})
				qt_install(TARGETS qglengine_core RUNTIME DESTINATION ${MINGW_BIN})
				qt_install(TARGETS qglengine_core RUNTIME DESTINATION QtBin)
			else()
				qt_install(FILES ${out_HDR} DESTINATION ${CMAKE_INSTALL_PREFIX}/include/qglengine)
				qt_install(TARGETS qglengine_core DESTINATION ${CMAKE_INSTALL_PREFIX}/lib)
			endif()
			message(STATUS "Install qglengine_core to system \"${CMAKE_INSTALL_PREFIX}\"")
		else()
			if(WIN32)
				qt_install(TARGETS qglengine_core RUNTIME DESTINATION bin)
				qt_install(TARGETS qglengine_core ARCHIVE DESTINATION lib)
			else()
				qt_install(TARGETS qglengine_core DESTINATION bin/lib)
			endif()
			qt_install(FILES ${out_HDR} DESTINATION include/qglengine)
			message(STATUS "Install qglengine_core to local \"bin\", \"lib\" and \"include\"")
		endif()
	endif()
	if (NOT DEFINED ANDROID_PLATFORM)
		if (EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/plugin")
			#add_subdirectory(plugin)
		endif()
	endif()

	qt_sources(test_SRC DIR "qglview_test")
	qt_wrap(${test_SRC} CPPS test_CPP)
	qt_add_executable(qglengine_test test_CPP)
	qt_target_link_libraries(qglengine_test qglengine_core qglengine_widgets)
	qt_target_include_directories(qglengine_test PRIVATE ${QAD_INCLUDES} "${CMAKE_CURRENT_SOURCE_DIR}" "${CMAKE_CURRENT_SOURCE_DIR}/core" "${CMAKE_CURRENT_SOURCE_DIR}/widgets")
endif()
