Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: Executable Target #1823

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ cairo-lang-compiler = "*"
cairo-lang-defs = "*"
cairo-lang-diagnostics = "*"
cairo-lang-doc = "*"
cairo-lang-executable = "*"
cairo-lang-filesystem = "*"
cairo-lang-formatter = "*"
cairo-lang-lowering = "*"
Expand Down
1 change: 1 addition & 0 deletions scarb/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ async-trait.workspace = true
cairo-lang-compiler.workspace = true
cairo-lang-defs.workspace = true
cairo-lang-diagnostics.workspace = true
cairo-lang-executable.workspace = true
cairo-lang-filesystem.workspace = true
cairo-lang-formatter.workspace = true
cairo-lang-lowering.workspace = true
Expand Down
6 changes: 6 additions & 0 deletions scarb/scarblib/executable/Scarb.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[package]
name = "executable"
version = "{{ CAIRO_VERSION }}"

[cairo-plugin]
builtin = true
44 changes: 44 additions & 0 deletions scarb/src/compiler/compilers/executable.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
use crate::compiler::helpers::write_json;
use crate::compiler::helpers::{build_compiler_config, collect_main_crate_ids};
use crate::compiler::{CairoCompilationUnit, CompilationUnitAttributes, Compiler};
use crate::core::{TargetKind, Workspace};
use anyhow::Result;
use cairo_lang_compiler::db::RootDatabase;
use cairo_lang_executable::executable::Executable;
use tracing::trace_span;

pub struct ExecutableCompiler;

impl Compiler for ExecutableCompiler {
fn target_kind(&self) -> TargetKind {
TargetKind::EXECUTABLE.clone()
}

fn compile(
&self,
unit: CairoCompilationUnit,
db: &mut RootDatabase,
ws: &Workspace<'_>,
) -> Result<()> {
let target_dir = unit.target_dir(ws);
let main_crate_ids = collect_main_crate_ids(&unit, db);
let compiler_config = build_compiler_config(db, &unit, &main_crate_ids, ws);
let _ = trace_span!("compile_executable").enter();
let executable = Executable::new(
cairo_lang_executable::compile::compile_executable_in_prepared_db(
FroyaTheHen marked this conversation as resolved.
Show resolved Hide resolved
db,
None,
main_crate_ids,
compiler_config.diagnostics_reporter,
)?,
);

write_json(
format!("{}.executable.json", unit.main_component().target_name()).as_str(),
"output file",
&target_dir,
ws,
&executable,
)
}
}
2 changes: 2 additions & 0 deletions scarb/src/compiler/compilers/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
pub use executable::*;
pub use lib::*;
pub use starknet_contract::*;
pub use test::*;

mod executable;
mod lib;
mod starknet_contract;
mod test;
23 changes: 23 additions & 0 deletions scarb/src/compiler/plugin/builtin.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use anyhow::Result;
use cairo_lang_defs::plugin::{MacroPlugin, MacroPluginMetadata, PluginResult};
use cairo_lang_executable::plugin::executable_plugin_suite;
use cairo_lang_semantic::plugin::PluginSuite;
use cairo_lang_starknet::starknet_plugin_suite;
use cairo_lang_syntax::node::ast::ModuleItem;
Expand Down Expand Up @@ -32,6 +33,28 @@ impl CairoPluginInstance for BuiltinStarkNetPluginInstance {
}
}

pub struct BuiltinExecutablePlugin;
impl CairoPlugin for BuiltinExecutablePlugin {
fn id(&self) -> PackageId {
PackageId::new(
PackageName::EXECUTABLE,
crate::version::get().cairo.version.to_version().unwrap(),
SourceId::for_std(),
)
}

fn instantiate(&self) -> Result<Box<dyn CairoPluginInstance>> {
Ok(Box::new(BuiltinExecutablePluginInstance))
}
}

struct BuiltinExecutablePluginInstance;
impl CairoPluginInstance for BuiltinExecutablePluginInstance {
fn plugin_suite(&self) -> PluginSuite {
executable_plugin_suite()
}
}

pub struct BuiltinTestPlugin;

impl CairoPlugin for BuiltinTestPlugin {
Expand Down
3 changes: 2 additions & 1 deletion scarb/src/compiler/plugin/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::collections::hash_map::Entry;
use std::collections::HashMap;
use std::fmt;

use crate::compiler::plugin::builtin::BuiltinTestAssertsPlugin;
use crate::compiler::plugin::builtin::{BuiltinExecutablePlugin, BuiltinTestAssertsPlugin};
use anyhow::{anyhow, bail, Result};
use cairo_lang_semantic::plugin::PluginSuite;
use itertools::Itertools;
Expand Down Expand Up @@ -63,6 +63,7 @@ impl CairoPluginRepository {
// `starknet` package which makes it a dependency. This way we can deliver Starknet Cairo
// library code to users etc.
repo.add(Box::new(BuiltinStarkNetPlugin)).unwrap();
repo.add(Box::new(BuiltinExecutablePlugin)).unwrap();
repo.add(Box::new(BuiltinTestPlugin)).unwrap();
repo.add(Box::new(BuiltinCairoRunPlugin)).unwrap();
repo.add(Box::new(BuiltinTestAssertsPlugin)).unwrap();
Expand Down
5 changes: 4 additions & 1 deletion scarb/src/compiler/repository.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ use cairo_lang_compiler::db::RootDatabase;
use itertools::Itertools;
use smol_str::SmolStr;

use crate::compiler::compilers::{LibCompiler, StarknetContractCompiler, TestCompiler};
use crate::compiler::compilers::{
ExecutableCompiler, LibCompiler, StarknetContractCompiler, TestCompiler,
};
use crate::compiler::{CairoCompilationUnit, CompilationUnitAttributes, Compiler};
use crate::core::Workspace;

Expand All @@ -27,6 +29,7 @@ impl CompilerRepository {
repo.add(Box::new(LibCompiler)).unwrap();
repo.add(Box::new(StarknetContractCompiler)).unwrap();
repo.add(Box::new(TestCompiler)).unwrap();
repo.add(Box::new(ExecutableCompiler)).unwrap();
repo
}

Expand Down
1 change: 1 addition & 0 deletions scarb/src/core/manifest/target_kind.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ impl TargetKind {
pub const LIB: Self = TargetKind(SmolStr::new_inline("lib"));
pub const TEST: Self = TargetKind(SmolStr::new_inline("test"));
pub const STARKNET_CONTRACT: Self = TargetKind(SmolStr::new_inline("starknet-contract"));
pub const EXECUTABLE: Self = TargetKind(SmolStr::new_inline("executable"));

/// Constructs and validates new [`TargetKind`].
///
Expand Down
4 changes: 3 additions & 1 deletion scarb/src/core/package/name.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ use crate::internal::restricted_names;
use crate::core::Package;

use crate::{
CAIRO_RUN_PLUGIN_NAME, STARKNET_PLUGIN_NAME, TEST_ASSERTS_PLUGIN_NAME, TEST_PLUGIN_NAME,
CAIRO_RUN_PLUGIN_NAME, EXECUTABLE_PLUGIN_NAME, STARKNET_PLUGIN_NAME, TEST_ASSERTS_PLUGIN_NAME,
TEST_PLUGIN_NAME,
};

/// A [`String`]-like type representing [`Package`] name.
Expand All @@ -33,6 +34,7 @@ pub struct PackageName(SmolStr);
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 EXECUTABLE: Self = PackageName(SmolStr::new_inline(EXECUTABLE_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));
pub const TEST_ASSERTS_PLUGIN: Self =
Expand Down
1 change: 1 addition & 0 deletions scarb/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,4 @@ pub const TEST_ASSERTS_PLUGIN_NAME: &str = "assert_macros";
pub const CAIRO_RUN_PLUGIN_NAME: &str = "cairo_run";
pub const CARGO_MANIFEST_FILE_NAME: &str = "Cargo.toml";
pub const CARGO_LOCK_FILE_NAME: &str = "Cargo.lock";
pub const EXECUTABLE_PLUGIN_NAME: &str = "executable";
5 changes: 5 additions & 0 deletions scarb/src/ops/resolve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,11 @@ pub fn resolve_workspace_with_opts(
.version_req(version_req.clone())
.source_id(SourceId::for_std())
.build(),
ManifestDependency::builder()
.name(PackageName::EXECUTABLE)
.version_req(version_req.clone())
.source_id(SourceId::for_std())
.build(),
ManifestDependency::builder()
.kind(DepKind::Target(TargetKind::TEST))
.name(PackageName::TEST_PLUGIN)
Expand Down
29 changes: 29 additions & 0 deletions scarb/tests/build_targets.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1047,3 +1047,32 @@ fn test_target_builds_external() {
t.child("hello/target/dev/hello_unittest.test.starknet_artifacts.json")
.assert_is_json::<serde_json::Value>();
}

#[test]
fn test_executable_compiler_creates_output_files() {
let t = TempDir::new().unwrap();
ProjectBuilder::start()
.name("executable_test")
.dep_cairo_test()
.dep_starknet()
.dep_executable()
.manifest_extra(indoc! {r#"
[[target.executable]]
"#})
.lib_cairo(indoc! {r#"
#[executable]
fn main() -> felt252 {
42
}
"#})
.build(&t);

Scarb::quick_snapbox()
.arg("build")
.current_dir(&t)
.assert()
.success();

t.child("target/dev/executable_test.executable.json")
.assert(predicates::path::exists());
}
4 changes: 4 additions & 0 deletions utils/scarb-test-support/src/project_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,10 @@ impl ProjectBuilder {
self.dep_builtin("starknet")
}

pub fn dep_executable(self) -> Self {
self.dep_builtin("executable")
}

pub fn dep_cairo_test(self) -> Self {
self.dev_dep_builtin("cairo_test")
}
Expand Down
Loading