rename.2: wfix
[man-pages.git] / Makefile
blob0da0328d879c0751b4181d2c4b22e9e7ce51637a
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:
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.
26 ########################################################################
28 MAKEFLAGS += --no-print-directory
29 MAKEFLAGS += --silent
30 MAKEFLAGS += --warn-undefined-variables
33 htmlbuilddir := $(CURDIR)/.html
34 HTOPTS :=
36 DESTDIR :=
37 prefix := /usr/local
38 datarootdir := $(prefix)/share
39 docdir := $(datarootdir)/doc
40 MANDIR := $(CURDIR)
41 mandir := $(datarootdir)/man
42 MAN1DIR := $(MANDIR)/man1
43 MAN2DIR := $(MANDIR)/man2
44 MAN3DIR := $(MANDIR)/man3
45 MAN4DIR := $(MANDIR)/man4
46 MAN5DIR := $(MANDIR)/man5
47 MAN6DIR := $(MANDIR)/man6
48 MAN7DIR := $(MANDIR)/man7
49 MAN8DIR := $(MANDIR)/man8
50 man1dir := $(mandir)/man1
51 man2dir := $(mandir)/man2
52 man3dir := $(mandir)/man3
53 man4dir := $(mandir)/man4
54 man5dir := $(mandir)/man5
55 man6dir := $(mandir)/man6
56 man7dir := $(mandir)/man7
57 man8dir := $(mandir)/man8
58 manext := \.[0-9]
59 man1ext := .1
60 man2ext := .2
61 man3ext := .3
62 man4ext := .4
63 man5ext := .5
64 man6ext := .6
65 man7ext := .7
66 man8ext := .8
67 htmldir := $(docdir)
68 htmldir_ := $(htmldir)/man
69 htmlext := .html
71 INSTALL := install
72 INSTALL_DATA := $(INSTALL) -m 644
73 INSTALL_DIR := $(INSTALL) -m 755 -d
74 RM := rm
75 RMDIR := rmdir --ignore-fail-on-non-empty
77 MAN_SECTIONS := 1 2 3 4 5 6 7 8
80 .PHONY: all
81 all:
82 $(MAKE) uninstall
83 $(MAKE) install
86 %/.:
87 $(info - INSTALL $(@D))
88 $(INSTALL_DIR) $(@D)
90 %-rm:
91 $(info - RM $*)
92 $(RM) $*
94 %-rmdir:
95 $(info - RMDIR $(@D))
96 $(RMDIR) $(@D)
99 .PHONY: install
100 install: install-man | installdirs
103 .PHONY: installdirs
104 installdirs: | installdirs-man
107 .PHONY: uninstall remove
108 uninstall remove: uninstall-man
111 .PHONY: clean
112 clean:
113 find man?/ -type f \
114 |while read f; do \
115 rm -f "$(htmlbuilddir)/$$f".*; \
116 done;
118 ########################################################################
119 # man
121 MANPAGES := $(sort $(shell find $(MANDIR)/man?/ -type f | grep '$(manext)$$'))
122 _manpages := $(patsubst $(MANDIR)/%,$(DESTDIR)$(mandir)/%,$(MANPAGES))
123 _man1pages := $(filter %$(man1ext),$(_manpages))
124 _man2pages := $(filter %$(man2ext),$(_manpages))
125 _man3pages := $(filter %$(man3ext),$(_manpages))
126 _man4pages := $(filter %$(man4ext),$(_manpages))
127 _man5pages := $(filter %$(man5ext),$(_manpages))
128 _man6pages := $(filter %$(man6ext),$(_manpages))
129 _man7pages := $(filter %$(man7ext),$(_manpages))
130 _man8pages := $(filter %$(man8ext),$(_manpages))
132 MANDIRS := $(sort $(shell find $(MANDIR)/man? -type d))
133 _mandirs := $(patsubst $(MANDIR)/%,$(DESTDIR)$(mandir)/%/.,$(MANDIRS))
134 _man1dir := $(filter %man1/.,$(_mandirs))
135 _man2dir := $(filter %man2/.,$(_mandirs))
136 _man3dir := $(filter %man3/.,$(_mandirs))
137 _man4dir := $(filter %man4/.,$(_mandirs))
138 _man5dir := $(filter %man5/.,$(_mandirs))
139 _man6dir := $(filter %man6/.,$(_mandirs))
140 _man7dir := $(filter %man7/.,$(_mandirs))
141 _man8dir := $(filter %man8/.,$(_mandirs))
142 _mandir := $(DESTDIR)$(mandir)/.
144 _manpages_rm := $(addsuffix -rm,$(wildcard $(_manpages)))
145 _man1pages_rm := $(filter %$(man1ext)-rm,$(_manpages_rm))
146 _man2pages_rm := $(filter %$(man2ext)-rm,$(_manpages_rm))
147 _man3pages_rm := $(filter %$(man3ext)-rm,$(_manpages_rm))
148 _man4pages_rm := $(filter %$(man4ext)-rm,$(_manpages_rm))
149 _man5pages_rm := $(filter %$(man5ext)-rm,$(_manpages_rm))
150 _man6pages_rm := $(filter %$(man6ext)-rm,$(_manpages_rm))
151 _man7pages_rm := $(filter %$(man7ext)-rm,$(_manpages_rm))
152 _man8pages_rm := $(filter %$(man8ext)-rm,$(_manpages_rm))
154 _mandirs_rmdir := $(addsuffix -rmdir,$(wildcard $(_mandirs)))
155 _man1dir_rmdir := $(addsuffix -rmdir,$(wildcard $(_man1dir)))
156 _man2dir_rmdir := $(addsuffix -rmdir,$(wildcard $(_man2dir)))
157 _man3dir_rmdir := $(addsuffix -rmdir,$(wildcard $(_man3dir)))
158 _man4dir_rmdir := $(addsuffix -rmdir,$(wildcard $(_man4dir)))
159 _man5dir_rmdir := $(addsuffix -rmdir,$(wildcard $(_man5dir)))
160 _man6dir_rmdir := $(addsuffix -rmdir,$(wildcard $(_man6dir)))
161 _man7dir_rmdir := $(addsuffix -rmdir,$(wildcard $(_man7dir)))
162 _man8dir_rmdir := $(addsuffix -rmdir,$(wildcard $(_man8dir)))
163 _mandir_rmdir := $(addsuffix -rmdir,$(wildcard $(_mandir)))
165 install_manX := $(foreach x,$(MAN_SECTIONS),install-man$(x))
166 installdirs_manX := $(foreach x,$(MAN_SECTIONS),installdirs-man$(x))
167 uninstall_manX := $(foreach x,$(MAN_SECTIONS),uninstall-man$(x))
170 .SECONDEXPANSION:
171 $(_manpages): $(DESTDIR)$(mandir)/man%: $(MANDIR)/man% | $$(@D)/.
172 $(info - INSTALL $@)
173 $(INSTALL_DATA) -T $< $@
175 $(_mandirs): %/.: | $$(dir %). $(_mandir)
177 $(_mandirs_rmdir): $(DESTDIR)$(mandir)/man%/.-rmdir: $$(_man%pages_rm) FORCE
178 $(_mandir_rmdir): $(uninstall_manX) FORCE
181 .PHONY: $(install_manX)
182 $(install_manX): install-man%: $$(_man%pages) | installdirs-man%
185 .PHONY: install-man
186 install-man: $(install_manX)
189 .PHONY: $(installdirs_manX)
190 $(installdirs_manX): installdirs-man%: $$(_man%dir) $(_mandir)
193 .PHONY: installdirs-man
194 installdirs-man: $(installdirs_manX)
197 .PHONY: $(uninstall_manX)
198 $(uninstall_manX): uninstall-man%: $$(_man%pages_rm) $$(_man%dir_rmdir)
201 .PHONY: uninstall-man
202 uninstall-man: $(_mandir_rmdir) $(uninstall_manX)
206 ########################################################################
207 # html
209 # Use with
210 # make HTOPTS=whatever html
211 # The sed removes the lines "Content-type: text/html\n\n"
212 .PHONY: html
213 html: | builddirs-html
214 find man?/ -type f \
215 |while read f; do \
216 man2html $(HTOPTS) "$$f" \
217 |sed -e '1,2d' \
218 >"$(htmlbuilddir)/$${f}$(htmlext)" \
219 || exit $$?; \
220 done;
222 .PHONY: builddirs-html
223 builddirs-html:
224 find man?/ -type d \
225 |while read d; do \
226 $(INSTALL_DIR) "$(htmlbuilddir)/$$d" || exit $$?; \
227 done;
229 .PHONY: install-html
230 install-html: | installdirs-html
231 cd $(htmlbuilddir) && \
232 find man?/ -type f \
233 |while read f; do \
234 $(INSTALL_DATA) -T "$$f" "$(DESTDIR)$(htmldir_)/$$f" || exit $$?; \
235 done;
237 .PHONY: installdirs-html
238 installdirs-html:
239 find man?/ -type d \
240 |while read d; do \
241 $(INSTALL_DIR) "$(DESTDIR)$(htmldir_)/$$d" || exit $$?; \
242 done;
244 .PHONY: uninstall-html
245 uninstall-html:
246 find man?/ -type f \
247 |while read f; do \
248 rm -f "$(DESTDIR)$(htmldir_)/$$f".* || exit $$?; \
249 done;
252 ########################################################################
253 # tests
255 # Check if groff reports warnings (may be words of sentences not displayed)
256 # from https://lintian.debian.org/tags/groff-message.html
257 .PHONY: check-groff-warnings
258 check-groff-warnings:
259 GROFF_LOG="$$(mktemp --tmpdir manpages-checksXXXX)" || exit $$?; \
260 for i in man?/*.[1-9]; \
261 do \
262 if grep -q 'SH.*NAME' "$$i"; then \
263 LC_ALL=en_US.UTF-8 MANWIDTH=80 man --warnings -E UTF-8 -l "$$i" > /dev/null 2>| "$$GROFF_LOG"; \
264 [ -s "$$GROFF_LOG" ] && { echo "$$i: "; cat "$$GROFF_LOG"; echo; }; \
265 fi; \
266 done; \
267 rm -f "$$GROFF_LOG"
269 # someone might also want to look at /var/catman/cat2 or so ...
270 # a problem is that the location of cat pages varies a lot
272 ########################################################################
274 FORCE: