-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMakefile
156 lines (108 loc) · 3.35 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
147
148
149
150
151
152
153
154
155
156
# usage:
# $ make
# $ make v=1 # verbose ouput
# $ make debug=y # debug
# target executable file or .a or .so or .out
TARGET := a.out
# Add additional include paths
INCLUDES :=
# Extra flags to give to compilers when they are supposed to invoke the linker,
# ‘ld’, such as -L. Libraries (-lfoo) should be added to the LDLIBS variable instead.
LDFLAGS :=
# Library flags or names given to compilers when they are supposed to invoke the linker,
# ‘ld’. LOADLIBES is a deprecated (but still supported) alternative to LDLIBS.
# Non-library linker flags, such as -L, should go in the LDFLAGS variable.
LDLIBS :=
# General compiler flags
CFLAGS = -std=c11
CXXFLAGS = -std=c++11
# Path to the source directory, relative to the makefile
SRC_DIR =
# Build and output paths
BUILD_DIR = build/
# Search Path for All Prerequisites
VPATH :=
# Compiler used
CC := gcc
CXX := g++
ARFLAGS := cr
# Define the installation command package
define run_install
install -v -d destination
install -v -p -D source destination
install -v -p -D -m 0755 source destination
endef
#### END PROJECT SETTINGS ####
# Generally should not need to edit below this line
.SECONDARY:
ifeq ($(v),1)
Q =
NQ = true
else
Q = @
NQ = echo
endif
ifeq ($(debug), y)
CFLAGS += -Wall -Wextra -D DEBUG -g
CXXFLAGS += -Wall -Wextra -D DEBUG -g
else
CFLAGS += -Wall -Wextra -D NDEBUG -O3
CXXFLAGS += -Wall -Wextra -D NDEBUG -O3
endif
%.so: CFLAGS += -fPIC -shared
%.so: CXXFLAGS += -fPIC -shared
VPATH := $(SRC_DIR) $(BUILD_DIR)
# Set the object file names, with the source directory stripped
# from the path, and the build path prepended in its place
OBJECTS := $(patsubst $(SRC_DIR)%.c,$(BUILD_DIR)%.o, $(wildcard $(SRC_DIR)*.c))
OBJECTS += $(patsubst $(SRC_DIR)%.cpp,$(BUILD_DIR)%.o, $(wildcard $(SRC_DIR)*.cpp))
# Set the dependency files that will be used to add header dependencies
DEPS := $(OBJECTS:.o=.d)
all: $(TARGET)
# Create static library
%.a: $(OBJECTS)
@$(NQ) "Generating static lib file..." $@
$(Q)$(AR) $(ARFLAGS) $(BUILD_DIR)$@ $^
# Create dynamic library
%.so: $(OBJECTS)
@$(NQ) "Generating dynamic lib file..." $@
$(Q)$(CXX) $^ -o $(BUILD_DIR)$@ $(LDFLAGS) $(LDLIBS)
# Generating executable file
%.out: $(OBJECTS)
@$(NQ) "Generating executable file..." $@
$(Q)$(CXX) $^ -o $(BUILD_DIR)$@ $(LDFLAGS) $(LDLIBS)
.PHONY: run
run:
$(Q)./$(BUILD_DIR)/$(TARGET)
.PHONY: install
install:
$(run_install)
# Removes all build files
.PHONY: clean
clean:
@$(NQ) "Clear build directory of $(TARGET)..."
$(Q)$(RM) -r $(BUILD_DIR)
$(BUILD_DIR):
@$(NQ) Creating build directory ...
$(Q) mkdir -p $@
@$(NQ) Build directory created!
@$(NQ)
# Function used to check variables. Use on the command line:
# make print-VARNAME
# Useful for debugging and adding features
d-%::
@$(NQ) '$*=(*)'
@$(NQ) ' origin = $(origin *)'
@$(NQ) ' flavor = $(flavor *)'
@$(NQ) ' value = $(value $*)'
# Source file rules
# After the first compilation they will be joined with the rules from the
# dependency files to provide header dependencies
$(BUILD_DIR)%.o: %.c | $(BUILD_DIR)
@$(NQ) "Compiling: $< -> $@"
$(Q)$(CC) $(CFLAGS) $(INCLUDES) -MP -MMD -c $< -o $@
$(BUILD_DIR)%.o: %.cpp | $(BUILD_DIR)
@$(NQ) "Compiling: $< -> $@"
$(Q)$(CXX) $(CXXFLAGS) $(INCLUDES) -MP -MMD -c $< -o $@
# Add dependency files, if they exist
-include $(DEPS)