6
6
# By: ldulling <ldulling@student.42.fr> +#+ +:+ +#+ #
7
7
# +#+#+#+#+#+ +#+ #
8
8
# Created: 2023/12/23 03:22:46 by ldulling #+# #+# #
9
- # Updated: 2024/09/05 10:57:10 by ldulling ### ########.fr #
9
+ # Updated: 2025/01/24 23:53:02 by ldulling ### ########.fr #
10
10
# #
11
11
# **************************************************************************** #
12
12
@@ -42,14 +42,14 @@ BUILDFILES := Makefile \
42
42
43
43
# Flags
44
44
45
- CC ?= cc
45
+ CC ?= cc
46
46
CC_VERSION := $(shell $(CC ) --version | head -1)
47
47
CFLAGS_STD := -Wall -Wextra -Werror
48
48
CFLAGS_DBG := -ggdb3
49
49
CFLAGS_SAN := -fsanitize=address,undefined,bounds,float-divide-by-zero
50
50
CFLAGS_OPT := -O3
51
- CFLAGS ?= $(CFLAGS_STD ) $(CFLAGS_DBG )
52
- CPPFLAGS := $(addprefix -I,$(INC_DIR ) $(LIB_INCLUDES ) )
51
+ CFLAGS ?= $(CFLAGS_STD ) $(CFLAGS_DBG )
52
+ CPPFLAGS := $(addprefix -I,$(INC_DIR ) $(LIB_INCLUDES ) )
53
53
DEPFLAGS = -M -MP -MF $@ -MT "$(OBJ_DIR ) /$* .o $@ "
54
54
LDFLAGS := $(addprefix -L,$(LIBRARIES ) )
55
55
LDLIBS := $(addprefix -l,$(patsubst lib% ,% ,$(notdir $(LIBRARIES ) $(LIBRARIES_EXT ) ) ) )
@@ -76,7 +76,6 @@ VALGRIND := $(shell which valgrind)
76
76
77
77
VALGRINDFLAGS = --errors-for-leak-kinds=all \
78
78
--leak-check=full \
79
- --read-var-info=yes \
80
79
--show-error-list=yes \
81
80
--show-leak-kinds=all \
82
81
--suppressions=$(CURDIR ) /minishell.supp \
@@ -96,24 +95,26 @@ ABSOLUTE_PATHS := /bin/* \
96
95
97
96
# Terminal
98
97
99
- TERMINAL := $(shell which gnome-terminal 2>/dev/null)
98
+ TERMINAL ?= $(if $(shell command -v gnome-terminal) , gnome-terminal, \
99
+ $(if $(shell command -v terminator) , terminator, \
100
+ $(if $(shell command -v xterm) , xterm, \
101
+ ) ) )
100
102
101
- ifeq (val, $(filter val,$(MAKECMDGOALS ) ) )
102
- TERMINALTITLE := valgrind $(MAKE_NAME )
103
- else ifeq (valfd, $(filter valfd,$(MAKECMDGOALS)))
104
- TERMINALTITLE := valgrind-fd $(MAKE_NAME )
105
- else
106
- TERMINALTITLE := $(MAKE_NAME )
107
- endif
103
+ TERMINALTITLE := $(if $(filter val, $(MAKECMDGOALS ) ) , valgrind $(MAKE_NAME ) , \
104
+ $(if $(filter valfd, $(MAKECMDGOALS ) ) , valgrind-fd $(MAKE_NAME ) , \
105
+ $(MAKE_NAME ) ) )
108
106
109
- TERMINALFLAGS := --title="$(TERMINALTITLE ) " -- /bin/sh -c
107
+ TERMINALFLAGS ?= $(if $(filter gnome-terminal, $(TERMINAL ) ) , --title="$(TERMINALTITLE ) " --, \
108
+ $(if $(filter terminator, $(TERMINAL ) ) , --title="$(TERMINALTITLE ) " -x, \
109
+ $(if $(filter xterm, $(TERMINAL ) ) , -title "$(TERMINALTITLE ) " -e, \
110
+ ) ) )
110
111
111
112
112
113
# Files
113
114
114
115
include $(BUILD_DIR ) /source_files.mk
115
116
SRC_EXTENSION := .c
116
- OBJ := $(SRC:%$(SRC_EXTENSION ) =$(OBJ_DIR ) /%.o )
117
+ OBJ := $(SRC:%$(SRC_EXTENSION ) =$(OBJ_DIR ) /%.o )
117
118
DEP := $(SRC:%$(SRC_EXTENSION ) =$(DEP_DIR ) /%.d )
118
119
119
120
@@ -127,9 +128,11 @@ DEP_SUBDIRS := $(sort $(dir $(DEP)))
127
128
128
129
export CC CFLAGS MAKECMDGOALS MAKEFLAGS
129
130
131
+ SHELL := /bin/bash
132
+
130
133
PHONY_TARGETS := all run noenv nocolor opt san val valfd term clear modes re \
131
134
build lib waitforlib clean fclean ffclean
132
- ENV_VARIABLES := MODE
135
+ ENV_VARIABLES := MODE TERMINAL
133
136
HELP_TARGETS := help help-print \
134
137
$(addprefix help-,$(PHONY_TARGETS ) $(ENV_VARIABLES ) ) \
135
138
$(addsuffix -help,$(PHONY_TARGETS ) $(ENV_VARIABLES ) )
@@ -200,19 +203,19 @@ endif
200
203
201
204
all :
202
205
if $( MAKE) --question build; then \
203
- echo -n $(MSG_NO_CHANGE ) ; \
204
- echo -n $(MSG_HELP ) ; \
206
+ echo -e - n $(MSG_NO_CHANGE ) ; \
207
+ echo -e - n $(MSG_HELP ) ; \
205
208
else \
206
- echo -n $(MSG_MODE ) ; \
207
- echo -n " " $(MSG_INFO ) ; \
208
- echo -n " " $(MSG_HELP ) ; \
209
- echo -n $(MSG_START ) ; \
209
+ echo -e - n $(MSG_MODE ) ; \
210
+ echo -e - n " " $(MSG_INFO ) ; \
211
+ echo -e - n " " $(MSG_HELP ) ; \
212
+ echo -e - n $(MSG_START ) ; \
210
213
if $( MAKE) build; then \
211
214
echo ; \
212
- echo -n $(MSG_SUCCESS ) ; \
215
+ echo -e - n $(MSG_SUCCESS ) ; \
213
216
else \
214
217
echo ; \
215
- echo -n $(MSG_FAILURE ) ; \
218
+ echo -e - n $(MSG_FAILURE ) ; \
216
219
exit 42; \
217
220
fi ; \
218
221
fi
@@ -230,10 +233,10 @@ modes :
230
233
clear; \
231
234
fi
232
235
if [ " $( NEW_TERM) " = " true" ] && [ -n " $( TERMINAL) " ]; then \
233
- $(TERMINAL ) $(TERMINALFLAGS ) \
234
- " bash --posix -c ' trap \"\" SIGINT; \
236
+ $(TERMINAL ) $(TERMINALFLAGS ) bash --posix -c \
237
+ " trap '' SIGINT; \
235
238
$(ENV ) ./$(NAME ) ; \
236
- exec bash --posix' "; \
239
+ exec bash --posix" ; \
237
240
elif [ " $( RUN) " = " true" ]; then \
238
241
$(ENV ) " ./$( NAME) " ; \
239
242
fi
@@ -275,14 +278,14 @@ waitforlib : lib
275
278
# Executable linkage
276
279
277
280
$(NAME ) : $(LIBRARIES ) $(OBJ )
278
- $(CC ) $(CFLAGS ) $(LDFLAGS ) $(OBJ ) $(LDLIBS ) -o $@
281
+ $(CC ) $(CFLAGS ) $(LDFLAGS ) $(OBJ ) $(LDLIBS ) -o $( NAME )
279
282
280
283
281
284
# Source file compilation
282
285
283
286
$(OBJ_DIR ) /% .o : $(SRC_DIR ) /% $(SRC_EXTENSION ) $(BUILDFILES ) | $(OBJ_SUBDIRS )
284
287
$(CC ) $(CPPFLAGS ) $(CFLAGS ) -c $< -o $@ \
285
- && echo -n $(MSG_PROGRESS )
288
+ && echo -e - n $(MSG_PROGRESS )
286
289
287
290
288
291
# Pre-processing and dependency file creation
@@ -301,7 +304,7 @@ $(DEP_SUBDIRS) :
301
304
# ***************************** CLEAN TARGETS ******************************** #
302
305
303
306
clean :
304
- echo -n $(MSG_CLEAN )
307
+ echo -e - n $(MSG_CLEAN )
305
308
$(MAKE ) clean -C $(LIBRARIES )
306
309
rm -f $(OBJ ) $(DEP )
307
310
ifneq (, $(wildcard $(OBJ_DIR)))
@@ -310,120 +313,132 @@ clean :
310
313
ifneq (, $(wildcard $(DEP_DIR)))
311
314
-find $(DEP_DIR) -type d -empty -delete
312
315
endif
313
- echo -n $(MSG_SUCCESS)
316
+ echo -e - n $(MSG_SUCCESS)
314
317
315
318
fclean :
316
- echo -n $(MSG_FCLEAN )
319
+ echo -e - n $(MSG_FCLEAN )
317
320
$(MAKE ) clean
318
321
$(MAKE ) fclean -C $(LIBRARIES )
319
322
rm -f $(NAME )
320
- echo -n $(MSG_SUCCESS )
323
+ echo -e - n $(MSG_SUCCESS )
321
324
322
325
ffclean :
323
- echo -n $(MSG_FFCLEAN )
326
+ echo -e - n $(MSG_FFCLEAN )
324
327
$(MAKE ) fclean
325
328
rm -rf $(OBJ_DIR ) $(DEP_DIR )
326
- echo -n $(MSG_SUCCESS )
329
+ echo -e - n $(MSG_SUCCESS )
327
330
328
331
329
332
# ****************************** HELP TARGETS ******************************** #
330
333
331
334
help :
332
- echo " Targets:"
333
- echo " all Build the project (default target)"
334
- echo " run Build and run the project"
335
- echo " noenv Build and run the project with an empty environment"
336
- echo " nocolor Rebuild the project without colors in the prompt and printouts"
337
- echo " opt Rebuild the project with optimizations"
338
- echo " san Rebuild the project with sanitizers"
339
- echo " val Build and run the project with valgrind"
340
- echo " valfd Build and run the project with valgrind and file descriptor tracking"
341
- echo " term Build and run the project in a new terminal window"
342
- echo " clear Build the project and clear the terminal"
343
- echo " re Rebuild the project"
344
- echo " clean Remove build artifacts"
345
- echo " fclean Remove build artifacts and executable"
346
- echo " ffclean Remove build artifacts and executable without checking for unknown files"
347
- echo " print-% Print the value of a Makefile variable (replace % with variable name)"
348
- echo " help Display this message"
349
- echo " help-% | %-help Display more information for a specific target (replace % with target name)"
335
+ echo -e " Targets:"
336
+ echo -e " all Build the project (default target)"
337
+ echo -e " run Build and run the project"
338
+ echo -e " noenv Build and run the project with an empty environment"
339
+ echo -e " nocolor Rebuild the project without colors in the prompt and printouts"
340
+ echo -e " opt Rebuild the project with optimizations"
341
+ echo -e " san Rebuild the project with sanitizers"
342
+ echo -e " val Build and run the project with valgrind"
343
+ echo -e " valfd Build and run the project with valgrind and file descriptor tracking"
344
+ echo -e " term Build and run the project in a new terminal window"
345
+ echo -e " clear Build the project and clear the terminal"
346
+ echo -e " re Rebuild the project"
347
+ echo -e " clean Remove build artifacts"
348
+ echo -e " fclean Remove build artifacts and executable"
349
+ echo -e " ffclean Remove build artifacts and executable without checking for unknown files"
350
+ echo -e " print-% Print the value of a Makefile variable (replace % with variable name)"
351
+ echo -e " help Display this message"
352
+ echo -e " help-% | %-help Display more information for a specific target (replace % with target name)"
350
353
echo
351
- echo " Environment Variables:"
352
- echo " MODE Build mode to combine multiple targets"
354
+ echo -e " Environment Variables:"
355
+ echo -e " MODE Build mode to combine multiple targets"
356
+ echo -e " TERMINAL Terminal emulator to use for targets opening a new terminal window"
353
357
echo
354
- echo " Usage: make [\\ $( STY_UND) target\\ $( STY_RES) ] [MODE=\" <\\ $( STY_UND) mode1\\ $( STY_RES) > [\\ $( STY_UND) mode2\\ $( STY_RES) ] [...]\" ]"
358
+ echo -e " Usage: make [\\ $( STY_UND) target\\ $( STY_RES) ] [MODE=\" <\\ $( STY_UND) mode1\\ $( STY_RES) > [\\ $( STY_UND) mode2\\ $( STY_RES) ] [...]\" ] [TERMINAL=< \\ $( STY_UND ) terminal \\ $( STY_RES ) > ]"
355
359
356
360
help-all :
357
- echo " Build the project."
358
- echo " This is the default target when no target is specified."
361
+ echo -e " Build the project."
362
+ echo -e " This is the default target when no target is specified."
359
363
360
364
help-run :
361
- echo " Build the project and run the executable."
365
+ echo -e " Build the project and run the executable."
362
366
363
367
help-noenv :
364
- echo " Build the project and run executable with an empty environment (env -i)."
368
+ echo -e " Build the project and run executable with an empty environment (env -i)."
365
369
366
370
help-nocolor :
367
- echo " Rebuild the project without colors in the prompt and printouts."
368
- echo " Useful when ANSI escape sequences are not well supported in a terminal emulator."
371
+ echo -e " Rebuild the project without colors in the prompt and printouts."
372
+ echo -e " Useful when ANSI escape sequences are not well supported in a terminal emulator."
369
373
370
374
help-opt :
371
- echo " Rebuild the project with the following compiler optimization flags:"
372
- echo " $( CFLAGS_OPT) "
375
+ echo -e " Rebuild the project with the following compiler optimization flags:"
376
+ echo -e " $( CFLAGS_OPT) "
373
377
374
378
help-san :
375
- echo " Rebuild the project with the following sanitizer flags:"
376
- echo " $( CFLAGS_SAN) "
379
+ echo -e " Rebuild the project with the following sanitizer flags:"
380
+ echo -e " $( CFLAGS_SAN) "
377
381
378
382
help-val :
379
- echo " Build the project and run the executable with valgrind."
383
+ echo -e " Build the project and run the executable with valgrind."
380
384
echo
381
- echo " The following valgrind flags are used:"
382
- echo " $( VALGRINDFLAGS) " | tr ' ' ' \n' | sed ' s/^/ /'
385
+ echo -e " The following valgrind flags are used:"
386
+ echo -e " $( VALGRINDFLAGS) " | tr ' ' ' \n' | sed ' s/^/ /'
383
387
384
388
help-valfd :
385
- echo " Build the project and run the executable with valgrind and file descriptor tracking."
386
- echo " A new terminal window is opened to avoid inheriting open file descriptors."
389
+ echo -e " Build the project and run the executable with valgrind and file descriptor tracking."
390
+ echo -e " A new terminal window is opened to avoid inheriting open file descriptors."
387
391
echo
388
- echo " The following valgrind flags are used:"
389
- echo " $( VALGRINDFLAGS) " | tr ' ' ' \n' | sed ' s/^/ /'
390
- echo " File descriptor specific flags:"
391
- echo " $( VALGRINDFDFLAGS) " | tr ' ' ' \n' | sed ' s/^/ /'
392
+ echo -e " The following valgrind flags are used:"
393
+ echo -e " $( VALGRINDFLAGS) " | tr ' ' ' \n' | sed ' s/^/ /'
394
+ echo -e " File descriptor specific flags:"
395
+ echo -e " $( VALGRINDFDFLAGS) " | tr ' ' ' \n' | sed ' s/^/ /'
392
396
393
397
help-term :
394
- echo " Build the project and run the executable in a new terminal window."
398
+ echo -e " Build the project and run the executable in a new terminal window."
399
+ echo -e " The terminal emulator used is determined by the TERMINAL variable."
400
+ echo
401
+ echo -e " The following terminal emulator is used by default:"
402
+ echo -e " $( TERMINAL) "
403
+ echo
404
+ echo -e " Usage: make term [TERMINAL=<\\ $( STY_UND) terminal\\ $( STY_RES) >]"
395
405
396
406
help-clear :
397
- echo " Build the project and clear the terminal."
407
+ echo -e " Build the project and clear the terminal."
398
408
399
409
help-re :
400
- echo " Rebuild the project."
410
+ echo -e " Rebuild the project."
401
411
402
412
help-clean :
403
- echo " Remove build artifacts."
413
+ echo -e " Remove build artifacts."
404
414
405
415
help-fclean :
406
- echo " Remove build artifacts and the executable."
416
+ echo -e " Remove build artifacts and the executable."
407
417
408
418
help-ffclean :
409
- echo " Remove build artifacts and the executable without checking for unknown files."
419
+ echo -e " Remove build artifacts and the executable without checking for unknown files."
410
420
411
421
help-print :
412
- echo " Print the value of a Makefile variable by appending the variable name to print-..."
413
- echo " Useful for Makefile debugging."
422
+ echo -e " Print the value of a Makefile variable by appending the variable name to print-..."
423
+ echo -e " Useful for Makefile debugging."
414
424
echo
415
- echo " Usage: make print-<\\ $( STY_UND) variable name\\ $( STY_RES) >"
425
+ echo -e " Usage: make print-<\\ $( STY_UND) variable name\\ $( STY_RES) >"
416
426
417
427
help-help :
418
- echo " Display more information for a specific target by appending or prepending help."
428
+ echo -e " Display more information for a specific target by appending or prepending help."
419
429
echo
420
- echo " Usage: make help-<\\ $( STY_UND) target\\ $( STY_RES) > | make <\\ $( STY_UND) target\\ $( STY_RES) >-help"
430
+ echo -e " Usage: make help-<\\ $( STY_UND) target\\ $( STY_RES) > | make <\\ $( STY_UND) target\\ $( STY_RES) >-help"
421
431
422
432
help-MODE MODE-help :
423
- echo " Build mode to combine with other targets."
424
- echo " Multiple modes can be combined by separating them with a space."
433
+ echo -e " Build mode to combine with other targets."
434
+ echo -e " Multiple modes can be combined by separating them with a space."
435
+ echo
436
+ echo -e " Usage: make <\\ $( STY_UND) target\\ $( STY_RES) > MODE=\" <\\ $( STY_UND) mode1\\ $( STY_RES) > [\\ $( STY_UND) mode2\\ $( STY_RES) ] [...]\" "
437
+
438
+ help-TERMINAL TERMINAL-help :
439
+ echo -e " Override the default terminal emulator for targets opening a new terminal window."
425
440
echo
426
- echo " Usage: make <\\ $( STY_UND) target\\ $( STY_RES) > MODE= \" <\\ $( STY_UND) mode1 \\ $( STY_RES) > [ \\ $( STY_UND ) mode2 \\ $( STY_RES ) ] [...] \" "
441
+ echo -e " Usage: make <\\ $( STY_UND) target\\ $( STY_RES) > TERMINAL= <\\ $( STY_UND) terminal \\ $( STY_RES) >"
427
442
428
443
% -help :
429
444
$(MAKE ) help-$(subst -help,,$@ )
@@ -547,7 +562,7 @@ endif
547
562
# *************************** MAKEFILE DEBUGGING ***************************** #
548
563
549
564
print-% :
550
- echo $* = $($* )
565
+ echo -e $* = $($* )
551
566
552
567
553
568
# ********************************* NOTES ************************************ #
0 commit comments