Skip to content

Commit

Permalink
Add allowed prebuilts list
Browse files Browse the repository at this point in the history
commit-id:66715aa1
  • Loading branch information
maciektr committed Dec 16, 2024
1 parent e028d40 commit 0b9657c
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 1 deletion.
1 change: 1 addition & 0 deletions scarb/src/compiler/compilation_unit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ pub struct CompilationUnitCairoPlugin {
/// The Scarb plugin [`Package`] to load.
pub package: Package,
pub builtin: bool,
pub prebuilt_allowed: bool,
}

/// Unique identifier of the compilation unit component.
Expand Down
6 changes: 6 additions & 0 deletions scarb/src/core/manifest/toml_manifest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -357,6 +357,12 @@ impl DefaultForProfile for TomlProfile {
}
}

#[derive(Debug, Default, Deserialize, Serialize, Clone)]
#[serde(rename_all = "kebab-case")]
pub struct TomlToolScarbMetadata {
pub allow_prebuilt_macros: Option<Vec<String>>,
}

impl TomlManifest {
pub fn read_from_path(path: &Utf8Path) -> Result<Self> {
let contents = fs::read_to_string(path)
Expand Down
8 changes: 7 additions & 1 deletion scarb/src/core/package/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ pub use name::*;
use scarb_ui::args::WithManifestPath;

use crate::core::manifest::Manifest;
use crate::core::{Target, TargetKind};
use crate::core::{Target, TargetKind, TomlToolScarbMetadata};
use crate::internal::fsx;

mod id;
Expand Down Expand Up @@ -137,6 +137,12 @@ impl Package {
Ok(structured)
}

pub fn scarb_tool_metadata(&self) -> Result<TomlToolScarbMetadata> {
let raw_metadata = self.fetch_tool_metadata("scarb")?.clone();
let metadata: TomlToolScarbMetadata = toml::Value::try_into(raw_metadata)?;
Ok(metadata)
}

pub fn manifest_mut(&mut self) -> &mut Manifest {
&mut Arc::make_mut(&mut self.0).manifest
}
Expand Down
35 changes: 35 additions & 0 deletions scarb/src/ops/resolve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,30 @@ async fn collect_packages_from_resolve_graph(
Ok(packages)
}

#[derive(Debug, Default)]
struct AllowedPrebuiltFilter(HashSet<PackageName>);

impl AllowedPrebuiltFilter {
pub fn new(package: &Package) -> Result<Self> {
let metadata = package.scarb_tool_metadata()?;
let allowed = metadata.allow_prebuilt_macros.unwrap_or_default();
let allowed = allowed
.into_iter()
.filter_map(|name| PackageName::try_new(name).ok())
.collect();
Ok(Self(allowed))
}

pub fn allow(&mut self, deps: &[PackageId]) {
self.0.extend(deps.iter().map(|id| id.name.clone()));
}

/// Check if package can be prebuilt.
pub fn check(&self, package: &Package) -> bool {
self.0.contains(&package.id.name)
}
}

#[tracing::instrument(skip_all, level = "debug")]
pub fn generate_compilation_units(
resolve: &WorkspaceResolve,
Expand Down Expand Up @@ -594,15 +618,26 @@ impl<'a> PackageSolutionCollector<'a> {
));
}

let mut allowed_prebuilds = AllowedPrebuiltFilter::new(self.member)?;
let cairo_plugins = cairo_plugins
.into_iter()
.map(|package| {
let prebuilt_allowed = allowed_prebuilds.check(&package);
if prebuilt_allowed {
allowed_prebuilds.allow(
&self
.resolve
.resolve
.package_dependencies_for_target_kind(package.id, target_kind),
);
}
// We can safely unwrap as all packages with `PackageClass::CairoPlugin` must define plugin target.
let target = package.target(&TargetKind::CAIRO_PLUGIN).unwrap();
let props: CairoPluginProps = target.props()?;
Ok(CompilationUnitCairoPlugin::builder()
.package(package)
.builtin(props.builtin)
.prebuilt_allowed(prebuilt_allowed)
.build())
})
.collect::<Result<Vec<_>>>()?;
Expand Down

0 comments on commit 0b9657c

Please sign in to comment.