-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmakefile
146 lines (114 loc) · 3.04 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
# Global settings
CC=gcc
CFLAGS=-c -g -Wall -I $(HDR_DIR)
LDFLAGS=-g
DEPFLAGS=-I $(HDR_DIR)
# Main binary settings
SRC_DIR=src
HDR_DIR=hdr
BIN_DIR=bin
TST_DIR=test
OBJ_DIR=obj
DEP_DIR=dep
SRCS=$(wildcard $(SRC_DIR)/*.c)
OBJS=$(addprefix $(OBJ_DIR)/,$(addsuffix .o,$(basename $(notdir $(SRCS)))))
LOBJS=$(addprefix $(OBJ_DIR)/,$(addsuffix .l.o,$(basename $(notdir $(SRCS)))))
DEPS=$(addprefix $(DEP_DIR)/,$(addsuffix .d,$(basename $(notdir $(SRCS)))))
NAME=lambda
TARGETS=$(BIN_DIR)/lib$(NAME).a $(BIN_DIR)/$(NAME)
# Test settings
T_SRC_DIR=test/src
T_BIN_DIR=test/bin
T_OBJ_DIR=test/obj
T_DEP_DIR=test/dep
T_SRCS=$(wildcard $(T_SRC_DIR)/*.c)
T_OBJS=$(addprefix $(T_OBJ_DIR)/,$(addsuffix .o,\
$(basename $(notdir $(T_SRCS)))))
T_DEPS=$(addprefix $(T_DEP_DIR)/,$(addsuffix .d,\
$(basename $(notdir $(T_SRCS)))))
T_TARGETS=$(addprefix $(T_BIN_DIR)/,$(addsuffix .test,\
$(basename $(notdir $(T_SRCS)))))
# Phony rules
.PHONY: default
default: all
.PHONY: all
all: release run-tests
.PHONY: run-tests
run-tests: build-tests
@printf 'Running tests: \n'
@for test_exec in $(T_TARGETS); do\
printf "\tTest \"$$test_exec\"...\n";\
./$$test_exec &&\
printf '\t\tSuccess\n' ||\
printf '\t\tFailed\n';\
done
@echo 'Done'
.PHONY: build-tests
build-tests: $(T_TARGETS)
.PHONY: release
release: $(TARGETS)
.PHONY: clean
clean:
@printf 'Cleaning...\t'
@rm -rf $(OBJ_DIR)
@rm -rf $(DEP_DIR)
@rm -rf $(BIN_DIR)
@rm -rf $(T_OBJ_DIR)
@rm -rf $(T_DEP_DIR)
@rm -rf $(T_BIN_DIR)
@echo 'Done'
.PHONY: init-structure
init-structure:
@printf 'Creating working structure...\t'
@mkdir -p $(SRC_DIR)
@mkdir -p $(HDR_DIR)
@mkdir -p $(TST_DIR)
@mkdir -p $(T_SRC_DIR)
@echo 'Done'
# Binary building rules
$(DEP_DIR)/%.d: $(SRC_DIR)/%.c
@mkdir -p $(DEP_DIR)
@$(CC) $(DEPFLAGS) -MM $< -MT \
$(addprefix $(OBJ_DIR)/,$(addsuffix .o,$(basename $(notdir $<)))) > $@
-include $(DEPS)
$(BIN_DIR)/lib$(NAME).a: $(LOBJS)
@printf 'Building $@ from $^...\t'
@mkdir -p $(BIN_DIR)
@ar rcs $@ $^
@echo 'Done'
$(BIN_DIR)/$(NAME): $(OBJS)
@printf 'Building $@ from $^...\t'
@mkdir -p $(BIN_DIR)
@$(CC) $(LDFLAGS) -o $@ $^
@echo 'Done'
$(OBJ_DIR)/%.l.o: $(SRC_DIR)/%.c
@printf 'Building $@ from $<...\t'
@mkdir -p $(OBJ_DIR)
@$(CC) $(CFLAGS) -DLIB -o $@ $<
@echo 'Done'
$(OBJ_DIR)/%.o: $(SRC_DIR)/%.c
@printf 'Building $@ from $<...\t'
@mkdir -p $(OBJ_DIR)
@$(CC) $(CFLAGS) -DEXE -o $@ $<
@echo 'Done'
# Test building rules
$(T_DEP_DIR)/%.d: $(T_SRC_DIR)/%.c
@mkdir -p $(T_DEP_DIR)
@$(CC) $(DEPFLAGS) -MM $< -MT \
$(addprefix $(T_OBJ_DIR)/,$(addsuffix .o,$(basename $(notdir $<)))) > $@
-include $(T_DEPS)
$(T_OBJ_DIR)/%.o: $(T_SRC_DIR)/%.c
@printf 'Building $@ from $<...\t'
@mkdir -p $(T_OBJ_DIR)
@$(CC) $(CFLAGS) -DTEST -o $@ $<
@echo 'Done'
$(T_BIN_DIR)/%.test: $(T_OBJ_DIR)/%.o $(T_OBJ_DIR)/lib$(NAME).a
@printf 'Building $@ from $<...\t'
@mkdir -p $(T_BIN_DIR)
@$(CC) $(LDFLAGS) -o $@ $< -L$(T_OBJ_DIR) -l$(NAME)
@echo 'Done'
$(T_OBJ_DIR)/lib$(NAME).a: $(BIN_DIR)/lib$(NAME).a
@mkdir -p $(T_OBJ_DIR)
@printf 'Copying $@ from $^...\t'
@cp $< $@
@echo 'Done'