-
Notifications
You must be signed in to change notification settings - Fork 6
/
Makefile
executable file
·162 lines (144 loc) · 5.15 KB
/
Makefile
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
#-------------------------------------------------------------------------------
# Variables to setup through environment
#-------------------------------------------------------------------------------
# Specify default values.
prefix := /usr/local
destdir :=
system := linux
# Fallback to defaults but allow to get the values from environment.
PREFIX ?= $(prefix)
BINDIR ?= $(PREFIX)/bin
DESTDIR ?= $(destdir)
SYSTEM ?= $(system)
#-------------------------------------------------------------------------------
# Installation paths and defaults
#-------------------------------------------------------------------------------
SHELL := /bin/bash
DIRNAME := $(shell dirname $(realpath $(lastword $(MAKEFILE_LIST))))
RST2MAN := rst2man
PROG := flow
PROGSINGLE := flow.sh
DATAPATHVAR := DATAPATH
USAGEVAR := USAGE
VERSIONVAR := VERF
README := README.md
MANFILE := $(PROG).1
MANRST := $(PROG).1.rst
USAGEFILE := $(PROG).usage
VERFILE := VERSION
CHLOGFILE := CHANGELOG.md
DESTPATH := $(DESTDIR)$(PREFIX)
BINPATH := $(DESTDIR)$(BINDIR)
SHAREPATH := $(DESTPATH)/share
MANDIR := man/man1
DISTNAME := $(PROG)-$$(cat $(VERFILE))-$(SYSTEM)
# COMPILEDIR is overriden by dist and distsingle recipes
COMPILEDIR := compiled
INSTFILE := install
INSDEVTFILE := install_develop
UNINSTFILE := uninstall
USAGEHEADER := "Usage: "
#-------------------------------------------------------------------------------
# Canned recipes
#-------------------------------------------------------------------------------
# Extract text from README between headers and format it to troff syntax
define compile_usage
@ echo -n "Compiling usage file ..."
@ echo -n "$(USAGEHEADER)" > $(COMPILEDIR)/$(USAGEFILE)
@ grep "^$(PROG) \[" $(MANRST) | sed 's/\\|/|/g' >> $(COMPILEDIR)/$(USAGEFILE)
@ echo ".TH" >> $(COMPILEDIR)/$(USAGEFILE)
@ sed -n '/^OPTIONS/,/^EXIT CODES/p' $(MANRST) | grep -v "^\(EXIT CODES\|OPTIONS\|======\)" \
| sed 's/^\\//;s/^-/.TP 18\n-/' | sed 's/^ //' | sed '/^$$/d' >> $(COMPILEDIR)/$(USAGEFILE)
@ echo DONE
endef
#-------------------------------------------------------------------------------
# Recipes
#-------------------------------------------------------------------------------
compile:
@ mkdir -p $(COMPILEDIR)
@ cp $(VERFILE) $(CHLOGFILE) $(COMPILEDIR)
@ # Insert default datapath variable into $(PROG)
@ echo -n "Compiling command file ..."
@ { \
head -n1 $(PROG); \
echo "$(DATAPATHVAR)=\"$(SHAREPATH)/$(PROG)\""; \
tail -n+2 $(PROG); \
} > $(COMPILEDIR)/$(PROG)
@ chmod +x $(COMPILEDIR)/$(PROG)
@ echo DONE
@ # Extract text from README between headers and convert it to troff syntax
@ echo -n "Compiling man file ..."
@ { \
echo -n ".TH \"FLOW\" \"1\" "; \
echo -n "\""; echo -n $$(stat -c %z $(MANRST) | cut -d" " -f1); echo -n "\" "; \
echo -n "\"User Manual\" "; \
echo -n "\"Version "; echo -n $$(cat $(VERFILE)); echo -n "\" "; \
echo; \
} > $(COMPILEDIR)/$(MANFILE)
@ sed 's/`\([^`]\+\)<\([^>]\+\)>`__/\1\n \2/g' $(MANRST) | $(RST2MAN) | tail -n+33 >> $(COMPILEDIR)/$(MANFILE)
@ echo DONE
@ # Copy README and MAN rst into COMPILEDIR
@ echo -n "Compiling readme file ..."
@ cp $(README) $(COMPILEDIR)/$(README)
@ cp $(MANRST) $(COMPILEDIR)/$(MANRST)
@ echo DONE
$(compile_usage)
@ echo -n "Compiling install file ..."
@ { \
echo "#!/bin/bash"; \
echo; \
echo ": \$${BINPATH:=$(BINPATH)}"; \
echo ": \$${SHAREPATH:=$(SHAREPATH)}"; \
echo ": \$${USRMANPATH:=\$$SHAREPATH/$(MANDIR)}"; \
echo; \
echo "dir=\"\$$(dirname \"\$$0\")\""; \
echo "mkdir -p \"\$$USRMANPATH\" \\"; \
echo "&& cp \"\$$dir/$(MANFILE)\" \"\$$USRMANPATH\" \\"; \
echo "&& mkdir -p \"\$$BINPATH\" \\"; \
echo "&& cp \"\$$dir/$(PROG)\" \"\$$BINPATH\" \\"; \
echo "&& mkdir -p \"\$$SHAREPATH/$(PROG)\" \\"; \
echo "&& cp \"\$$dir/$(USAGEFILE)\" \"\$$dir/$(VERFILE)\" \"\$$SHAREPATH/$(PROG)\" \\"; \
echo "&& echo 'Installation completed.' \\"; \
echo "|| { echo 'Installation failed.'; exit 1; }"; \
} > $(COMPILEDIR)/$(INSTFILE)
@ chmod +x $(COMPILEDIR)/$(INSTFILE)
@ echo DONE
@ echo -n "Compiling uninstall file ..."
@ { \
echo "#!/bin/bash"; \
echo; \
echo ": \$${BINPATH:=$(BINPATH)}"; \
echo ": \$${SHAREPATH:=$(SHAREPATH)}"; \
echo ": \$${USRMANPATH:=\$$SHAREPATH/$(MANDIR)}"; \
echo; \
echo "rm \"\$$USRMANPATH/$(MANFILE)\""; \
echo "rm \"\$$BINPATH/$(PROG)\""; \
echo "rm -rf \"\$$SHAREPATH/$(PROG)\""; \
echo "echo 'Uninstallation completed.'"; \
} > $(COMPILEDIR)/$(UNINSTFILE)
@ chmod +x $(COMPILEDIR)/$(UNINSTFILE)
@ echo DONE
dist: COMPILEDIR=$(DISTNAME)
dist: compile
@ tar czf $(COMPILEDIR).tar.gz $(COMPILEDIR)
@ echo "Distribution built; see 'tar tzf $(COMPILEDIR).tar.gz'"
distsingle: COMPILEDIR=.
distsingle:
@ $(compile_usage)
@ echo -n "Compiling single script ..."
@ # Insert content of $(USAGEFILE) and $(VERFILE) into $(PROG) as variables
@ { \
head -n1 $(PROG); \
echo "$(USAGEVAR)=\"$$(cat $(USAGEFILE))\""; \
echo "$(VERSIONVAR)=\"$$(cat $(VERFILE))\""; \
tail -n+2 $(PROG); \
} > $(PROGSINGLE)
@ chmod +x $(PROGSINGLE)
@ echo DONE
clean:
@ rm -rf $(COMPILEDIR)
@ rm -rf $(DISTNAME)
@ rm -f $(USAGEFILE)
distclean:
@ rm -f *.tar.gz
@ rm -f $(PROGSINGLE)