From f03bd33c4bd8a0e69f2bf3da95ce8144015a9b3d Mon Sep 17 00:00:00 2001 From: Sebastiaan van Stijn Date: Sun, 15 Aug 2021 12:03:41 +0200 Subject: [PATCH] Use go1.16 "embed" to set version from VERSION file This allows building runc without passing the VERSION build-arg, but requires go 1.16 or up. Unfortunately, there's no easy way to get the git-commit from the filesystem (possibly through `.git/logs/HEAD`, which is a large file); a proposal has been accepted to add git information ([1]), which will be included in go1.18. For users building through `go install github.com/opencontainers/runc@version`), we may be able to use runtime/debug.BuildInfo() ([2]). BuildInfo provides the module version and checksum, but only when building using `go install`. When building from a local module (git clone), the version is alway set to `(devel)`, see [3]. We could consider add module (optional) if available, e.g.: // Print module information if available. When built from local source, // (using git clone), module version is not available, and version is // set to "(devel)"; see golang/go#29814, and golang/go#37475. if bi, ok := debug.ReadBuildInfo(); ok && bi.Main.Version != "(devel)" { v = append(v, "module version: "+bi.Main.Version+", sum: "+bi.Main.Sum) } With this patch (on go1.16): make go build -trimpath "-buildmode=pie" -tags "seccomp" -ldflags "-X main.gitCommit=v1.0.0-133-g27d75cca " -o runc . ./runc --version runc version 1.0.0+dev commit: v1.0.0-133-g27d75cca spec: 1.0.2-dev go: go1.16.7 libseccomp: 2.3.3 [1]: https://github.com/golang/go/issues/37475 [2]: https://pkg.go.dev/runtime/debug#BuildInfo [3]: https://github.com/golang/go/issues/29814 Signed-off-by: Sebastiaan van Stijn --- Makefile | 4 ++-- main.go | 7 ++++--- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/Makefile b/Makefile index aeb62f8c3f7..e19a84e487b 100644 --- a/Makefile +++ b/Makefile @@ -21,9 +21,9 @@ ifeq ($(shell $(GO) env GOOS),linux) endif endif GO_BUILD := $(GO) build -trimpath $(GO_BUILDMODE) $(EXTRA_FLAGS) -tags "$(BUILDTAGS)" \ - -ldflags "-X main.gitCommit=$(COMMIT) -X main.version=$(VERSION) $(EXTRA_LDFLAGS)" + -ldflags "-X main.gitCommit=$(COMMIT) $(EXTRA_LDFLAGS)" GO_BUILD_STATIC := CGO_ENABLED=1 $(GO) build -trimpath $(EXTRA_FLAGS) -tags "$(BUILDTAGS) netgo osusergo" \ - -ldflags "-extldflags -static -X main.gitCommit=$(COMMIT) -X main.version=$(VERSION) $(EXTRA_LDFLAGS)" + -ldflags "-extldflags -static -X main.gitCommit=$(COMMIT) $(EXTRA_LDFLAGS)" .DEFAULT: runc diff --git a/main.go b/main.go index bb69672f71d..e271410d75c 100644 --- a/main.go +++ b/main.go @@ -1,6 +1,7 @@ package main import ( + _ "embed" "errors" "fmt" "io" @@ -17,9 +18,9 @@ import ( "github.com/urfave/cli" ) -// version must be set from the contents of VERSION file by go build's -// -X main.version= option in the Makefile. -var version = "unknown" +// version is automatically set from the contents of VERSION file on go1.16+. +//go:embed VERSION +var version string // gitCommit will be the hash that the binary was built from // and will be populated by the Makefile