Skip to content

Commit

Permalink
refactor(dojo-lang): output debug info conditionally (#2411)
Browse files Browse the repository at this point in the history
output debug info based on flag
  • Loading branch information
kariy authored Sep 10, 2024
1 parent d4401e3 commit dbe8ca8
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 8 deletions.
5 changes: 5 additions & 0 deletions bin/sozo/src/commands/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@ pub struct BuildArgs {
/// Specify packages to build.
#[command(flatten)]
pub packages: Option<PackagesFilter>,

#[arg(long)]
#[arg(help = "Output the Sierra debug information for the compiled contracts.")]
pub output_debug_info: bool,
}

impl BuildArgs {
Expand Down Expand Up @@ -167,6 +171,7 @@ impl Default for BuildArgs {
bindings_output: "bindings".to_string(),
stats: false,
packages: None,
output_debug_info: false,
}
}
}
Expand Down
10 changes: 8 additions & 2 deletions bin/sozo/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,16 @@ fn cli_main(args: SozoArgs) -> Result<()> {
let cairo_plugins = CairoPluginRepository::default();

match &args.command {
Commands::Build(_) | Commands::Dev(_) | Commands::Migrate(_) => {
Commands::Build(args) => {
trace!("Adding DojoCompiler to compiler repository.");
compilers.add(Box::new(DojoCompiler)).unwrap()
compilers.add(Box::new(DojoCompiler::new(args.output_debug_info))).unwrap()
}

Commands::Dev(_) | Commands::Migrate(_) => {
trace!("Adding DojoCompiler to compiler repository.");
compilers.add(Box::new(DojoCompiler::default())).unwrap()
}

_ => {}
}

Expand Down
21 changes: 16 additions & 5 deletions crates/dojo-lang/src/compiler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,21 @@ pub(crate) const LOG_TARGET: &str = "dojo_lang::compiler";
#[path = "compiler_test.rs"]
mod test;

#[derive(Debug)]
pub struct DojoCompiler;
#[derive(Debug, Default)]
pub struct DojoCompiler {
/// Output the debug information of the compiled Sierra contracts.
///
/// Mainly used for the Walnut debugger integration. It is used
/// internally by Walnut to build the Dojo project with the Sierra
/// debug information. This flag has no use outside of that.
output_debug_info: bool,
}

impl DojoCompiler {
pub fn new(output_debug_info: bool) -> Self {
Self { output_debug_info }
}
}

#[derive(Debug, Default, Serialize, Deserialize)]
#[serde(rename_all = "kebab-case")]
Expand Down Expand Up @@ -143,9 +156,7 @@ impl Compiler for DojoCompiler {
compile_prepared_db(db, &contracts, compiler_config)?
};

// TODO: get the debug flag from the `dojo_<profile>.toml` file.
let with_debug_info = true;
let debug_info_classes: Vec<Option<SierraToCairoDebugInfo>> = if with_debug_info {
let debug_info_classes: Vec<Option<SierraToCairoDebugInfo>> = if self.output_debug_info {
let debug_classes =
crate::scarb_internal::debug::compile_prepared_db_to_debug_info(db, &contracts)?;

Expand Down
2 changes: 1 addition & 1 deletion crates/dojo-test-utils/src/compiler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,7 @@ pub fn copy_project_temp(
/// * `profile` - The profile to use for the config.
pub fn build_test_config(path: &str, profile: Profile) -> anyhow::Result<Config> {
let mut compilers = CompilerRepository::empty();
compilers.add(Box::new(DojoCompiler)).unwrap();
compilers.add(Box::new(DojoCompiler::default())).unwrap();

let cairo_plugins = CairoPluginRepository::default();

Expand Down

0 comments on commit dbe8ca8

Please sign in to comment.