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