4coder/build_new/scripts/build.sh

307 lines
8.5 KiB
Bash
Raw Permalink Normal View History

2025-07-06 19:44:54 +00:00
#!/bin/bash
# build.sh - Master build script for 4coder simplified build system
# Usage: ./build.sh [platform] [config] [arch]
# Examples:
# ./build.sh macos debug x64
# ./build.sh linux release x64
# ./build.sh win32 debug x64
set -e # Exit on error
# =============================================================================
# Configuration
# =============================================================================
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
BUILD_ROOT="$SCRIPT_DIR/../../build"
CONFIG_DIR="$SCRIPT_DIR/../config"
HELPERS_DIR="$SCRIPT_DIR/../helpers"
# Source configuration files
source "$CONFIG_DIR/build-config.sh"
# =============================================================================
# Utility Functions
# =============================================================================
# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color
print_success() {
echo -e "${GREEN}${NC} $1"
}
print_warning() {
echo -e "${YELLOW}${NC} $1"
}
print_error() {
echo -e "${RED}${NC} $1"
}
print_info() {
echo -e "${BLUE}${NC} $1"
}
print_step() {
echo -e "${BLUE}===${NC} $1 ${BLUE}===${NC}"
}
show_usage() {
echo "Usage: $0 [platform] [config] [arch]"
echo ""
echo "Parameters:"
echo " platform - Target platform (auto-detect if not specified)"
echo " Options: macos, linux, win32"
echo " config - Build configuration (default: debug)"
echo " Options: debug, release"
echo " arch - Target architecture (auto-detect if not specified)"
echo " Options: x64, x86, arm64"
echo ""
echo "Examples:"
echo " $0 # Auto-detect platform and arch, debug build"
echo " $0 macos debug x64 # Explicit macOS debug x64 build"
echo " $0 linux release x64 # Linux release build"
echo " $0 win32 debug x64 # Windows debug build"
echo ""
echo "Environment:"
echo " BUILD_VERBOSE=1 # Enable verbose compilation output"
echo " BUILD_CLEAN=1 # Clean before build"
}
# =============================================================================
# Parameter Processing
# =============================================================================
detect_platform_and_arch() {
local detected_platform detected_arch
if [[ -x "$HELPERS_DIR/detect-platform.sh" ]]; then
detected_platform=$("$HELPERS_DIR/detect-platform.sh" detect 2>/dev/null)
detected_arch=$("$HELPERS_DIR/detect-platform.sh" arch 2>/dev/null)
if [[ -n "$detected_platform" && -n "$detected_arch" ]]; then
# Don't print here, let the caller print after parsing
echo "$detected_platform $detected_arch"
else
print_error "Failed to detect platform or architecture" >&2
return 1
fi
else
print_error "Platform detection script not found" >&2
return 1
fi
}
validate_parameters() {
local platform=$1
local config=$2
local arch=$3
# Validate platform
case "$platform" in
"macos"|"linux"|"win32")
;;
*)
print_error "Invalid platform: $platform"
print_info "Valid platforms: macos, linux, win32"
return 1
;;
esac
# Validate config
case "$config" in
"debug"|"release")
;;
*)
print_error "Invalid config: $config"
print_info "Valid configs: debug, release"
return 1
;;
esac
# Validate arch
case "$arch" in
"x64"|"x86"|"arm64")
;;
*)
print_error "Invalid architecture: $arch"
print_info "Valid architectures: x64, x86, arm64"
return 1
;;
esac
return 0
}
# =============================================================================
# Build Environment Setup
# =============================================================================
setup_build_environment() {
local platform=$1
local config=$2
local arch=$3
print_step "Setting up build environment"
# Create build directories
mkdir -p "$BUILD_ROOT"
mkdir -p "$BUILD_ROOT/temp"
# Export build variables
export BUILD_PLATFORM="$platform"
export BUILD_CONFIG="$config"
export BUILD_ARCH="$arch"
export BUILD_ROOT="$BUILD_ROOT"
export CODE_DIR="$CODE_DIR"
export CUSTOM_DIR="$CUSTOM_DIR"
export FOREIGN_DIR="$FOREIGN_DIR"
print_success "Build environment ready"
print_info "Platform: $platform"
print_info "Config: $config"
print_info "Architecture: $arch"
print_info "Build root: $BUILD_ROOT"
}
# =============================================================================
# Build Execution
# =============================================================================
execute_platform_build() {
local platform=$1
local config=$2
local arch=$3
local platform_script="$SCRIPT_DIR/build-$platform.sh"
if [[ -x "$platform_script" ]]; then
print_step "Executing platform-specific build"
print_info "Running: $platform_script $config $arch"
# Execute platform-specific build script
"$platform_script" "$config" "$arch"
return $?
else
print_error "Platform-specific build script not found: $platform_script"
return 1
fi
}
# =============================================================================
# Clean Build Support
# =============================================================================
clean_build() {
print_step "Cleaning build directory"
if [[ -d "$BUILD_ROOT" ]]; then
print_info "Removing $BUILD_ROOT"
rm -rf "$BUILD_ROOT"
fi
print_success "Build directory cleaned"
}
# =============================================================================
# Main Build Process
# =============================================================================
main() {
local platform="${1:-}"
local config="${2:-debug}"
local arch="${3:-}"
# Handle help request
if [[ "$1" == "help" || "$1" == "-h" || "$1" == "--help" ]]; then
show_usage
return 0
fi
# Handle clean request
if [[ "$1" == "clean" ]]; then
clean_build
return 0
fi
print_step "4coder Simplified Build System"
# Auto-detect platform and arch if not provided
if [[ -z "$platform" || -z "$arch" ]]; then
local detected_info detected_platform detected_arch
detected_info=$(detect_platform_and_arch)
if [[ $? -ne 0 ]]; then
print_error "Failed to auto-detect platform and architecture"
return 1
fi
read -r detected_platform detected_arch <<< "$detected_info"
platform="${platform:-$detected_platform}"
arch="${arch:-$detected_arch}"
print_info "Auto-detected platform: $platform"
print_info "Auto-detected architecture: $arch"
fi
# Validate parameters
if ! validate_parameters "$platform" "$config" "$arch"; then
echo ""
show_usage
return 1
fi
# Clean if requested
if [[ "${BUILD_CLEAN:-}" == "1" ]]; then
clean_build
fi
# Setup build environment
if ! setup_build_environment "$platform" "$config" "$arch"; then
return 1
fi
# Record build start time
local build_start_time
build_start_time=$(date +%s)
# Execute platform-specific build
print_step "Starting build process"
if execute_platform_build "$platform" "$config" "$arch"; then
local build_end_time build_duration
build_end_time=$(date +%s)
build_duration=$((build_end_time - build_start_time))
print_step "Build completed successfully!"
print_success "Build time: ${build_duration}s"
print_info "Outputs in: $BUILD_ROOT"
# List built artifacts
print_info "Built artifacts:"
if [[ -f "$BUILD_ROOT/4ed" ]]; then
print_success " 4ed (main executable)"
fi
if [[ -f "$BUILD_ROOT/4ed_app.so" ]]; then
print_success " 4ed_app.so (core engine)"
fi
if [[ -f "$BUILD_ROOT/custom_4coder.so" ]]; then
print_success " custom_4coder.so (custom layer)"
fi
return 0
else
print_error "Build failed"
return 1
fi
}
# Only run main if script is executed directly (not sourced)
if [[ "${BASH_SOURCE[0]}" == "${0}" ]]; then
main "$@"
fi