From 8333e4805b3844db9ed5a4cba76d30c9c0417765 Mon Sep 17 00:00:00 2001 From: PoneyUHC Date: Sat, 13 Jul 2024 17:48:12 +0200 Subject: [PATCH] better maefile, avoid full recompile each time --- .gitignore | 4 +++- Makefile | 38 ++++++++++++++++++++++++++++++-------- 2 files changed, 33 insertions(+), 9 deletions(-) diff --git a/.gitignore b/.gitignore index 9576b9b..67b0fd3 100644 --- a/.gitignore +++ b/.gitignore @@ -5,4 +5,6 @@ .vscode -output/ \ No newline at end of file +output/ + +build/ \ No newline at end of file diff --git a/Makefile b/Makefile index 1f0470c..51768c7 100644 --- a/Makefile +++ b/Makefile @@ -1,13 +1,35 @@ -BUILD_DIR = build -EXEC_NAME = render.exe +CXX = g++ +RM = rm -f +CXXFLAGS = -g -O3 -Wall -Wpedantic -std=c++17 -MMD -MP + +EXEC = render.exe OUTPUT_DIR = output SRC_DIR = src -SRC = $(wildcard $(SRC_DIR)/*.cpp) $(wildcard $(SRC_DIR)/**/*.cpp) -CFLAGS = -g -O3 -Wall -Wpedantic -std=c++17 +BUILD_DIR = build +BUILD_OBJS_DIR = $(BUILD_DIR)/binaries +BUILD_EXEC_DIR = $(BUILD_DIR)/executable + +SRCS = $(wildcard $(SRC_DIR)/*.cpp) $(wildcard $(SRC_DIR)/**/*.cpp) +OBJS = $(patsubst %.cpp, %.o, $(SRCS)) + +OBJS_LOCATION = $(patsubst %.o, $(BUILD_OBJS_DIR)/%.o, $(OBJS)) +DEPS = $(patsubst %.o, %.d, $(OBJS_LOCATION)) + +.PHONY: all clean + +all: $(EXEC) + +clean: + $(RM) $(OBJS_LOCATION) $(DEPS) + +$(EXEC): $(OBJS_LOCATION) + if [ ! -d $(BUILD_EXEC_DIR) ]; then mkdir $(BUILD_EXEC_DIR); fi + $(CXX) $(OBJS_LOCATION) $(CXXFLAGS) -I $(SRC_DIR) -o $(BUILD_EXEC_DIR)/$(EXEC) + +-include $(DEPS) -default: render.exe +$(BUILD_OBJS_DIR)/%.o: %.cpp Makefile + if [ ! -d $(dir $@) ]; then mkdir -p $(dir $@); fi + $(CXX) $(CXXFLAGS) -I $(SRC_DIR) -c $< -o $@ -$(EXEC_NAME): - @if [ ! -d $(BUILD_DIR) ]; then mkdir $(BUILD_DIR); fi - @g++ $(SRC) $(CFLAGS) -I $(SRC_DIR) -o $(BUILD_DIR)/$(EXEC_NAME)