Repository navigation
Expand file tree
/
Copy pathMakefile
More file actions
134 lines (102 loc) · 3.34 KB
/
Copy pathMakefile
File metadata and controls
134 lines (102 loc) · 3.34 KB
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
#
# 'make' build executable file 'WebCerver'
# 'make clean' removes all .o and executable files
#
# define the C compiler to use
CC = clang
# define any compile-time flags
CFLAGS := -Wall -Wextra -g -Ilib/sds -Ilib/argtable3
# define library paths in addition to /usr/lib
# if I wanted to include libraries not in /usr/lib I'd specify
# their path using -Lpath, something like:
LFLAGS = -lm
# define output directory
OUTPUT := output
# define source directory
SRC := src
# define tests directory
TEST := tests
# define include directory
INCLUDE := include
# define lib directory
LIB := lib
ifeq ($(OS),Windows_NT)
MAIN := WebCerver.exe
SOURCEDIRS := $(SRC) \ $(LIB)
INCLUDEDIRS := $(INCLUDE)
LIBDIRS := $(LIB)
FIXPATH = $(subst /,\,$1)
RM := del /q /f
MD := mkdir
else
MAIN := WebCerver
SOURCEDIRS := $(shell find $(SRC) -type d) \ $(shell find $(LIB) -type d)
INCLUDEDIRS := $(shell find $(INCLUDE) -type d)
LIBDIRS := $(shell find $(LIB) -type d)
FIXPATH = $1
RM = rm -f
MD := mkdir -p
endif
# define any directories containing header files other than /usr/include
INCLUDES := $(patsubst %,-I%, $(INCLUDEDIRS:%/=%))
# define the C libs
LIBS := $(patsubst %,-L%, $(LIBDIRS:%/=%))
# define the C source files
SOURCES := $(wildcard $(patsubst %,%/*.c, $(SOURCEDIRS)))
# define the C object files
OBJECTS := $(SOURCES:.c=.o)
# define the dependency output files
DEPS := $(OBJECTS:.o=.d)
# define test source, object, dependency and executable files
TEST_SOURCES := $(wildcard $(TEST)/*.c)
TEST_OBJECTS := $(TEST_SOURCES:.c=.o)
TEST_DEPS := $(TEST_OBJECTS:.o=.d)
TEST_BINS := $(patsubst $(TEST)/%.c,$(OUTPUT)/tests/%,$(TEST_SOURCES))
# define http test link dependencies
TEST_HTTP_OBJECTS := src/http/http.o lib/sds/sds.o
#
# The following part of the makefile is generic; it can be used to
# build any executable just by changing the definitions above and by
# deleting dependencies appended to the file from 'make depend'
#
OUTPUTMAIN := $(call FIXPATH,$(OUTPUT)/$(MAIN))
all: $(OUTPUT) $(MAIN)
@echo Executing 'all' complete!
$(OUTPUT):
$(MD) $(OUTPUT)
$(MAIN): $(OBJECTS)
$(CC) $(CFLAGS) $(INCLUDES) -o $(OUTPUTMAIN) $(OBJECTS) $(LFLAGS) $(LIBS)
$(OUTPUT)/tests/test_http_parser: tests/test_http_parser.o $(TEST_HTTP_OBJECTS)
$(CC) $(CFLAGS) $(INCLUDES) -o $@ $^ $(LFLAGS) $(LIBS)
$(OUTPUT)/tests/test_network: tests/test_network.o src/net/network.o
$(CC) $(CFLAGS) $(INCLUDES) -o $@ $^ $(LFLAGS) $(LIBS)
$(OUTPUT)/tests:
$(MD) $(OUTPUT)/tests
$(TEST_BINS): | $(OUTPUT)/tests
# include all .d files
-include $(DEPS) $(TEST_DEPS)
# this is a suffix replacement rule for building .o's and .d's from .c's
# it uses automatic variables $<: the name of the prerequisite of
# the rule(a .c file) and $@: the name of the target of the rule (a .o file)
# -MMD generates dependency output files same name as the .o file
# (see the gnu make manual section about automatic variables)
.c.o:
$(CC) $(CFLAGS) $(INCLUDES) -c -MMD $< -o $@
.PHONY: clean
clean:
$(RM) $(OUTPUTMAIN)
$(RM) $(call FIXPATH,$(OBJECTS))
$(RM) $(call FIXPATH,$(DEPS))
$(RM) $(call FIXPATH,$(TEST_OBJECTS))
$(RM) $(call FIXPATH,$(TEST_DEPS))
$(RM) $(call FIXPATH,$(TEST_BINS))
@echo Cleanup complete!
run: all
./$(OUTPUTMAIN)
@echo Executing 'run: all' complete!
.PHONY: test
test: $(TEST_BINS)
@for test in $(TEST_BINS); do \
echo "Running $$test"; \
./$$test || exit 1; \
done