Skip to content

Commit 87dbe9e

Browse files
feat(apollo_starknet_os_program): compile programs asynchronously
1 parent b317326 commit 87dbe9e

File tree

3 files changed

+35
-26
lines changed

3 files changed

+35
-26
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/apollo_starknet_os_program/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,4 @@ thiserror.workspace = true
2020
[build-dependencies]
2121
apollo_infra_utils.workspace = true
2222
cairo-vm.workspace = true
23+
tokio = { workspace = true, features = ["macros", "rt-multi-thread"] }

crates/apollo_starknet_os_program/build.rs

Lines changed: 33 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -5,26 +5,34 @@ use apollo_infra_utils::compile_time_cargo_manifest_dir;
55

66
/// Build script for the `apollo_starknet_os_program` crate.
77
/// Recompiles the OS program if the source files change.
8-
fn main() {
8+
#[tokio::main]
9+
async fn main() {
910
let out_dir = PathBuf::from(std::env::var("OUT_DIR").expect("OUT_DIR not set."));
1011

11-
println!("cargo::warning=Compiling Starknet OS program...");
12-
let starknet_os_bytes = compile_starknet_os();
13-
println!("cargo::warning=Done. Writing compiled bytes to output directory.");
14-
let starknet_os_bytes_path = out_dir.join("starknet_os_bytes");
15-
std::fs::write(&starknet_os_bytes_path, &starknet_os_bytes)
16-
.expect("Failed to write the compiled OS bytes to the output directory.");
17-
18-
println!("cargo::warning=Compiling Starknet aggregator program...");
19-
let starknet_aggregator_bytes = compile_starknet_aggregator();
20-
println!("cargo::warning=Done. Writing compiled bytes to output directory.");
21-
let starknet_aggregator_bytes_path = out_dir.join("starknet_aggregator_bytes");
22-
std::fs::write(&starknet_aggregator_bytes_path, &starknet_aggregator_bytes)
23-
.expect("Failed to write the compiled aggregator bytes to the output directory.");
12+
let mut task_set = tokio::task::JoinSet::new();
13+
task_set.spawn(compile_and_output_program(
14+
out_dir.clone(),
15+
"starkware/starknet/core/os/os.cairo",
16+
"starknet_os",
17+
));
18+
task_set.spawn(compile_and_output_program(
19+
out_dir,
20+
"starkware/starknet/core/aggregator/main.cairo",
21+
"starknet_aggregator",
22+
));
23+
task_set.join_all().await;
2424
}
2525

26-
fn compile_program(path_to_main_file: PathBuf) -> Vec<u8> {
27-
match compile_cairo0_program(path_to_main_file, cairo_root_path()) {
26+
async fn compile_and_output_program(
27+
out_dir: PathBuf,
28+
path_to_main_file_from_cairo_root: &str,
29+
program_name: &str,
30+
) {
31+
println!("cargo::warning=Compiling {program_name} program...");
32+
let bytes = match compile_cairo0_program(
33+
cairo_root_path().join(path_to_main_file_from_cairo_root),
34+
cairo_root_path(),
35+
) {
2836
Ok(bytes) => bytes,
2937
Err(Cairo0CompilerError::Cairo0CompilerVersion(error)) => {
3038
panic!(
@@ -35,19 +43,18 @@ fn compile_program(path_to_main_file: PathBuf) -> Vec<u8> {
3543
)
3644
}
3745
Err(other_error) => {
38-
panic!("Failed to compile the program. Error:\n{other_error}.")
46+
panic!("Failed to compile the {program_name} program. Error:\n{other_error}.")
3947
}
40-
}
48+
};
49+
println!(
50+
"cargo::warning=Done compiling {program_name}. Writing compiled bytes to output directory."
51+
);
52+
let bytes_path = out_dir.join(format!("{program_name}_bytes"));
53+
std::fs::write(&bytes_path, &bytes).unwrap_or_else(|error| {
54+
panic!("Failed to write the compiled {program_name} bytes to {bytes_path:?}: {error}.")
55+
});
4156
}
4257

4358
fn cairo_root_path() -> PathBuf {
4459
PathBuf::from(compile_time_cargo_manifest_dir!()).join("src/cairo")
4560
}
46-
47-
fn compile_starknet_os() -> Vec<u8> {
48-
compile_program(cairo_root_path().join("starkware/starknet/core/os/os.cairo"))
49-
}
50-
51-
fn compile_starknet_aggregator() -> Vec<u8> {
52-
compile_program(cairo_root_path().join("starkware/starknet/core/aggregator/main.cairo"))
53-
}

0 commit comments

Comments
 (0)