253899c1b0eae814a35b73a38dffb5d4fcdfeec1
[dragonfly.git] / share / mk / bsd.crunchgen.mk
1 #
2 # Generate crunched binaries using crunchgen(1).
3 #
4 # Make variables used to generate the crunchgen(1) config file:
5 #
6 # CRUNCH_SRCDIRS        Directories to search for included programs
7 # CRUNCH_PATH_${D}      Path to the source directory ${D}
8 # CRUNCH_PROGS_${D}     Programs to be included inside directory ${D}
9 # CRUNCH_LIBS           Libraries to be statically linked with
10 # CRUNCH_SHLIBS         Libraries to be dynamically linked with
11 # CRUNCH_BUILDOPTS      Build options to be added for every program
12 # CRUNCH_CFLAGS         Compiler flags to be added for every program
13 # CRUNCH_LINKOPTS       Options to be added for linking the final binary
14 #
15 # Special options can be specified for individual programs:
16 #
17 # CRUNCH_SRCDIR_${P}    Base source directory for program ${P}
18 # CRUNCH_BUILDOPTS_${P} Additional build options for ${P}
19 # CRUNCH_CFLAGS_${P}    Additional compiler flags 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/soft 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', then no links will be generated.
28 # If CRUNCH_USE_SYMLINKS is defined, then soft links will be used instead
29 # of hard links.
30 #
31
32 # $FreeBSD: head/share/mk/bsd.crunchgen.mk 305257 2016-09-01 23:52:20Z bdrewery $
33
34
35 CONF=   ${PROG}.conf
36 OUTMK=  ${PROG}.mk
37 OUTC=   ${PROG}.c
38 OUTPUTS=${OUTMK} ${OUTC} ${PROG}.cache
39 CRUNCHOBJS= ${.OBJDIR}
40 CRUNCH_GENERATE_LINKS?= yes
41 CRUNCH_LINKTYPE?= hard
42 .if defined(CRUNCH_USE_SYMLINKS)
43 CRUNCH_LINKTYPE= soft
44 .endif
45
46 CLEANFILES+= ${CONF} *.o *.lo *.c *.mk *.cache *.a *.h
47
48 # Set a default SRCDIR for each for simpler handling below.
49 .for D in ${CRUNCH_SRCDIRS}
50 .for P in ${CRUNCH_PROGS_${D}}
51 CRUNCH_SRCDIR_${P}?=    ${CRUNCH_PATH_${D}}/${D}/${P}
52 .endfor
53 .endfor
54
55 # Program names and their aliases that contribute links to crunched
56 # executable, except for the suppressed ones.
57 .for D in ${CRUNCH_SRCDIRS}
58 .for P in ${CRUNCH_PROGS_${D}}
59 ${OUTPUTS}: ${CRUNCH_SRCDIR_${P}}/Makefile
60 .if ${CRUNCH_GENERATE_LINKS} == "yes"
61 .ifndef CRUNCH_SUPPRESS_LINK_${P}
62 .if ${CRUNCH_LINKTYPE} == "soft"
63 SYMLINKS+= ${PROG} ${BINDIR}/${P}
64 .else
65 LINKS+= ${BINDIR}/${PROG} ${BINDIR}/${P}
66 .endif
67 .endif   # !CRUNCH_SUPPRESS_LINK_${P}
68 .for A in ${CRUNCH_ALIAS_${P}}
69 .ifndef CRUNCH_SUPPRESS_LINK_${A}
70 .if ${CRUNCH_LINKTYPE} == "soft"
71 SYMLINKS+= ${PROG} ${BINDIR}/${A}
72 .else
73 LINKS+= ${BINDIR}/${PROG} ${BINDIR}/${A}
74 .endif
75 .endif   # !CRUNCH_SUPPRESS_LINK_${A}
76 .endfor  # CRUNCH_ALIAS_${P}
77 .endif   # CRUNCH_GENERATE_LINKS
78 .endfor  # CRUNCH_PROGS_${D}
79 .endfor  # CRUNCH_SRCDIRS
80
81 .if !defined(_SKIP_BUILD)
82 all: ${PROG}
83 .endif
84 exe: ${PROG}
85
86 ${CONF}: Makefile
87         echo "# Auto-generated, do not edit" >${.TARGET}
88 .ifdef CRUNCH_BUILDOPTS
89         echo "buildopts ${CRUNCH_BUILDOPTS}" >>${.TARGET}
90 .endif
91 .ifdef CRUNCH_CFLAGS
92         echo "buildopts CRUNCH_CFLAGS=\"${CRUNCH_CFLAGS}\"" >>${.TARGET}
93 .endif
94 .ifdef CRUNCH_LINKOPTS
95         echo "linkopts ${CRUNCH_LINKOPTS}" >>${.TARGET}
96 .endif
97 .ifdef CRUNCH_LIBS
98         echo "libs ${CRUNCH_LIBS}" >>${.TARGET}
99 .endif
100 .ifdef CRUNCH_SHLIBS
101         echo "libs_so ${CRUNCH_SHLIBS}" >>${.TARGET}
102 .endif
103 .for D in ${CRUNCH_SRCDIRS}
104 .for P in ${CRUNCH_PROGS_${D}}
105         echo "progs ${P}" >>${.TARGET}
106         echo "special ${P} srcdir ${CRUNCH_SRCDIR_${P}}" >>${.TARGET}
107 .ifdef CRUNCH_CFLAGS_${P}
108         echo "special ${P} buildopts \
109             DIRPRFX=${DIRPRFX}${P}/ \
110             ${CRUNCH_BUILDOPTS_${P}} \
111             CRUNCH_CFLAGS=\"${CRUNCH_CFLAGS_${P}}\"" >>${.TARGET}
112 .else
113         echo "special ${P} buildopts \
114             DIRPRFX=${DIRPRFX}${P}/ \
115             ${CRUNCH_BUILDOPTS_${P}}" >>${.TARGET}
116 .endif
117 .ifdef CRUNCH_KEEP_${P}
118         echo "special ${P} keep ${CRUNCH_KEEP_${P}}" >>${.TARGET}
119 .endif
120 .for A in ${CRUNCH_ALIAS_${P}}
121         echo "ln ${P} ${A}" >>${.TARGET}
122 .endfor
123 .endfor
124 .endfor
125
126 CRUNCHGEN?= crunchgen
127 CRUNCHENV?=     # empty
128 .ORDER: ${OUTPUTS} objs
129 ${OUTPUTS:[1]}: .META
130 ${OUTPUTS:[2..-1]}: .NOMETA
131 ${OUTPUTS}: ${CONF}
132         MAKE="${MAKE}" ${CRUNCHENV} MAKEOBJDIRPREFIX=${CRUNCHOBJS} \
133             ${CRUNCHGEN} -fq -m ${OUTMK} -c ${OUTC} ${CONF}
134         # Avoid redundantly calling 'make objs' which we've done by our
135         # own dependencies.
136         sed -i '' -e "/^${PROG}:/s/\$$[({]SUBMAKE_TARGETS[})]//" ${OUTMK}
137
138 # These 2 targets cannot use .MAKE since they depend on the generated
139 # ${OUTMK} above.
140 ${PROG}: ${OUTPUTS} objs .NOMETA .PHONY
141         ${CRUNCHENV} MAKEOBJDIRPREFIX=${CRUNCHOBJS} \
142             ${MAKE} .MAKE.MODE="${.MAKE.MODE} curdirOk=yes" \
143             .MAKE.META.IGNORE_PATHS="${.MAKE.META.IGNORE_PATHS}" \
144             -f ${OUTMK} exe
145
146 objs: ${OUTMK} .META
147         ${CRUNCHENV} MAKEOBJDIRPREFIX=${CRUNCHOBJS} \
148             ${MAKE} -f ${OUTMK} objs
149
150 # Use a separate build tree to hold files compiled for this crunchgen binary
151 # Yes, this does seem to partly duplicate <bsd.subdir.mk>, but I can't
152 # get that to cooperate with <bsd.prog.mk>.  Besides, many of the standard
153 # targets should NOT be propagated into the components.
154 __targets= clean cleandepend cleandir obj objlink depend
155 .for __target in ${__targets}
156 .for D in ${CRUNCH_SRCDIRS}
157 .for P in ${CRUNCH_PROGS_${D}}
158 ${__target}_crunchdir_${P}: .PHONY .MAKE
159         (cd ${CRUNCH_SRCDIR_${P}} && \
160             ${CRUNCHENV} MAKEOBJDIRPREFIX=${CANONICALOBJDIR} ${MAKE} \
161                 DIRPRFX=${DIRPRFX}${P}/ ${CRUNCH_BUILDOPTS} ${__target})
162 ${__target}: ${__target}_crunchdir_${P}
163 .endfor
164 .endfor
165 .endfor
166
167 clean:
168         rm -f ${CLEANFILES}
169         if [ -e ${.OBJDIR}/${OUTMK} ]; then                     \
170                 ${CRUNCHENV} MAKEOBJDIRPREFIX=${CRUNCHOBJS}     \
171                     ${MAKE} -f ${OUTMK} clean;                  \
172         fi
173
174 .ORDER: ${__targets} all install