feat: enhence makefile

This commit is contained in:
2025-09-23 13:47:09 +08:00
parent 237e7c3138
commit b5ccc595e0
4 changed files with 96 additions and 21 deletions

View File

@@ -4,9 +4,13 @@ LDFLAGS :=
SRC_DIR := src
BUILD_DIR := build
TARGET := app
CLANG_FORMAT ?= clang-format
CLANG_TIDY ?= clang-tidy
SRCS := $(wildcard $(SRC_DIR)/*.cpp)
OBJS := $(patsubst $(SRC_DIR)/%.cpp,$(BUILD_DIR)/%.o,$(SRCS))
HDRS := $(wildcard $(SRC_DIR)/*.h) $(wildcard $(SRC_DIR)/*.hpp)
FORMAT_SRCS := $(SRCS) $(HDRS)
$(TARGET): $(OBJS)
$(CXX) $(OBJS) $(LDFLAGS) -o $@
@@ -17,7 +21,7 @@ $(BUILD_DIR)/%.o: $(SRC_DIR)/%.cpp | $(BUILD_DIR)
$(BUILD_DIR):
mkdir -p $(BUILD_DIR)
.PHONY: clean run format
.PHONY: clean run format tidy tools install
run: $(TARGET)
./$(TARGET)
@@ -26,4 +30,53 @@ clean:
rm -rf $(BUILD_DIR) $(TARGET)
format:
clang-format -i $(SRCS)
@command -v $(CLANG_FORMAT) >/dev/null 2>&1 || { \
echo "error: $(CLANG_FORMAT) not found in PATH" >&2; \
exit 1; \
}
@if [ -n "$(strip $(FORMAT_SRCS))" ]; then \
$(CLANG_FORMAT) -i $(FORMAT_SRCS); \
echo "formatted: $(FORMAT_SRCS)"; \
else \
echo "warning: no source files found to format"; \
fi
tidy:
@command -v $(CLANG_TIDY) >/dev/null 2>&1 || { \
echo "error: $(CLANG_TIDY) not found in PATH" >&2; \
exit 1; \
}
@if [ -n "$(strip $(SRCS))" ]; then \
for src in $(SRCS); do \
echo "running clang-tidy on $$src"; \
$(CLANG_TIDY) $$src -- $(CXXFLAGS); \
done; \
else \
echo "warning: no source files found for clang-tidy"; \
fi
tools:
@command -v $(CLANG_FORMAT) >/dev/null 2>&1 && echo "found $(CLANG_FORMAT)" || echo "missing $(CLANG_FORMAT)"
@command -v $(CLANG_TIDY) >/dev/null 2>&1 && echo "found $(CLANG_TIDY)" || echo "missing $(CLANG_TIDY)"
install:
@missing=""; \
if ! command -v $(CLANG_FORMAT) >/dev/null 2>&1; then \
missing="$$missing clang-format"; \
fi; \
if ! command -v $(CLANG_TIDY) >/dev/null 2>&1; then \
missing="$$missing clang-tidy"; \
fi; \
if [ -z "$$missing" ]; then \
echo "clang-format and clang-tidy already installed"; \
else \
echo "missing tools:$$missing"; \
if command -v apt-get >/dev/null 2>&1; then \
if command -v sudo >/dev/null 2>&1; then SUDO=sudo; else SUDO=; fi; \
$$SUDO apt-get update; \
$$SUDO apt-get install -y $$missing; \
else \
echo "error: apt-get not available; install manually:$$missing" >&2; \
exit 1; \
fi; \
fi