Skip to content

Commit

Permalink
Use go1.16 "embed" to set version from VERSION file
Browse files Browse the repository at this point in the history
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]: golang/go#37475
[2]: https://pkg.go.dev/runtime/debug#BuildInfo
[3]: golang/go#29814

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
  • Loading branch information
thaJeztah committed Dec 2, 2021
1 parent 0c0ec3f commit f03bd33
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 5 deletions.
4 changes: 2 additions & 2 deletions Makefile
Expand Up @@ -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

Expand Down
7 changes: 4 additions & 3 deletions main.go
@@ -1,6 +1,7 @@
package main

import (
_ "embed"
"errors"
"fmt"
"io"
Expand All @@ -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
Expand Down

0 comments on commit f03bd33

Please sign in to comment.