From e83df82dc344595b7765c75bf8bcce8121a8f3cd Mon Sep 17 00:00:00 2001 From: whaffman Date: Sun, 23 Feb 2025 11:59:59 +0100 Subject: [PATCH] create better Makefile --- Makefile | 139 +++++++++++++++++++++------ Makefile.old | 77 +++++++++++++++ sources.mk | 20 ++++ src/executor/executor_create_pipes.c | 5 +- 4 files changed, 210 insertions(+), 31 deletions(-) create mode 100644 Makefile.old create mode 100644 sources.mk diff --git a/Makefile b/Makefile index 634f3cc..8d04098 100644 --- a/Makefile +++ b/Makefile @@ -6,7 +6,7 @@ # By: qmennen +#+ # # +#+ # # Created: 2024/10/15 11:48:46 by whaffman #+# #+# # -# Updated: 2025/02/20 11:10:42 by whaffman ######## odam.nl # +# Updated: 2025/02/23 11:20:52 by willem ######## odam.nl # # # # **************************************************************************** # @@ -15,63 +15,144 @@ NAME = minishell SRC_PATH = src INC_PATH = inc LIB_PATH = lib +BUILD_PATH = build LIBFT_PATH = $(LIB_PATH)/libft LIBFT_INC_PATH = $(LIBFT_PATH)/inc LIBFT = $(LIBFT_PATH)/libft.a -OBJ_PATH = obj - -VPATH = src:src/environment:src/prompt:src/lexer:src/token:src/utils: -VPATH += src/executor:src/parser:src/expander:src/debug:src/signal:src/builtin -SOURCES = $(shell basename -a $(shell find $(SRC_PATH) -type f -name "*.c")) - -OBJECTS = $(addprefix $(OBJ_PATH)/, $(SOURCES:.c=.o)) -DEPENDS = ${OBJECTS:.o=.d} - CC = cc RM = rm -rf INCLUDES = -I./$(INC_PATH) -I./$(LIBFT_INC_PATH) -CFLAGS = -Wall -Wextra -Werror -fsanitize=address,undefined -MMD -g3 UNAME_S := $(shell uname -s) ifeq ($(UNAME_S),Linux) - LDLIBS := -L$(LIBFT_PATH) -lft -lreadline + LDLIBS := -L$(LIBFT_PATH) -lft -lreadline endif -all: $(NAME) - echo $(SOURCES) +# Include sources.mk if it exists +-include sources.mk -$(NAME): $(LIBFT) $(OBJECTS) - $(CC) $(CFLAGS) $(OBJECTS) $(LDLIBS) -o $(NAME) +# Build configurations +BUILD_CONFIGS = release debug asan tsan --include ${DEPENDS} +release_CFLAGS = -Wall -Werror -Werror -O2 +debug_CFLAGS = -Wall -Werror -Werror -g3 +asan_CFLAGS = -Wall -Werror -Werror -fsanitize=address,leak,undefined -g3 +tsan_CFLAGS = -Wall -Werror -Werror -fsanitize=thread -g3 -$(LIBFT): $(LIBFT_PATH) +# Targets for each build configuration +define BUILD_TARGETS +$(1)_OBJ_PATH = $(BUILD_PATH)/$(1)/obj +$(1)_DEPENDS = $$(addprefix $$($(1)_OBJ_PATH)/, $$(SOURCES:.c=.d)) +$(1)_OBJECTS = $$(addprefix $$($(1)_OBJ_PATH)/, $$(SOURCES:.c=.o)) + +.PHONY: $(1) +$(1): CFLAGS = $$($(1)_CFLAGS) +$(1): $(BUILD_PATH)/$(1)/$(NAME) + +.PHONY: run_$(1) + +run_$(1): $(1) + $$(info $(bold)$(green)Running $(1)$(reset)) + ./$(BUILD_PATH)/$(1)/$(NAME) + +$(BUILD_PATH)/$(1)/$(NAME): $(LIBFT) $$($(1)_OBJECTS) + $$(info $(bold)$(green)Linking $(1) config$(reset)) + $$(CC) $$(CFLAGS) $$($(1)_OBJECTS) $$(LDLIBS) -o $$@ + +-include $$($(1)_DEPENDS) + +$$($(1)_OBJ_PATH)/%.o: %.c $(LIBFT) | $$($(1)_OBJ_PATH) + $$(CC) $$(CFLAGS) $$(INCLUDES) -MMD -MP -c $$< -o $$@ + +$$($(1)_OBJ_PATH): + $$(info $(bold)$(green)Creating $(1) object directory$(reset)) + mkdir -p $$@ +endef + +# Copy the release build configuration to the root +$(NAME): $(BUILD_PATH)/release/$(NAME) + $(info $(bold)$(green)Copying release build to root$(reset)) + cp $< $@ + +$(foreach config,$(BUILD_CONFIGS),$(eval $(call BUILD_TARGETS,$(config)))) + +# Build all configurations +all: release debug asan tsan + $(info $(bold)$(green)Building all$(reset)) + +# Build libft +$(LIBFT): $(LIBFT_PATH) check-and-reinit-submodules + $(info $(bold)$(green)Building libft$(reset)) $(MAKE) -C $(LIBFT_PATH) $(LIBFT_PATH): + $(info $(bold)$(green)Adding libft submodule$(reset)) git submodule add https://gitea.duinvoetje.nl/willem/libft.git $(LIBFT_PATH) -$(OBJ_PATH): - mkdir -p $@ - -$(OBJ_PATH)/%.o: %.c $(LIBFT) | $(OBJ_PATH) - $(CC) $(CFLAGS) $(INCLUDES) -c $< -o $@ +# Generate sources.mk +sources.mk: + $(info $(bold)$(green)Generating sources.mk$(reset)) + @echo "VPATH = $$(find $(SRC_PATH) -type d | tr '\n' ':')" > sources.mk + @echo "SOURCES = $$(find $(SRC_PATH) -type f -name '*.c' -exec basename {} \; | tr '\n' ' ' | fold -s -w 70 | sed 's/$$/ \\/')" >> sources.mk +# Remove build artifacts clean: - $(RM) $(OBJECTS) $(OBJ_PATH) + $(info $(bold)$(red)Cleaning$(reset)) + find $(BUILD_PATH) -type d -name 'obj' -exec $(RM) {} + $(MAKE) -C $(LIBFT_PATH) clean - +# Remove build artifacts and the executable fclean: clean + $(info $(bold)$(red)Full cleaning$(reset)) + $(RM) $(BUILD_PATH) $(RM) $(NAME) $(MAKE) -C $(LIBFT_PATH) fclean -re: fclean all +# Rebuild the project +re: fclean + $(info $(bold)$(green)Rebuilding$(reset)) + $(MAKE) all $(NAME) -run: all - ./$(NAME) +# Run the project with the release build configuration +run: run_release + $(info $(bold)$(green)Running$(reset)) + +# Remove sources.mk to force regeneration +srcs: + $(info $(bold)$(green)Regenerating sources.mk$(reset)) + $(RM) sources.mk + +# Show this help. +help: + @echo "Usage: make [target]" + @echo "" + @echo "Targets:" + @awk '/^#/{c=substr($$0,3);next}c&&/^[[:alpha:]][[:alnum:]_-]+:/{print substr($$1,1,index($$1,":")),c}1{c=0}' $(MAKEFILE_LIST) | column -s: -t + @echo "" + +.PHONY: check-and-reinit-submodules +check-and-reinit-submodules: + $(info $(bold)$(green)Checking and reinitializing submodules$(reset)) + @if git submodule status | egrep -q '^[-+]' ; then \ + echo "INFO: Need to reinitialize git submodules"; \ + git submodule update --init; \ + fi + +.PHONY: all clean fclean re srcs run help + +red:=$(shell tput setaf 1) +green:=$(shell tput setaf 2) +yellow:=$(shell tput setaf 3) +blue:=$(shell tput setaf 4) +magenta:=$(shell tput setaf 5) +cyan:=$(shell tput setaf 6) +white:=$(shell tput setaf 7) + +bold:=$(shell tput bold) +underline:=$(shell tput smul) + +reset:=$(shell tput sgr0) -.PHONY: all clean fclean re run diff --git a/Makefile.old b/Makefile.old new file mode 100644 index 0000000..634f3cc --- /dev/null +++ b/Makefile.old @@ -0,0 +1,77 @@ +# **************************************************************************** # +# # +# :::::::: # +# Makefile :+: :+: # +# +:+ # +# By: qmennen +#+ # +# +#+ # +# Created: 2024/10/15 11:48:46 by whaffman #+# #+# # +# Updated: 2025/02/20 11:10:42 by whaffman ######## odam.nl # +# # +# **************************************************************************** # + +NAME = minishell + +SRC_PATH = src +INC_PATH = inc +LIB_PATH = lib + +LIBFT_PATH = $(LIB_PATH)/libft +LIBFT_INC_PATH = $(LIBFT_PATH)/inc +LIBFT = $(LIBFT_PATH)/libft.a + +OBJ_PATH = obj + +VPATH = src:src/environment:src/prompt:src/lexer:src/token:src/utils: +VPATH += src/executor:src/parser:src/expander:src/debug:src/signal:src/builtin +SOURCES = $(shell basename -a $(shell find $(SRC_PATH) -type f -name "*.c")) + +OBJECTS = $(addprefix $(OBJ_PATH)/, $(SOURCES:.c=.o)) +DEPENDS = ${OBJECTS:.o=.d} + +CC = cc +RM = rm -rf + +INCLUDES = -I./$(INC_PATH) -I./$(LIBFT_INC_PATH) +CFLAGS = -Wall -Wextra -Werror -fsanitize=address,undefined -MMD -g3 + +UNAME_S := $(shell uname -s) +ifeq ($(UNAME_S),Linux) + LDLIBS := -L$(LIBFT_PATH) -lft -lreadline +endif + +all: $(NAME) + echo $(SOURCES) + +$(NAME): $(LIBFT) $(OBJECTS) + $(CC) $(CFLAGS) $(OBJECTS) $(LDLIBS) -o $(NAME) + +-include ${DEPENDS} + +$(LIBFT): $(LIBFT_PATH) + $(MAKE) -C $(LIBFT_PATH) + +$(LIBFT_PATH): + git submodule add https://gitea.duinvoetje.nl/willem/libft.git $(LIBFT_PATH) + +$(OBJ_PATH): + mkdir -p $@ + +$(OBJ_PATH)/%.o: %.c $(LIBFT) | $(OBJ_PATH) + $(CC) $(CFLAGS) $(INCLUDES) -c $< -o $@ + +clean: + $(RM) $(OBJECTS) $(OBJ_PATH) + $(MAKE) -C $(LIBFT_PATH) clean + + +fclean: clean + $(RM) $(NAME) + $(MAKE) -C $(LIBFT_PATH) fclean + +re: fclean all + +run: all + ./$(NAME) + +.PHONY: all clean fclean re run diff --git a/sources.mk b/sources.mk new file mode 100644 index 0000000..862e5a7 --- /dev/null +++ b/sources.mk @@ -0,0 +1,20 @@ +VPATH = src:src/parser:src/environment:src/lexer:src/debug:src/token:src/signal:src/prompt:src/utils:src/expander:src/executor:src/builtin: +SOURCES = parser_get_arguments.c parser_new_command.c parser_get_commands.c \ +environment_get.c environment_get_arr.c environment_free_list.c \ +environment_add.c environment_print.c environment_del.c \ +environment_free.c environment_parse.c lexer_new.c lexer_read_word.c \ +lexer_read_char.c lexer_parse_input.c lexer_token_next.c \ +print_commands.c main.c token_new.c token_parse.c signal.c \ +history_write.c prompt.c history_load.c ft_malloc_safe.c \ +free_command_list.c free_minishell.c free_lexer.c print_banner.c \ +check_malloc.c error_msg.c free_token_list.c free_minishell_line.c \ +ft_strdup_safe.c free_token.c init_minishell.c ft_strjoin_safe.c \ +expander_allocate_memory.c expander_parse_string.c \ +expander_parse_variables.c expander_expand_dollar.c \ +expander_is_character.c expander_get_variable.c executor_open_fds.c \ +executor_count_fds.c executor_fork.c executor_absolute_path.c \ +executor_execute_pipeline.c executor_child.c executor_close_fds.c \ +executor_create_redirects.c executor_create_pipes.c builtin_export.c \ +simple_builtins.c builtin_router.c is_builtin.c builtin_unset.c \ +builtin_env.c builtin_cd.c builtin_pwd.c builtin_exit.c \ +builtin_echo.c \ diff --git a/src/executor/executor_create_pipes.c b/src/executor/executor_create_pipes.c index 6c24ff5..9cff884 100644 --- a/src/executor/executor_create_pipes.c +++ b/src/executor/executor_create_pipes.c @@ -6,7 +6,7 @@ /* By: willem +#+ */ /* +#+ */ /* Created: 2025/02/12 21:25:22 by willem #+# #+# */ -/* Updated: 2025/02/21 12:48:04 by whaffman ######## odam.nl */ +/* Updated: 2025/02/22 22:25:28 by willem ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -38,7 +38,8 @@ void executor_create_pipes(t_minishell *minishell) command = (t_command *)current->content; if (current->next) { - pipe(fd); + if (!pipe(fd)) + error_msg("pipe", "pipe creation failed"); command->fd_out = fd[1]; } else