Skip to content

Commit

Permalink
Create cairo-run executable plugin
Browse files Browse the repository at this point in the history
commit-id:0037da24
  • Loading branch information
maciektr committed Jun 20, 2024
1 parent 6dd385b commit dbe68a2
Show file tree
Hide file tree
Showing 8 changed files with 151 additions and 16 deletions.
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
9 changes: 9 additions & 0 deletions scarb/src/core/manifest/summary.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,18 @@ impl Summary {
.version_req(DependencyVersionReq::exact(&cairo_version))
.build()
});
static CAIRO_RUN_PLUGIN_DEPENDENCY: Lazy<ManifestDependency> = Lazy::new(|| {
// NOTE: Pin test plugin to exact version, because we know that's the only one we have.
let cairo_version = crate::version::get().cairo.version.parse().unwrap();
ManifestDependency::builder()
.name(PackageName::CAIRO_RUN_PLUGIN)
.version_req(DependencyVersionReq::exact(&cairo_version))
.build()
});
let mut deps: Vec<&ManifestDependency> = Vec::new();
if !self.no_core {
deps.push(&CORE_DEPENDENCY);
deps.push(&CAIRO_RUN_PLUGIN_DEPENDENCY);
}
deps.into_iter()
}
Expand Down
6 changes: 3 additions & 3 deletions scarb/src/core/package/name.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,10 @@ use cairo_lang_filesystem::db::CORELIB_CRATE_NAME;
use serde::{Deserialize, Serialize};
use smol_str::SmolStr;

use crate::internal::restricted_names;

#[cfg(doc)]
use crate::core::Package;
use crate::{STARKNET_PLUGIN_NAME, TEST_PLUGIN_NAME};
use crate::internal::restricted_names;
use crate::{CAIRO_RUN_PLUGIN_NAME, STARKNET_PLUGIN_NAME, TEST_PLUGIN_NAME};

/// A [`String`]-like type representing [`Package`] name.
///
Expand All @@ -31,6 +30,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
2 changes: 2 additions & 0 deletions scarb/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

use camino::Utf8PathBuf;
use once_cell::sync::Lazy;

pub use subcommands::EXTERNAL_CMD_PREFIX;

pub mod compiler;
Expand Down Expand Up @@ -38,3 +39,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";
20 changes: 14 additions & 6 deletions scarb/src/ops/resolve.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
use std::collections::{BTreeMap, HashMap, HashSet, VecDeque};

use anyhow::{bail, Result};
use cairo_lang_filesystem::cfg::{Cfg, CfgSet};
use futures::TryFutureExt;
use indoc::formatdoc;
use itertools::Itertools;

use crate::compiler::plugin::{fetch_cairo_plugin, CairoPluginProps};
use crate::compiler::{
CairoCompilationUnit, CompilationUnit, CompilationUnitAttributes, CompilationUnitCairoPlugin,
Expand All @@ -20,12 +28,6 @@ use crate::internal::to_version::ToVersion;
use crate::ops::lockfile::{read_lockfile, write_lockfile};
use crate::ops::{FeaturesOpts, FeaturesSelector};
use crate::{resolver, DEFAULT_SOURCE_PATH};
use anyhow::{bail, Result};
use cairo_lang_filesystem::cfg::{Cfg, CfgSet};
use futures::TryFutureExt;
use indoc::formatdoc;
use itertools::Itertools;
use std::collections::{BTreeMap, HashMap, HashSet, VecDeque};

pub struct WorkspaceResolve {
pub resolve: Resolve,
Expand Down Expand Up @@ -93,6 +95,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
Loading

0 comments on commit dbe68a2

Please sign in to comment.