Skip to content

Commit a0b9bda

Browse files
committed
feat: compile scarb contracts
1 parent 7faf271 commit a0b9bda

File tree

1 file changed

+49
-5
lines changed

1 file changed

+49
-5
lines changed

src/bin/scarb-native-dump.rs

Lines changed: 49 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,15 @@ fn main() -> anyhow::Result<()> {
2828
let native_context = NativeContext::new();
2929
for package in metadata.packages.iter() {
3030
for target in &package.targets {
31-
let file_path = target_dir.join(format!("{}.sierra.json", target.name.clone()));
31+
let lib_file_path = target_dir.join(format!("{}.sierra.json", target.name.clone()));
32+
println!("Compiling {:?}", lib_file_path);
3233

33-
if file_path.exists() {
34+
if lib_file_path.exists() {
3435
let compiled = serde_json::from_str::<VersionedProgram>(
35-
&fs::read_to_string(file_path.clone())
36-
.with_context(|| format!("failed to read file: {file_path}"))?,
36+
&fs::read_to_string(lib_file_path.clone())
37+
.with_context(|| format!("failed to read file: {lib_file_path}"))?,
3738
)
38-
.with_context(|| format!("failed to deserialize compiled file: {file_path}"))?;
39+
.with_context(|| format!("failed to deserialize compiled file: {lib_file_path}"))?;
3940

4041
// Compile the sierra program into a MLIR module.
4142
let native_module = native_context
@@ -52,6 +53,49 @@ fn main() -> anyhow::Result<()> {
5253
&output_str,
5354
)?;
5455
}
56+
57+
let contract_files = fs::read_dir(&target_dir)
58+
.with_context(|| format!("failed to read directory: {}", target_dir.display()))?
59+
.filter_map(Result::ok)
60+
.filter(|entry| entry.file_type().map(|ft| ft.is_file()).unwrap_or(false))
61+
.filter_map(|entry| {
62+
let path = entry.path();
63+
path.extension()
64+
.and_then(|ext| ext.to_str())
65+
.filter(|&ext| ext == "json")
66+
.and_then(|_| path.file_name())
67+
.and_then(|name| name.to_str())
68+
.filter(|name| name.starts_with(&target.name) && name.ends_with(".contract_class.json"))
69+
.map(|_| path)
70+
});
71+
72+
73+
for contract_file_path in contract_files {
74+
let sierra_contract_class: cairo_lang_starknet_classes::contract_class::ContractClass = serde_json::from_str(
75+
&fs::read_to_string(&contract_file_path)
76+
.with_context(|| format!("failed to read file: {}", contract_file_path.display()))?,
77+
)
78+
.with_context(|| format!("failed to deserialize compiled file: {}", contract_file_path.display()))?;
79+
80+
let sierra_program = sierra_contract_class.extract_sierra_program()?;
81+
82+
// Compile the sierra program into a MLIR module.
83+
let native_module = native_context
84+
.compile(&sierra_program, false)
85+
.unwrap();
86+
87+
// Write the output.
88+
let output_str = native_module.module().as_operation().to_string_with_flags(
89+
OperationPrintingFlags::new().enable_debug_info(true, false),
90+
)?;
91+
92+
let output_file_name = contract_file_path.file_name().unwrap().to_str().unwrap()
93+
.replace(".json", ".mlir");
94+
fs::write(
95+
target_dir.join(output_file_name),
96+
&output_str,
97+
)?;
98+
}
5599
}
56100
}
57101

0 commit comments

Comments
 (0)