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
|
CC = gcc -g
CFLAGS = -Og -Wall -Werror -Wstrict-prototypes
AS = as -g
ASFLAGS =
CPPFLAGS = -Iinclude
LDLIBS = -Llibcsapp -lcsapp
SED = sed
# Recognize operating system
ifeq ($(shell uname -s), Darwin)
CPPFLAGS += -DMACOS
SED = gsed
endif
ifeq ($(shell uname -s), Linux)
CPPFLAGS += -DLINUX
endif
ifeq ($(shell uname -s), FreeBSD)
CPPFLAGS += -DFREEBSD
endif
# Pass "VERBOSE=1" at command line to display command being invoked by GNU Make
ifneq ($(VERBOSE), 1)
.SILENT:
endif
LIBSRC_C = $(wildcard libcsapp/*.c)
LIBSRC_S = $(wildcard libcsapp/*.s)
LIBSRC_H = $(wildcard include/*.h)
LIBSRCS = $(LIBSRC_C) $(LIBSRC_S) $(LIBSRC_H)
LIBOBJS = $(LIBSRC_C:%.c=%.o)
ifneq ($(shell uname -s), Darwin)
LIBOBJS += $(LIBSRC_S:%.s=%.o)
endif
LIB = libcsapp/libcsapp.a
SRC_C = $(wildcard *.c)
SRC_S = $(wildcard *.s)
SRC_H = $(wildcard *.h)
SRCS = $(SRC_C) $(SRC_S)
OBJS = $(SRC_C:%.c=%.o)
SOURCES = $(SRCS) $(LIBSRCS)
OBJECTS = $(OBJS) $(LIBOBJS)
DEPFILES = $(foreach f,$(SRC_C) $(LIBSRC_C),\
$(dir $(f))$(patsubst %.c,.%.d,$(notdir $(f))))
ARCHIVE = so$(shell date +'%y')_$(shell basename $(PWD))
FILES = Makefile Makefile.include $(EXTRA-FILES)
all: $(DEPFILES) $(LIB) $(PROGS)
$(LIB): $(LIBOBJS)
# Generate dependencies automatically
ifeq ($(words $(findstring $(MAKECMDGOALS), archive clean)), 0)
-include $(DEPFILES)
endif
# Disable all built-in recipes and define our own
.SUFFIXES:
.%.d: %.c
$(CC) $(CPPFLAGS) -MM -MG -o $@ $<
%.o: %.c .%.d
@echo "[CC] $@ <- $<"
$(CC) $(CPPFLAGS) $(CFLAGS) -c -o $@ $<
%.o: %.s
@echo "[AS] $@ <- $<"
$(AS) $(ASFLAGS) -c -o $@ $<
%.a:
@echo "[AR] $@ <- $^"
$(AR) rc $@ $^
%: %.o $(LIB)
@echo "[LD] $@ <- $^"
$(CC) $(LDFLAGS) -o $@ $^ $(LDLIBS)
clean:
rm -vf $(PROGS) $(OBJECTS) $(DEPFILES) $(LIB)
rm -vf $(shell find -L . -iname '*~')
rm -vf $(ARCHIVE).tar.gz
rm -vrf $(EXTRA-CLEAN) *.dSYM
format:
clang-format --style=file -i $(LIBSRC_C) $(LIBSRC_H) $(SRC_C) $(SRC_H)
archive: clean
mkdir -p $(ARCHIVE) $(ARCHIVE)/libcsapp $(ARCHIVE)/include
cp -L $(SRCS) $(SRC_H) $(FILES) $(ARCHIVE)/
cp -L $(LIBSRCS) $(ARCHIVE)/libcsapp/
cp -L $(LIBSRC_H) $(ARCHIVE)/include/
for f in $(SRCS:%=$(ARCHIVE)/%); do \
$(SED) --in-place='' -e '/^#if.*STUDENT/,/^#endif.*STUDENT/d' $$f; \
done
tar cvzhf $(ARCHIVE).tar.gz $(ARCHIVE)
rm -rf $(ARCHIVE)
.PHONY: all clean format archive
# vim: ts=8 sw=8 noet
|