1#	$NetBSD: Makefile.crunch,v 1.27 2022/05/11 10:45:48 rin Exp $
2#
3# Makefile snippet to build a crunchgen(1)ed binary from the provided lists
4#
5# Required variables:
6#	NETBSDSRCDIR	top level of src tree (set by <bsd.own.mk>)
7#	CRUNCHBIN	name of crunchgen(1)ed binary
8#	LISTS		list file(s) to use
9#
10# Optional variables:
11#	SMALLPROG	if != 0, add SMALLPROG=1 to CRUNCHENV.  [default: 1]
12#	SMALLPROG_INET6	if != 0 and SMALLPROG != 0, and ${USE_INET6} != "no",
13#			add SMALLPROG_INET6=1 to CRUNCHENV.  [default: 0]
14#	CRUNCHENV	environment to pass to crunchgen(1) and when building
15#			the crunched program with make(1) (as command line
16#			variables, to override any user provided environment
17#			or make(1) cmdline vars).
18#	CRUNCHGEN_FLAGS	extra options to crunchgen(1)
19#	DESTDIR		destination directory
20#	PARSELISTENV	environment variables to set for parselist.awk
21#
22# Variables modified by this:
23#	CRUNCHENV	may get SMALLPROG=1 or SMALLPROG_INET6=1 added
24#
25
26.if !defined(_MAKEFILE_CRUNCH_)
27_MAKEFILE_CRUNCH_=1
28
29DBG?=-Os
30CRUNCHGEN_FLAGS?=\
31-V LDSTATIC=-static \
32-V DBG="${DBG}" \
33-V NOMAN= \
34-V NOLIBCSANITIZER= \
35-V NOSANITIZER= \
36-V NOPIE= \
37-V NOSSP= \
38-V NOFORT=
39
40CRUNCHENV+=		AWK=${TOOL_AWK:Q}
41
42SMALLPROG?=		1
43SMALLPROG_INET6?=	0
44NOIEEE80211?=		0
45.if ${SMALLPROG}				# {
46CRUNCHENV+=		SMALLPROG=1
47.if ${SMALLPROG_INET6} && (${USE_INET6} != "no")
48CRUNCHENV+=		SMALLPROG_INET6=1
49.endif
50.if ${NOIEEE80211}
51CRUNCHENV+=		NOIEEE80211=1
52.endif
53.endif						# }
54
55.include "${DISTRIBDIR}/common/Makefile.parselist"
56
57.PHONY:	${CRUNCHBIN}
58${CRUNCHBIN}: ${CRUNCHBIN}.mk ${CRUNCHBIN}.cache ${CRUNCHBIN}.c
59	${MAKE} ${CRUNCHENV} -f ${CRUNCHBIN}.mk ${CRUNCHBIN}.crunched
60
61${CRUNCHBIN}.c: ${CRUNCHBIN}.mk
62	[ ! -f ${.TARGET} ] || touch ${.TARGET}
63
64${CRUNCHBIN}.cache: ${CRUNCHBIN}.mk
65	[ ! -f ${.TARGET} ] || touch ${.TARGET}
66
67CRUNCHGEN != command -v ${TOOL_CRUNCHGEN:[-1]} || echo
68
69${CRUNCHBIN}.mk: ${CRUNCHBIN}.conf  ${CRUNCHGEN}
70	${CRUNCHENV} ${TOOL_CRUNCHGEN} -f -D ${NETBSDSRCDIR} \
71	    -L ${DESTDIR}/usr/lib -q ${CRUNCHGEN_FLAGS} ${CRUNCHBIN}.conf
72
73${CRUNCHBIN}.conf: ${LISTS} ${PARSELISTDEP}
74	-rm -f ${.TARGET} ${.TARGET}.tmp
75	${PARSELIST} -v mode=crunch ${LISTS} > ${.TARGET}.tmp \
76	&& mv ${.TARGET}.tmp ${.TARGET}
77
78CLEANFILES+=	${CRUNCHBIN} ${CRUNCHBIN}.conf ${CRUNCHBIN}.conf.tmp \
79		${CRUNCHBIN}.cache *.o *.cro *.c
80
81clean cleandir distclean: cleancrunchgen
82
83.PHONY: cleancrunchgen
84
85cleancrunchgen:
86	if [ -f ${CRUNCHBIN}.mk ]; then \
87		${MAKE} -f ${CRUNCHBIN}.mk clean; \
88	fi
89	rm -f ${CRUNCHBIN}.mk
90
91
92.endif	# _MAKEFILE_CRUNCH_
93