chore: added Makefile

This commit is contained in:
Zoëy Noort 2025-05-11 19:53:20 +02:00
parent e884562ba6
commit 6aef79af7e

48
Makefile Normal file
View file

@ -0,0 +1,48 @@
# Variables
CC = clang
CFLAGS = -Wall -Wextra -Werror -O3
LDFLAGS = -lSDL2 -flto
LIBFT_DIR = libs/libft
LIBFT_BUILD_DIR = $(LIBFT_DIR)/build
LIBFT_LIB = $(LIBFT_BUILD_DIR)/libft.a
SRC_DIR = srcs
OBJ_DIR = obj
BIN_DIR = bin
HDR_DIR = hdrs
SRCS = $(wildcard $(SRC_DIR)/*.c) # List all .c files in srcs directory
OBJS = $(SRCS:$(SRC_DIR)/%.c=$(OBJ_DIR)/%.o) # Convert .c to .o in obj directory
EXEC = $(BIN_DIR)/zboy # The output binary name
# Default target
all: $(EXEC)
# Create bin and obj directories if they don't exist
$(OBJ_DIR) $(BIN_DIR):
@mkdir -p $@
# Compile .c files to .o object files
$(OBJ_DIR)/%.o: $(SRC_DIR)/%.c $(HDR_DIR)/*.h | $(OBJ_DIR)
$(CC) $(CFLAGS) -I$(HDR_DIR) -I$(LIBFT_DIR)/hdrs -c $< -o $@
# Build libft.a by running make in libs/libft
$(LIBFT_LIB):
@echo "Building libft..."
$(MAKE) -C $(LIBFT_DIR)
# Link object files and libft.a to create the final executable
$(EXEC): $(OBJS) $(LIBFT_LIB) | $(BIN_DIR)
$(CC) $(OBJS) -o $(EXEC) $(LIBFT_LIB) $(LDFLAGS)
# Clean up object files and the executable
clean:
rm -rf $(OBJ_DIR)/*.o $(EXEC)
# Remove all generated files including bin and obj directories
fclean: clean
rm -rf $(OBJ_DIR) $(BIN_DIR)
$(MAKE) -C $(LIBFT_DIR) fclean
# Rebuild everything from scratch
re: fclean all
.PHONY: all clean fclean re