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
"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",
"unordered_map": "cpp",
"string_view": "cpp"
}
},
"cmake.buildDirectory": "${workspaceFolder}/build"
}

View File

@ -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

View File

@ -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