CPP04 1.0
読み取り中…
検索中…
一致する文字列を見つけられません
Makefile
[詳解]
1# **************************************************************************** #
2# #
3# ::: :::::::: #
4# Makefile :+: :+: :+: #
5# +:+ +:+ +:+ #
6# By: kamitsui <kamitsui@student.42tokyo.jp> +#+ +:+ +#+ #
7# +#+#+#+#+#+ +#+ #
8# Created: 2025/05/20 16:30:43 by kamitsui #+# #+# #
9# Updated: 2025/06/03 12:36:58 by kamitsui ### ########.fr #
10# #
11# **************************************************************************** #
12
13#******************************************************************************#
14# @file ex00/Makefile #
15# @brief Makefile for C++ Module 04 Exercise 00. #
16# #
17# This Makefile automates the compilation and linking process for the #
18# Animal, Dog, Cat, WrongAnimal, and WrongCat classes, along with the main #
19# test program. It provides standard targets for building, cleaning, and #
20# recompiling the project. #
21# #
22# Targets: #
23# all: Builds the main executable. #
24# clean: Removes all object files (.o). #
25# fclean: Removes all object files and the executable. #
26# re: Cleans the project and then rebuilds it. #
27#******************************************************************************#
28
29# Directories
30OBJ_DIR = objs
31DEP_DIR = .deps
32
33# Source files
34SRCS = main.cpp Animal.cpp Cat.cpp Dog.cpp WrongAnimal.cpp WrongCat.cpp \
35 Tests.cpp TestRaii.cpp
36
37# Object files and dependency files
38OBJS = $(addprefix $(OBJ_DIR)/, $(SRCS:.cpp=.o))
39DEPS = $(addprefix $(DEP_DIR)/, $(SRCS:.cpp=.d))
40
41# Build target
42NAME = a.out
43
44# Compiler
45CXX = c++
46CXXFLAGS = -Wall -Wextra -Werror -std=c++98
47CF_DEP = -MMD -MP -MF $(@:$(OBJ_DIR)/%.o=$(DEP_DIR)/%.d)
48
49# Rules for building object files
50$(OBJ_DIR)/%.o: %.cpp
51 $(CXX) $(CXXFLAGS) $(CF_DEP) -c $< -o $@
52
53# Default target
54all: directories $(NAME)
55.PHONY: all
56
57# Make Directories
58directories:
59 @mkdir -p $(OBJ_DIR)
60 @mkdir -p $(DEP_DIR)
61.PHONY: directories
62
63# Build Target
64$(NAME): $(OBJS)
65 $(CXX) $(CXXFLAGS) $(OBJS) -o $(NAME)
66 $(call ASCII_ART,$(NAME))
67
68# REPOSITORY for Test
69TEST_ROOT = ../../cpp_module_tester
70TEST_DIR = $(TEST_ROOT)/cpp04
71URL_TEST_CPP = "https://github.com/kamitsui/cpp_module_tester.git"
72TEST_TARGET = $(TEST_DIR)/ex00_app
73EX_NUM = ex00
74
75# Test
76test:
77ifeq ($(shell test -d $(TEST_DIR) && echo exist),)
78 @echo "Please clone the test repository"
79 $(call ASK_AND_EXECUTE_ON_YES, git clone $(URL_TEST_CPP) $(TEST_ROOT))
80endif
81 $(call ASK_AND_EXECUTE_ON_YES, make -C $(TEST_DIR) $(EX_NUM))
82.PHONY: test
83
84# Rule for removing object & dependency files
85clean:
86 rm -rf $(OBJ_DIR) $(DEP_DIR) $(TEST_DIR)/objs $(TEST_DIR)/.deps
87.PHONY: clean
88
89# Rule for removing Target & others
90fclean: clean
91 rm -f $(NAME) $(TEST_TARGET)
92.PHONY: fclean
93
94# Rule for Clean & Build Target
95re: fclean all
96.PHONY: re
97
98# Enable dependency file
99-include $(DEPS)
100
101# ASCII Art : Display Tips the way to use.
102define ASCII_ART
103 @echo " _____________________________________________"
104 @echo "< Usage : >"
105 @echo "< $$ ./a.out >"
106 @echo "< >"
107 @echo "< Test Case : >"
108 @echo "< $$ make test >"
109 @echo "----------------------------------------------"
110 @echo " \ ^__^"
111 @echo " \ (oo)\_______"
112 @echo " (__)\ )\\/\\"
113 @echo " ||----w |"
114 @echo " || ||\n"
115endef
116
117# Ask and Exit on No
118define ASK_AND_EXECUTE_ON_YES
119 @echo "Execute? $(1) (y/n)"
120 @read -r answer; \
121 case "$$answer" in \
122 [yY]) \
123 echo $(1); \
124 $(1); \
125 ;; \
126 *) \
127 echo "Skip ..."; \
128 ;; \
129 esac
130endef
131
132# Makefile Option : Disable '--print-directory'
133MAKEFLAGS += --no-print-directory