Skip to content

Commit

Permalink
feat: compile executable
Browse files Browse the repository at this point in the history
  • Loading branch information
FroyaTheHen committed Dec 11, 2024
1 parent 2dfef6b commit 6c0c742
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 1 deletion.
44 changes: 44 additions & 0 deletions scarb/src/compiler/compilers/executable.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
use crate::compiler::helpers::write_json;
use crate::compiler::helpers::{build_compiler_config, collect_main_crate_ids};
use crate::compiler::{CairoCompilationUnit, CompilationUnitAttributes, Compiler};
use crate::core::{TargetKind, Workspace};
use anyhow::Result;
use cairo_lang_compiler::db::RootDatabase;
use cairo_lang_executable::executable::Executable;
use tracing::trace_span;

pub struct ExecutableCompiler;

impl Compiler for ExecutableCompiler {
fn target_kind(&self) -> TargetKind {
TargetKind::EXECUTABLE.clone()
}

fn compile(
&self,
unit: CairoCompilationUnit,
db: &mut RootDatabase,
ws: &Workspace<'_>,
) -> Result<()> {
let target_dir = unit.target_dir(ws);
let main_crate_ids = collect_main_crate_ids(&unit, db);
let compiler_config = build_compiler_config(db, &unit, &main_crate_ids, ws);
let _ = trace_span!("compile_executable").enter();
let executable = Executable::new(
cairo_lang_executable::compile::compile_executable_in_prepared_db(
db,
None,
main_crate_ids,
compiler_config.diagnostics_reporter,
)?,
);

write_json(
format!("{}.executable.json", unit.main_component().target_name()).as_str(),
"output file",
&target_dir,
ws,
&executable,
)
}
}
2 changes: 2 additions & 0 deletions scarb/src/compiler/compilers/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
pub use executable::*;
pub use lib::*;
pub use starknet_contract::*;
pub use test::*;

mod executable;
mod lib;
mod starknet_contract;
mod test;
5 changes: 4 additions & 1 deletion scarb/src/compiler/repository.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ use cairo_lang_compiler::db::RootDatabase;
use itertools::Itertools;
use smol_str::SmolStr;

use crate::compiler::compilers::{LibCompiler, StarknetContractCompiler, TestCompiler};
use crate::compiler::compilers::{
ExecutableCompiler, LibCompiler, StarknetContractCompiler, TestCompiler,
};
use crate::compiler::{CairoCompilationUnit, CompilationUnitAttributes, Compiler};
use crate::core::Workspace;

Expand All @@ -27,6 +29,7 @@ impl CompilerRepository {
repo.add(Box::new(LibCompiler)).unwrap();
repo.add(Box::new(StarknetContractCompiler)).unwrap();
repo.add(Box::new(TestCompiler)).unwrap();
repo.add(Box::new(ExecutableCompiler)).unwrap();
repo
}

Expand Down
1 change: 1 addition & 0 deletions scarb/src/core/manifest/target_kind.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ impl TargetKind {
pub const LIB: Self = TargetKind(SmolStr::new_inline("lib"));
pub const TEST: Self = TargetKind(SmolStr::new_inline("test"));
pub const STARKNET_CONTRACT: Self = TargetKind(SmolStr::new_inline("starknet-contract"));
pub const EXECUTABLE: Self = TargetKind(SmolStr::new_inline("executable"));

/// Constructs and validates new [`TargetKind`].
///
Expand Down
29 changes: 29 additions & 0 deletions scarb/tests/build_targets.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1047,3 +1047,32 @@ fn test_target_builds_external() {
t.child("hello/target/dev/hello_unittest.test.starknet_artifacts.json")
.assert_is_json::<serde_json::Value>();
}

#[test]
fn test_executable_compiler_creates_output_files() {
let t = TempDir::new().unwrap();
ProjectBuilder::start()
.name("executable_test")
.dep_cairo_test()
.dep_starknet()
.dep_executable()
.manifest_extra(indoc! {r#"
[[target.executable]]
"#})
.lib_cairo(indoc! {r#"
#[executable]
fn main() -> felt252 {
42
}
"#})
.build(&t);

Scarb::quick_snapbox()
.arg("build")
.current_dir(&t)
.assert()
.success();

t.child("target/dev/executable_test.executable.json")
.assert(predicates::path::exists());
}

0 comments on commit 6c0c742

Please sign in to comment.