-
Notifications
You must be signed in to change notification settings - Fork 1
/
Makefile
61 lines (48 loc) · 1.81 KB
/
Makefile
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# Generic parameters
DATE=$(shell date -u +%Y-%m-%d-%H:%M:%S-%Z)
# Go parameters
GOCMD=go
GOBUILD=$(GOCMD) build
GOCLEAN=$(GOCMD) clean
GOTEST=$(GOCMD) test
GOGET=$(GOCMD) get
BUILD_DIR=build
WORKING_DIRECTORY=$(dir $(realpath $(firstword $(MAKEFILE_LIST))))
BINARY_NAME=$(shell basename $(WORKING_DIRECTORY))
OS := $(if $(GOOS),$(GOOS),$(shell go env GOOS))
ARCH := $(if $(GOARCH),$(GOARCH),$(shell go env GOARCH))
BINARY=$(BINARY_NAME)-$(OS)-$(ARCH)
# This version-strategy uses git tags to set the version string
VERSION := $(shell git describe --tags --always --dirty)
COMMIT := $(shell git rev-list -1 HEAD)
# This version-strategy uses a manual value to set the version string
#VERSION := 1.2.3
BIN_FILE=$(BUILD_DIR)/$(VERSION)/$(BINARY)
.PHONY: all
all: test build ## Test and build
.PHONY: build
build: ## Build the binary
echo $(BINARY_NAME)
rm -f $(BIN_FILE)
CGO_ENABLED=1 GOOS=$(OS) GOARCH=$(ARCH) $(GOBUILD) -ldflags "-X github.com/srimaln91/go-make.version=$(VERSION) \
-X github.com/srimaln91/go-make.date=$(DATE) \
-X github.com/srimaln91/go-make.gitCommit=$(COMMIT) \
-X github.com/srimaln91/go-make.osArch=$(OS)/$(ARCH)" \
-o $(BIN_FILE) -v
.PHONY: test
test: ## Run unit tests
$(GOTEST) -covermode=atomic -coverprofile=coverage.txt -v ./... -ldflags "-X github.com/srimaln91/go-make.version=$(VERSION) \
-X github.com/srimaln91/go-make.date=$(DATE) \
-X github.com/srimaln91/go-make.gitCommit=$(COMMIT) \
-X github.com/srimaln91/go-make.osArch=$(OS)/$(ARCH)"
.PHONY: clean
clean: ## Clean the build directory
$(GOCLEAN)
rm -rf $(BUILD_DIR)
.PHONY: run
run: ## Build and run the binary
$(GOBUILD) -o $(BIN_FILE) -v
./$(BIN_FILE)
.PHONY: help
help: ## display help page
@grep -E '^[a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) | sort | awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-30s\033[0m %s\n", $$1, $$2}'