|
| 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 | +# The script does not put anything on $PATH; callers that need the binary on |
| 8 | +# $PATH should use --dest. |
| 9 | +# |
| 10 | +# Usage: |
| 11 | +# scripts/install_compiler_binaries.sh # Install both |
| 12 | +# scripts/install_compiler_binaries.sh --sierra # Sierra only |
| 13 | +# scripts/install_compiler_binaries.sh --native # Native only |
| 14 | +# scripts/install_compiler_binaries.sh --dest /path/to/dir # Stage at fixed path |
| 15 | +# scripts/install_compiler_binaries.sh --auto-install-llvm # If LLVM 19 missing, |
| 16 | +# # run install_llvm19.sh |
| 17 | +# |
| 18 | +# --dest stages a copy at a caller-specified fixed path. Use for artifact |
| 19 | +# pipelines that require a stable known location (e.g. binaries to be uploaded |
| 20 | +# as build artifacts with a fixed object key). |
| 21 | +# |
| 22 | +# --auto-install-llvm opts into running scripts/install_llvm19.sh (which uses |
| 23 | +# sudo apt) when LLVM 19 is missing. Default is to fail with a clear message; |
| 24 | +# this avoids surprising callers with a system-package install side-effect. |
| 25 | +# |
| 26 | +# Stdout: absolute path of each installed binary, one per line. |
| 27 | +# Stderr: progress logs (so stdout stays parseable). |
| 28 | + |
| 29 | +set -e |
| 30 | + |
| 31 | +SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]:-$0}")" && pwd)" |
| 32 | + |
| 33 | +# Source common apt utilities (for log_step). |
| 34 | +if [ -f "${SCRIPT_DIR}/apt_utils.sh" ]; then |
| 35 | + source "${SCRIPT_DIR}/apt_utils.sh" |
| 36 | +elif [ -f "./apt_utils.sh" ]; then |
| 37 | + source "./apt_utils.sh" |
| 38 | +else |
| 39 | + echo "Error: apt_utils.sh not found in ${SCRIPT_DIR} or current directory" >&2 |
| 40 | + exit 1 |
| 41 | +fi |
| 42 | + |
| 43 | +# Source compiler version variables (provides $REPO_ROOT, $SIERRA_COMPILE_VERSION, |
| 44 | +# $NATIVE_COMPILE_VERSION). The sourced script validates the version strings. |
| 45 | +source "${SCRIPT_DIR}/compiler_versions.sh" |
| 46 | + |
| 47 | +# Parse arguments. |
| 48 | +INSTALL_SIERRA=false |
| 49 | +INSTALL_NATIVE=false |
| 50 | +DEST_DIR="" |
| 51 | +AUTO_INSTALL_LLVM=false |
| 52 | +explicit_selection=false |
| 53 | +while [ $# -gt 0 ]; do |
| 54 | + case "$1" in |
| 55 | + --sierra) INSTALL_SIERRA=true; explicit_selection=true ;; |
| 56 | + --native) INSTALL_NATIVE=true; explicit_selection=true ;; |
| 57 | + --dest) DEST_DIR="$2"; shift ;; |
| 58 | + --auto-install-llvm) AUTO_INSTALL_LLVM=true ;; |
| 59 | + *) echo "Unknown argument: $1" >&2; exit 1 ;; |
| 60 | + esac |
| 61 | + shift |
| 62 | +done |
| 63 | +if ! $explicit_selection; then |
| 64 | + INSTALL_SIERRA=true |
| 65 | + INSTALL_NATIVE=true |
| 66 | +fi |
| 67 | + |
| 68 | +CARGO_TOOLS_ROOT="${CARGO_TOOLS_ROOT:-${CARGO_HOME:-$HOME/.cargo}/tools}" |
| 69 | + |
| 70 | +# LLVM/MLIR env vars are normally set by .cargo/config.toml, but `cargo install` |
| 71 | +# runs outside the workspace so they must be exported explicitly. We read the |
| 72 | +# three known LLVM-tooling variables from an explicit allow-list and validate |
| 73 | +# each value: cargo install runs untrusted build scripts, so any env var |
| 74 | +# leakage from this function would let a malicious .cargo/config.toml change |
| 75 | +# influence the install environment (e.g. LD_PRELOAD-adjacent attacks). |
| 76 | +readonly _LLVM_ENV_VARS=(LLVM_SYS_191_PREFIX MLIR_SYS_190_PREFIX TABLEGEN_190_PREFIX) |
| 77 | +readonly _LLVM_PATH_RE='^[A-Za-z0-9_/.:-]+$' |
| 78 | +function export_llvm_env_vars() { |
| 79 | + # If the caller already provided all three vars in the environment (e.g. |
| 80 | + # Dockerfiles use `ENV`), trust them and skip the config.toml lookup. |
| 81 | + # Cargo install will receive them via the normal inheritance. |
| 82 | + local name all_set=true |
| 83 | + for name in "${_LLVM_ENV_VARS[@]}"; do |
| 84 | + if [ -z "${!name+x}" ]; then |
| 85 | + all_set=false |
| 86 | + break |
| 87 | + fi |
| 88 | + done |
| 89 | + if $all_set; then |
| 90 | + return 0 |
| 91 | + fi |
| 92 | + |
| 93 | + # Otherwise read them from .cargo/config.toml. The fields are constrained |
| 94 | + # to a known allow-list (no greedy regex), and each value is validated |
| 95 | + # against a path-shape pattern: cargo install runs untrusted build |
| 96 | + # scripts, so leakage from this function would let a malicious config.toml |
| 97 | + # influence the install environment (e.g. LD_PRELOAD-adjacent attacks). |
| 98 | + local config_file="$REPO_ROOT/.cargo/config.toml" |
| 99 | + if [ ! -f "$config_file" ]; then |
| 100 | + echo "Error: missing LLVM env vars and ${config_file} not found." >&2 |
| 101 | + echo " Set ${_LLVM_ENV_VARS[*]} in the environment, or run from a workspace with .cargo/config.toml." >&2 |
| 102 | + exit 1 |
| 103 | + fi |
| 104 | + local value |
| 105 | + for name in "${_LLVM_ENV_VARS[@]}"; do |
| 106 | + value=$(sed -nE "s/^${name}[[:space:]]*=[[:space:]]*\"([^\"]*)\".*/\\1/p" "$config_file") |
| 107 | + if [ -z "$value" ]; then |
| 108 | + echo "Error: ${name} not found in ${config_file}" >&2 |
| 109 | + exit 1 |
| 110 | + fi |
| 111 | + if [[ ! "$value" =~ $_LLVM_PATH_RE ]]; then |
| 112 | + echo "Error: ${name} has unexpected value in ${config_file}: '${value}'" >&2 |
| 113 | + exit 1 |
| 114 | + fi |
| 115 | + export "$name=$value" |
| 116 | + done |
| 117 | +} |
| 118 | + |
| 119 | +# Atomic copy: write to a temp file in the destination directory then rename. |
| 120 | +# Avoids a parallel reader catching a half-written binary. |
| 121 | +function atomic_install() { |
| 122 | + local src=$1 dst=$2 |
| 123 | + mkdir -p "$(dirname "$dst")" |
| 124 | + local tmp="${dst}.tmp.$$" |
| 125 | + cp "$src" "$tmp" |
| 126 | + mv -f "$tmp" "$dst" |
| 127 | +} |
| 128 | + |
| 129 | +# Install <binary> at <version> into a per-version --root. |
| 130 | +# Reuses an existing install (caches across branch swaps); stages a copy at |
| 131 | +# caller-requested DEST_DIR if requested; prints the absolute installed path |
| 132 | +# on stdout. |
| 133 | +function install_compiler_if_needed() { |
| 134 | + local binary_name=$1 |
| 135 | + local version=$2 |
| 136 | + local install_root="${CARGO_TOOLS_ROOT}/${binary_name}-${version}" |
| 137 | + local versioned_binary="${install_root}/bin/${binary_name}" |
| 138 | + |
| 139 | + if [ -x "$versioned_binary" ]; then |
| 140 | + log_step "install_compiler_binaries" "${binary_name} ${version} already installed, skipping" >&2 |
| 141 | + else |
| 142 | + log_step "install_compiler_binaries" "Installing ${binary_name} ${version}..." >&2 |
| 143 | + cargo install --locked --root "$install_root" "$binary_name" --version "$version" >&2 |
| 144 | + log_step "install_compiler_binaries" "Installed ${binary_name} ${version}" >&2 |
| 145 | + fi |
| 146 | + |
| 147 | + # Optional caller-requested copy at a fixed path (artifact pipelines). |
| 148 | + if [ -n "$DEST_DIR" ]; then |
| 149 | + atomic_install "$versioned_binary" "$DEST_DIR/${binary_name}" |
| 150 | + fi |
| 151 | + |
| 152 | + echo "$versioned_binary" |
| 153 | +} |
| 154 | + |
| 155 | +if $INSTALL_SIERRA; then |
| 156 | + install_compiler_if_needed "starknet-sierra-compile" "$SIERRA_COMPILE_VERSION" |
| 157 | +fi |
| 158 | + |
| 159 | +if $INSTALL_NATIVE; then |
| 160 | + # starknet-native-compile requires LLVM 19. By default we fail with a clear |
| 161 | + # message rather than silently invoking sudo apt; pass --auto-install-llvm |
| 162 | + # to opt into the system-level install via install_llvm19.sh. |
| 163 | + if ! command -v llvm-config-19 &>/dev/null; then |
| 164 | + if $AUTO_INSTALL_LLVM; then |
| 165 | + log_step "install_compiler_binaries" "LLVM 19 not found; --auto-install-llvm set, running install_llvm19.sh..." >&2 |
| 166 | + "${SCRIPT_DIR}/install_llvm19.sh" >&2 |
| 167 | + else |
| 168 | + echo "Error: LLVM 19 not installed (llvm-config-19 not on PATH)." >&2 |
| 169 | + echo " Run scripts/install_llvm19.sh first, or pass --auto-install-llvm to run it now." >&2 |
| 170 | + exit 1 |
| 171 | + fi |
| 172 | + fi |
| 173 | + export_llvm_env_vars |
| 174 | + install_compiler_if_needed "starknet-native-compile" "$NATIVE_COMPILE_VERSION" |
| 175 | +fi |
0 commit comments