xref: /freebsd/contrib/bmake/mk/man.mk (revision 7ef62cebc2f965b0f640263e179276928885e33d)
1# $Id: man.mk,v 1.25 2021/10/31 03:03:14 sjg Exp $
2
3.if !target(__${.PARSEFILE}__)
4__${.PARSEFILE}__: .NOTMAIN
5
6OPTIONS_DEFAULT_NO += CMT2DOC
7
8.include <init.mk>
9.include <options.mk>
10
11# unlike bsd.man.mk we handle 3 approaches
12# 1. install unformated nroff (default)
13# 2. install formatted pages
14# 3. install formatted pages but with extension of .0
15# sadly we cannot rely on a shell that supports ${foo#...} and ${foo%...}
16# so we have to use sed(1).
17
18# set MANTARGET=cat for formatted pages
19MANTARGET?=	man
20# set this to .0 for same behavior as bsd.man.mk
21MCATEXT?=
22
23NROFF?=		nroff
24MANDIR?=	/usr/share/man
25MANDOC?= man
26
27MAN_SUFFIXES?= .1 .2 .3 .4 .5 .6 .7 .8 .9
28.SUFFIXES: ${MAN_SUFFIXES}
29.if ${MANTARGET} == "cat"
30.SUFFIXES: ${MAN_SUFFIXES:S,.,.cat,}
31.endif
32
33${MAN_SUFFIXES:@s@$s${s:S,.,.cat,}@}:
34	@echo "${NROFF} -${MANDOC} ${.IMPSRC} > ${.TARGET:T}"
35	@${NROFF} -${MANDOC} ${.IMPSRC} > ${.TARGET:T}.new && \
36	mv ${.TARGET:T}.new ${.TARGET:T}
37
38.if defined(MAN) && !empty(MAN)
39
40.if ${MK_CMT2DOC} == "yes"
41# use cmt2doc.py to extract manpages from source
42CMT2DOC?= cmt2doc.py
43CMT2DOC_OPTS?=  ${CMT2DOC_ORGOPT} -pmS${.TARGET:E}
44CMT2DOC_SUFFIXES+= .c .h .sh .pl .py
45
46.SUFFIXES: ${CMT2DOC_SUFFIXES}
47
48${CMT2DOC_SUFFIXES:@s@${MAN_SUFFIXES:@m@$s$m@}@}:
49	@echo "${CMT2DOC} ${.IMPSRC} > ${.TARGET:T}"
50	@${CMT2DOC} ${CMT2DOC_OPTS} ${.IMPSRC} > ${.TARGET:T}.new && \
51	mv ${.TARGET:T}.new ${.TARGET:T}
52
53.endif
54
55_mandir=${DESTDIR}${MANDIR}/${MANTARGET}`echo $$page | sed -e 's/.*\.cat/./' -e 's/.*\.//'`
56.if ${MANTARGET} == "cat"
57_mfromdir?=.
58MANALL=	${MAN:${MAN_SUFFIXES:S,.,,:@m@S/.$m/.cat$m/@:ts:}}
59.if ${MCATEXT} == ""
60_minstpage=`echo $$page | sed 's/\.cat/./'`
61.else
62_minstpage=`echo $$page | sed 's/\.cat.*//'`${MCATEXT}
63.endif
64.endif
65.if target(${MAN:[1]})
66_mfromdir?=.
67.endif
68_mfromdir?=${.CURDIR}
69MANALL?= ${MAN}
70_minstpage?=$${page}
71.endif
72
73.if !empty(MANOWN)
74MAN_INSTALL_OWN ?= -o ${MANOWN} -g ${MANGRP}
75MAN_CHOWN ?= chown
76.else
77MAN_CHOWN = :
78.endif
79
80MINSTALL=	${INSTALL} ${COPY} ${MAN_INSTALL_OWN} -m ${MANMODE}
81.if defined(MANZ)
82# chown and chmod are done afterward automatically
83MCOMPRESS=	gzip -cf
84MCOMPRESSSUFFIX= .gz
85.endif
86
87maninstall:
88.if defined(MANALL) && !empty(MANALL)
89	@for page in ${MANALL:T}; do \
90		test -s ${_mfromdir}/$$page || continue; \
91		dir=${_mandir}; \
92		test -d $$dir || ${INSTALL} -d ${MAN_INSTALL_OWN} -m 775 $$dir; \
93		instpage=$${dir}${MANSUBDIR}/${_minstpage}${MCOMPRESSSUFFIX}; \
94		if [ X"${MCOMPRESS}" = X ]; then \
95			echo ${MINSTALL} ${_mfromdir}/$$page $$instpage; \
96			${MINSTALL} ${_mfromdir}/$$page $$instpage; \
97		else \
98			rm -f $$instpage; \
99			echo ${MCOMPRESS} ${_mfromdir}/$$page \> $$instpage; \
100			${MCOMPRESS} ${_mfromdir}/$$page > $$instpage; \
101			${MAN_CHOWN} ${MANOWN}:${MANGRP} $$instpage; \
102			chmod ${MANMODE} $$instpage; \
103		fi \
104	done
105.if defined(MLINKS) && !empty(MLINKS)
106	@set ${MLINKS}; \
107	while test $$# -ge 2; do \
108		page=$$1; \
109		shift; \
110		dir=${_mandir}; \
111		l=${_minstpage}${MCOMPRESSSUFFIX}; \
112		page=$$1; \
113		shift; \
114		dir=${_mandir}; \
115		t=$${dir}${MANSUBDIR}/${_minstpage}${MCOMPRESSSUFFIX}; \
116		echo $$t -\> $$l; \
117		rm -f $$t; \
118		ln -s $$l $$t; \
119	done
120.endif
121.endif
122
123.if defined(MANALL) && !empty(MANALL)
124manall: ${MANALL}
125all: manall
126.endif
127
128.if defined(CLEANMAN) && !empty(CLEANMAN)
129cleandir: cleanman
130cleanman:
131	rm -f ${CLEANMAN}
132.endif
133.endif
134