-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMakefile
More file actions
622 lines (568 loc) · 17.9 KB
/
Copy pathMakefile
File metadata and controls
622 lines (568 loc) · 17.9 KB
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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
# Copyright 2024 Ian Lewis
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# Set the initial shell so we can determine extra options.
SHELL := /usr/bin/env bash -ueo pipefail
DEBUG_LOGGING ?= $(shell if [[ "$(GITHUB_ACTIONS)" == "true" ]] && [[ -n "$(RUNNER_DEBUG)" || "$(ACTIONS_RUNNER_DEBUG)" == "true" || "$(ACTIONS_STEP_DEBUG)" == "true" ]]; then echo "true"; else echo ""; fi)
BASH_OPTIONS := $(shell if [ "$(DEBUG_LOGGING)" == "true" ]; then echo "-x"; else echo ""; fi)
# Add extra options for debugging.
SHELL := /usr/bin/env bash -ueo pipefail $(BASH_OPTIONS)
uname_s := $(shell uname -s)
uname_m := $(shell uname -m)
arch.x86_64 := amd64
arch.aarch64 := arm64
arch.arm64 := arm64
arch := $(arch.$(uname_m))
kernel.Linux := linux
kernel.Darwin := darwin
kernel := $(kernel.$(uname_s))
OUTPUT_FORMAT ?= $(shell if [ "$(GITHUB_ACTIONS)" == "true" ]; then echo "github"; else echo ""; fi)
REPO_ROOT := $(shell dirname $(realpath $(firstword $(MAKEFILE_LIST))))
REPO_NAME := $(shell basename "$(REPO_ROOT)")
# renovate: datasource=github-releases depName=aquaproj/aqua versioning=loose
AQUA_VERSION ?= v2.59.1
# renovate: datasource=github-releases depName=aquaproj/aqua-installer versioning=loose
AQUA_INSTALLER_VERSION ?= v4.0.4
AQUA_REPO := github.com/aquaproj/aqua
AQUA_CHECKSUM ?= $(AQUA_CHECKSUM.$(kernel).$(arch))
AQUA_INSTALLER_URL := https://raw.githubusercontent.com/aquaproj/aqua-installer/$(AQUA_INSTALLER_VERSION)/aqua-installer
export AQUA_ROOT_DIR = $(REPO_ROOT)/.aqua
# Ensure that aqua and aqua installed tools are in the PATH.
export PATH := $(REPO_ROOT)/.bin/aqua-$(AQUA_VERSION):$(AQUA_ROOT_DIR)/bin:$(PATH)
# We want GNU versions of tools so prefer them if present.
GREP := $(shell command -v ggrep 2>/dev/null || command -v grep 2>/dev/null)
AWK := $(shell command -v gawk 2>/dev/null || command -v awk 2>/dev/null)
MKTEMP := $(shell command -v gmktemp 2>/dev/null || command -v mktemp 2>/dev/null)
# The help command prints targets in groups. Help documentation in the Makefile
# uses comments with double hash marks (##). Documentation is printed by the
# help target in the order in appears in the Makefile.
#
# Make targets can be documented with double hash marks as follows:
#
# target-name: ## target documentation.
#
# Groups can be added with the following style:
#
# ## Group name
.PHONY: help
help: ## Print all Makefile targets (this message).
@# bash \
echo "$(REPO_NAME) Makefile"; \
echo "Usage: $(MAKE) [COMMAND]"; \
echo ""; \
normal=""; \
cyan=""; \
if command -v tput >/dev/null 2>&1; then \
if [ -t 1 ]; then \
normal=$$(tput sgr0); \
cyan=$$(tput setaf 6); \
fi; \
fi; \
$(GREP) --no-filename -E '^([/a-z.A-Z0-9_%-]+:.*?|)##' $(MAKEFILE_LIST) | \
$(AWK) \
--assign=normal="$${normal}" \
--assign=cyan="$${cyan}" \
'BEGIN {FS = "(:.*?|)## ?"}; { \
if (length($$1) > 0) { \
printf(" " cyan "%-25s" normal " %s\n", $$1, $$2); \
} else { \
if (length($$2) > 0) { \
printf("%s\n", $$2); \
} \
} \
}'
# Node.js setup
#####################################################################
package-lock.json: package.json $(AQUA_ROOT_DIR)/.installed
@# bash \
loglevel="notice"; \
if [ -n "$(DEBUG_LOGGING)" ]; then \
loglevel="verbose"; \
fi; \
# NOTE: npm install will happily ignore the fact that integrity hashes are \
# missing in the package-lock.json. We need to check for missing integrity \
# fields ourselves. If any are missing, then we need to regenerate the \
# package-lock.json from scratch. \
nointegrity=""; \
noresolved=""; \
if [ -f "$@" ]; then \
nointegrity=$$(jq '.packages | del(."") | .[] | select(has("integrity") | not)' < $@); \
noresolved=$$(jq '.packages | del(."") | .[] | select(has("resolved") | not)' < $@); \
fi; \
if [ ! -f "$@" ] || [ -n "$${nointegrity}" ] || [ -n "$${noresolved}" ]; then \
# NOTE: package-lock.json is removed to ensure that npm includes the \
# integrity field. npm install will not restore this field if \
# missing in an existing package-lock.json file. \
rm -f $@; \
# NOTE: We clean the node_modules directory to ensure that npm install \
# will not desync between the package.json, package-lock.json \
# and the node_modules directory. \
$(MAKE) clean-node-modules; \
npm --loglevel="$${loglevel}" install \
--no-audit \
--no-fund; \
else \
npm --loglevel="$${loglevel}" install \
--package-lock-only \
--no-audit \
--no-fund; \
fi
node_modules/.installed: package.json
@# bash \
loglevel="silent"; \
if [ -n "$(DEBUG_LOGGING)" ]; then \
loglevel="verbose"; \
fi; \
npm --loglevel="$${loglevel}" clean-install; \
npm --loglevel="$${loglevel}" audit signatures; \
touch $@
# Python setup
#####################################################################
.uv/venv/bin/activate:
@# bash \
mkdir -p .uv; \
python -m venv .uv/venv; \
touch $@
.uv/.installed: requirements-dev.txt .uv/venv/bin/activate
@# bash \
./.uv/venv/bin/pip install -r $< --require-hashes; \
touch $@
uv.lock: pyproject.toml .uv/.installed
@# bash \
./.uv/venv/bin/uv lock; \
touch $@
.venv/.installed: pyproject.toml .uv/.installed
@# bash \
./.uv/venv/bin/uv sync --locked; \
touch $@
# Aqua setup
#####################################################################
# NOTE: aqua-installer itself is treated as a lockfile.
.PHONY: aqua-installer
aqua-installer:
curl -sSfL -o .aqua-installer $(AQUA_INSTALLER_URL); \
# TODO(github.com/aquaproj/aqua-installer/pull/932): Remove after merge \
sed -i'' -E 's/rm -R /rm -Rf /' .aqua-installer; \
chmod +x .aqua-installer
$(AQUA_ROOT_DIR)/bin/aqua:
@# bash \
./.aqua-installer -v "$(AQUA_VERSION)"
.aqua-checksums.json: .aqua.yaml $(AQUA_ROOT_DIR)/bin/aqua
@# bash \
loglevel="info"; \
if [ -n "$(DEBUG_LOGGING)" ]; then \
loglevel="debug"; \
fi; \
$(AQUA_ROOT_DIR)/bin/aqua \
--config ".aqua.yaml" \
--log-level "$${loglevel}" \
update-checksum
$(AQUA_ROOT_DIR)/.installed: $(AQUA_ROOT_DIR)/bin/aqua .aqua.yaml
@# bash \
loglevel="info"; \
if [ -n "$(DEBUG_LOGGING)" ]; then \
loglevel="debug"; \
fi; \
$(AQUA_ROOT_DIR)/bin/aqua \
--config ".aqua.yaml" \
--log-level "$${loglevel}" \
install; \
touch $@
## Build
#####################################################################
.PHONY: all
all: test package ## Run all tests and build a release package.
.PHONY: package
package: .venv/.installed ## Create a release package.
@# bash \
$(REPO_ROOT)/.venv/bin/python -m build
## Testing
#####################################################################
.PHONY: test
test: lint unit-test ## Run all linters and tests.
.PHONY: unit-test
unit-test: .venv/.installed ## Run unit tests.
@# bash \
$(REPO_ROOT)/.venv/bin/coverage run -m unittest discover .; \
$(REPO_ROOT)/.venv/bin/coverage xml; \
$(REPO_ROOT)/.venv/bin/coverage report -m
## Formatting
#####################################################################
.PHONY: format
format: json-format license-headers md-format py-format yaml-format ## Format all files
.PHONY: json-format
json-format: node_modules/.installed ## Format JSON files.
@# bash \
loglevel="log"; \
if [ -n "$(DEBUG_LOGGING)" ]; then \
loglevel="debug"; \
fi; \
files=$$( \
git ls-files --deduplicate \
'*.json' \
'*.json5' \
| while IFS='' read -r f; do [ -f "$${f}" ] && echo "$${f}" || true; done \
); \
if [ "$${files}" == "" ]; then \
exit 0; \
fi; \
./node_modules/.bin/prettier \
--log-level "$${loglevel}" \
--no-error-on-unmatched-pattern \
--write \
$${files}
.PHONY: license-headers
license-headers: ## Update license headers.
@# bash \
files=$$( \
git ls-files --deduplicate \
'*.c' \
'*.cpp' \
'*.go' \
'*.h' \
'*.hpp' \
'*.js' \
'*.lua' \
'*.py' \
'*.rb' \
'*.rs' \
'*.yaml' \
'*.yml' \
'Makefile' \
| while IFS='' read -r f; do [ -f "$${f}" ] && echo "$${f}" || true; done \
); \
name=$$(git config user.name); \
if [ "$${name}" == "" ]; then \
>&2 echo "git user.name is required."; \
>&2 echo "Set it up using:"; \
>&2 echo "git config user.name \"John Doe\""; \
exit 1; \
fi; \
for filename in $${files}; do \
if ! ( head "$${filename}" | $(GREP) -iL "Copyright" > /dev/null ); then \
./third_party/mbrukman/autogen/autogen.sh \
--in-place \
--no-code \
--no-tlc \
--copyright "$${name}" \
--license apache \
"$${filename}"; \
fi; \
done
.PHONY: md-format
md-format: node_modules/.installed ## Format Markdown files.
@# bash \
loglevel="log"; \
if [ -n "$(DEBUG_LOGGING)" ]; then \
loglevel="debug"; \
fi; \
files=$$( \
git ls-files --deduplicate \
'*.md' \
| while IFS='' read -r f; do [ -f "$${f}" ] && echo "$${f}" || true; done \
); \
if [ "$${files}" == "" ]; then \
exit 0; \
fi; \
# NOTE: prettier uses .editorconfig for tab-width. \
./node_modules/.bin/prettier \
--log-level "$${loglevel}" \
--no-error-on-unmatched-pattern \
--write \
$${files}
.PHONY: py-format
py-format: $(AQUA_ROOT_DIR)/.installed ## Format Python files.
@# bash \
files=$$( \
git ls-files --deduplicate \
'*.py' \
| while IFS='' read -r f; do [ -f "$${f}" ] && echo "$${f}" || true; done \
); \
if [ "$${files}" == "" ]; then \
exit 0; \
fi; \
ruff check --select I --fix $${files}; \
ruff format $${files}
.PHONY: yaml-format
yaml-format: node_modules/.installed ## Format YAML files.
@# bash \
loglevel="log"; \
if [ -n "$(DEBUG_LOGGING)" ]; then \
loglevel="debug"; \
fi; \
files=$$( \
git ls-files --deduplicate \
'*.yml' \
'*.yaml' \
); \
if [ "$${files}" == "" ]; then \
exit 0; \
fi; \
./node_modules/.bin/prettier \
--log-level "$${loglevel}" \
--no-error-on-unmatched-pattern \
--write \
$${files}
## Linting
#####################################################################
.PHONY: lint
lint: actionlint checkmake commitlint fixme format-check markdownlint mypy renovate-config-validator ruff textlint yamllint zizmor ## Run all linters.
.PHONY: actionlint
actionlint: $(AQUA_ROOT_DIR)/.installed ## Runs the actionlint linter.
@# bash \
# NOTE: We need to ignore config files used in tests. \
files=$$( \
git ls-files --deduplicate \
'.github/workflows/*.yml' \
'.github/workflows/*.yaml' \
| while IFS='' read -r f; do [ -f "$${f}" ] && echo "$${f}" || true; done \
); \
if [ "$${files}" == "" ]; then \
exit 0; \
fi; \
if [ "$(OUTPUT_FORMAT)" == "github" ]; then \
actionlint \
-format '{{range $$err := .}}::error file={{$$err.Filepath}},line={{$$err.Line}},col={{$$err.Column}}::{{$$err.Message}}%0A```%0A{{replace $$err.Snippet "\\n" "%0A"}}%0A```\n{{end}}' \
-ignore 'SC2016:' \
$${files}; \
else \
actionlint \
-ignore 'SC2016:' \
$${files}; \
fi
.PHONY: checkmake
checkmake: $(AQUA_ROOT_DIR)/.installed ## Runs the checkmake linter.
@# bash \
# NOTE: We need to ignore config files used in tests. \
files=$$( \
git ls-files --deduplicate \
'Makefile' \
| while IFS='' read -r f; do [ -f "$${f}" ] && echo "$${f}" || true; done \
); \
if [ "$${files}" == "" ]; then \
exit 0; \
fi; \
if [ "$(OUTPUT_FORMAT)" == "github" ]; then \
checkmake \
--format '::error file={{.FileName}},line={{.LineNumber}}::{{.Rule}}: {{.Violation}}' \
$${files}; \
else \
checkmake $${files}; \
fi
.PHONY: commitlint
commitlint: node_modules/.installed ## Run commitlint linter.
@# bash \
commitlint_from=$(COMMITLINT_FROM_REF); \
commitlint_to=$(COMMITLINT_TO_REF); \
if [ "$${commitlint_from}" == "" ]; then \
# Try to get the default branch without hitting the remote server \
if git symbolic-ref --short refs/remotes/origin/HEAD >/dev/null 2>&1; then \
commitlint_from=$$(git symbolic-ref --short refs/remotes/origin/HEAD); \
elif git show-ref refs/remotes/origin/master >/dev/null 2>&1; then \
commitlint_from="origin/master"; \
else \
commitlint_from="origin/main"; \
fi; \
fi; \
if [ "$${commitlint_to}" == "" ]; then \
# If upstream of HEAD is on the commitlint_from branch, then we will \
# lint the last commit by default. \
current_branch=$$(git rev-parse --abbrev-ref @{u}); \
if [ "$${commitlint_from}" == "$${current_branch}" ]; then \
commitlint_from="HEAD~1"; \
fi; \
commitlint_to="HEAD"; \
fi; \
./node_modules/.bin/commitlint \
--from "$${commitlint_from}" \
--to "$${commitlint_to}" \
--verbose \
--strict
.PHONY: fixme
fixme: $(AQUA_ROOT_DIR)/.installed ## Check for outstanding FIXMEs.
@# bash \
output="default"; \
if [ "$(OUTPUT_FORMAT)" == "github" ]; then \
output="github"; \
fi; \
# NOTE: todos does not use `git ls-files` because many files might be \
# unsupported and generate an error if passed directly on the \
# command line. \
todos \
--output "$${output}" \
--todo-types="FIXME,Fixme,fixme,BUG,Bug,bug,XXX,COMBAK"
.PHONY: format-check
format-check: ## Check that files are properly formatted.
@# bash \
if [ -n "$$(git diff)" ]; then \
>&2 echo "The working directory is dirty. Please commit, stage, or stash changes and try again."; \
exit 1; \
fi; \
$(MAKE) format; \
exit_code=0; \
if [ -n "$$(git diff)" ]; then \
>&2 echo "Some files need to be formatted. Please run '$(MAKE) format' and try again."; \
if [ "$(OUTPUT_FORMAT)" == "github" ]; then \
echo "::group::git diff"; \
fi; \
git --no-pager diff; \
if [ "$(OUTPUT_FORMAT)" == "github" ]; then \
echo "::endgroup::"; \
fi; \
exit_code=1; \
fi; \
git restore .; \
exit "$${exit_code}"
.PHONY: markdownlint
markdownlint: node_modules/.installed $(AQUA_ROOT_DIR)/.installed ## Runs the markdownlint linter.
@# bash \
# NOTE: Issue and PR templates are handled specially so we can disable \
# MD041/first-line-heading/first-line-h1 without adding an ugly html comment \
# at the top of the file. \
files=$$( \
git ls-files --deduplicate \
'*.md' \
| while IFS='' read -r f; do [ -f "$${f}" ] && echo "$${f}" || true; done \
); \
if [ "$${files}" == "" ]; then \
exit 0; \
fi; \
./node_modules/.bin/markdownlint-cli2 $${files}
.PHONY: mypy
mypy: .venv/.installed ## Runs the mypy type checker.
@# bash \
files=$$( \
git ls-files --deduplicate \
'*.py' \
| while IFS='' read -r f; do [ -f "$${f}" ] && echo "$${f}" || true; done \
); \
if [ "$${files}" == "" ]; then \
exit 0; \
fi; \
${REPO_ROOT}/.venv/bin/mypy \
--config-file mypy.ini \
$${files}
.PHONY: renovate-config-validator
renovate-config-validator: node_modules/.installed ## Validate Renovate configuration.
@# bash \
./node_modules/.bin/renovate-config-validator \
--strict
.PHONY: ruff
ruff: $(AQUA_ROOT_DIR)/.installed ## Runs the ruff linter.
@# bash \
files=$$( \
git ls-files --deduplicate \
'*.py' \
| while IFS='' read -r f; do [ -f "$${f}" ] && echo "$${f}" || true; done \
); \
if [ "$${files}" == "" ]; then \
exit 0; \
fi; \
if [ "$(OUTPUT_FORMAT)" == "github" ]; then \
ruff check --output-format=github $${files}; \
else \
ruff check $${files}; \
fi
.PHONY: textlint
textlint: node_modules/.installed $(AQUA_ROOT_DIR)/.installed ## Runs the textlint linter.
@# bash \
files=$$( \
git ls-files --deduplicate \
'*.md' \
'*.txt' \
':!:requirements*.txt' \
| while IFS='' read -r f; do [ -f "$${f}" ] && echo "$${f}" || true; done \
); \
if [ "$${files}" == "" ]; then \
exit 0; \
fi; \
./node_modules/.bin/textlint $${files}
.PHONY: yamllint
yamllint: .venv/.installed ## Runs the yamllint linter.
@# bash \
files=$$( \
git ls-files --deduplicate \
'*.yml' \
'*.yaml' \
| while IFS='' read -r f; do [ -f "$${f}" ] && echo "$${f}" || true; done \
); \
if [ "$${files}" == "" ]; then \
exit 0; \
fi; \
format="standard"; \
if [ "$(OUTPUT_FORMAT)" == "github" ]; then \
format="github"; \
fi; \
./.venv/bin/yamllint \
--strict \
--format "$${format}" \
$${files}
.PHONY: zizmor
zizmor: .venv/.installed ## Runs the zizmor linter.
@# bash \
# NOTE: On GitHub actions this outputs SARIF format to zizmor.sarif.json \
# in addition to outputting errors to the terminal. \
files=$$( \
git ls-files --deduplicate \
'.github/workflows/*.yml' \
'.github/workflows/*.yaml' \
| while IFS='' read -r f; do [ -f "$${f}" ] && echo "$${f}" || true; done \
); \
if [ "$${files}" == "" ]; then \
exit 0; \
fi; \
if [ "$(OUTPUT_FORMAT)" == "github" ]; then \
./.venv/bin/zizmor \
--quiet \
--pedantic \
--format sarif \
$${files} > zizmor.sarif.json; \
fi; \
./.venv/bin/zizmor \
--quiet \
--pedantic \
--format plain \
$${files}
## Maintenance
#####################################################################
.PHONY: update-lockfiles
update-lockfiles: .aqua-checksums.json package-lock.json uv.lock aqua-installer ## Update lockfiles.
.PHONY: todos
todos: $(AQUA_ROOT_DIR)/.installed ## Print outstanding TODOs.
@# bash \
output="default"; \
if [ "$(OUTPUT_FORMAT)" == "github" ]; then \
output="github"; \
fi; \
# NOTE: todos does not use `git ls-files` because many files might be \
# unsupported and generate an error if passed directly on the \
# command line. \
todos \
--output "$${output}" \
--todo-types="TODO,Todo,todo,FIXME,Fixme,fixme,BUG,Bug,bug,XXX,COMBAK"
.PHONY: clean-node-modules
clean-node-modules:
@$(RM) -r node_modules
.PHONY: clean
clean: clean-node-modules ## Delete temporary files.
@$(RM) -r .bin
@$(RM) -r $(AQUA_ROOT_DIR)
@$(RM) -r .venv
@$(RM) -r .uv
@$(RM) *.sarif.json
@$(RM) -r dist/
@$(RM) -r *.egg-info/
@$(RM) -r .coverage
@$(RM) -r coverage.xml
@python -Bc "import pathlib; [p.unlink() for p in pathlib.Path('.').rglob('*.py[cod]')]"
@python -Bc "import pathlib; [p.rmdir() for p in pathlib.Path('.').rglob('__pycache__')]"