summaryrefslogtreecommitdiff
path: root/common.mk
blob: 33beed69ce83946740fd2dee6fca96ba513ebd60 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
topdir := $(dir $(lastword $(MAKEFILE_LIST)))
include $(topdir)/config.mk
pkgconfdir ?= $(sysconfdir)/libretools.d
pkglibexecdir ?= $(libexecdir)/libretools

# Misc variables for use in recipes ############################################

# Usage: <INPUT $(edit) >OUTPUT
# Expand m4_include macros to use librelib
# This is used when using sources grabbed from devtools
edit = sed -e 's|m4_include(lib/\(.*\))|. $$(librelib \1)|'

# Usage <INPUT $(pofmt) >OUTPUT
# Normalize a .po(t) file
pofmt = msguniq -Fi

xgettext-keywords-sh += --keyword={_,print}
xgettext-keywords-sh += --keyword={msg,msg2,warning,error,stat_busy,die}
xgettext-keywords-sh += --keyword={lock,slock}:3

xgettext-sh = xgettext --omit-header --from-code=UTF-8 -L shell -o -
xgettext-sh-std = $(xgettext-sh) $(xgettext-keywords-sh) $^

pot-remove-wrap = tr '\n' '\r' | sed 's/"\r\s*"//g' | tr '\r' '\n'
html-whitespace-collapse = sed -r -e 's/(\\n|\\t|\t)/ /g' -e 's/  +/ /g'
xgettext-sh-prose = $(xgettext-sh) -k --keyword={prose,bullet,flag:2} $^ | $(pot-remove-wrap) | $(html-whitespace-collapse) | sed '/^\#, sh-format/d'

# Usage: as a variable
# relative path to `/` from $(bindir)
# used for symlinking libexec files
rootdir = $(shell sed -r 's|^/||;s|[^/]+|..|g'<<<$(bindir))

# Detect things aboud the director we're in ####################################

progs += $(filter-out $(no-progs),$(shell find . -maxdepth 1 -type f -executable -printf '%f\n'))
confs += $(filter-out $(no-conf) ,$(wildcard *.conf))
mans  += $(filter-out $(no-mans) ,$(patsubst %.ronn,%,$(wildcard *.ronn)))
docs  += $(filter-out $(no-docs) ,$(wildcard *.md) $(wildcard *.ronn))
libexecs +=
libs  +=

copy_files +=
build_files += $(progs) $(confs) $(mans) $(patsubst %.in,%,$(copy_files))
install_files += $(addprefix $(DESTDIR)$(bindir)/,$(progs)) \
                 $(addprefix $(DESTDIR)$(pkgconfdir)/,$(confs)) \
                 $(addprefix $(DESTDIR)$(pkglibexecdir)/,$(libexecs) $(libs)) \
                 $(addprefix $(DESTDIR)$(docdir)/libretools/,$(docs)) \
                 $(addprefix $(DESTDIR)$(mandir)/man1/,$(filter %.1,$(mans))) \
                 $(addprefix $(DESTDIR)$(mandir)/man2/,$(filter %.2,$(mans))) \
                 $(addprefix $(DESTDIR)$(mandir)/man3/,$(filter %.3,$(mans))) \
                 $(addprefix $(DESTDIR)$(mandir)/man4/,$(filter %.4,$(mans))) \
                 $(addprefix $(DESTDIR)$(mandir)/man5/,$(filter %.5,$(mans))) \
                 $(addprefix $(DESTDIR)$(mandir)/man6/,$(filter %.6,$(mans))) \
                 $(addprefix $(DESTDIR)$(mandir)/man7/,$(filter %.7,$(mans))) \
                 $(addprefix $(DESTDIR)$(mandir)/man8/,$(filter %.8,$(mans)))
clean_files += $(patsubst %.in,%,$(copy_files)) $(copy_files) $(mans) $(wildcard *.pot)

# Set the default target #######################################################

all: PHONY build

# Copy (files from devtools) ###################################################

copy: PHONY $(copy_files)

%.in: $(devtoolsdir)/%.in
	cp $< $@
%.in: $(devtoolsdir)/lib/%
	cp $< $@

# Build ########################################################################

build: PHONY $(build_files)

%: %.ronn
	ronn --roff $(RONNFLAGS) < '$<' > '$@'
%.html: %.ronn
	ronn --html $(RONNFLAGS) < '$<' > '$@'
%: %.in
	@echo "GEN $@"
	@$(edit) <"$<" >"$@" || { rm -f -- '$@'; false; }
	@chmod 755 "$@" || { rm -f -- '$@'; false; }

%.pot: %
	{ $(xgettext-sh-std); $(xgettext-sh-prose); } | $(pofmt) > '$@'

# Install ######################################################################

install: PHONY $(install_files)

# We sort the explicit targets because sort also removes duplicates; in a few
# cases, a target could be listed twice, which is a warning.

# bindir: $(progs) vs $(libexecs)
$(sort $(addprefix $(DESTDIR)$(bindir)/,$(filter-out $(libexecs),$(progs)))): $(DESTDIR)$(bindir)/%: %
	install -Dm755 '$<' '$@'
$(sort $(addprefix $(DESTDIR)$(bindir)/,$(libexecs))): $(DESTDIR)$(bindir)/%: %
	install -d '$(@D)'
	ln -sf '$(rootdir)$(pkglibexecdir)/$(@F)' "$@"
# pkglibexecdir: $(libexecs) vs $(libs)
$(sort $(addprefix $(DESTDIR)$(pkglibexecdir)/,$(libexecs))): $(DESTDIR)$(pkglibexecdir)/%: %
	install -Dm755 '$<' '$@'
$(sort $(addprefix $(DESTDIR)$(pkglibexecdir)/,$(libs))): $(DESTDIR)$(pkglibexecdir)/%: %
	install -Dm644 '$<' '$@'
# everything else
$(DESTDIR)$(pkgconfdir)/%: %
	install -Dm644 '$<' '$@'
$(DESTDIR)$(docdir)/libretools/%: %
	install -Dm644 '$<' '$@'
$(DESTDIR)$(mandir)/man1/%.1: %.1
	install -Dm644 '$<' '$@'
$(DESTDIR)$(mandir)/man2/%.2: %.2
	install -Dm644 '$<' '$@'
$(DESTDIR)$(mandir)/man3/%.3: %.3
	install -Dm644 '$<' '$@'
$(DESTDIR)$(mandir)/man4/%.4: %.4
	install -Dm644 '$<' '$@'
$(DESTDIR)$(mandir)/man5/%.5: %.5
	install -Dm644 '$<' '$@'
$(DESTDIR)$(mandir)/man6/%.6: %.6
	install -Dm644 '$<' '$@'
$(DESTDIR)$(mandir)/man7/%.7: %.7
	install -Dm644 '$<' '$@'
$(DESTDIR)$(mandir)/man8/%.8: %.8
	install -Dm644 '$<' '$@'

# Clean ########################################################################

clean: PHONY clean-hook
	rm -f -- $(clean_files)

clean-hook: PHONY

# Boiler-plate #################################################################

FORCE: PHONY
PHONY:
.PHONY: FORCE PHONY