-
Notifications
You must be signed in to change notification settings - Fork 0
/
Makefile
353 lines (264 loc) · 8.65 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
347
348
349
350
351
352
353
# Structure of the project:
#
# /
# build/
# shared/
# libproject-name.so
# src/
# object files...
# static/
# libproject-name.a
# src/
# object files...
# executable/
# project-name
# src/
# object files
# src/
# sources files...
# inc/
# header files...
# Note: inc and src can be the same folder
#####
##### FOLDER SETTINGS
#####
SRC_FOLDER := src
INC_FOLDER := include
BUILD_FOLDER := build
BUILD_SHARED_FOLDER := $(BUILD_FOLDER)/shared
BUILD_STATIC_FOLDER := $(BUILD_FOLDER)/static
BUILD_EXE_FOLDER := $(BUILD_FOLDER)/executable
#####
##### GENERAL SETTINGS
#####
PROJECT_NAME := sandbox
CXX := g++
SXX := ar
# Targets
TARGET_SHARED := $(BUILD_SHARED_FOLDER)/lib$(PROJECT_NAME).so
TARGET_STATIC := $(BUILD_STATIC_FOLDER)/lib$(PROJECT_NAME).a
TARGET_EXE := $(BUILD_EXE_FOLDER)/$(PROJECT_NAME)
# Target to build when `make` or `make all` is typed
TARGET_ALL := $(TARGET_STATIC)
#####
##### FILES SETTINGS
#####
# Extension
EXT_SRC_FILE = .cpp
EXT_INC_FILE = .hpp
# Get header from source file
# This function is only used to checks if the header has bee modified and the object file must be rebuild
# So if the header doesn't exist it's ok
# The first argument is the source file relative to $(SRC_FOLDER)
# The header must be relative to $(INC_FOLDER)
# EX: $(1:%$(EXT_SRC_FILE)=%$(EXT_INC_FILE))
# will take the file "folder/sub_folder_file.cpp"
# and transform it into "folder/sub_folder_file.hpp"
header-of = $(1:%$(EXT_SRC_FILE)=vcrate/%$(EXT_INC_FILE))
# Relative to $(SRC_FOLDER)
SRC_EXCLUDE_FILE :=
# All files that are not use for libraries, don't add src/
SRC_MAINS := main.cpp
# The main file to use (must be in $(SRC_MAINS))
SRC_MAIN := main.cpp
#####
##### FLAGS
#####
FLAGS := -std=c++17 -g3 -Wall -Wextra -Wno-pmf-conversions -O2
STATIC_LINK_FLAG := rcs
# Include path
# Must be use with -I
INC_FLAG := -I $(INC_FOLDER) -I lib/bytecode-description/include
#####
##### LIBRARY
#####
# Path to libaries if not in $PATH, for example (relative to the project folder): lib/
# Must be use with -L
LIBS_PATH := -L lib/bytecode-description/build/static
# For example: -lsfml-graphics
LIBS := -lbytecode_desc
# Library that require to be build
LIB_TO_BUILD := lib/bytecode-description/build/static/libbytecode_desc.a
# Create rules to build the libraries
lib/bytecode-description/build/static/libbytecode_desc.a:
@$(call _special,BUILDING STATIC LIBRARY ($@)...)
@cd lib/bytecode-description/ && make | sed "s/^/\t/"
###############################################
# PRIVATE #
###############################################
#####
##### OTHER
#####
_RESET := \033[0m
_BOLD := \033[1m
_COLOR_RED := \033[31m
_COLOR_GREEN := \033[32m
_COLOR_YELLOW := \033[33m
_COLOR_BLUE := \033[34m
_COLOR_MAGENTA := \033[35m
_COLOR_CYAN := \033[36m
_COLOR_WHITE := \033[37m
SHARED_FLAGS := -fPIC
MAKEFLAGS += --no-print-directory
#####
##### FUNCTIONS
#####
_void =
_space = $(_void) $(_void)
_comma = ,
# join <between> <list>
_join = $(subst $(_space),$(1),$(2))
# _header <message>
_header = echo -e "$(_COLOR_CYAN)$(_BOLD)>>> $(1)$(_RESET)"
# _sub-header <message>
_sub-header = echo -e "$(_COLOR_GREEN)>>> $(1)$(_RESET)"
# _build-msg <target> <from>
_build-msg = echo -e "$(_COLOR_WHITE):: Building $(_BOLD)$(1)$(_RESET)$(_COLOR_WHITE) from $(_BOLD)$(2)$(_RESET)"
# _special <message>
_special = echo -e "$(_COLOR_MAGENTA)$(_BOLD)\# $(1)$(_RESET)"
# not <value>
# return an empty string if value is not
not = $(if $(1),,not-empty-string)
# _remove-folder <folder>
define _remove-folder
rm -rf $(1)
endef
# _is-empty <value> [<message>]
# example: $(call check-not-empty,FOLDER,The folder must be specified)
_is-empty = $(call not,$(1))
define _is-empty-er
$(if $(call _is-empty,$(1)),$(error Value is empty $(if $(2),($(2)) )))
endef
# _exists <file> [<message>]
_exists = $(wildcard $(1))
define _exists-er
$(if $(call _exists,$(1)),,$(error File '$(1)' doesn't exists $(if $(2),($(2)) )))
endef
#####
##### SOURCES
#####
_SRC_MAINS := $(addprefix $(SRC_FOLDER)/,$(SRC_MAINS))
# All sources files not main
_ALL_SRC_FILES := $(shell find $(SRC_FOLDER) -name '*$(EXT_SRC_FILE)')
_SRC_FILES := $(filter-out $(_SRC_MAINS),$(_ALL_SRC_FILES))
#####
##### DIRECTORIES
#####
# All sources file directories
_SRC_DIR := $(sort $(dir $(_ALL_SRC_FILES)))
_EXE_DIR := $(addprefix $(BUILD_EXE_FOLDER)/,$(_SRC_DIR))
_SHARED_DIR := $(addprefix $(BUILD_SHARED_FOLDER)/,$(_SRC_DIR))
_STATIC_DIR := $(addprefix $(BUILD_STATIC_FOLDER)/,$(_SRC_DIR))
_BUILD_DIR := $(_EXE_DIR) $(BUILD_EXE_FOLDER)
_BUILD_DIR += $(BUILD_SHARED_FOLDER) $(_SHARED_DIR)
_BUILD_DIR += $(BUILD_STATIC_FOLDER) $(_STATIC_DIR)
#####
##### OBJECT FILES
#####
_OBJ_MAIN := $(SRC_MAIN:%$(EXT_SRC_FILE)=$(BUILD_EXE_FOLDER)/$(SRC_FOLDER)/%.o)
_OBJ_SRC_EXE := $(_OBJ_MAIN) $(_SRC_FILES:%$(EXT_SRC_FILE)=$(BUILD_EXE_FOLDER)/%.o)
_OBJ_SRC_SHARED := $(_SRC_FILES:%$(EXT_SRC_FILE)=$(BUILD_SHARED_FOLDER)/%.o)
_OBJ_SRC_STATIC := $(_SRC_FILES:%$(EXT_SRC_FILE)=$(BUILD_STATIC_FOLDER)/%.o)
_LIB_PATH_LD := $(call _join,:,$(strip $(filter-out -L,$(LIBS_PATH))))
export LD_LIBRARY_PATH += $(_LIB_PATH_LD)
#####
##### RULES
#####
.PHONY: all executable shared static
.PHONY: clean clean-executable clean-shared clean-static
.PHONY: re re-executable re-shared re-static
.PHONY: re-run run
.DEFAULT_GOAL := all
all:
ifneq ($(findstring $(TARGET_EXE),$(TARGET_ALL)),)
@make executable
endif
ifneq ($(findstring $(TARGET_SHARED),$(TARGET_ALL)),)
@make shared
endif
ifneq ($(findstring $(TARGET_STATIC),$(TARGET_ALL)),)
@make static
endif
executable:
@$(call _header,BUILDING EXECUTABLE...)
@make $(TARGET_EXE)
shared:
@$(call _header,BUILDING SHARED LIBRARY...)
@make $(TARGET_SHARED)
static:
@$(call _header,BUILDING STATIC LIBRARY...)
@make $(TARGET_STATIC)
clean:
@$(call _header,REMOVING $(BUILD_FOLDER))
@$(call _remove-folder,$(BUILD_FOLDER))
clean-executable:
@$(call _header,REMOVING $(BUILD_EXE_FOLDER))
@$(call _remove-folder,$(BUILD_EXE_FOLDER))
clean-shared:
@$(call _header,REMOVING $(BUILD_SHARED_FOLDER))
@$(call _remove-folder,$(BUILD_SHARED_FOLDER))
clean-static:
@$(call _header,REMOVING $(BUILD_STATIC_FOLDER))
@$(call _remove-folder,$(BUILD_STATIC_FOLDER))
where-executable:
@echo $(TARGET_EXE)
where-shared:
@echo $(TARGET_SHARED)
where-static:
@echo $(TARGET_STATIC)
re:
@make clean
@make
re-executable:
@make clean-executable
@make executable
re-shared:
@make clean-shared
@make shared
re-static:
@make clean-static
@make static
run:
@make executable
@echo
@$(call _special,EXECUTING $(TARGET_EXE)...)
@$(TARGET_EXE) $(args); ERR=$$?; $(call _special,PROGRAM HALT WITH CODE $$ERR); exit $$ERR;
re-run:
@make re-executable
@make run
valgrind:
@make executable
@echo
@$(call _special,EXECUTING $(TARGET_EXE) WITH VALGRIND...)
@valgrind $(TARGET_EXE) $(args); ERR=$$?; $(call _special,PROGRAM HALT WITH CODE $$ERR); exit $$ERR;
re-valgrind:
@make re-executable
@make valgrind
$(_BUILD_DIR):
@mkdir -p $(_BUILD_DIR)
###
$(TARGET_STATIC): $(_BUILD_DIR) $(LIB_TO_BUILD) $(_OBJ_SRC_STATIC)
@$(call _sub-header,Archiving...)
@$(SXX) $(STATIC_LINK_FLAG) $(TARGET_STATIC) $(_OBJ_SRC_STATIC)
@$(call _header,Static library done ($(TARGET_STATIC)))
$(BUILD_STATIC_FOLDER)/$(SRC_FOLDER)/%.o: $(SRC_FOLDER)/%$(EXT_SRC_FILE) $(INC_FOLDER)/$(call header-of,%$(EXT_SRC_FILE))
@$(call _build-msg,$(notdir $@),$(call _join,$(_comma)$(_space),$(strip $(notdir $< $(wildcard $(word 2,$^))))))
@$(CXX) -c $(INC_FLAG) $(FLAGS) -o "$@" "$<"
###
$(TARGET_SHARED): $(_BUILD_DIR) $(LIB_TO_BUILD) $(_OBJ_SRC_SHARED)
@$(call _sub-header,Shared library creation...)
@$(CXX) $(INC_FLAG) $(FLAGS) -shared -o $(TARGET_SHARED) $(_OBJ_SRC_SHARED) $(LIBS_PATH) $(LIBS)
@$(call _header,Shared library done ($(TARGET_SHARED)))
$(BUILD_SHARED_FOLDER)/$(SRC_FOLDER)/%.o: $(SRC_FOLDER)/%$(EXT_SRC_FILE) $(INC_FOLDER)/$(call header-of,%$(EXT_SRC_FILE))
@$(call _build-msg,$(notdir $@),$(call _join,$(_comma)$(_space),$(strip $(notdir $< $(wildcard $(word 2,$^))))))
@$(CXX) -c $(INC_FLAG) $(FLAGS) $(SHARED_FLAGS) -o "$@" "$<"
###
$(TARGET_EXE): $(_BUILD_DIR) $(LIB_TO_BUILD) $(_OBJ_SRC_EXE)
@$(call _sub-header,Linking...)
@$(CXX) $(INC_FLAG) $(FLAGS) $(_OBJ_SRC_EXE) -o "$@" $(LIBS_PATH) $(LIBS)
@$(call _header,Executable done ($(TARGET_EXE)))
$(BUILD_EXE_FOLDER)/$(SRC_FOLDER)/%.o: $(SRC_FOLDER)/%$(EXT_SRC_FILE) $(INC_FOLDER)/$(call header-of,%$(EXT_SRC_FILE))
@$(call _build-msg,$(notdir $@),$(call _join,$(_comma)$(_space),$(strip $(notdir $< $(wildcard $(word 2,$^))))))
@$(CXX) -c $(INC_FLAG) $(FLAGS) -o "$@" "$<"
# Just to avoid file without headers
%$(EXT_INC_FILE):