-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMakefile
362 lines (306 loc) · 10.1 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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
#------------------------------------------------------------------------------
SHELL = /bin/sh
#------------------------------------------------------------------------------
# Choose! Are you making an exe, an archive (static library), or a DLL (dynamic library)
#
BLDTYPE=exe
#BLDTYPE=arc
#BLDTYPE=dll # TODO : Add support for dynamic libraries
#------------------------------------------------------------------------------
# Give your tool a name!
# All file extensions are appended later
#
TOOLNAME = chatpad360
TESTPRAM = $(shell pwd)/default.conf -n
SERVICE = chatpad360.service
#------------------------------------------------------------------------------
# Commands (for future cross platform support)
#
GETDEPS = gcc -M
LINK = gcc -o
MAKE = make
DEL = rm -f
COPY = cp
CAT = cat
MD = mkdir -p
RMF = rm -f
RMRF = rm -rf
ECHO = /bin/echo -e
AR = ar -crfvs
TAR = tar -cvf
SEDI = sed -i
READELF = readelf
SVNVER = svnversion . -n | sed s/^[0-9]*://
SVNTMP = ~svntmp.tmp
SUDO = sudo
CHMOD = chmod
CHOWN = chown
UNAME_A = uname -a
UNAME_M = uname -m
OSREL = lsb_release -crid
WHO = id -un
WHERE = hostname -A
WHEN = date +%Y-%m-%d@%H:%M:%S
#------------------------------------------------------------------------------
# ANSI colours
#
aBLK=\e[30m
aRED=\e[31m
aGRN=\e[32m
aYEL=\e[33m
aBLU=\e[34m
aMAG=\e[35m
aCYN=\e[36m
aWHT=\e[37m
aONBLK=\e[40m
aONRED=\e[41m
aONGRN=\e[42m
aONYEL=\e[43m
aONBLU=\e[44m
aONMAG=\e[45m
aONCYN=\e[46m
aONWHT=\e[47m
aNRM=\e[0m
aBRT=\e[1m
aUNL=\e[4m
aFLS=\e[5m
aINV=\e[7m
aHID=\e[8m
tPRE =$(aNRM)$(aBRT):-----[
tPOST=$(aNRM)$(aBRT)]-----:$(aNRM)
#------------------------------------------------------------------------------
# MACROs used by the source code, generated at compile time
#
VER_RCS = $(shell $(SVNVER))
VER_DAT = $(shell $(DATE))
CCDEF = -DTOOLNAME="$(TOOLNAME)" -DVER_SVN="$(VER_RCS)" -DVER_DAT="$(VER_DAT)"
#------------------------------------------------------------------------------
# Files, directories & libraries in use
#
ARCH = $(shell $(UNAME_M))
# Make sure ALL of these are ALWAYS define (see 'clean' & 'tidy' rules)
BINROOT = bin
BINDIR = $(BINROOT)/$(ARCH)
LIBDIR = $(BINDIR)/lib
INCDIR = $(BINDIR)/inc
EXE = $(BINDIR)/$(TOOLNAME)
ARC = $(LIBDIR)/$(TOOLNAME).a
SRC = $(wildcard *.c)
HDR = $(wildcard *.h)
MISC =
LIBS =
TARBALL = $(TOOLNAME)_$(VER_RCS)_$(ARCH).tar.gz
# Build files
BLDROOT = b
BDIR = $(BLDROOT)/$(ARCH)
OBJ = $(SRC:%.c=$(BDIR)/%.o)
DEP = $(SRC:%.c=$(BDIR)/%.d) $(HDR:%.h=$(BDIR)/%.hd)
# Library headers
LIBHDR = $(HDR:%=$(INCDIR)/%)
#------------------------------------------------------------------------------
# Description file
#
ifeq ($(BLDTYPE), exe)
TARGET = $(EXE)
DIZDIR = $(BINDIR)
ELFINFO = $(READELF) -sd $(EXE)
MOREELF = $(READELF) -sd $(EXE) | grep -e NEEDED -e FUNC.*GLOBAL.*UND
HELP = "\texe : Only make $(EXE)"
else ifeq ($(BLDTYPE), arc)
TARGET = $(ARC)
DIZDIR = $(LIBDIR)
ELFINFO = $(READELF) -sh $(ARC)
MOREELF = $(ELFINFO) | grep -e ^F -e FILE -e FUNC | grep -v FUNC.*LOCAL
@$(ECHO) "\tarc : Only make $(ARC)"
else ifeq ($(BLDTYPE), dll)
@$(ECHO) "Broken Makefile : Dynamically Linked Library (DLL) support not written yet."
# @$(ECHO) "\tdll : Only make $(DLL)"
false
else
@$(ECHO) "Broken Makefile : Unknown Build Type - BLDTYPE=exe|arc|dll"
false
endif
DIZ = $(DIZDIR)/$(TOOLNAME).diz
#------------------------------------------------------------------------------
# Compiler directives
# https://gcc.gnu.org/onlinedocs/gcc/Warning-Options.html
# gcc -Q --help=warning | sed -e 's/^\s*\(\-\S*\)\s*\[\w*\]/\1 /gp;d' | tr -d '\n'
#
CERR = -Wall -Wextra -Wformat -Werror
CFLG = -std=c99
CMP = -c
DBG = $(CMP) -g
CC = gcc $(CCDEF) $(CFLG) $(CMP) $(CERR)
#------------------------------------------------------------------------------
#------------------------------------------------------------------------------
# Default make rule
.PHONY : default
default: run
#------------------------------------------------------------------------------
.PHONY : help
help :
@$(ECHO) "Available make rules"
@$(ECHO) "\thelp : This help"
@$(ECHO) ""
@$(ECHO) "\tall : Build binary to: $(BINDIR)"
@$(ECHO) "\trun : Test binary with: $(EXE) $(TESTPRAM)"
@$(ECHO) ""
@$(ECHO) $(HELP)
@$(ECHO) "\tdiz : Only make $(DIZ)"
@$(ECHO) ""
@$(ECHO) "\ttidy : Erase build directory: $(BDIR)"
@$(ECHO) "\tclean : Erase build and binary dir: $(BDIR), $(BINDIR)"
@$(ECHO) ""
@$(ECHO) "\tdiz : Create dizcription file: $(DIZ)"
@$(ECHO) "\ttarball : Create archive of source & binary: $(TARBALL)"
@$(ECHO) ""
@$(ECHO) "\tsvnignore : Set SVN to ignore temp files"
@$(ECHO) "\tsvnadd : Add $(ARCH) build to svn repo"
@$(ECHO) ""
@$(ECHO) "\tinstall : Copy key files in to their respective system directories"
@$(ECHO) "\tuninstall : Remove key files from their system directories"
#------------------------------------------------------------------------------
.PHONY : install
install : uninstall $(EXE) default.conf $(SERVICE)
@$(ECHO) "$(tPRE)$(aRED) INSTALL: $(EXE) $(tPOST)"
# Don't overwrite an existing conf file
# There could be a LOT of work stored in it!
@if [ -f /etc/chatpad360.conf ] ; then \
$(ECHO) "$(aBRT)$(aWHT)Not overwriting config file /etc/chatpad360.conf$(aNRM) $(aWHT)(Loss of work)$(aNRM)" ;\
else \
$(SUDO) $(COPY) default.conf /etc/chatpad360.conf ;\
fi
$(SUDO) $(CHMOD) 600 /etc/chatpad360.conf
$(SUDO) $(CHOWN) root:root /etc/chatpad360.conf
@$(ECHO) ""
$(SUDO) $(COPY) $(EXE) /usr/sbin/$(TOOLNAME)
$(SUDO) $(CHMOD) 755 /usr/sbin/$(TOOLNAME)
$(SUDO) $(CHOWN) root:root /usr/sbin/$(TOOLNAME)
@$(ECHO) ""
$(SUDO) $(COPY) $(SERVICE) /etc/systemd/$(SERVICE)
$(SUDO) $(CHMOD) 755 /etc/systemd/$(SERVICE)
$(SUDO) $(CHOWN) root:root /etc/systemd/$(SERVICE)
@$(ECHO) ""
systemctl daemon-reload
systemctl enable $(SERVICE)
systemctl start $(SERVICE)
#------------------------------------------------------------------------------
.PHONY : uninstall
uninstall :
@$(ECHO) "$(tPRE)$(aRED) UNINSTALL: $(EXE) $(tPOST)"
systemctl stop $(SERVICE)
systemctl disable $(SERVICE)
systemctl daemon-reload
@$(ECHO) ""
$(SUDO) $(RMF) /usr/sbin/$(TOOLNAME)
$(SUDO) $(RMF) /etc/systemd/$(SERVICE)
@$(ECHO) "$(aBRT)$(aWHT)Not removing config file /etc/chatpad360.conf$(aNRM) $(aWHT)(Loss of work)$(aNRM)"
#$(SUDO) $(RMF) /etc/chatpad360.conf
# @$(ECHO) "$(aBRT)$(aWHT)Not removing logfile /var/log/chatpad360.log$(aNRM) $(aWHT)(Loss of history)$(aNRM)"
# #$(SUDO) $(RMF) /var/log/chatpad360.log
#------------------------------------------------------------------------------
all : $(TARGET) $(DIZ)
#------------------------------------------------------------------------------
run : $(EXE)
@$(ECHO) "$(tPRE)$(aRED) RUN: $(EXE) $(tPOST)"
[ -x $(EXE) ] && ./$(EXE) $(TESTPRAM)
#------------------------------------------------------------------------------
.PHONY : tidy
tidy :
@$(ECHO) "$(tPRE)$(aGRN) TIDY $(tPOST)"
$(RMRF) $(BDIR)/
.PHONY : clean
clean :
@$(ECHO) "$(tPRE)$(aGRN) CLEAN $(tPOST)"
$(RMRF) $(BDIR)/ $(BINDIR)/ $(LIBDIR)/ $(INCDIR)/
#------------------------------------------------------------------------------
# Create Dizcription file (when target updates)
#
.PHONY : diz
diz $(DIZ) : $(TARGET)
@$(ECHO) "$(tPRE)$(aCYN) DIZCRIPTION: $@ $(tPOST)"
@ >$(DIZ) $(ECHO) -n
@>>$(DIZ) $(ECHO) "timstamp : $(shell $(WHEN))"
@>>$(DIZ) $(ECHO) "whoami@host : $(shell $(WHO))@$(shell $(WHERE))"
@>>$(DIZ) $(ECHO) "uname -a : $(shell $(UNAME_A))"
@>>$(DIZ) $(OSREL)
@$(CAT) $(DIZ)
# This will not be helpful output during compile
@>>$(DIZ) $(ECHO) "----------------------------------------"
$(MOREELF) >>$(DIZ)
@>>$(DIZ) $(ECHO) "----------------------------------------"
$(ELFINFO) >>$(DIZ)
@$(ECHO) "----------------------------------------"
#------------------------------------------------------------------------------
.PHONY : svnadd
svnadd :
svn --force --parents --no-ignore add $(EXEDIR)
svn ci $(EXEDIR) -m "Checkin of $(ARCH) compiled code"
.PHONY : svnignore
svnignore :
$(MD) $(BDIR)
@ > $(BDIR)/$(SVNTMP) $(ECHO) -n
@>> $(BDIR)/$(SVNTMP) $(ECHO) $(TMP)
@>> $(BDIR)/$(SVNTMP) $(ECHO) $(BDIR)
#<->@>> $(BDIR)/$(SVNTMP) $(ECHO) $(BINDIR)
svn propset svn:ignore -F $(BDIR)/$(SVNTMP) .
#------------------------------------------------------------------------------
# Archve & Link rules
#
.PHONY : exe
exe $(EXE) : $(OBJ) $(DEP)
@$(ECHO) "$(tPRE)$(aYEL) LINK: $@ $(tPOST)"
$(MD) $(BINDIR)
$(LINK) $(EXE) $(OBJ) $(LIBS)
.PHONY : arc
arc $(ARC) : $(OBJ) $(DEP) $(LIBHDR)
@$(ECHO) "$(tPRE)$(aYEL) ARCHIVE: $@ $(tPOST)"
$(MD) $(LIBDIR)
$(AR) $(ARC) $(OBJ)
#.PHONY : dll
#$(DLL) : $(OBJ) $(DEP) $(LIBHDR)
#<->@$(ECHO) "$(tPRE)$(aYEL) ARCHIVE: $@ $(tPOST)"
#<->$(MD) $(LIBDIR)
#<->????
#------------------------------------------------------------------------------
.PHONY : tarball
tarball : $(EXE) $(DIZ) $(ARC) $(LIBHDR) $(SRC) $(HDR) $(MISC) Makefile
@$(ECHO) "$(tPRE)$(aWHT) TARBALL: $(TARBALL) $(tPOST)"
$(RMF) $(TARBALL)
$(TAR) $(TARBALL) $^
#------------------------------------------------------------------------------
# 'Install' the .h files in the lib directory (via the .hd)
#
$(INCDIR)/%.h : $(BDIR)/%.hd
@$(ECHO) "$(tPRE)$(aYEL) HEADER: $@ $(tPOST)"
@$(MD) $(INCDIR)
$(COPY) $(notdir $(<:.hd=.h)) $(dir $@)
#------------------------------------------------------------------------------
# Compile .c (via the .d)
#
$(BDIR)/%.o : $(BDIR)/%.d
@$(ECHO) "$(tPRE)$(aCYN) COMPILE: $@ $(tPOST)"
@$(MD) $(BDIR)
$(CC) -o $@ $(notdir $(<:.d=.c))
#------------------------------------------------------------------------------
# Create dependency from .h
#
$(BDIR)/%.hd : %.h
@$(ECHO) "$(tPRE)$(aWHT) DEPS: $@ $(tPOST)"
@$(MD) $(BDIR)
@$(DEL) $@
$(GETDEPS) $< > $@
@$(SEDI) 's,\($*\)\(\.o\)[ :]*,$(INCDIR)/\1.h $@ : ,g' $@
#------------------------------------------------------------------------------
# Create dependency from .c
#
$(BDIR)/%.d : %.c
@$(ECHO) "$(tPRE)$(aWHT) DEPS: $@ $(tPOST)"
@$(MD) $(BDIR)
@$(DEL) $@
$(GETDEPS) $< > $@
@$(SEDI) 's,\($*\)\.o[ :]*,\1.o $@ : ,g' $@
#------------------------------------------------------------------------------
ifneq (,$(DEP))
-include $(DEP)
endif