function(detect_libgcc_version outputVersionVar outputDirVar libgccLibDir flavour)
	file(GLOB FILEZ LIST_DIRECTORIES ON "${libgccLibDir}/*")
	set(latestLibgccVersion "")
	set(latestLibgccVersionDir "")
	foreach(file ${FILEZ})
		if(IS_DIRECTORY "${file}")
			get_filename_component(dirName "${file}" NAME)
			string(REGEX MATCH "^(([0-9]+)(.[0-9]+)*)(-${flavour})?$" MATCHED "${dirName}")
			if(MATCHED)
				set(libgccVersionCandidate "${CMAKE_MATCH_1}")
				message(STATUS "Found MinGW libGCC version candidate for ${flavour}: ${libgccVersionCandidate}")
				if(libgccVersionCandidate VERSION_GREATER latestLibgccVersion)
					set(latestLibgccVersion "${libgccVersionCandidate}")
					set(latestLibgccVersionDir "${dirName}")
					#message(STATUS "updated")
				endif()
			endif()
		endif()
	endforeach()
	if(latestLibgccVersion)
		set(latestLibgccVersionDir "${libgccLibDir}/${latestLibgccVersionDir}")
		set("${outputDirVar}" "${latestLibgccVersionDir}" PARENT_SCOPE)
		set("${outputVersionVar}" "${latestLibgccVersion}" PARENT_SCOPE)
		message(STATUS "Detected MinGW libGCC version for ${flavour}: ${latestLibgccVersion}")
		message(STATUS "In directory: ${latestLibgccVersionDir}")
	endif()
endfunction()
