multiversx_sc_meta_lib/cli/
cli_args_build.rs

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
use clap::{ArgAction, Args};

use super::CliArgsToRaw;

#[derive(Clone, PartialEq, Eq, Debug, Args)]
pub struct BuildArgs {
    /// Require that the Cargo.lock in the wasm crates is up to date.
    #[arg(long = "locked", verbatim_doc_comment)]
    pub locked: bool,

    /// Adds debug symbols in the resulting WASM binary. Adds bloat, but helps with debugging. Do not use in production.
    #[arg(long = "wasm-symbols", verbatim_doc_comment)]
    pub wasm_symbols: bool,

    /// Overrides the main contract output name.
    #[arg(long = "wasm-name", verbatim_doc_comment)]
    pub wasm_name_override: Option<String>,

    /// Adds given suffix to all built contracts.
    #[arg(long = "wasm-suffix", verbatim_doc_comment)]
    pub wasm_name_suffix: Option<String>,

    /// True if wasm-opt should be used.
    #[arg(
        long = "no-wasm-opt",
        help = "Skips applying the wasm-opt tool after building the contracts.",
        action = ArgAction::SetFalse,
    )]
    pub wasm_opt: bool,

    /// Also generate a WAT file when building.
    #[arg(long = "wat", verbatim_doc_comment)]
    pub wat: bool,

    /// Also emit MIR files when building.
    #[arg(long = "mir", verbatim_doc_comment)]
    pub emit_mir: bool,

    /// Also emit LL (LLVM) files when building.
    #[arg(long = "llvm-ir", verbatim_doc_comment)]
    pub emit_llvm_ir: bool,

    #[arg(
        long = "no-imports",
        help = "Skips extracting the EI imports after building the contracts.",
        action = ArgAction::SetFalse,
    )]
    pub extract_imports: bool,

    /// For the wasm crate, allows specifying the target directory where the Rust compiler will build the intermediary files.
    /// Sharing the same target directory can speed up building multiple contract crates at once.
    /// Has alias `target-dir` for backwards compatibility.
    #[arg(long = "target-dir-wasm", alias = "target-dir", verbatim_doc_comment)]
    pub target_dir_wasm: Option<String>,

    /// Generate a twiggy top report after building.
    #[arg(long = "twiggy-top", verbatim_doc_comment)]
    pub twiggy_top: bool,

    /// Generate a twiggy paths report after building.
    #[arg(long = "twiggy-paths", verbatim_doc_comment)]
    pub twiggy_paths: bool,

    /// Generate a twiggy monos report after building.
    #[arg(long = "twiggy-monos", verbatim_doc_comment)]
    pub twiggy_monos: bool,

    /// Generate a twiggy dominators report after building.
    #[arg(long = "twiggy-dominators", verbatim_doc_comment)]
    pub twiggy_dominators: bool,

    /// Backwards compatibility with mxpy, delete when github actions are fixed.
    #[deprecated]
    #[arg(long = "target", verbatim_doc_comment)]
    pub target: Option<String>,

    /// Backwards compatibility with mxpy, delete when github actions are fixed.
    #[deprecated]
    #[arg(long, verbatim_doc_comment)]
    pub release: bool,

    /// Backwards compatibility with mxpy, delete when github actions are fixed.
    #[deprecated]
    #[arg(long = "out-dir", verbatim_doc_comment)]
    pub out_dir: Option<String>,
}

impl Default for BuildArgs {
    #[allow(deprecated)]
    fn default() -> Self {
        BuildArgs {
            locked: false,
            wasm_symbols: false,
            wasm_name_override: None,
            wasm_name_suffix: None,
            wasm_opt: true,
            wat: false,
            emit_mir: false,
            emit_llvm_ir: false,
            extract_imports: true,
            target_dir_wasm: None,
            twiggy_top: false,
            twiggy_paths: false,
            twiggy_monos: false,
            twiggy_dominators: false,
            target: None,
            release: false,
            out_dir: None,
        }
    }
}

impl BuildArgs {
    pub fn has_twiggy_call(&self) -> bool {
        self.twiggy_top || self.twiggy_paths || self.twiggy_monos || self.twiggy_dominators
    }
}

impl CliArgsToRaw for BuildArgs {
    fn to_raw(&self) -> Vec<String> {
        let mut raw = Vec::new();
        if self.locked {
            raw.push("--locked".to_string());
        }
        if self.wasm_symbols {
            raw.push("--wasm-symbols".to_string());
        }
        if let Some(wasm_name_override) = &self.wasm_name_override {
            raw.push("--wasm-name".to_string());
            raw.push(wasm_name_override.clone())
        }
        if let Some(wasm_name_suffix) = &self.wasm_name_suffix {
            raw.push("--wasm-suffix".to_string());
            raw.push(wasm_name_suffix.clone())
        }
        if !self.wasm_opt {
            raw.push("--no-wasm-opt".to_string());
        }
        if self.wat {
            raw.push("--wat".to_string());
        }
        if self.emit_mir {
            raw.push("--mir".to_string());
        }
        if self.emit_llvm_ir {
            raw.push("--llvm-ir".to_string());
        }
        if !self.extract_imports {
            raw.push("--no-imports".to_string());
        }
        if let Some(target_dir_wasm) = &self.target_dir_wasm {
            // not using --target-dir-wasm, for backward compatibility
            raw.push("--target-dir".to_string());
            raw.push(target_dir_wasm.clone());
        }
        if self.twiggy_top {
            raw.push("--twiggy-top".to_string());
        }
        if self.twiggy_paths {
            raw.push("--twiggy-paths".to_string());
        }
        if self.twiggy_monos {
            raw.push("--twiggy-monos".to_string());
        }
        if self.twiggy_dominators {
            raw.push("--twiggy-dominators".to_string());
        }
        raw
    }
}

#[derive(Clone, PartialEq, Eq, Debug, Args)]
pub struct BuildDbgArgs {
    /// For the wasm crate, allows specifying the target directory where the Rust compiler will build the intermediary files.
    /// Sharing the same target directory can speed up building multiple contract crates at once.
    /// Has alias `target-dir` for backwards compatibility.
    #[arg(long = "target-dir-wasm", alias = "target-dir", verbatim_doc_comment)]
    pub target_dir_wasm: Option<String>,

    /// Generate a twiggy top report after building.
    #[arg(long = "twiggy-top", verbatim_doc_comment)]
    pub twiggy_top: bool,

    /// Generate a twiggy paths report after building.
    #[arg(long = "twiggy-paths", verbatim_doc_comment)]
    pub twiggy_paths: bool,

    /// Generate a twiggy monos report after building.
    #[arg(long = "twiggy-monos", verbatim_doc_comment)]
    pub twiggy_monos: bool,

    /// Generate a twiggy dominators report after building.
    #[arg(long = "twiggy-dominators", verbatim_doc_comment)]
    pub twiggy_dominators: bool,
}

impl BuildDbgArgs {
    /// Base config when calling `cargo run build-dbg`, with no additional configs.
    pub fn into_build_args(self) -> BuildArgs {
        BuildArgs {
            wasm_symbols: true,
            wasm_name_override: None,
            wasm_name_suffix: Some("dbg".to_string()),
            wasm_opt: false,
            wat: true,
            extract_imports: false,
            target_dir_wasm: self.target_dir_wasm,
            twiggy_top: self.twiggy_top,
            twiggy_paths: self.twiggy_paths,
            twiggy_monos: self.twiggy_monos,
            twiggy_dominators: self.twiggy_dominators,
            ..BuildArgs::default()
        }
    }
}

impl CliArgsToRaw for BuildDbgArgs {
    fn to_raw(&self) -> Vec<String> {
        let mut raw = Vec::new();
        if let Some(target_dir_wasm) = &self.target_dir_wasm {
            // not using --target-dir-wasm, for backward compatibility
            raw.push("--target-dir".to_string());
            raw.push(target_dir_wasm.clone());
        }
        if self.twiggy_top {
            raw.push("--twiggy-top".to_string());
        }
        if self.twiggy_paths {
            raw.push("--twiggy-paths".to_string());
        }
        if self.twiggy_monos {
            raw.push("--twiggy-monos".to_string());
        }
        if self.twiggy_dominators {
            raw.push("--twiggy-dominators".to_string());
        }
        raw
    }
}

#[derive(Clone, PartialEq, Eq, Debug, Args)]
pub struct TwiggyArgs {
    /// For the wasm crate, allows specifying the target directory where the Rust compiler will build the intermediary files.
    /// Sharing the same target directory can speed up building multiple contract crates at once.
    /// Has alias `target-dir` for backwards compatibility.
    #[arg(long = "target-dir-wasm", alias = "target-dir", verbatim_doc_comment)]
    pub target_dir_wasm: Option<String>,
}

impl TwiggyArgs {
    pub fn into_build_args(self) -> BuildArgs {
        BuildDbgArgs {
            target_dir_wasm: self.target_dir_wasm,
            twiggy_top: true,
            twiggy_paths: true,
            twiggy_monos: true,
            twiggy_dominators: true,
        }
        .into_build_args()
    }
}

impl CliArgsToRaw for TwiggyArgs {
    fn to_raw(&self) -> Vec<String> {
        let mut raw = Vec::new();
        if let Some(target_dir) = &self.target_dir_wasm {
            // not using --target-dir-wasm, for backward compatibility
            raw.push("--target-dir".to_string());
            raw.push(target_dir.clone());
        }
        raw
    }
}