feat: Add version print

This commit is contained in:
Muyao CHEN
2024-10-02 22:21:09 +02:00
parent 08e2d6bd4a
commit 62597e4534
7 changed files with 185 additions and 2 deletions

View File

@ -4,6 +4,28 @@ COMMON_SELF_DIR := $(dir $(lastword $(MAKEFILE_LIST)))
ROOT_DIR := $(abspath $(shell cd $(COMMON_SELF_DIR)/ && pwd -P))
OUTPUT_DIR := $(ROOT_DIR)/_output
# ==============================================================================
# Version
VERSION_PACKAGE=git.vinchent.xyz/vinchent/howmuch/pkg/version
ifeq ($(origin VERSION), undefined)
VERSION:=$(shell git describe --tags --always --match='v*')
endif
GIT_TREE_STATE="dirty"
ifeq (, $(shell git status --porcelain 2>/dev/null))
GIT_TREE_STATE ="clean"
endif
GIT_COMMIT:=$(shell git rev-parse HEAD)
GO_LDFLAGS += \
-X $(VERSION_PACKAGE).GitVersion=$(VERSION) \
-X $(VERSION_PACKAGE).GitCommit=$(GIT_COMMIT) \
-X $(VERSION_PACKAGE).GitTreeState=$(GIT_TREE_STATE) \
-X $(VERSION_PACKAGE).BuildDate=$(shell date -u +'%Y-%m-%dT%H:%M:%SZ')
# ==============================================================================
.PHONY: all
all: add-copyright format build
@ -12,7 +34,7 @@ all: add-copyright format build
.PHONY: build
build: tidy # build.
@go build -v -o $(OUTPUT_DIR)/howmuch $(ROOT_DIR)/cmd/howmuch/main.go
@go build -v -ldflags "$(GO_LDFLAGS)" -o $(OUTPUT_DIR)/howmuch $(ROOT_DIR)/cmd/howmuch/main.go
.PHONY: format
format: # format code.