|
| 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 | +} |
0 commit comments