#!/bin/bash # Colors for output GREEN='\033[0;32m' RED='\033[0;31m' YELLOW='\033[1;33m' NC='\033[0m' # No Color echo -e "${GREEN}=== Sun Language Installer ===${NC}" # Function to check command existence check_cmd() { command -v "$1" &> /dev/null } # 1. Check for build tools and install if missing (Ubuntu/Debian only) echo "Checking dependencies..." if ! check_cmd make || ! check_cmd clang++; then if [ -f /etc/debian_version ]; then echo -e "${YELLOW}Dependencies missing. Attempting to install...${NC}" echo "Running: sudo apt update && sudo apt install -y build-essential llvm clang" sudo apt update && sudo apt install -y build-essential llvm clang else if ! check_cmd make; then echo -e "${RED}Error: 'make' is not installed.${NC}"; exit 1; fi if ! check_cmd clang++; then echo -e "${RED}Error: 'clang++' is not installed.${NC}"; exit 1; fi fi fi # 2. Detect LLVM echo "Detecting LLVM..." LLVM_CONFIG="" # Try standard llvm-config if command -v llvm-config &> /dev/null; then LLVM_CONFIG="llvm-config" fi # Try macOS Homebrew path if [ -z "$LLVM_CONFIG" ] && [ -f "/opt/homebrew/opt/llvm/bin/llvm-config" ]; then LLVM_CONFIG="/opt/homebrew/opt/llvm/bin/llvm-config" fi # Try versioned names (common on Linux) if [ -z "$LLVM_CONFIG" ]; then for ver in 21 20 19 18 17 16 15; do if command -v "llvm-config-$ver" &> /dev/null; then LLVM_CONFIG="llvm-config-$ver" break fi done fi if [ -z "$LLVM_CONFIG" ]; then echo -e "${RED}Error: LLVM not found.${NC}" if [ -f /etc/debian_version ]; then echo -e "${YELLOW}Attempting to install LLVM...${NC}" sudo apt install -y llvm # Try to find it again if command -v llvm-config &> /dev/null; then LLVM_CONFIG="llvm-config" else # Try versioned names again for ver in 21 20 19 18 17 16 15; do if command -v "llvm-config-$ver" &> /dev/null; then LLVM_CONFIG="llvm-config-$ver" break fi done fi fi fi if [ -z "$LLVM_CONFIG" ]; then echo -e "${RED}Error: LLVM could not be found or installed.${NC}" echo "Please install LLVM manually:" echo " - macOS: brew install llvm@21" echo " - Ubuntu: sudo apt install llvm clang" exit 1 fi echo "Using LLVM config: $LLVM_CONFIG" # 3. Build echo "Building Sun..." # Pass LLVM_CONFIG to make if ! make LLVM_CONFIG="$LLVM_CONFIG"; then echo -e "${RED}Build failed.${NC}" exit 1 fi # 4. Install INSTALL_DIR="/usr/local/bin" # Check if /usr/local/bin exists, if not create it if [ ! -d "$INSTALL_DIR" ]; then echo "Creating $INSTALL_DIR..." sudo mkdir -p "$INSTALL_DIR" fi echo "Installing 'sun' binary to $INSTALL_DIR..." if [ -w "$INSTALL_DIR" ]; then cp sun "$INSTALL_DIR/sun" else echo -e "${YELLOW}Permission denied. Trying with sudo...${NC}" if sudo cp sun "$INSTALL_DIR/sun"; then echo "Copied successfully with sudo." else echo -e "${RED}Failed to install. Please run with sudo or check permissions.${NC}" exit 1 fi fi echo -e "${GREEN}Success! Sun has been installed.${NC}" echo "You can now run 'sun' from anywhere."