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