|
| 1 | +#!/bin/env bash |
| 2 | +# Installs Sierra compiler binaries (starknet-sierra-compile, starknet-native-compile). |
| 3 | +# Versions are read from plain text files (the single source of truth for both Rust and shell). |
| 4 | +# |
| 5 | +# Each version is installed under its own --root so multiple versions can coexist: |
| 6 | +# ${CARGO_TOOLS_ROOT:-$CARGO_HOME/tools}/<binary>-<version>/bin/<binary> |
| 7 | +# A copy is also placed in $CARGO_HOME/bin so the binary is discoverable on PATH. |
| 8 | +# |
| 9 | +# Usage: |
| 10 | +# scripts/install_compiler_binaries.sh # Install both |
| 11 | +# scripts/install_compiler_binaries.sh --sierra # Sierra only |
| 12 | +# scripts/install_compiler_binaries.sh --native # Native only |
| 13 | +# scripts/install_compiler_binaries.sh --dest /usr/local/bin # Also copy to dest |
| 14 | +# |
| 15 | +# --dest copies each installed binary into the given directory (useful in Docker |
| 16 | +# so the final stage can COPY from a known path regardless of CARGO_HOME). |
| 17 | +# |
| 18 | +# Stdout: absolute path of each installed binary, one per line. |
| 19 | +# Stderr: progress logs (so stdout stays parseable). |
| 20 | +# |
| 21 | +# Self-recovers if LLVM 19 is missing for the native compiler by invoking |
| 22 | +# scripts/install_llvm19.sh. |
| 23 | + |
| 24 | +set -e |
| 25 | + |
| 26 | +SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]:-$0}")" && pwd)" |
| 27 | + |
| 28 | +# Source common apt utilities (for log_step). |
| 29 | +if [ -f "${SCRIPT_DIR}/apt_utils.sh" ]; then |
| 30 | + source "${SCRIPT_DIR}/apt_utils.sh" |
| 31 | +elif [ -f "./apt_utils.sh" ]; then |
| 32 | + source "./apt_utils.sh" |
| 33 | +else |
| 34 | + echo "Error: apt_utils.sh not found in ${SCRIPT_DIR} or current directory" >&2 |
| 35 | + exit 1 |
| 36 | +fi |
| 37 | + |
| 38 | +# Source compiler version variables. |
| 39 | +source "${SCRIPT_DIR}/compiler_versions.sh" |
| 40 | + |
| 41 | +# Parse arguments. |
| 42 | +INSTALL_SIERRA=false |
| 43 | +INSTALL_NATIVE=false |
| 44 | +DEST_DIR="" |
| 45 | +explicit_selection=false |
| 46 | +while [ $# -gt 0 ]; do |
| 47 | + case "$1" in |
| 48 | + --sierra) INSTALL_SIERRA=true; explicit_selection=true ;; |
| 49 | + --native) INSTALL_NATIVE=true; explicit_selection=true ;; |
| 50 | + --dest) DEST_DIR="$2"; shift ;; |
| 51 | + *) echo "Unknown argument: $1" >&2; exit 1 ;; |
| 52 | + esac |
| 53 | + shift |
| 54 | +done |
| 55 | +if ! $explicit_selection; then |
| 56 | + INSTALL_SIERRA=true |
| 57 | + INSTALL_NATIVE=true |
| 58 | +fi |
| 59 | + |
| 60 | +CARGO_HOME_DIR="${CARGO_HOME:-$HOME/.cargo}" |
| 61 | +CARGO_TOOLS_ROOT="${CARGO_TOOLS_ROOT:-${CARGO_HOME_DIR}/tools}" |
| 62 | + |
| 63 | +# LLVM/MLIR env vars are normally set by .cargo/config.toml, but `cargo install` |
| 64 | +# runs outside the workspace so they must be exported explicitly. We parse them |
| 65 | +# out of .cargo/config.toml dynamically so adding/removing variables there |
| 66 | +# automatically propagates here. |
| 67 | +function export_llvm_env_vars() { |
| 68 | + local config_file="$REPO_ROOT/.cargo/config.toml" |
| 69 | + [ -f "$config_file" ] || return 0 |
| 70 | + local line var_name var_value |
| 71 | + while IFS= read -r line; do |
| 72 | + var_name=$(echo "$line" | sed -n 's/^\([A-Z0-9_]*\) = .*/\1/p') |
| 73 | + var_value=$(echo "$line" | sed -n 's/^[A-Z0-9_]* = "\(.*\)"/\1/p') |
| 74 | + if [ -n "$var_name" ] && [ -n "$var_value" ]; then |
| 75 | + export "$var_name=$var_value" |
| 76 | + fi |
| 77 | + done < <(grep -E '^(LLVM_SYS|MLIR_SYS|TABLEGEN)' "$config_file") |
| 78 | +} |
| 79 | + |
| 80 | +# Install <binary> at <version> into a per-version --root. |
| 81 | +# Reuses an existing install (caches across branch swaps); copies to PATH and |
| 82 | +# any caller-requested DEST_DIR; prints the absolute installed path on stdout. |
| 83 | +function install_compiler_if_needed() { |
| 84 | + local binary_name=$1 |
| 85 | + local version=$2 |
| 86 | + local install_root="${CARGO_TOOLS_ROOT}/${binary_name}-${version}" |
| 87 | + local versioned_binary="${install_root}/bin/${binary_name}" |
| 88 | + |
| 89 | + if [ -x "$versioned_binary" ]; then |
| 90 | + log_step "install_compiler_binaries" "${binary_name} ${version} already installed, skipping" >&2 |
| 91 | + else |
| 92 | + log_step "install_compiler_binaries" "Installing ${binary_name} ${version}..." >&2 |
| 93 | + cargo install --locked --root "$install_root" "$binary_name" --version "$version" >&2 |
| 94 | + log_step "install_compiler_binaries" "Installed ${binary_name} ${version}" >&2 |
| 95 | + fi |
| 96 | + |
| 97 | + # Place in $CARGO_HOME/bin so plain `Command::new("$binary_name")` finds it on PATH. |
| 98 | + mkdir -p "${CARGO_HOME_DIR}/bin" |
| 99 | + cp "$versioned_binary" "${CARGO_HOME_DIR}/bin/${binary_name}" |
| 100 | + |
| 101 | + # Optional caller-requested copy (e.g. for Docker COPY or workflow upload). |
| 102 | + if [ -n "$DEST_DIR" ]; then |
| 103 | + mkdir -p "$DEST_DIR" |
| 104 | + cp "$versioned_binary" "$DEST_DIR/${binary_name}" |
| 105 | + fi |
| 106 | + |
| 107 | + echo "$versioned_binary" |
| 108 | +} |
| 109 | + |
| 110 | +if $INSTALL_SIERRA; then |
| 111 | + install_compiler_if_needed "starknet-sierra-compile" "$SIERRA_COMPILE_VERSION" |
| 112 | +fi |
| 113 | + |
| 114 | +if $INSTALL_NATIVE; then |
| 115 | + # starknet-native-compile requires LLVM 19; auto-install if missing instead |
| 116 | + # of failing with a cryptic tblgen build error. |
| 117 | + if ! command -v llvm-config-19 &>/dev/null; then |
| 118 | + log_step "install_compiler_binaries" "LLVM 19 not found, running install_llvm19.sh..." >&2 |
| 119 | + "${SCRIPT_DIR}/install_llvm19.sh" >&2 |
| 120 | + fi |
| 121 | + export_llvm_env_vars |
| 122 | + install_compiler_if_needed "starknet-native-compile" "$NATIVE_COMPILE_VERSION" |
| 123 | +fi |
0 commit comments