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