refactor: streamline build directory detection and update Makefile for environment handling

This commit is contained in:
whaffman 2025-09-24 22:34:20 +02:00
parent b145a6eef2
commit feeadb0a21
4 changed files with 34 additions and 22 deletions

View File

@ -65,5 +65,8 @@
}, },
// Simple post-create setup // 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') &"
} }

View File

@ -47,5 +47,6 @@
"string": "cpp", "string": "cpp",
"unordered_map": "cpp", "unordered_map": "cpp",
"string_view": "cpp" "string_view": "cpp"
} },
"cmake.buildDirectory": "${workspaceFolder}/build"
} }

View File

@ -1,35 +1,51 @@
# Variables # Variables
# Detect if we're in a dev container or local development BUILD_DIR = build
ifeq ($(shell whoami),vscode)
BUILD_DIR = build-container
else
BUILD_DIR = build-local
endif
CMAKE = cmake CMAKE = cmake
CMAKE_BUILD = cmake --build CMAKE_BUILD = cmake --build
CMAKE_FLAGS = -DCMAKE_EXPORT_COMPILE_COMMANDS=ON CMAKE_FLAGS = -DCMAKE_EXPORT_COMPILE_COMMANDS=ON
CONFIG_FILE = config/default.conf 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 # Default target
all: release all: release
# Configure CMake if build directory doesn't exist # Configure CMake if build directory doesn't exist
$(BUILD_DIR): $(BUILD_DIR):
$(CMAKE) -B $(BUILD_DIR) $(CMAKE_FLAGS) $(CMAKE) -B $(BUILD_DIR) $(CMAKE_FLAGS)
@echo "$(CURRENT_ENV)" > $(BUILD_DIR)/.build-env
# Build targets with specific build types # Build targets with specific build types
release: $(BUILD_DIR) release: $(BUILD_DIR)
$(CMAKE) -B $(BUILD_DIR) $(CMAKE_FLAGS) -DCMAKE_BUILD_TYPE=Release $(CMAKE) -B $(BUILD_DIR) $(CMAKE_FLAGS) -DCMAKE_BUILD_TYPE=Release
$(CMAKE_BUILD) $(BUILD_DIR) --target webserv --parallel $(CMAKE_BUILD) $(BUILD_DIR) --target webserv --parallel
@echo "$(CURRENT_ENV)" > $(BUILD_DIR)/.build-env
debug: $(BUILD_DIR) debug: $(BUILD_DIR)
$(CMAKE) -B $(BUILD_DIR) $(CMAKE_FLAGS) -DCMAKE_BUILD_TYPE=Debug $(CMAKE) -B $(BUILD_DIR) $(CMAKE_FLAGS) -DCMAKE_BUILD_TYPE=Debug
$(CMAKE_BUILD) $(BUILD_DIR) --target webserv $(CMAKE_BUILD) $(BUILD_DIR) --target webserv
@echo "$(CURRENT_ENV)" > $(BUILD_DIR)/.build-env
asan: $(BUILD_DIR) asan: $(BUILD_DIR)
$(CMAKE) -B $(BUILD_DIR) $(CMAKE_FLAGS) -DCMAKE_BUILD_TYPE=ASAN $(CMAKE) -B $(BUILD_DIR) $(CMAKE_FLAGS) -DCMAKE_BUILD_TYPE=ASAN
$(CMAKE_BUILD) $(BUILD_DIR) --target webserv $(CMAKE_BUILD) $(BUILD_DIR) --target webserv
@echo "$(CURRENT_ENV)" > $(BUILD_DIR)/.build-env
run: run_release run: run_release

View File

@ -3,12 +3,8 @@
# Don't exit on first error - we want to continue checking all files # Don't exit on first error - we want to continue checking all files
# set -e # set -e
# Detect project root - try container path first, then current directory # Use current working directory as project root
if [ -d "/workspace" ]; then PROJECT_ROOT="$(pwd)"
PROJECT_ROOT="/workspace"
else
PROJECT_ROOT="$(pwd)"
fi
# Find the build directory - check multiple possible locations # Find the build directory - check multiple possible locations
BUILD_DIR="" BUILD_DIR=""
@ -67,12 +63,8 @@ if [ -z "$BUILD_DIR" ]; then
echo -e "${YELLOW}🔨 Running cmake to create build directory...${NC}" echo -e "${YELLOW}🔨 Running cmake to create build directory...${NC}"
cd "$PROJECT_ROOT" 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"
BUILD_DIR="$PROJECT_ROOT/build-container"
else
BUILD_DIR="$PROJECT_ROOT/build-local"
fi
mkdir -p "$BUILD_DIR" mkdir -p "$BUILD_DIR"
cd "$BUILD_DIR" cd "$BUILD_DIR"