Skip to content

Commit

Permalink
Merge pull request #80 from software-mansion-labs/revert-77-1737-make…
Browse files Browse the repository at this point in the history
…-scarb-integration-independent-of-cairo_projecttoml

Revert "Deprecate `cairo_project.toml` in favour of `Scarb.toml`"
  • Loading branch information
MaksymilianDemitraszek authored May 30, 2023
2 parents a8ca26f + 47775ec commit 7f7539e
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 107 deletions.
2 changes: 1 addition & 1 deletion crates/cairo-lang-compiler/src/project.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ pub enum ProjectError {

/// Setup to 'db' to compile the file at the given path.
/// Returns the id of the generated crate.
pub fn setup_single_file_project(
fn setup_single_file_project(
db: &mut dyn SemanticGroup,
path: &Path,
) -> Result<CrateId, ProjectError> {
Expand Down
75 changes: 1 addition & 74 deletions crates/cairo-lang-protostar/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,37 +1,13 @@
use std::collections::HashMap;
use std::fs;
use std::path::{Path, PathBuf};
use std::sync::Arc;

use anyhow::{Context, Result};
use cairo_lang_compiler::db::RootDatabase;
use cairo_lang_compiler::project::{
get_main_crate_ids_from_project, setup_single_file_project,
update_crate_roots_from_project_config, ProjectError,
};
use cairo_lang_compiler::CompilerConfig;
use cairo_lang_filesystem::ids::CrateId;
use cairo_lang_project::{DeserializationError, ProjectConfig, ProjectConfigContent};
use cairo_lang_semantic::db::SemanticGroup;
use anyhow::Context;
use cairo_lang_sierra::program::Program;
use cairo_lang_sierra::ProgramParser;
use cairo_lang_starknet::contract_class::{compile_contract_in_prepared_db, ContractClass};
use cairo_lang_starknet::plugin::StarkNetPlugin;
use casm_generator::{SierraCasmGenerator, TestConfig};
use smol_str::SmolStr;

pub mod casm_generator;
pub mod test_collector;

pub fn build_project_config(
source_root: &Path,
crate_name: &str,
) -> Result<ProjectConfig, DeserializationError> {
let base_path: PathBuf = source_root.to_str().ok_or(DeserializationError::PathError)?.into();
let crate_roots = HashMap::from([(SmolStr::from(crate_name), base_path.clone())]);
Ok(ProjectConfig { base_path, content: ProjectConfigContent { crate_roots }, corelib: None })
}

pub fn build_protostar_casm(
collected_tests: &Vec<TestConfig>,
sierra_contents: &str,
Expand Down Expand Up @@ -60,52 +36,3 @@ pub fn build_protostar_casm_from_sierra(
}
Ok(Some(casm_contents))
}

pub fn setup_project_without_cairo_project_toml(
db: &mut dyn SemanticGroup,
path: &Path,
crate_name: &str,
) -> Result<Vec<CrateId>, ProjectError> {
if path.is_dir() {
match build_project_config(path, crate_name) {
Ok(config) => {
let main_crate_ids = get_main_crate_ids_from_project(db, &config);
update_crate_roots_from_project_config(db, config);
Ok(main_crate_ids)
}
_ => Err(ProjectError::LoadProjectError),
}
} else {
Ok(vec![setup_single_file_project(db, path)?])
}
}

pub fn compile_from_resolved_dependencies(
input_path: &str,
contract_path: Option<&str>,
compiler_config: CompilerConfig<'_>,
maybe_cairo_paths: Option<Vec<(&str, &str)>>,
) -> Result<ContractClass> {
let mut db = RootDatabase::builder()
.detect_corelib()
.with_semantic_plugin(Arc::new(StarkNetPlugin::default()))
.build()?;

let cairo_paths = match maybe_cairo_paths {
Some(paths) => paths,
None => vec![],
};
let main_crate_name = match cairo_paths.iter().find(|(path, _crate_name)| **path == *input_path)
{
Some((_crate_path, crate_name)) => crate_name,
None => "",
};

let main_crate_ids =
setup_project_without_cairo_project_toml(&mut db, Path::new(&input_path), main_crate_name)?;
for (cairo_path, crate_name) in cairo_paths {
setup_project_without_cairo_project_toml(&mut db, Path::new(cairo_path), crate_name)?;
}

compile_contract_in_prepared_db(&mut db, contract_path, main_crate_ids, compiler_config)
}
27 changes: 9 additions & 18 deletions crates/cairo-lang-protostar/src/test_collector/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use anyhow::{anyhow, Context, Result};
use cairo_felt::Felt252;
use cairo_lang_compiler::db::RootDatabase;
use cairo_lang_compiler::diagnostics::DiagnosticsReporter;
use cairo_lang_compiler::project::setup_project;
use cairo_lang_debug::DebugWithDb;
use cairo_lang_defs::ids::{FreeFunctionId, FunctionWithBodyId, ModuleItemId};
use cairo_lang_defs::plugin::PluginDiagnostic;
Expand All @@ -31,7 +32,6 @@ use itertools::Itertools;
use num_traits::ToPrimitive;

use crate::casm_generator::{SierraCasmGenerator, TestConfig as TestConfigInternal};
use crate::setup_project_without_cairo_project_toml;

/// Expectation for a panic case.
pub enum PanicExpectation {
Expand Down Expand Up @@ -214,7 +214,7 @@ fn extract_panic_values(db: &dyn SyntaxGroup, attr: &Attribute) -> Option<Vec<Fe
pub fn collect_tests(
input_path: &String,
output_path: Option<&String>,
maybe_cairo_paths: Option<Vec<(&String, &String)>>,
maybe_cairo_paths: Option<Vec<&String>>,
maybe_builtins: Option<Vec<&String>>,
) -> Result<(Program, Vec<TestConfigInternal>)> {
// code taken from crates/cairo-lang-test-runner/src/lib.rs
Expand All @@ -227,23 +227,14 @@ pub fn collect_tests(
b.build()?
};

let cairo_paths = match maybe_cairo_paths {
Some(paths) => paths,
None => vec![],
};
let main_crate_name = match cairo_paths.iter().find(|(path, _crate_name)| **path == *input_path)
{
Some((_crate_path, crate_name)) => crate_name,
None => "",
};
let main_crate_ids = setup_project(db, Path::new(&input_path))
.with_context(|| format!("Failed to setup project for path({})", input_path))?;

let main_crate_ids =
setup_project_without_cairo_project_toml(db, Path::new(&input_path), main_crate_name)
.with_context(|| format!("Failed to setup project for path({})", input_path))?;

for (cairo_path, crate_name) in cairo_paths {
setup_project_without_cairo_project_toml(db, Path::new(cairo_path), crate_name)
.with_context(|| format!("Failed to add linked library ({})", input_path))?;
if let Some(cairo_paths) = maybe_cairo_paths {
for cairo_path in cairo_paths {
setup_project(db, Path::new(cairo_path))
.with_context(|| format!("Failed to add linked library ({})", input_path))?;
}
}

if DiagnosticsReporter::stderr().check(db) {
Expand Down
24 changes: 11 additions & 13 deletions crates/cairo-lang-python-bindings/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
use std::fs;
use std::path::Path;
use std::path::{Path, PathBuf};

use anyhow::Context;
use cairo_lang_compiler::CompilerConfig;
use cairo_lang_protostar::build_protostar_casm_from_sierra;
use cairo_lang_protostar::casm_generator::TestConfig;
use cairo_lang_protostar::test_collector::collect_tests as internal_collect_tests;
use cairo_lang_protostar::{build_protostar_casm_from_sierra, compile_from_resolved_dependencies};
use cairo_lang_starknet::casm_contract_class::CasmContractClass;
use cairo_lang_starknet::contract_class::ContractClass;
use cairo_lang_starknet::contract_class::{compile_path as compile_starknet, ContractClass};
use pyo3::exceptions::RuntimeError;
use pyo3::prelude::*;
use pyo3::wrap_pyfunction;
Expand All @@ -30,7 +30,7 @@ fn ensure_path_is_dir(path_str: &str) -> Result<(), anyhow::Error> {
fn compile_starknet_contract_to_sierra_from_path(
input_path: &str,
output_path: Option<&str>,
maybe_cairo_paths: Option<Vec<(&str, &str)>>,
maybe_cairo_paths: Option<Vec<&str>>,
) -> PyResult<String> {
ensure_path_is_dir(input_path)
.map_err(|e| PyErr::new::<RuntimeError, _>(format!("{:?}", e)))?;
Expand All @@ -47,10 +47,10 @@ fn compile_starknet_contract_to_sierra_from_path(

fn starknet_cairo_to_sierra(
input_path: &str,
maybe_cairo_paths: Option<Vec<(&str, &str)>>,
maybe_cairo_paths: Option<Vec<&str>>,
) -> Result<String, anyhow::Error> {
let contract = compile_from_resolved_dependencies(
input_path,
let contract = compile_starknet(
&PathBuf::from(input_path),
None,
CompilerConfig { replace_ids: true, ..CompilerConfig::default() },
maybe_cairo_paths,
Expand All @@ -65,7 +65,7 @@ fn starknet_cairo_to_sierra(
fn compile_starknet_contract_to_casm_from_path(
input_path: &str,
output_path: Option<&str>,
maybe_cairo_paths: Option<Vec<(&str, &str)>>,
maybe_cairo_paths: Option<Vec<&str>>,
) -> PyResult<String> {
ensure_path_is_dir(input_path)
.map_err(|e| PyErr::new::<RuntimeError, _>(format!("{:?}", e)))?;
Expand Down Expand Up @@ -95,7 +95,7 @@ fn starknet_sierra_to_casm(sierra: &str) -> Result<String, anyhow::Error> {

fn starknet_cairo_to_casm(
input_path: &str,
maybe_cairo_paths: Option<Vec<(&str, &str)>>,
maybe_cairo_paths: Option<Vec<&str>>,
) -> Result<String, anyhow::Error> {
let sierra = starknet_cairo_to_sierra(input_path, maybe_cairo_paths)?;
starknet_sierra_to_casm(&sierra)
Expand Down Expand Up @@ -131,15 +131,13 @@ fn compile_starknet_contract_sierra_to_casm_from_sierra_code(
fn collect_tests(
input_path: String,
output_path: Option<String>,
maybe_cairo_paths: Option<Vec<(String, String)>>,
maybe_cairo_paths: Option<Vec<String>>,
maybe_builtins: Option<Vec<String>>,
) -> PyResult<(String, Vec<CollectedTest>)> {
let (sierra_program, collected) = internal_collect_tests(
&input_path,
output_path.as_ref(),
maybe_cairo_paths
.as_ref()
.map(|a| a.iter().map(|(b, c)| (b, c)).collect::<Vec<(&String, &String)>>()),
maybe_cairo_paths.as_ref().map(|a| a.iter().map(|b| b).collect::<Vec<&String>>()),
maybe_builtins.as_ref().map(|a| a.iter().map(|b| b).collect::<Vec<&String>>()),
)
.map_err(|e| {
Expand Down
2 changes: 1 addition & 1 deletion crates/cairo-lang-starknet/src/contract_class.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ pub fn compile_path(
/// Runs StarkNet contract compiler on the specified contract.
/// If no contract was specified, verify that there is only one.
/// Otherwise, return an error.
pub fn compile_contract_in_prepared_db(
fn compile_contract_in_prepared_db(
db: &mut RootDatabase,
contract_path: Option<&str>,
main_crate_ids: Vec<CrateId>,
Expand Down

0 comments on commit 7f7539e

Please sign in to comment.