This repository has been archived by the owner on Apr 7, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
var.Makefile
65 lines (51 loc) · 1.8 KB
/
var.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
ifndef __VAR_MAKEFILE__
__VAR_MAKEFILE__ := included
# Provides a class of targets that ensures variables are
# defined and trigger rebuilds when variable values change.
#
# Usage:
#
# my_target: .build/var/REGISTRY
#
# my_target rebuilds when the value of $(REGISTRY) changes.
# .build/var/REGISTRY also ensures that $(REGISTRY) value
# is non-empty.
# The main target that this Makefile offers.
# This is a real file that gets updated when a variable value
# change is detected. This rule does not have a recipe. It
# relies on the %-phony prerequisite to detect the change and
# update the file.
.build/var/%: .build/var/%-required .build/var/%-phony ;
.build/var:
mkdir -p .build/var
# Since we can't make pattern targets phony, we make them
# effectively phony by depending on this phony target.
.PHONY: var/phony
var/phony: ;
# An effectively phony target that always runs to compare the current
# value of the variable (say VARNAME) with the content of the
# corresponding .build/var/VARNAME file. If the contents differ,
# the recipe updates .build/var/VARNAME, which effectively trigger
# rebuilding of targets depending on .build/var.VARNAME.
.build/var/%-phony: var/phony | .build/var
@ \
var_key="$*" ; \
var_val="${$*}" ; \
var_val_old=$$(cat ".build/var/$$var_key" 2> /dev/null) ; \
if [ "$$var_val" != "$$var_val_old" ]; then \
$(call print_notice,$$var_key has been updated) ; \
printf " old value: $$var_val_old\n" ; \
printf " new value: $$var_val\n" ; \
printf "$$var_val" > .build/var/$$var_key ; \
fi
# An effectively phony target that verifies that the variable
# has a non-empty value.
.build/var/%-required: var/phony
@ \
var_key="$*" ; \
var_val="${$*}" ; \
if [ "$$var_val" = "" ]; then \
$(call print_notice,Make variable '$*' is required); \
exit 1; \
fi
endif