Skip to content

Commit 259439f

Browse files
avi-starkwareclaude
andcommitted
scripts: add install_compiler_binaries.sh
New script to install Sierra compiler binaries (starknet-sierra-compile, starknet-native-compile). Reads versions from .txt files, handles LLVM env vars for native compile, and skips gracefully when LLVM 19 is missing. Includes compiler_versions.sh helper that reads version .txt files. Not yet called from install_cargo_tools.sh — wired up in a follow-up. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent a80bb64 commit 259439f

4 files changed

Lines changed: 195 additions & 34 deletions

File tree

scripts/compiler_versions.sh

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#!/bin/env bash
2+
# Reads compiler binary versions from version files (the single source of truth).
3+
# Source this script to get $SIERRA_COMPILE_VERSION and $NATIVE_COMPILE_VERSION.
4+
5+
COMPILER_VERSIONS_SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]:-$0}")" && pwd)"
6+
REPO_ROOT="$(cd "${COMPILER_VERSIONS_SCRIPT_DIR}/.." && pwd)"
7+
8+
SIERRA_COMPILE_VERSION=$(cat "$REPO_ROOT/crates/apollo_infra_utils/src/cairo_compiler_version.txt")
9+
NATIVE_COMPILE_VERSION=$(cat "$REPO_ROOT/crates/apollo_compile_to_native/src/native_compiler_version.txt")

scripts/dependencies.sh

Lines changed: 1 addition & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -35,43 +35,10 @@ function install_essential_deps_linux() {
3535
log_step "dependencies" "Essential Linux dependencies installed successfully"
3636
}
3737

38-
function setup_llvm_deps() {
39-
log_step "dependencies" "Setting up LLVM 19 dependencies..."
40-
case "$(uname)" in
41-
Darwin)
42-
echo "Detected macOS, using Homebrew..."
43-
brew update
44-
brew install llvm@19
45-
;;
46-
Linux)
47-
echo "Detected Linux, using apt..."
48-
$SUDO bash -c "$(declare -f apt_update_with_retry); $(declare -f apt_install_with_retry)"'
49-
echo "Downloading LLVM installation script..."
50-
curl https://apt.llvm.org/llvm.sh -Lo llvm.sh
51-
echo "Running LLVM 19 installation script..."
52-
bash ./llvm.sh 19 all
53-
rm -f ./llvm.sh
54-
echo "Installing LLVM-related packages (MLIR, Polly, etc.)..."
55-
apt_update_with_retry && apt_install_with_retry -y \
56-
libgmp3-dev \
57-
libmlir-19-dev \
58-
libpolly-19-dev \
59-
libzstd-dev \
60-
mlir-19-tools
61-
'
62-
;;
63-
*)
64-
echo "Error: Unsupported operating system"
65-
exit 1
66-
;;
67-
esac
68-
log_step "dependencies" "LLVM 19 dependencies setup completed"
69-
}
70-
7138
function main() {
7239
log_step "dependencies" "Starting dependencies installation..."
7340
[ "$(uname)" = "Linux" ] && install_essential_deps_linux
74-
setup_llvm_deps
41+
"${SCRIPT_DIR}/install_llvm19.sh"
7542
log_step "dependencies" "All dependencies installed successfully!"
7643
}
7744

Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
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

scripts/install_llvm19.sh

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
#!/bin/env bash
2+
# Installs LLVM 19 + MLIR/Polly dev packages required by starknet-native-compile.
3+
# Idempotent: skips if llvm-config-19 is already on PATH.
4+
#
5+
# Sourced by both scripts/dependencies.sh and scripts/install_compiler_binaries.sh
6+
# (the latter as a self-recovery fallback when LLVM 19 is missing).
7+
8+
set -e
9+
10+
[[ ${UID} == "0" ]] || SUDO="sudo"
11+
12+
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]:-$0}")" && pwd)"
13+
if [ -f "${SCRIPT_DIR}/apt_utils.sh" ]; then
14+
source "${SCRIPT_DIR}/apt_utils.sh"
15+
elif [ -f "./apt_utils.sh" ]; then
16+
source "./apt_utils.sh"
17+
else
18+
echo "Error: apt_utils.sh not found in ${SCRIPT_DIR} or current directory" >&2
19+
exit 1
20+
fi
21+
22+
function install_llvm19() {
23+
if command -v llvm-config-19 &>/dev/null; then
24+
log_step "install_llvm19" "LLVM 19 already installed, skipping"
25+
return 0
26+
fi
27+
log_step "install_llvm19" "Setting up LLVM 19 dependencies..."
28+
case "$(uname)" in
29+
Darwin)
30+
echo "Detected macOS, using Homebrew..."
31+
brew update
32+
brew install llvm@19
33+
;;
34+
Linux)
35+
echo "Detected Linux, using apt..."
36+
$SUDO bash -c "$(declare -f apt_update_with_retry); $(declare -f apt_install_with_retry)"'
37+
echo "Downloading LLVM installation script..."
38+
curl https://apt.llvm.org/llvm.sh -Lo llvm.sh
39+
echo "Running LLVM 19 installation script..."
40+
bash ./llvm.sh 19 all
41+
rm -f ./llvm.sh
42+
echo "Installing LLVM-related packages (MLIR, Polly, etc.)..."
43+
apt_update_with_retry && apt_install_with_retry -y \
44+
libgmp3-dev \
45+
libmlir-19-dev \
46+
libpolly-19-dev \
47+
libzstd-dev \
48+
mlir-19-tools
49+
'
50+
;;
51+
*)
52+
echo "Error: Unsupported operating system" >&2
53+
exit 1
54+
;;
55+
esac
56+
log_step "install_llvm19" "LLVM 19 installed successfully"
57+
}
58+
59+
# Run when invoked directly; allow sourcing without auto-running.
60+
if [ "${BASH_SOURCE[0]}" = "$0" ]; then
61+
install_llvm19
62+
fi

0 commit comments

Comments
 (0)