# find_qt([<Module1> <Module2> ...] [Qt4] [Qt5])
#
# Find Qt4 or/and Qt5 packages
# If Qt4/5 found, set
#  * Qt<4/5>_ROOT        var to Qt root dir
#  * Qt<4/5>_BIN         var to Qt binary dir
#  * Qt<4/5>_PLUGINS_DIR var to Qt plugins dir
#  * Qt<4/5>_LANG_DIR    var to Qt translations dir
#
#
#
#
# qt_sources(<src_out_var> [NO_DEFAULT] [DIR <dir>] [<regexp> ...])
#
# Collect all sources, optionally in directory <dir> and
# Additional filters <regexp>. By default filter files with regexps
# "*.h", "*.hpp", "*.c", "*.cpp", "*.ui", "*.qrc", "*.rc", "*.ts" and "lang/*.ts".
# Default filters disabled with option NO_DEFAULT
#
#
#
#
# qt_wrap(<file0> <file1> ... [HDRS <hrd_var>] [CPPS <cpp_var>] [QMS <qm_var>])
#
# Prepare sources for compile
# Store headers to <hrd_var>, all wrapped Qt4 files to <cpp_var>_Qt4 and Qt5 files to <cpp_var>_Qt5
# version are automatic detected after find_qt() call
#
#
#
#
# qt_add_executable(<name> [WIN32] [MACOSX_BUNDLE] [EXCLUDE_FROM_ALL] source1 [source2 ...])
#
# Add executables for founded Qt versions, target names is <name><TARGET_SUFFIX_Qt?>
# All needed Qt includes automatic added to corresponding targets
# <source> cant be CPPS variable passed to qt_wrap() or some list or filename
#
#
#
#
# qt_add_library(<name> [STATIC | SHARED | MODULE] [EXCLUDE_FROM_ALL] source1 [source2 ...])
#
# Add library for founded Qt versions, target names is <name><TARGET_SUFFIX_Qt?>
# All needed Qt includes automatic added to corresponding targets
# <source> cant be CPPS variable passed to qt_wrap() or some list or filename
#
#
#
#
# qt_target_link_libraries(<name> ... <item> ...)
#
# Link targets for founded Qt versions, target names is <name><TARGET_SUFFIX_Qt?>
# All needed Qt libraries automatic added to corresponding targets
# if QT_MULTILIB_LIST contains some linking library, to it name automatic
# add <MULTILIB_SUFFIX_Qt?> or individual <MULTILIB_>libname<_SUFFIX_Qt?>
#
#
#
#
# qt_target_compile_definitions(<name> ...)
#
# Syntax similar to original "target_compile_definitions()"
# Specify compile definitions to use when compiling a given <name>
#
#
#
#
# qt_target_include_directories(<name> ...)
#
# Syntax similar to original "target_include_directories()"
# Add include directories to a target
#
#
#
#
# qt_install(...)
#
# Syntax similar to original "install()"
# Install targets for founded Qt versions
#
# if DESTINATION set to "QtBin" files will be installed
# to corresponding Qt binary directory
# if DESTINATION set to "QtLang" files will be installed
# to corresponding Qt translation directory
# if DESTINATION starts with "QtPlugins" files will be installed
# to corresponding Qt plugins directory
#
# Add LANG type for first argument - in this case you should
# specify variable name, not variable value!
# e.g. "qt_install(LANG my_QM DESTINATION QtLang)"
#
#
#
#
# qt_install_lang(<name> DESTINATION <dir>)
#
# Search Qt translations for all <name> dependencies
# in Qt<?>_LANG_DIR for <name>_LANG languages
# and install them to <dir>. You can you "set_lang()" macro
# before this action
#
#


cmake_policy(SET CMP0011 NEW) # don`t affect includer policies
cmake_policy(SET CMP0020 NEW) # Automatically link Qt executables to qtmain target on Windows


set(_QT_VERSIONS_ 4 5)
foreach(_v ${_QT_VERSIONS_})
	if (NOT DEFINED TARGET_SUFFIX_Qt${_v})
		set(TARGET_SUFFIX_Qt${_v} "${_v}")
		set(MULTILIB_SUFFIX_Qt${_v} "${_v}")
	endif()
	set(MOC_INC_Qt${_v})
endforeach()
if (NOT DEFINED QT_MULTILIB_LIST)
	set(QT_MULTILIB_LIST)
endif()
#set(TARGET_SUFFIX_Qt4 "")


macro(find_qt)
	set(_QCOMP)
	foreach(_v ${_QT_VERSIONS_})
		set(_NEED${_v} 0)
		set(LOCAL_FOUND${_v} 0)
		set(MOC_INC_Qt${_v})
		set(Qt${_v}_INCLUDES)
		set(Qt${_v}_LIBRARIES)
		set(Qt${_v}_ROOT)
		set(Qt${_v}_BIN)
		set(Qt${_v}_PLUGINS_DIR)
		set(Qt${_v}_LANG_DIR)
	endforeach()
	set(_NEED_SOME 0)
	set(_QUIET)
	foreach(_i ${ARGN})
		set(_FOUND_NEED 0)
		foreach(_v ${_QT_VERSIONS_})
			if ("x${_i}" STREQUAL "xQt${_v}")
				set(_NEED${_v} 1)
				set(_NEED_SOME 1)
				set(_FOUND_NEED 1)
			endif()
			if ("x${_i}" STREQUAL "xQUIET")
				set(_QUIET QUIET)
			endif()
		endforeach()
		if (NOT _FOUND_NEED)
			list(APPEND _QCOMP "${_i}")
		endif()
	endforeach()
	if (NOT _NEED_SOME)
		foreach(_v ${_QT_VERSIONS_})
			set(_NEED${_v} 1)
		endforeach()
	endif()
	list(REMOVE_DUPLICATES _QCOMP)
	if (_NEED4)
		find_package(Qt4 ${_QUIET})
		if (Qt4_FOUND)
			set(LOCAL_FOUND4 1)
			get_filename_component(Qt4_BIN ${QT_QMAKE_EXECUTABLE} PATH)
			set(Qt4_INCLUDES ${QT_INCLUDES})
			foreach(m ${_QCOMP})
				string(TOUPPER "${m}" _um)
				#message("${QT_QT${_um}_FOUND}")
				if (${QT_QT${_um}_FOUND})
					list(APPEND Qt4_LIBRARIES ${QT_QT${_um}_LIBRARY})
				endif()
			endforeach()
			set(Qt4_PLUGINS_DIR ${QT_PLUGINS_DIR})
			set(Qt4_LANG_DIR ${Qt4_BIN}/../translations)
			set(Qt4_ROOT "${Qt4_BIN}/../")
		endif()
	endif()
	if (_NEED5)
		#set(_MSG 1)
		#if (Qt5_FOUND)
		#	set(_MSG 0)
		#endif()
		find_package(Qt5 COMPONENTS LinguistTools UiPlugin Widgets ${_QCOMP} ${_QUIET})
		if (Qt5_FOUND)
			set(LOCAL_FOUND5 1)
			set(_Qt5Modules)
			foreach(m ${_QCOMP})
				if (${Qt5${m}_FOUND})
					list(APPEND _Qt5Modules ${m})
					list(APPEND Qt5_INCLUDES ${Qt5${m}_INCLUDE_DIRS})
					list(APPEND Qt5_LIBRARIES ${Qt5${m}_LIBRARIES})
				endif()
			endforeach()
			#get_property(_up_dir TARGET Qt5::UiPlugin PROPERTY INTERFACE_INCLUDE_DIRECTORIES)
			#message("${_up_dir}")
			#if (NOT "x${_up_dir}" STREQUAL "x")
			#	list(APPEND Qt5_INCLUDES ${_up_dir})
			#endif()
			get_target_property(_Qt5_qmake Qt5::qmake LOCATION)
			get_filename_component(Qt5_BIN ${_Qt5_qmake} PATH)
			foreach(_p ${Qt5Gui_PLUGINS})
				get_target_property(_l ${_p} LOCATION)
				get_filename_component(_l ${_l} DIRECTORY)
				string(FIND "${_l}" "/" _ind REVERSE)
				string(SUBSTRING "${_l}" 0 ${_ind} _l)
				set(Qt5_PLUGINS_DIR ${_l})
				#message("Qt5: plugins dir: ${Qt5_PLUGINS_DIR}")
				break()
			endforeach()
			string(FIND "${Qt5_BIN}" "/" _ind REVERSE)
			string(SUBSTRING "${Qt5_BIN}" 0 ${_ind} Qt5_ROOT)
			if ("x${Qt5_PLUGINS_DIR}" STREQUAL "x")
				set(Qt5_PLUGINS_DIR "${Qt5_ROOT}/plugins")
			endif()
			if (APPLE)
				list(APPEND MOC_INC_Qt5 "-F${Qt5_ROOT}/lib")
			endif()
			set(Qt5_LANG_DIR ${Qt5_BIN}/../translations)
			if (NOT _QT5_MSG)
				message(STATUS "Found Qt5: ${_Qt5_qmake} (version \"${Qt5_VERSION}\")")
				set(_QT5_MSG 1 CACHE BOOL "msg_qt5" FORCE)
				#message(STATUS "Found Qt5 modules: ${_Qt5Modules}")
			endif()
		endif()
	endif()
	foreach(_v ${_QT_VERSIONS_})
		if (LOCAL_FOUND${_v})
			foreach(_p ${Qt${_v}_INCLUDES})
				list(APPEND MOC_INC_Qt${_v} "-I${_p}")
				#message("${_p}")
			endforeach()
		endif()
		#message("${MOC_INC_Qt${_v}}")
	endforeach()

endmacro()


macro(qt_sources OUT)
	set(${OUT})
	set(_DIR "")
	set(_IS_DIR 0)
	set(_NO_DEFAULT 0)
	set(_DEFAULT)
	set(_REGEXP)
	set(_REGEXP_WD)
	foreach(_i ${ARGN})
		if     (_IS_DIR)
			set(_DIR "${_i}")
			set(_IS_DIR 0)
		elseif ("x${_i}" STREQUAL "xDIR")
			set(_IS_DIR 1)
		elseif ("x${_i}" STREQUAL "xNO_DEFAULT")
			set(_NO_DEFAULT 1)
		else()
			list(APPEND _REGEXP ${_i})
		endif()
	endforeach()
	if (NOT "x${_DIR}" STREQUAL "x")
		#file(TO_CMAKE_PATH "${_DIR}//" _DIR)
		string(REPLACE "\\" "/" _DIR "${_DIR}/")
		string(REPLACE "//" "/" _DIR "${_DIR}")
	endif()
	foreach(_i ${_REGEXP})
		list(APPEND _REGEXP_WD "${_DIR}${_i}")
	endforeach()
	if (NOT _NO_DEFAULT)
		set(_DEFAULT "${_DIR}*.h" "${_DIR}*.hpp" "${_DIR}*.c" "${_DIR}*.cpp" "${_DIR}*.ui" "${_DIR}*.qrc" "${_DIR}*.rc" "${_DIR}*.ts" "${_DIR}lang/*.ts")
	endif()
	#message("${_DEFAULT}, ${_REGEXP_WD}")
	file(GLOB ${OUT} ${_DEFAULT} ${_REGEXP_WD})
endmacro()


macro(qt_wrap)
	set(HDR_VAR)
	set(CPP_VAR)
	set(QM_VAR)
	set(_HDR_ARG 0)
	set(_CPP_ARG 0)
	set(_QM_ARG 0)
	set(WAS_HDR_ARG 0)
	set(WAS_CPP_ARG 0)
	set(WAS_QM_ARG 0)
	set(FILE_LIST)
	foreach(_i ${ARGN})
		if     (_HDR_ARG)
			set(HDR_VAR ${_i})
			set(WAS_HDR_ARG 1)
			set(_HDR_ARG 0)
		elseif (_CPP_ARG)
			set(CPP_VAR ${_i})
			set(WAS_CPP_ARG 1)
			set(_CPP_ARG 0)
		elseif (_QM_ARG)
			set(QM_VAR ${_i})
			set(WAS_QM_ARG 1)
			set(_QM_ARG 0)
		elseif ("x${_i}" STREQUAL "xHDRS")
			set(_HDR_ARG 1)
		elseif ("x${_i}" STREQUAL "xCPPS")
			set(_CPP_ARG 1)
		elseif ("x${_i}" STREQUAL "xQMS")
			set(_QM_ARG 1)
		else()
			list(APPEND FILE_LIST "${_i}")
		endif()
	endforeach()
	foreach(_v ${_QT_VERSIONS_})
		set(CPP${_v}_VAR)
		set(QM${_v}_VAR)
		if (LOCAL_FOUND${_v})
			if (WAS_CPP_ARG)
				set(CPP${_v}_VAR ${CPP_VAR}_Qt${_v})
			endif()
			if (WAS_QM_ARG)
				set(QM${_v}_VAR ${QM_VAR}_Qt${_v})
			endif()
		endif()
	endforeach()
	#message("found 4: ${LOCAL_FOUND4}, 5: ${LOCAL_FOUND5}")
	#message("has HRDS: ${WAS_HDR_ARG}: ${HDR_VAR}")
	#message("has CPPS4: ${WAS_CPP_ARG}: ${CPP4_VAR}")
	#message("has CPPS5: ${WAS_CPP_ARG}: ${CPP5_VAR}")
	#message("files: ${FILE_LIST}")
	set(H_LIST)
	set(CPP_LIST)
	set(UI_LIST)
	set(RES_LIST)
	set(RC_LIST)
	set(TS_LIST)
	foreach(_i ${FILE_LIST})
		get_filename_component(_EXT "${_i}" EXT)
		#message("${_EXT}")
		if (NOT ("x${_EXT}" STREQUAL "x"))
			if (("x${_EXT}" STREQUAL "x.h") OR ("x${_EXT}" STREQUAL "x.hpp"))
				list(APPEND H_LIST "${_i}")
				#message("header")
			endif()
			if (("x${_EXT}" STREQUAL "x.c") OR ("x${_EXT}" STREQUAL "x.cpp") OR ("x${_EXT}" STREQUAL "x.cxx"))
				list(APPEND CPP_LIST "${_i}")
				#message("source")
			endif()
			if ("x${_EXT}" STREQUAL "x.ui")
				list(APPEND UI_LIST "${_i}")
			endif()
			if ("x${_EXT}" STREQUAL "x.qrc")
				list(APPEND RES_LIST "${_i}")
			endif()
			if ("x${_EXT}" STREQUAL "x.rc")
				list(APPEND RC_LIST "${_i}")
			endif()
			if ("x${_EXT}" STREQUAL "x.ts")
				list(APPEND TS_LIST "${_i}")
			endif()
		endif()
	endforeach()
	if (WAS_HDR_ARG)
		set(${HDR_VAR})
		list(APPEND ${HDR_VAR} ${H_LIST})
	endif()
	set(SRC_CMAKE_CURRENT_BINARY_DIR ${CMAKE_CURRENT_BINARY_DIR})
	foreach(_v ${_QT_VERSIONS_})
		if (LOCAL_FOUND${_v} AND WAS_CPP_ARG)
			set(CMAKE_CURRENT_BINARY_DIR ${SRC_CMAKE_CURRENT_BINARY_DIR}/qt${_v})
			set(${CPP${_v}_VAR})
			if (WAS_QM_ARG)
				set(${QM${_v}_VAR})
			endif()
			set(MOCS${_v})
			set(CUIS${_v})
			set(CRES${_v})
			if (${_v} EQUAL 4)
				qt4_wrap_cpp(MOCS${_v} ${H_LIST} OPTIONS -nw ${MOC_INC_Qt4})
				qt4_wrap_ui(CUIS${_v} ${UI_LIST})
				qt4_add_resources(CRES${_v} ${RES_LIST})
				if (WAS_QM_ARG)
					set(${QM${_v}_VAR} "")
					qt4_add_translation(${QM${_v}_VAR} ${TS_LIST})
				endif()
			elseif (${_v} EQUAL 5)
				qt5_wrap_cpp(MOCS${_v} ${H_LIST} OPTIONS -nw ${MOC_INC_Qt5})
				qt5_wrap_ui(CUIS${_v} ${UI_LIST})
				qt5_add_resources(CRES${_v} ${RES_LIST})
				if (WAS_QM_ARG)
					set(${QM${_v}_VAR} "")
					qt5_add_translation(${QM${_v}_VAR} ${TS_LIST})
				endif()
			else()
				# Qt6 ...
			endif()
			#message("${${QM${_v}_VAR}}")
			list(APPEND ${CPP${_v}_VAR} ${CPP_LIST})
			list(APPEND ${CPP${_v}_VAR} ${RC_LIST})
			list(APPEND ${CPP${_v}_VAR} ${MOCS${_v}})
			list(APPEND ${CPP${_v}_VAR} ${CUIS${_v}})
			list(APPEND ${CPP${_v}_VAR} ${CRES${_v}})
			if (WAS_QM_ARG)
				list(APPEND ${CPP${_v}_VAR} ${${QM${_v}_VAR}})
			endif()
			#message("${${QM${_v}_VAR}}")
		endif()
	endforeach()
	set(CMAKE_CURRENT_BINARY_DIR ${SRC_CMAKE_CURRENT_BINARY_DIR})
endmacro()


macro(_qt_split_add_args _P _A)
	set(${_P})
	set(${_A})
	foreach(_i ${ARGN})
		if (NOT ("x${${_i}_Qt${_v}}" STREQUAL "x"))
			list(APPEND ${_A} ${${_i}_Qt${_v}})
		elseif (("x${_i}" STREQUAL "xWIN32") OR
				("x${_i}" STREQUAL "xMACOSX_BUNDLE") OR
				("x${_i}" STREQUAL "xSTATIC") OR
				("x${_i}" STREQUAL "xSHARED") OR
				("x${_i}" STREQUAL "xMODULE") OR
				("x${_i}" STREQUAL "xEXCLUDE_FROM_ALL"))
			list(APPEND ${_P} ${_i})
		elseif ("x${${_i}}" STREQUAL "x")
			list(APPEND ${_A} ${_i})
		else()
			list(APPEND ${_A} ${${_i}})
		endif()
	endforeach()
endmacro()


macro(qt_add_executable _NAME)
	foreach(_v ${_QT_VERSIONS_})
		if (LOCAL_FOUND${_v})
			_qt_split_add_args(_PREF _ARGS ${ARGN})
			if(APPLE)
				set(_PREV_CIND "${CMAKE_INSTALL_NAME_DIR}")
				set(CMAKE_INSTALL_NAME_DIR "@rpath")
			else()
				if(NOT WIN32)
					set(CMAKE_INSTALL_RPATH "\$ORIGIN/lib")
				endif()
			endif()
			set(_TARGET ${_NAME}${TARGET_SUFFIX_Qt${_v}})
			add_executable(${_TARGET} ${_PREF} ${_ARGS})
			set(_${_NAME}_is_qt 1)
			target_include_directories(${_TARGET} PRIVATE ${CMAKE_CURRENT_SOURCE_DIR} ${CMAKE_CURRENT_BINARY_DIR} ${CMAKE_CURRENT_BINARY_DIR}/qt${_v} ${Qt${_v}_INCLUDES})

			if(APPLE)
				set(CMAKE_INSTALL_NAME_DIR "${_PREV_CIND}")
			endif()
			# # Apple crosscompiling rpath patch
			# if (APPLE AND CMAKE_CROSSCOMPILING AND CMAKE_MACOSX_RPATH)
				# foreach(_RP ${CMAKE_INSTALL_RPATH})
					# add_custom_command(TARGET ${_TARGET} POST_BUILD
						# COMMAND "${CMAKE_INSTALL_NAME_TOOL}"
								# "-add_rpath" "${_RP}"
								# "$<TARGET_FILE_DIR:${_TARGET}>/$<TARGET_FILE_NAME:${_TARGET}>"
						# COMMENT "Add to ${_TARGET} rpath \"${_RP}\"")
				# endforeach()
			# endif()

			#list(APPEND _ALL_TARGETS "${_TARGET}")
			#set(_ALL_TARGETS ${_ALL_TARGETS} PARENT_SCOPE)
		endif()
	endforeach()
endmacro()


macro(qt_add_library _NAME)
	foreach(_v ${_QT_VERSIONS_})
		if (LOCAL_FOUND${_v})
			_qt_split_add_args(_PREF _ARGS ${ARGN})
			if(APPLE)
				set(_PREV_CIND "${CMAKE_INSTALL_NAME_DIR}")
				set(CMAKE_INSTALL_NAME_DIR "@rpath")
			else()
				if(NOT WIN32)
					set(CMAKE_INSTALL_RPATH "\$ORIGIN/lib")
				endif()
			endif()
			set(_TARGET ${_NAME}${TARGET_SUFFIX_Qt${_v}})
			add_library(${_TARGET} ${_PREF} ${_ARGS})
			set(_${_NAME}_is_qt 1)
			target_include_directories(${_TARGET} PRIVATE ${CMAKE_CURRENT_SOURCE_DIR} ${CMAKE_CURRENT_BINARY_DIR} ${CMAKE_CURRENT_BINARY_DIR}/qt${_v} ${Qt${_v}_INCLUDES})
			#message("add lib: \"${_TARGET}\"")

			if(APPLE)
				set(CMAKE_INSTALL_NAME_DIR "${_PREV_CIND}")
			endif()
			# # Apple crosscompiling rpath patch
			# if (APPLE AND CMAKE_CROSSCOMPILING AND CMAKE_MACOSX_RPATH)
				# foreach(_RP ${CMAKE_INSTALL_RPATH})
					# add_custom_command(TARGET ${_TARGET} POST_BUILD
						# COMMAND "${CMAKE_INSTALL_NAME_TOOL}"
								# "-add_rpath" "${_RP}"
								# "$<TARGET_FILE_DIR:${_TARGET}>/$<TARGET_FILE_NAME:${_TARGET}>"
						# COMMENT "Add to ${_TARGET} rpath \"${_RP}\"")
				# endforeach()
			# endif()

			list(APPEND _ALL_TARGETS "${_TARGET}")
			set(_ALL_TARGETS ${_ALL_TARGETS} PARENT_SCOPE)
		endif()
	endforeach()
endmacro()


macro(_qt_multitarget_suffix_ _OUT _IN _v)
	if (NOT "x${MULTILIB_${_IN}_SUFFIX_Qt${_v}}" STREQUAL "x")
		set(${_OUT} "${MULTILIB_${_IN}_SUFFIX_Qt${_v}}")
	else()
		set(${_OUT} "${MULTILIB_SUFFIX_Qt${_v}}")
	endif()
endmacro()


macro(qt_target_link_libraries _NAME)
	foreach(_v ${_QT_VERSIONS_})
		if (LOCAL_FOUND${_v})
			set(_TARGET ${_NAME}${TARGET_SUFFIX_Qt${_v}})
			set(_ARGS)
			foreach(_i ${ARGN})
				set(_ind -1)
				list(FIND QT_MULTILIB_LIST "${_i}" _ind)
				if (_ind GREATER -1)
					_qt_multitarget_suffix_(_TS ${_i} ${_v})
					list(APPEND _ARGS ${_i}${_TS})
				else()
					list(APPEND _ARGS ${_i})
				endif()
			endforeach()
			target_link_libraries(${_TARGET} ${Qt${_v}_LIBRARIES} ${_ARGS})
			#message("link ${_TARGET}: ${Qt${_v}_LIBRARIES} ${_ARGS}")
		endif()
	endforeach()
endmacro()


macro(qt_target_compile_definitions _NAME)
	foreach(_v ${_QT_VERSIONS_})
		if (LOCAL_FOUND${_v})
			set(_TARGET ${_NAME}${TARGET_SUFFIX_Qt${_v}})
			target_compile_definitions(${_TARGET} ${ARGN})
		endif()
	endforeach()
endmacro()


macro(qt_target_include_directories _NAME)
	foreach(_v ${_QT_VERSIONS_})
		if (LOCAL_FOUND${_v})
			set(_TARGET ${_NAME}${TARGET_SUFFIX_Qt${_v}})
			target_include_directories(${_TARGET} ${ARGN})
		endif()
	endforeach()
endmacro()


macro(qt_install)
	set(_prev_inst)
	#message("command: ${ARGN}")
	foreach(_v ${_QT_VERSIONS_})
		if (LOCAL_FOUND${_v})
			set(_INST_LIST)
			set(_IS_TARGET 0)
			set(_IS_FILES 0)
			set(_IS_DEST 0)
			set(_IS_LANG 0)
			set(_INVALID 0)
			foreach(_i ${ARGN})
				if ("x${_i}" STREQUAL "xQtLang")
					set(_IS_LANG 1)
				endif()
			endforeach()
			foreach(_i ${ARGN})
				if (_IS_TARGET)
					set(_IS_TARGET 0)
					if (_${_i}_is_qt)
						set(_INST_LIST ${_INST_LIST} ${_i}${TARGET_SUFFIX_Qt${_v}})
					else()
						set(_INST_LIST ${_INST_LIST} ${_i})
					endif()
				elseif (_IS_FILES)
					set(_IS_FILES 0)
					if (_IS_LANG)
						if (("x${${_i}_Qt${_v}}" STREQUAL "x") OR ("x${_i}" STREQUAL "xDESTINATION"))
							set(_INVALID 1)
						else()
							set(_INST_LIST ${_INST_LIST} ${${_i}_Qt${_v}})
						endif()
					else()
						set(_INST_LIST ${_INST_LIST} ${_i})
					endif()
				else()
					if (_IS_DEST)
						set(_IS_DEST 0)
						if     ("x${_i}" STREQUAL "xQtBin")
							set(_INST_LIST ${_INST_LIST} "${Qt${_v}_BIN}")
						elseif ("x${_i}" STREQUAL "xQtLang")
							set(_INST_LIST ${_INST_LIST} "${Qt${_v}_LANG_DIR}")
						else()
							set(_ind -1)
							string(FIND "${_i}" "QtPlugins" _ind)
							if (_ind GREATER -1)
								string(REPLACE "QtPlugins" "${Qt${_v}_PLUGINS_DIR}" _o "${_i}")
								set(_INST_LIST ${_INST_LIST} "${_o}")
							else()
								string(REPLACE "\\" "/" _o "${_i}")
								set(_INST_LIST ${_INST_LIST} ${_o})
							endif()
						endif()
					else()
						set(_ii ${_i})
						if     ("x${_i}" STREQUAL "xTARGETS")
							set(_IS_TARGET 1)
						elseif ("x${_i}" STREQUAL "xFILES")
							set(_IS_FILES 1)
						elseif ("x${_i}" STREQUAL "xDESTINATION")
							set(_IS_DEST 1)
						elseif ("x${_i}" STREQUAL "xLANG")
							set(_IS_FILES 1)
							set(_IS_LANG 1)
							set(_ii "FILES")
						endif()
						set(_INST_LIST ${_INST_LIST} ${_ii})
					endif()
				endif()
			endforeach()
			if (NOT _INVALID)
				if (NOT ("x${_prev_inst}" STREQUAL "x${_INST_LIST}"))
					#message("install: ${_INST_LIST}")
					install(${_INST_LIST})
					set(_prev_inst "${_INST_LIST}")
				endif()
			endif()
		endif()
	endforeach()
endmacro()


macro(qt_install_lang _NAME _dest_tag _DEST)
	set(_prev_inst)
	#message("command: ${ARGN}")
	foreach(_v ${_QT_VERSIONS_})
		if (LOCAL_FOUND${_v})
			set(_target)
			if (_${_NAME}_is_qt)
				set(_target ${_NAME}${TARGET_SUFFIX_Qt${_v}})
			endif()
			get_target_property(_LL ${_target} LINK_LIBRARIES)
			foreach (_L ${_LL})
				get_filename_component(_libname "${_L}" NAME)
				#message("depend on ${_libname}")
				if ("${_libname}" MATCHES "Qt${_v}::.*")
					string(SUBSTRING "${_libname}" 5 -1 _libname)
					string(TOLOWER "${_libname}" _libname)
					#message("qt lib \"${_libname}\"")
					list(APPEND _qt_libs qt${_libname})
				elseif ("${_libname}" MATCHES "qad_.*${_v}")
					string(LENGTH ${_libname} _sl)
					math(EXPR _sl ${_sl}-1)
					string(SUBSTRING ${_libname} 0 ${_sl} _libname)
					#message("qad lib \"${_libname}\"")
					list(APPEND _qt_libs ${_libname})
				endif()
			endforeach()
			if (NOT "x${_qt_libs}" STREQUAL "x")
				list(APPEND _qt_libs "qtbase")
				foreach (_i ${${PROJECT_NAME}_LANG})
					foreach (_l ${_qt_libs})
						set(_qm_path "${Qt${_v}_LANG_DIR}/${_l}_${_i}.qm")
						if (EXISTS "${_qm_path}")
							install(FILES ${_qm_path} DESTINATION "${_DEST}")
							#message("qm = \"${_qm_path}\"")
						endif()
					endforeach()
				endforeach()
			endif()
		endif()
	endforeach()
endmacro()
