|
| 1 | +#!/bin/env bash |
| 2 | +# Installs Sierra compiler binaries (starknet-sierra-compile, starknet-native-compile). |
| 3 | +# Versions are read from plain text files that are the single source of truth for both |
| 4 | +# Rust code and this script. |
| 5 | +# |
| 6 | +# Usage: |
| 7 | +# scripts/install_compiler_binaries.sh # Install both |
| 8 | +# scripts/install_compiler_binaries.sh --sierra # Sierra only |
| 9 | +# scripts/install_compiler_binaries.sh --native # Native only |
| 10 | +# scripts/install_compiler_binaries.sh --dest /usr/local/bin # Copy binaries to dest |
| 11 | +# |
| 12 | +# --dest copies each installed binary into the given directory (useful in Docker |
| 13 | +# so the final stage can COPY from a known path regardless of CARGO_HOME). |
| 14 | +# |
| 15 | +# Can be run standalone (requires LLVM 19 for native; run scripts/dependencies.sh first). |
| 16 | + |
| 17 | +set -e |
| 18 | + |
| 19 | +# Source common apt utilities. |
| 20 | +# Handle both cases: script in subdirectory or current directory. |
| 21 | +SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]:-$0}")" && pwd)" |
| 22 | +if [ -f "${SCRIPT_DIR}/apt_utils.sh" ]; then |
| 23 | + source "${SCRIPT_DIR}/apt_utils.sh" |
| 24 | +elif [ -f "./apt_utils.sh" ]; then |
| 25 | + source "./apt_utils.sh" |
| 26 | +else |
| 27 | + echo "Error: apt_utils.sh not found in ${SCRIPT_DIR} or current directory" >&2 |
| 28 | + exit 1 |
| 29 | +fi |
| 30 | + |
| 31 | +# Source version files. |
| 32 | +source "${SCRIPT_DIR}/compiler_versions.sh" |
| 33 | + |
| 34 | +# Parse arguments. |
| 35 | +INSTALL_SIERRA=false |
| 36 | +INSTALL_NATIVE=false |
| 37 | +DEST_DIR="" |
| 38 | +explicit_selection=false |
| 39 | +while [ $# -gt 0 ]; do |
| 40 | + case "$1" in |
| 41 | + --sierra) INSTALL_SIERRA=true; explicit_selection=true ;; |
| 42 | + --native) INSTALL_NATIVE=true; explicit_selection=true ;; |
| 43 | + --dest) DEST_DIR="$2"; shift ;; |
| 44 | + *) echo "Unknown argument: $1" >&2; exit 1 ;; |
| 45 | + esac |
| 46 | + shift |
| 47 | +done |
| 48 | +if ! $explicit_selection; then |
| 49 | + INSTALL_SIERRA=true |
| 50 | + INSTALL_NATIVE=true |
| 51 | +fi |
| 52 | + |
| 53 | +# LLVM/MLIR env vars are normally set by .cargo/config.toml, but cargo install runs |
| 54 | +# outside the workspace so they must be set explicitly. |
| 55 | +function export_llvm_env_vars() { |
| 56 | + local config_file="$REPO_ROOT/.cargo/config.toml" |
| 57 | + if [ ! -f "$config_file" ]; then |
| 58 | + return |
| 59 | + fi |
| 60 | + local line var_name var_value |
| 61 | + while IFS= read -r line; do |
| 62 | + var_name=$(echo "$line" | sed -n 's/^\([A-Z0-9_]*\) = .*/\1/p') |
| 63 | + var_value=$(echo "$line" | sed -n 's/^[A-Z0-9_]* = "\(.*\)"/\1/p') |
| 64 | + if [ -n "$var_name" ] && [ -n "$var_value" ]; then |
| 65 | + export "$var_name=$var_value" |
| 66 | + fi |
| 67 | + done < <(grep -E '^(LLVM_SYS|MLIR_SYS|TABLEGEN)' "$config_file") |
| 68 | +} |
| 69 | + |
| 70 | +# Install a compiler binary with multi-version support. |
| 71 | +# Installs as <binary_name>-<version> and symlinks <binary_name> to the active version. |
| 72 | +# Skips installation if the versioned binary already exists. |
| 73 | +# If DEST_DIR is set, copies the binary there. |
| 74 | +function install_compiler_if_needed() { |
| 75 | + local binary_name=$1 |
| 76 | + local version=$2 |
| 77 | + local versioned_name="${binary_name}-${version}" |
| 78 | + |
| 79 | + if command -v "$versioned_name" &>/dev/null; then |
| 80 | + log_step "install_build_tools" "$versioned_name already installed, skipping" |
| 81 | + else |
| 82 | + log_step "install_build_tools" "Installing $versioned_name..." |
| 83 | + cargo install "$binary_name" --version "$version" --force |
| 84 | + # Rename to versioned path so multiple versions can coexist. |
| 85 | + mv "$(which "$binary_name")" "$(dirname "$(which "$binary_name")")/$versioned_name" |
| 86 | + log_step "install_build_tools" "$versioned_name installed successfully" |
| 87 | + fi |
| 88 | + |
| 89 | + # Symlink the active version. |
| 90 | + local bin_dir |
| 91 | + bin_dir="$(dirname "$(which "$versioned_name")")" |
| 92 | + ln -sf "$bin_dir/$versioned_name" "$bin_dir/$binary_name" |
| 93 | + |
| 94 | + # Copy to destination directory if requested. |
| 95 | + if [ -n "$DEST_DIR" ]; then |
| 96 | + mkdir -p "$DEST_DIR" |
| 97 | + cp "$bin_dir/$binary_name" "$DEST_DIR/" |
| 98 | + fi |
| 99 | +} |
| 100 | + |
| 101 | +if $INSTALL_SIERRA; then |
| 102 | + install_compiler_if_needed "starknet-sierra-compile" "$SIERRA_COMPILE_VERSION" |
| 103 | +fi |
| 104 | + |
| 105 | +if $INSTALL_NATIVE; then |
| 106 | + # starknet-native-compile requires LLVM 19. If LLVM is not installed, print instructions |
| 107 | + # instead of failing with a cryptic tblgen build error. |
| 108 | + if command -v llvm-config-19 &>/dev/null; then |
| 109 | + export_llvm_env_vars |
| 110 | + install_compiler_if_needed "starknet-native-compile" "$NATIVE_COMPILE_VERSION" |
| 111 | + else |
| 112 | + log_step "install_build_tools" "Skipping starknet-native-compile (LLVM 19 not found)." |
| 113 | + log_step "install_build_tools" "To install it: run 'scripts/dependencies.sh' first, then re-run this script." |
| 114 | + fi |
| 115 | +fi |
0 commit comments