Set BuildKit version using buildinfo
follow-up https://github.com/moby/moby/pull/44686#discussion_r1094416821
closes #44917

**- What I did**

This sets BuildKit version from the build information embedded in running binary so we are aligned with the expected vendoring.

**- How I did it**

We iterate over all dependencies and find the BuildKit one and set the right version. We also check if the module is replaced and use it in this case.

There is also additional checks if a pseudo version is detected (see comments in code for more info)

But there is a drawback...

As we are not supporting (yet) Go modules, dependencies are not embedded in the binary and therefore we can't retrieve the BuildKit module version. Last commit enables Go modules when building dockerd and docker-proxy so we have buildinfo with deps embedded in the binary. Once we switch to Go modules, we can remove this workaround.

**- How to verify it**

**- Description for the changelog**
<!--
Write a short (one line) summary that describes the changes in this
pull request for inclusion in the changelog:
-->


**- A picture of a cute animal (not mandatory but encouraged)**

hack/make/.binary
#!/usr/bin/env bash
set -e

# a helper to provide ".exe" when it's appropriate
binary_extension() {
	if [ "$(go env GOOS)" = 'windows' ]; then
		echo -n '.exe'
	fi
}

BINARY_EXTENSION="$(binary_extension)"
BINARY_FULLNAME="$BINARY_NAME$BINARY_EXTENSION"

source "${MAKEDIR}/.go-autogen"

(
	export GOGC=${DOCKER_BUILD_GOGC:-1000}

	if [ "$(go env GOOS)/$(go env GOARCH)" != "$(go env GOHOSTOS)/$(go env GOHOSTARCH)" ]; then
		# must be cross-compiling!
		if [ "$(go env GOOS)/$(go env GOARCH)" = "linux/arm" ]; then
			# specify name of the target ARM architecture
			case "$(go env GOARM)" in
				5)
					export CGO_CFLAGS="-march=armv5t"
					export CGO_CXXFLAGS="-march=armv5t"
					;;
				6)
					export CGO_CFLAGS="-march=armv6"
					export CGO_CXXFLAGS="-march=armv6"
					;;
				7)
					export CGO_CFLAGS="-march=armv7-a"
					export CGO_CXXFLAGS="-march=armv7-a"
					;;
			esac
		fi
	fi

	# -buildmode=pie is not supported on Windows arm64 and Linux mips*, ppc64be
	# https://github.com/golang/go/blob/go1.19.4/src/cmd/internal/sys/supported.go#L125-L132
	if ! [ "$DOCKER_STATIC" = "1" ]; then
		# -buildmode=pie not supported when -race is enabled
		if [[ " $BUILDFLAGS " != *" -race "* ]]; then
			case "$(go env GOOS)/$(go env GOARCH)" in
				windows/arm64 | linux/mips* | linux/ppc64) ;;
				*)
					BUILDFLAGS+=("-buildmode=pie")
					;;
			esac
		fi
	fi

	# only necessary for non-sandboxed invocation where TARGETPLATFORM is empty
	PLATFORM_NAME=$TARGETPLATFORM
	if [ -z "$PLATFORM_NAME" ]; then
		PLATFORM_NAME="$(go env GOOS)/$(go env GOARCH)"
		if [ -n "$(go env GOARM)" ]; then
			PLATFORM_NAME+="/v$(go env GOARM)"
		elif [ -n "$(go env GOAMD64)" ] && [ "$(go env GOAMD64)" != "v1" ]; then
			PLATFORM_NAME+="/$(go env GOAMD64)"
		fi
	fi

	echo "Building $([ "$DOCKER_STATIC" = "1" ] && echo "static" || echo "dynamic") $DEST/$BINARY_FULLNAME ($PLATFORM_NAME)..."
	if [ -n "$DOCKER_DEBUG" ]; then
		set -x
	fi
	go build -o "$DEST/$BINARY_FULLNAME" "${BUILDFLAGS[@]}" -ldflags "$LDFLAGS $LDFLAGS_STATIC $DOCKER_LDFLAGS" ${GO_PACKAGE}
)

echo "Created binary: $DEST/$BINARY_FULLNAME"

#!/usr/bin/env bash
set -e

# a helper to provide ".exe" when it's appropriate
binary_extension() {
	if [ "$(go env GOOS)" = 'windows' ]; then
		echo -n '.exe'
	fi
}

BINARY_EXTENSION="$(binary_extension)"
BINARY_FULLNAME="$BINARY_NAME$BINARY_EXTENSION"

source "${MAKEDIR}/.go-autogen"

(
	export GOGC=${DOCKER_BUILD_GOGC:-1000}

	if [ "$(go env GOOS)/$(go env GOARCH)" != "$(go env GOHOSTOS)/$(go env GOHOSTARCH)" ]; then
		# must be cross-compiling!
		if [ "$(go env GOOS)/$(go env GOARCH)" = "linux/arm" ]; then
			# specify name of the target ARM architecture
			case "$(go env GOARM)" in
				5)
					export CGO_CFLAGS="-march=armv5t"
					export CGO_CXXFLAGS="-march=armv5t"
					;;
				6)
					export CGO_CFLAGS="-march=armv6"
					export CGO_CXXFLAGS="-march=armv6"
					;;
				7)
					export CGO_CFLAGS="-march=armv7-a"
					export CGO_CXXFLAGS="-march=armv7-a"
					;;
			esac
		fi
	fi

	# -buildmode=pie is not supported on Windows arm64 and Linux mips*, ppc64be
	# https://github.com/golang/go/blob/go1.19.4/src/cmd/internal/sys/supported.go#L125-L132
	if ! [ "$DOCKER_STATIC" = "1" ]; then
		# -buildmode=pie not supported when -race is enabled
		if [[ " $BUILDFLAGS " != *" -race "* ]]; then
			case "$(go env GOOS)/$(go env GOARCH)" in
				windows/arm64 | linux/mips* | linux/ppc64) ;;
				*)
					BUILDFLAGS+=("-buildmode=pie")
					;;
			esac
		fi
	fi

	# only necessary for non-sandboxed invocation where TARGETPLATFORM is empty
	PLATFORM_NAME=$TARGETPLATFORM
	if [ -z "$PLATFORM_NAME" ]; then
		PLATFORM_NAME="$(go env GOOS)/$(go env GOARCH)"
		if [ -n "$(go env GOARM)" ]; then
			PLATFORM_NAME+="/v$(go env GOARM)"
		elif [ -n "$(go env GOAMD64)" ] && [ "$(go env GOAMD64)" != "v1" ]; then
			PLATFORM_NAME+="/$(go env GOAMD64)"
		fi
	fi

	# This is a workaround to have buildinfo with deps embedded in the binary. We
	# need to create a go.mod file before building with -modfile=vendor.mod,
	# otherwise it fails with: "-modfile cannot be used to set the module root directory."
	if [ ! -f "go.mod" ]; then
		printf '%s\n\n%s' 'module github.com/docker/docker' 'go 1.19' > "go.mod"
		trap 'rm -f go.mod' EXIT
	fi

	echo "Building $([ "$DOCKER_STATIC" = "1" ] && echo "static" || echo "dynamic") $DEST/$BINARY_FULLNAME ($PLATFORM_NAME)..."
	if [ -n "$DOCKER_DEBUG" ]; then
		set -x
	fi
	GO111MODULE=on go build -mod=vendor -modfile=vendor.mod -o "$DEST/$BINARY_FULLNAME" "${BUILDFLAGS[@]}" -ldflags "$LDFLAGS $LDFLAGS_STATIC $DOCKER_LDFLAGS" ${GO_PACKAGE}
)

echo "Created binary: $DEST/$BINARY_FULLNAME"

Log output was a bit clunky using `with-go-mod` and also the dependency on `git` was an issue as we ignore `.git` in `.dockerignore`: https://github.com/moby/moby/pull/44917/files#diff-fa876a8e859e8583907d556eeed2e4b8cc98d7847b2124521aaef1e9984f50cf