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