Skip to content

Commit

Permalink
Introduced the new per-crate plugin logic
Browse files Browse the repository at this point in the history
  • Loading branch information
integraledelebesgue committed Dec 18, 2024
1 parent c89d819 commit 554003c
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 19 deletions.
17 changes: 8 additions & 9 deletions extensions/scarb-doc/src/db.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use cairo_lang_compiler::project::{update_crate_roots_from_project_config, ProjectConfig};
use cairo_lang_defs::db::{try_ext_as_virtual_impl, DefsDatabase, DefsGroup};
use cairo_lang_defs::db::{init_defs_group, try_ext_as_virtual_impl, DefsDatabase, DefsGroup};
use cairo_lang_doc::db::{DocDatabase, DocGroup};
use cairo_lang_filesystem::cfg::{Cfg, CfgSet};
use cairo_lang_filesystem::db::{
Expand All @@ -8,7 +8,9 @@ use cairo_lang_filesystem::db::{
use cairo_lang_filesystem::ids::VirtualFile;
use cairo_lang_lowering::db::{LoweringDatabase, LoweringGroup};
use cairo_lang_parser::db::{ParserDatabase, ParserGroup};
use cairo_lang_semantic::db::{SemanticDatabase, SemanticGroup};
use cairo_lang_semantic::db::{
init_semantic_group, PluginSuiteInput, SemanticDatabase, SemanticGroup,
};
use cairo_lang_semantic::inline_macros::get_default_plugin_suite;
use cairo_lang_semantic::plugin::PluginSuite;
use cairo_lang_starknet::starknet_plugin_suite;
Expand Down Expand Up @@ -44,10 +46,13 @@ impl ScarbDocDatabase {
};

init_files_group(&mut db);
init_defs_group(&mut db);
init_semantic_group(&mut db);

db.set_cfg_set(Self::initial_cfg_set().into());

db.apply_plugin_suite(plugin_suite);
let interned_plugin_suite = db.intern_plugin_suite(plugin_suite);
db.set_default_plugins_from_suite(interned_plugin_suite);

if let Some(config) = project_config {
db.apply_project_config(config);
Expand All @@ -60,12 +65,6 @@ impl ScarbDocDatabase {
CfgSet::from_iter([Cfg::name("doc")])
}

fn apply_plugin_suite(&mut self, plugin_suite: PluginSuite) {
self.set_macro_plugins(plugin_suite.plugins);
self.set_inline_macro_plugins(plugin_suite.inline_macro_plugins.into());
self.set_analyzer_plugins(plugin_suite.analyzer_plugins);
}

fn apply_project_config(&mut self, config: ProjectConfig) {
update_crate_roots_from_project_config(self, &config);
}
Expand Down
18 changes: 14 additions & 4 deletions scarb/src/compiler/compilers/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ use anyhow::{Context, Result};
use cairo_lang_compiler::db::RootDatabase;
use cairo_lang_compiler::CompilerConfig;
use cairo_lang_defs::db::DefsGroup;
use cairo_lang_defs::plugin::MacroPlugin;
use cairo_lang_filesystem::db::FilesGroup;
use cairo_lang_filesystem::ids::CrateLongId;
use cairo_lang_sierra::program::VersionedProgram;
use cairo_lang_sierra_to_casm::compiler::SierraToCasmConfig;
use cairo_lang_sierra_to_casm::metadata::{calc_metadata, calc_metadata_ap_change_only};
Expand Down Expand Up @@ -142,15 +145,22 @@ fn validate_compiler_config(
unit: &CairoCompilationUnit,
ws: &Workspace<'_>,
) {
let main_component = unit.main_component();
let main_crate_id = db.intern_crate(CrateLongId::Real {
name: main_component.target_name(),
discriminator: main_component.id.to_discriminator(),
});

// Generally, lib target compilation should be driven by a certain objective (e.g. cairo-run,
// test framework, etc.), expressed by the plugin set with executables definition.
// This does not apply to debug build (expressed by `replace_ids` flag),
// which is a goal by itself.
// See starkware-libs/cairo#5440 for more context.
let executable_plugin = db
.macro_plugins()
.iter()
.any(|plugin| !plugin.executable_attributes().is_empty());
let executable_plugin = db.crate_macro_plugins(main_crate_id).iter().any(|&plugin| {
!db.lookup_intern_macro_plugin(plugin)
.executable_attributes()
.is_empty()
});
if !executable_plugin && !compiler_config.replace_ids {
ws.config().ui().warn(formatdoc! {r#"
artefacts produced by this build may be hard to utilize due to the build configuration
Expand Down
15 changes: 10 additions & 5 deletions scarb/src/compiler/db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,13 +58,13 @@ fn load_plugins(
let package_id = plugin_info.package.id;
let plugin = ws.config().cairo_plugins().fetch(package_id)?;
let instance = plugin.instantiate()?;
builder.with_plugin_suite(instance.plugin_suite());
builder.with_default_plugin_suite(instance.plugin_suite());
} else {
proc_macros.register(plugin_info.package.clone(), ws.config())?;
}
}
let macro_host = Arc::new(proc_macros.into_plugin()?);
builder.with_plugin_suite(ProcMacroHostPlugin::build_plugin_suite(macro_host.clone()));
builder.with_default_plugin_suite(ProcMacroHostPlugin::build_plugin_suite(macro_host.clone()));
Ok(macro_host)
}

Expand Down Expand Up @@ -198,10 +198,15 @@ fn build_project_config(unit: &CairoCompilationUnit) -> Result<ProjectConfig> {
Ok(project_config)
}

pub(crate) fn has_starknet_plugin(db: &RootDatabase) -> bool {
db.macro_plugins()
pub(crate) fn has_starknet_plugin(db: &RootDatabase, component: &CompilationUnitComponent) -> bool {
let crate_id = db.intern_crate(CrateLongId::Real {
name: component.target_name(),
discriminator: component.id.to_discriminator(),
});

db.crate_macro_plugins(crate_id)
.iter()
.any(|plugin| is_starknet_plugin(&**plugin))
.any(|&plugin| is_starknet_plugin(&db.lookup_intern_macro_plugin(plugin)))
}

fn is_starknet_plugin(plugin: &dyn MacroPlugin) -> bool {
Expand Down
2 changes: 1 addition & 1 deletion scarb/src/ops/compile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,7 @@ fn check_starknet_dependency(
// I think we can get away with emitting false positives for users who write raw contracts
// without using Starknet code generators. Such people shouldn't do what they do 😁
if unit.main_component().target_kind() == TargetKind::STARKNET_CONTRACT
&& !has_starknet_plugin(db)
&& !has_starknet_plugin(db, unit.main_component())
{
ws.config().ui().warn(formatdoc! {
r#"
Expand Down

0 comments on commit 554003c

Please sign in to comment.