Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
FroyaTheHen committed Dec 10, 2024
1 parent 7b11ab7 commit 25b9be8
Show file tree
Hide file tree
Showing 7 changed files with 80 additions and 1 deletion.
26 changes: 26 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions scarb/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ async-trait.workspace = true
cairo-lang-compiler.workspace = true
cairo-lang-defs.workspace = true
cairo-lang-diagnostics.workspace = true
cairo-lang-executable = "2.9.1"
cairo-lang-filesystem.workspace = true
cairo-lang-formatter.workspace = true
cairo-lang-lowering.workspace = true
Expand Down
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::{Context, Result};
use cairo_lang_compiler::db::RootDatabase;
use cairo_lang_executable::executable::Executable;

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 executable = Executable::new(
cairo_lang_executable::compile::compile_executable_in_prepared_db(
db,
None,
main_crate_ids,
compiler_config.diagnostics_reporter,
)
.with_context(|| format!("Failed to compile executable"))?,
);

write_json(
format!("{}.executable.json", unit.main_component().target_name()).as_str(),
"output file",
&target_dir,
ws,
&executable,
)?;
Ok(())
}
}
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
2 changes: 2 additions & 0 deletions scarb/tests/build_targets.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1047,3 +1047,5 @@ fn test_target_builds_external() {
t.child("hello/target/dev/hello_unittest.test.starknet_artifacts.json")
.assert_is_json::<serde_json::Value>();
}

// TODO: test ExecutableCompiler

0 comments on commit 25b9be8

Please sign in to comment.