xref: /dragonfly/share/mk/bsd.crunchgen.mk (revision ee21086c)
1#
2# Generate crunched binaries using crunchgen(1).
3#
4# Make variables used to generate the crunchgen(1) config file:
5#
6# CRUNCH_SRCDIRS	Directories to search for included programs
7# CRUNCH_PROGS		Programs to be included
8# CRUNCH_LIBS		Libraries to be statically linked with
9# CRUNCH_SHLIBS		Libraries to be dynamically linked with
10# CRUNCH_BUILDOPTS	Build options to be added for every program
11# CRUNCH_BUILDTOOLS	Programs that need build tools built in the
12#			local architecture.
13#
14# Special options can be specified for individual programs:
15#
16# CRUNCH_SRCDIR_${P}	Base source directory for program ${P}
17# CRUNCH_BUILDOPTS_${P}	Additional build options for ${P}
18# CRUNCH_ALIAS_${P}	Additional names to be used for ${P}
19#
20# By default, any name appearing in CRUNCH_PROGS or CRUNCH_ALIAS_${P}
21# will be used to generate a hard link to the resulting binary.
22# Specific links can be suppressed by setting
23# CRUNCH_SUPPRESS_LINK_${NAME} to 1.
24#
25# If CRUNCH_GENERATE_LINKS is set to 'no', then no links will be generated.
26#
27
28# $FreeBSD: head/share/mk/bsd.crunchgen.mk 305257 2016-09-01 23:52:20Z bdrewery $
29
30
31CONF=	${PROG}.conf
32OUTMK=	${PROG}.mk
33OUTC=	${PROG}.c
34OUTPUTS=${OUTMK} ${OUTC} ${PROG}.cache
35CRUNCHOBJS= ${.OBJDIR}
36CRUNCH_GENERATE_LINKS?=	yes
37
38CLEANFILES+= ${CONF} *.o *.lo *.c *.mk *.cache *.a *.h
39
40# Set a default SRCDIR for each for simpler handling below.
41.for D in ${CRUNCH_SRCDIRS}
42.for P in ${CRUNCH_PROGS_${D}}
43CRUNCH_SRCDIR_${P}?=	${.CURDIR}/../../${D}/${P}
44.endfor
45.endfor
46
47# Program names and their aliases contribute hardlinks to 'rescue' executable,
48# except for those that get suppressed.
49.for D in ${CRUNCH_SRCDIRS}
50.for P in ${CRUNCH_PROGS_${D}}
51${OUTPUTS}: ${CRUNCH_SRCDIR_${P}}/Makefile
52.if ${CRUNCH_GENERATE_LINKS} == "yes"
53.ifndef CRUNCH_SUPPRESS_LINK_${P}
54LINKS+= ${BINDIR}/${PROG} ${BINDIR}/${P}
55.endif
56.for A in ${CRUNCH_ALIAS_${P}}
57.ifndef CRUNCH_SUPPRESS_LINK_${A}
58LINKS+= ${BINDIR}/${PROG} ${BINDIR}/${A}
59.endif
60.endfor
61.endif
62.endfor
63.endfor
64
65.if !defined(_SKIP_BUILD)
66all: ${PROG}
67.endif
68exe: ${PROG}
69
70${CONF}: Makefile
71	echo "# Auto-generated, do not edit" >${.TARGET}
72.ifdef CRUNCH_BUILDOPTS
73	echo "buildopts ${CRUNCH_BUILDOPTS}" >>${.TARGET}
74.endif
75.ifdef CRUNCH_LIBS
76	echo "libs ${CRUNCH_LIBS}" >>${.TARGET}
77.endif
78.ifdef CRUNCH_SHLIBS
79	echo "libs_so ${CRUNCH_SHLIBS}" >>${.TARGET}
80.endif
81.for D in ${CRUNCH_SRCDIRS}
82.for P in ${CRUNCH_PROGS_${D}}
83	echo "progs ${P}" >>${.TARGET}
84	echo "special ${P} srcdir ${CRUNCH_SRCDIR_${P}}" >>${.TARGET}
85.ifdef CRUNCH_BUILDOPTS_${P}
86	echo "special ${P} buildopts DIRPRFX=${DIRPRFX}${P}/ ${CRUNCH_BUILDOPTS_${P}}" >>${.TARGET}
87.else
88	echo "special ${P} buildopts DIRPRFX=${DIRPRFX}${P}/" >>${.TARGET}
89.endif
90.ifdef CRUNCH_KEEP_${P}
91	echo "special ${P} keep ${CRUNCH_KEEP_${P}}" >>${.TARGET}
92.endif
93.for A in ${CRUNCH_ALIAS_${P}}
94	echo "ln ${P} ${A}" >>${.TARGET}
95.endfor
96.endfor
97.endfor
98
99CRUNCHGEN?= crunchgen
100CRUNCHENV?=	# empty
101.ORDER: ${OUTPUTS} objs
102${OUTPUTS:[1]}: .META
103${OUTPUTS:[2..-1]}: .NOMETA
104${OUTPUTS}: ${CONF}
105	MAKE="${MAKE}" ${CRUNCHENV} MAKEOBJDIRPREFIX=${CRUNCHOBJS} \
106	    ${CRUNCHGEN} -fq -m ${OUTMK} -c ${OUTC} ${CONF}
107	# Avoid redundantly calling 'make objs' which we've done by our
108	# own dependencies.
109	sed -i '' -e "s/^\(${PROG}:.*\) \$$(SUBMAKE_TARGETS)/\1/" ${OUTMK}
110
111# These 2 targets cannot use .MAKE since they depend on the generated
112# ${OUTMK} above.
113${PROG}: ${OUTPUTS} objs .NOMETA .PHONY
114	${CRUNCHENV} MAKEOBJDIRPREFIX=${CRUNCHOBJS} \
115	    ${MAKE} .MAKE.MODE="${.MAKE.MODE} curdirOk=yes" \
116	    .MAKE.META.IGNORE_PATHS="${.MAKE.META.IGNORE_PATHS}" \
117	    -f ${OUTMK} exe
118
119objs: ${OUTMK} .META
120	${CRUNCHENV} MAKEOBJDIRPREFIX=${CRUNCHOBJS} \
121	    ${MAKE} -f ${OUTMK} objs
122
123# <sigh> Someone should replace the bin/csh build-tools with
124# shell scripts so we can remove this nonsense.
125.for _tool in ${CRUNCH_BUILDTOOLS}
126build-tools-${_tool}:
127	${_+_}cd ${.CURDIR}/../../../${_tool}; \
128	    ${CRUNCHENV} MAKEOBJDIRPREFIX=${CRUNCHOBJS} ${MAKE} obj; \
129	    ${CRUNCHENV} MAKEOBJDIRPREFIX=${CRUNCHOBJS} ${MAKE} build-tools
130build-tools: build-tools-${_tool}
131.endfor
132
133# Use a separate build tree to hold files compiled for this crunchgen binary
134# Yes, this does seem to partly duplicate bsd.subdir.mk, but I can't
135# get that to cooperate with bsd.prog.mk.  Besides, many of the standard
136# targets should NOT be propagated into the components.
137.for __target in clean cleandepend cleandir obj objlink
138.for D in ${CRUNCH_SRCDIRS}
139.for P in ${CRUNCH_PROGS_${D}}
140${__target}_crunchdir_${P}: .PHONY .MAKE
141	${_+_}cd ${CRUNCH_SRCDIR_${P}} && \
142	    ${CRUNCHENV} MAKEOBJDIRPREFIX=${CANONICALOBJDIR} ${MAKE} \
143	    DIRPRFX=${DIRPRFX}${P}/ ${CRUNCH_BUILDOPTS} ${__target}
144${__target}: ${__target}_crunchdir_${P}
145.endfor
146.endfor
147.endfor
148
149clean:
150	rm -f ${CLEANFILES}
151	${_+_}if [ -e ${.OBJDIR}/${OUTMK} ]; then		\
152		${CRUNCHENV} MAKEOBJDIRPREFIX=${CRUNCHOBJS}	\
153		    ${MAKE} -f ${OUTMK} clean;			\
154	fi
155