feat(cli): --version using ldflags

The go-modules approach is a bit broken: https://github.com/golang/go/issues/29228
To resolve that, we now compute that version using `git describe` and
set it using `ldflags`.

While this is not as nice/builtin as the other approach, it at least
works all the time and across go versions.

Furthermore, I did refactor the Makefile in preparation for an AUR
package that builds from source and for building release artifacts in CI.
This commit is contained in:
sh0rez 2020-02-28 19:40:37 +01:00
parent f8be6a936f
commit 80c06a483a
No known key found for this signature in database
GPG key ID: 87C71DF9F8181FF1
4 changed files with 37 additions and 83 deletions

View file

@ -1,31 +1,35 @@
all: check-license build generate test
.PHONY: all check-license crossbuild build install test generate embedmd
GITHUB_URL=github.com/jsonnet-bundler/jsonnet-bundler
GOOS?=$(shell uname -s | tr A-Z a-z)
GOARCH?=$(subst x86_64,amd64,$(patsubst i%86,386,$(shell uname -m)))
VERSION := $(shell git describe --tags --dirty --always)
OUT_DIR=_output
BIN?=jb
VERSION?=$(shell cat VERSION)
PKGS=$(shell go list ./... | grep -v /vendor/)
check-license:
@echo ">> checking license headers"
@./scripts/check_license.sh
all: check-license build generate test
crossbuild:
@GOOS=linux ARCH=amd64 $(MAKE) -s build
# Binaries
LDFLAGS := '-s -w -extldflags "-static" -X main.Version=${VERSION}'
cross: clean
CGO_ENABLED=0 gox \
-output="$(OUT_DIR)/jb-{{.OS}}-{{.Arch}}" \
-ldflags=$(LDFLAGS) \
-arch="amd64 arm64 arm" -os="linux" \
-osarch="darwin/amd64" \
./cmd/$(BIN)
static:
CGO_ENABLED=0 go build -ldflags=${LDFLAGS} -o $(OUT_DIR)/$(BIN) ./cmd/$(BIN)
build:
@$(eval OUTPUT=$(OUT_DIR)/$(GOOS)/$(GOARCH)/$(BIN))
@echo ">> building for $(GOOS)/$(GOARCH) to $(OUTPUT)"
@mkdir -p $(OUT_DIR)/$(GOOS)/$(GOARCH)
@CGO_ENABLED=0 go build --installsuffix cgo -o $(OUTPUT) $(GITHUB_URL)/cmd/$(BIN)
CGO_ENABLED=0 go build ./cmd/$(BIN)
install: build
@$(eval OUTPUT=$(OUT_DIR)/$(GOOS)/$(GOARCH)/$(BIN))
install: static
@echo ">> copying $(BIN) into $(GOPATH)/bin/$(BIN)"
@cp $(OUTPUT) $(GOPATH)/bin/$(BIN)
cp $(OUT_DIR)/$(BIN) $(GOPATH)/bin/$(BIN)
# Tests
test:
@echo ">> running all unit tests"
go test -v $(PKGS)
@ -34,12 +38,22 @@ test-integration:
@echo ">> running all integration tests"
go test -v -tags=integration $(PKGS)
# Documentation
generate: embedmd
@echo ">> generating docs"
@./scripts/generate-help-txt.sh
@$(GOPATH)/bin/embedmd -w `find ./ -path ./vendor -prune -o -name "*.md" -print`
embedmd:
@go get github.com/campoy/embedmd
check-license:
@echo ">> checking license headers"
@./scripts/check_license.sh
.PHONY: all check-license crossbuild build install test generate embedmd
embedmd:
pushd /tmp && GO111MODULES=on go get github.com/campoy/embedmd && popd
# Other
clean:
rm -rf $(OUT_DIR) $(BIN)
drone:
drone jsonnet --format