-
Notifications
You must be signed in to change notification settings - Fork 16
/
Makefile
136 lines (116 loc) · 4.3 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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
export GO111MODULE ?= on
export GOPROXY ?= https://proxy.golang.org
export GOSUMDB ?= sum.golang.org
CGO ?= 0
CLI_BINARY = ofn
# Get the currently used golang install path (in GOPATH/bin, unless GOBIN is set)
ifeq (,$(shell go env GOBIN))
GOBIN=$(shell go env GOPATH)/bin
else
GOBIN=$(shell go env GOBIN)
endif
LOCAL_ARCH := $(shell uname -m)
ifeq ($(LOCAL_ARCH),x86_64)
TARGET_ARCH_LOCAL = amd64
else ifeq ($(shell echo $(LOCAL_ARCH) | head -c 5),armv8)
TARGET_ARCH_LOCAL = arm64
else ifeq ($(shell echo $(LOCAL_ARCH) | head -c 5),aarch64)
TARGET_ARCH_LOCAL = arm64
else ifeq ($(shell echo $(LOCAL_ARCH) | head -c 4),armv)
TARGET_ARCH_LOCAL = arm
else ifeq ($(shell echo $(LOCAL_ARCH) | head -c 5),arm64)
TARGET_ARCH_LOCAL = arm64
else
TARGET_ARCH_LOCAL = amd64
endif
export GOARCH ?= $(TARGET_ARCH_LOCAL)
LOCAL_OS := $(shell uname)
ifeq ($(LOCAL_OS),Linux)
TARGET_OS_LOCAL = linux
GOLANGCI_LINT:=golangci-lint
export ARCHIVE_EXT = .tar.gz
else ifeq ($(LOCAL_OS),Darwin)
TARGET_OS_LOCAL = darwin
GOLANGCI_LINT:=golangci-lint
export ARCHIVE_EXT = .tar.gz
else
TARGET_OS_LOCAL ?= windows
BINARY_EXT_LOCAL = .exe
GOLANGCI_LINT:=golangci-lint.exe
export ARCHIVE_EXT = .zip
endif
export GOOS ?= $(TARGET_OS_LOCAL)
export BINARY_EXT ?= $(BINARY_EXT_LOCAL)
TEST_OUTPUT_FILE ?= test_output.json
# Use the variable H to add a header (equivalent to =>) to informational output
H = $(shell printf "\033[34;1m=>\033[0m")
ifeq ($(origin DEBUG), undefined)
BUILDTYPE_DIR:=release
else ifeq ($(DEBUG),0)
BUILDTYPE_DIR:=release
else
BUILDTYPE_DIR:=debug
GCFLAGS:=-gcflags="all=-N -l"
$(info $(H) Build with debugger information)
endif
GIT_COMMIT = $(shell git rev-parse HEAD)
GIT_SHA = $(shell git rev-parse --short HEAD)
GIT_TAG = $(shell git describe --tags --abbrev=0 --exact-match 2>/dev/null)
GIT_DIRTY = $(shell test -n "`git status --porcelain`" && echo "dirty" || echo "clean")
VERSION_METADATA = unreleased
VERSION = latest
# Clear the "unreleased" string in BuildMetadata
ifneq ($(GIT_TAG),)
VERSION_METADATA =
VERSION = ${GIT_TAG}
endif
LDFLAGS += -X github.com/OpenFunction/cli/version.version=${VERSION}
LDFLAGS += -X github.com/OpenFunction/cli/version.metadata=${VERSION_METADATA}
LDFLAGS += -X github.com/OpenFunction/cli/version.gitCommit=${GIT_COMMIT}
LDFLAGS += -X github.com/OpenFunction/cli/version.gitTreeState=${GIT_DIRTY}
################################################################################
# Go build details #
################################################################################
OUT_DIR := ./dist
BINS_OUT_DIR := $(OUT_DIR)
################################################################################
# Target: build #
################################################################################
.PHONY: build
build: test $(CLI_BINARY)
$(CLI_BINARY):
CGO_ENABLED=$(CGO) GOOS=$(GOOS) GOARCH=$(GOARCH) go build $(GCFLAGS) -ldflags '$(LDFLAGS)' \
-o $(BINS_OUT_DIR)/$(CLI_BINARY)_$(GOOS)_$(GOARCH)$(BINARY_EXT) cmd/main.go;
################################################################################
# Target: build #
################################################################################
.PHONY: test
test: fmt vet
go test -v ./... -coverprofile cover.out
################################################################################
# Target: lint #
################################################################################
.PHONY: lint
lint:
$(GOLANGCI_LINT) run --timeout=20m
fmt: goimports ## Run go fmt && goimports against code.
go fmt ./...
$(GOIMPORTS) -w cmd/ pkg/ testdata/
GOIMPORTS=$(shell pwd)/bin/goimports
goimports:
$(call go-get-tool,$(GOIMPORTS),golang.org/x/tools/cmd/[email protected])
vet: ## Run go vet against code.
go vet ./...
# go-get-tool will 'go get' any package $2 and install it to $1.
PROJECT_DIR := $(shell dirname $(abspath $(lastword $(MAKEFILE_LIST))))
define go-get-tool
@[ -f $(1) ] || { \
set -e ;\
TMP_DIR=$$(mktemp -d) ;\
cd $$TMP_DIR ;\
go mod init tmp ;\
echo "Downloading $(2)" ;\
GOBIN=$(PROJECT_DIR)/bin go install -v $(2) ;\
rm -rf $$TMP_DIR ;\
}
endef