1#################################################################
2#
3# Generate crunched binaries using crunchgen(1).
4#
5# General notes:
6#
7# A number of Make variables are used to generate the crunchgen config file.
8#
9#  CRUNCH_SRCDIRS: lists directories to search for included programs
10#  CRUNCH_PROGS:  lists programs to be included
11#  CRUNCH_LIBS:  libraries to statically link with
12#  CRUNCH_SHLIBS:  libraries to dynamically link with
13#  CRUNCH_BUILDOPTS: generic build options to be added to every program
14#  CRUNCH_BUILDTOOLS: lists programs that need build tools built in the
15#       local architecture.
16#
17# Special options can be specified for individual programs
18#  CRUNCH_SRCDIR_${P}: base source directory for program ${P}
19#  CRUNCH_BUILDOPTS_${P}: additional build options for ${P}
20#  CRUNCH_ALIAS_${P}: additional names to be used for ${P}
21#  CRUNCH_KEEP_${P}: additional symbols to keep for ${P}
22#
23# By default, any name appearing in CRUNCH_PROGS or CRUNCH_ALIAS_${P}
24# will be used to generate a hard link to the resulting binary.
25# Specific links can be suppressed by setting
26# CRUNCH_SUPPRESS_LINK_${NAME} to 1.
27#
28# If CRUNCH_GENERATE_LINKS is set to no, no links will be generated.
29#
30
31##################################################################
32#  The following is pretty nearly a generic crunchgen-handling makefile
33#
34
35CONF=	${PROG}.conf
36OUTMK=	${PROG}.mk
37OUTC=	${PROG}.c
38OUTPUTS=${OUTMK} ${OUTC} ${PROG}.cache
39CRUNCHOBJS= ${.OBJDIR}
40CRUNCH_GENERATE_LINKS?= yes
41# Don't let the prog.mk use MK_AUTO_OBJ, but do let the component builds use
42# it.
43CRUNCHARGS+= MK_AUTO_OBJ=no
44CRUNCH_BUILDOPTS+= MK_AUTO_OBJ=${MK_AUTO_OBJ}
45
46CLEANFILES+= ${CONF} *.o *.lo *.c *.mk *.cache *.a *.h
47
48# Don't try to extract debug info from ${PROG}.
49MK_DEBUG_FILES= no
50
51# Set a default SRCDIR for each for simpler handling below.
52.for D in ${CRUNCH_SRCDIRS}
53.for P in ${CRUNCH_PROGS_${D}}
54CRUNCH_SRCDIR_${P}?=	${.CURDIR}/../../${D}/${P}
55.endfor
56.endfor
57
58# Program names and their aliases contribute hardlinks to 'rescue' executable,
59# except for those that get suppressed.
60.for D in ${CRUNCH_SRCDIRS}
61.for P in ${CRUNCH_PROGS_${D}}
62${OUTPUTS}: ${CRUNCH_SRCDIR_${P}}/Makefile
63.if ${CRUNCH_GENERATE_LINKS} == "yes"
64.ifndef CRUNCH_SUPPRESS_LINK_${P}
65LINKS+= ${BINDIR}/${PROG} ${BINDIR}/${P}
66.endif
67.for A in ${CRUNCH_ALIAS_${P}}
68.ifndef CRUNCH_SUPPRESS_LINK_${A}
69LINKS+= ${BINDIR}/${PROG} ${BINDIR}/${A}
70.endif
71.endfor
72.endif
73.endfor
74.endfor
75
76.if !defined(_SKIP_BUILD)
77all: ${PROG}
78.endif
79exe: ${PROG}
80
81${CONF}: Makefile
82	echo \# Auto-generated, do not edit >${.TARGET}
83.ifdef CRUNCH_BUILDOPTS
84	echo buildopts ${CRUNCH_BUILDOPTS} >>${.TARGET}
85.endif
86.ifdef CRUNCH_LIBS
87	echo libs ${CRUNCH_LIBS} >>${.TARGET}
88.endif
89.ifdef CRUNCH_SHLIBS
90	echo libs_so ${CRUNCH_SHLIBS} >>${.TARGET}
91.endif
92.for D in ${CRUNCH_SRCDIRS}
93.for P in ${CRUNCH_PROGS_${D}}
94	echo progs ${P} >>${.TARGET}
95	echo special ${P} srcdir ${CRUNCH_SRCDIR_${P}} >>${.TARGET}
96.ifdef CRUNCH_BUILDOPTS_${P}
97	echo special ${P} buildopts DIRPRFX=${DIRPRFX}${P}/ \
98	    ${CRUNCH_BUILDOPTS_${P}} >>${.TARGET}
99.else
100	echo special ${P} buildopts DIRPRFX=${DIRPRFX}${P}/ >>${.TARGET}
101.endif
102.ifdef CRUNCH_LIBS_${P}
103	echo special ${P} lib ${CRUNCH_LIBS_${P}} >>${.TARGET}
104.endif
105.ifdef CRUNCH_KEEP_${P}
106	echo special ${P} keep ${CRUNCH_KEEP_${P}} >>${.TARGET}
107.endif
108.for A in ${CRUNCH_ALIAS_${P}}
109	echo ln ${P} ${A} >>${.TARGET}
110.endfor
111.endfor
112.endfor
113
114CRUNCHGEN?= crunchgen
115CRUNCHENV+= MK_TESTS=no \
116	    UPDATE_DEPENDFILE=no \
117	    _RECURSING_CRUNCH=1
118.ORDER: ${OUTPUTS} objs
119${OUTPUTS:[1]}: .META
120${OUTPUTS:[2..-1]}: .NOMETA
121${OUTPUTS}: ${CONF}
122	MAKE="${MAKE}" ${CRUNCHENV} MAKEOBJDIRPREFIX=${CRUNCHOBJS} \
123	    MK_AUTO_OBJ=${MK_AUTO_OBJ} \
124	    ${CRUNCHGEN} -fq -m ${OUTMK} -c ${OUTC} ${CONF}
125	# Avoid redundantly calling 'make objs' which we've done by our
126	# own dependencies.
127	sed -i '' \
128	    -e "s/^\(${PROG}:.*\) \$$(SUBMAKE_TARGETS)/\1/" \
129	    ${OUTMK}
130
131# These 2 targets cannot use .MAKE since they depend on the generated
132# ${OUTMK} above.
133${PROG}: ${OUTPUTS} objs .NOMETA .PHONY
134	${CRUNCHENV} \
135	    CC="${CC} ${CFLAGS} ${LDFLAGS}" \
136	    CXX="${CXX} ${CXXFLAGS} ${LDFLAGS}" \
137	    ${MAKE} ${CRUNCHARGS} .MAKE.MODE="${.MAKE.MODE} curdirOk=yes" \
138	    .MAKE.META.IGNORE_PATHS="${.MAKE.META.IGNORE_PATHS}" \
139	    -f ${OUTMK} exe
140
141objs: ${OUTMK} .META
142	${CRUNCHENV} MAKEOBJDIRPREFIX=${CRUNCHOBJS} \
143	    ${MAKE} -f ${OUTMK} ${CRUNCHARGS} BUILD_TOOLS_META=.NOMETA objs
144
145# <sigh> Someone should replace the bin/csh and bin/sh build-tools with
146# shell scripts so we can remove this nonsense.
147.for _tool in ${CRUNCH_BUILDTOOLS}
148build-tools-${_tool}:
149	${_+_}cd ${.CURDIR}/../../${_tool}; \
150	    if [ "${MK_AUTO_OBJ}" = "no" ]; then \
151	        ${CRUNCHENV} MAKEOBJDIRPREFIX=${CRUNCHOBJS} ${MAKE} obj; \
152	    fi; \
153	    ${CRUNCHENV} MAKEOBJDIRPREFIX=${CRUNCHOBJS} ${MAKE} build-tools
154build-tools: build-tools-${_tool}
155.endfor
156
157# Use a separate build tree to hold files compiled for this crunchgen binary
158# Yes, this does seem to partly duplicate bsd.subdir.mk, but I can't
159# get that to cooperate with bsd.prog.mk.  Besides, many of the standard
160# targets should NOT be propagated into the components.
161.if ${MK_AUTO_OBJ} == "no"
162_obj=	obj
163.endif
164.for __target in clean cleandepend cleandir ${_obj} objlink
165.for D in ${CRUNCH_SRCDIRS}
166.for P in ${CRUNCH_PROGS_${D}}
167${__target}_crunchdir_${P}: .PHONY .MAKE
168	${_+_}cd ${CRUNCH_SRCDIR_${P}} && \
169	    ${CRUNCHENV} MAKEOBJDIRPREFIX=${CANONICALOBJDIR} ${MAKE} \
170	    ${CRUNCHARGS} \
171	    DIRPRFX=${DIRPRFX}${P}/ ${CRUNCH_BUILDOPTS} \
172	    ${CRUNCH_BUILDOPTS_${P}} ${__target}
173${__target}: ${__target}_crunchdir_${P}
174.endfor
175.endfor
176.endfor
177
178clean:
179	rm -f ${CLEANFILES}
180	${_+_}if [ -e ${.OBJDIR}/${OUTMK} ]; then			\
181		${CRUNCHENV} MAKEOBJDIRPREFIX=${CRUNCHOBJS} ${MAKE} 	\
182		-f ${OUTMK} clean;					\
183	fi
184
185META_XTRAS+=	${find ${CRUNCHOBJS}${SRCTOP} -name '*.meta' 2>/dev/null || true:L:sh}
186META_XTRAS+=	${echo ${CRUNCHOBJS}/*.lo.meta 2>/dev/null || true:L:sh}
187META_XTRAS+=	${PROG}.meta
188