-
Notifications
You must be signed in to change notification settings - Fork 206
Expand file tree
/
Copy pathbuild.rs
More file actions
316 lines (264 loc) · 10 KB
/
build.rs
File metadata and controls
316 lines (264 loc) · 10 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
use std::cmp::Reverse;
use anyhow::Result;
use clap::{Args, Parser};
use colored::{ColoredString, Colorize};
use dojo_bindgen::{BuiltinPlugins, PluginManager};
use dojo_world::local::{ResourceLocal, WorldLocal};
use dojo_world::ResourceType;
use scarb_interop::{self, Scarb};
use scarb_metadata::Metadata;
use scarb_metadata_ext::MetadataDojoExt;
use tabled::settings::Style;
use tabled::{Table, Tabled};
use crate::features::FeaturesSpec;
#[derive(Debug, Clone, Args)]
pub struct BuildArgs {
#[arg(long)]
#[arg(help = "Generate Typescript bindings.")]
pub typescript: bool,
#[arg(long)]
#[arg(help = "Generate Recs bindings.")]
pub recs: bool,
#[arg(long)]
#[arg(help = "Generate Unity bindings.")]
pub unity: bool,
#[arg(long)]
#[arg(help = "Generate Unreal Engine bindings.")]
pub unrealengine: bool,
#[arg(long)]
#[arg(help = "Output directory.", default_value = "bindings")]
pub bindings_output: String,
/// Disable Scarb warnings.
#[arg(long)]
#[arg(help = "Scarb warnings are not shown in the output.")]
pub no_scarb_warnings: bool,
/// Specify packages to build.
/// Packages to run this command on, can be a concrete package name (`foobar`) or
/// a prefix glob (`foo*`).
#[arg(short, long, value_delimiter = ',', env = "SCARB_PACKAGES_FILTER")]
pub packages: Vec<String>,
/// Specify the features to activate.
#[command(flatten)]
pub features: FeaturesSpec,
/// Display statistics about the compiled contracts.
#[command(flatten)]
pub stats: StatOptions,
}
#[derive(Debug, Clone, Args, Default, PartialEq)]
#[command(next_help_heading = "Statistics options")]
pub struct StatOptions {
#[arg(long = "stats.by-tag")]
#[arg(help = "Sort the stats by tag.")]
#[arg(conflicts_with_all = ["sort_by_sierra_mb", "sort_by_sierra_felts", "sort_by_casm_felts"])]
#[arg(default_value_t = false)]
pub sort_by_tag: bool,
#[arg(long = "stats.by-sierra-mb")]
#[arg(help = "Sort the stats by Sierra file size in MB.")]
#[arg(conflicts_with_all = ["sort_by_tag", "sort_by_sierra_felts", "sort_by_casm_felts"])]
#[arg(default_value_t = false)]
pub sort_by_sierra_mb: bool,
#[arg(long = "stats.by-sierra-felts")]
#[arg(help = "Sort the stats by Sierra program size in felts.")]
#[arg(conflicts_with_all = ["sort_by_tag", "sort_by_sierra_mb", "sort_by_casm_felts"])]
#[arg(default_value_t = false)]
pub sort_by_sierra_felts: bool,
#[arg(long = "stats.by-casm-felts")]
#[arg(help = "Sort the stats by Casm bytecode size in felts.")]
#[arg(conflicts_with_all = ["sort_by_tag", "sort_by_sierra_mb", "sort_by_sierra_felts"])]
#[arg(default_value_t = false)]
pub sort_by_casm_felts: bool,
}
impl BuildArgs {
pub async fn run(self, scarb_metadata: &Metadata) -> Result<()> {
scarb_metadata.clean_dir_profile();
let other_args =
if self.no_scarb_warnings { vec!["--verbosity", "no-warnings"] } else { vec![] };
Scarb::build(
&scarb_metadata.workspace.manifest_path,
scarb_metadata.current_profile.as_str(),
&self.packages.join(","),
self.features.into(),
other_args,
)?;
let mut builtin_plugins = vec![];
if self.typescript {
builtin_plugins.push(BuiltinPlugins::Typescript);
}
if self.recs {
builtin_plugins.push(BuiltinPlugins::Recs);
}
if self.unity {
builtin_plugins.push(BuiltinPlugins::Unity);
}
if self.unrealengine {
builtin_plugins.push(BuiltinPlugins::UnrealEngine);
}
// Custom plugins are always empty for now.
let bindgen = PluginManager {
profile_name: scarb_metadata.current_profile.to_string(),
root_package_name: scarb_metadata.workspace_package_name()?,
output_path: self.bindings_output.into(),
manifest_path: scarb_metadata.dojo_manifest_path_profile(),
plugins: vec![],
builtin_plugins,
};
// TODO: check about the skip migration as now we process the metadata
// directly during the compilation to get the data we need from it.
bindgen.generate(None).await?;
// During the build, the class hashes are not used. Set to true as default since it
// will be the standard once 0.14.1 hits mainnet.
let use_blake2s_casm_class_hash = true;
if self.stats != StatOptions::default() {
let world = WorldLocal::from_directory(
scarb_metadata.target_dir_profile(),
&scarb_metadata.current_profile,
scarb_metadata.load_dojo_profile_config().unwrap(),
use_blake2s_casm_class_hash,
)?;
let world_stat = world.to_stat_item();
let mut stats = vec![world_stat];
for r in world.resources.values() {
if r.resource_type() != ResourceType::Namespace {
stats.push(r.to_stat_item());
}
}
if self.stats.sort_by_tag {
stats.sort_by_key(|s| s.tag.clone());
} else if self.stats.sort_by_sierra_mb {
stats.sort_by_key(|s| Reverse(s.sierra_file_size));
} else if self.stats.sort_by_sierra_felts {
stats.sort_by_key(|s| Reverse(s.sierra_program_size));
} else if self.stats.sort_by_casm_felts {
stats.sort_by_key(|s| Reverse(s.casm_bytecode_size));
}
let mut table = Table::new(stats.iter().map(StatItemPrint::from).collect::<Vec<_>>());
table.with(Style::psql());
println!();
println!("{table}");
if stats.iter().all(|s| s.casm_bytecode_size == 0) {
println!(
"{}",
r#"
All the casm bytecode sizes are 0, did you forget to enable casm compilation?
To enable casm compilation, add the following to your Scarb.toml:
[[target.starknet-contract]]
sierra = true
casm = true
"#
.bright_yellow()
);
}
println!(
"\nRefer to https://docs.starknet.io/tools/limits-and-triggers/ for more \
information about the public networks limits."
);
}
Ok(())
}
}
impl Default for BuildArgs {
fn default() -> Self {
// use the clap defaults
let features = FeaturesSpec::parse_from([""]);
Self {
features,
packages: vec![],
typescript: false,
recs: false,
unity: false,
unrealengine: false,
bindings_output: "bindings".to_string(),
stats: StatOptions::default(),
no_scarb_warnings: false,
}
}
}
trait ContractStats {
fn to_stat_item(&self) -> StatItem;
fn sierra_file_size(&self) -> Result<usize>;
fn sierra_program_felt_size(&self) -> usize;
fn casm_program_felt_size(&self) -> usize;
}
#[derive(Debug, Tabled)]
struct StatItemPrint {
#[tabled(rename = "")]
tag: ColoredString,
#[tabled(rename = "Sierra size (byte)")]
sierra_file_size: ColoredString,
#[tabled(rename = "Sierra felts")]
sierra_program_size: ColoredString,
#[tabled(rename = "Casm felts")]
casm_bytecode_size: ColoredString,
}
impl From<&StatItem> for StatItemPrint {
fn from(item: &StatItem) -> Self {
const MAX_SIERRA_SIZE_BYTES: usize = 4_089_446;
const MAX_CASM_FELTS: usize = 81_290;
let tag = if item.tag == "world" {
"World".to_string().bright_magenta()
} else {
item.tag.to_string().bright_blue()
};
let sierra_file_size = if item.sierra_file_size > MAX_SIERRA_SIZE_BYTES {
item.sierra_file_size.to_string().bright_red()
} else {
item.sierra_file_size.to_string().bright_green()
};
let sierra_program_size = item.sierra_program_size.to_string().bright_black();
let casm_bytecode_size = if item.casm_bytecode_size > MAX_CASM_FELTS {
item.casm_bytecode_size.to_string().bright_red()
} else {
item.casm_bytecode_size.to_string().bright_green()
};
Self { tag, sierra_file_size, sierra_program_size, casm_bytecode_size }
}
}
#[derive(Debug)]
struct StatItem {
tag: String,
sierra_file_size: usize,
sierra_program_size: usize,
casm_bytecode_size: usize,
}
impl ContractStats for ResourceLocal {
fn to_stat_item(&self) -> StatItem {
StatItem {
tag: self.tag(),
sierra_file_size: self.sierra_file_size().unwrap(),
sierra_program_size: self.sierra_program_felt_size(),
casm_bytecode_size: self.casm_program_felt_size(),
}
}
fn sierra_file_size(&self) -> Result<usize> {
// Easiest way to get the file size is by reserializing into the original json
// the class file.
Ok(serde_json::to_string(&self.common().class)?.len())
}
fn sierra_program_felt_size(&self) -> usize {
self.common().class.sierra_program.len()
}
fn casm_program_felt_size(&self) -> usize {
self.common().casm_class.as_ref().map_or(0, |casm| casm.bytecode.len())
}
}
impl ContractStats for WorldLocal {
fn to_stat_item(&self) -> StatItem {
StatItem {
tag: "world".to_string(),
sierra_file_size: self.sierra_file_size().unwrap(),
sierra_program_size: self.sierra_program_felt_size(),
casm_bytecode_size: self.casm_program_felt_size(),
}
}
fn sierra_file_size(&self) -> Result<usize> {
// Easiest way to get the file size is by reserializing into the original json
// the class file.
Ok(serde_json::to_string(&self.class)?.len())
}
fn sierra_program_felt_size(&self) -> usize {
self.class.sierra_program.len()
}
fn casm_program_felt_size(&self) -> usize {
self.casm_class.as_ref().map_or(0, |casm| casm.bytecode.len())
}
}