From db02f567abf9acb25759d9aeb2a1087f122f0c8d Mon Sep 17 00:00:00 2001 From: Dori Medini Date: Sat, 19 Apr 2025 16:37:13 +0300 Subject: [PATCH] feat(apollo_starknet_os_program): compile programs asynchronously --- Cargo.lock | 1 + crates/apollo_starknet_os_program/Cargo.toml | 1 + .../build/compile_program.rs | 31 ++++++++++++------- .../apollo_starknet_os_program/build/main.rs | 28 ++++++++--------- 4 files changed, 35 insertions(+), 26 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index e5e17e7e231..7a1d4d7fabf 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1943,6 +1943,7 @@ dependencies = [ "starknet-types-core", "starknet_api", "thiserror 1.0.69", + "tokio", ] [[package]] diff --git a/crates/apollo_starknet_os_program/Cargo.toml b/crates/apollo_starknet_os_program/Cargo.toml index 89f363497e7..efa738247c9 100644 --- a/crates/apollo_starknet_os_program/Cargo.toml +++ b/crates/apollo_starknet_os_program/Cargo.toml @@ -24,6 +24,7 @@ thiserror.workspace = true [build-dependencies] apollo_infra_utils.workspace = true serde_json.workspace = true +tokio = { workspace = true, features = ["macros", "rt-multi-thread"] } [dev-dependencies] apollo_infra_utils = { workspace = true, features = ["testing"] } diff --git a/crates/apollo_starknet_os_program/build/compile_program.rs b/crates/apollo_starknet_os_program/build/compile_program.rs index 987b7a8d3ac..a2fe501e46c 100644 --- a/crates/apollo_starknet_os_program/build/compile_program.rs +++ b/crates/apollo_starknet_os_program/build/compile_program.rs @@ -3,8 +3,16 @@ use std::path::PathBuf; use apollo_infra_utils::cairo0_compiler::{compile_cairo0_program, Cairo0CompilerError}; use apollo_infra_utils::compile_time_cargo_manifest_dir; -fn compile_program(path_to_main_file: PathBuf) -> Vec { - match compile_cairo0_program(path_to_main_file, cairo_root_path()) { +pub async fn compile_and_output_program( + out_dir: PathBuf, + path_to_main_file_from_cairo_root: &str, + program_name: &str, +) { + println!("cargo::warning=Compiling {program_name} program..."); + let bytes = match compile_cairo0_program( + cairo_root_path().join(path_to_main_file_from_cairo_root), + cairo_root_path(), + ) { Ok(bytes) => bytes, Err(Cairo0CompilerError::Cairo0CompilerVersion(error)) => { panic!( @@ -15,19 +23,18 @@ fn compile_program(path_to_main_file: PathBuf) -> Vec { ) } Err(other_error) => { - panic!("Failed to compile the program. Error:\n{other_error}.") + panic!("Failed to compile the {program_name} program. Error:\n{other_error}.") } - } + }; + println!( + "cargo::warning=Done compiling {program_name}. Writing compiled bytes to output directory." + ); + let bytes_path = out_dir.join(format!("{program_name}_bytes")); + std::fs::write(&bytes_path, &bytes).unwrap_or_else(|error| { + panic!("Failed to write the compiled {program_name} bytes to {bytes_path:?}: {error}.") + }); } fn cairo_root_path() -> PathBuf { PathBuf::from(compile_time_cargo_manifest_dir!()).join("src/cairo") } - -pub fn compile_starknet_os() -> Vec { - compile_program(cairo_root_path().join("starkware/starknet/core/os/os.cairo")) -} - -pub fn compile_starknet_aggregator() -> Vec { - compile_program(cairo_root_path().join("starkware/starknet/core/aggregator/main.cairo")) -} diff --git a/crates/apollo_starknet_os_program/build/main.rs b/crates/apollo_starknet_os_program/build/main.rs index bee35be5fed..ad352cc0aac 100644 --- a/crates/apollo_starknet_os_program/build/main.rs +++ b/crates/apollo_starknet_os_program/build/main.rs @@ -7,23 +7,23 @@ mod dump_source; /// Build script for the `apollo_starknet_os_program` crate. /// Recompiles the OS program if the source files change. /// Optionally, also exposes all source cairo files in a mapping from file path to contents. -fn main() { +#[tokio::main] +async fn main() { let out_dir = PathBuf::from(std::env::var("OUT_DIR").expect("OUT_DIR not set.")); #[cfg(feature = "dump_source_files")] dump_source::dump_source_files(&out_dir.join("cairo_files_map.json")); - println!("cargo::warning=Compiling Starknet OS program..."); - let starknet_os_bytes = compile_program::compile_starknet_os(); - println!("cargo::warning=Done. Writing compiled bytes to output directory."); - let starknet_os_bytes_path = out_dir.join("starknet_os_bytes"); - std::fs::write(&starknet_os_bytes_path, &starknet_os_bytes) - .expect("Failed to write the compiled OS bytes to the output directory."); - - println!("cargo::warning=Compiling Starknet aggregator program..."); - let starknet_aggregator_bytes = compile_program::compile_starknet_aggregator(); - println!("cargo::warning=Done. Writing compiled bytes to output directory."); - let starknet_aggregator_bytes_path = out_dir.join("starknet_aggregator_bytes"); - std::fs::write(&starknet_aggregator_bytes_path, &starknet_aggregator_bytes) - .expect("Failed to write the compiled aggregator bytes to the output directory."); + let mut task_set = tokio::task::JoinSet::new(); + task_set.spawn(compile_program::compile_and_output_program( + out_dir.clone(), + "starkware/starknet/core/os/os.cairo", + "starknet_os", + )); + task_set.spawn(compile_program::compile_and_output_program( + out_dir, + "starkware/starknet/core/aggregator/main.cairo", + "starknet_aggregator", + )); + task_set.join_all().await; }