Skip to content

Commit

Permalink
Adds basic build tag support with different versions. (hashicorp#2463)
Browse files Browse the repository at this point in the history
  • Loading branch information
slackpad authored Nov 3, 2016
1 parent e8caf76 commit 2a950f3
Show file tree
Hide file tree
Showing 8 changed files with 57 additions and 37 deletions.
12 changes: 6 additions & 6 deletions GNUmakefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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) ;\
Expand Down
11 changes: 6 additions & 5 deletions commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand All @@ -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
Expand Down Expand Up @@ -177,7 +178,7 @@ func init() {

"version": func() (cli.Command, error) {
return &command.VersionCommand{
HumanVersion: GetHumanVersion(),
HumanVersion: version.GetHumanVersion(),
Ui: ui,
}, nil
},
Expand Down
2 changes: 1 addition & 1 deletion scripts/build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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
Expand Down
20 changes: 14 additions & 6 deletions scripts/dist.sh
Original file line number Diff line number Diff line change
@@ -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
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."
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]}"
Expand All @@ -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.
Expand Down
8 changes: 3 additions & 5 deletions scripts/dist_build.sh
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down
7 changes: 3 additions & 4 deletions scripts/test.sh
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
#!/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`
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}
18 changes: 8 additions & 10 deletions version.go → version/version.go
Original file line number Diff line number Diff line change
@@ -1,23 +1,21 @@
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
)

// 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"
// Release versions of the build. These will be filled in by one of the
// build tag-specific files.
Version = "unknown"
VersionPrerelease = "unknown"
)

// GetHumanVersion composes the parts of the version in a way that's suitable
// for displaying to humans.
Expand Down
16 changes: 16 additions & 0 deletions version/version_base.go
Original file line number Diff line number Diff line change
@@ -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"
}

0 comments on commit 2a950f3

Please sign in to comment.