Skip to content

Commit

Permalink
Create cairo-run executable plugin (#1387)
Browse files Browse the repository at this point in the history
commit-id:37f9ce70

---

**Stack**:
- #1388
- #1387⚠️ *Part of a stack created by [spr](https://github.com/ejoffe/spr). Do
not merge manually using the UI - doing so may have unexpected results.*
  • Loading branch information
maciektr authored Jun 24, 2024
1 parent a52b2f3 commit 5cf4d55
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 1 deletion.
8 changes: 8 additions & 0 deletions scarb/scarblib/cairo_run/Scarb.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[package]
name = "cairo_run"
version = "{{ CAIRO_VERSION }}"

no-core = true

[cairo-plugin]
builtin = true
53 changes: 53 additions & 0 deletions scarb/src/compiler/plugin/builtin.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand Down Expand Up @@ -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<Box<dyn CairoPluginInstance>> {
Ok(Box::new(BuiltinCairoRunPluginInstance))
}
}

struct BuiltinCairoRunPluginInstance;

impl CairoPluginInstance for BuiltinCairoRunPluginInstance {
fn plugin_suite(&self) -> PluginSuite {
let mut suite = PluginSuite::default();
suite.add_plugin::<CairoRunPlugin>();
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<String> {
vec![CAIRO_RUN_EXECUTABLE.to_string()]
}
fn executable_attributes(&self) -> Vec<String> {
self.declared_attributes()
}
}
2 changes: 2 additions & 0 deletions scarb/src/compiler/plugin/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -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
}

Expand Down
3 changes: 2 additions & 1 deletion scarb/src/core/package/name.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
///
Expand All @@ -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`].
///
Expand Down
1 change: 1 addition & 0 deletions scarb/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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";
6 changes: 6 additions & 0 deletions scarb/src/ops/resolve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down

0 comments on commit 5cf4d55

Please sign in to comment.