xref: /freebsd/share/mk/bsd.crunchgen.mk (revision a4dc509f723944821bcfcc52005ff87c9a5dee5b)
1#################################################################
2#
3# General notes:
4#
5# A number of Make variables are used to generate the crunchgen config file.
6#
7#  CRUNCH_SRCDIRS: lists directories to search for included programs
8#  CRUNCH_PROGS:  lists programs to be included
9#  CRUNCH_LIBS:  libraries to statically link with
10#  CRUNCH_SHLIBS:  libraries to dynamically link with
11#  CRUNCH_BUILDOPTS: generic build options to be added to every program
12#  CRUNCH_BUILDTOOLS: lists programs that need build tools built in the
13#       local architecture.
14#
15# Special options can be specified for individual programs
16#  CRUNCH_SRCDIR_${P}: base source directory for program ${P}
17#  CRUNCH_BUILDOPTS_${P}: additional build options for ${P}
18#  CRUNCH_ALIAS_${P}: additional names to be used for ${P}
19#
20# By default, any name appearing in CRUNCH_PROGS or CRUNCH_ALIAS_${P}
21# will be used to generate a hard link to the resulting binary.
22# Specific links can be suppressed by setting
23# CRUNCH_SUPPRESS_LINK_${NAME} to 1.
24#
25# If CRUNCH_GENERATE_LINKS is set to no, no links will be generated.
26#
27
28# $FreeBSD$
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}
39.if defined(MAKEOBJDIRPREFIX)
40CANONICALOBJDIR:= ${MAKEOBJDIRPREFIX}${.CURDIR}
41.elif defined(MAKEOBJDIR) && ${MAKEOBJDIR:M/*} != ""
42CANONICALOBJDIR:= ${MAKEOBJDIR}
43.else
44CANONICALOBJDIR:= /usr/obj${.CURDIR}
45.endif
46CRUNCH_GENERATE_LINKS?= yes
47
48CLEANFILES+= ${CONF} *.o *.lo *.c *.mk *.cache *.a *.h
49
50# Don't try to extract debug info from ${PROG}.
51MK_DEBUG_FILES= no
52
53# Set a default SRCDIR for each for simpler handling below.
54.for D in ${CRUNCH_SRCDIRS}
55.for P in ${CRUNCH_PROGS_${D}}
56CRUNCH_SRCDIR_${P}?=	${.CURDIR}/../../${D}/${P}
57.endfor
58.endfor
59
60# Program names and their aliases contribute hardlinks to 'rescue' executable,
61# except for those that get suppressed.
62.for D in ${CRUNCH_SRCDIRS}
63.for P in ${CRUNCH_PROGS_${D}}
64${OUTPUTS}: ${CRUNCH_SRCDIR_${P}}/Makefile
65.if ${CRUNCH_GENERATE_LINKS} == "yes"
66.ifndef CRUNCH_SUPPRESS_LINK_${P}
67LINKS+= ${BINDIR}/${PROG} ${BINDIR}/${P}
68.endif
69.for A in ${CRUNCH_ALIAS_${P}}
70.ifndef CRUNCH_SUPPRESS_LINK_${A}
71LINKS+= ${BINDIR}/${PROG} ${BINDIR}/${A}
72.endif
73.endfor
74.endif
75.endfor
76.endfor
77
78all: ${PROG}
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.for A in ${CRUNCH_ALIAS_${P}}
103	echo ln ${P} ${A} >>${.TARGET}
104.endfor
105.endfor
106.endfor
107
108CRUNCHGEN?= crunchgen
109CRUNCHENV?= MK_TESTS=no
110.ORDER: ${OUTPUTS} objs
111${OUTPUTS}: ${CONF} .META
112	MAKE=${MAKE} MAKEOBJDIRPREFIX=${CRUNCHOBJS} ${CRUNCHGEN} -fq \
113	    -m ${OUTMK} -c ${OUTC} ${CONF}
114
115# These 2 targets cannot use .MAKE since they depend on the generated
116# ${OUTMK} above.
117${PROG}: ${OUTPUTS} objs
118	${CRUNCHENV} MAKEOBJDIRPREFIX=${CRUNCHOBJS} ${MAKE} -f ${OUTMK} exe
119
120objs: ${OUTMK}
121	${CRUNCHENV} MAKEOBJDIRPREFIX=${CRUNCHOBJS} ${MAKE} -f ${OUTMK} objs
122
123# <sigh> Someone should replace the bin/csh and bin/sh build-tools with
124# shell scripts so we can remove this nonsense.
125.for _tool in ${CRUNCH_BUILDTOOLS}
126build-tools-${_tool}:
127	${_+_}cd ${.CURDIR}/../../${_tool}; \
128	    ${CRUNCHENV} MAKEOBJDIRPREFIX=${CRUNCHOBJS} ${MAKE} obj; \
129	    ${CRUNCHENV} MAKEOBJDIRPREFIX=${CRUNCHOBJS} ${MAKE} build-tools
130build-tools: build-tools-${_tool}
131.endfor
132
133# Use a separate build tree to hold files compiled for this crunchgen binary
134# Yes, this does seem to partly duplicate bsd.subdir.mk, but I can't
135# get that to cooperate with bsd.prog.mk.  Besides, many of the standard
136# targets should NOT be propagated into the components.
137.for __target in clean cleandepend cleandir obj objlink
138.for D in ${CRUNCH_SRCDIRS}
139.for P in ${CRUNCH_PROGS_${D}}
140${__target}_crunchdir_${P}: .PHONY .MAKE
141	${_+_}cd ${CRUNCH_SRCDIR_${P}} && \
142	    ${CRUNCHENV} MAKEOBJDIRPREFIX=${CANONICALOBJDIR} ${MAKE} \
143	    DIRPRFX=${DIRPRFX}${P}/ ${CRUNCH_BUILDOPTS} ${__target}
144${__target}: ${__target}_crunchdir_${P}
145.endfor
146.endfor
147.endfor
148
149clean:
150	rm -f ${CLEANFILES}
151	${_+_}if [ -e ${.OBJDIR}/${OUTMK} ]; then			\
152		${CRUNCHENV} MAKEOBJDIRPREFIX=${CRUNCHOBJS} ${MAKE} 	\
153		-f ${OUTMK} clean;					\
154	fi
155