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: releng/10.3/share/mk/bsd.crunchgen.mk 291786 2015-12-04 18:04:09Z 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# Program names and their aliases contribute hardlinks to 'rescue' executable,
56# except for those that get suppressed.
57.for D in $(CRUNCH_SRCDIRS)
58.for P in $(CRUNCH_PROGS_$(D))
59.ifdef CRUNCH_SRCDIR_${P}
60$(OUTPUTS): $(CRUNCH_SRCDIR_${P})/Makefile
61.else
62$(OUTPUTS): $(.CURDIR)/../../$(D)/$(P)/Makefile
63.endif
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
77all: $(PROG)
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.ifdef CRUNCH_SRCDIR_${P}
95	echo special $(P) srcdir $(CRUNCH_SRCDIR_${P}) >>$(.TARGET)
96.else
97	echo special $(P) srcdir $(.CURDIR)/../../$(D)/$(P) >>$(.TARGET)
98.endif
99.ifdef CRUNCH_BUILDOPTS_${P}
100	echo special $(P) buildopts DIRPRFX=${DIRPRFX}${P}/ \
101	    $(CRUNCH_BUILDOPTS_${P}) >>$(.TARGET)
102.else
103	echo special $(P) buildopts DIRPRFX=${DIRPRFX}${P}/ >>$(.TARGET)
104.endif
105.for A in $(CRUNCH_ALIAS_$(P))
106	echo ln $(P) $(A) >>$(.TARGET)
107.endfor
108.endfor
109.endfor
110
111# XXX Make sure we don't pass -P to crunchgen(1).
112.MAKEFLAGS:= ${.MAKEFLAGS:N-P}
113.ORDER: $(OUTPUTS) objs
114$(OUTPUTS): $(CONF)
115	MAKE=${MAKE} MAKEOBJDIRPREFIX=${CRUNCHOBJS} crunchgen -fq -m $(OUTMK) \
116	    -c $(OUTC) $(CONF)
117
118$(PROG): $(OUTPUTS) objs
119	MAKEOBJDIRPREFIX=${CRUNCHOBJS} ${MAKE} -f $(OUTMK) exe
120
121objs: $(OUTMK)
122	MAKEOBJDIRPREFIX=${CRUNCHOBJS} ${MAKE} -f $(OUTMK) objs
123
124# <sigh> Someone should replace the bin/csh and bin/sh build-tools with
125# shell scripts so we can remove this nonsense.
126build-tools:
127.for _tool in $(CRUNCH_BUILDTOOLS)
128	cd $(.CURDIR)/../../${_tool}; \
129	MAKEOBJDIRPREFIX=${CRUNCHOBJS} ${MAKE} obj; \
130	MAKEOBJDIRPREFIX=${CRUNCHOBJS} ${MAKE} build-tools
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.
137cleandepend cleandir obj objlink:
138.for D in $(CRUNCH_SRCDIRS)
139.for P in $(CRUNCH_PROGS_$(D))
140.ifdef CRUNCH_SRCDIR_${P}
141	cd ${CRUNCH_SRCDIR_$(P)} && \
142	    MAKEOBJDIRPREFIX=${CANONICALOBJDIR} ${MAKE} \
143	    DIRPRFX=${DIRPRFX}${P}/ ${CRUNCH_BUILDOPTS} ${.TARGET}
144.else
145	cd $(.CURDIR)/../../${D}/${P} && \
146	    MAKEOBJDIRPREFIX=${CANONICALOBJDIR} ${MAKE} \
147	    DIRPRFX=${DIRPRFX}${P}/ ${CRUNCH_BUILDOPTS} ${.TARGET}
148.endif
149.endfor
150.endfor
151
152clean:
153	rm -f ${CLEANFILES}
154	if [ -e ${.OBJDIR}/$(OUTMK) ]; then				\
155		MAKEOBJDIRPREFIX=${CRUNCHOBJS} ${MAKE} -f $(OUTMK) clean;	\
156	fi
157.for D in $(CRUNCH_SRCDIRS)
158.for P in $(CRUNCH_PROGS_$(D))
159.ifdef CRUNCH_SRCDIR_${P}
160	cd ${CRUNCH_SRCDIR_$(P)} && \
161	    MAKEOBJDIRPREFIX=${CANONICALOBJDIR} ${MAKE} \
162	    DIRPRFX=${DIRPRFX}${P}/ ${CRUNCH_BUILDOPTS} ${.TARGET}
163.else
164	cd $(.CURDIR)/../../${D}/${P} && \
165	    MAKEOBJDIRPREFIX=${CANONICALOBJDIR} ${MAKE} \
166	    DIRPRFX=${DIRPRFX}${P}/ ${CRUNCH_BUILDOPTS} ${.TARGET}
167.endif
168.endfor
169.endfor
170