-
Notifications
You must be signed in to change notification settings - Fork 0
/
package.mk
120 lines (96 loc) · 2.17 KB
/
package.mk
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
# overridable parameters
PREFIX?=/usr/local
EXE?=${NAME}
LIB?=lib${NAME}.a
TST?=test-${NAME}
PROG?=$(wildcard main.c)
TEST?=$(wildcard test.c)
HDRS?=$(wildcard ${NAME}*.h)
SRCS?=$(wildcard ${NAME}*.c)
LANGUAGE?=-std=c11
WARNINGS?=-pedantic -Wall -Wextra -Wno-unused
DEBUG?=-g
OPTIMIZE?=-Os
DEFINES?=
INCLUDES?=-I.
################################################################################
UPNAME=$(shell echo ${NAME} | tr 'a-z' 'A-Z')
CFLAGS=${LANGUAGE} ${WARNINGS} ${DEBUG} ${OPTIMIZE}
CPPFLAGS=${DEFINES} -DIN_${UPNAME} ${INCLUDES}
OBJS=$(SRCS:.c=.o)
ALLTGTS=$(if ${PROG},${EXE}) $(if ${SRCS},${LIB}) $(if ${TEST},${TST})
ALLSRCS=$(if ${PROG},${PROG}) $(if ${SRCS},${SRCS}) $(if ${TEST},${TEST})
ALLOBJS=$(ALLSRCS:.c=.o)
ALLDEPS=$(ALLSRCS:.c=.d)
# define default target first
default: all
# include/update dependencies
-include ${ALLDEPS}
# main target
all: ${ALLTGTS}
# rule for the program
ifneq (${EXE},)
${EXE}: ${PROG} ${LIB}
${CC} ${CPPFLAGS} -DIN_${UPNAME}_PROG ${CFLAGS} -o $@ $^
size $@
endif
# rule for the library
ifneq (${LIB},)
${LIB}: ${OBJS}
rm -f $@ && ${AR} -crs $@ $^
size $@
endif
# rules for the test
ifeq (${TEST},)
test:
@echo "This package has no tests."
.PHONY: test
else
test: ${TST}
./${TST}
.PHONY: test
${TST}: ${TEST} ${LIB}
${CC} ${CPPFLAGS} -DIN_${UPNAME}_TEST ${CFLAGS} -o $@ $^
size $@
endif
# generate dependencies
%.d: %.c
@echo updating $@
@set -e; rm -f $@; \
${CC} -M ${CPPFLAGS} $< > $@.$$$$; \
sed 's,\($*\)\.o[ :]*,\1.o $@ : ,g' < $@.$$$$ > $@; \
rm -f $@.$$$$
# build rule for C files
%.o: %.c
${CC} ${CPPFLAGS} ${CFLAGS} -c -o $@ $<
# package install
install: all
ifneq (${PROG},)
mkdir -p ${PREFIX}/bin
install ${BIN} ${PREFIX}/bin
endif
ifneq (${SRCS},)
mkdir -p ${PREFIX}/lib
install ${LIB} ${PREFIX}/lib
endif
ifneq (${HDRS},)
mkdir -p ${PREFIX}/include
install -t ${PREFIX}/include ${HDRS}
endif
.PHONY: install
# package uninstall
uninstall:
ifneq (${PROG},)
rm -f ${PREFIX}/bin/${EXE}
endif
ifneq (${SRCS},)
rm -f ${PREFIX}/lib/${LIB}
endif
ifneq (${HDRS},)
for f in ${HDRS}; do rm -f ${PREFIX}/include/$$f; done
endif
.PHONY: uninstall
# source cleanup
clean:
rm -f ${ALLOBJS} ${ALLDEPS} ${ALLTGTS}
.PHONY: clean