refactor: adopt layered C++ project layout

This commit is contained in:
2025-09-23 14:03:16 +08:00
parent b5ccc595e0
commit b74681a0f5
6 changed files with 95 additions and 27 deletions

View File

@@ -1,22 +1,28 @@
CXX := g++
CXXFLAGS := -std=c++20 -Wall -Wextra -Wpedantic -O2
CPPFLAGS :=
LDFLAGS :=
SRC_DIR := src
INC_DIR := include
BUILD_DIR := build
TARGET := app
CLANG_FORMAT ?= clang-format
CLANG_TIDY ?= clang-tidy
SRCS := $(wildcard $(SRC_DIR)/*.cpp)
SRCS := $(shell find $(SRC_DIR) -name '*.cpp' 2>/dev/null)
OBJS := $(patsubst $(SRC_DIR)/%.cpp,$(BUILD_DIR)/%.o,$(SRCS))
HDRS := $(wildcard $(SRC_DIR)/*.h) $(wildcard $(SRC_DIR)/*.hpp)
FORMAT_SRCS := $(SRCS) $(HDRS)
SRC_HDRS := $(shell find $(SRC_DIR) \( -name '*.hpp' -o -name '*.h' \) 2>/dev/null)
INCLUDE_HDRS := $(shell [ -d $(INC_DIR) ] && find $(INC_DIR) \( -name '*.hpp' -o -name '*.h' \) 2>/dev/null)
FORMAT_SRCS := $(SRCS) $(SRC_HDRS) $(INCLUDE_HDRS)
CPPFLAGS += -I$(INC_DIR)
$(TARGET): $(OBJS)
$(CXX) $(OBJS) $(LDFLAGS) -o $@
$(BUILD_DIR)/%.o: $(SRC_DIR)/%.cpp | $(BUILD_DIR)
$(CXX) $(CXXFLAGS) -c $< -o $@
$(BUILD_DIR)/%.o: $(SRC_DIR)/%.cpp
@mkdir -p $(dir $@)
$(CXX) $(CPPFLAGS) $(CXXFLAGS) -c $< -o $@
$(BUILD_DIR):
mkdir -p $(BUILD_DIR)
@@ -49,7 +55,7 @@ tidy:
@if [ -n "$(strip $(SRCS))" ]; then \
for src in $(SRCS); do \
echo "running clang-tidy on $$src"; \
$(CLANG_TIDY) $$src -- $(CXXFLAGS); \
$(CLANG_TIDY) $$src -- $(CPPFLAGS) $(CXXFLAGS); \
done; \
else \
echo "warning: no source files found for clang-tidy"; \