]> git.ipfire.org Git - thirdparty/man-pages.git/blob - Makefile
Makefile: GROFFFLAGS: Enable all warnings with -ww
[thirdparty/man-pages.git] / Makefile
1 ########################################################################
2 # Copyright (C) 2021 Alejandro Colomar <alx.manpages@gmail.com>
3 # SPDX-License-Identifier: GPL-2.0 OR LGPL-2.0
4 ########################################################################
5 # Conventions:
6 #
7 # - Follow "Makefile Conventions" from the "GNU Coding Standards" closely.
8 # However, when something could be improved, don't follow those.
9 # - Uppercase variables, when referring files, refer to files in this repo,
10 # or temporary files produced in $builddir.
11 # - Lowercase variables, when referring files, refer to system files.
12 # - Variables starting with '_' refer to absolute paths, including $(DESTDIR).
13 # - Variables ending with '_' refer to a subdir of their parent dir, which
14 # is in a variable of the same name but without the '_'. The subdir is
15 # named after this project: <*/man>.
16 # - Variables ending in '_rm' refer to files that can be removed (exist).
17 # - Variables ending in '_rmdir' refer to dirs that can be removed (exist).
18 # - Targets of the form '%-rm' remove their corresponding file '%'.
19 # - Targets of the form '%/.-rmdir' remove their corresponding dir '%/'.
20 # - Targets of the form '%/.' create their corresponding directory '%/'.
21 # - Every file or directory to be created depends on its parent directory.
22 # This avoids race conditions caused by `mkdir -p`. Only the root
23 # directories are created with parents.
24 # - The 'FORCE' target is used to make phony some variables that can't be
25 # .PHONY to avoid some optimizations.
26 #
27 ########################################################################
28
29 MAKEFLAGS += --no-print-directory
30 MAKEFLAGS += --warn-undefined-variables
31
32
33 srcdir := .
34 builddir := tmp
35 LINTDIR := $(builddir)/lint
36 HTMLDIR := $(builddir)/html
37
38 DESTDIR :=
39 prefix := /usr/local
40 SYSCONFDIR := $(srcdir)/etc
41 TMACDIR := $(SYSCONFDIR)/groff/tmac
42 datarootdir := $(prefix)/share
43 docdir := $(datarootdir)/doc
44 MANDIR := $(srcdir)
45 mandir := $(datarootdir)/man
46 MAN0DIR := $(MANDIR)/man0
47 MAN1DIR := $(MANDIR)/man1
48 MAN2DIR := $(MANDIR)/man2
49 MAN3DIR := $(MANDIR)/man3
50 MAN4DIR := $(MANDIR)/man4
51 MAN5DIR := $(MANDIR)/man5
52 MAN6DIR := $(MANDIR)/man6
53 MAN7DIR := $(MANDIR)/man7
54 MAN8DIR := $(MANDIR)/man8
55 man0dir := $(mandir)/man0
56 man1dir := $(mandir)/man1
57 man2dir := $(mandir)/man2
58 man3dir := $(mandir)/man3
59 man4dir := $(mandir)/man4
60 man5dir := $(mandir)/man5
61 man6dir := $(mandir)/man6
62 man7dir := $(mandir)/man7
63 man8dir := $(mandir)/man8
64 manext := \.[0-9]
65 man0ext := .0
66 man1ext := .1
67 man2ext := .2
68 man3ext := .3
69 man4ext := .4
70 man5ext := .5
71 man6ext := .6
72 man7ext := .7
73 man8ext := .8
74 htmldir := $(docdir)
75 htmldir_ := $(htmldir)/man
76 htmlext := .html
77
78 TMACFILES := $(sort $(shell find $(TMACDIR) -not -type d))
79 TMACNAMES := $(basename $(notdir $(TMACFILES)))
80 GROFF_CHECKSTYLE_LVL := 3
81 DEFAULT_GROFFFLAGS := -man
82 DEFAULT_GROFFFLAGS += -t
83 DEFAULT_GROFFFLAGS += -M $(TMACDIR)
84 DEFAULT_GROFFFLAGS += $(foreach x,$(TMACNAMES),-m $(x))
85 DEFAULT_GROFFFLAGS += -rCHECKSTYLE=$(GROFF_CHECKSTYLE_LVL)
86 DEFAULT_GROFFFLAGS += -ww
87 EXTRA_GROFFFLAGS :=
88 GROFFFLAGS := $(DEFAULT_GROFFFLAGS) $(EXTRA_GROFFFLAGS)
89
90 DEFAULT_MANDOCFLAGS := -man
91 DEFAULT_MANDOCFLAGS += -Tlint
92 EXTRA_MANDOCFLAGS :=
93 MANDOCFLAGS := $(DEFAULT_MANDOCFLAGS) $(EXTRA_MANDOCFLAGS)
94
95 DEFAULT_MAN2HTMLFLAGS :=
96 EXTRA_MAN2HTMLFLAGS :=
97 MAN2HTMLFLAGS := $(DEFAULT_MAN2HTMLFLAGS) $(EXTRA_MAN2HTMLFLAGS)
98
99 INSTALL := install
100 INSTALL_DATA := $(INSTALL) -m 644
101 INSTALL_DIR := $(INSTALL) -m 755 -d
102 RM := rm
103 RMDIR := rmdir --ignore-fail-on-non-empty
104 GROFF := groff
105 MANDOC := mandoc
106 MAN2HTML := man2html
107
108 MAN_SECTIONS := 0 1 2 3 4 5 6 7 8
109
110
111 .PHONY: all
112 all:
113 $(MAKE) uninstall
114 $(MAKE) install
115
116
117 %/.:
118 $(info INSTALL $(@D)/)
119 $(INSTALL_DIR) $(@D)
120
121 %-rm:
122 $(info RM $*)
123 $(RM) $*
124
125 %-rmdir:
126 $(info RMDIR $(@D))
127 $(RMDIR) $(@D)
128
129
130 .PHONY: install
131 install: install-man | installdirs
132 @:
133
134 .PHONY: installdirs
135 installdirs: | installdirs-man
136 @:
137
138 .PHONY: uninstall remove
139 uninstall remove: uninstall-man
140 @:
141
142 .PHONY: clean
143 clean:
144 $(RM) -rf $(builddir)
145
146 ########################################################################
147 # man
148
149 MANPAGES := $(sort $(shell find $(MANDIR)/man?/ -type f | grep '$(manext)$$'))
150 HTMLPAGES := $(patsubst $(MANDIR)/%,$(HTMLDIR)/%.html,$(MANPAGES))
151 _htmlpages := $(patsubst $(HTMLDIR)/%,$(DESTDIR)$(htmldir_)/%,$(HTMLPAGES))
152 _manpages := $(patsubst $(MANDIR)/%,$(DESTDIR)$(mandir)/%,$(MANPAGES))
153 _man0pages := $(filter %$(man0ext),$(_manpages))
154 _man1pages := $(filter %$(man1ext),$(_manpages))
155 _man2pages := $(filter %$(man2ext),$(_manpages))
156 _man3pages := $(filter %$(man3ext),$(_manpages))
157 _man4pages := $(filter %$(man4ext),$(_manpages))
158 _man5pages := $(filter %$(man5ext),$(_manpages))
159 _man6pages := $(filter %$(man6ext),$(_manpages))
160 _man7pages := $(filter %$(man7ext),$(_manpages))
161 _man8pages := $(filter %$(man8ext),$(_manpages))
162 LINT_groff := $(patsubst $(MANDIR)/%,$(LINTDIR)/%.lint.groff.touch,$(MANPAGES))
163 LINT_mandoc:= $(patsubst $(MANDIR)/%,$(LINTDIR)/%.lint.mandoc.touch,$(MANPAGES))
164
165 MANDIRS := $(sort $(shell find $(MANDIR)/man? -type d))
166 HTMLDIRS := $(patsubst $(MANDIR)/%,$(HTMLDIR)/%/.,$(MANDIRS))
167 LINTDIRS := $(patsubst $(MANDIR)/%,$(LINTDIR)/%/.,$(MANDIRS))
168 _htmldirs := $(patsubst $(HTMLDIR)/%,$(DESTDIR)$(htmldir_)/%,$(HTMLDIRS))
169 _mandirs := $(patsubst $(MANDIR)/%,$(DESTDIR)$(mandir)/%/.,$(MANDIRS))
170 _man0dir := $(filter %man0/.,$(_mandirs))
171 _man1dir := $(filter %man1/.,$(_mandirs))
172 _man2dir := $(filter %man2/.,$(_mandirs))
173 _man3dir := $(filter %man3/.,$(_mandirs))
174 _man4dir := $(filter %man4/.,$(_mandirs))
175 _man5dir := $(filter %man5/.,$(_mandirs))
176 _man6dir := $(filter %man6/.,$(_mandirs))
177 _man7dir := $(filter %man7/.,$(_mandirs))
178 _man8dir := $(filter %man8/.,$(_mandirs))
179 _mandir := $(DESTDIR)$(mandir)/.
180 _htmldir := $(DESTDIR)$(htmldir_)/.
181
182 _htmlpages_rm := $(addsuffix -rm,$(wildcard $(_htmlpages)))
183 _manpages_rm := $(addsuffix -rm,$(wildcard $(_manpages)))
184 _man0pages_rm := $(filter %$(man0ext)-rm,$(_manpages_rm))
185 _man1pages_rm := $(filter %$(man1ext)-rm,$(_manpages_rm))
186 _man2pages_rm := $(filter %$(man2ext)-rm,$(_manpages_rm))
187 _man3pages_rm := $(filter %$(man3ext)-rm,$(_manpages_rm))
188 _man4pages_rm := $(filter %$(man4ext)-rm,$(_manpages_rm))
189 _man5pages_rm := $(filter %$(man5ext)-rm,$(_manpages_rm))
190 _man6pages_rm := $(filter %$(man6ext)-rm,$(_manpages_rm))
191 _man7pages_rm := $(filter %$(man7ext)-rm,$(_manpages_rm))
192 _man8pages_rm := $(filter %$(man8ext)-rm,$(_manpages_rm))
193
194 _htmldirs_rmdir := $(addsuffix -rmdir,$(wildcard $(_htmldirs)))
195 _mandirs_rmdir := $(addsuffix -rmdir,$(wildcard $(_mandirs)))
196 _man0dir_rmdir := $(addsuffix -rmdir,$(wildcard $(_man0dir)))
197 _man1dir_rmdir := $(addsuffix -rmdir,$(wildcard $(_man1dir)))
198 _man2dir_rmdir := $(addsuffix -rmdir,$(wildcard $(_man2dir)))
199 _man3dir_rmdir := $(addsuffix -rmdir,$(wildcard $(_man3dir)))
200 _man4dir_rmdir := $(addsuffix -rmdir,$(wildcard $(_man4dir)))
201 _man5dir_rmdir := $(addsuffix -rmdir,$(wildcard $(_man5dir)))
202 _man6dir_rmdir := $(addsuffix -rmdir,$(wildcard $(_man6dir)))
203 _man7dir_rmdir := $(addsuffix -rmdir,$(wildcard $(_man7dir)))
204 _man8dir_rmdir := $(addsuffix -rmdir,$(wildcard $(_man8dir)))
205 _mandir_rmdir := $(addsuffix -rmdir,$(wildcard $(_mandir)))
206 _htmldir_rmdir := $(addsuffix -rmdir,$(wildcard $(_htmldir)))
207
208 install_manX := $(foreach x,$(MAN_SECTIONS),install-man$(x))
209 installdirs_manX := $(foreach x,$(MAN_SECTIONS),installdirs-man$(x))
210 uninstall_manX := $(foreach x,$(MAN_SECTIONS),uninstall-man$(x))
211
212
213 .SECONDEXPANSION:
214 $(_manpages): $(DESTDIR)$(mandir)/man%: $(MANDIR)/man% | $$(@D)/.
215 $(info INSTALL $@)
216 $(INSTALL_DATA) -T $< $@
217
218 $(_mandirs): %/.: | $$(dir %). $(_mandir)
219
220 $(_mandirs_rmdir): $(DESTDIR)$(mandir)/man%/.-rmdir: $$(_man%pages_rm) FORCE
221 $(_mandir_rmdir): $(uninstall_manX) FORCE
222
223
224 .PHONY: $(install_manX)
225 $(install_manX): install-man%: $$(_man%pages) | installdirs-man%
226 @:
227
228 .PHONY: install-man
229 install-man: $(install_manX)
230 @:
231
232 .PHONY: $(installdirs_manX)
233 $(installdirs_manX): installdirs-man%: $$(_man%dir)
234 @:
235
236 .PHONY: installdirs-man
237 installdirs-man: $(installdirs_manX)
238 @:
239
240 .PHONY: $(uninstall_manX)
241 $(uninstall_manX): uninstall-man%: $$(_man%pages_rm) $$(_man%dir_rmdir)
242 @:
243
244 .PHONY: uninstall-man
245 uninstall-man: $(_mandir_rmdir) $(uninstall_manX)
246 @:
247
248
249 ########################################################################
250 # lint
251
252 linters := groff mandoc
253 lint := $(foreach x,$(linters),lint-$(x))
254
255 $(LINT_groff): $(LINTDIR)/%.lint.groff.touch: $(MANDIR)/% | $$(@D)/.
256 $(info LINT (groff) $@)
257 $(GROFF) $(GROFFFLAGS) -z $<
258 touch $@
259
260 $(LINT_mandoc): $(LINTDIR)/%.lint.mandoc.touch: $(MANDIR)/% | $$(@D)/.
261 $(info LINT (mandoc) $@)
262 $(MANDOC) $(MANDOCFLAGS) $<
263 touch $@
264
265 $(LINTDIRS): %/.: | $$(dir %). $(LINTDIR)/.
266
267 .PHONY: $(lint)
268 $(lint): lint-%: $$(LINT_%) | lintdirs
269 @:
270
271 .PHONY: lintdirs
272 lintdirs: $(LINTDIRS)
273 @:
274
275 .PHONY: lint
276 lint: $(lint)
277 @:
278
279
280 ########################################################################
281 # html
282
283 # Use with
284 # make MAN2HTMLFLAGS=whatever html
285 # The sed removes the lines "Content-type: text/html\n\n"
286 $(HTMLPAGES): $(HTMLDIR)/%.html: $(MANDIR)/% | $$(@D)/.
287 $(info MAN2HTML $@)
288 $(MAN2HTML) $(MAN2HTMLFLAGS) $< | sed -e 1,2d >$@ || exit $$?
289
290 $(HTMLDIRS): %/.: | $$(dir %). $(HTMLDIR)/.
291
292 $(_htmlpages): $(DESTDIR)$(htmldir_)/%: $(HTMLDIR)/% | $$(@D)/.
293 $(info INSTALL $@)
294 $(INSTALL_DATA) -T $< $@
295
296 $(_htmldirs): %/.: | $$(dir %). $(_htmldir)
297
298
299 .PHONY: build-html html
300 build-html html: $(HTMLPAGES) | builddirs-html
301 @:
302
303 .PHONY: builddirs-html
304 builddirs-html: $(HTMLDIRS)
305 @:
306
307 .PHONY: install-html
308 install-html: $(_htmlpages) | installdirs-html
309 @:
310
311 .PHONY: installdirs-html
312 installdirs-html: $(_htmldirs)
313 @:
314
315 .PHONY: uninstall-html
316 uninstall-html: $(_htmldir_rmdir) $(_htmldirs_rmdir) $(_htmlpages_rm)
317 @:
318
319
320 ########################################################################
321 # tests
322
323 # Check if groff reports warnings (may be words of sentences not displayed)
324 # from https://lintian.debian.org/tags/groff-message.html
325 .PHONY: check-groff-warnings
326 check-groff-warnings:
327 GROFF_LOG="$$(mktemp --tmpdir manpages-checksXXXX)" || exit $$?; \
328 for i in man?/*.[0-9]; \
329 do \
330 if grep -q 'SH.*NAME' "$$i"; then \
331 LC_ALL=en_US.UTF-8 MANWIDTH=80 man --warnings -E UTF-8 -l "$$i" > /dev/null 2>| "$$GROFF_LOG"; \
332 [ -s "$$GROFF_LOG" ] && { echo "$$i: "; cat "$$GROFF_LOG"; echo; }; \
333 fi; \
334 done; \
335 rm -f "$$GROFF_LOG"
336
337 # someone might also want to look at /var/catman/cat2 or so ...
338 # a problem is that the location of cat pages varies a lot
339
340 ########################################################################
341
342 $(V).SILENT:
343 FORCE: