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 01:44:05 by kamitsui ### ########.fr #
10# #
11# **************************************************************************** #
12
13#******************************************************************************#
14# @file ex02/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 \
35 WrongAnimal.cpp WrongCat.cpp \
36 Brain.cpp
37
38# Object files and dependency files
39OBJS = $(addprefix $(OBJ_DIR)/, $(SRCS:.cpp=.o))
40DEPS = $(addprefix $(DEP_DIR)/, $(SRCS:.cpp=.d))
41
42# Build target
43NAME = a.out
44
45# Compiler
46CXX = c++
47CXXFLAGS = -Wall -Wextra -Werror -std=c++98
48CF_DEP = -MMD -MP -MF $(@:$(OBJ_DIR)/%.o=$(DEP_DIR)/%.d)
49
50# Default target
51all: directories $(NAME)
52.PHONY: all
53
54# Build Target
55$(NAME): $(OBJS)
56 $(CXX) $(CXXFLAGS) $(OBJS) -o $(NAME)
57 $(call ASCII_ART,$(NAME))
58
59# Rules for building object files
60$(OBJ_DIR)/%.o: %.cpp
61 $(CXX) $(CXXFLAGS) $(CF_DEP) -c $< -o $@
62
63# Make Directories
64directories:
65 @mkdir -p $(OBJ_DIR)
66 @mkdir -p $(DEP_DIR)
67.PHONY: directories
68
69# REPOSITORY for Test
70TEST_ROOT = ../../cpp_module_tester
71TEST_DIR = $(TEST_ROOT)/cpp04
72URL_TEST_CPP = "https://github.com/kamitsui/cpp_module_tester.git"
73TEST_TARGET = $(TEST_DIR)/ex02_app
74EX_NUM = ex02
75
76# Test
77test:
78ifeq ($(shell test -d $(TEST_DIR) && echo exist),)
79 @echo "Please clone the test repository"
80 $(call ASK_AND_EXECUTE_ON_YES, git clone $(URL_TEST_CPP) $(TEST_ROOT))
81endif
82 $(call ASK_AND_EXECUTE_ON_YES, make -C $(TEST_DIR) $(EX_NUM))
83.PHONY: test
84
85# Rule for removing object & dependency files
86clean:
87 rm -rf $(OBJ_DIR) $(DEP_DIR) $(TEST_DIR)/objs $(TEST_DIR)/.deps
88.PHONY: clean
89
90# Rule for removing Target & others
91fclean: clean
92 rm -f $(NAME) $(TEST_TARGET)
93.PHONY: fclean
94
95# Rule for Clean & Build Target
96re: fclean all
97.PHONY: re
98
99# Enable dependency file
100-include $(DEPS)
101
102# ASCII Art : Display Tips the way to use.
103define ASCII_ART
104 @echo " _____________________________________________"
105 @echo "< Usage : >"
106 @echo "< $$ ./a.out >"
107 @echo "< >"
108 @echo "< Test Case : >"
109 @echo "< $$ make test >"
110 @echo "----------------------------------------------"
111 @echo " \ ^__^"
112 @echo " \ (oo)\_______"
113 @echo " (__)\ )\\/\\"
114 @echo " ||----w |"
115 @echo " || ||\n"
116endef
117
118# Ask and Exit on No
119define ASK_AND_EXECUTE_ON_YES
120 @echo "Execute? $(1) (y/n)"
121 @read -r answer; \
122 case "$$answer" in \
123 [yY]) \
124 echo $(1); \
125 $(1); \
126 ;; \
127 *) \
128 echo "Skip ..."; \
129 ;; \
130 esac
131endef
132
133# Makefile Option : Disable '--print-directory'
134MAKEFLAGS += --no-print-directory