From feeadb0a21d61c1423a724eb5ab870ef385b0b45 Mon Sep 17 00:00:00 2001 From: whaffman Date: Wed, 24 Sep 2025 22:34:20 +0200 Subject: [PATCH] refactor: streamline build directory detection and update Makefile for environment handling --- .devcontainer/devcontainer.json | 5 ++++- .vscode/settings.json | 3 ++- Makefile | 30 +++++++++++++++++++++++------- check_iwyu.sh | 18 +++++------------- 4 files changed, 34 insertions(+), 22 deletions(-) diff --git a/.devcontainer/devcontainer.json b/.devcontainer/devcontainer.json index a277f19..ee0281e 100644 --- a/.devcontainer/devcontainer.json +++ b/.devcontainer/devcontainer.json @@ -65,5 +65,8 @@ }, // Simple post-create setup - "postCreateCommand": "echo 'Dev container ready! Use GitHub CLI (gh auth login) or configure Git with HTTPS for GitHub access.'" + "postCreateCommand": "echo 'Dev container ready! Use GitHub CLI (gh auth login) or configure Git with HTTPS for GitHub access.'", + + // Clean build directory and optionally build every time VS Code attaches to container + // "postAttachCommand": "rm -rf /workspaces/webserv/build && echo 'Cleaned build directory for container environment' && (cd /workspaces/webserv && timeout 60 make debug && echo 'Initial build completed successfully' || echo 'Initial build failed or timed out - you can build manually with: make debug') &" } \ No newline at end of file diff --git a/.vscode/settings.json b/.vscode/settings.json index 8a5b79f..00be151 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -47,5 +47,6 @@ "string": "cpp", "unordered_map": "cpp", "string_view": "cpp" - } + }, + "cmake.buildDirectory": "${workspaceFolder}/build" } diff --git a/Makefile b/Makefile index f1eabe3..e33b402 100644 --- a/Makefile +++ b/Makefile @@ -1,35 +1,51 @@ # Variables -# Detect if we're in a dev container or local development -ifeq ($(shell whoami),vscode) - BUILD_DIR = build-container -else - BUILD_DIR = build-local -endif - +BUILD_DIR = build CMAKE = cmake CMAKE_BUILD = cmake --build CMAKE_FLAGS = -DCMAKE_EXPORT_COMPILE_COMMANDS=ON CONFIG_FILE = config/default.conf +# Environment detection +ifeq ($(shell whoami),vscode) + CURRENT_ENV = container +else ifneq ($(REMOTE_CONTAINERS),) + CURRENT_ENV = container +else + CURRENT_ENV = local +endif + +# Check if build directory was created in different environment +ifneq ($(wildcard $(BUILD_DIR)/.build-env),) + PREVIOUS_ENV := $(shell cat $(BUILD_DIR)/.build-env 2>/dev/null || echo "unknown") + ifneq ($(PREVIOUS_ENV),$(CURRENT_ENV)) + $(info Detected environment switch from $(PREVIOUS_ENV) to $(CURRENT_ENV) - cleaning build directory) + $(shell rm -rf $(BUILD_DIR)) + endif +endif + # Default target all: release # Configure CMake if build directory doesn't exist $(BUILD_DIR): $(CMAKE) -B $(BUILD_DIR) $(CMAKE_FLAGS) + @echo "$(CURRENT_ENV)" > $(BUILD_DIR)/.build-env # Build targets with specific build types release: $(BUILD_DIR) $(CMAKE) -B $(BUILD_DIR) $(CMAKE_FLAGS) -DCMAKE_BUILD_TYPE=Release $(CMAKE_BUILD) $(BUILD_DIR) --target webserv --parallel + @echo "$(CURRENT_ENV)" > $(BUILD_DIR)/.build-env debug: $(BUILD_DIR) $(CMAKE) -B $(BUILD_DIR) $(CMAKE_FLAGS) -DCMAKE_BUILD_TYPE=Debug $(CMAKE_BUILD) $(BUILD_DIR) --target webserv + @echo "$(CURRENT_ENV)" > $(BUILD_DIR)/.build-env asan: $(BUILD_DIR) $(CMAKE) -B $(BUILD_DIR) $(CMAKE_FLAGS) -DCMAKE_BUILD_TYPE=ASAN $(CMAKE_BUILD) $(BUILD_DIR) --target webserv + @echo "$(CURRENT_ENV)" > $(BUILD_DIR)/.build-env run: run_release diff --git a/check_iwyu.sh b/check_iwyu.sh index 119e2f4..1839a70 100755 --- a/check_iwyu.sh +++ b/check_iwyu.sh @@ -3,12 +3,8 @@ # Don't exit on first error - we want to continue checking all files # set -e -# Detect project root - try container path first, then current directory -if [ -d "/workspace" ]; then - PROJECT_ROOT="/workspace" -else - PROJECT_ROOT="$(pwd)" -fi +# Use current working directory as project root +PROJECT_ROOT="$(pwd)" # Find the build directory - check multiple possible locations BUILD_DIR="" @@ -67,13 +63,9 @@ if [ -z "$BUILD_DIR" ]; then echo -e "${YELLOW}🔨 Running cmake to create build directory...${NC}" cd "$PROJECT_ROOT" - # Try to create build directory (prefer build-container in container, build-local otherwise) - if [ -d "/workspace" ]; then - BUILD_DIR="$PROJECT_ROOT/build-container" - else - BUILD_DIR="$PROJECT_ROOT/build-local" - fi - + + BUILD_DIR="$PROJECT_ROOT/build" + mkdir -p "$BUILD_DIR" cd "$BUILD_DIR" cmake .. -DCMAKE_EXPORT_COMPILE_COMMANDS=ON