1# in: PROJECT_ROOT
2# in: PKGNAME
3# in: FEATURES (optional)
4# in: BUILD_INFO (optional)
5# in: OPENSSL (optional)
6# in: OPENSSL_FOUND (optional)
7# in: SOURCE_DATE_EPOCH (optional)
8
9ifndef PROJECT_ROOT
10$(error PROJECT_ROOT not defined)
11endif
12
13ifndef PKGNAME
14$(error PKGNAME not defined)
15endif
16
17ifndef OPENSSL
18ifdef OPENSSL_FOUND
19OPENSSL=	$(OPENSSL_FOUND)/bin/openssl
20else
21OPENSSL=	openssl
22endif
23endif
24
25BASENAME?=	basename
26CUT?=		cut
27DATE?=		date
28DIFF?=		diff
29GIT?=		git
30GREP?=		grep
31RM?=		rm
32SED?=		sed
33SORT?=		sort
34TR?=		tr
35WC?=		wc
36
37GITDIR:=	$(wildcard $(PROJECT_ROOT)/.git)
38VERSION_FILE:=	$(wildcard $(PROJECT_ROOT)/VERSION)
39HASHES_FILE:=	$(wildcard $(PROJECT_ROOT)/HASHES)
40NEWS_FILE:=	$(firstword $(wildcard $(PROJECT_ROOT)/NEWS*))
41
42ifdef GITDIR
43BUILD_VERSION:=	$(shell cd $(PROJECT_ROOT) && $(GIT) describe --tags --dirty --always)
44BUILD_INFO+=	V:GIT
45else
46ifdef VERSION_FILE
47BUILD_VERSION:=	$(shell $(CAT) $(VERSION_FILE))
48BUILD_INFO+=	V:FILE
49else
50BUILD_VERSION:=	$(shell cd $(PROJECT_ROOT) && $(BASENAME) $(PWD)|\
51			$(GREP) $(PKGNAME)-|\
52			$(SED) 's/.*$(PKGNAME)-\(.*\)/\1/g')
53BUILD_INFO+=	V:DIR
54endif
55ifdef HASHES_FILE
56BUILD_INFO+=	HDIFF:$(shell $(OPENSSL) dgst -sha1 -r *.[hc]|\
57		$(SORT) -k 2 >HASHES~;\
58		$(DIFF) -u HASHES HASHES~|\
59		$(GREP) '^-[^-]'|$(WC) -l|$(TR) -d ' ';\
60		$(RM) HASHES~)
61endif
62ifdef NEWS_FILE
63NEWS_SHA:=	$(shell $(OPENSSL) dgst -sha1 -r $(NEWS_FILE) |\
64			$(CUT) -c -7)
65BUILD_INFO+=	N:$(NEWS_SHA)
66endif
67endif # GITDIR
68
69ifdef SOURCE_DATE_EPOCH
70BUILD_DATE:=	$(shell \
71		$(DATE) -u -d "@$(SOURCE_DATE_EPOCH)" "+%Y-%m-%d" 2>/dev/null||\
72		$(DATE) -u -r "$(SOURCE_DATE_EPOCH)" "+%Y-%m-%d" 2>/dev/null||\
73		$(DATE) -u "+%Y-%m-%d")
74else
75BUILD_DATE:=	$(shell date +%Y-%m-%d)
76endif
77BUILD_CPPFLAGS+=-D"BUILD_PKGNAME=\"$(PKGNAME)\"" \
78		-D"BUILD_VERSION=\"$(BUILD_VERSION)\"" \
79		-D"BUILD_DATE=\"$(BUILD_DATE)\"" \
80		-D"BUILD_INFO=\"$(BUILD_INFO)\"" \
81		-D"BUILD_FEATURES=\"$(FEATURES)\""
82
83# out: NEWS_FILE
84# out: NEWS_SHA
85# out: VERSION_FILE
86# out: GITDIR
87# out: BUILD_VERSION
88# out: BUILD_DATE
89# out: BUILD_INFO
90# out: BUILD_CPPFLAGS
91