-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmakefile
421 lines (378 loc) · 21 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
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
# ==========================================================
# @file: MAKEFILE
#
# @package: hyper-allocator
#
# @brief: Makefile responsible for compiling and
# managing the static and shared libraries
# of the hyper-allocator project.
#
# @version: v1.0.0
# @dependencies: gcc, ar, make
#
# @author: Rafael V. Volkmer
# @date: 12/10/2023
# ==========================================================
# ------------------------------------------
# Silence All Notifications in Makefile
# ------------------------------------------
.SILENT:
# ==========================================
# ANSI Color and Style Definitions for Makefile
# ==========================================
# ------------------------------------------
# Text Colors (Foreground Colors)
# ------------------------------------------
BLACK = \033[0;30m# Black
RED = \033[0;31m# Red
GREEN = \033[1;32m# Green (Bold)
YELLOW = \033[1;33m# Yellow (Bold)
BLUE = \033[1;34m# Blue (Bold)
PURPLE = \033[1;35m# Purple (Bold)
CYAN = \033[1;36m# Cyan (Bold)
WHITE = \033[0;37m# White
# ------------------------------------------
# Light Text Colors (Bright Foreground Colors)
# ------------------------------------------
LIGHT_RED = \033[0;91m# Light Red
LIGHT_GREEN = \033[0;92m# Light Green
LIGHT_YELLOW = \033[0;93m# Light Yellow
LIGHT_BLUE = \033[0;94m# Light Blue
LIGHT_MAGENTA = \033[0;95m# Light Magenta
LIGHT_CYAN = \033[0;96m# Light Cyan
LIGHT_WHITE = \033[0;97m# Light White
# ------------------------------------------
# Background Colors
# ------------------------------------------
BG_BLACK = \033[40m# Black Background
BG_RED = \033[41m# Red Background
BG_GREEN = \033[42m# Green Background
BG_YELLOW = \033[43m# Yellow Background
BG_BLUE = \033[44m# Blue Background
BG_MAGENTA = \033[45m# Magenta Background
BG_CYAN = \033[46m# Cyan Background
BG_WHITE = \033[47m# White Background
# ------------------------------------------
# Text Styles
# ------------------------------------------
BOLD = \033[1m# Bold Text
DIM = \033[2m# Dim Text
UNDERLINE = \033[4m# Underlined Text
INVERT = \033[7m# Inverted Colors
STRIKETHROUGH = \033[9m# Strikethrough Text
# ------------------------------------------
# Reset Styles and Colors
# ------------------------------------------
RESET = \033[0m# Reset All Styles and Colors
RESET_COLOR = \033[39m# Reset Text Color
RESET_BG = \033[49m# Reset Background Color
# ==========================================
# Unicode Table Border Characters Definitions
# ==========================================
# ==========================================
# Unicode Table Border Characters Definitions
# ==========================================
# ------------------------------------------
# Single Line Borders
# ------------------------------------------
SINGLE_TOP_LEFT = ┌# Unicode character for the top-left corner of a single-line table
SINGLE_TOP_RIGHT = ┐# Unicode character for the top-right corner of a single-line table
SINGLE_BOTTOM_LEFT = └# Unicode character for the bottom-left corner of a single-line table
SINGLE_BOTTOM_RIGHT= ┘# Unicode character for the bottom-right corner of a single-line table
SINGLE_HORIZONTAL = ─# Unicode character for single horizontal lines
SINGLE_VERTICAL = │# Unicode character for single vertical lines
# ------------------------------------------
# Double Line Borders
# ------------------------------------------
DOUBLE_TOP_LEFT = ╔# Unicode character for the top-left corner of a double-line table
DOUBLE_TOP_RIGHT = ╗# Unicode character for the top-right corner of a double-line table
DOUBLE_BOTTOM_LEFT = ╚# Unicode character for the bottom-left corner of a double-line table
DOUBLE_BOTTOM_RIGHT = ╝# Unicode character for the bottom-right corner of a double-line table
DOUBLE_HORIZONTAL = ═# Unicode character for double horizontal lines
DOUBLE_VERTICAL = ║# Unicode character for double vertical lines
# ------------------------------------------
# Rounded Borders
# ------------------------------------------
ROUNDED_TOP_LEFT = ╭# Unicode character for the top-left corner of a rounded table
ROUNDED_TOP_RIGHT = ╮# Unicode character for the top-right corner of a rounded table
ROUNDED_BOTTOM_LEFT = ╰# Unicode character for the bottom-left corner of a rounded table
ROUNDED_BOTTOM_RIGHT= ╯# Unicode character for the bottom-right corner of a rounded table
ROUNDED_HORIZONTAL = ─# Unicode character for horizontal lines in rounded tables
ROUNDED_VERTICAL = │# Unicode character for vertical lines in rounded tables
# ------------------------------------------
# Heavy Borders
# ------------------------------------------
HEAVY_TOP_LEFT = ╒# Unicode character for the top-left corner of a heavy table
HEAVY_TOP_RIGHT = ╕# Unicode character for the top-right corner of a heavy table
HEAVY_BOTTOM_LEFT = ╘# Unicode character for the bottom-left corner of a heavy table
HEAVY_BOTTOM_RIGHT= ╛# Unicode character for the bottom-right corner of a heavy table
HEAVY_HORIZONTAL = ═# Unicode character for heavy horizontal lines
HEAVY_VERTICAL = ║# Unicode character for heavy vertical lines
# ------------------------------------------
# Mixed Borders
# ------------------------------------------
MIXED_TOP_LEFT = ╓ # Unicode character for the top-left corner of a mixed border table
MIXED_TOP_RIGHT = ╖ # Unicode character for the top-right corner of a mixed border table
MIXED_BOTTOM_LEFT = ╙ # Unicode character for the bottom-left corner of a mixed border table
MIXED_BOTTOM_RIGHT= ╜ # Unicode character for the bottom-right corner of a mixed border table
MIXED_HORIZONTAL = ═ # Unicode character for mixed horizontal lines
MIXED_VERTICAL = ║ # Unicode character for mixed vertical lines
# ------------------------------------------
# Dashed Borders
# ------------------------------------------
DASHED_TOP_LEFT = ┌# Unicode character for the top-left corner of a dashed border table
DASHED_TOP_RIGHT = ┐# Unicode character for the top-right corner of a dashed border table
DASHED_BOTTOM_LEFT = └# Unicode character for the bottom-left corner of a dashed border table
DASHED_BOTTOM_RIGHT= ┘# Unicode character for the bottom-right corner of a dashed border table
DASHED_HORIZONTAL = ┄# Unicode character for dashed horizontal lines
DASHED_VERTICAL = ┆# Unicode character for dashed vertical lines
# ------------------------------------------
# Curved Borders
# ------------------------------------------
CURVED_TOP_LEFT = ╭# Unicode character for the top-left corner of a curved border table
CURVED_TOP_RIGHT = ╮# Unicode character for the top-right corner of a curved border table
CURVED_BOTTOM_LEFT = ╰# Unicode character for the bottom-left corner of a curved border table
CURVED_BOTTOM_RIGHT= ╯# Unicode character for the bottom-right corner of a curved border table
CURVED_HORIZONTAL = ─# Unicode character for curved horizontal lines
CURVED_VERTICAL = │# Unicode character for curved vertical lines
# ==========================================
# Current Compiler
# ==========================================
CC = gcc
# ==========================================
# Directories Relative to ROOT_DIR
# ==========================================
ROOT_DIR ?= $(abspath .)
INC_DIR := $(ROOT_DIR)/inc
SRC_DIR := $(ROOT_DIR)/src
TEST_DIR := $(ROOT_DIR)/tests
SCRIPTS_DIR := $(ROOT_DIR)/utils
BIN_DIR := $(ROOT_DIR)/bin
# ------------------------------------------
# Create the bin directory if it doesn't exist
# ------------------------------------------
$(BIN_DIR):
@mkdir -p $(BIN_DIR)
# ==========================================
# Includes and Paths
# ==========================================
INCLUDES_common := -I$(INC_DIR) -I$(ROOT_DIR)
# ==========================================
# Sources and Objects
# ==========================================
LIB_SRC = $(wildcard $(SRC_DIR)/*.c)
LIB_OBJS = $(patsubst $(SRC_DIR)/%.c, $(BIN_DIR)/%.o, $(LIB_SRC))
# ==========================================
# Names of Libraries and Executables
# ==========================================
LIB_STATIC = $(BIN_DIR)/libmemalloc.a
LIB_SHARED = $(BIN_DIR)/libmemalloc.so
# ==========================================
# Heap Size (default: 10 KB)
# ==========================================
HEAP_SIZE ?= 10240
# ==========================================
# Common Compiling Flags
# ==========================================
CFLAGS_common := \
-Wall \
-Werror \
-Wextra \
-Wpedantic \
-Wshadow \
-Wconversion \
-Wstrict-prototypes \
-Wformat=2 \
-Wunreachable-code \
-Wunused-parameter \
-fstrict-aliasing \
-fwrapv \
-fno-common \
-Wno-unused-function
# ==========================================
# Release Compiling Flags
# ==========================================
CFLAGS_release := \
-Ofast \
-Os \
-flto \
-ffast-math \
-fomit-frame-pointer \
-funroll-loops \
-fno-exceptions \
-fno-unwind-tables \
-fno-stack-protector \
-fvisibility=hidden \
-fdata-sections \
-ffunction-sections
# ==========================================
# Release Linking Flags
# ==========================================
LDFLAGS_release := \
-Wl, \
--gc-sections \
-flto
# ==========================================
# Debug Compiling Flags
# ==========================================
CFLAGS_debug := \
-O0 \
-g \
-D_DEBUG_=1
# ==========================================
# Debug Linking Flags
# ==========================================
LDFLAGS_debug := \
-Wl, \
--gc-sections \
-flto
# ==========================================
# Position Independent Code Flag
# ==========================================
POSITION_INDEP := \
-fPIC
# ==========================================
# Phony Targets
# ==========================================
.PHONY: all clean test release debug build
# ==========================================
# Default Target
# ==========================================
all: release
# ==========================================
# Release Build Target
# ==========================================
release: CFLAGS = $(CFLAGS_common) $(CFLAGS_release)
release: LDFLAGS = $(LDFLAGS_release)
release: build
# ==========================================
# Debug Build Target
# ==========================================
debug: CFLAGS = $(CFLAGS_common) $(CFLAGS_debug)
debug: LDFLAGS = $(LDFLAGS_debug)
debug: build
# ==========================================
# Build Rules
# ==========================================
build: $(BIN_DIR) $(LIB_STATIC) $(LIB_SHARED)
@echo " "
@echo "$(GREEN)════════════════════════════════════════════════════════ ═══════ ════ ══$(RESET)"
@echo "$(GREEN)Build process completed successfully!$(RESET)"
@echo "$(GREEN)════════════════════════════════════════════════════════ ═══════ ════ ══$(RESET)"
@echo " "
# ==========================================
# Compile Object Files with -fPIC for Shared Library
# ==========================================
$(BIN_DIR)/%.o: $(SRC_DIR)/%.c $(INC_DIR) | $(BIN_DIR)
@$(MAKE) print_object_file_conversion_table
@echo "$(PURPLE)Source File from: $< $(RESET)"
@echo "$(BLUE)Binary File to: $@ $(RESET)"
@echo " "
@echo "$(YELLOW)Compiling object...$(RESET)"
@echo " "
@echo "$(CC) $(CFLAGS) $(INCLUDES_common) -DHEAP_SIZE=$(HEAP_SIZE) $(POSITION_INDEP) -c $< -o $@"
$(CC) $(CFLAGS) $(INCLUDES_common) -DHEAP_SIZE=$(HEAP_SIZE) $(POSITION_INDEP) -c $< -o $@
@echo " "
@echo "$(GREEN)Compiled $< into: $(BIN_DIR)/$(@F)$(RESET)"
# ==========================================
# Create Static Library
# ==========================================
$(LIB_STATIC): $(LIB_OBJS) | $(BIN_DIR)
@$(MAKE) print_static_library_table
@echo "$(PURPLE)Object from: $(LIB_OBJS)$(RESET)"
@echo "$(BLUE)Static library to: $(LIB_STATIC)$(RESET)"
@echo " "
@echo "$(YELLOW)Creating static library...$(RESET)"
@echo " "
@echo "ar rcs $(LIB_STATIC) $(LIB_OBJS)"
ar rcs $(LIB_STATIC) $(LIB_OBJS)
@echo " "
@echo "$(GREEN)Static library: $(LIB_STATIC) created successfully.$(RESET)"
# ==========================================
# Create Shared Library
# ==========================================
$(LIB_SHARED): $(LIB_OBJS) | $(BIN_DIR)
@$(MAKE) print_shared_library_table
@echo "$(PURPLE)Object from: $(LIB_OBJS)$(RESET)"
@echo "$(BLUE)Shared Library to: $(LIB_SHARED)$(RESET)"
@echo " "
@echo "$(YELLOW)Creating shared library...$(RESET)"
@echo " "
@echo "$(CC) -shared -o $(LIB_SHARED) $(LIB_OBJS)"
$(CC) -shared -o $(LIB_SHARED) $(LIB_OBJS)
@echo " "
@echo "$(GREEN)Shared library: $(LIB_SHARED) created successfully.$(RESET)"
# ==========================================
# Clean Target
# ==========================================
clean:
@$(MAKE) print_clean_table
@echo "rm -rf $(BIN_DIR)"
rm -rf $(BIN_DIR)
@echo " "
@echo "$(RED)═ ════ ═══════ ═════════════════════════════════════════════════════════$(RESET)"
@echo "$(RED)Cleanup completed.$(RESET)"
@echo "$(RED)═ ═══ ═══════ ══════════════════════════════════════════════════════════$(RESET)"
@echo " "
# ==========================================
# Print Object File Conversion Table
# ==========================================
print_object_file_conversion_table:
@echo " "
@echo "$(YELLOW)$(SINGLE_TOP_LEFT)─────────────────────────────────────────────────────────────────$(SINGLE_TOP_RIGHT)$(RESET)"
@echo "$(YELLOW)$(SINGLE_VERTICAL) Building Code Binary (.o) $(SINGLE_VERTICAL)$(RESET)"
@echo "$(YELLOW)$(SINGLE_VERTICAL)─────────────────────────────────────────────────────────────────$(SINGLE_VERTICAL)$(RESET)"
@echo "$(YELLOW)$(SINGLE_VERTICAL) gcc <GccFlags> <LdFlags> -I <IncPath> *.c -o <ObjectPath/name> $(SINGLE_VERTICAL)$(RESET)"
@echo "$(YELLOW)$(SINGLE_VERTICAL)─────────────────────────────────────────────────────────────────$(SINGLE_VERTICAL)$(RESET)"
@echo "$(YELLOW)$(SINGLE_VERTICAL) Source File (.c) -> Object File (.o) $(SINGLE_VERTICAL)$(RESET)"
@echo "$(YELLOW)$(SINGLE_VERTICAL) Compiles each .c file into a .o file for later linking. $(SINGLE_VERTICAL)$(RESET)"
@echo "$(YELLOW)$(SINGLE_BOTTOM_LEFT)─────────────────────────────────────────────────────────────────$(SINGLE_BOTTOM_RIGHT)$(RESET)"
@echo " "
# ==========================================
# Print Static Library Creation Table
# ==========================================
print_static_library_table:
@echo " "
@echo "$(YELLOW)$(SINGLE_TOP_LEFT)────────────────────────────────────────────────────────────────$(SINGLE_TOP_RIGHT)$(RESET)"
@echo "$(YELLOW)$(SINGLE_VERTICAL) Building Static Library (.a) $(SINGLE_VERTICAL)$(RESET)"
@echo "$(YELLOW)$(SINGLE_VERTICAL)────────────────────────────────────────────────────────────────$(SINGLE_VERTICAL)$(RESET)"
@echo "$(YELLOW)$(SINGLE_VERTICAL) ar rcs <LibName.a> <ObjectName.o> $(SINGLE_VERTICAL)$(RESET)"
@echo "$(YELLOW)$(SINGLE_VERTICAL)────────────────────────────────────────────────────────────────$(SINGLE_VERTICAL)$(RESET)"
@echo "$(YELLOW)$(SINGLE_VERTICAL) This command creates a static library named $(SINGLE_VERTICAL)$(RESET)"
@echo "$(YELLOW)$(SINGLE_VERTICAL) libmemalloc.a from object files (.o). The rcs flags $(SINGLE_VERTICAL)$(RESET)"
@echo "$(YELLOW)$(SINGLE_VERTICAL) mean: $(SINGLE_VERTICAL)$(RESET)"
@echo "$(YELLOW)$(SINGLE_VERTICAL) r: Replace or insert members. $(SINGLE_VERTICAL)$(RESET)"
@echo "$(YELLOW)$(SINGLE_VERTICAL) c: Create the archive if it doesn't exist. $(SINGLE_VERTICAL)$(RESET)"
@echo "$(YELLOW)$(SINGLE_VERTICAL) s: Create an index for faster symbol lookup. $(SINGLE_VERTICAL)$(RESET)"
@echo "$(YELLOW)$(SINGLE_BOTTOM_LEFT)────────────────────────────────────────────────────────────────$(SINGLE_BOTTOM_RIGHT)$(RESET)"
@echo " "
# ==========================================
# Print Shared Library Creation Table
# ==========================================
print_shared_library_table:
@echo " "
@echo "$(YELLOW)$(SINGLE_TOP_LEFT)───────────────────────────────────────────────────────────────$(SINGLE_TOP_RIGHT)$(RESET)"
@echo "$(YELLOW)$(SINGLE_VERTICAL) Building Shared Library (.so) $(SINGLE_VERTICAL)$(RESET)"
@echo "$(YELLOW)$(SINGLE_VERTICAL)───────────────────────────────────────────────────────────────$(SINGLE_VERTICAL)$(RESET)"
@echo "$(YELLOW)$(SINGLE_VERTICAL) gcc -shared -o <LibName.so> <ObjectName.o> $(SINGLE_VERTICAL)$(RESET)"
@echo "$(YELLOW)$(SINGLE_VERTICAL)───────────────────────────────────────────────────────────────$(SINGLE_VERTICAL)$(RESET)"
@echo "$(YELLOW)$(SINGLE_VERTICAL) This command compiles the object files into a $(SINGLE_VERTICAL)$(RESET)"
@echo "$(YELLOW)$(SINGLE_VERTICAL) shared library (.so) that can be dynamically linked $(SINGLE_VERTICAL)$(RESET)"
@echo "$(YELLOW)$(SINGLE_VERTICAL) during runtime. $(SINGLE_VERTICAL)$(RESET)"
@echo "$(YELLOW)$(SINGLE_BOTTOM_LEFT)───────────────────────────────────────────────────────────────$(SINGLE_BOTTOM_RIGHT)$(RESET)"
@echo " "
# ==========================================
# Print Clean Up Table
# ==========================================
print_clean_table:
@echo " "
@echo "$(RED)$(SINGLE_TOP_LEFT)─────────────────────────────────────────────────────────────────$(SINGLE_TOP_RIGHT)$(RESET)"
@echo "$(RED)$(SINGLE_VERTICAL) Clean Up $(SINGLE_VERTICAL)$(RESET)"
@echo "$(RED)$(SINGLE_VERTICAL)─────────────────────────────────────────────────────────────────$(SINGLE_VERTICAL)$(RESET)"
@echo "$(RED)$(SINGLE_VERTICAL) rm -rf bin $(SINGLE_VERTICAL)$(RESET)"
@echo "$(RED)$(SINGLE_VERTICAL)─────────────────────────────────────────────────────────────────$(SINGLE_VERTICAL)$(RESET)"
@echo "$(RED)$(SINGLE_VERTICAL) Removes the entire bin directory along with $(SINGLE_VERTICAL)$(RESET)"
@echo "$(RED)$(SINGLE_VERTICAL) object files and compiled libraries. $(SINGLE_VERTICAL)$(RESET)"
@echo "$(RED)$(SINGLE_BOTTOM_LEFT)─────────────────────────────────────────────────────────────────$(SINGLE_BOTTOM_RIGHT)$(RESET)"
@echo " "
### end of file ###