-
Notifications
You must be signed in to change notification settings - Fork 0
/
Makefile
196 lines (157 loc) · 4.99 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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
SHELL := /bin/sh
BIN := /usr/local/bin
REV := $(shell git rev-parse HEAD)
CHANGES := $(shell test -n "$$(git status --porcelain)" && echo '+CHANGES' || true)
PROTO_LOCATION := $(shell find proto -iname "proto" -exec echo "-I="{} \;)
PACKAGE = lda
TARGET = lda
VERSION=$(shell git describe --tags --abbrev=0 || echo "x.x.x")
COMMIT=$(shell git rev-parse --short HEAD)
BRANCH=$(shell git rev-parse --abbrev-ref HEAD)
# figure out current platform
UNAME := $(shell uname | tr A-Z a-z )
# docker registry prefix:
DOCKER_REGISTRY := registry.gitlab.codilas.com/codilas/devzero/lda
# Setup the -ldflags option for go build here, interpolate the variable values
LDFLAGS = -s -w -X config.Version=${VERSION} -X config.Commit=${COMMIT}${CHANGES} -X config.Branch=${BRANCH}
CGO_CFLAGS=-I/usr/local/include
CGO_LDFLAGS=-L/usr/local/lib
ROOT_DIR:=$(shell dirname $(realpath $(firstword $(MAKEFILE_LIST))))
BUF_VERSION := 1.30.0
BUF_BINARY_NAME := buf
# count processors
NPROCS:=1
OS:=$(shell uname -s)
ifeq ($(OS),Linux)
NPROCS:=$(shell grep -c ^processor /proc/cpuinfo)
endif
ifeq ($(OS),Darwin) # Assume Mac OS X
NPROCS:=$(shell sysctl -n hw.ncpu)
endif
.DEFAULT_GOAL := all
.PHONY: \
all \
build \
deps \
doc \
buf \
init \
run \
clean \
lint \
proto \
proto-lint \
hooks \
hooks-install \
docker-create-buildx \
docker-push-buildx
all: build debug
## Build binary
build: proto
CGO_ENABLED=0 GOOS=$(UNAME) GOARCH=$(ARCH) go build -a -tags netgo -ldflags="$(LDFLAGS)" -o "$(TARGET)" .
## Install binary to GOPATH
install: proto
CGO_ENABLED=1 GOOS=$(UNAME) go install -a -tags netgo -ldflags="$(LDFLAGS)"
## Install binary to /usr/local/bin
install-global: proto install
@sudo cp ${GOPATH}/bin/$(TARGET) /usr/local/bin/$(TARGET)
# Check if buf is installed and version is correct, enabled smarter handling of buf rule.
BUF_INSTALLED_VERSION := $(shell if command -v $(BIN)/$(BUF_BINARY_NAME) >/dev/null; then $(BIN)/$(BUF_BINARY_NAME) --version; fi)
VERSION_MATCH := $(findstring ${BUF_INSTALLED_VERSION},$(BUF_VERSION))
## Install buf binary
buf:
@if [ -x "$(BIN)/$(BUF_BINARY_NAME)" ] && [ -n "$(VERSION_MATCH)" ]; then \
echo "The required version of $(BUF_BINARY_NAME) is already installed: $(BUF_VERSION)"; \
else \
echo "Installing or updating $(BUF_BINARY_NAME)..."; \
curl -sSL "https://github.com/bufbuild/buf/releases/download/v${BUF_VERSION}/${BUF_BINARY_NAME}-$(shell uname -s)-$(shell uname -m)" -o "${BIN}/${BUF_BINARY_NAME}"; \
chmod +x $(BIN)/$(BUF_BINARY_NAME); \
fi
## Build and debug
debug: build
./$(TARGET)
## Install tools and deps
deps: tools
go get -t -v ./...
## Run local godoc server on :8080
doc:
godoc -http=localhost:8080 -index
## Install precommit hooks
hooks-install:
pre-commit install
## Run precommit hooks
hooks:
pre-commit run -a
## Install tools, deps, build, check
init: deps buf build
## Lint code
lint:
go fmt ./...
## Build and run
run: build
./$(TARGET)
## Run go tests
test:
go test ./... -v
## Clean up all generated files
clean:
rm -rf ./docs/*
rm -rf ./$(TARGET)
## Lint proto files
proto-lint:
buf lint --error-format=json
## Compile all proto files with buf
proto: clean buf
rm -rf ./gen/*
mkdir -p gen # create the empty directory for proto targets.
buf generate --verbose .
## Rebuild dockerfile and run docker compose
docker-compose:
docker-compose build
docker-compose up
## Rebuild dockerfile and run docker compose - ubuntu
docker-compose-ubuntu:
docker-compose -f ubuntu.docker-compose.yml build
docker-compose -f ubuntu.docker-compose.yml up
# ## Push docker image to registry
docker-push:
$(eval NAME=${DOCKER_REGISTRY}/${TARGET})
$(eval DOCKER_FQDN=${NAME}:${BRANCH}-${COMMIT})
docker build -f Dockerfile . -t ${DOCKER_FQDN}
docker push ${DOCKER_FQDN}
@echo "Pushed ${DOCKER_FQDN} !"
## Create buildx remote builder for m1 support
docker-create-buildx:
export DOCKER_HOST=tcp://10.10.150.66:2375
docker buildx create --driver docker-container --platform linux/amd64,linux/arm64 --name remote-builder
docker buildx use remote-builder
## Push docker image to registry with buildx
docker-push-buildx:
$(eval NAME=${DOCKER_REGISTRY}/${TARGET})
$(eval DOCKER_FQDN=${NAME}:${BRANCH}-${COMMIT})
docker buildx build --platform linux/amd64,linux/arm64 -t ${DOCKER_FQDN} . --push
@echo "Pushed ${DOCKER_FQDN} !"
# Fancy help message
# Source: https://gist.github.com/prwhite/8168133
# COLOR
GREEN := $(shell tput -Txterm setaf 2)
YELLOW := $(shell tput -Txterm setaf 3)
WHITE := $(shell tput -Txterm setaf 7)
RESET := $(shell tput -Txterm sgr0)
TARGET_MAX_CHAR_NUM=20
## Show help
help:
@echo ''
@echo 'Usage:'
@echo ' ${YELLOW}make${RESET} ${GREEN}<target>${RESET}'
@echo ''
@echo 'Targets:'
@awk '/^[a-zA-Z\-\0-9]+:/ { \
helpMessage = match(lastLine, /^## (.*)/); \
if (helpMessage) { \
helpCommand = substr($$1, 0, index($$1, ":")-1); \
helpMessage = substr(lastLine, RSTART + 3, RLENGTH); \
printf " ${YELLOW}%-$(TARGET_MAX_CHAR_NUM)s${RESET} ${GREEN}%s${RESET}\n", helpCommand, helpMessage; \
} \
} \
{ lastLine = $$0 }' $(MAKEFILE_LIST)