forked from HewlettPackard/galadriel
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Makefile
217 lines (169 loc) · 6.01 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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
-include Makefile-poc.mk
DIR := ${CURDIR}
.DEFAULT_GOAL = help
E:=@
ifeq ($(V),1)
E=
endif
cyan := $(shell which tput > /dev/null && tput setaf 6 2>/dev/null || echo "")
reset := $(shell which tput > /dev/null && tput sgr0 2>/dev/null || echo "")
bold := $(shell which tput > /dev/null && tput bold 2>/dev/null || echo "")
.PHONY: default
default: build
all: build test
############################################################################
# OS/ARCH detection
############################################################################
os1=$(shell uname -s)
os2=
ifeq ($(os1),Darwin)
os1=darwin
os2=osx
else ifeq ($(os1),Linux)
os1=linux
os2=linux
else ifeq (,$(findstring MYSYS_NT-10-0-, $(os1)))
os1=windows
os2=windows
else
$(error unsupported OS: $(os1))
endif
arch1=$(shell uname -m)
ifeq ($(arch1),x86_64)
arch2=amd64
else ifeq ($(arch1),aarch64)
arch2=arm64
else ifeq ($(arch1),arm64)
arch2=arm64
else
$(error unsupported ARCH: $(arch1))
endif
############################################################################
# Vars
############################################################################
build_dir := $(DIR)/.build/$(os1)-$(arch1)
go_version_full := $(shell cat .go-version)
go_version := $(go_version_full:.0=)
go_dir := $(build_dir)/go/$(go_version)
ifeq ($(os1),windows)
go_bin_dir = $(go_dir)/go/bin
go_url = https://storage.googleapis.com/golang/go$(go_version).$(os1)-$(arch2).zip
exe=".exe"
else
go_bin_dir = $(go_dir)/bin
go_url = https://storage.googleapis.com/golang/go$(go_version).$(os1)-$(arch2).tar.gz
exe=
endif
go_path := PATH="$(go_bin_dir):$(PATH)"
oapi_codegen_version = 1.12.4
oapi_codegen_dir = $(build_dir)/protoc/$(protoc_version):q
server_sqlc_config_file = $(DIR)/pkg/server/datastore/sqlc.yaml
sqlc_dir = $(build_dir)/sqlc/$(sqlc_version)
sqlc_bin = $(sqlc_dir)/sqlc
sqlc_version = 1.17.0
ifeq ($(os1),windows)
sqlc_url = https://github.com/kyleconroy/sqlc/releases/download/v${sqlc_version}/sqlc_${sqlc_version}_windows_amd64.zip
else ifeq ($(os1),darwin)
sqlc_url = https://github.com/kyleconroy/sqlc/releases/download/v${sqlc_version}/sqlc_${sqlc_version}_darwin_$(arch2).zip
else
sqlc_url = https://github.com/kyleconroy/sqlc/releases/download/v${sqlc_version}/sqlc_${sqlc_version}_linux_amd64.zip
endif
go-check:
ifeq (go$(go_version), $(shell $(go_path) go version 2>/dev/null | cut -f3 -d' '))
else
@echo "Installing go $(go_version)..."
$(E)rm -rf $(dir $(go_dir))
$(E)mkdir -p $(go_dir)
$(E)curl -sSfL $(go_url) | tar xz -C $(go_dir) --strip-components=1
endif
## Checks installed go version and prints the path it is installed.
go-bin-path: go-check
@echo "$(go_bin_dir):${PATH}"
install-toolchain: install-sqlc | go-check
install-sqlc: $(sqlc_bin)
$(sqlc_bin):
@echo "Installing sqlc $(sqlc_version)..."
$(E)rm -rf $(dir $(sqlc_dir))
$(E)mkdir -p $(sqlc_dir)
$(E)echo $(sqlc_url); curl -sSfL $(sqlc_url) -o $(build_dir)/tmp.zip; unzip -q -d $(sqlc_dir) $(build_dir)/tmp.zip; rm $(build_dir)/tmp.zip
# The following vars are used in rule construction
comma := ,
null :=
space := $(null)
.PHONY: build
## Compiles all Galadriel binaries.
build: bin/galadriel-harvester bin/galadriel-server
# This is the master template for compiling Go binaries
define binary_rule
.PHONY: $1
$1: | go-check bin/
@echo Building $1...
$(E)$(go_path) go build -o $1 $2
endef
# This dynamically generates targets for each binary using
# the binary_rule template above
$(eval $(call binary_rule,bin/galadriel-harvester,cmd/harvester/main.go))
$(eval $(call binary_rule,bin/galadriel-server,cmd/server/main.go))
bin/:
@mkdir -p $@
CONTAINER_OPTIONS = docker podman
CONTAINER_EXEC := $(foreach exec,$(CONTAINER_OPTIONS),\
$(if $(shell which $(exec)),$(exec)))
server-run: build
./bin/galadriel-server run
## Runs the go unit tests.
test: test-unit
test-unit:
go test -cover ./...
## Runs unit tests with race detection.
race-test:
go test -cover -race ./...
## Generates the test coverage for the code with the Go tool.
coverage:
$(E)mkdir -p out/coverage
go test -v -coverprofile ./out/coverage/coverage.out ./... && \
go tool cover -html=./out/coverage/coverage.out -o ./out/coverage/index.html
## Builds docker image for Galadriel Server.
docker-build-server:
docker build . --target galadriel-server --tag galadriel-server:latest
## Builds docker image for Galadriel Harvester.
docker-build-harvester:
docker build . --target galadriel-harvester --tag galadriel-harvester:latest
## Builds all docker images.
docker-build: docker-build-server docker-build-harvester
#------------------------------------------------------------------------
# Document file
#------------------------------------------------------------------------
# VARIABLES
NAME = Galadriel
VERSION = 0.1.0
AUTHOR=HPE
# COLORS
GREEN := $(shell tput -Txterm setaf 2)
RESET := $(shell tput -Txterm sgr0)
TARGET_MAX_CHAR_NUM=30
## Shows help.
help:
@echo "$(bold)Usage:$(reset) make $(cyan)<target>$(reset)"
@echo
@echo "$(bold)Build:$(reset)"
@echo " $(cyan)build$(reset) - build all Galadriel binaries"
@echo
@echo "$(bold)Test:$(reset)"
@echo " $(cyan)test$(reset) - run unit tests"
@echo
@echo "$(bold)Build and test:$(reset)"
@echo " $(cyan)all$(reset) - build all Galadriel binaries, and run unit tests"
@echo
@echo "$(bold)Code generation:$(reset)"
@echo " $(cyan)generate$(reset) - generate datastore sql and api boileplate code"
### Code generation ####
.PHONY: generate-sqlc-server generete-spec
generate-sqlc-server: install-sqlc run-sqlc-server
run-sqlc-server:
$(sqlc_bin) generate --file $(server_sqlc_config_file)
generate-spec:
go install github.com/deepmap/oapi-codegen/cmd/oapi-codegen@v$(oapi_codegen_version)
cd ./pkg/common/api; $(GOPATH)/bin/oapi-codegen -config schemas.cfg.yaml schemas.yaml
cd ./pkg/server/api/admin; $(GOPATH)/bin/oapi-codegen -config admin.cfg.yaml admin.yaml
cd ./pkg/server/api/harvester; $(GOPATH)/bin/oapi-codegen -config harvester.cfg.yaml harvester.yaml