Home /Modules/scripts/build-macos.sh

build-macos.sh

scripts/build-macos.sh · 192 line(s) · bash

#!/bin/bash

# =============================================================================
# build-macos.sh — Build SLeeLa for macOS; optional install when BIN_DIR is set
# =============================================================================
# 
# Builds the core binaries (sleela, nordshrift) and the XLT CPU-model compiler
# (xltc, from impl/xlt). When BIN_DIR is set, all three are installed there.
#
# Usage:
#   ./build-macos.sh              # Build only (no install)
#   CC=clang CXX=clang++ ./build-macos.sh
#   BIN_DIR="$HOME/bin" ./build-macos.sh
#
# Environment variables (optional):
#   CC              C compiler (default: clang)
#   CXX             C++ compiler (default: clang++)
#   BIN_DIR         Optional installation directory (unset means build only)
#   CFLAGS          Custom C compiler flags
#   CXXFLAGS        Custom C++ compiler flags
#
# Requirements:
#   - Xcode Command Line Tools (xcode-select --install)
#   - C11-compatible C compiler (clang)
#   - C++17-compatible C++ compiler
#   - make
#
# =============================================================================

set -e  # Exit on error

# Configuration
BIN_DIR="${BIN_DIR:-}"
SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
REPO_ROOT="$(cd "$SCRIPT_DIR/.." && pwd)"
IMPL_DIR="$REPO_ROOT/impl"
BUILD_DIR="$IMPL_DIR/build"
XLT_DIR="$IMPL_DIR/xlt"
XLT_BUILD_DIR="$XLT_DIR/build"

# Set default compilers if not already set (macOS typically uses clang)
export CC="${CC:-clang}"
export CXX="${CXX:-clang++}"

# The impl build is fail-closed on a trusted SHA-256 manifest. Default it to the
# repository manifest so this script works out of the box; an explicit
# SLEELA_SHA256_MANIFEST in the environment still overrides it.
export SLEELA_SHA256_MANIFEST="${SLEELA_SHA256_MANIFEST:-$REPO_ROOT/security/sha256-manifest.json}"

# Print configuration
echo "=========================================="
echo "SLeeLa macOS Build Configuration"
echo "=========================================="
echo "C Compiler:       $CC"
echo "C++ Compiler:     $CXX"
echo "Installation Dir: ${BIN_DIR:-<build only>}"
echo "Build Dir:        $BUILD_DIR"
echo "========================================"
echo ""

# Check for required tools
check_command() {
    if ! command -v "$1" &> /dev/null; then
        echo "ERROR: '$1' not found. Please install it and try again."
        if [ "$1" == "clang" ] || [ "$1" == "clang++" ]; then
            echo "  Install Xcode Command Line Tools: xcode-select --install"
        fi
        exit 1
    fi
}

check_command "$CC"
check_command "$CXX"
check_command "make"

# Check C compiler version
echo "Checking C compiler capabilities..."
if $CC --version | grep -q "clang"; then
    $CC --version | head -1
else
    echo "WARNING: Non-clang compiler detected on macOS."
fi

# Check C++ compiler version
echo "Checking C++ compiler capabilities..."
if $CXX --version | grep -q "clang"; then
    $CXX --version | head -1
else
    echo "WARNING: Non-clang++ compiler detected on macOS."
fi

echo ""

# Build
echo "Building Sleela and Nordshrift..."
cd "$IMPL_DIR"
make clean
make all

if [ ! -f "$BUILD_DIR/sleela" ] || [ ! -f "$BUILD_DIR/nordshrift" ]; then
    echo "ERROR: Build failed. Binaries not found."
    exit 1
fi

# Build the XLT (.xlt) CPU-model compiler. It is self-contained (own Makefile,
# no dependency on the Nordshrift build) and produces impl/xlt/build/xltc.
echo ""
echo "Building XLT compiler (xltc)..."
make -C "$XLT_DIR" clean
make -C "$XLT_DIR"

if [ ! -f "$XLT_BUILD_DIR/xltc" ]; then
    echo "ERROR: XLT build failed. 'xltc' not found in $XLT_BUILD_DIR."
    exit 1
fi
STAGE_DIR="$BUILD_DIR/SLeeLa"
STAGE_CONFIG_FILE="$STAGE_DIR/Config/sleela.conf"
if [ ! -f "$STAGE_CONFIG_FILE" ] || [ ! -d "$STAGE_DIR/Options" ]; then
    echo "ERROR: Build staging is incomplete: expected $STAGE_CONFIG_FILE and $STAGE_DIR/Options."
    exit 1
fi

echo ""
echo "Build completed successfully!"
echo ""

# Verify binaries
echo "=========================================="
echo "Generated Binaries"
echo "=========================================="
ls -lh "$BUILD_DIR/sleela" "$BUILD_DIR/nordshrift" "$XLT_BUILD_DIR/xltc"
echo ""

# Check if we have write permission to BIN_DIR
if [ -n "$BIN_DIR" ] && [ "$BIN_DIR" != "$BUILD_DIR" ]; then
    if [ ! -d "$BIN_DIR" ]; then
        echo "ERROR: Installation directory '$BIN_DIR' does not exist."
        echo "Create it first, or set BIN_DIR to an existing directory."
        exit 1
    fi
    
    if [ ! -w "$BIN_DIR" ]; then
        echo "ERROR: No write permission to '$BIN_DIR'."
        echo "Try running with sudo or set BIN_DIR to a writable directory:"
        echo "  BIN_DIR=\$HOME/bin ./build-macos.sh"
        exit 1
    fi

    echo "Installing binaries to $BIN_DIR..."
    cp "$BUILD_DIR/sleela" "$BIN_DIR/sleela"
    cp "$BUILD_DIR/nordshrift" "$BIN_DIR/nordshrift"
    cp "$XLT_BUILD_DIR/xltc" "$BIN_DIR/xltc"
    chmod +x "$BIN_DIR/sleela" "$BIN_DIR/nordshrift" "$BIN_DIR/xltc"
    
    echo "=========================================="
    echo "Installation Complete"
    echo "=========================================="
    echo "sleela:     $BIN_DIR/sleela"
    echo "nordshrift: $BIN_DIR/nordshrift"
    echo "xltc:       $BIN_DIR/xltc"
    echo ""
    echo "Verify installation:"
    echo "  $BIN_DIR/sleela version"
    echo "  $BIN_DIR/nordshrift version"
    echo "  $BIN_DIR/xltc $REPO_ROOT/lib/xlt/examples/epyc-ddr2.xlt"
else
    echo "=========================================="
    echo "Build Complete (no separate install)"
    echo "=========================================="
    echo "Binaries remain in: $BUILD_DIR (sleela, nordshrift)"
    echo "XLT compiler remains in: $XLT_BUILD_DIR (xltc)"
    echo "Staged runtime: $STAGE_DIR"
    echo "Consolidated config: $STAGE_CONFIG_FILE"
    echo ""
    echo "To use globally, copy them to a directory in \$PATH:"
    echo "  sudo cp $BUILD_DIR/sleela /usr/local/bin/"
    echo "  sudo cp $BUILD_DIR/nordshrift /usr/local/bin/"
    echo "  sudo cp $XLT_BUILD_DIR/xltc /usr/local/bin/"
    echo ""
    echo "Or set BIN_DIR and run again:"
    echo "  BIN_DIR=/usr/local/bin ./build-macos.sh"
fi

echo ""
echo ""
echo "Common configuration:"
echo "  Staged default configuration: $STAGE_CONFIG_FILE"
echo "  SLEELA_CONFIG_FILE may override the selected configuration path."
echo "  macOS target: configured for supported modern macOS releases."
echo ""
echo "Build process finished."