From 72ed7bd3e95e8222beece3540ef5022b90aa9df5 Mon Sep 17 00:00:00 2001 From: Sebastiaan van Stijn Date: Wed, 6 Apr 2022 11:06:44 +0200 Subject: [PATCH 1/2] scripts/build: fix date formatting on macOS This was introduced in 9d40c7464eb2a351e8b2e63dd1fdce6a43f2c70f, which added support for `SOURCE_DATE_EPOCH` to override the build-time. macOS uses the BSD flavor of `date`, which does not support the `--date` option to set a custom time. Before this: DISABLE_WARN_OUTSIDE_CONTAINER=1 make binary ./scripts/build/binary date: illegal option -- - usage: date [-jnRu] [-d dst] [-r seconds] [-t west] [-v[+|-]val[ymwdHMS]] ... [-f fmt date | [[[mm]dd]HH]MM[[cc]yy][.ss]] [+format] After this: DISABLE_WARN_OUTSIDE_CONTAINER=1 make binary ./scripts/build/binary Building static docker-darwin-amd64 + go build -o build/docker-darwin-amd64 -tags ' osusergo pkcs11' -ldflags ' -w -X "github.com/docker/cli/cli/version.GitCommit=a4b6fe16a" -X "github.com/docker/cli/cli/version.BuildTime=2022-04-06T10:57:25Z" -X "github.com/docker/cli/cli/version.Version=20.10.2-589-ga4b6fe16a.m"' -buildmode=pie github.com/docker/cli/cmd/docker ... Signed-off-by: Sebastiaan van Stijn --- scripts/build/.variables | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/scripts/build/.variables b/scripts/build/.variables index 76b0602f06..ae2500dfa4 100755 --- a/scripts/build/.variables +++ b/scripts/build/.variables @@ -12,7 +12,15 @@ TARGET=${TARGET:-"build"} PLATFORM=${PLATFORM:-} VERSION=${VERSION:-$(git describe --match 'v[0-9]*' --dirty='.m' --always --tags | sed 's/^v//' 2>/dev/null || echo "unknown-version" )} GITCOMMIT=${GITCOMMIT:-$(git rev-parse --short HEAD 2> /dev/null || true)} -BUILDTIME=${BUILDTIME:-$(date -u --date="@${SOURCE_DATE_EPOCH:-$(date +%s)}" +"%Y-%m-%dT%H:%M:%SZ")} + +if [ "$(uname)" = "Darwin" ]; then + # Using BSD date (macOS), which doesn't suppoort the --date option + # date -jf "" "" +"" (https://unix.stackexchange.com/a/86510) + BUILDTIME=${BUILDTIME:-$(date -jf "%s" "${SOURCE_DATE_EPOCH:-$(date +%s)}" +"%Y-%m-%dT%H:%M:%SZ")} +else + # Using GNU date (Linux) + BUILDTIME=${BUILDTIME:-$(date -u --date="@${SOURCE_DATE_EPOCH:-$(date +%s)}" +"%Y-%m-%dT%H:%M:%SZ")} +fi GOOS="$(go env GOOS)" GOARCH="$(go env GOARCH)" From 8fd92f18669bffdbd73b5bf2cb3706f529ddf59f Mon Sep 17 00:00:00 2001 From: Sebastiaan van Stijn Date: Wed, 6 Apr 2022 11:16:48 +0200 Subject: [PATCH 2/2] scripts/build: make sure build-time is in UTC Before this: SOURCE_DATE_EPOCH=1606660000 DISABLE_WARN_OUTSIDE_CONTAINER=1 make binary ... + go build -o build/docker-darwin-amd64 -tags ' osusergo pkcs11' -ldflags ' -w -X "github.com/docker/cli/cli/version.GitCommit=090dd58dc" -X "github.com/docker/cli/cli/version.BuildTime=2020-11-29T15:26:40Z" -X "github.com/docker/cli/cli/version.Version=20.10.2-590-g090dd58dc"' -buildmode=pie github.com/docker/cli/cmd/docker ... make -f docker.Makefile shell SOURCE_DATE_EPOCH=1606660000 make binary ... + go build -o build/docker-linux-amd64 -tags -ldflags ' -w -X "github.com/docker/cli/cli/version.GitCommit=090dd58dc" -X "github.com/docker/cli/cli/version.BuildTime=2020-11-29T14:26:40Z" -X "github.com/docker/cli/cli/version.Version=20.10.0-dev"' github.com/docker/cli/cmd/docker ... Notice that `version.BuildTime` is `2020-11-29T15:26:40Z` outside the container, and `2020-11-29T14:26:40Z` inside. After this: SOURCE_DATE_EPOCH=1606660000 DISABLE_WARN_OUTSIDE_CONTAINER=1 make binary ... + go build -o build/docker-darwin-amd64 -tags ' osusergo pkcs11' -ldflags ' -w -X "github.com/docker/cli/cli/version.GitCommit=090dd58dc" -X "github.com/docker/cli/cli/version.BuildTime=2020-11-29T14:26:40Z" -X "github.com/docker/cli/cli/version.Version=20.10.2-590-g090dd58dc.m"' -buildmode=pie github.com/docker/cli/cmd/docker ... make -f docker.Makefile shell SOURCE_DATE_EPOCH=1606660000 make binary ... + go build -o build/docker-linux-amd64 -tags -ldflags ' -w -X "github.com/docker/cli/cli/version.GitCommit=090dd58dc" -X "github.com/docker/cli/cli/version.BuildTime=2020-11-29T14:26:40Z" -X "github.com/docker/cli/cli/version.Version=20.10.0-dev"' github.com/docker/cli/cmd/docker ... Notice that `version.BuildTime` is `2020-11-29T14:26:40Z` both outside and inside the container. Signed-off-by: Sebastiaan van Stijn --- scripts/build/.variables | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/scripts/build/.variables b/scripts/build/.variables index ae2500dfa4..f14e54f2f4 100755 --- a/scripts/build/.variables +++ b/scripts/build/.variables @@ -16,10 +16,10 @@ GITCOMMIT=${GITCOMMIT:-$(git rev-parse --short HEAD 2> /dev/null || true)} if [ "$(uname)" = "Darwin" ]; then # Using BSD date (macOS), which doesn't suppoort the --date option # date -jf "" "" +"" (https://unix.stackexchange.com/a/86510) - BUILDTIME=${BUILDTIME:-$(date -jf "%s" "${SOURCE_DATE_EPOCH:-$(date +%s)}" +"%Y-%m-%dT%H:%M:%SZ")} + BUILDTIME=${BUILDTIME:-$(TZ=UTC date -jf "%s" "${SOURCE_DATE_EPOCH:-$(date +%s)}" +"%Y-%m-%dT%H:%M:%SZ")} else # Using GNU date (Linux) - BUILDTIME=${BUILDTIME:-$(date -u --date="@${SOURCE_DATE_EPOCH:-$(date +%s)}" +"%Y-%m-%dT%H:%M:%SZ")} + BUILDTIME=${BUILDTIME:-$(TZ=UTC date -u --date="@${SOURCE_DATE_EPOCH:-$(date +%s)}" +"%Y-%m-%dT%H:%M:%SZ")} fi GOOS="$(go env GOOS)"