Skip to content

Commit 997e418

Browse files
avi-starkwareclaude
andcommitted
apollo_compile_to_casm,apollo_compile_to_native: switch to script-based compiler installation
Remove build.rs from both compiler crates — compiler binaries are now installed by scripts/install_compiler_binaries.sh (called from install_cargo_tools.sh) instead of as a cargo build side effect. - Delete build.rs and [build-dependencies] from both crates - Compiler constructors use verify_compiler_binary + PATH-based lookup (skip version check for custom paths in SierraToNativeCompiler) - install_cargo_tools.sh calls install_compiler_binaries.sh - Dockerfiles use cargo install with versions from .txt files - Base Dockerfile copies new scripts and version files Old install_compiler_binary() and legacy path functions are left for removal in a follow-up cleanup. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent 3310da5 commit 997e418

14 files changed

Lines changed: 110 additions & 110 deletions

File tree

crates/apollo_compilation_utils/src/build_utils.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,15 @@ use std::process::Command;
33

44
use tempfile::TempDir;
55

6-
use crate::paths::{binary_path, shared_folder_dir};
6+
use crate::paths::{legacy_binary_path, shared_folder_dir};
77

88
pub fn install_compiler_binary(
99
binary_name: &str,
1010
required_version: &str,
1111
cargo_install_args: &[&str],
1212
out_dir: &std::path::Path,
1313
) {
14-
let binary_path = binary_path(out_dir, binary_name);
14+
let binary_path = legacy_binary_path(out_dir, binary_name);
1515
match Command::new(&binary_path).args(["--version"]).output() {
1616
Ok(binary_version) => {
1717
let binary_version = String::from_utf8(binary_version.stdout)

crates/apollo_compilation_utils/src/paths.rs

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
// It must not contain functionality that is available in only in one of these modes. Specifically,
33
// it must avoid relying on env variables such as 'CARGO_*' or 'OUT_DIR'.
44

5+
use std::path::PathBuf;
6+
57
fn target_dir(out_dir: &std::path::Path) -> std::path::PathBuf {
68
out_dir
79
.ancestors()
@@ -14,6 +16,31 @@ pub fn shared_folder_dir(out_dir: &std::path::Path) -> std::path::PathBuf {
1416
target_dir(out_dir).join("shared_executables")
1517
}
1618

17-
pub fn binary_path(out_dir: &std::path::Path, binary_name: &str) -> std::path::PathBuf {
19+
/// Returns `<cargo_tools_root>/<binary>-<version>/bin/<binary>`, where
20+
/// `cargo_tools_root` resolves to `$CARGO_TOOLS_ROOT`, else `$CARGO_HOME/tools`,
21+
/// else `$HOME/.cargo/tools`.
22+
///
23+
/// The path is constructed deterministically; there is no `$PATH` lookup. The
24+
/// version is part of the path, so a version mismatch surfaces as "file not
25+
/// found" at startup rather than as a silent run against the wrong binary.
26+
pub fn binary_path(binary_name: &str, required_version: &str) -> PathBuf {
27+
cargo_tools_root()
28+
.join(format!("{binary_name}-{required_version}"))
29+
.join("bin")
30+
.join(binary_name)
31+
}
32+
33+
fn cargo_tools_root() -> PathBuf {
34+
if let Ok(p) = std::env::var("CARGO_TOOLS_ROOT") {
35+
return PathBuf::from(p);
36+
}
37+
let cargo_home = std::env::var("CARGO_HOME").map(PathBuf::from).unwrap_or_else(|_| {
38+
PathBuf::from(std::env::var("HOME").expect("HOME must be set")).join(".cargo")
39+
});
40+
cargo_home.join("tools")
41+
}
42+
43+
// TODO(Avi): Remove once build.rs callers are gone.
44+
pub fn legacy_binary_path(out_dir: &std::path::Path, binary_name: &str) -> std::path::PathBuf {
1845
shared_folder_dir(out_dir).join(binary_name)
1946
}

crates/apollo_compile_to_casm/Cargo.toml

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ workspace = true
1616
apollo_compilation_utils.workspace = true
1717
apollo_compile_to_casm_types.workspace = true
1818
apollo_infra.workspace = true
19+
apollo_infra_utils.workspace = true
1920
apollo_metrics.workspace = true
2021
apollo_proc_macros.workspace = true
2122
apollo_sierra_compilation_config.workspace = true
@@ -29,12 +30,7 @@ tracing.workspace = true
2930

3031
[dev-dependencies]
3132
apollo_compilation_utils = { workspace = true, features = ["testing"] }
32-
apollo_infra_utils.workspace = true
3333
assert_matches.workspace = true
3434
expect-test.workspace = true
3535
mempool_test_utils.workspace = true
3636
regex.workspace = true
37-
38-
[build-dependencies]
39-
apollo_compilation_utils.workspace = true
40-
apollo_infra_utils.workspace = true

crates/apollo_compile_to_casm/build.rs

Lines changed: 0 additions & 36 deletions
This file was deleted.

crates/apollo_compile_to_casm/src/compiler.rs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
use std::path::PathBuf;
22

3+
use apollo_compilation_utils::build_utils::verify_compiler_binary;
34
use apollo_compilation_utils::compiler_utils::compile_with_args;
45
use apollo_compilation_utils::errors::CompilationUtilError;
56
use apollo_compilation_utils::paths::binary_path;
67
use apollo_compilation_utils::resource_limits::ResourceLimits;
8+
use apollo_infra_utils::cairo_compiler_version::CAIRO1_COMPILER_VERSION;
79
use apollo_sierra_compilation_config::config::SierraCompilationConfig;
810
use cairo_lang_starknet_classes::casm_contract_class::CasmContractClass;
911
use cairo_lang_starknet_classes::contract_class::ContractClass;
@@ -19,7 +21,8 @@ pub struct SierraToCasmCompiler {
1921

2022
impl SierraToCasmCompiler {
2123
pub fn new(config: SierraCompilationConfig) -> Self {
22-
let path_to_binary = binary_path(&out_dir(), CAIRO_LANG_BINARY_NAME);
24+
let path_to_binary = binary_path(CAIRO_LANG_BINARY_NAME, CAIRO1_COMPILER_VERSION);
25+
verify_compiler_binary(&path_to_binary, CAIRO1_COMPILER_VERSION);
2326
info!("Using Sierra compiler binary at: {:?}", path_to_binary);
2427
Self { config, path_to_binary }
2528
}
@@ -51,8 +54,3 @@ impl SierraToCasmCompiler {
5154
Ok(serde_json::from_slice::<CasmContractClass>(&stdout)?)
5255
}
5356
}
54-
55-
// Returns the OUT_DIR. This function is only operable at run time.
56-
fn out_dir() -> PathBuf {
57-
env!("RUNTIME_ACCESSIBLE_OUT_DIR").into()
58-
}

crates/apollo_compile_to_native/Cargo.toml

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,3 @@ assert_matches.workspace = true
2323
mempool_test_utils.workspace = true
2424
rstest.workspace = true
2525
toml_test_utils.workspace = true
26-
27-
[build-dependencies]
28-
apollo_compilation_utils.workspace = true
29-
apollo_infra_utils.workspace = true

crates/apollo_compile_to_native/build.rs

Lines changed: 0 additions & 35 deletions
This file was deleted.

crates/apollo_compile_to_native/src/compiler.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
use std::path::{Path, PathBuf};
1+
use std::path::PathBuf;
22

3+
use apollo_compilation_utils::build_utils::verify_compiler_binary;
34
use apollo_compilation_utils::compiler_utils::compile_with_args;
45
use apollo_compilation_utils::errors::CompilationUtilError;
56
use apollo_compilation_utils::paths::binary_path;
@@ -9,7 +10,7 @@ use cairo_lang_starknet_classes::contract_class::ContractClass;
910
use cairo_native::executor::AotContractExecutor;
1011
use tempfile::NamedTempFile;
1112

12-
use crate::constants::CAIRO_NATIVE_BINARY_NAME;
13+
use crate::constants::{CAIRO_NATIVE_BINARY_NAME, REQUIRED_CAIRO_NATIVE_VERSION};
1314

1415
#[derive(Clone)]
1516
pub struct SierraToNativeCompiler {
@@ -21,7 +22,11 @@ impl SierraToNativeCompiler {
2122
pub fn new(config: SierraCompilationConfig) -> Self {
2223
let path_to_binary = match &config.compiler_binary_path {
2324
Some(path) => path.clone(),
24-
None => binary_path(&out_dir(), CAIRO_NATIVE_BINARY_NAME),
25+
None => {
26+
let path = binary_path(CAIRO_NATIVE_BINARY_NAME, REQUIRED_CAIRO_NATIVE_VERSION);
27+
verify_compiler_binary(&path, REQUIRED_CAIRO_NATIVE_VERSION);
28+
path
29+
}
2530
};
2631
Self { config, path_to_binary }
2732
}
@@ -50,13 +55,8 @@ impl SierraToNativeCompiler {
5055
resource_limits,
5156
)?;
5257

53-
Ok(AotContractExecutor::from_path(Path::new(&output_file_path))
58+
Ok(AotContractExecutor::from_path(output_file.path())
5459
.map_err(|e| CompilationUtilError::CompilationError(e.to_string()))?
5560
.unwrap())
5661
}
5762
}
58-
59-
// Returns the OUT_DIR. This function is only operable at run time.
60-
fn out_dir() -> PathBuf {
61-
env!("RUNTIME_ACCESSIBLE_OUT_DIR").into()
62-
}

crates/blockifier_reexecution/replay/Dockerfile

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,12 @@ RUN cargo chef cook --release -p blockifier_reexecution --features cairo_native
8282
COPY . .
8383
RUN cargo build --release -p blockifier_reexecution --features cairo_native
8484

85+
# Install compiler binaries used for Sierra compilation at runtime.
86+
# Binaries land under $CARGO_TOOLS_ROOT/<binary>-<version>/bin/<binary>; the
87+
# final stage below COPYs the entire $CARGO_TOOLS_ROOT tree so this layout is
88+
# preserved at runtime.
89+
RUN scripts/install_compiler_binaries.sh
90+
8591
# =============================================================================
8692
# Stage 4: Final runtime image
8793
# =============================================================================
@@ -100,8 +106,10 @@ WORKDIR /app
100106

101107
# Copy the binary and both compilers (Sierra-to-CASM and Sierra-to-Native).
102108
COPY --from=builder /app/target/release/blockifier_reexecution ./target/release/blockifier_reexecution
103-
COPY --from=builder /app/target/release/shared_executables/starknet-sierra-compile ./target/release/shared_executables/starknet-sierra-compile
104-
COPY --from=builder /app/target/release/shared_executables/starknet-native-compile ./target/release/shared_executables/starknet-native-compile
109+
# Per-version compiler binaries; their absolute path is constructed from
110+
# $CARGO_TOOLS_ROOT at runtime.
111+
ENV CARGO_TOOLS_ROOT=/opt/cargo-tools
112+
COPY --from=builder /var/tmp/rust/tools ${CARGO_TOOLS_ROOT}
105113
COPY --from=builder /usr/bin/tini /usr/bin/tini
106114

107115
RUN set -ex; \

crates/starknet_transaction_prover/Dockerfile

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,12 @@ RUN BUILD_FLAGS=$([ "$BUILD_MODE" = "release" ] && echo "--release" || true); \
136136
cargo build $BUILD_FLAGS -p starknet_transaction_prover --features stwo_proving; \
137137
fi
138138

139+
# Install compiler binary used for Sierra to CASM compilation at runtime.
140+
# Binary lands under $CARGO_TOOLS_ROOT/<binary>-<version>/bin/<binary>; the
141+
# final stage below COPYs the entire $CARGO_TOOLS_ROOT tree so this layout is
142+
# preserved at runtime.
143+
RUN scripts/install_compiler_binaries.sh --sierra
144+
139145
# =============================================================================
140146
# Stage 4: Final runtime image
141147
# =============================================================================
@@ -158,8 +164,10 @@ COPY --from=builder /app/target/${BUILD_MODE}/starknet_transaction_prover ./targ
158164
# Copy resources needed by the service.
159165
COPY --from=builder /app/crates/starknet_transaction_prover/resources ./crates/starknet_transaction_prover/resources
160166

161-
# Copy starknet-sierra-compile binary required for Sierra to Casm compilation.
162-
COPY --from=builder /app/target/${BUILD_MODE}/shared_executables/starknet-sierra-compile ./target/${BUILD_MODE}/shared_executables/starknet-sierra-compile
167+
# Per-version Sierra-to-CASM compiler binary; its absolute path is constructed
168+
# from $CARGO_TOOLS_ROOT at runtime.
169+
ENV CARGO_TOOLS_ROOT=/opt/cargo-tools
170+
COPY --from=builder /var/tmp/rust/tools ${CARGO_TOOLS_ROOT}
163171

164172
# Copy tini for proper init handling.
165173
COPY --from=builder /usr/bin/tini /usr/bin/tini

0 commit comments

Comments
 (0)