-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMakefile
103 lines (80 loc) · 1.85 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
# Helal anwar
# May 30 2023
# A generic build template for C/C++ programs
# Thanks to Thomas Daley(https://gist.github.com/tomdaley92/190c68e8a84038cc91a5459409e007df)
# executable name
EXE = app
# C compiler
CC = clang
# C++ compiler
CXX = clang++
# linker
LD = clang++
# C flags
CFLAGS = -std=c2x
# C++ flags
CXXFLAGS = -std=c++2b
# C/C++ flags
CPPFLAGS = -Wall
# dependency-generation flags
DEPFLAGS = -MMD -MP
# linker flags
LDFLAGS =
# library flags
LDLIBS =
# build directories
BIN = bin
OBJ = obj
SRC = src
SOURCES := $(wildcard $(SRC)/*.c $(SRC)/*.cc $(SRC)/*.cpp $(SRC)/*.cxx)
OBJECTS := \
$(patsubst $(SRC)/%.c, $(OBJ)/%.o, $(wildcard $(SRC)/*.c)) \
$(patsubst $(SRC)/%.cc, $(OBJ)/%.o, $(wildcard $(SRC)/*.cc)) \
$(patsubst $(SRC)/%.cpp, $(OBJ)/%.o, $(wildcard $(SRC)/*.cpp)) \
$(patsubst $(SRC)/%.cxx, $(OBJ)/%.o, $(wildcard $(SRC)/*.cxx))
# include compiler-generated dependency rules
DEPENDS := $(OBJECTS:.o=.d)
# compile C source
COMPILE.c = $(CC) $(DEPFLAGS) $(CFLAGS) $(CPPFLAGS) -c -o $@
# compile C++ source
COMPILE.cxx = $(CXX) $(DEPFLAGS) $(CXXFLAGS) $(CPPFLAGS) -c -o $@
# link objects
LINK.o = $(LD) $(LDFLAGS) $(LDLIBS) $(OBJECTS) -o $@
.DEFAULT_GOAL = all
.PHONY: all
all: $(BIN)/$(EXE)
$(BIN)/$(EXE): $(SRC) $(OBJ) $(BIN) $(OBJECTS)
$(LINK.o)
$(SRC):
mkdir -p $(SRC)
$(OBJ):
mkdir -p $(OBJ)
$(BIN):
mkdir -p $(BIN)
$(OBJ)/%.o: $(SRC)/%.c
$(COMPILE.c) $<
$(OBJ)/%.o: $(SRC)/%.cc
$(COMPILE.cxx) $<
$(OBJ)/%.o: $(SRC)/%.cpp
$(COMPILE.cxx) $<
$(OBJ)/%.o: $(SRC)/%.cxx
$(COMPILE.cxx) $<
# force rebuild
.PHONY: remake
remake: clean $(BIN)/$(EXE)
# execute the program
.PHONY: run
run: $(BIN)/$(EXE)
./$(BIN)/$(EXE)
# remove previous build and objects
.PHONY: clean
clean:
$(RM) $(OBJECTS)
$(RM) $(DEPENDS)
$(RM) $(BIN)/$(EXE)
# remove everything except source
.PHONY: reset
reset:
$(RM) -r $(OBJ)
$(RM) -r $(BIN)
-include $(DEPENDS)