xref: /freebsd/contrib/bmake/mk/man.mk (revision a91a246563dffa876a52f53a98de4af9fa364c52)
1# $Id: man.mk,v 1.29 2024/02/19 00:06:19 sjg Exp $
2
3# should be set properly in sys.mk
4_this ?= ${.PARSEFILE:S,bsd.,,}
5
6.if !target(__${_this}__)
7__${_this}__: .NOTMAIN
8
9OPTIONS_DEFAULT_NO += CMT2DOC
10
11.include <init.mk>
12.include <options.mk>
13
14# unlike bsd.man.mk we handle 3 approaches
15# 1. install unformated nroff (default)
16# 2. install formatted pages
17# 3. install formatted pages but with extension of .0
18# sadly we cannot rely on a shell that supports ${foo#...} and ${foo%...}
19# so we have to use sed(1).
20
21# set MANTARGET=cat for formatted pages
22MANTARGET ?=	man
23# set this to .0 for same behavior as bsd.man.mk
24MCATEXT ?=
25
26NROFF ?=	nroff
27MANDIR ?=	/usr/share/man
28MANDOC ?= man
29
30MAN_SUFFIXES?= .1 .2 .3 .4 .5 .6 .7 .8 .9
31.SUFFIXES: ${MAN_SUFFIXES}
32.if ${MANTARGET} == "cat"
33.SUFFIXES: ${MAN_SUFFIXES:S,.,.cat,}
34.endif
35
36${MAN_SUFFIXES:@s@$s${s:S,.,.cat,}@}:
37	@echo "${NROFF} -${MANDOC} ${.IMPSRC} > ${.TARGET:T}"
38	@${NROFF} -${MANDOC} ${.IMPSRC} > ${.TARGET:T}.new && \
39	mv ${.TARGET:T}.new ${.TARGET:T}
40
41
42.if !empty(MANOWN)
43MAN_INSTALL_OWN ?= -o ${MANOWN} -g ${MANGRP}
44MAN_CHOWN ?= chown
45.else
46MAN_CHOWN = :
47.endif
48
49MINSTALL = ${INSTALL} ${COPY} ${MAN_INSTALL_OWN} -m ${MANMODE}
50
51.if defined(MAN) && !empty(MAN)
52
53.if ${MANTARGET} == "cat"
54MANALL ?= ${MAN:T:@p@${p:R}.cat${p:E}@}
55.else
56MANALL ?= ${MAN}
57.endif
58
59.if ${MK_CMT2DOC} == "yes"
60# use cmt2doc.py to extract manpages from source
61CMT2DOC ?= cmt2doc.py
62# -m  produces man(7)
63# -mm produces mdoc(7)
64CMT2DOC_FLAGS ?= -pm
65CMT2DOC_OPTS ?=  ${CMT2DOC_ORGOPT} -S${.TARGET:E}
66CMT2DOC_SUFFIXES += .c .h .sh .pl .py
67
68.SUFFIXES: ${CMT2DOC_SUFFIXES}
69
70${CMT2DOC_SUFFIXES:@s@${MAN_SUFFIXES:@m@$s$m@}@}:
71	@echo "${CMT2DOC} ${.IMPSRC} > ${.TARGET:T}"
72	@${CMT2DOC} ${CMT2DOC_FLAGS} ${CMT2DOC_OPTS} ${.IMPSRC} > ${.TARGET:T}.new && \
73	mv ${.TARGET:T}.new ${.TARGET:T}
74
75.endif
76
77# none of this is relevant unless doing maninstall
78.if make(*install)
79_mandir = ${DESTDIR}${MANDIR}/${MANTARGET}`echo $$page | sed -e 's/.*\.cat/./' -e 's/.*\.//'`
80.if ${MANTARGET} == "cat"
81_mfromdir ?= .
82.if ${MCATEXT} == ""
83_minstpage = `echo $$page | sed 's/\.cat/./'`
84.else
85_minstpage = `echo $$page | sed 's/\.cat.*//'`${MCATEXT}
86.endif
87.endif
88.if target(${MAN:[1]})
89_mfromdir ?= .
90.endif
91_mfromdir ?= ${.CURDIR}
92_minstpage ?= $${page}
93.endif
94
95.if defined(MANZ)
96# chown and chmod are done afterward automatically
97MCOMPRESS_CMD ?= gzip -cf
98MCOMPRESS_EXT ?= .gz
99
100_MANZ_USE:	.USE
101	@${MCOMPRESS_CMD} ${.ALLSRC} > ${.TARGET}
102
103.for _page in ${MANALL}
104${_page:T}${MCOMPRESS_EXT}: ${_page} _MANZ_USE
105.endfor
106.endif
107
108.if ${MK_STAGING_MAN} == "yes"
109_mansets := ${MAN:E:O:u:M*[1-9]:@s@man$s@}
110.if ${MANTARGET} == "cat"
111STAGE_AS_SETS += ${_mansets}
112_stage_man = stage_as
113.else
114STAGE_SETS += ${_mansets}
115_stage_man = stage_files
116.endif
117STAGE_TARGETS += ${_stage_man}
118.for _page _as in ${MANALL:@x@$x ${x:T:S/.cat/./}@}
119${_stage_man}.man${_as:E}: ${_page}
120.if target(${_page:T}${MCOMPRESS_EXT:Umanz})
121${_man_stage}.man${_as:E}: ${_page:T}${MCOMPRESS_EXT}
122.endif
123STAGE_DIR.man${_as:E} ?= ${STAGE_OBJTOP}${MANDIR}/${MANTARGET}${_as:E}${MANSUBDIR}
124.if ${MANTARGET} == "cat"
125STAGE_AS_${_page} = ${_as}
126.endif
127.endfor
128.if !defined(NO_MLINKS) && !empty(MLINKS)
129STAGE_SETS += mlinks
130STAGE_TARGETS += stage_links
131STAGE_LINKS.mlinks := ${MLINKS:M*.[1-9]:@f@${f:S,^,${MANDIR}/${MANTARGET}${f:E}${MANSUBDIR}/,}@}
132stage_links.mlinks: ${_mansets:@s@stage_files.$s@}
133.endif
134.endif
135
136.endif
137
138maninstall:
139.if defined(MANALL) && !empty(MANALL)
140	@for page in ${MANALL:T}; do \
141		test -s ${_mfromdir}/$$page || continue; \
142		dir=${_mandir}; \
143		test -d $$dir || ${INSTALL} -d ${MAN_INSTALL_OWN} -m 775 $$dir; \
144		instpage=$${dir}${MANSUBDIR}/${_minstpage}${MCOMPRESSSUFFIX}; \
145		if [ X"${MCOMPRESS}" = X ]; then \
146			echo ${MINSTALL} ${_mfromdir}/$$page $$instpage; \
147			${MINSTALL} ${_mfromdir}/$$page $$instpage; \
148		else \
149			rm -f $$instpage; \
150			echo ${MCOMPRESS} ${_mfromdir}/$$page \> $$instpage; \
151			${MCOMPRESS} ${_mfromdir}/$$page > $$instpage; \
152			${MAN_CHOWN} ${MANOWN}:${MANGRP} $$instpage; \
153			chmod ${MANMODE} $$instpage; \
154		fi \
155	done
156.if defined(MLINKS) && !empty(MLINKS)
157	@set ${MLINKS}; \
158	while test $$# -ge 2; do \
159		page=$$1; \
160		shift; \
161		dir=${_mandir}; \
162		l=${_minstpage}${MCOMPRESSSUFFIX}; \
163		page=$$1; \
164		shift; \
165		dir=${_mandir}; \
166		t=$${dir}${MANSUBDIR}/${_minstpage}${MCOMPRESSSUFFIX}; \
167		echo $$t -\> $$l; \
168		rm -f $$t; \
169		ln -s $$l $$t; \
170	done
171.endif
172.endif
173
174.if defined(MANALL) && !empty(MANALL)
175manall: ${MANALL}
176all: manall
177.endif
178
179.if defined(CLEANMAN) && !empty(CLEANMAN)
180cleandir: cleanman
181cleanman:
182	rm -f ${CLEANMAN}
183.endif
184.endif
185