create better Makefile

This commit is contained in:
whaffman 2025-02-23 11:59:59 +01:00
parent a81b84f605
commit e83df82dc3
4 changed files with 210 additions and 31 deletions

139
Makefile
View File

@ -6,7 +6,7 @@
# By: qmennen <qmennen@student.codam.nl> +#+ # # By: qmennen <qmennen@student.codam.nl> +#+ #
# +#+ # # +#+ #
# Created: 2024/10/15 11:48:46 by whaffman #+# #+# # # 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 SRC_PATH = src
INC_PATH = inc INC_PATH = inc
LIB_PATH = lib LIB_PATH = lib
BUILD_PATH = build
LIBFT_PATH = $(LIB_PATH)/libft LIBFT_PATH = $(LIB_PATH)/libft
LIBFT_INC_PATH = $(LIBFT_PATH)/inc LIBFT_INC_PATH = $(LIBFT_PATH)/inc
LIBFT = $(LIBFT_PATH)/libft.a 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 CC = cc
RM = rm -rf RM = rm -rf
INCLUDES = -I./$(INC_PATH) -I./$(LIBFT_INC_PATH) INCLUDES = -I./$(INC_PATH) -I./$(LIBFT_INC_PATH)
CFLAGS = -Wall -Wextra -Werror -fsanitize=address,undefined -MMD -g3
UNAME_S := $(shell uname -s) UNAME_S := $(shell uname -s)
ifeq ($(UNAME_S),Linux) ifeq ($(UNAME_S),Linux)
LDLIBS := -L$(LIBFT_PATH) -lft -lreadline LDLIBS := -L$(LIBFT_PATH) -lft -lreadline
endif endif
all: $(NAME) # Include sources.mk if it exists
echo $(SOURCES) -include sources.mk
$(NAME): $(LIBFT) $(OBJECTS) # Build configurations
$(CC) $(CFLAGS) $(OBJECTS) $(LDLIBS) -o $(NAME) 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) $(MAKE) -C $(LIBFT_PATH)
$(LIBFT_PATH): $(LIBFT_PATH):
$(info $(bold)$(green)Adding libft submodule$(reset))
git submodule add https://gitea.duinvoetje.nl/willem/libft.git $(LIBFT_PATH) git submodule add https://gitea.duinvoetje.nl/willem/libft.git $(LIBFT_PATH)
$(OBJ_PATH): # Generate sources.mk
mkdir -p $@ sources.mk:
$(info $(bold)$(green)Generating sources.mk$(reset))
$(OBJ_PATH)/%.o: %.c $(LIBFT) | $(OBJ_PATH) @echo "VPATH = $$(find $(SRC_PATH) -type d | tr '\n' ':')" > sources.mk
$(CC) $(CFLAGS) $(INCLUDES) -c $< -o $@ @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: clean:
$(RM) $(OBJECTS) $(OBJ_PATH) $(info $(bold)$(red)Cleaning$(reset))
find $(BUILD_PATH) -type d -name 'obj' -exec $(RM) {} +
$(MAKE) -C $(LIBFT_PATH) clean $(MAKE) -C $(LIBFT_PATH) clean
# Remove build artifacts and the executable
fclean: clean fclean: clean
$(info $(bold)$(red)Full cleaning$(reset))
$(RM) $(BUILD_PATH)
$(RM) $(NAME) $(RM) $(NAME)
$(MAKE) -C $(LIBFT_PATH) fclean $(MAKE) -C $(LIBFT_PATH) fclean
re: fclean all # Rebuild the project
re: fclean
$(info $(bold)$(green)Rebuilding$(reset))
$(MAKE) all $(NAME)
run: all # Run the project with the release build configuration
./$(NAME) 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

77
Makefile.old Normal file
View File

@ -0,0 +1,77 @@
# **************************************************************************** #
# #
# :::::::: #
# Makefile :+: :+: #
# +:+ #
# By: qmennen <qmennen@student.codam.nl> +#+ #
# +#+ #
# 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

20
sources.mk Normal file
View File

@ -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 \

View File

@ -6,7 +6,7 @@
/* By: willem <willem@student.codam.nl> +#+ */ /* By: willem <willem@student.codam.nl> +#+ */
/* +#+ */ /* +#+ */
/* Created: 2025/02/12 21:25:22 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; command = (t_command *)current->content;
if (current->next) if (current->next)
{ {
pipe(fd); if (!pipe(fd))
error_msg("pipe", "pipe creation failed");
command->fd_out = fd[1]; command->fd_out = fd[1];
} }
else else