-
Notifications
You must be signed in to change notification settings - Fork 0
/
Makefile
117 lines (82 loc) · 3.22 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
SRCEXT := c
OBJEXT := o
HEADEREXT := h
# directories
SRC_DIR := src
BUILD_DIR := build
TARGET_DIR := target
LIB_DIR := lib
TEST_DIR := tests
INCLUDE_DIR := include
# compiler options
LIB_NAME := xalloc
CC := gcc
CFLAGS := -Wall -Ofast
CDBGFLAGS := -Wall -g -D DEBUG
DBG := gdb -q
INCLUDE := -I $(INCLUDE_DIR) -I $(LIB_DIR)
LIB := -L$(LIB_DIR) -lm
LIBRARIES := $(shell find $(LIB_DIR)/ -name "*.a")
# targets
TARGET_NAME := lib$(LIB_NAME)
TARGET := $(TARGET_DIR)/$(TARGET_NAME).a
DBG_TARGET := $(TARGET_DIR)/$(TARGET_NAME)-dbg.a
HDR_TARGET := $(TARGET_DIR)/$(TARGET_NAME).$(HEADEREXT)
SOURCES := $(shell find $(SRC_DIR)/ -name "*."$(SRCEXT))
HEADERS := $(shell find $(INCLUDE_DIR)/ -name "*."$(HEADEREXT))
TESTSRC := $(shell find $(TEST_DIR)/ -name "*."$(SRCEXT))
## release build
rel: mkdirp $(HDR_TARGET) $(TARGET)
OBJECTS := $(patsubst $(SRC_DIR)/%.$(SRCEXT), $(BUILD_DIR)/%-rel.$(OBJEXT), $(shell find $(SRC_DIR)/ -name "*."$(SRCEXT)))
$(OBJECTS): $(SOURCES) $(HEADERS)
@cd $(SRC_DIR) && $(MAKE)
$(TARGET): $(LIBRARIES) $(OBJECTS)
ar rcs $(TARGET) $(BUILD_DIR)/*-rel.$(OBJEXT)
## debug build
dbg: mkdirp $(HDR_TARGET) $(DBG_TARGET)
DBG_OBJECTS := $(patsubst $(SRC_DIR)/%.$(SRCEXT), $(BUILD_DIR)/%-dbg.$(OBJEXT), $(shell find $(SRC_DIR)/ -name "*."$(SRCEXT)))
$(DBG_OBJECTS): $(SOURCES) $(HEADERS)
@cd $(SRC_DIR) && $(MAKE) dbg
$(DBG_TARGET): $(LIBRARIES) $(DBG_OBJECTS)
ar rcs $(DBG_TARGET) $(BUILD_DIR)/*-dbg.$(OBJEXT)
## make lib headers
$(HDR_TARGET): $(INCLUDE_DIR)/$(TARGET_NAME).$(HEADEREXT)
@cp $(INCLUDE_DIR)/$(TARGET_NAME).$(HEADEREXT) $(HDR_TARGET)
$(info make $(HDR_TARGET))
## build libraries
$(LIBRARIES):
@cd $(LIB_DIR) && $(MAKE)
## testing / execution
test: rel $(TESTSRC)
@$(CC) $(CFLAGS) -I $(TARGET_DIR) $(TEST_DIR)/test.$(SRCEXT) -o $(TEST_DIR)/test-rel.out -L$(TARGET_DIR) -l$(LIB_NAME) $(LIB)
./$(TEST_DIR)/test-rel.out
testdbg: dbg $(TESTSRC)
@$(CC) $(CDBGFLAGS) -I $(TARGET_DIR) $(DBG_OBJECTS) $(TEST_DIR)/test.$(SRCEXT) -o $(TEST_DIR)/test-dbg.out $(LIB)
$(DBG) $(TEST_DIR)/test-dbg.out
test-fail: rel $(TESTSRC)
@$(CC) $(CFLAGS) -I $(TARGET_DIR) $(TEST_DIR)/test-fail.$(SRCEXT) -o $(TEST_DIR)/test-fail-rel.out -L$(TARGET_DIR) -l$(LIB_NAME) $(LIB)
./$(TEST_DIR)/test-fail-rel.out
test-fail-dbg: dbg $(TESTSRC)
@$(CC) $(CDBGFLAGS) -I $(TARGET_DIR) $(DBG_OBJECTS) $(TEST_DIR)/test-fail.$(SRCEXT) -o $(TEST_DIR)/test-fail-dbg.out $(LIB)
$(DBG) $(TEST_DIR)/test-fail-dbg.out
test-no-malloc: rel $(TESTSRC)
@$(CC) $(CFLAGS) -I $(TARGET_DIR) $(TEST_DIR)/test-no-malloc.$(SRCEXT) -o $(TEST_DIR)/test-no-malloc-rel.out -L$(TARGET_DIR) -l$(LIB_NAME) $(LIB)
./$(TEST_DIR)/test-no-malloc-rel.out
test-no-malloc-dbg: dbg $(TESTSRC)
@$(CC) $(CDBGFLAGS) -I $(TARGET_DIR) $(DBG_OBJECTS) $(TEST_DIR)/test-no-malloc.$(SRCEXT) -o $(TEST_DIR)/test-no-malloc-dbg.out $(LIB)
$(DBG) $(TEST_DIR)/test-no-malloc-dbg.out
## mkdirp
mkdirp:
@mkdir -p $(BUILD_DIR)
@mkdir -p $(TARGET_DIR)
## cleanup
clean:
@cd $(SRC_DIR) && $(MAKE) clean
@cd $(LIB_DIR) && $(MAKE) clean
@rm -rf $(TEST_DIR)/*.out
cleaner:
@cd $(SRC_DIR) && $(MAKE) cleaner
@cd $(LIB_DIR) && $(MAKE) cleaner
@rm -rf $(TEST_DIR)/*.out
@rm -rf $(BUILD_DIR)
@rm -rf $(TARGET_DIR)