Skip to content

Commit d246e0a

Browse files
authored
Merge pull request #389 from LeaYeh/build-makefile-compatibility
2 parents 0ff9a6a + e478063 commit d246e0a

File tree

1 file changed

+105
-90
lines changed

1 file changed

+105
-90
lines changed

Makefile

Lines changed: 105 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
# By: ldulling <ldulling@student.42.fr> +#+ +:+ +#+ #
77
# +#+#+#+#+#+ +#+ #
88
# 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 #
1010
# #
1111
# **************************************************************************** #
1212

@@ -42,14 +42,14 @@ BUILDFILES := Makefile \
4242

4343
# Flags
4444

45-
CC ?= cc
45+
CC ?= cc
4646
CC_VERSION := $(shell $(CC) --version | head -1)
4747
CFLAGS_STD := -Wall -Wextra -Werror
4848
CFLAGS_DBG := -ggdb3
4949
CFLAGS_SAN := -fsanitize=address,undefined,bounds,float-divide-by-zero
5050
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))
5353
DEPFLAGS = -M -MP -MF $@ -MT "$(OBJ_DIR)/$*.o $@"
5454
LDFLAGS := $(addprefix -L,$(LIBRARIES))
5555
LDLIBS := $(addprefix -l,$(patsubst lib%,%,$(notdir $(LIBRARIES) $(LIBRARIES_EXT))))
@@ -76,7 +76,6 @@ VALGRIND := $(shell which valgrind)
7676

7777
VALGRINDFLAGS = --errors-for-leak-kinds=all \
7878
--leak-check=full \
79-
--read-var-info=yes \
8079
--show-error-list=yes \
8180
--show-leak-kinds=all \
8281
--suppressions=$(CURDIR)/minishell.supp \
@@ -96,24 +95,26 @@ ABSOLUTE_PATHS := /bin/* \
9695

9796
# Terminal
9897

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+
)))
100102

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)))
108106

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+
)))
110111

111112

112113
# Files
113114

114115
include $(BUILD_DIR)/source_files.mk
115116
SRC_EXTENSION := .c
116-
OBJ := $(SRC:%$(SRC_EXTENSION)=$(OBJ_DIR)/%.o)
117+
OBJ := $(SRC:%$(SRC_EXTENSION)=$(OBJ_DIR)/%.o)
117118
DEP := $(SRC:%$(SRC_EXTENSION)=$(DEP_DIR)/%.d)
118119

119120

@@ -127,9 +128,11 @@ DEP_SUBDIRS := $(sort $(dir $(DEP)))
127128

128129
export CC CFLAGS MAKECMDGOALS MAKEFLAGS
129130

131+
SHELL := /bin/bash
132+
130133
PHONY_TARGETS := all run noenv nocolor opt san val valfd term clear modes re \
131134
build lib waitforlib clean fclean ffclean
132-
ENV_VARIABLES := MODE
135+
ENV_VARIABLES := MODE TERMINAL
133136
HELP_TARGETS := help help-print \
134137
$(addprefix help-,$(PHONY_TARGETS) $(ENV_VARIABLES)) \
135138
$(addsuffix -help,$(PHONY_TARGETS) $(ENV_VARIABLES))
@@ -200,19 +203,19 @@ endif
200203

201204
all :
202205
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); \
205208
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); \
210213
if $(MAKE) build; then \
211214
echo; \
212-
echo -n $(MSG_SUCCESS); \
215+
echo -e -n $(MSG_SUCCESS); \
213216
else \
214217
echo; \
215-
echo -n $(MSG_FAILURE); \
218+
echo -e -n $(MSG_FAILURE); \
216219
exit 42; \
217220
fi; \
218221
fi
@@ -230,10 +233,10 @@ modes :
230233
clear; \
231234
fi
232235
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; \
235238
$(ENV) ./$(NAME); \
236-
exec bash --posix'"; \
239+
exec bash --posix"; \
237240
elif [ "$(RUN)" = "true" ]; then \
238241
$(ENV) "./$(NAME)"; \
239242
fi
@@ -275,14 +278,14 @@ waitforlib : lib
275278
# Executable linkage
276279

277280
$(NAME) : $(LIBRARIES) $(OBJ)
278-
$(CC) $(CFLAGS) $(LDFLAGS) $(OBJ) $(LDLIBS) -o $@
281+
$(CC) $(CFLAGS) $(LDFLAGS) $(OBJ) $(LDLIBS) -o $(NAME)
279282

280283

281284
# Source file compilation
282285

283286
$(OBJ_DIR)/%.o : $(SRC_DIR)/%$(SRC_EXTENSION) $(BUILDFILES) | $(OBJ_SUBDIRS)
284287
$(CC) $(CPPFLAGS) $(CFLAGS) -c $< -o $@ \
285-
&& echo -n $(MSG_PROGRESS)
288+
&& echo -e -n $(MSG_PROGRESS)
286289

287290

288291
# Pre-processing and dependency file creation
@@ -301,7 +304,7 @@ $(DEP_SUBDIRS) :
301304
# ***************************** CLEAN TARGETS ******************************** #
302305

303306
clean :
304-
echo -n $(MSG_CLEAN)
307+
echo -e -n $(MSG_CLEAN)
305308
$(MAKE) clean -C $(LIBRARIES)
306309
rm -f $(OBJ) $(DEP)
307310
ifneq (, $(wildcard $(OBJ_DIR)))
@@ -310,120 +313,132 @@ clean :
310313
ifneq (, $(wildcard $(DEP_DIR)))
311314
-find $(DEP_DIR) -type d -empty -delete
312315
endif
313-
echo -n $(MSG_SUCCESS)
316+
echo -e -n $(MSG_SUCCESS)
314317

315318
fclean :
316-
echo -n $(MSG_FCLEAN)
319+
echo -e -n $(MSG_FCLEAN)
317320
$(MAKE) clean
318321
$(MAKE) fclean -C $(LIBRARIES)
319322
rm -f $(NAME)
320-
echo -n $(MSG_SUCCESS)
323+
echo -e -n $(MSG_SUCCESS)
321324

322325
ffclean :
323-
echo -n $(MSG_FFCLEAN)
326+
echo -e -n $(MSG_FFCLEAN)
324327
$(MAKE) fclean
325328
rm -rf $(OBJ_DIR) $(DEP_DIR)
326-
echo -n $(MSG_SUCCESS)
329+
echo -e -n $(MSG_SUCCESS)
327330

328331

329332
# ****************************** HELP TARGETS ******************************** #
330333

331334
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)"
350353
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"
353357
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)>]"
355359

356360
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."
359363

360364
help-run :
361-
echo "Build the project and run the executable."
365+
echo -e "Build the project and run the executable."
362366

363367
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)."
365369

366370
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."
369373

370374
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)"
373377

374378
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)"
377381

378382
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."
380384
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/^/ /'
383387

384388
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."
387391
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/^/ /'
392396

393397
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)>]"
395405

396406
help-clear :
397-
echo "Build the project and clear the terminal."
407+
echo -e "Build the project and clear the terminal."
398408

399409
help-re :
400-
echo "Rebuild the project."
410+
echo -e "Rebuild the project."
401411

402412
help-clean :
403-
echo "Remove build artifacts."
413+
echo -e "Remove build artifacts."
404414

405415
help-fclean :
406-
echo "Remove build artifacts and the executable."
416+
echo -e "Remove build artifacts and the executable."
407417

408418
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."
410420

411421
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."
414424
echo
415-
echo "Usage: make print-<\\$(STY_UND)variable name\\$(STY_RES)>"
425+
echo -e "Usage: make print-<\\$(STY_UND)variable name\\$(STY_RES)>"
416426

417427
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."
419429
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"
421431

422432
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."
425440
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)>"
427442

428443
%-help:
429444
$(MAKE) help-$(subst -help,,$@)
@@ -547,7 +562,7 @@ endif
547562
# *************************** MAKEFILE DEBUGGING ***************************** #
548563

549564
print-% :
550-
echo $* = $($*)
565+
echo -e $* = $($*)
551566

552567

553568
# ********************************* NOTES ************************************ #

0 commit comments

Comments
 (0)