Skip to content

Commit c8e6fc4

Browse files
authored
feat: allow specifying features for cargo near abi (#366)
1 parent 723097b commit c8e6fc4

File tree

6 files changed

+77
-1
lines changed

6 files changed

+77
-1
lines changed

cargo-near-build/src/near/abi/mod.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,14 @@ pub fn build(args: abi_types::Opts) -> eyre::Result<camino::Utf8PathBuf> {
3333
.get_out_dir()
3434
.clone();
3535

36+
let cargo_feature_args = {
37+
let mut feat_args = vec![];
38+
if let Some(features) = args.features.as_deref() {
39+
feat_args.extend_from_slice(&["--features", features]);
40+
}
41+
feat_args
42+
};
43+
3644
let format = if args.compact_abi {
3745
abi_types::Format::JsonMin
3846
} else {
@@ -43,7 +51,7 @@ pub fn build(args: abi_types::Opts) -> eyre::Result<camino::Utf8PathBuf> {
4351
args.no_locked,
4452
!args.no_doc,
4553
false,
46-
&[],
54+
&cargo_feature_args,
4755
&[],
4856
color,
4957
)?;

cargo-near-build/src/types/near/abi.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ pub struct Opts {
1212
pub out_dir: Option<camino::Utf8PathBuf>,
1313
/// Path to the `Cargo.toml` of the contract to build
1414
pub manifest_path: Option<camino::Utf8PathBuf>,
15+
/// Activate additional cargo features during ABI generation
16+
pub features: Option<String>,
1517
/// Coloring: auto, always, never
1618
pub color: Option<ColorPreference>,
1719
}

cargo-near/src/commands/abi/mod.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,15 @@ pub struct Command {
3838
#[interactive_clap(skip_interactive_input)]
3939
#[interactive_clap(verbatim_doc_comment)]
4040
pub manifest_path: Option<crate::types::utf8_path_buf::Utf8PathBuf>,
41+
/// Space or comma separated list of features to activate
42+
///
43+
/// e.g. --features 'feature0 crate3/feature1 feature3'
44+
/// This just passes the argument as `--features` argument to downstream `cargo` command.
45+
/// Unlike `cargo` argument, this argument doesn't support repetition, at most 1 argument can be specified.
46+
#[interactive_clap(long)]
47+
#[interactive_clap(skip_interactive_input)]
48+
#[interactive_clap(verbatim_doc_comment)]
49+
pub features: Option<String>,
4150
/// Whether to color output to stdout and stderr by printing ANSI escape sequences: auto, always, never
4251
#[interactive_clap(long)]
4352
#[interactive_clap(value_enum)]
@@ -53,6 +62,7 @@ impl From<Command> for AbiOpts {
5362
compact_abi: value.compact_abi,
5463
out_dir: value.out_dir.map(Into::into),
5564
manifest_path: value.manifest_path.map(Into::into),
65+
features: value.features,
5666
color: value.color.map(Into::into),
5767
}
5868
}
@@ -72,6 +82,7 @@ impl AbiCommandlContext {
7282
compact_abi: scope.compact_abi,
7383
out_dir: scope.out_dir.clone(),
7484
manifest_path: scope.manifest_path.clone(),
85+
features: scope.features.clone(),
7586
color: scope.color.clone(),
7687
};
7788
cargo_near_build::abi::build(args.into())?;

integration-tests/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@ pub fn invoke_cargo_near(
111111
compact_abi: cmd.compact_abi,
112112
out_dir: cmd.out_dir.map(Into::into),
113113
manifest_path: Some(cargo_path),
114+
features: cmd.features,
114115
color: cmd.color.map(Into::into),
115116
};
116117
tracing::debug!("AbiOpts: {:#?}", args);
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
[package]
2+
name = "::name::"
3+
version = "0.1.0"
4+
edition = "2021"
5+
6+
[lib]
7+
crate-type = ["cdylib"]
8+
9+
[features]
10+
default = []
11+
gated = []
12+
13+
[dependencies]
14+
near-sdk = { features = ["unstable"], ::sdk-git-version-toml:: }
15+
serde = { version = "1", features = ["derive"] }
16+
17+
[workspace]
18+
members = []
19+
20+
[profile.release]
21+
codegen-units = 1
22+
opt-level = "z"
23+
lto = true
24+
debug = false
25+
panic = "abort"

integration-tests/tests/abi/opts.rs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,3 +74,32 @@ fn test_abi_opt_out_dir() -> cargo_near::CliResult {
7474

7575
Ok(())
7676
}
77+
78+
#[test]
79+
#[named]
80+
fn test_abi_opt_features() -> cargo_near::CliResult {
81+
setup_tracing();
82+
let abi_root = generate_abi_fn_with! {
83+
Cargo: "/templates/abi/_Cargo_features.toml";
84+
Opts: "--features gated";
85+
Code:
86+
#[cfg(feature = "gated")]
87+
pub fn gated_only(&self) -> bool {
88+
true
89+
}
90+
};
91+
92+
let function_names: Vec<&str> = abi_root
93+
.body
94+
.functions
95+
.iter()
96+
.map(|function| function.name.as_str())
97+
.collect();
98+
99+
assert!(
100+
function_names.contains(&"gated_only"),
101+
"expected the ABI to include `gated_only` when the feature is enabled"
102+
);
103+
104+
Ok(())
105+
}

0 commit comments

Comments
 (0)