Skip to content

Commit 662b2c5

Browse files
feat(apollo_starknet_os_program): compile programs asynchronously
1 parent 6b303d2 commit 662b2c5

File tree

4 files changed

+35
-26
lines changed

4 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
@@ -24,3 +24,4 @@ thiserror.workspace = true
2424
[build-dependencies]
2525
apollo_infra_utils.workspace = true
2626
serde_json.workspace = true
27+
tokio = { workspace = true, features = ["macros", "rt-multi-thread"] }

crates/apollo_starknet_os_program/build/compile_program.rs

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,16 @@ use std::path::PathBuf;
33
use apollo_infra_utils::cairo0_compiler::{compile_cairo0_program, Cairo0CompilerError};
44
use apollo_infra_utils::compile_time_cargo_manifest_dir;
55

6-
fn compile_program(path_to_main_file: PathBuf) -> Vec<u8> {
7-
match compile_cairo0_program(path_to_main_file, cairo_root_path()) {
6+
pub async fn compile_and_output_program(
7+
out_dir: PathBuf,
8+
path_to_main_file_from_cairo_root: &str,
9+
program_name: &str,
10+
) {
11+
println!("cargo::warning=Compiling {program_name} program...");
12+
let bytes = match compile_cairo0_program(
13+
cairo_root_path().join(path_to_main_file_from_cairo_root),
14+
cairo_root_path(),
15+
) {
816
Ok(bytes) => bytes,
917
Err(Cairo0CompilerError::Cairo0CompilerVersion(error)) => {
1018
panic!(
@@ -15,19 +23,18 @@ fn compile_program(path_to_main_file: PathBuf) -> Vec<u8> {
1523
)
1624
}
1725
Err(other_error) => {
18-
panic!("Failed to compile the program. Error:\n{other_error}.")
26+
panic!("Failed to compile the {program_name} program. Error:\n{other_error}.")
1927
}
20-
}
28+
};
29+
println!(
30+
"cargo::warning=Done compiling {program_name}. Writing compiled bytes to output directory."
31+
);
32+
let bytes_path = out_dir.join(format!("{program_name}_bytes"));
33+
std::fs::write(&bytes_path, &bytes).unwrap_or_else(|error| {
34+
panic!("Failed to write the compiled {program_name} bytes to {bytes_path:?}: {error}.")
35+
});
2136
}
2237

2338
fn cairo_root_path() -> PathBuf {
2439
PathBuf::from(compile_time_cargo_manifest_dir!()).join("src/cairo")
2540
}
26-
27-
pub fn compile_starknet_os() -> Vec<u8> {
28-
compile_program(cairo_root_path().join("starkware/starknet/core/os/os.cairo"))
29-
}
30-
31-
pub fn compile_starknet_aggregator() -> Vec<u8> {
32-
compile_program(cairo_root_path().join("starkware/starknet/core/aggregator/main.cairo"))
33-
}

crates/apollo_starknet_os_program/build/main.rs

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,23 +7,23 @@ mod dump_source;
77
/// Build script for the `apollo_starknet_os_program` crate.
88
/// Recompiles the OS program if the source files change.
99
/// Optionally, also exposes all source cairo files in a mapping from file path to contents.
10-
fn main() {
10+
#[tokio::main]
11+
async fn main() {
1112
let out_dir = PathBuf::from(std::env::var("OUT_DIR").expect("OUT_DIR not set."));
1213

1314
#[cfg(feature = "dump_source_files")]
1415
dump_source::dump_source_files(&out_dir.join("cairo_files_map.json"));
1516

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

0 commit comments

Comments
 (0)