Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 20 additions & 4 deletions FULL_HELP_DOCS.md
Original file line number Diff line number Diff line change
Expand Up @@ -382,7 +382,11 @@ To view the commands that will be executed, without executing them, use the --pr
If ommitted, wasm files are written only to the cargo target directory.

- `--locked` — Assert that `Cargo.lock` will remain unchanged
- `--optimize` — Optimize the generated wasm
- `--optimize <OPTIMIZE>` — Optimize the generated wasm. Enabled by default; pass `--optimize=false` to disable

Default value: `true`

Possible values: `true`, `false`

###### **Other:**

Expand Down Expand Up @@ -474,7 +478,11 @@ Deploy a wasm contract
Default value: `false`

- `--alias <ALIAS>` — The alias that will be used to save the contract's id. Whenever used, `--alias` will always overwrite the existing contract id configuration without asking for confirmation
- `--optimize` — Optimize the generated wasm
- `--optimize <OPTIMIZE>` — Optimize the generated wasm. Enabled by default; pass `--optimize=false` to disable

Default value: `true`

Possible values: `true`, `false`

###### **RPC Options:**

Expand Down Expand Up @@ -836,7 +844,11 @@ Install a WASM file to the ledger without creating a contract instance

Default value: `false`

- `--optimize` — Optimize the generated wasm
- `--optimize <OPTIMIZE>` — Optimize the generated wasm. Enabled by default; pass `--optimize=false` to disable

Default value: `true`

Possible values: `true`, `false`

###### **RPC Options:**

Expand Down Expand Up @@ -888,7 +900,11 @@ Install a WASM file to the ledger without creating a contract instance

Default value: `false`

- `--optimize` — Optimize the generated wasm
- `--optimize <OPTIMIZE>` — Optimize the generated wasm. Enabled by default; pass `--optimize=false` to disable

Default value: `true`

Possible values: `true`, `false`

###### **RPC Options:**

Expand Down
4 changes: 2 additions & 2 deletions cmd/soroban-cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ doctest = false

[features]
default = ["additional-libs"]
additional-libs = ["dep:wasm-opt", "dep:keyring", "dep:stellar-ledger"]
additional-libs = ["dep:keyring", "dep:stellar-ledger"]
Comment thread
fnando marked this conversation as resolved.
Outdated
emulator-tests = ["stellar-ledger/emulator-tests"]

[dependencies]
Expand Down Expand Up @@ -80,7 +80,7 @@ reqwest = { version = "0.12.7", default-features = false, features = [
"stream",
] }
regex = "1.6.0"
wasm-opt = { version = "0.116.1", optional = true }
wasm-opt = { version = "0.116.1" }
chrono = { version = "0.4.27", features = ["serde"] }
rpassword = "7.2.0"
toml = { workspace = true }
Expand Down
39 changes: 21 additions & 18 deletions cmd/soroban-cli/src/commands/contract/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,8 @@ use std::{
};
use stellar_xdr::curr::{Limited, Limits, ScMetaEntry, ScMetaV0, StringM, WriteXdr};

#[cfg(feature = "additional-libs")]
use crate::commands::contract::optimize;
use crate::{
commands::{global, version},
commands::{contract::optimize, global, version},
print::Print,
wasm,
};
Expand Down Expand Up @@ -101,18 +99,34 @@ pub struct Cmd {
}

/// Shared build options for meta and optimization, reused by deploy and upload.
#[derive(Parser, Debug, Clone, Default)]
#[derive(Parser, Debug, Clone)]
pub struct BuildArgs {
/// Add key-value to contract meta (adds the meta to the `contractmetav0` custom section)
#[arg(long, num_args=1, value_parser=parse_meta_arg, action=clap::ArgAction::Append, help_heading = "Metadata")]
pub meta: Vec<(String, String)>,

/// Optimize the generated wasm.
#[cfg_attr(feature = "additional-libs", arg(long))]
#[cfg_attr(not(feature = "additional-libs"), arg(long, hide = true))]
/// Optimize the generated wasm. Enabled by default; pass `--optimize=false` to disable.
#[arg(
long,
default_value_t = true,
default_missing_value = "true",
num_args = 0..=1,
Comment thread
fnando marked this conversation as resolved.
action = clap::ArgAction::Set,
)]
pub optimize: bool,
}

// Manual impl so `optimize` defaults to `true`, matching the CLI default.
// `#[derive(Default)]` would set it to `false`.
impl Default for BuildArgs {
fn default() -> Self {
Self {
meta: Vec::new(),
optimize: true,
}
}
}

pub fn parse_meta_arg(s: &str) -> Result<(String, String), Error> {
let parts = s.splitn(2, '=');

Expand Down Expand Up @@ -170,16 +184,12 @@ pub enum Error {
)]
RustVersion(String),

#[error("must install with \"additional-libs\" feature.")]
OptimizeFeatureNotEnabled,

#[error("invalid Cargo.toml configuration: {0}")]
CargoConfiguration(String),

#[error(transparent)]
Xdr(#[from] stellar_xdr::curr::Error),

#[cfg(feature = "additional-libs")]
#[error(transparent)]
Optimize(#[from] optimize::Error),

Expand Down Expand Up @@ -314,10 +324,8 @@ impl Cmd {
};

let wasm_bytes = fs::read(&final_path).map_err(Error::ReadingWasmFile)?;
#[cfg_attr(not(feature = "additional-libs"), allow(unused_mut))]
let mut optimized_wasm_bytes: Vec<u8> = Vec::new();

#[cfg(feature = "additional-libs")]
if self.build_args.optimize {
let mut path = final_path.clone();
path.set_extension("optimized.wasm");
Expand All @@ -328,11 +336,6 @@ impl Cmd {
fs::rename(&path, &final_path).map_err(Error::CopyingWasmFile)?;
}

#[cfg(not(feature = "additional-libs"))]
if self.build_args.optimize {
return Err(Error::OptimizeFeatureNotEnabled);
}

Self::print_build_summary(
&print,
&p.name,
Expand Down
13 changes: 0 additions & 13 deletions cmd/soroban-cli/src/commands/contract/optimize.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use clap::Parser;
use std::{fmt::Debug, path::PathBuf};
#[cfg(feature = "additional-libs")]
use wasm_opt::{Feature, OptimizationError, OptimizationOptions};

use crate::wasm;
Expand All @@ -22,31 +21,19 @@ pub enum Error {
#[error(transparent)]
Wasm(#[from] wasm::Error),

#[cfg(feature = "additional-libs")]
#[error("optimization error: {0}")]
OptimizationError(OptimizationError),

#[cfg(not(feature = "additional-libs"))]
#[error("must install with \"additional-libs\" feature.")]
Install,

#[error("--wasm-out cannot be used with --wasm option when passing multiple files")]
MultipleFilesOutput,
}

impl Cmd {
#[cfg(not(feature = "additional-libs"))]
pub fn run(&self) -> Result<(), Error> {
Err(Error::Install)
}

#[cfg(feature = "additional-libs")]
pub fn run(&self) -> Result<(), Error> {
optimize(false, self.wasm.clone(), self.wasm_out.clone())
}
}
Comment thread
fnando marked this conversation as resolved.

#[cfg(feature = "additional-libs")]
pub fn optimize(
quiet: bool,
wasm: Vec<PathBuf>,
Expand Down
2 changes: 0 additions & 2 deletions cmd/soroban-cli/src/commands/doctor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,6 @@ fn check_wasm_target(print: &Print) {
fn check_optional_features(print: &Print) {
#[cfg(feature = "additional-libs")]
{
print.checkln("Wasm optimization");
print.checkln("Secure store (OS keyring)");
print.checkln("Ledger hardware wallet");
}
Expand All @@ -218,7 +217,6 @@ fn check_optional_features(print: &Print) {
print.warnln(
"The following features are disabled until `--features additional-libs` is used:",
);
print.blankln("- Wasm optimization");
print.blankln("- Secure store (OS keyring)");
print.blankln("- Ledger hardware wallet");
}
Expand Down
Loading