-
Notifications
You must be signed in to change notification settings - Fork 0
/
Makefile
138 lines (112 loc) · 3.41 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
# C Project Makefile
#========= Debug mode setup ========#
# Comment one line only!
# -DDEBUG: Debug testing
# -DVERBOSE: Print error messages
# -DTRACE: Trace malloc and free
# Uncomment this line for release
DEBUG := -DNDEBUG -UDEBUG -DVERBOSE -UTRACE
# Uncomment this line for debug
#DEBUG := -DDEBUG -DVERBOSE -DTRACE
#===== Compiler / linker setup =====#
CC := gcc
CFLAGS := -g -O3 -Wall -Wpedantic -Wextra -std=gnu99
DFLAGS := -MP -MMD
LFLAGS := -g -lm
INCLUDE :=
LIBRARY :=
#======== Source code setup ========#
# Directory for all project files and
# the main.c file.
INCLUDE_DIR := ./src
INCLUDE += -I$(INCLUDE_DIR)
SRC_DIR := ./src
# Source files
# CFILES excluses MAIN
CFILES := $(wildcard $(SRC_DIR)/*.c)
HFILES := $(wildcard $(INCLUDE_DIR)/*.h)
IFILES := $(wildcard $(SRC_DIR)/*.inc)
# Important files
MAKEFILE := Makefile
IMPORTANT := $(MAKEFILE) README.md
#=========== Build setup ===========#
# Directory for built files.
BUILD_DIR := build
OFILES := $(CFILES:$(SRC_DIR)/%.c=$(BUILD_DIR)/%.o)
DFILES := $(OFILES:%.o=%.d)
# Tar file of the entire project
TAR := ./project.tar.gz
#============ Main files ===========#
# Standalone text executable sources
# to link with the rest of the code.
MAIN_DIR := main
MCFILES := $(wildcard $(MAIN_DIR)/*.c)
MOFILES := $(MCFILES:$(MAIN_DIR)/%.c=$(BUILD_DIR)/$(MAIN_DIR)/%.o)
MDFILES := $(MOFILES:%.o=%.d)
ALL_EXECUTABLES := $(MCFILES:$(MAIN_DIR)/%.c=%.exe)
TESTS := $(filter test_%.exe,$(ALL_EXECUTABLES))
EXECUTABLES := $(filter-out test_%.exe,$(ALL_EXECUTABLES))
#========== libshared Setup =========#
LIB_DIR := lib
LIBSHARED_DIR := $(LIB_DIR)/libshared
INCLUDE += -I$(LIBSHARED_DIR)/include
LIBRARY += -L$(LIBSHARED_DIR)/bin -lshared
IMPORTANT += $(LIBSHARED_DIR)
#========== Documentation ==========#
# Doxygen documentation setup
DOC_DIR := docs
DOXYFILE := Doxyfile
DOXFILES := $(wildcard doxygen/*)
IMPORTANT += $(DOXYFILE) $(DOXFILES)
#============== Rules ==============#
# Default: just make executables
.PHONY: default
default: $(BUILD_DIR) libshared $(OFILES) $(EXECUTABLES)
# Make just the tests
.PHONY: tests
tests: $(BUILD_DIR) $(OFILES) $(TESTS)
# Default - make the executable
.PHONY: all
all: default tests
# Make libshared
.PHONY: libshared
libshared:
$(MAKE) -C $(LIBSHARED_DIR) default
# Put all the .o files in the build directory
$(BUILD_DIR):
-mkdir $@
-mkdir $@/$(MAIN_DIR)
# Compile the source files
.SECONDARY: $(DFILES)
.SECONDARY: $(OFILES)
$(BUILD_DIR)/%.o: $(SRC_DIR)/%.c $(MAKEFILE)
$(CC) $(CFLAGS) $(DFLAGS) $(DEBUG) $(INCLUDE) -c $< -o $@
# Compile the main method executables
.SECONDARY: $(MDFILES)
.SECONDARY: $(MOFILES)
$(BUILD_DIR)/$(MAIN_DIR)/%.o: $(MAIN_DIR)/%.c $(MAKEFILE)
$(CC) $(CFLAGS) $(DFLAGS) $(DEBUG) $(INCLUDE) -c $< -o $@
# Automatic dependency files
-include $(DFILES)
-include $(MDFILES)
# Documentation
.PHONY: documentation
documentation: $(DOC_DIR)
$(DOC_DIR): $(DOXFILES) $(CFILES) $(IFILES) $(HFILES) $(MCFILES)
doxygen Doxyfile
# Make executable for each driver
%.exe: $(BUILD_DIR)/$(MAIN_DIR)/%.o $(OFILES)
$(CC) -o $@ $^ $(LIBRARY) $(LFLAGS)
#============== Clean ==============#
# Clean up build files and executable
.PHONY: clean
clean:
-rm -rf $(BUILD_DIR) $(EXECUTABLES) $(TESTS)
$(MAKE) -C $(LIBSHARED_DIR) clean
#============= Archive =============#
# Package all the files into a tar.
.PHONY: tar
tar: $(TAR)
$(TAR): $(CFILES) $(HFILES) $(DOXFILES) $(IMPORTANT) $(LIB_DIR)
tar -czvf $@ $^
#===================================#