From c6eb1d500ee24470ded335cd5363bc4aafe6ce94 Mon Sep 17 00:00:00 2001 From: maciektr Date: Thu, 23 May 2024 16:21:33 +0200 Subject: [PATCH] Resolve to grouped compilation units (#1313) commit-id:d7615ff7 --- extensions/scarb-cairo-test/tests/build.rs | 10 ++--- scarb/src/ops/compile.rs | 7 ++- scarb/src/ops/resolve.rs | 52 +++++++++++++++++----- 3 files changed, 51 insertions(+), 18 deletions(-) diff --git a/extensions/scarb-cairo-test/tests/build.rs b/extensions/scarb-cairo-test/tests/build.rs index f90920f88..89854fa1e 100644 --- a/extensions/scarb-cairo-test/tests/build.rs +++ b/extensions/scarb-cairo-test/tests/build.rs @@ -229,13 +229,13 @@ fn integration_tests() { .success() .stdout_matches(indoc! {r#" [..]Compiling test(hello_unittest) hello v1.0.0 ([..]Scarb.toml) - [..]Compiling test(hello_integrationtest) hello_a v1.0.0 ([..]Scarb.toml) - [..]Compiling test(hello_integrationtest) hello_b v1.0.0 ([..]Scarb.toml) + [..]Compiling test(hello_integrationtest) hello_integrationtest v1.0.0 ([..]Scarb.toml) [..]Finished release target(s) in [..] testing hello ... - running 1 test - test hello_b::b::tests::it_works ... ok (gas usage est.: 42440) - test result: ok. 1 passed; 0 failed; 0 ignored; 0 filtered out; + running 2 tests + test hello_integrationtest::[..]::tests::it_works ... ok (gas usage est.: 42440) + test hello_integrationtest::[..]::tests::it_works ... ok (gas usage est.: 42440) + test result: ok. 2 passed; 0 failed; 0 ignored; 0 filtered out; running 1 test test hello::tests::it_works ... ok (gas usage est.: 42440) diff --git a/scarb/src/ops/compile.rs b/scarb/src/ops/compile.rs index e8a47c8e1..39b9891f2 100644 --- a/scarb/src/ops/compile.rs +++ b/scarb/src/ops/compile.rs @@ -89,7 +89,12 @@ where .manifest .targets .iter() - .map(|t| package.id.for_test_target(t.name.clone())) + .filter(|t| t.is_test()) + .map(|t| { + package + .id + .for_test_target(t.group_id.clone().unwrap_or(t.name.clone())) + }) .collect(); result.push(package_id); result diff --git a/scarb/src/ops/resolve.rs b/scarb/src/ops/resolve.rs index c41222883..828556104 100644 --- a/scarb/src/ops/resolve.rs +++ b/scarb/src/ops/resolve.rs @@ -204,39 +204,68 @@ fn generate_cairo_compilation_units( ) -> Result> { let profile = ws.current_profile()?; let mut solution = PackageSolutionCollector::new(member, resolve, ws); - member + let grouped = member .manifest .targets .iter() - .sorted_by_key(|target| target.kind.clone()) + .filter(|target| target.group_id.is_some()) + .group_by(|target| target.group_id.clone()) + .into_iter() + .map(|(group_id, group)| (group_id, group.collect_vec())) + .sorted_by_key(|(_, group)| group[0].kind.clone()) + .map(|(_group_id, group)| { + let group = group.into_iter().cloned().collect_vec(); + Ok(CompilationUnit::Cairo(cairo_compilation_unit_for_target( + group, + member, + profile.clone(), + enabled_features, + &mut solution, + )?)) + }) + .collect::>>()?; + let result = member + .manifest + .targets + .iter() + .filter(|target| target.group_id.is_none()) .map(|member_target| { Ok(CompilationUnit::Cairo(cairo_compilation_unit_for_target( - member_target, + vec![member_target.clone()], member, profile.clone(), enabled_features, &mut solution, )?)) }) - .collect::>>() + .collect::>>()? + .into_iter() + .chain(grouped) + .collect(); + Ok(result) } fn cairo_compilation_unit_for_target( - member_target: &Target, + member_targets: Vec, member: &Package, profile: Profile, enabled_features: &FeaturesOpts, solution: &mut PackageSolutionCollector<'_>, ) -> Result { + let member_target = member_targets.first().cloned().unwrap(); solution.collect(&member_target.kind)?; let packages = solution.packages.as_ref().unwrap(); let cairo_plugins = solution.cairo_plugins.as_ref().unwrap(); - let cfg_set = build_cfg_set(member_target); + let cfg_set = build_cfg_set(&member_target); let props: TestTargetProps = member_target.props()?; let is_integration_test = props.test_type == TestTargetType::Integration; - let test_package_id = member.id.for_test_target(member_target.name.clone()); + let name = member_target + .group_id + .clone() + .unwrap_or(member_target.name.clone()); + let test_package_id = member.id.for_test_target(name); let mut components: Vec = packages .iter() @@ -245,14 +274,13 @@ fn cairo_compilation_unit_for_target( // If this is this compilation's unit main package, then use the target we are // building. Otherwise, assume library target for all dependency packages, // because that's what it is for. - let target = if package.id == member.id { - member_target + let targets = if package.id == member.id { + member_targets.clone() } else { // We can safely unwrap here, because compilation unit generator ensures // that all dependencies have library target. - package.fetch_target(&TargetKind::LIB).unwrap() + vec![package.fetch_target(&TargetKind::LIB).unwrap().clone()] }; - let target = target.clone(); // For integration tests target, rewrite package with prefixed name. // This allows integration test code to reference main package as dependency. @@ -290,7 +318,7 @@ fn cairo_compilation_unit_for_target( } }; - CompilationUnitComponent::try_new(package, vec![target], cfg_set) + CompilationUnitComponent::try_new(package, targets, cfg_set) }) .collect::>()?;