Skip to content

Instantly share code, notes, and snippets.

@akutz
Created September 11, 2016 15:19
Show Gist options
  • Save akutz/452896c825a53b6b7d3aa0b33641c2af to your computer and use it in GitHub Desktop.
Save akutz/452896c825a53b6b7d3aa0b33641c2af to your computer and use it in GitHub Desktop.
################################################################################
## PROJECT INFO ##
################################################################################
GO_LIST_BUILD_INFO_CMD := go list -f '{{with $$ip:=.}}{{with $$ctx:=context}}{{printf "%s %s %s %s %s 0,%s" $$ip.ImportPath $$ip.Name $$ip.Dir $$ctx.GOOS $$ctx.GOARCH (join $$ctx.BuildTags ",")}}{{end}}{{end}}'
BUILD_INFO := $(shell $(GO_LIST_BUILD_INFO_CMD))
ROOT_IMPORT_PATH := $(word 1,$(BUILD_INFO))
ROOT_IMPORT_NAME := $(word 2,$(BUILD_INFO))
ROOT_DIR := $(word 3,$(BUILD_INFO))
GOOS ?= $(word 4,$(BUILD_INFO))
GOARCH ?= $(word 5,$(BUILD_INFO))
BUILD_TAGS := $(word 6,$(BUILD_INFO))
BUILD_TAGS := $(subst $(COMMA), ,$(BUILD_TAGS))
BUILD_TAGS := $(wordlist 2,$(words $(BUILD_TAGS)),$(BUILD_TAGS))
VENDORED := 0
ifneq (,$(strip $(findstring vendor,$(ROOT_IMPORT_PATH))))
VENDORED := 1
endif
################################################################################
## VERSION ##
################################################################################
# figure out the gitroot
ifneq (1,$(VENDORED))
GIT_ROOT:=$(shell find $(HOME)/.glide -name "*emccode-libstorage" -d)/.git
ifeq (,$(wildcard $(GIT_ROOT)))
GIT_ROOT:=.git
endif
else
GIT_ROOT:=.git
endif
# parse a semver
SEMVER_PATT := ^[^\d]*(\d+)\.(\d+)\.(\d+)(?:-([a-zA-Z].+?))?(?:-(\d+)-g(.+?)(?:-(dirty))?)?$$
PARSE_SEMVER = $(shell echo $(1) | perl -pe 's/$(SEMVER_PATT)/$(2)/gim')
# describe the git information and create a parsing function for it
GIT_DESCRIBE := $(shell git --git-dir="$(GIT_ROOT)" describe --long --dirty)
PARSE_GIT_DESCRIBE = $(call PARSE_SEMVER,$(GIT_DESCRIBE),$(1))
# parse the version components from the git information
V_MAJOR := $(call PARSE_GIT_DESCRIBE,$$1)
V_MINOR := $(call PARSE_GIT_DESCRIBE,$$2)
V_PATCH := $(call PARSE_GIT_DESCRIBE,$$3)
V_NOTES := $(call PARSE_GIT_DESCRIBE,$$4)
V_BUILD := $(call PARSE_GIT_DESCRIBE,$$5)
V_SHA_SHORT := $(call PARSE_GIT_DESCRIBE,$$6)
V_DIRTY := $(call PARSE_GIT_DESCRIBE,$$7)
V_OS := $(OS)
V_ARCH := $(ARCH)
V_OS_ARCH := $(V_OS)-$(V_ARCH)
# the long commit hash
V_SHA_LONG := $(shell git --git-dir="$(GIT_ROOT)" show HEAD -s --format=%H)
# the branch name, possibly from travis-ci
ifeq ($(origin TRAVIS_BRANCH), undefined)
TRAVIS_BRANCH := $(shell git --git-dir="$(GIT_ROOT)" branch | grep '*')
else
ifeq (,$(strip $(TRAVIS_BRANCH)))
TRAVIS_BRANCH := $(shell git --git-dir="$(GIT_ROOT)" branch | grep '*')
endif
endif
TRAVIS_BRANCH := $(subst $(ASTERIK) ,,$(TRAVIS_BRANCH))
TRAVIS_BRANCH := $(subst $(LPAREN)HEAD detached at ,,$(TRAVIS_BRANCH))
TRAVIS_BRANCH := $(subst $(RPAREN),,$(TRAVIS_BRANCH))
ifeq ($(origin TRAVIS_TAG), undefined)
TRAVIS_TAG := $(TRAVIS_BRANCH)
else
ifeq ($(strip $(TRAVIS_TAG)),)
TRAVIS_TAG := $(TRAVIS_BRANCH)
endif
endif
V_BRANCH := $(TRAVIS_TAG)
# the build date as an epoch
V_EPOCH := $(shell date +%s)
# the build date
V_BUILD_DATE := $(shell perl -e 'use POSIX strftime; print strftime("%a, %d %b %Y %H:%M:%S %Z", localtime($(V_EPOCH)))')
# the release date as required by bintray
V_RELEASE_DATE := $(shell perl -e 'use POSIX strftime; print strftime("%Y-%m-%d", localtime($(V_EPOCH)))')
# init the semver
V_SEMVER := $(V_MAJOR).$(V_MINOR).$(V_PATCH)
ifneq ($(V_NOTES),)
V_SEMVER := $(V_SEMVER)-$(V_NOTES)
endif
# get the version file's version
V_FILE := $(strip $(shell cat VERSION 2> /dev/null))
# append the build number and dirty values to the semver if appropriate
ifneq ($(V_BUILD),)
ifneq ($(V_BUILD),0)
# if the version file's version is different than the version parsed from the
# git describe information then use the version file's version
ifneq ($(V_SEMVER),$(V_FILE))
V_MAJOR := $(call PARSE_SEMVER,$(V_FILE),$$1)
V_MINOR := $(call PARSE_SEMVER,$(V_FILE),$$2)
V_PATCH := $(call PARSE_SEMVER,$(V_FILE),$$3)
V_NOTES := $(call PARSE_SEMVER,$(V_FILE),$$4)
V_SEMVER := $(V_MAJOR).$(V_MINOR).$(V_PATCH)
ifneq ($(V_NOTES),)
V_SEMVER := $(V_SEMVER)-$(V_NOTES)
endif
endif
V_SEMVER := $(V_SEMVER)+$(V_BUILD)
endif
endif
ifeq ($(V_DIRTY),dirty)
V_SEMVER := $(V_SEMVER)+$(V_DIRTY)
endif
define API_GENERATED_CONTENT
package api
import (
"time"
"github.com/emccode/libstorage/api/types"
)
func init() {
Version = &types.VersionInfo{}
Version.Arch = "$(V_OS_ARCH)"
Version.Branch = "$(V_BRANCH)"
Version.BuildTimestamp = time.Unix($(V_EPOCH), 0)
Version.SemVer = "$(V_SEMVER)"
Version.ShaLong = "$(V_SHA_LONG)"
}
endef
export API_GENERATED_CONTENT
PRINTF_VERSION_CMD += @printf "SemVer: %s\nBinary: %s\nBranch: %s\nCommit:
PRINTF_VERSION_CMD += %s\nFormed: %s\n" "$(V_SEMVER)" "$(V_OS_ARCH)"
PRINTF_VERSION_CMD += "$(V_BRANCH)" "$(V_SHA_LONG)" "$(V_BUILD_DATE)"
API_GENERATED_SRC := ./api/api_generated.go
$(API_GENERATED_SRC):
echo generating $@
@echo "$$API_GENERATED_CONTENT" > $@
$(API_GENERATED_SRC)-clean:
rm -f $(API_GENERATED_SRC)
GO_CLEAN += $(API_GENERATED_SRC)-clean
GO_PHONE += $(API_GENERATED_SRC)-clean
API_A := $(GOPATH)/pkg/$(GOOS)_$(GOARCH)/$(ROOT_IMPORT_PATH)/api.a
$(API_A): $(API_GENERATED_SRC)
version:
$(PRINTF_VERSION_CMD)
GO_PHONY += version
$ make version
SemVer: 0.2.0+dirty
Binary: Darwin-x86_64
Branch: v0.2.0
Commit: c23090e58041510950fc6d59adb52ef3763d8a99
Formed: Sun, 11 Sep 2016 10:19:17 CDT
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment