@@ -28,14 +28,15 @@ fn main() -> anyhow::Result<()> {
28
28
let native_context = NativeContext :: new ( ) ;
29
29
for package in metadata. packages . iter ( ) {
30
30
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) ;
32
33
33
- if file_path . exists ( ) {
34
+ if lib_file_path . exists ( ) {
34
35
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 }" ) ) ?,
37
38
)
38
- . with_context ( || format ! ( "failed to deserialize compiled file: {file_path }" ) ) ?;
39
+ . with_context ( || format ! ( "failed to deserialize compiled file: {lib_file_path }" ) ) ?;
39
40
40
41
// Compile the sierra program into a MLIR module.
41
42
let native_module = native_context
@@ -52,6 +53,49 @@ fn main() -> anyhow::Result<()> {
52
53
& output_str,
53
54
) ?;
54
55
}
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
+ }
55
99
}
56
100
}
57
101
0 commit comments