bsd.crunchgen.mk revision 289870
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: head/share/mk/bsd.crunchgen.mk 289870 2015-10-24 04:55:17Z bdrewery $
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}
41.if defined(MAKEOBJDIRPREFIX)
42CANONICALOBJDIR:= ${MAKEOBJDIRPREFIX}${.CURDIR}
43.elif defined(MAKEOBJDIR) && ${MAKEOBJDIR:M/*} != ""
44CANONICALOBJDIR:= ${MAKEOBJDIR}
45.else
46CANONICALOBJDIR:= /usr/obj${.CURDIR}
47.endif
48CRUNCH_GENERATE_LINKS?= yes
49
50CLEANFILES+= ${CONF} *.o *.lo *.c *.mk *.cache *.a *.h
51
52# Don't try to extract debug info from ${PROG}.
53MK_DEBUG_FILES= no
54
55# Set a default SRCDIR for each for simpler handling below.
56.for D in ${CRUNCH_SRCDIRS}
57.for P in ${CRUNCH_PROGS_${D}}
58CRUNCH_SRCDIR_${P}?=	${.CURDIR}/../../${D}/${P}
59.endfor
60.endfor
61
62# Program names and their aliases contribute hardlinks to 'rescue' executable,
63# except for those that get suppressed.
64.for D in ${CRUNCH_SRCDIRS}
65.for P in ${CRUNCH_PROGS_${D}}
66${OUTPUTS}: ${CRUNCH_SRCDIR_${P}}/Makefile
67.if ${CRUNCH_GENERATE_LINKS} == "yes"
68.ifndef CRUNCH_SUPPRESS_LINK_${P}
69LINKS+= ${BINDIR}/${PROG} ${BINDIR}/${P}
70.endif
71.for A in ${CRUNCH_ALIAS_${P}}
72.ifndef CRUNCH_SUPPRESS_LINK_${A}
73LINKS+= ${BINDIR}/${PROG} ${BINDIR}/${A}
74.endif
75.endfor
76.endif
77.endfor
78.endfor
79
80all: ${PROG}
81exe: ${PROG}
82
83${CONF}: Makefile
84	echo \# Auto-generated, do not edit >${.TARGET}
85.ifdef CRUNCH_BUILDOPTS
86	echo buildopts ${CRUNCH_BUILDOPTS} >>${.TARGET}
87.endif
88.ifdef CRUNCH_LIBS
89	echo libs ${CRUNCH_LIBS} >>${.TARGET}
90.endif
91.ifdef CRUNCH_SHLIBS
92	echo libs_so ${CRUNCH_SHLIBS} >>${.TARGET}
93.endif
94.for D in ${CRUNCH_SRCDIRS}
95.for P in ${CRUNCH_PROGS_${D}}
96	echo progs ${P} >>${.TARGET}
97	echo special ${P} srcdir ${CRUNCH_SRCDIR_${P}} >>${.TARGET}
98.ifdef CRUNCH_BUILDOPTS_${P}
99	echo special ${P} buildopts DIRPRFX=${DIRPRFX}${P}/ \
100	    ${CRUNCH_BUILDOPTS_${P}} >>${.TARGET}
101.else
102	echo special ${P} buildopts DIRPRFX=${DIRPRFX}${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.ORDER: ${OUTPUTS} objs
113${OUTPUTS}: ${CONF} .META
114	MAKE=${MAKE} MAKEOBJDIRPREFIX=${CRUNCHOBJS} ${CRUNCHGEN} -fq \
115	    -m ${OUTMK} -c ${OUTC} ${CONF}
116
117# These 2 targets cannot use .MAKE since they depend on the generated
118# ${OUTMK} above.
119${PROG}: ${OUTPUTS} objs
120	${CRUNCHENV} MAKEOBJDIRPREFIX=${CRUNCHOBJS} ${MAKE} -f ${OUTMK} exe
121
122objs: ${OUTMK}
123	${CRUNCHENV} MAKEOBJDIRPREFIX=${CRUNCHOBJS} ${MAKE} -f ${OUTMK} objs
124
125# <sigh> Someone should replace the bin/csh and bin/sh build-tools with
126# shell scripts so we can remove this nonsense.
127.for _tool in ${CRUNCH_BUILDTOOLS}
128build-tools-${_tool}:
129	${_+_}cd ${.CURDIR}/../../${_tool}; \
130	    ${CRUNCHENV} MAKEOBJDIRPREFIX=${CRUNCHOBJS} ${MAKE} obj; \
131	    ${CRUNCHENV} MAKEOBJDIRPREFIX=${CRUNCHOBJS} ${MAKE} build-tools
132build-tools: build-tools-${_tool}
133.endfor
134
135# Use a separate build tree to hold files compiled for this crunchgen binary
136# Yes, this does seem to partly duplicate bsd.subdir.mk, but I can't
137# get that to cooperate with bsd.prog.mk.  Besides, many of the standard
138# targets should NOT be propagated into the components.
139.for __target in clean cleandepend cleandir obj objlink
140.for D in ${CRUNCH_SRCDIRS}
141.for P in ${CRUNCH_PROGS_${D}}
142${__target}_crunchdir_${P}: .PHONY .MAKE
143	${_+_}cd ${CRUNCH_SRCDIR_${P}} && \
144	    ${CRUNCHENV} MAKEOBJDIRPREFIX=${CANONICALOBJDIR} ${MAKE} \
145	    DIRPRFX=${DIRPRFX}${P}/ ${CRUNCH_BUILDOPTS} ${__target}
146${__target}: ${__target}_crunchdir_${P}
147.endfor
148.endfor
149.endfor
150
151clean:
152	rm -f ${CLEANFILES}
153	${_+_}if [ -e ${.OBJDIR}/${OUTMK} ]; then			\
154		${CRUNCHENV} MAKEOBJDIRPREFIX=${CRUNCHOBJS} ${MAKE} 	\
155		-f ${OUTMK} clean;					\
156	fi
157