Skip to content

Commit

Permalink
chore: remove builder and group compile options in clap
Browse files Browse the repository at this point in the history
  • Loading branch information
TomAFrench committed Feb 24, 2023
1 parent ecc986c commit 09085c3
Show file tree
Hide file tree
Showing 12 changed files with 100 additions and 149 deletions.
33 changes: 0 additions & 33 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

38 changes: 23 additions & 15 deletions crates/nargo/src/cli/check_cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use acvm::ProofSystemCompiler;
use clap::Args;
use iter_extended::btree_map;
use noirc_abi::{Abi, AbiParameter, AbiType};
use noirc_driver::CompileOptionsBuilder;
use noirc_driver::CompileOptions;
use std::{
collections::BTreeMap,
path::{Path, PathBuf},
Expand All @@ -15,27 +15,24 @@ use crate::constants::{PROVER_INPUT_FILE, VERIFIER_INPUT_FILE};

/// Checks the constraint system for errors
#[derive(Debug, Clone, Args)]
pub(crate) struct CheckCommand {
/// Issue a warning for each unused variable instead of an error
#[arg(short, long)]
allow_warnings: bool,
}
pub(crate) struct CheckCommand {}

pub(crate) fn run(args: CheckCommand, config: NargoConfig) -> Result<(), CliError> {
check_from_path(config.program_dir, args.allow_warnings)?;
pub(crate) fn run(_args: CheckCommand, config: NargoConfig) -> Result<(), CliError> {
check_from_path(config.program_dir, &CompileOptions::from(config.compile_options))?;
println!("Constraint system successfully built!");
Ok(())
}
// This is exposed so that we can run the examples and verify that they pass
pub fn check_from_path<P: AsRef<Path>>(p: P, allow_warnings: bool) -> Result<(), CliError> {
pub(crate) fn check_from_path<P: AsRef<Path>>(
p: P,
compile_options: &CompileOptions,
) -> Result<(), CliError> {
let backend = crate::backends::ConcreteBackend;

let config = CompileOptionsBuilder::default().allow_warnings(allow_warnings).build().unwrap();

let mut driver = Resolver::resolve_root_config(p.as_ref(), backend.np_language())?;
add_std_lib(&mut driver);

if driver.check_crate(&config).is_err() {
if driver.check_crate(compile_options).is_err() {
std::process::exit(1);
}

Expand Down Expand Up @@ -75,18 +72,21 @@ fn build_empty_map(abi: Abi) -> BTreeMap<String, &'static str> {

#[cfg(test)]
mod tests {
use noirc_driver::CompileOptions;

const TEST_DATA_DIR: &str = "tests/target_tests_data";

#[test]
fn pass() {
let mut pass_dir = std::path::PathBuf::from(env!("CARGO_MANIFEST_DIR"));
pass_dir.push(&format!("{TEST_DATA_DIR}/pass"));

let config = CompileOptions::default();
let paths = std::fs::read_dir(pass_dir).unwrap();
for path in paths.flatten() {
let path = path.path();
assert!(
super::check_from_path(path.clone(), false).is_ok(),
super::check_from_path(path.clone(), &config).is_ok(),
"path: {}",
path.display()
);
Expand All @@ -99,11 +99,12 @@ mod tests {
let mut fail_dir = std::path::PathBuf::from(env!("CARGO_MANIFEST_DIR"));
fail_dir.push(&format!("{TEST_DATA_DIR}/fail"));

let config = CompileOptions::default();
let paths = std::fs::read_dir(fail_dir).unwrap();
for path in paths.flatten() {
let path = path.path();
assert!(
super::check_from_path(path.clone(), false).is_err(),
super::check_from_path(path.clone(), &config).is_err(),
"path: {}",
path.display()
);
Expand All @@ -115,10 +116,17 @@ mod tests {
let mut pass_dir = std::path::PathBuf::from(env!("CARGO_MANIFEST_DIR"));
pass_dir.push(&format!("{TEST_DATA_DIR}/pass_dev_mode"));

let mut config = CompileOptions::default();
config.allow_warnings = true;

let paths = std::fs::read_dir(pass_dir).unwrap();
for path in paths.flatten() {
let path = path.path();
assert!(super::check_from_path(path.clone(), true).is_ok(), "path: {}", path.display());
assert!(
super::check_from_path(path.clone(), &config).is_ok(),
"path: {}",
path.display()
);
}
}
}
22 changes: 6 additions & 16 deletions crates/nargo/src/cli/compile_cmd.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use acvm::acir::circuit::Circuit;
use acvm::ProofSystemCompiler;
use noirc_driver::CompileOptionsBuilder;
use noirc_driver::CompileOptions;
use std::path::{Path, PathBuf};

use clap::Args;
Expand All @@ -15,10 +15,6 @@ use super::{add_std_lib, NargoConfig};
pub(crate) struct CompileCommand {
/// The name of the ACIR file
circuit_name: String,

/// Issue a warning for each unused variable instead of an error
#[arg(short, long)]
allow_warnings: bool,
}

pub(crate) fn run(args: CompileCommand, config: NargoConfig) -> Result<(), CliError> {
Expand All @@ -29,7 +25,7 @@ pub(crate) fn run(args: CompileCommand, config: NargoConfig) -> Result<(), CliEr
&args.circuit_name,
config.program_dir,
circuit_path,
args.allow_warnings,
&CompileOptions::from(config.compile_options),
)?;

println!("Generated ACIR code into {}", circuit_path.display());
Expand All @@ -41,9 +37,9 @@ fn compile_and_preprocess_circuit<P: AsRef<Path>>(
circuit_name: &str,
program_dir: P,
circuit_dir: P,
allow_warnings: bool,
compile_options: &CompileOptions,
) -> Result<PathBuf, CliError> {
let compiled_program = compile_circuit(program_dir, false, allow_warnings)?;
let compiled_program = compile_circuit(program_dir, compile_options)?;
let circuit_path = save_acir_to_dir(&compiled_program.circuit, circuit_name, &circuit_dir);

preprocess_with_path(circuit_name, circuit_dir, compiled_program.circuit)?;
Expand All @@ -53,19 +49,13 @@ fn compile_and_preprocess_circuit<P: AsRef<Path>>(

pub fn compile_circuit<P: AsRef<Path>>(
program_dir: P,
show_ssa: bool,
allow_warnings: bool,
compile_options: &CompileOptions,
) -> Result<noirc_driver::CompiledProgram, CliError> {
let backend = crate::backends::ConcreteBackend;
let mut driver = Resolver::resolve_root_config(program_dir.as_ref(), backend.np_language())?;
add_std_lib(&mut driver);

let config = CompileOptionsBuilder::default()
.allow_warnings(allow_warnings)
.show_ssa(show_ssa)
.build()
.unwrap();
driver.into_compiled_program(&config).map_err(|_| std::process::exit(1))
driver.into_compiled_program(compile_options).map_err(|_| std::process::exit(1))
}

fn preprocess_with_path<P: AsRef<Path>>(
Expand Down
12 changes: 5 additions & 7 deletions crates/nargo/src/cli/contract_cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,15 @@ use super::NargoConfig;
use crate::{cli::compile_cmd::compile_circuit, constants::CONTRACT_DIR, errors::CliError};
use acvm::SmartContract;
use clap::Args;
use noirc_driver::CompileOptions;

/// Generates a Solidity verifier smart contract for the program
#[derive(Debug, Clone, Args)]
pub(crate) struct ContractCommand {
/// Issue a warning for each unused variable instead of an error
#[arg(short, long)]
allow_warnings: bool,
}
pub(crate) struct ContractCommand {}

pub(crate) fn run(args: ContractCommand, config: NargoConfig) -> Result<(), CliError> {
let compiled_program = compile_circuit(config.program_dir.clone(), false, args.allow_warnings)?;
pub(crate) fn run(_args: ContractCommand, config: NargoConfig) -> Result<(), CliError> {
let compiled_program =
compile_circuit(config.program_dir.clone(), &CompileOptions::from(config.compile_options))?;

let backend = crate::backends::ConcreteBackend;
let smart_contract_string = backend.eth_contract_from_cs(compiled_program.circuit);
Expand Down
17 changes: 4 additions & 13 deletions crates/nargo/src/cli/execute_cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use acvm::PartialWitnessGenerator;
use clap::Args;
use noirc_abi::input_parser::{Format, InputValue};
use noirc_abi::{InputMap, WitnessMap};
use noirc_driver::CompiledProgram;
use noirc_driver::{CompileOptions, CompiledProgram};

use super::fs::{inputs::read_inputs_from_file, witness::save_witness_to_dir};
use super::NargoConfig;
Expand All @@ -19,19 +19,11 @@ use crate::{
pub(crate) struct ExecuteCommand {
/// Write the execution witness to named file
witness_name: Option<String>,

/// Issue a warning for each unused variable instead of an error
#[arg(short, long)]
allow_warnings: bool,

/// Emit debug information for the intermediate SSA IR
#[arg(short, long)]
show_ssa: bool,
}

pub(crate) fn run(args: ExecuteCommand, config: NargoConfig) -> Result<(), CliError> {
let (return_value, solved_witness) =
execute_with_path(&config.program_dir, args.show_ssa, args.allow_warnings)?;
execute_with_path(&config.program_dir, &CompileOptions::from(config.compile_options))?;

println!("Circuit witness successfully solved");
if let Some(return_value) = return_value {
Expand All @@ -50,10 +42,9 @@ pub(crate) fn run(args: ExecuteCommand, config: NargoConfig) -> Result<(), CliEr

fn execute_with_path<P: AsRef<Path>>(
program_dir: P,
show_ssa: bool,
allow_warnings: bool,
compile_options: &CompileOptions,
) -> Result<(Option<InputValue>, WitnessMap), CliError> {
let compiled_program = compile_circuit(&program_dir, show_ssa, allow_warnings)?;
let compiled_program = compile_circuit(&program_dir, compile_options)?;

// Parse the initial witness values from Prover.toml
let (inputs_map, _) = read_inputs_from_file(
Expand Down
20 changes: 6 additions & 14 deletions crates/nargo/src/cli/gates_cmd.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use acvm::ProofSystemCompiler;
use clap::Args;
use noirc_driver::CompileOptions;
use std::path::Path;

use crate::cli::compile_cmd::compile_circuit;
Expand All @@ -9,26 +10,17 @@ use super::NargoConfig;

/// Counts the occurrences of different gates in circuit
#[derive(Debug, Clone, Args)]
pub(crate) struct GatesCommand {
/// Issue a warning for each unused variable instead of an error
#[arg(short, long)]
allow_warnings: bool,

/// Emit debug information for the intermediate SSA IR
#[arg(short, long)]
show_ssa: bool,
}
pub(crate) struct GatesCommand {}

pub(crate) fn run(args: GatesCommand, config: NargoConfig) -> Result<(), CliError> {
count_gates_with_path(config.program_dir, args.show_ssa, args.allow_warnings)
pub(crate) fn run(_args: GatesCommand, config: NargoConfig) -> Result<(), CliError> {
count_gates_with_path(config.program_dir, &CompileOptions::from(config.compile_options))
}

pub fn count_gates_with_path<P: AsRef<Path>>(
program_dir: P,
show_ssa: bool,
allow_warnings: bool,
compile_options: &CompileOptions,
) -> Result<(), CliError> {
let compiled_program = compile_circuit(program_dir.as_ref(), show_ssa, allow_warnings)?;
let compiled_program = compile_circuit(program_dir.as_ref(), compile_options)?;
let num_opcodes = compiled_program.circuit.opcodes.len();
let backend = crate::backends::ConcreteBackend;

Expand Down
40 changes: 36 additions & 4 deletions crates/nargo/src/cli/mod.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
pub use check_cmd::check_from_path;
use clap::{Args, Parser, Subcommand};
use const_format::formatcp;
use noirc_abi::InputMap;
use noirc_driver::Driver;
use noirc_driver::{CompileOptions, Driver};
use noirc_frontend::graph::{CrateName, CrateType};
use std::path::{Path, PathBuf};
extern crate tempdir;
Expand Down Expand Up @@ -42,6 +41,34 @@ struct NargoCli {
pub(crate) struct NargoConfig {
#[arg(short, long, hide=true, default_value_os_t = std::env::current_dir().unwrap())]
program_dir: PathBuf,

#[clap(flatten)]
compile_options: NargoCompileOptions,
}

#[derive(Args, Clone, Debug, Default)]
pub(crate) struct NargoCompileOptions {
/// Emit debug information for the intermediate SSA IR
#[arg(short, long)]
pub show_ssa: bool,

/// Issue a warning for each unused variable instead of an error
#[arg(short, long)]
pub allow_warnings: bool,

/// Display output of println statements during tests
#[arg(long)]
pub show_output: bool,
}

impl From<NargoCompileOptions> for CompileOptions {
fn from(value: NargoCompileOptions) -> Self {
let mut options = CompileOptions::default();
options.show_ssa = value.show_ssa;
options.allow_warnings = value.allow_warnings;
options.show_output = value.show_output;
options
}
}

#[non_exhaustive]
Expand Down Expand Up @@ -80,14 +107,19 @@ pub fn start_cli() {
// helper function which tests noir programs by trying to generate a proof and verify it
pub fn prove_and_verify(proof_name: &str, prg_dir: &Path, show_ssa: bool) -> bool {
let tmp_dir = TempDir::new("p_and_v_tests").unwrap();
let compile_options = CompileOptions::from(NargoCompileOptions {
show_ssa,
allow_warnings: false,
show_output: false,
});

match prove_cmd::prove_with_path(
Some(proof_name.to_owned()),
prg_dir,
&tmp_dir.into_path(),
None,
true,
show_ssa,
false,
&compile_options,
) {
Ok(_) => true,
Err(error) => {
Expand Down
Loading

0 comments on commit 09085c3

Please sign in to comment.