function(get_version_from_git)
	find_package(Git REQUIRED)

	if(NOT GIT_TAG)
		execute_process(
			COMMAND ${GIT_EXECUTABLE} describe --tags --always
			WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
			OUTPUT_VARIABLE GIT_TAG
			OUTPUT_STRIP_TRAILING_WHITESPACE
			RESULT_VARIABLE GIT_RESULT
		)
		if(NOT GIT_RESULT EQUAL 0)
			message(FATAL_ERROR "Failed to get git tag")
		endif()
	endif()

	string(REGEX REPLACE "^v" "" CLEAN_TAG "${GIT_TAG}")
	if(CLEAN_TAG MATCHES "^([0-9]+)\\.([0-9]+)\\.([0-9]+)(-.*)?$")

		set(CMAKE_PROJECT_VERSION_MAJOR ${CMAKE_MATCH_1})
		set(CMAKE_PROJECT_VERSION_MAJOR ${CMAKE_MATCH_1} PARENT_SCOPE)
		set(CMAKE_PROJECT_VERSION_MINOR ${CMAKE_MATCH_2})
		set(CMAKE_PROJECT_VERSION_MINOR ${CMAKE_MATCH_2} PARENT_SCOPE)
		set(CMAKE_PROJECT_VERSION_PATCH ${CMAKE_MATCH_3})
		set(CMAKE_PROJECT_VERSION_PATCH ${CMAKE_MATCH_3} PARENT_SCOPE)

		set(PROJECT_VERSION "${CMAKE_MATCH_1}.${CMAKE_MATCH_2}.${CMAKE_MATCH_3}")
		set(PROJECT_VERSION "${CMAKE_MATCH_1}.${CMAKE_MATCH_2}.${CMAKE_MATCH_3}" PARENT_SCOPE)
	else()
		message(WARNING "Tag '${CLEAN_TAG}' does not match semver format")
		set(CMAKE_PROJECT_VERSION_MAJOR 0)
		set(CMAKE_PROJECT_VERSION_MAJOR 0 PARENT_SCOPE)
		set(CMAKE_PROJECT_VERSION_MINOR 0)
		set(CMAKE_PROJECT_VERSION_MINOR 0 PARENT_SCOPE)
		set(CMAKE_PROJECT_VERSION_PATCH 0)
		set(CMAKE_PROJECT_VERSION_PATCH 0 PARENT_SCOPE)
	endif()
endfunction()
