-
Notifications
You must be signed in to change notification settings - Fork 0
/
makefile
255 lines (202 loc) · 7.31 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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
# vim: set noexpandtab fo-=t:
# https://www.gnu.org/software/make/manual/make.html
.PHONY: default
default:
########################################################################
# boiler plate
########################################################################
SHELL=bash
current_makefile:=$(lastword $(MAKEFILE_LIST))
current_makefile_dirname:=$(dir $(current_makefile))
current_makefile_dirname_abspath:=$(dir $(abspath $(current_makefile)))
current_makefile_dirname_realpath:=$(dir $(realpath $(current_makefile)))
ifneq ($(filter all vars,$(VERBOSE)),)
dump_var=$(info var $(1)=$($(1)))
dump_vars=$(foreach var,$(1),$(call dump_var,$(var)))
else
dump_var=
dump_vars=
endif
ifneq ($(filter all targets,$(VERBOSE)),)
__ORIGINAL_SHELL:=$(SHELL)
SHELL=$(warning Building $@$(if $<, (from $<))$(if $?, ($? newer)))$(TIME) $(__ORIGINAL_SHELL)
endif
define __newline
endef
skip=
# skipable makes the targets passed to it skipable with skip=foo%
# $(1): targets that should be skipable
skipable=$(filter-out $(skip),$(1))
########################################################################
# variables ...
########################################################################
main_cmd=iagotmpl
tools_dir=local-tools
cache_dir=$(HOME)/.cache
build_dir=build
GOPATH:=$(shell type go >/dev/null 2>&1 && go env GOPATH)
export PATH:=$(if $(GOPATH),$(GOPATH)/bin:,)$(PATH)
# export GOFLAGS:=-mod=vendor
giti_description=$(shell git describe --tags --always)
giti_commit_count:=$(shell git rev-list --no-merges --count HEAD)
giti_cstate=$(shell [[ -z $$(git status -s) ]] && echo "s" || echo "m")
giti_commit_hash=$(shell git log -1 --format="%H")
giti_commit_hash_short=$(shell git log -1 --format="%h")
giti_suffix=$(giti_commit_count)$(giti_cstate)-$(giti_commit_hash_short)
giti_url:=$(shell git remote get-url origin)
$(call dump_vars,giti_suffix)
dt_info:=$(shell date "+%Y-%m-%d %H:%M:%S%z")
########################################################################
# targets ...
########################################################################
.PHONY: all
default: all
all:
.PHONY: clean
clean: clean-$(build_dir)/
.PHONY: distclean
distclean: clean-$(tools_dir)/
modd_bin=$(tools_dir)/modd
modd_version=0.8
$(modd_bin): | $(tools_dir)/ $(cache_dir)/
cd $(cache_dir) && wget -c https://github.com/cortesi/modd/releases/download/v$(modd_version)/modd-$(modd_version)-linux64.tgz
tar -zxvf $(cache_dir)/modd-0.8-linux64.tgz --strip-components=1 -C $(tools_dir)
antlr_jar=$(cache_dir)/antlr-4.9.2-complete.jar
antlr=java -jar $(antlr_jar)
$(antlr_jar): | $(cache_dir)/
wget -O $(@) https://www.antlr.org/download/$(notdir $(@))
.PHONY: toolchain
toolchain: $(modd_bin) $(antlr_jar)
cd ~/ && go get $(if $(filter all commands,$(VERBOSE)),-v) $(go_get_flags) \
github.com/golangci/golangci-lint/cmd/[email protected] \
&& true
.PHONY: toolchain-update
toolchain-update: go_get_flags+=-u
toolchain-update: toolchain
.PHONY: validate-static
validate-static:
golangci-lint run $(if $(filter all commands,$(VERBOSE)),-v) ./...
.PHONY: validate-fix
validate-fix:
golangci-lint run $(if $(filter all commands,$(VERBOSE)),-v) --fix ./...
.PHONY: test
test:
go test -cover -race $(if $(filter all commands,$(VERBOSE)),-v) ./...
.PHONY: validate-dynamic
validate-dynamic: test
.PHONY: validate
validate: validate-static validate-dynamic
.PHONY: watch
watch: $(modd_bin)
./$(modd_bin) --debug --notify --file modd.conf
.PHONY: generate
generate:
#$(antlr) -Dlanguage=Go NT.g4 -o internal/nt_parser/ -visitor -package nt_parser
$(antlr) -Dlanguage=Go NT.g4 -o internal/nt_parser/ -package nt_parser
go_ldflags= \
-ldflags \
"-X 'main.gitCommit=$(giti_commit_hash)' \
-X 'main.gitRemote=$(giti_url)' \
-X 'main.gitDesc=$(giti_description)' \
-X 'main.buildDT=$(dt_info)'" \
.PHONY: build
all: build
build: $(call skipable,validate)
go build $(go_ldflags) ./...
.PHONY: install
install: $(call skipable,validate)
go install $(go_ldflags) ./...
.PHONY: run-help
run-help:
$(foreach cmd,$(wildcard ./cmd/*),go run $(cmd) --help)
.PHONY: run
run: run_args=run
run:
go run ./cmd/$(main_cmd) $(run_args)
build-oci-inputs: | $(build_dir)/go/bin/
CGO_ENABLED=0 GOOS=linux GOARCH=amd64 \
go build $(go_ldflags) -a -v \
-o $(build_dir)/go/bin/ \
./...
hadolint_tag=:latest
.PHONY: validate-dockerfile
validate-dockerfile:
docker run --rm -i ghcr.io/hadolint/hadolint$(hadolint_tag) < Dockerfile
oci_tag_prefix=
oci_tag_suffixes = \
gits-$(giti_suffix) \
gitd-$(giti_description) \
latest \
oci_registry=example.com
oci_repo=some/service
oci_refs_local=\
$(foreach oci_tag_suffix,\
$(oci_tag_suffixes),\
ocreg.localhost/$(oci_registry)/$(oci_repo):$(oci_tag_prefix)$(oci_tag_suffix))
oci_refs_remote=\
$(foreach oci_tag_suffix,\
$(oci_tag_suffixes),\
$(oci_registry)/$(oci_repo):$(oci_tag_prefix)$(oci_tag_suffix))
oci_refs = \
$(oci_refs_local) \
$(oci_refs_remote) \
$(call dump_vars,oci_refs)
.PHONY: build-oci
build-oci: Dockerfile $(call skipable,validate-dockerfile build-oci-inputs)
docker image build --force-rm \
$(foreach oci_tag,$(oci_refs),--tag $(oci_tag)) \
--build-arg main_cmd=$(main_cmd) \
--file $(<) \
$(build_dir)
dockle_tag=:latest
.PHONY: validate-oci
validate-oci: $(call skipable,build-oci)
docker run --rm -v /var/run/docker.sock:/var/run/docker.sock \
docker.io/goodwithtech/dockle$(dockle_tag) \
$(firstword $(oci_refs_local))
.PHONY: run-oci
run-oci: $(call skipable,build-oci)
docker container run --rm -it \
$(firstword $(oci_refs_local))
.PHONY: push-oci
push-oci: $(call skipable,build-oci)
$(foreach oci_ref_remote,$(oci_refs_remote),\
docker push $(oci_ref_remote)$(__newline))
help:
@printf "########################################################################\n"
@printf "TARGETS:\n"
@printf "########################################################################\n"
@printf "%-32s%s\n" "help" "Show this output ..."
@printf "%-32s%s\n" "all" "Build all outputs (default)"
@printf "%-32s%s\n" "toolchain" "Install toolchain"
@printf "%-32s%s\n" "validate" "Validate everything"
@printf "%-32s%s\n" "validate-fix" "Fix auto-fixable validation errors"
@printf "%-32s%s\n" "generate" "Generate code"
@printf "%-32s%s\n" "build" "Build everything"
@printf "%-32s%s\n" "install" "Install everything"
@printf "%-32s%s\n" "run-help" "Runs every command with --help"
@printf "%-32s%s\n" "run" "Runs the main command"
@printf "%-32s%s\n" "distclean" "Cleans all things that should not be checked in"
@printf "%-32s%s\n" "build-oci" "Build the OCI Image"
@printf "%-32s%s\n" "run-oci" "Run the OCI Image"
@printf "%-32s%s\n" "validate-oci" "Validate the OCI Image"
@printf "########################################################################\n"
@printf "VARIABLES:\n"
@printf "########################################################################\n"
@printf "%-32s%s\n" "VERBOSE" "Sets verbosity for specific aspects." \
"" "Space seperated." \
"" "Valid values: all, vars, commands, targets"
########################################################################
# useful ...
########################################################################
## force ...
.PHONY: .FORCE
.FORCE:
$(force_targets): .FORCE
## dirs ...
.PRECIOUS: %/
%/:
mkdir -vp $(@)
.PHONY: clean-%/
clean-%/:
@{ test -d $(*) && { set -x; rm -vr $(*); set +x; } } || echo "directory $(*) does not exist ... nothing to clean"