1#         $NetBSD: bsd.man.mk,v 1.122 2020/11/10 21:47:49 kamil Exp $
2#         @(#)bsd.man.mk      8.1 (Berkeley) 6/8/93
3
4.include <bsd.init.mk>
5
6##### Basic targets
7.PHONY:             catinstall maninstall catpages manpages catlinks manlinks
8.PHONY:             htmlinstall htmlpages htmllinks
9.PHONY:             lintmanpages
10realinstall:        ${MANINSTALL}
11
12# If our install destination is case-preserving, but case-insensitive
13# then we do filesystem comparisons in lower case to make sure that
14# we always refresh the target when needed. In general we don't
15# want to do this, otherwise things like _exit.2 -> _Exit.2 get
16# installed on each build even when they don't need to. Note that
17# the CASE_INSENSITIVE_DEST macro is currently not defined anywhere,
18# and the expansion does not really work because of make(1).
19.if defined(CASE_INSENSITIVE_DEST)
20_FLATTEN?=tl:
21.endif
22
23##### Default values
24.if ${USETOOLS} == "yes"
25TMACDEPDIR?=        ${TOOLDIR}/share/groff/tmac
26.else
27TMACDEPDIR?=        /usr/share/tmac
28.endif
29
30HTMLDIR?= ${DESTDIR}${MANDIR}
31.if ${MKMANDOC} == yes && !defined(NOMANDOC)
32CATDEPS?=
33.else
34CATDEPS?= ${TMACDEPDIR}/andoc.tmac \
35                    ${TMACDEPDIR}/doc.tmac \
36                    ${TMACDEPDIR}/mdoc/doc-common \
37                    ${TMACDEPDIR}/mdoc/doc-ditroff \
38                    ${TMACDEPDIR}/mdoc/doc-nroff \
39                    ${TMACDEPDIR}/mdoc/doc-syms
40.endif
41MANTARGET?=         cat
42
43MAN?=
44MLINKS?=
45_MSECTIONS=         1 2 3 4 5 6 7 8 9
46_MSECTIONS+=        3lua 9lua
47_MSECTIONREGEX=     ${_MSECTIONS:ts|} # e.g. 1|2|3|...
48.SUFFIXES:          ${_MSECTIONS:@N@.$N@}
49
50.if ${MKMANZ} == "no"
51MANCOMPRESS?=
52MANSUFFIX?=
53.else
54MANCOMPRESS?=       ${TOOL_GZIP_N} -cf
55MANSUFFIX?=         .gz
56.endif
57
58# make MANCOMPRESS a filter, so it can be inserted on an as-needed basis
59.if !empty(MANCOMPRESS)
60MANCOMPRESS:=       | ${MANCOMPRESS}
61.endif
62
63__installpage: .USE
64          @cmp -s ${.ALLSRC} ${.TARGET} > /dev/null 2>&1 || \
65              (${_MKSHMSG_INSTALL} ${.TARGET}; \
66               ${_MKSHECHO} "${INSTALL_FILE} -o ${MANOWN} -g ${MANGRP} -m ${MANMODE} \
67                    ${.ALLSRC} ${.TARGET}" && \
68               ${INSTALL_FILE} -o ${MANOWN} -g ${MANGRP} -m ${MANMODE} \
69                    ${.ALLSRC} ${.TARGET})
70
71# XXX consider including bsd.links.mk and using __linkinstall instead
72__linkinstallpage: .USE
73          ${_MKSHMSG_INSTALL} ${.TARGET}; \
74          ${_MKSHECHO} "${INSTALL_LINK} -o ${MANOWN} -g ${MANGRP} -m ${MANMODE} \
75              ${.ALLSRC} ${.TARGET}" && \
76          ${INSTALL_LINK} -o ${MANOWN} -g ${MANGRP} -m ${MANMODE} \
77              ${.ALLSRC} ${.TARGET}
78
79##### Build and install rules (source form pages)
80
81.if ${MKMAN} != "no"
82maninstall:         manpages manlinks
83manpages::          # ensure target exists
84MANPAGES= ${MAN:C/.$/&${MANSUFFIX}/}
85
86realall:  ${MANPAGES}
87.if !empty(MANSUFFIX)
88.NOPATH:  ${MANPAGES}
89.SUFFIXES:          ${_MSECTIONS:@N@.$N${MANSUFFIX}@}
90
91${_MSECTIONS:@N@.$N.$N${MANSUFFIX}@}:                       # build rule
92          ${_MKTARGET_FORMAT}
93          cat ${.IMPSRC} ${MANCOMPRESS} > ${.TARGET}.tmp && ${MV} ${.TARGET}.tmp ${.TARGET}
94.endif # !empty(MANSUFFIX)
95
96.for F in ${MANPAGES:S/${MANSUFFIX}$//:O:u}
97_F:=                ${DESTDIR}${MANDIR}/man${F:T:E}${MANSUBDIR}/${F}${MANSUFFIX}
98
99.if ${MKUPDATE} == "no"
100${_F}!              ${F}${MANSUFFIX} __installpage                    # install rule
101.if !defined(BUILD) && !make(all) && !make(${F})
102${_F}!              .MADE                                             # no build at install
103.endif
104.else
105${_F}:              ${F}${MANSUFFIX} __installpage                    # install rule
106.if !defined(BUILD) && !make(all) && !make(${F})
107${_F}:              .MADE                                             # no build at install
108.endif
109.endif
110
111manpages::          ${_F}
112.PRECIOUS:          ${_F}                                             # keep if install fails
113.endfor
114
115manlinks::                                                            # link install
116
117.for _src _dst in ${MLINKS}
118_l:=${DESTDIR}${MANDIR}/man${_src:T:E}${MANSUBDIR}/${_src}${MANSUFFIX}
119_t:=${DESTDIR}${MANDIR}/man${_dst:T:E}${MANSUBDIR}/${_dst}${MANSUFFIX}
120
121# Handle case conflicts carefully, when _dst occurs
122# more than once after case flattening
123.if ${MKUPDATE} == "no" || ${MLINKS:${_FLATTEN}M${_dst:${_FLATTEN}Q}:[\#]} > 1
124${_t}!              ${_l} __linkinstallpage
125.else
126${_t}:              ${_l} __linkinstallpage
127.endif
128
129manlinks::          ${_t}
130.PRECIOUS:          ${_t}
131.endfor
132.endif # ${MKMAN} != "no"
133
134##### Build and install rules (plaintext pages)
135
136.if (${MKCATPAGES} != "no") && (${MKMAN} != "no")
137catinstall:         catpages catlinks
138catpages::          # ensure target exists
139CATPAGES= ${MAN:C/\.(${_MSECTIONREGEX})\$/.cat\1${MANSUFFIX}/}
140
141realall:  ${CATPAGES}
142.NOPATH:  ${CATPAGES}
143.SUFFIXES:          ${_MSECTIONS:@N@.cat$N${MANSUFFIX}@}
144.MADE:    ${CATDEPS}
145
146${_MSECTIONS:@N@.$N.cat$N${MANSUFFIX}@}: ${CATDEPS}         # build rule
147          ${_MKTARGET_FORMAT}
148.if ${MKMANDOC} == yes && !defined(NOMANDOC)
149          if test ""${NOMANDOC.${.IMPSRC:T}:tl:Q} != "yes"; then \
150                    ${TOOL_MANDOC_ASCII} ${.IMPSRC} ${MANCOMPRESS} \
151                        > ${.TARGET}.tmp && ${MV} ${.TARGET}.tmp ${.TARGET}; \
152          else \
153                    ${TOOL_ROFF_ASCII} -mandoc ${.IMPSRC} ${MANCOMPRESS} \
154                        > ${.TARGET}.tmp && ${MV} ${.TARGET}.tmp ${.TARGET}; \
155          fi
156.elif defined(USETBL)
157          ${TOOL_TBL} ${.IMPSRC} | ${TOOL_ROFF_ASCII} -mandoc ${MANCOMPRESS} \
158              > ${.TARGET}.tmp && ${MV} ${.TARGET}.tmp ${.TARGET}
159.else
160          ${TOOL_ROFF_ASCII} -mandoc ${.IMPSRC} ${MANCOMPRESS} \
161              > ${.TARGET}.tmp && ${MV} ${.TARGET}.tmp ${.TARGET}
162.endif
163
164.for F in ${CATPAGES:S/${MANSUFFIX}$//:O:u}
165_F:=                ${DESTDIR}${MANDIR}/${F:T:E}${MANSUBDIR}/${F:R}.0${MANSUFFIX}
166
167.if ${MKUPDATE} == "no"
168${_F}!              ${F}${MANSUFFIX} __installpage                    # install rule
169.if !defined(BUILD) && !make(all) && !make(${F})
170${_F}!              .MADE                                             # no build at install
171.endif
172.else
173${_F}:              ${F}${MANSUFFIX} __installpage                    # install rule
174.if !defined(BUILD) && !make(all) && !make(${F})
175${_F}:              .MADE                                             # no build at install
176.endif
177.endif
178
179catpages::          ${_F}
180.PRECIOUS:          ${_F}                                             # keep if install fails
181.endfor
182
183catlinks::                                                            # link install
184
185.for _src _dst in ${MLINKS}
186_l:=${DESTDIR}${MANDIR}/cat${_src:T:E}${MANSUBDIR}/${_src:R}.0${MANSUFFIX}
187_t:=${DESTDIR}${MANDIR}/cat${_dst:T:E}${MANSUBDIR}/${_dst:R}.0${MANSUFFIX}
188
189# Handle case conflicts carefully, when _dst occurs
190# more than once after case flattening
191.if ${MKUPDATE} == "no" || ${MLINKS:${_FLATTEN}M${_dst:${_FLATTEN}Q}:[\#]} > 1
192${_t}!              ${_l} __linkinstallpage
193.else
194${_t}:              ${_l} __linkinstallpage
195.endif
196
197catlinks::          ${_t}
198.PRECIOUS:          ${_t}
199.endfor
200.endif # (${MKCATPAGES} != "no") && (${MKMAN} != "no")
201
202##### Build and install rules (HTML pages)
203
204.if (${MKHTML} != "no") && (${MKMAN} != "no")               # {
205htmlinstall:        htmlpages htmllinks
206htmlpages::         # ensure target exists
207HTMLPAGES=          ${MAN:C/\.(${_MSECTIONREGEX})\$/.html\1/}
208
209HTMLLINKS=          ${MANSUBDIR:?../:}../html%S/%N.html
210HTMLSTYLE=          ${MANSUBDIR:?../:}../style.css
211
212realall:  ${HTMLPAGES}
213.NOPATH:  ${HTMLPAGES}
214.SUFFIXES:          ${_MSECTIONS:@N@.html$N@}
215
216${_MSECTIONS:@N@.$N.html$N@}:                                         # build rule
217          ${_MKTARGET_FORMAT}
218.if ${MKMANDOC} == yes && !defined(NOMANDOC)
219          if test ""${NOMANDOC.${.IMPSRC:T}:tl:Q} != "yes"; then \
220              ${TOOL_MANDOC_HTML} -Oman=${HTMLLINKS},style=${HTMLSTYLE} \
221                    ${.IMPSRC} > ${.TARGET}.tmp && \
222                    ${MV} ${.TARGET}.tmp ${.TARGET}; \
223          else \
224                    ${TOOL_ROFF_HTML} ${.IMPSRC} ${MANCOMPRESS} \
225                        > ${.TARGET}.tmp && ${MV} ${.TARGET}.tmp ${.TARGET}; \
226          fi
227.elif defined(USETBL)
228          ${TOOL_TBL} ${.IMPSRC} | ${TOOL_ROFF_HTML} ${MANCOMPRESS} \
229              > ${.TARGET}.tmp && ${MV} ${.TARGET}.tmp ${.TARGET}
230.else
231          ${TOOL_ROFF_HTML} ${.IMPSRC} ${MANCOMPRESS} \
232              > ${.TARGET}.tmp && ${MV} ${.TARGET}.tmp ${.TARGET}
233.endif
234
235.for F in ${HTMLPAGES:O:u}
236# construct installed path
237_F:=                ${HTMLDIR}/${F:T:E}${MANSUBDIR}/${F:R:S-/index$-/x&-}.html
238
239.if ${MKUPDATE} == "no"
240${_F}!              ${F} __installpage                      # install rule
241.if !defined(BUILD) && !make(all) && !make(${F})
242${_F}!              .MADE                                             # no build at install
243.endif
244.else
245${_F}:              ${F} __installpage                      # install rule
246.if !defined(BUILD) && !make(all) && !make(${F})
247${_F}:              .MADE                                             # no build at install
248.endif
249.endif
250
251htmlpages::         ${_F}
252.PRECIOUS:          ${_F}                                             # keep if install fails
253.endfor
254
255htmllinks::                                                           # link install
256
257.for _src _dst in ${MLINKS}
258_l:=${HTMLDIR}/html${_src:T:E}${MANSUBDIR}/${_src:R:S-/index$-/x&-}.html
259_t:=${HTMLDIR}/html${_dst:T:E}${MANSUBDIR}/${_dst:R:S-/index$-/x&-}.html
260
261# Handle case conflicts carefully, when _dst occurs
262# more than once after case flattening
263.if ${MKUPDATE} == "no" || ${MLINKS:${_FLATTEN}M${_dst:${_FLATTEN}Q}:[\#]} > 1
264${_t}!              ${_l} __linkinstallpage
265.else
266${_t}:              ${_l} __linkinstallpage
267.endif
268
269htmllinks::         ${_t}
270.PRECIOUS:          ${_t}
271.endfor
272
273.endif                                                                # }
274
275##### Clean rules
276.undef _F
277
278.if !empty(MAN) && (${MKMAN} != "no")
279.if (${MKCATPAGES} != "no")
280CLEANDIRFILES+= ${CATPAGES}
281.endif
282.if !empty(MANSUFFIX)
283CLEANDIRFILES+= ${MANPAGES} ${CATPAGES:S/${MANSUFFIX}$//}
284.endif
285.if ${MKHTML} != "no"
286CLEANDIRFILES+= ${HTMLPAGES}
287.endif
288.endif
289# (XXX ${CATPAGES:S...} cleans up old .catN files where .catN.gz now used)
290
291.if !empty(MANPAGES)
292lintmanpages: ${MANPAGES}
293          ${TOOL_MANDOC_LINT} -Tlint -fstrict -Wall,stop ${.ALLSRC}
294.endif
295
296##### describe
297describe:
298.for _M in ${MANPAGES}
299          @echo $$(basename ${_M}) - \
300                    $$(${TOOL_SED} < ${${_M}:P} -n -e '/^\.Nd /{;s/^....//;p;}')
301.endfor
302
303
304##### Pull in related .mk logic
305.include <bsd.obj.mk>
306.include <bsd.files.mk>
307.include <bsd.sys.mk>
308.include <bsd.clean.mk>
309
310${TARGETS} catinstall maninstall htmlinstall: # ensure existence
311