Skip to content

Commit f5f2e37

Browse files
feat(apollo_starknet_os_program): define and implement static Program
1 parent 01f24e5 commit f5f2e37

File tree

4 files changed

+64
-0
lines changed

4 files changed

+64
-0
lines changed

Cargo.lock

Lines changed: 4 additions & 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: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,10 @@ description = "The source (Cairo) code of the Starknet OS."
88

99
[lints]
1010
workspace = true
11+
12+
[dependencies]
13+
cairo-vm.workspace = true
14+
15+
[build-dependencies]
16+
apollo_infra_utils.workspace = true
17+
cairo-vm.workspace = true
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
use std::path::PathBuf;
2+
use std::process::Command;
3+
4+
use apollo_infra_utils::cairo0_compiler::verify_cairo0_compiler_deps;
5+
use apollo_infra_utils::compile_time_cargo_manifest_dir;
6+
7+
/// Build script for the `apollo_starknet_os_program` crate.
8+
/// Recompiles the OS program if the source files change.
9+
fn main() {
10+
let out_dir = std::env::var("OUT_DIR").expect("OUT_DIR not set.");
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 = PathBuf::from(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+
19+
/// Compile the StarkNet OS program.
20+
fn compile_starknet_os() -> Vec<u8> {
21+
verify_cairo0_compiler_deps();
22+
let cairo_root_path = PathBuf::from(compile_time_cargo_manifest_dir!()).join("src/cairo");
23+
let os_main_path = cairo_root_path.join("starkware/starknet/core/os/os.cairo");
24+
assert!(os_main_path.exists(), "OS main file does not exist at {os_main_path:?}.");
25+
let mut compile_os_command = Command::new("cairo-compile");
26+
compile_os_command.args([
27+
os_main_path.to_str().expect("Path is valid unicode."),
28+
"--debug_info_with_source",
29+
"--cairo_path",
30+
cairo_root_path.to_str().expect("Path to cairo is valid unicode."),
31+
]);
32+
println!("cargo::warning=Running command {compile_os_command:?}.");
33+
let compile_os_output =
34+
compile_os_command.output().expect("Failed to run the OS compile command.");
35+
36+
// Verify output.
37+
if !compile_os_output.status.success() {
38+
let stderr = String::from_utf8_lossy(&compile_os_output.stderr);
39+
panic!("Failed to compile the OS. Error: {}", stderr.trim());
40+
}
41+
42+
compile_os_output.stdout
43+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,11 @@
1+
use std::sync::LazyLock;
12

3+
use cairo_vm::types::program::Program;
4+
5+
pub const OS_PROGRAM: LazyLock<Program> = LazyLock::new(|| {
6+
Program::from_bytes(
7+
include_bytes!(concat!(env!("OUT_DIR"), "/starknet_os_bytes")),
8+
Some("main"),
9+
)
10+
.expect("Failed to load the OS bytes.")
11+
});

0 commit comments

Comments
 (0)