-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMakefileUtils.mk
More file actions
74 lines (70 loc) · 2.32 KB
/
Copy pathMakefileUtils.mk
File metadata and controls
74 lines (70 loc) · 2.32 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
#################################
# Helpers and utils
TARGET := $(firstword $(MAKECMDGOALS))
ARGS := $(wordlist 2,$(words $(MAKECMDGOALS)),$(MAKECMDGOALS))
comma := ,
empty :=
space := $(empty) $(empty)
JOIN_ARGS = $(subst $(space),$(1),$(ARGS))
GET_ARG = $(word $(1),$(ARGS))
NARGS = $(words $(ARGS))
.PHONY: $(TARGET) $(ARGS)
ifneq ($(strip $(ARGS)),)
$(ARGS): ; @:
endif
define REQUIRE_ARGS
@if [ $(NARGS) -lt $(1) ]; then \
printf '❌ Usage: make %s %s\n' '$(TARGET)' '$(2)'; \
exit 1; \
fi
endef
define REQUIRE_VAR
@if [ -z "$($(1))" ]; then \
printf '❌ Missing required variable: %s\n' '$(1)'; \
printf ' Usage: make $(TARGET) %s=<value>\n' '$(1)'; \
exit 1; \
fi
endef
#################################
# ───────── Colors (off in CI) ─────────
ifndef CI_JOB_ID
RED := $(shell tput -Txterm setaf 1)
GREEN := $(shell tput -Txterm setaf 2)
YELLOW := $(shell tput -Txterm setaf 3)
BLUE := $(shell tput -Txterm setaf 4)
CYAN := $(shell tput -Txterm setaf 6)
WHITE := $(shell tput -Txterm setaf 7)
RESET := $(shell tput -Txterm sgr0)
else
GREEN := ; YELLOW := ; RED := ; WHITE := ; RESET :=
endif
#################################
# ────────── Logger ─────────────
define __LOG
@lvl='$(1)'; msg='$(2)'; \
case "$$lvl" in \
info) color='$(CYAN)'; tag='[INFO]';; \
success) color='$(GREEN)'; tag='[SUCCESS]';; \
warn) color='$(YELLOW)'; tag='[WARN]';; \
error) color='$(RED)'; tag='[ERROR]';; \
*) color='$(WHITE)'; tag='[LOG]';; \
esac; \
printf "%s%s%s %s\n" "$$color" "$$tag" "$(RESET)" "$$msg"
endef
log = $(call __LOG,default,$(1))
log_info = $(call __LOG,info,$(1))
log_success= $(call __LOG,success,$(1))
log_warn = $(call __LOG,warn,$(1))
log_error = $(call __LOG,error,$(1))
.DEFAULT_GOAL := help
help::
@echo "${GREEN}My project${RESET}"
@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; \
} \
isTopic = match(lastLine, /^###/); \
if (isTopic) { printf "\n%s\n", $$1; } \
} { lastLine = $$0 }' $(MAKEFILE_LIST)