-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMakefile
346 lines (280 loc) · 10.6 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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
# ######################################################################
# We include the user-specified variables first
include build.config
ifndef PROJNAME
$(error $$PROJNAME not defined. Is the 'build.config' file missing?)
endif
VERSION?=0.0.0
# ######################################################################
# Set some colours for $(ECHO) to use
NONE:=\e[0m
INV:=\e[7m
RED:=\e[31m
GREEN:=\e[32m
BLUE:=\e[34m
CYAN:=\e[36m
YELLOW:=\e[33m
# ######################################################################
# We record the start time, for determining how long the build took
START_TIME:=$(shell date +"%s")
# ######################################################################
# Some housekeeping to determine if we are running on a POSIX
# platform or on Windows
MAKEPROGRAM_EXE=$(findstring exe,$(MAKE))
MAKEPROGRAM_MINGW=$(findstring mingw,$(MAKE))
GITSHELL=$(findstring Git,$(SHELL))
GITSHELL+=$(findstring git,$(SHELL))
# TODO: Remember that freebsd might use not gmake/gnu-make; must add in
# some diagnostics so that user gets a message to install gnu make.
ifneq ($(MAKEPROGRAM_EXE),)
ifeq ($(strip $(GITSHELL)),)
$(error On windows this must be executed from the Git bash shell)
endif
HOME=$(subst \,/,$(HOMEDRIVE)$(HOMEPATH))
PLATFORM:=Windows
EXE_EXT:=.exe
LIB_EXT:=.dll
PLATFORM_LDFLAGS:=--L$(HOME)/lib lmingw32 -lws2_32 -lmsvcrt -lgcc
PLATFORM_CFLAGS:= -D__USE_MINGW_ANSI_STDIO
ECHO:=echo -e
endif
ifneq ($(MAKEPROGRAM_MINGW),)
ifeq ($(strip $(GITSHELL)),)
$(error On windows this must be executed from the Git bash shell)
endif
HOME=$(subst \,/,$(HOMEDRIVE)$(HOMEPATH))
PLATFORM:=Windows
EXE_EXT:=.exe
LIB_EXT:=.dll
PLATFORM_LDFLAGS:=-L$(HOME)/lib -lmingw32 -lws2_32 -lmsvcrt -lgcc
PLATFORM_CFLAGS:= -D__USE_MINGW_ANSI_STDIO
ECHO:=echo -e
endif
# If neither of the above are true then we assume a working POSIX
# platform
ifeq ($(PLATFORM),)
PLATFORM:=POSIX
EXE_EXT:=.elf
LIB_EXT:=.so
PLATFORM_LDFLAGS:= -lpthread -ldl
ECHO:=echo
REAL_SHOW:=real-show
endif
# ######################################################################
# Set the output directories, output filenames
OUTDIR=debug
ifneq (,$(findstring debug,$(MAKECMDGOALS)))
OUTDIR=debug
endif
ifneq (,$(findstring release,$(MAKECMDGOALS)))
OUTDIR=release
endif
TARGET:=$(shell $(GCC) -dumpmachine)
OUTLIB:=$(OUTDIR)/lib/$(TARGET)
OUTBIN:=$(OUTDIR)/bin/$(TARGET)
OUTOBS:=$(OUTDIR)/obs/$(TARGET)
OUTDIRS:=$(OUTLIB) $(OUTBIN) $(OUTOBS) include
# ######################################################################
# Declare the final outputs
BINPROGS:=\
$(foreach fname,$(MAIN_PROGRAM_CSOURCEFILES),$(OUTBIN)/$(fname)$(EXE_EXT))\
$(foreach fname,$(MAIN_PROGRAM_CPPSOURCEFILES),$(OUTBIN)/$(fname)$(EXE_EXT))
DYNLIB:=$(OUTLIB)/lib$(PROJNAME)-$(VERSION)$(LIB_EXT)
STCLIB:=$(OUTLIB)/lib$(PROJNAME)-$(VERSION).a
DYNLNK_TARGET:=lib$(PROJNAME)-$(VERSION)$(LIB_EXT)
STCLNK_TARGET:=lib$(PROJNAME)-$(VERSION).a
DYNLNK_NAME:=$(OUTLIB)/lib$(PROJNAME)$(LIB_EXT)
STCLNK_NAME:=$(OUTLIB)/lib$(PROJNAME).a
# ######################################################################
# Declare the intermediate outputs
BIN_COBS:=\
$(foreach fname,$(MAIN_PROGRAM_CSOURCEFILES),$(OUTOBS)/$(fname).o)
BIN_CPPOBS:=\
$(foreach fname,$(MAIN_PROGRAM_CPPSOURCEFILES),$(OUTOBS)/$(fname).o)
BINOBS:=$(BIN_COBS) $(BIN_CPPOBS)
COBS:=\
$(foreach fname,$(LIBRARY_OBJECT_CSOURCEFILES),$(OUTOBS)/$(fname).o)
CPPOBS:=\
$(foreach fname,$(LIBRARY_OBJECT_CPPSOURCEFILES),$(OUTOBS)/$(fname).o)
OBS:=$(COBS) $(CPPOBS)
ALL_OBS:=$(OBS) $(BINOBS)
DEPS:=\
$(subst $(OUTOBS),src,$(subst .o,.d,$(ALL_OBS)))
# ######################################################################
# Find all the source files so that we can do dependencies properly
SOURCES:=\
$(shell find . | grep -E "\.(c|cpp)\$$")
# ######################################################################
# Declare the build programs
ifndef GCC
GCC=gcc
endif
ifndef GXX
GXX=g++
endif
ifndef LD_PROG
LD_PROG=gcc
endif
ifndef LD_LIB
LD_LIB=gcc
endif
# ######################################################################
# Declare all the flags we need to compile and link
BUILD_TIMESTAMP:=$(shell date +"%Y%m%d%H%M%S")
CC:=$(GCC)
CXX:=$(GXX)
PROG_LD=$(GCC_LD_PROG)
LIB_LD=$(GCC_LD_LIB)
INCLUDE_DIRS:=\
$(foreach ipath,$(INCLUDE_PATHS),-I$(ipath))
LIBDIRS:=\
$(foreach lpath,$(LIBRARY_PATHS),-L$(lpath))
LIBFILES:=\
$(foreach lfile,$(LIBRARY_FILES),-l$(lfile))
COMMONFLAGS:=\
$(EXTRA_COMPILER_FLAGS)\
-W -Wall -c -fPIC \
-DPLATFORM=$(PLATFORM) -DPLATFORM_$(PLATFORM) \
-D$(PROJNAME)_version='"$(VERSION)"'\
-DBUILD_TIMESTAMP='"$(BUILD_TIMESTAMP)"'\
$(PLATFORM_CFLAGS)\
$(INCLUDE_DIRS)
CFLAGS:=$(COMMONFLAGS) $(EXTRA_CFLAGS)
CXXFLAGS:=$(COMMONFLAGS) $(EXTRA_CXXFLAGS)
LD:=$(GCC)
LDFLAGS:= $(LIBDIRS) $(LIBFILES) -lm $(PLATFORM_LDFLAGS)
AR:=ar
ARFLAGS:= rcs
.PHONY: help real-help show real-show debug release clean-all deps
# ######################################################################
# All the conditional targets
help: real-help
debug: CFLAGS+= -ggdb -DDEBUG
debug: CXXFLAGS+= -ggdb -DDEBUG
debug: all
release: CFLAGS+= -O3
release: CXXFLAGS+= -O3
release: all
# ######################################################################
# Finally, build the system
real-help:
@$(ECHO) "Possible targets:"
@$(ECHO) "help: This message."
@$(ECHO) "show: Display all the variable values that will be"
@$(ECHO) " used during execution. Also 'show debug' or"
@$(ECHO) " 'show release' works."
@$(ECHO) "deps: Make the dependencies only."
@$(ECHO) "debug: Build debug binaries."
@$(ECHO) "release: Build release binaries."
@$(ECHO) "clean-debug: Clean a debug build (release is ignored)."
@$(ECHO) "clean-release: Clean a release build (debug is ignored)."
@$(ECHO) "clean-all: Clean everything."
real-all: $(OUTDIRS) $(DYNLIB) $(STCLIB) $(BINPROGS)
all: real-all
@$(ECHO) "[$(CYAN)Soft linking$(NONE)] [$(STCLNK_TARGET)]"
@ln -f -s $(STCLNK_TARGET) $(STCLNK_NAME)
@$(ECHO) "[$(CYAN)Soft linking$(NONE)] [$(DYNLNK_TARGET)]"
@ln -f -s $(DYNLNK_TARGET) $(DYNLNK_NAME)
@$(ECHO) "[$(CYAN)Copying$(NONE) ] [ -> $(OUTDIR)/lib]"
@cp $(OUTLIB)/* $(OUTDIR)/lib
@$(ECHO) "[$(CYAN)Copying$(NONE) ] [ -> ./include/]"
@cp -R $(HEADERS) include
@$(ECHO) "$(INV)$(YELLOW)Build completed: `date`$(NONE)"
@$(ECHO) "$(YELLOW)Total build time: $$((`date +"%s"` - $(START_TIME)))s"\
"$(NONE)"
real-show:
@$(ECHO) "$(GREEN)HOME$(NONE) $(HOME)"
@$(ECHO) "$(GREEN)SHELL$(NONE) $(SHELL)"
@$(ECHO) "$(GREEN)EXE_EXT$(NONE) $(EXE_EXT)"
@$(ECHO) "$(GREEN)LIB_EXT$(NONE) $(LIB_EXT)"
@$(ECHO) "$(GREEN)DYNLIB$(NONE) $(DYNLIB)"
@$(ECHO) "$(GREEN)STCLIB$(NONE) $(STCLIB)"
@$(ECHO) "$(GREEN)CC$(NONE) $(CC)"
@$(ECHO) "$(GREEN)CXX$(NONE) $(CXX)"
@$(ECHO) "$(GREEN)CFLAGS$(NONE) $(CFLAGS)"
@$(ECHO) "$(GREEN)CXXFLAGS$(NONE) $(CXXFLAGS)"
@$(ECHO) "$(GREEN)LD_LIB$(NONE) $(LD_LIB)"
@$(ECHO) "$(GREEN)LD_PROG$(NONE) $(LD_PROG)"
@$(ECHO) "$(GREEN)LDFLAGS$(NONE) $(LDFLAGS)"
@$(ECHO) "$(GREEN)AR$(NONE) $(AR)"
@$(ECHO) "$(GREEN)ARFLAGS$(NONE) $(ARFLAGS)"
@$(ECHO) "$(GREEN)"
@$(ECHO) "$(GREEN)PLATFORM$(NONE) $(PLATFORM)"
@$(ECHO) "$(GREEN)TARGET$(NONE) $(TARGET)"
@$(ECHO) "$(GREEN)OUTBIN$(NONE) $(OUTBIN)"
@$(ECHO) "$(GREEN)OUTLIB$(NONE) $(OUTLIB)"
@$(ECHO) "$(GREEN)OUTOBS$(NONE) $(OUTOBS)"
@$(ECHO) "$(GREEN)OUTDIRS$(NONE) "
@for X in $(OUTDIRS); do $(ECHO) " $$X"; done
@$(ECHO) "$(GREEN)DEPS$(NONE) "
@for X in $(DEPS); do $(ECHO) " $$X"; done
@$(ECHO) "$(GREEN)HEADERS$(NONE) "
@for X in $(HEADERS); do $(ECHO) " $$X"; done
@$(ECHO) "$(GREEN)COBS$(NONE) "
@for X in $(COBS); do $(ECHO) " $$X"; done
@$(ECHO) "$(GREEN)CPPOBS$(NONE) "
@for X in $(CPPOBS); do $(ECHO) " $$X"; done
@$(ECHO) "$(GREEN)OBS$(NONE) "
@for X in $(OBS); do $(ECHO) " $$X"; done
@$(ECHO) "$(GREEN)BIN_COBS$(NONE) "
@for X in $(BIN_COBS); do $(ECHO) " $$X"; done
@$(ECHO) "$(GREEN)BIN_CPPOBS$(NONE) "
@for X in $(BIN_CPPOBS); do $(ECHO) " $$X"; done
@$(ECHO) "$(GREEN)BINOBS$(NONE) "
@for X in $(BINOBS); do $(ECHO) " $$X"; done
@$(ECHO) "$(GREEN)BINPROGS$(NONE) "
@for X in $(BINPROGS); do $(ECHO) " $$X"; done
@$(ECHO) "$(GREEN)SOURCES$(NONE) "
@for X in $(SOURCES); do $(ECHO) " $$X"; done
@$(ECHO) "$(GREEN)PWD$(NONE) $(PWD)"
show: real-show
@$(ECHO) "Only target 'show' selected, ending now."
@false
$(DEPS): $(HEADERS)
deps: $(DEPS)
src/%.d: src/%.c
@$(ECHO) "[$(RED)Dependency$(NONE) ] [$@]"
@$(CC) $(CFLAGS) -MM -MF $@ -MT $(OUTOBS)/$*.o $< ||\
($(ECHO) "$(INV)$(RED)[Depend failure ] [$@]$(NONE)" ; exit 127)
src/%.d: src/%.cpp
@$(ECHO) "[$(RED)Dependency$(NONE) ] [$@]"
@$(CXX) $(CXXFLAGS) -MM -MF $@ $< ||\
($(ECHO) "$(INV)$(RED)[Depend failure ] [$@]$(NONE)" ; exit 127)
$(BIN_COBS) $(COBS): $(OUTOBS)/%.o: src/%.c src/%.d
@$(ECHO) "[$(BLUE)Building$(NONE) ] [$@]"
@$(CC) $(CFLAGS) -o $@ $< ||\
($(ECHO) "$(INV)$(RED)[Compile failure] [$@]$(NONE)" ; exit 127)
$(BIN_CPPOBS) $(CPPOBS): $(OUTOBS)/%.o: src/%.cpp src/%.d
@$(ECHO) "[$(BLUE)Building$(NONE) ] [$@]"
@$(CXX) $(CXXFLAGS) -o $@ $< ||\
($(ECHO) "$(INV)$(RED)[Compile failure] [$@]$(NONE)" ; exit 127)
$(OUTBIN)/%.exe: $(OUTOBS)/%.o $(OBS)
@$(ECHO) "[$(GREEN)Linking$(NONE) ] [$@]"
@$(LD_PROG) $< $(OBS) -o $@ $(LDFLAGS) $(EXTRA_PROG_LDFLAGS) ||\
($(ECHO) "$(INV)$(RED)[Link failure] [$@]$(NONE)" ; exit 127)
$(OUTBIN)/%.elf: $(OUTOBS)/%.o $(OBS)
@$(ECHO) "[$(GREEN)Linking$(NONE) ] [$@]"
@$(LD_PROG) $< $(OBS) -o $@ $(LDFLAGS) $(EXTRA_PROG_LDFLAGS) ||\
($(ECHO) "$(INV)$(RED)[Link failure] [$@]$(NONE)" ; exit 127)
$(DYNLIB): $(OBS)
@$(ECHO) "[$(GREEN)Linking$(NONE) ] [$@]"
@$(LD_LIB) -shared $^ -o $@ $(LDFLAGS) $(EXTRA_LIB_LDFLAGS) ||\
($(ECHO) "$(INV)$(RED)[Link failure] [$@]$(NONE)" ; exit 127)
$(STCLIB): $(OBS)
@$(ECHO) "[$(GREEN)Linking$(NONE) ] [$@]"
@$(AR) $(ARFLAGS) $@ $^ ||\
($(ECHO) "$(INV)$(RED)[Link failure] [$@]$(NONE)" ; exit 127)
$(OUTDIRS):
@$(ECHO) "[$(CYAN)Creating dir$(NONE)] [$@]"
@mkdir -p $@ ||\
($(ECHO) "$(INV)$(RED)[mkdir failure] [$@]$(NONE)" ; exit 127)
clean-release:
@rm -rfv release
clean-debug:
@rm -rfv debug
clean-all: clean-release clean-debug
@rm -rfv include
@rm -rfv `find . | grep "\.d$$"`
clean:
@$(ECHO) Choose either clean-release or clean-debug
include $(DEPS)