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