From 9003454f88f99b0f7b4cb4306a360aa8c3691844 Mon Sep 17 00:00:00 2001 From: James Phillips Date: Wed, 2 Nov 2016 17:27:49 -0700 Subject: [PATCH] Adds basic build tag support with different versions. (#2463) --- GNUmakefile | 12 ++++++------ commands.go | 11 ++++++----- scripts/build.sh | 2 +- scripts/dist.sh | 20 ++++++++++++++------ scripts/dist_build.sh | 8 +++----- scripts/test.sh | 7 +++---- version.go => version/version.go | 18 ++++++++---------- version/version_base.go | 16 ++++++++++++++++ 8 files changed, 57 insertions(+), 37 deletions(-) rename version.go => version/version.go (60%) create mode 100644 version/version_base.go diff --git a/GNUmakefile b/GNUmakefile index 9f32667f1..bfef9434e 100644 --- a/GNUmakefile +++ b/GNUmakefile @@ -7,7 +7,7 @@ GOTOOLS = \ PACKAGES=$(shell go list ./... | grep -v '^github.com/hashicorp/consul/vendor/') VETARGS?=-asmdecl -atomic -bool -buildtags -copylocks -methods \ -nilfunc -printf -rangeloops -shift -structtags -unsafeptr -VERSION?=$(shell awk -F\" '/^const Version/ { print $$2; exit }' version.go) +BUILD_TAGS?=consul # all builds binaries for all targets all: bin @@ -20,15 +20,15 @@ ci: bin: tools @mkdir -p bin/ - @sh -c "'$(CURDIR)/scripts/build.sh'" + @BUILD_TAGS='$(BUILD_TAGS)' sh -c "'$(CURDIR)/scripts/build.sh'" # dev creates binaries for testing locally - these are put into ./bin and $GOPATH dev: format - @CONSUL_DEV=1 sh -c "'$(CURDIR)/scripts/build.sh'" + @CONSUL_DEV=1 BUILD_TAGS='$(BUILD_TAGS)' sh -c "'$(CURDIR)/scripts/build.sh'" # dist builds binaries for all platforms and packages them for distribution dist: - @sh -c "'$(CURDIR)/scripts/dist.sh' $(VERSION)" + @BUILD_TAGS='$(BUILD_TAGS)' sh -c "'$(CURDIR)/scripts/dist.sh'" cov: gocov test ./... | gocov-html > /tmp/coverage.html @@ -37,7 +37,7 @@ cov: test: format @$(MAKE) vet @./scripts/verify_no_uuid.sh - @./scripts/test.sh + @BUILD_TAGS='$(BUILD_TAGS)' sh -c "'$(CURDIR)/scripts/test.sh'" cover: go list ./... | xargs -n1 go test --cover @@ -49,7 +49,7 @@ format: vet: @echo "--> Running go tool vet $(VETARGS) ." @go list ./... \ - | grep -v ^github.com/hashicorp/consul/vendor/ \ + | grep -v /vendor/ \ | cut -d '/' -f 4- \ | xargs -n1 \ go tool vet $(VETARGS) ;\ diff --git a/commands.go b/commands.go index 99bebff45..eb90a4093 100644 --- a/commands.go +++ b/commands.go @@ -7,6 +7,7 @@ import ( "github.com/hashicorp/consul/command" "github.com/hashicorp/consul/command/agent" + "github.com/hashicorp/consul/version" "github.com/mitchellh/cli" ) @@ -19,10 +20,10 @@ func init() { Commands = map[string]cli.CommandFactory{ "agent": func() (cli.Command, error) { return &agent.Command{ - Revision: GitCommit, - Version: Version, - VersionPrerelease: VersionPrerelease, - HumanVersion: GetHumanVersion(), + Revision: version.GitCommit, + Version: version.Version, + VersionPrerelease: version.VersionPrerelease, + HumanVersion: version.GetHumanVersion(), Ui: ui, ShutdownCh: make(chan struct{}), }, nil @@ -177,7 +178,7 @@ func init() { "version": func() (cli.Command, error) { return &command.VersionCommand{ - HumanVersion: GetHumanVersion(), + HumanVersion: version.GetHumanVersion(), Ui: ui, }, nil }, diff --git a/scripts/build.sh b/scripts/build.sh index f782f63f5..ef232460f 100755 --- a/scripts/build.sh +++ b/scripts/build.sh @@ -3,7 +3,6 @@ # This script builds the application from source for multiple platforms. set -e -export GO15VENDOREXPERIMENT=1 export CGO_ENABLED=0 # Get the parent directory of where this script is. @@ -43,6 +42,7 @@ echo "==> Building..." -osarch="!darwin/arm" \ -ldflags "-X main.GitCommit='${GIT_COMMIT}${GIT_DIRTY}' -X main.GitDescribe='${GIT_DESCRIBE}'" \ -output "pkg/{{.OS}}_{{.Arch}}/consul" \ + -tags="${BUILD_TAGS}" \ . # Move all the compiled things to the $GOPATH/bin diff --git a/scripts/dist.sh b/scripts/dist.sh index da5d5e21f..3bbf071fa 100755 --- a/scripts/dist.sh +++ b/scripts/dist.sh @@ -1,14 +1,22 @@ #!/usr/bin/env bash set -e -export GO15VENDOREXPERIMENT=1 - -# Get the version from the command line. -VERSION=$1 +# Get the version from the environment, or try to figure it out from the build tags. +# We process the files in the same order Go does to find the last matching tag. if [ -z $VERSION ]; then - echo "Please specify a version." + for file in $(ls version/version_*.go | sort); do + for tag in "$BUILD_TAGS"; do + if grep -q "// +build $tag" $file; then + VERSION=$(awk -F\" '/Version =/ { print $2; exit }' <$file) + fi + done + done +fi +if [ -z $VERSION ]; then + echo "Please specify a version (couldn't find one based on build tags)." exit 1 fi +echo "==> Building version $VERSION..." # Get the parent directory of where this script is. SOURCE="${BASH_SOURCE[0]}" @@ -28,7 +36,7 @@ fi # Do a hermetic build inside a Docker container. if [ -z $NOBUILD ]; then docker build -t hashicorp/consul-builder scripts/consul-builder/ - docker run --rm -v "$(pwd)":/gopath/src/github.com/hashicorp/consul hashicorp/consul-builder ./scripts/dist_build.sh + docker run --rm -e "BUILD_TAGS=$BUILD_TAGS" -v "$(pwd)":/gopath/src/github.com/hashicorp/consul hashicorp/consul-builder ./scripts/dist_build.sh fi # Zip all the files. diff --git a/scripts/dist_build.sh b/scripts/dist_build.sh index 4edbf252e..83fa16a5f 100755 --- a/scripts/dist_build.sh +++ b/scripts/dist_build.sh @@ -1,8 +1,6 @@ #!/usr/bin/env bash set -e -export GO15VENDOREXPERIMENT=1 - # Get the parent directory of where this script is. SOURCE="${BASH_SOURCE[0]}" while [ -h "$SOURCE" ] ; do SOURCE="$(readlink "$SOURCE")"; done @@ -12,7 +10,7 @@ DIR="$( cd -P "$( dirname "$SOURCE" )/.." && pwd )" cd $DIR # Make sure build tools are abailable. -make -f GNUMakefile tools +make tools # Build the standalone version of the web assets for the sanity check. pushd ui @@ -29,7 +27,7 @@ popd # Regenerate the built-in web assets. If there are any diffs after doing this # then we know something is up. -make -f GNUMakefile static-assets +make static-assets if ! git diff --quiet command/agent/bindata_assetfs.go; then echo "Checked-in web assets are out of date, build aborted" exit 1 @@ -39,7 +37,7 @@ fi # away our pkg folder so we have to regenerate the ui once more. This is probably # for the best since we have meddled with the timestamps. rm -rf pkg -make -f GNUMakefile all +make all pushd ui make dist popd diff --git a/scripts/test.sh b/scripts/test.sh index b05c6500e..5c7a5a42f 100755 --- a/scripts/test.sh +++ b/scripts/test.sh @@ -1,6 +1,5 @@ #!/usr/bin/env bash - -export GO15VENDOREXPERIMENT=1 +set -e # Create a temp dir and clean it up on exit TEMPDIR=`mktemp -d -t consul-test.XXX` @@ -8,8 +7,8 @@ trap "rm -rf $TEMPDIR" EXIT HUP INT QUIT TERM # Build the Consul binary for the API tests echo "--> Building consul" -go build -o $TEMPDIR/consul || exit 1 +go build -tags="${BUILD_TAGS}" -o $TEMPDIR/consul || exit 1 # Run the tests echo "--> Running tests" -go list ./... | grep -v '^github.com/hashicorp/consul/vendor/' | PATH=$TEMPDIR:$PATH xargs -n1 go test ${GOTEST_FLAGS:--cover -timeout=360s} +go list ./... | grep -v '^/vendor/' | PATH=$TEMPDIR:$PATH xargs -n1 go test -tags="${BUILD_TAGS}" ${GOTEST_FLAGS:--cover -timeout=360s} diff --git a/version.go b/version/version.go similarity index 60% rename from version.go rename to version/version.go index 7e0b3e31b..1acae6706 100644 --- a/version.go +++ b/version/version.go @@ -1,24 +1,22 @@ -package main +package version import ( "fmt" "strings" ) -// The git commit that was compiled. This will be filled in by the compiler. var ( + // The git commit that was compiled. These will be filled in by the + // compiler. GitCommit string GitDescribe string + + // Release versions of the build. These will be filled in by one of the + // build tag-specific files. + Version = "unknown" + VersionPrerelease = "unknown" ) -// The main version number that is being run at the moment. -const Version = "0.7.1" - -// A pre-release marker for the version. If this is "" (empty string) -// then it means that it is a final release. Otherwise, this is a pre-release -// such as "dev" (in development), "beta", "rc1", etc. -const VersionPrerelease = "dev" - // GetHumanVersion composes the parts of the version in a way that's suitable // for displaying to humans. func GetHumanVersion() string { diff --git a/version/version_base.go b/version/version_base.go new file mode 100644 index 000000000..bca1ad780 --- /dev/null +++ b/version/version_base.go @@ -0,0 +1,16 @@ +// +build consul + +package version + +// NOTE we rely on other "version_*.go" files to be lexically after +// "version_base.go" in order for this to get properly overridden. Be careful +// adding new versions and pick a name that will follow "version_base.go". +func init() { + // The main version number that is being run at the moment. + Version = "0.7.1" + + // A pre-release marker for the version. If this is "" (empty string) + // then it means that it is a final release. Otherwise, this is a pre-release + // such as "dev" (in development), "beta", "rc1", etc. + VersionPrerelease = "dev" +}