MACRO( THIRDPARTY_DIR thirdpartydir)
	set( thirdparty_dir ${thirdpartydir} )
	file(GLOB thirdparty ${thirdpartydir}/* )
	set( CMAKE_PREFIX_PATH ${CMAKE_PREFIX_PATH} ${thirdparty} )
	file(GLOB thirdparty_lib ${thirdpartydir}/*/lib )
	file(GLOB thirdparty_bin ${thirdpartydir}/*/bin )
	LINK_DIRECTORIES( ${thirdparty_bin} )
	
	IF(EXISTS ${thirdpartydir}/extra.txt)
		FILE(READ ${thirdpartydir}/extra.txt extra)
		STRING( REPLACE "\n" "" extra "${extra}" )
		STRING( REPLACE "\r" "" extra "${extra}" )
		STRING( REPLACE "\\" "/" extra "${extra}" )
		FOREACH( each ${extra} )
			set( CMAKE_PREFIX_PATH ${CMAKE_PREFIX_PATH} ${each} )
			LINK_DIRECTORIES( ${each}/bin )
			LIST( APPEND thirdparty_lib ${each}/lib )
			LIST( APPEND thirdparty_lib ${each}/bin )
		ENDFOREACH()
	ENDIF()
ENDMACRO()

FOREACH( each ${CMAKE_MODULE_PATH} )
	FILE( GLOB each_dependencies.py ${each}/dependencies.py )
	LIST(APPEND dependencies.py ${each_dependencies.py} )
ENDFOREACH()
LIST( GET dependencies.py 0 dependencies.py )

MACRO( ESCAPE_ARGS_LIST outvar )
	SET( ${outvar} "" )
	FOREACH( each ${ARGN} )
		STRING( REPLACE " " "\\ " each_escaped ${each} )
		LIST( APPEND ${outvar} ${each_escaped} )
	ENDFOREACH()
ENDMACRO()

MACRO( INSTALL_WITH_DEPS )
	cmake_policy(PUSH)
	cmake_policy(SET CMP0026 OLD)

	SET( CPACK_SET_DESTDIR TRUE )
	FIND_PACKAGE(PythonInterp REQUIRED interpreter)

	SET( COMPONENTS "RUNTIME;LIBRARY;ARCHIVE;TARGETS" )
	SET( OPTIONS "DESTINATION" )
	SET( last_COMPONENT "" )
	SET( last_OPTION "" )
	FOREACH( each ${ARGN} )
		LIST(FIND COMPONENTS ${each} list_find)
		LIST(FIND OPTIONS ${each} list_find2)
		IF(${list_find} GREATER -1 )
			SET( last_COMPONENT ${each} )
			SET( last_OPTION "" )
		ELSEIF( ${list_find2} GREATER -1 )
			SET( last_OPTION ${each} )
		ELSE()
			LIST(APPEND ${last_COMPONENT}_${last_OPTION} ${each} )
		ENDIF()
	ENDFOREACH()

	INSTALL( ${ARGN} )

	FOREACH( each_TARGET ${TARGETS_} )
		GET_TARGET_PROPERTY(each_target_location ${each_TARGET} LOCATION)
		ESCAPE_ARGS_LIST( arg_list1 ${PYTHON_EXECUTABLE} ${dependencies.py} )
		ESCAPE_ARGS_LIST( arg_list2 ${thirdparty_dir} ${thirdparty_bin} ${thirdparty_lib} ${each_target_location} )
		INSTALL( CODE "EXECUTE_PROCESS(COMMAND ${arg_list1} \${CMAKE_INSTALL_PREFIX}/${RUNTIME_DESTINATION} \${CMAKE_INSTALL_PREFIX}/${LIBRARY_DESTINATION} ${arg_list2} OUTPUT_VARIABLE output_${each_TARGET})")
		IF( ${WIN32} )
			INSTALL( CODE "FILE(INSTALL \${output_${each_TARGET}} DESTINATION \${CMAKE_INSTALL_PREFIX}/${RUNTIME_DESTINATION})" )
		ELSE()
			INSTALL( CODE "FILE(INSTALL \${output_${each_TARGET}} DESTINATION \${CMAKE_INSTALL_PREFIX}/${LIBRARY_DESTINATION})"  )
		ENDIF()
	ENDFOREACH()

	cmake_policy(POP)
ENDMACRO()
