#[[

qad_find_qt([<Module1> <Module2> ...] [Qt4] [Qt5] [Qt6])

Find Qt4 or/and Qt5, Qt6 packages
If Qt4/5/6 found, set
* Qt<4/5/6>_ROOT        var to Qt root dir
* Qt<4/5/6>_BIN         var to Qt binary dir
* Qt<4/5/6>_PLUGINS_DIR var to Qt plugins dir
* Qt<4/5/6>_LANG_DIR    var to Qt translations dir
Also set variable SOME_QT_FOUND




qad_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




qad_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, Qt5 files to <cpp_var>_Qt5 and Qt6 files to <cpp_var>_Qt6
version are automatic detected after qad_find_qt() call




qad_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 qad_wrap() or some list or filename




qad_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 qad_wrap() or some list or filename




qad_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?>




qad_target_compile_definitions(<name> ...)

Syntax similar to original "target_compile_definitions()"
Specify compile definitions to use when compiling a given <name>




qad_target_include_directories(<name> ...)

Syntax similar to original "target_include_directories()"
Add include directories to a target




qad_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. "qad_install(LANG my_QM DESTINATION QtLang)"




qad_install_lang(<name> DESTINATION <dir> [PATHS <path0> [<path1>] [...] ])

Search Qt translations for all <name> dependencies
in Qt<?>_LANG_DIR for <name>_LANG languages
and install them to <dir>. PATH are additional search paths.
You should use "set_lang()" macro before this action




qad_get_target(<name> <var> [Qt4 | Qt5 | Qt6])

Write real <name> Qt target name to <var>
If Qt4 or Qt5, Qt6 specified, search for this version,
otherwise returns greatest version




qad_get_targets(<name> <var>)

Write all real <name> Qt target names to <var>




qad_generate_export_header(<name> ...)

Call generate_export_header on Qt multitarget


]]


cmake_policy(SET CMP0011 NEW) # don`t affect includer policies
cmake_policy(SET CMP0020 NEW) # Automatically link Qt executables to qtmain target on Windows
if (POLICY CMP0057)
	cmake_policy(SET CMP0057 NEW) # Support if() IN_LIST
endif()
if (POLICY CMP0074)
	cmake_policy(SET CMP0074 NEW) # ignore <PackageName>_ROOT variables
endif()
set(__qt_bug_modules "UiTools")

set(__qt_modules_4 "CLucene;Core;Declarative;Gui;Help;main;Multimedia;Network;OpenGL;\
Script;ScriptTools;Sql;Svg;Test;UiTools;UiToolsd;WebKit;Xml;XmlPatterns")

set(__qt_modules_5 "3DAnimation;3DCore;3DExtras;3DInput;3DLogic;3DQuick;3DQuickAnimation;\
3DQuickExtras;3DQuickInput;3DQuickRender;3DQuickScene2D;3DRender;AccessibilitySupport;\
AttributionsScannerTools;AxBase;AxContainer;AxServer;Bluetooth;Bootstrap;Concurrent;\
Core;DataVisualization;DBus;Designer;DesignerComponents;DeviceDiscoverySupport;DocTools;EdidSupport;\
EglSupport;EventDispatcherSupport;FbSupport;FontDatabaseSupport;Gamepad;Gui;Help;\
LinguistTools;Location;Multimedia;MultimediaQuick;MultimediaWidgets;Network;NetworkAuth;\
Nfc;OpenGL;OpenGLExtensions;PacketProtocol;PlatformCompositorSupport;Positioning;\
PositioningQuick;PrintSupport;Qml;QmlDebug;QmlDevTools;QmlImportScanner;QmlModels;\
QmlWorkerScript;Quick;QuickCompiler;QuickControls2;QuickParticles;QuickShapes;QuickTemplates2;\
QuickTest;QuickWidgets;RemoteObjects;RepParser;Scxml;Sensors;SerialBus;SerialPort;\
Sql;Svg;Test;TextToSpeech;ThemeSupport;UiPlugin;UiTools;VulkanSupport;WebChannel;WebSockets;\
Widgets;WindowsUIAutomationSupport;WinExtras;Xml;XmlPatterns;Zlib;Charts;AndroidExtras")

set(__qt_modules_6 "3DAnimation;3DCore;3DExtras;3DInput;3DLogic;3DQuick;3DQuickAnimation;3DQuickExtras;\
3DQuickInput;3DQuickRender;3DQuickScene2D;3DRender;ActiveQt;AxBasePrivate;AxContainer;\
AxContainerTools;AxServer;AxServerTools;Bluetooth;BodymovinPrivate;BuildInternals;\
Bundled_Clip2Tri;BundledFreetype;BundledLibpng;BundledOpenwnn;BundledPinyin;BundledTcime;\
Charts;ChartsQml;Coap;Concurrent;Core;Core5Compat;CoreTools;DataVisualization;DBus;\
DBusTools;Designer;DesignerComponentsPrivate;DeviceDiscoverySupportPrivate;EntryPointPrivate;\
FbSupportPrivate;Gui;GuiTools;Help;HostInfo;LabsAnimation;LabsFolderListModel;LabsQmlModels;\
LabsSettings;LabsSharedImage;LabsWavefrontMesh;Linguist;LinguistTools;Mqtt;\
Multimedia;MultimediaQuickPrivate;MultimediaWidgets;Network;NetworkAuth;Nfc;OpcUa;\
OpenGL;OpenGLWidgets;PacketProtocolPrivate;Positioning;PositioningQuick;PrintSupport;\
Qml;QmlCompilerPrivate;QmlCore;QmlDebugPrivate;QmlDevToolsPrivate;QmlDomPrivate;QmlImportScanner;\
QmlLocalStorage;QmlModels;QmlTools;QmlWorkerScript;QmlXmlListModel;Quick;Quick3D;\
Quick3DAssetImport;Quick3DAssetUtils;Quick3DEffects;Quick3DHelpers;Quick3DIblBaker;Quick3DParticles;\
Quick3DRuntimeRender;Quick3DTools;Quick3DUtils;QuickControls2;QuickControls2Impl;\
QuickControlsTestUtilsPrivate;QuickDialogs2;QuickDialogs2QuickImpl;QuickDialogs2Utils;QuickLayouts;\
QuickParticlesPrivate;QuickShapesPrivate;QuickTemplates2;QuickTest;QuickTestUtilsPrivate;\
QuickTimeline;QuickWidgets;RemoteObjects;RemoteObjectsQml;RemoteObjectsTools;RepParser;\
Scxml;ScxmlQml;ScxmlTools;Sensors;SensorsQuick;SerialBus;SerialBusTools;SerialPort;\
ShaderTools;ShaderToolsTools;Sql;StateMachine;StateMachineQml;Svg;SvgWidgets;Test;Tools;\
ToolsTools;UiPlugin;UiTools;VirtualKeyboard;WebChannel;WebSockets;WebView;WebViewQuick;\
Widgets;WidgetsTools;Xml;AndroidExtras")


set(_QT_VERSIONS_ 4 5 6)
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(qad_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()
	if (NOT DEFINED SOME_QT_FOUND)
		set(SOME_QT_FOUND 0 CACHE BOOL "")
	endif()
	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)
			if (NOT "x${_i}" STREQUAL "xQUIET")
				list(APPEND _QCOMP "${_i}")
			endif()
		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})
				if (m IN_LIST __qt_modules_4)
					string(TOUPPER "${m}" _um)
					#message("${QT_QT${_um}_FOUND}")
					if (${QT_QT${_um}_FOUND})
						list(APPEND Qt4_LIBRARIES ${QT_QT${_um}_LIBRARY})
					endif()
				endif()
			endforeach()
			set(Qt4_PLUGINS_DIR ${QT_PLUGINS_DIR} CACHE STRING "")
			set(Qt4_LANG_DIR ${Qt4_BIN}/../translations CACHE STRING "")
			set(Qt4_ROOT "${Qt4_BIN}/../" CACHE STRING "")
		endif()
	endif()
	if (_NEED5)
		#set(_MSG 1)
		#if (Qt5_FOUND)
		#	set(_MSG 0)
		#endif()
		set(_QCOMP5 Core LinguistTools UiPlugin Widgets ${_QCOMP})
		list(REMOVE_DUPLICATES _QCOMP5)
		find_package(Qt5 COMPONENTS ${_QCOMP5} QUIET)
		if (DEFINED ANDROID_PLATFORM)
			set(CMAKE_SHARED_MODULE_SUFFIX_CXX ".so")
			set(CMAKE_SHARED_LIBRARY_SUFFIX_CXX ".so")
			set(CMAKE_SHARED_MODULE_SUFFIX_C ".so")
			set(CMAKE_SHARED_LIBRARY_SUFFIX_C ".so")
		endif()
		if (Qt5Core_FOUND)
			set(LOCAL_FOUND5 1)
			set(_Qt5Modules)
			foreach(m ${_QCOMP5})
				if (m IN_LIST __qt_modules_5)
					if (${Qt5${m}_FOUND})
						set(__lib ${Qt5${m}_LIBRARIES})
						#if (DEFINED ANDROID_PLATFORM)
							if (m IN_LIST __qt_bug_modules)
								get_target_property(__lib Qt5::${m} LOCATION)
							endif()
						#endif()
						list(APPEND _Qt5Modules ${m})
						list(APPEND Qt5_INCLUDES ${Qt5${m}_INCLUDE_DIRS})
						list(APPEND Qt5_LIBRARIES ${__lib})
					else()
						if (NOT "x${_QUIET}" STREQUAL "xQUIET")
							message(FATAL_ERROR "Qt5${m} NOT FOUND")
						endif()
					endif()
				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)
				#message("Qt5: plugins dir: ${Qt5_PLUGINS_DIR}")
				break()
			endforeach()
			string(FIND "${_Qt5_BIN}" "/" _ind REVERSE)
			string(SUBSTRING "${_Qt5_BIN}" 0 ${_ind} _Qt5_ROOT)
			set(Qt5_ROOT "${_Qt5_ROOT}" CACHE STRING "")
			set(Qt5_BIN  "${_Qt5_BIN}"  CACHE STRING "")
			set(Qt5_LANG_DIR ${Qt5_BIN}/../translations CACHE STRING "")
			if ("x${Qt5_PLUGINS_DIR}" STREQUAL "x")
				set(Qt5_PLUGINS_DIR "${Qt5_ROOT}/plugins" CACHE STRING "")
			endif()
			if (APPLE)
				list(APPEND MOC_INC_Qt5 "-F${Qt5_ROOT}/lib")
			endif()
			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()
	if (_NEED6)
		#set(_MSG 1)
		#if (Qt6_FOUND)
		#	set(_MSG 0)
		#endif()
		set(_QCOMP6 Core LinguistTools UiPlugin Widgets ${_QCOMP})
		list(REMOVE_DUPLICATES _QCOMP6)
		find_package(Qt6 COMPONENTS ${_QCOMP6} QUIET)
		if (DEFINED ANDROID_PLATFORM)
			set(CMAKE_SHARED_MODULE_SUFFIX_CXX ".so")
			set(CMAKE_SHARED_LIBRARY_SUFFIX_CXX ".so")
			set(CMAKE_SHARED_MODULE_SUFFIX_C ".so")
			set(CMAKE_SHARED_LIBRARY_SUFFIX_C ".so")
		endif()
		if (Qt6Core_FOUND)
			set(LOCAL_FOUND6 1)
			set(_Qt6Modules)
			foreach(m ${_QCOMP6})
				if (m IN_LIST __qt_modules_6)
					if (${Qt6${m}_FOUND})
						set(__lib ${Qt6${m}_LIBRARIES})
						if (DEFINED ANDROID_PLATFORM)
							if (m IN_LIST __qt_bug_modules)
								get_target_property(__lib Qt6::${m} LOCATION)
							endif()
						endif()
						list(APPEND _Qt6Modules ${m})
						list(APPEND Qt6_INCLUDES ${Qt6${m}_INCLUDE_DIRS})
						list(APPEND Qt6_LIBRARIES ${__lib})
					else()
						if (NOT "x${_QUIET}" STREQUAL "xQUIET")
							message(FATAL_ERROR "Qt6${m} NOT FOUND")
						endif()
					endif()
				endif()
			endforeach()
			#get_property(_up_dir TARGET Qt6::UiPlugin PROPERTY INTERFACE_INCLUDE_DIRECTORIES)
			#message("${_up_dir}")
			#if (NOT "x${_up_dir}" STREQUAL "x")
			#	list(APPEND Qt6_INCLUDES ${_up_dir})
			#endif()
			get_target_property(_Qt6_qmake Qt6::qmake LOCATION)
			get_filename_component(_Qt6_BIN ${_Qt6_qmake} PATH)
			foreach(_p ${Qt6Gui_PLUGINS})
				get_target_property(_l ${_p} LOCATION)
				get_filename_component(_l ${_l} DIRECTORY)
				string(FIND "${_l}" "/" _ind REVERSE)
				string(SUBSTRING "${_l}" 0 ${_ind} _l)
				#message("Qt6: plugins dir: ${Qt6_PLUGINS_DIR}")
				break()
			endforeach()
			string(FIND "${_Qt6_BIN}" "/" _ind REVERSE)
			string(SUBSTRING "${_Qt6_BIN}" 0 ${_ind} _Qt6_ROOT)
			set(Qt6_ROOT "${_Qt6_ROOT}" CACHE STRING "")
			set(Qt6_BIN  "${_Qt6_BIN}"  CACHE STRING "")
			set(Qt6_LANG_DIR ${Qt6_BIN}/../translations CACHE STRING "")
			if ("x${Qt6_PLUGINS_DIR}" STREQUAL "x")
				set(Qt6_PLUGINS_DIR "${Qt6_ROOT}/plugins" CACHE STRING "")
			endif()
			if (APPLE)
				list(APPEND MOC_INC_Qt6 "-F${Qt6_ROOT}/lib")
			endif()
			if (NOT _QT6_MSG)
				message(STATUS "Found Qt6: ${_Qt6_qmake} (version \"${Qt6_VERSION}\")")
				set(_QT6_MSG 1 CACHE BOOL "msg_qt6" FORCE)
				message(STATUS "Found Qt6 modules: ${_Qt6Modules}")
			endif()
		endif()
	endif()
	foreach(_v ${_QT_VERSIONS_})
		if (LOCAL_FOUND${_v})
			set(SOME_QT_FOUND 1 CACHE BOOL "" FORCE)
			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(qad_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(qad_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()
			elseif (${_v} EQUAL 6)
				qt6_wrap_cpp(MOCS${_v} ${H_LIST} OPTIONS -nw ${MOC_INC_Qt6})
				qt6_wrap_ui(CUIS${_v} ${UI_LIST})
				qt6_add_resources(CRES${_v} ${RES_LIST})
				if (WAS_QM_ARG)
					set(${QM${_v}_VAR} "")
					qt6_add_translation(${QM${_v}_VAR} ${TS_LIST})
				endif()
			else()
				# Qt7 ...
			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_set_defines _NAME _T)
	target_compile_definitions(${_T} PRIVATE "__TARGET_NAME__=\"${_NAME}\"")
	target_compile_definitions(${_T} PRIVATE "__TARGET_LABEL__=\"${${_NAME}_LABEL}\"")
	target_compile_definitions(${_T} PRIVATE "__TARGET_COMPANY__=\"${${_NAME}_COMPANY}\"")
	target_compile_definitions(${_T} PRIVATE "__TARGET_VERSION__=\"${${_NAME}_VERSION}\"")
endmacro()


macro(qad_add_executable _NAME)
	#message("*qad_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;\$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})
			__qt_set_defines(${_NAME} ${_TARGET})
			#message("add exe: \"${_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(qad_add_library _NAME)
	#message("*qad_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;\$ORIGIN/lib")
				endif()
			endif()
			set(_TARGET ${_NAME}${TARGET_SUFFIX_Qt${_v}})
			add_library(${_TARGET} ${_PREF} ${_ARGS})
			#message("add lib: \"${_TARGET} ${_PREF}\"")
			set(_${_NAME}_is_qt 1)
			target_include_directories(${_TARGET} PRIVATE ${CMAKE_CURRENT_BINARY_DIR}/qt${_v})
			target_include_directories(${_TARGET} PUBLIC ${CMAKE_CURRENT_BINARY_DIR} ${CMAKE_CURRENT_SOURCE_DIR} ${Qt${_v}_INCLUDES})
			__qt_set_defines(${_NAME} ${_TARGET})
			#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)
	string(REPLACE ":" "_" _in_str "${_IN}")
	if (NOT "x${MULTILIB_${_in_str}_SUFFIX_Qt${_v}}" STREQUAL "x")
		set(${_OUT} "${MULTILIB_${_in_str}_SUFFIX_Qt${_v}}")
	else()
		set(${_OUT} "${MULTILIB_SUFFIX_Qt${_v}}")
	endif()
endmacro()


macro(qad_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})
					#message(STATUS "${_i} in MULTILIB_LIST, suffix = ${_TS}")
					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(qad_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(qad_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(qad_install)
	set(_prev_inst)
	set(__add_args)
	#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)
					set(__target ${_i})
					if (_${_i}_is_qt)
						set(__target ${_i}${TARGET_SUFFIX_Qt${_v}})
					endif()
					if (DEFINED ANDROID_PLATFORM)
						get_target_property(_TT ${__target} TYPE)
						if ("x${_TT}" STREQUAL "xEXECUTABLE")
							list(APPEND __add_args EXCLUDE_FROM_ALL)
						endif()
					endif()
					list(APPEND _INST_LIST ${__target})
				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()
							list(APPEND _INST_LIST ${${_i}_Qt${_v}})
						endif()
					else()
						list(APPEND _INST_LIST ${_i})
					endif()
				else()
					if (_IS_DEST)
						set(_IS_DEST 0)
						if     ("x${_i}" STREQUAL "xQtBin")
							list(APPEND _INST_LIST "${Qt${_v}_BIN}")
						elseif ("x${_i}" STREQUAL "xQtLang")
							list(APPEND _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}")
								list(APPEND _INST_LIST "${_o}")
							else()
								string(REPLACE "\\" "/" _o "${_i}")
								list(APPEND _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()
						list(APPEND _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} ${__add_args})
					set(_prev_inst "${_INST_LIST}")
				endif()
			endif()
		endif()
	endforeach()
endmacro()


macro(_qt_gather_deps _T _out __p)
	get_target_property(_II ${_T} IMPORTED)
	if (_II)
		get_target_property(_TLL${__p} ${_T} INTERFACE_LINK_LIBRARIES)
	else()
		get_target_property(_TLL${__p} ${_T} LINK_LIBRARIES)
	endif()
	#message("_qt_gather_deps ${_T} \"${__p}\" = ${_TLL${__p}}")# ${${_out}}")
	foreach (_TL${__p} ${_TLL${__p}})
		set(_lib ${_TL${__p}})
		if (TARGET ${_lib})
			get_target_property(_AT ${_lib} ALIASED_TARGET)
			if (_AT)
				set(_lib ${_AT})
			endif()
			#message("${_T} links with ${_lib}")
			if (NOT _lib IN_LIST ${_out})
				#message("look for ${_lib}")
				list(APPEND ${_out} ${_lib})
				string(MAKE_C_IDENTIFIER "${_lib}" _p)
				_qt_gather_deps(${_lib} ${_out} "${_p}")
			endif()
		else()
			#message("${_T} reject ${_lib}")
		endif()
	endforeach()
endmacro()


macro(qad_install_lang _NAME)
	set(_prev_inst)
	#message("command: ${ARGN}")
	set(_PATHS)
	set(_DEST)
	if(CMAKE_CROSSCOMPILING)
		list(APPEND _PATHS "${CMAKE_PREFIX_PATH}/lang")
	endif()
	set(_cur_arg)
	set(__args "DESTINATION;PATHS")
	foreach(_i ${ARGN})
		if (_i IN_LIST __args)
			set(_cur_arg "${_i}")
		elseif("${_cur_arg}" STREQUAL "DESTINATION")
			set(_DEST "${_i}")
		elseif("${_cur_arg}" STREQUAL "PATHS")
			list(APPEND _PATHS "${_i}")
		endif()
	endforeach()
	foreach(_v ${_QT_VERSIONS_})
		if (LOCAL_FOUND${_v})
			set(_target)
			if (_${_NAME}_is_qt)
				set(_target ${_NAME}${TARGET_SUFFIX_Qt${_v}})
			endif()
			set(_LL)
			_qt_gather_deps(${_target} _LL "")
			#message("${_target} libs = ${_LL}")
			foreach (_L ${_LL})
				get_filename_component(_libname "${_L}" NAME_WE)
				#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})
				else()
					if ("${_libname}" MATCHES "lib.*")
						string(LENGTH ${_libname} _sl)
						math(EXPR _sl ${_sl}-3)
						string(SUBSTRING ${_libname} 3 ${_sl} _libname)
					endif()
					if ("${_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()
					if ("${_libname}" MATCHES ".*::.*")
						string(LENGTH "${_libname}" _sl)
						math(EXPR _sl ${_sl}-1)
						string(SUBSTRING "${_libname}" 0 ${_sl} _libname)
						string(REPLACE "::" "_" _libname "${_libname}")
						string(TOLOWER "${_libname}" _libname )
						#message("imp lib \"${_libname}\"")
						list(APPEND _qt_libs ${_libname})
					endif()
				endif()
			endforeach()
			if (NOT "x${_qt_libs}" STREQUAL "x")
				list(APPEND _qt_libs "qtbase")
				foreach (_i ${${PROJECT_NAME}_LANG})
					foreach (_l ${_qt_libs})
						unset(_qm_path CACHE)
						find_file(_qm_path "${_l}_${_i}.qm" PATHS "${Qt${_v}_LANG_DIR}" ${_PATHS} NO_DEFAULT_PATH)
						if (EXISTS "${_qm_path}")
							install(FILES ${_qm_path} DESTINATION "${_DEST}")
							#message("qm = \"${_qm_path}\"")
						endif()
					endforeach()
				endforeach()
			endif()
		endif()
	endforeach()
endmacro()


macro(qad_get_target _NAME _OUT)
	set(${_OUT})
	set(_ver)
	foreach(_i ${ARGN})
		foreach(_v ${_QT_VERSIONS_})
			if ("x${_i}" STREQUAL "xQt${_v}")
				set(_ver ${_v})
				break()
			endif()
		endforeach()
		if (NOT "x${_ver}" STREQUAL "x")
			break()
		endif()
	endforeach()
	foreach(_v ${_QT_VERSIONS_})
		if (LOCAL_FOUND${_v} AND (("x${_ver}" STREQUAL "x") OR ((NOT "x${_ver}" STREQUAL "x") AND ("x${_ver}" STREQUAL "x${_v}"))))
			set(${_OUT} ${_NAME}${TARGET_SUFFIX_Qt${_v}})
		endif()
	endforeach()
endmacro()


macro(qad_get_targets _NAME _OUT)
	set(${_OUT})
	foreach(_v ${_QT_VERSIONS_})
		if (LOCAL_FOUND${_v})
			list(APPEND ${_OUT} ${_NAME}${TARGET_SUFFIX_Qt${_v}})
		endif()
	endforeach()
endmacro()


macro(qad_generate_export_header _NAME)
	qad_get_target(${_NAME} _some_target)
	qad_get_target(${_NAME} _targets)
	foreach(_t ${_targets})
		set_target_properties(${_t} PROPERTIES DEFINE_SYMBOL ${_NAME}_EXPORTS)
	endforeach()
	set_target_properties(${_some_target} PROPERTIES DEFINE_SYMBOL ${_NAME}_EXPORTS)
	generate_export_header(${_some_target} BASE_NAME "${_NAME}" ${ARGN})
endmacro()
