Skip to content

Commit 322d023

Browse files
committed
feat: compile executable
commit-id:4510e5a4
1 parent b97fdf5 commit 322d023

File tree

8 files changed

+75
-12
lines changed

8 files changed

+75
-12
lines changed
+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
use crate::compiler::helpers::write_json;
2+
use crate::compiler::helpers::{build_compiler_config, collect_main_crate_ids};
3+
use crate::compiler::{CairoCompilationUnit, CompilationUnitAttributes, Compiler};
4+
use crate::core::{TargetKind, Workspace};
5+
use anyhow::Result;
6+
use cairo_lang_compiler::db::RootDatabase;
7+
use cairo_lang_executable::executable::Executable;
8+
use tracing::trace_span;
9+
10+
pub struct ExecutableCompiler;
11+
12+
impl Compiler for ExecutableCompiler {
13+
fn target_kind(&self) -> TargetKind {
14+
TargetKind::EXECUTABLE.clone()
15+
}
16+
17+
fn compile(
18+
&self,
19+
unit: CairoCompilationUnit,
20+
db: &mut RootDatabase,
21+
ws: &Workspace<'_>,
22+
) -> Result<()> {
23+
let target_dir = unit.target_dir(ws);
24+
let main_crate_ids = collect_main_crate_ids(&unit, db);
25+
let compiler_config = build_compiler_config(db, &unit, &main_crate_ids, ws);
26+
let span = trace_span!("compile_executable");
27+
let executable = {
28+
let _guard = span.enter();
29+
Executable::new(
30+
cairo_lang_executable::compile::compile_executable_in_prepared_db(
31+
db,
32+
None,
33+
main_crate_ids,
34+
compiler_config.diagnostics_reporter,
35+
)?,
36+
)
37+
};
38+
39+
write_json(
40+
format!("{}.executable.json", unit.main_component().target_name()).as_str(),
41+
"output file",
42+
&target_dir,
43+
ws,
44+
&executable,
45+
)
46+
}
47+
}

scarb/src/compiler/compilers/lib.rs

+6-3
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,9 @@ impl Compiler for LibCompiler {
6262

6363
validate_compiler_config(db, &compiler_config, &unit, ws);
6464

65+
let span = trace_span!("compile_sierra");
6566
let sierra_program: VersionedProgram = {
66-
let _ = trace_span!("compile_sierra").enter();
67+
let _guard = span.enter();
6768
let program_artifact = cairo_lang_compiler::compile_prepared_db_program_artifact(
6869
db,
6970
main_crate_ids,
@@ -101,8 +102,9 @@ impl Compiler for LibCompiler {
101102
if props.casm {
102103
let program = sierra_program.into_v1().unwrap().program;
103104

105+
let span = trace_span!("casm_calc_metadata");
104106
let metadata = {
105-
let _ = trace_span!("casm_calc_metadata").enter();
107+
let _guard = span.enter();
106108

107109
if unit.compiler_config.enable_gas {
108110
debug!("calculating Sierra variables");
@@ -114,8 +116,9 @@ impl Compiler for LibCompiler {
114116
.context("failed calculating Sierra variables")?
115117
};
116118

119+
let span = trace_span!("compile_casm");
117120
let cairo_program = {
118-
let _ = trace_span!("compile_casm").enter();
121+
let _guard = span.enter();
119122
let sierra_to_casm = SierraToCasmConfig {
120123
gas_usage_check: unit.compiler_config.enable_gas,
121124
max_bytecode_size: usize::MAX,

scarb/src/compiler/compilers/mod.rs

+2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1+
pub use executable::*;
12
pub use lib::*;
23
pub use starknet_contract::*;
34
pub use test::*;
45

6+
mod executable;
57
mod lib;
68
mod starknet_contract;
79
mod test;

scarb/src/compiler/compilers/starknet_contract/compiler.rs

+9-4
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,9 @@ impl Compiler for StarknetContractCompiler {
114114
check_allowed_libfuncs(&props, &contracts, &classes, db, &unit, ws)?;
115115

116116
let casm_classes: Vec<Option<CasmContractClass>> = if props.casm {
117-
let _ = trace_span!("compile_sierra").enter();
117+
let span = trace_span!("compile_sierra");
118+
let _guard = span.enter();
119+
118120
zip(&contracts, &classes)
119121
.map(|(decl, class)| -> Result<_> {
120122
let contract_name = decl.submodule_id.name(db.upcast_mut());
@@ -159,8 +161,9 @@ pub fn get_compiled_contracts(
159161
.collect::<Vec<_>>();
160162
trace!(contracts = ?contract_paths);
161163

164+
let span = trace_span!("compile_starknet");
162165
let classes = {
163-
let _ = trace_span!("compile_starknet").enter();
166+
let _guard = span.enter();
164167
compile_prepared_db(db, &contracts.iter().collect::<Vec<_>>(), compiler_config)?
165168
};
166169
Ok(CompiledContracts {
@@ -177,14 +180,16 @@ pub fn find_project_contracts(
177180
main_crate_ids: Vec<CrateId>,
178181
external_contracts: Option<Vec<ContractSelector>>,
179182
) -> Result<Vec<ContractDeclaration>> {
183+
let span = trace_span!("find_internal_contracts");
180184
let internal_contracts = {
181-
let _ = trace_span!("find_internal_contracts").enter();
185+
let _guard = span.enter();
182186
find_contracts(db, &main_crate_ids)
183187
};
184188

189+
let span = trace_span!("find_external_contracts");
185190
let external_contracts: Vec<ContractDeclaration> =
186191
if let Some(external_contracts) = external_contracts {
187-
let _ = trace_span!("find_external_contracts").enter();
192+
let _guard = span.enter();
188193
debug!("external contracts selectors: {:?}", external_contracts);
189194

190195
let crate_ids = external_contracts

scarb/src/compiler/compilers/test.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,9 @@ impl Compiler for TestCompiler {
6262
let diagnostics_reporter =
6363
build_compiler_config(db, &unit, &test_crate_ids, ws).diagnostics_reporter;
6464

65+
let span = trace_span!("compile_test");
6566
let test_compilation = {
66-
let _ = trace_span!("compile_test").enter();
67+
let _guard = span.enter();
6768
let config = TestsCompilationConfig {
6869
starknet,
6970
add_statements_functions: unit
@@ -79,9 +80,9 @@ impl Compiler for TestCompiler {
7980
compile_test_prepared_db(db, config, test_crate_ids.clone(), diagnostics_reporter)?
8081
};
8182

83+
let span = trace_span!("serialize_test");
8284
{
83-
let _ = trace_span!("serialize_test").enter();
84-
85+
let _guard = span.enter();
8586
let sierra_program: VersionedProgram = test_compilation.sierra_program.clone().into();
8687
let file_name = format!("{}.test.sierra.json", unit.main_component().target_name());
8788
write_json(&file_name, "output file", &target_dir, ws, &sierra_program)?;

scarb/src/compiler/plugin/proc_macro/compilation.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -200,8 +200,9 @@ fn run_cargo(action: CargoAction, package: &Package, ws: &Workspace<'_>) -> Resu
200200
.to_path_buf(),
201201
config: ws.config(),
202202
};
203+
let span = trace_span!("proc_macro");
203204
{
204-
let _ = trace_span!("proc_macro").enter();
205+
let _guard = span.enter();
205206
exec(&mut cmd.into(), ws.config())?;
206207
}
207208
Ok(())

scarb/src/compiler/repository.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@ use cairo_lang_compiler::db::RootDatabase;
77
use itertools::Itertools;
88
use smol_str::SmolStr;
99

10-
use crate::compiler::compilers::{LibCompiler, StarknetContractCompiler, TestCompiler};
10+
use crate::compiler::compilers::{
11+
ExecutableCompiler, LibCompiler, StarknetContractCompiler, TestCompiler,
12+
};
1113
use crate::compiler::{CairoCompilationUnit, CompilationUnitAttributes, Compiler};
1214
use crate::core::Workspace;
1315

@@ -27,6 +29,7 @@ impl CompilerRepository {
2729
repo.add(Box::new(LibCompiler)).unwrap();
2830
repo.add(Box::new(StarknetContractCompiler)).unwrap();
2931
repo.add(Box::new(TestCompiler)).unwrap();
32+
repo.add(Box::new(ExecutableCompiler)).unwrap();
3033
repo
3134
}
3235

scarb/src/core/manifest/target_kind.rs

+1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ impl TargetKind {
1515
pub const LIB: Self = TargetKind(SmolStr::new_inline("lib"));
1616
pub const TEST: Self = TargetKind(SmolStr::new_inline("test"));
1717
pub const STARKNET_CONTRACT: Self = TargetKind(SmolStr::new_inline("starknet-contract"));
18+
pub const EXECUTABLE: Self = TargetKind(SmolStr::new_inline("executable"));
1819

1920
/// Constructs and validates new [`TargetKind`].
2021
///

0 commit comments

Comments
 (0)