Skip to content

Commit

Permalink
Pass --features through to cargo (#853)
Browse files Browse the repository at this point in the history
* WIP adding features

* Add features flag to build and check commands

* Add feature flags to test command

* Fix readme doc test
  • Loading branch information
ascjones authored Dec 6, 2022
1 parent defd393 commit 6059a3a
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 6 deletions.
2 changes: 2 additions & 0 deletions crates/build/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use contract_build::{
Verbosity,
BuildArtifacts,
BuildMode,
Features,
Network,
OptimizationPasses,
OutputType,
Expand All @@ -23,6 +24,7 @@ let args = contract_build::ExecuteArgs {
manifest_path,
verbosity: Verbosity::Default,
build_mode: BuildMode::Release,
features: Features::default(),
network: Network::Online,
build_artifact: BuildArtifacts::All,
unstable_flags: UnstableFlags::default(),
Expand Down
25 changes: 25 additions & 0 deletions crates/build/src/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -224,3 +224,28 @@ impl TryFrom<&UnstableOptions> for UnstableFlags {
})
}
}

/// Define the standard `cargo` features args to be passed through.
#[derive(Default, Clone, Debug, Args)]
pub struct Features {
/// Space or comma separated list of features to activate
#[clap(long)]
features: Vec<String>,
}

impl Features {
/// Appends a feature.
pub fn push(&mut self, feature: &str) {
self.features.push(feature.to_owned())
}

/// Appends the raw features args to pass through to the `cargo` invocation.
pub fn append_to_args<'a>(&'a self, args: &mut Vec<&'a str>) {
if !self.features.is_empty() {
args.push("--features");
for feature in &self.features {
args.push(feature)
}
}
}
}
10 changes: 9 additions & 1 deletion crates/build/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ pub use self::{
BuildArtifacts,
BuildMode,
BuildSteps,
Features,
Network,
OutputType,
UnstableFlags,
Expand Down Expand Up @@ -99,6 +100,7 @@ pub struct ExecuteArgs {
pub manifest_path: ManifestPath,
pub verbosity: Verbosity,
pub build_mode: BuildMode,
pub features: Features,
pub network: Network,
pub build_artifact: BuildArtifacts,
pub unstable_flags: UnstableFlags,
Expand Down Expand Up @@ -232,6 +234,7 @@ impl BuildResult {
fn exec_cargo_for_wasm_target(
crate_metadata: &CrateMetadata,
command: &str,
features: &Features,
build_mode: BuildMode,
network: Network,
verbosity: Verbosity,
Expand All @@ -248,14 +251,16 @@ fn exec_cargo_for_wasm_target(
"--release",
&target_dir,
];
let mut features = features.clone();
if network == Network::Offline {
args.push("--offline");
}
if build_mode == BuildMode::Debug {
args.push("--features=ink/ink-debug");
features.push("ink/ink-debug");
} else {
args.push("-Zbuild-std-features=panic_immediate_abort");
}
features.append_to_args(&mut args);
let mut env = vec![(
"RUSTFLAGS",
Some("-C link-arg=-zstack-size=65536 -C link-arg=--import-memory -Clinker-plugin-lto -C target-cpu=mvp"),
Expand Down Expand Up @@ -555,6 +560,7 @@ pub fn execute(args: ExecuteArgs) -> Result<BuildResult> {
let ExecuteArgs {
manifest_path,
verbosity,
features,
build_mode,
network,
build_artifact,
Expand Down Expand Up @@ -619,6 +625,7 @@ pub fn execute(args: ExecuteArgs) -> Result<BuildResult> {
exec_cargo_for_wasm_target(
&crate_metadata,
"build",
&features,
build_mode,
network,
verbosity,
Expand Down Expand Up @@ -683,6 +690,7 @@ pub fn execute(args: ExecuteArgs) -> Result<BuildResult> {
exec_cargo_for_wasm_target(
&crate_metadata,
"check",
&features,
BuildMode::Release,
network,
verbosity,
Expand Down
3 changes: 3 additions & 0 deletions crates/build/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@ fn optimization_passes_from_cli_must_take_precedence_over_profile(
let args = ExecuteArgs {
manifest_path: manifest_path.clone(),
verbosity: Verbosity::Default,
features: Default::default(),
build_mode: Default::default(),
network: Default::default(),
build_artifact: BuildArtifacts::All,
Expand Down Expand Up @@ -192,6 +193,7 @@ fn optimization_passes_from_profile_must_be_used(
let args = ExecuteArgs {
manifest_path: manifest_path.clone(),
verbosity: Verbosity::Default,
features: Default::default(),
build_mode: Default::default(),
network: Default::default(),
build_artifact: BuildArtifacts::All,
Expand Down Expand Up @@ -236,6 +238,7 @@ fn contract_lib_name_different_from_package_name_must_build(
let args = ExecuteArgs {
manifest_path: manifest_path.clone(),
verbosity: Verbosity::Default,
features: Default::default(),
build_mode: Default::default(),
network: Default::default(),
build_artifact: BuildArtifacts::All,
Expand Down
7 changes: 7 additions & 0 deletions crates/cargo-contract/src/cmd/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ use contract_build::{
BuildMode,
BuildResult,
ExecuteArgs,
Features,
ManifestPath,
Network,
OptimizationPasses,
Expand Down Expand Up @@ -69,6 +70,8 @@ pub struct BuildCommand {
#[clap(long = "generate", value_enum, default_value = "all")]
build_artifact: BuildArtifacts,
#[clap(flatten)]
features: Features,
#[clap(flatten)]
verbosity: VerbosityFlags,
#[clap(flatten)]
unstable_options: UnstableOptions,
Expand Down Expand Up @@ -140,6 +143,7 @@ impl BuildCommand {
manifest_path,
verbosity,
build_mode,
features: self.features.clone(),
network,
build_artifact: self.build_artifact,
unstable_flags,
Expand All @@ -163,6 +167,8 @@ pub struct CheckCommand {
#[clap(flatten)]
verbosity: VerbosityFlags,
#[clap(flatten)]
features: Features,
#[clap(flatten)]
unstable_options: UnstableOptions,
}

Expand All @@ -177,6 +183,7 @@ impl CheckCommand {
manifest_path,
verbosity,
build_mode: BuildMode::Debug,
features: self.features.clone(),
network: Network::default(),
build_artifact: BuildArtifacts::CheckOnly,
unstable_flags,
Expand Down
19 changes: 14 additions & 5 deletions crates/cargo-contract/src/cmd/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ use colored::Colorize;
use contract_build::{
maybe_println,
util,
Features,
ManifestPath,
Verbosity,
VerbosityFlags,
Expand All @@ -37,14 +38,16 @@ pub struct TestCommand {
manifest_path: Option<PathBuf>,
#[clap(flatten)]
verbosity: VerbosityFlags,
#[clap(flatten)]
features: Features,
}

impl TestCommand {
pub fn exec(&self) -> Result<TestResult> {
let manifest_path = ManifestPath::try_from(self.manifest_path.as_ref())?;
let verbosity = TryFrom::<&VerbosityFlags>::try_from(&self.verbosity)?;

execute(&manifest_path, verbosity)
execute(&manifest_path, verbosity, &self.features)
}
}

Expand All @@ -62,10 +65,11 @@ impl TestResult {
}
}

/// Executes `cargo +nightly test`.
/// Executes `cargo test`.
pub(crate) fn execute(
manifest_path: &ManifestPath,
verbosity: Verbosity,
features: &Features,
) -> Result<TestResult> {
maybe_println!(
verbosity,
Expand All @@ -74,8 +78,11 @@ pub(crate) fn execute(
"Running tests".bright_green().bold()
);

let mut args = Vec::new();
features.append_to_args(&mut args);

let stdout =
util::invoke_cargo("test", [""], manifest_path.directory(), verbosity, vec![])?;
util::invoke_cargo("test", args, manifest_path.directory(), verbosity, vec![])?;

Ok(TestResult { stdout, verbosity })
}
Expand All @@ -84,6 +91,7 @@ pub(crate) fn execute(
#[cfg(test)]
mod tests_ci_only {
use contract_build::{
Features,
ManifestPath,
Verbosity,
};
Expand All @@ -107,8 +115,9 @@ mod tests_ci_only {
Regex::new(r"test result: ok. \d+ passed; 0 failed; \d+ ignored")
.expect("regex pattern compilation failed");

let res = super::execute(&manifest_path, Verbosity::Default)
.expect("test execution failed");
let res =
super::execute(&manifest_path, Verbosity::Default, &Features::default())
.expect("test execution failed");

assert!(ok_output_pattern.is_match(&String::from_utf8_lossy(&res.stdout)));
}
Expand Down

0 comments on commit 6059a3a

Please sign in to comment.