diff --git a/scarb/scarblib/cairo_run/Scarb.toml b/scarb/scarblib/cairo_run/Scarb.toml new file mode 100644 index 000000000..fd9d586bb --- /dev/null +++ b/scarb/scarblib/cairo_run/Scarb.toml @@ -0,0 +1,8 @@ +[package] +name = "cairo_run" +version = "{{ CAIRO_VERSION }}" + +no-core = true + +[cairo-plugin] +builtin = true diff --git a/scarb/src/compiler/plugin/builtin.rs b/scarb/src/compiler/plugin/builtin.rs index 3525d6e2a..9963d8264 100644 --- a/scarb/src/compiler/plugin/builtin.rs +++ b/scarb/src/compiler/plugin/builtin.rs @@ -1,6 +1,9 @@ use anyhow::Result; +use cairo_lang_defs::plugin::{MacroPlugin, MacroPluginMetadata, PluginResult}; use cairo_lang_semantic::plugin::PluginSuite; use cairo_lang_starknet::starknet_plugin_suite; +use cairo_lang_syntax::node::ast::ModuleItem; +use cairo_lang_syntax::node::db::SyntaxGroup; use cairo_lang_test_plugin::test_plugin_suite; use crate::compiler::plugin::{CairoPlugin, CairoPluginInstance}; @@ -52,3 +55,53 @@ impl CairoPluginInstance for BuiltinTestPluginInstance { test_plugin_suite() } } + +pub struct BuiltinCairoRunPlugin; + +impl CairoPlugin for BuiltinCairoRunPlugin { + fn id(&self) -> PackageId { + PackageId::new( + PackageName::CAIRO_RUN_PLUGIN, + crate::version::get().cairo.version.to_version().unwrap(), + SourceId::for_std(), + ) + } + + fn instantiate(&self) -> Result> { + Ok(Box::new(BuiltinCairoRunPluginInstance)) + } +} + +struct BuiltinCairoRunPluginInstance; + +impl CairoPluginInstance for BuiltinCairoRunPluginInstance { + fn plugin_suite(&self) -> PluginSuite { + let mut suite = PluginSuite::default(); + suite.add_plugin::(); + suite + } +} + +const CAIRO_RUN_EXECUTABLE: &str = "main"; + +/// A plugin that defines an executable attribute for cairo-run. +/// No code generation is performed. +#[derive(Debug, Default)] +struct CairoRunPlugin {} + +impl MacroPlugin for CairoRunPlugin { + fn generate_code( + &self, + _db: &dyn SyntaxGroup, + _item_ast: ModuleItem, + _metadata: &MacroPluginMetadata<'_>, + ) -> PluginResult { + PluginResult::default() + } + fn declared_attributes(&self) -> Vec { + vec![CAIRO_RUN_EXECUTABLE.to_string()] + } + fn executable_attributes(&self) -> Vec { + self.declared_attributes() + } +} diff --git a/scarb/src/compiler/plugin/mod.rs b/scarb/src/compiler/plugin/mod.rs index 702c2146c..6aa9d7137 100644 --- a/scarb/src/compiler/plugin/mod.rs +++ b/scarb/src/compiler/plugin/mod.rs @@ -7,6 +7,7 @@ use cairo_lang_semantic::plugin::PluginSuite; use itertools::Itertools; use serde::{Deserialize, Serialize}; +use crate::compiler::plugin::builtin::BuiltinCairoRunPlugin; use crate::core::{Package, PackageId, TargetKind, Workspace}; use self::builtin::{BuiltinStarkNetPlugin, BuiltinTestPlugin}; @@ -62,6 +63,7 @@ impl CairoPluginRepository { // library code to users etc. repo.add(Box::new(BuiltinStarkNetPlugin)).unwrap(); repo.add(Box::new(BuiltinTestPlugin)).unwrap(); + repo.add(Box::new(BuiltinCairoRunPlugin)).unwrap(); repo } diff --git a/scarb/src/core/package/name.rs b/scarb/src/core/package/name.rs index e7e399334..8e6a90c9d 100644 --- a/scarb/src/core/package/name.rs +++ b/scarb/src/core/package/name.rs @@ -8,10 +8,10 @@ use serde::{Deserialize, Serialize}; use smol_str::SmolStr; use crate::internal::restricted_names; +use crate::{CAIRO_RUN_PLUGIN_NAME, STARKNET_PLUGIN_NAME, TEST_PLUGIN_NAME}; #[cfg(doc)] use crate::core::Package; -use crate::{STARKNET_PLUGIN_NAME, TEST_PLUGIN_NAME}; /// A [`String`]-like type representing [`Package`] name. /// @@ -31,6 +31,7 @@ impl PackageName { pub const CORE: Self = PackageName(SmolStr::new_inline(CORELIB_CRATE_NAME)); pub const STARKNET: Self = PackageName(SmolStr::new_inline(STARKNET_PLUGIN_NAME)); pub const TEST_PLUGIN: Self = PackageName(SmolStr::new_inline(TEST_PLUGIN_NAME)); + pub const CAIRO_RUN_PLUGIN: Self = PackageName(SmolStr::new_inline(CAIRO_RUN_PLUGIN_NAME)); /// Constructs and validates new [`PackageName`]. /// diff --git a/scarb/src/lib.rs b/scarb/src/lib.rs index 6e863a90b..7dc61eed6 100644 --- a/scarb/src/lib.rs +++ b/scarb/src/lib.rs @@ -38,3 +38,4 @@ pub const DEFAULT_README_FILE_NAME: &str = "README.md"; pub const DEFAULT_LICENSE_FILE_NAME: &str = "LICENSE"; pub const STARKNET_PLUGIN_NAME: &str = "starknet"; pub const TEST_PLUGIN_NAME: &str = "cairo_test"; +pub const CAIRO_RUN_PLUGIN_NAME: &str = "cairo_run"; diff --git a/scarb/src/ops/resolve.rs b/scarb/src/ops/resolve.rs index 412a2aaf7..8dfeaedf5 100644 --- a/scarb/src/ops/resolve.rs +++ b/scarb/src/ops/resolve.rs @@ -93,6 +93,12 @@ pub fn resolve_workspace_with_opts( .version_req(version_req.clone()) .source_id(SourceId::for_std()) .build(), + ManifestDependency::builder() + .kind(DepKind::Target(TargetKind::LIB)) + .name(PackageName::CAIRO_RUN_PLUGIN) + .version_req(version_req.clone()) + .source_id(SourceId::for_std()) + .build(), ], ); if let Some(custom_source_patches) = ws.config().custom_source_patches() {