build.bash: make reproduceable builds easier

* Reduce the build time precision from seconds to days
* Allow to specify an arbitrary build date through an
  env variable
This commit is contained in:
Jakob Unterwurzacher 2017-09-06 21:41:22 +02:00
parent 512be8f081
commit 830cbb7218
2 changed files with 15 additions and 12 deletions

View File

@ -1,4 +1,11 @@
#!/bin/bash #!/bin/bash
#
# Compile gocryptfs and bake the git version string of itself and the go-fuse
# library into the binary.
#
# If you want to fake a build date to reproduce a specific build,
# you can use:
# BUILDDATE=2017-02-03 ./build.bash
set -eu set -eu
@ -24,13 +31,15 @@ fi
GITVERSIONFUSE=$OUT GITVERSIONFUSE=$OUT
cd "$MYDIR" cd "$MYDIR"
# Build Unix timestamp, something like 1467554204. # Build date, something like "2017-09-06"
BUILDTIME=$(date +%s) if [[ -z ${BUILDDATE:-} ]] ; then
BUILDDATE=$(date +%Y-%m-%d)
fi
# Make sure we have the go binary # Make sure we have the go binary
go version > /dev/null go version > /dev/null
LDFLAGS="-X main.GitVersion=$GITVERSION -X main.GitVersionFuse=$GITVERSIONFUSE -X main.BuildTime=$BUILDTIME" LDFLAGS="-X main.GitVersion=$GITVERSION -X main.GitVersionFuse=$GITVERSIONFUSE -X main.BuildDate=$BUILDDATE"
go build "-ldflags=$LDFLAGS" $@ go build "-ldflags=$LDFLAGS" $@
(cd gocryptfs-xray; go build $@) (cd gocryptfs-xray; go build $@)

12
main.go
View File

@ -7,7 +7,6 @@ import (
"runtime" "runtime"
"strconv" "strconv"
"strings" "strings"
"time"
"github.com/hanwen/go-fuse/fuse" "github.com/hanwen/go-fuse/fuse"
@ -26,8 +25,8 @@ var GitVersion = "[GitVersion not set - please compile using ./build.bash]"
// GitVersionFuse is the go-fuse library version, set by build.bash // GitVersionFuse is the go-fuse library version, set by build.bash
var GitVersionFuse = "[GitVersionFuse not set - please compile using ./build.bash]" var GitVersionFuse = "[GitVersionFuse not set - please compile using ./build.bash]"
// BuildTime is the Unix timestamp, set by build.bash // BuildDate is a date string like "2017-09-06", set by build.bash
var BuildTime = "0" var BuildDate = "0000-00-00"
// raceDetector is set to true by race.go if we are compiled with "go build -race" // raceDetector is set to true by race.go if we are compiled with "go build -race"
var raceDetector bool var raceDetector bool
@ -92,16 +91,11 @@ func changePassword(args *argContainer) {
// printVersion prints a version string like this: // printVersion prints a version string like this:
// gocryptfs v0.12-36-ge021b9d-dirty; go-fuse a4c968c; 2016-07-03 go1.6.2 // gocryptfs v0.12-36-ge021b9d-dirty; go-fuse a4c968c; 2016-07-03 go1.6.2
func printVersion() { func printVersion() {
humanTime := "0000-00-00"
if i, _ := strconv.ParseInt(BuildTime, 10, 64); i > 0 {
t := time.Unix(i, 0).UTC()
humanTime = fmt.Sprintf("%d-%02d-%02d", t.Year(), t.Month(), t.Day())
}
buildFlags := "" buildFlags := ""
if stupidgcm.BuiltWithoutOpenssl { if stupidgcm.BuiltWithoutOpenssl {
buildFlags = " without_openssl" buildFlags = " without_openssl"
} }
built := fmt.Sprintf("%s %s", humanTime, runtime.Version()) built := fmt.Sprintf("%s %s", BuildDate, runtime.Version())
if raceDetector { if raceDetector {
built += " -race" built += " -race"
} }