Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: create a consolidated options struct for Driver #908

Merged
merged 11 commits into from
Mar 6, 2023
33 changes: 33 additions & 0 deletions Cargo.lock

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

5 changes: 4 additions & 1 deletion crates/nargo/src/cli/check_cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use acvm::ProofSystemCompiler;
use clap::Args;
use iter_extended::btree_map;
use noirc_abi::{Abi, AbiParameter, AbiType};
use noirc_driver::CompileOptionsBuilder;
use std::{
collections::BTreeMap,
path::{Path, PathBuf},
Expand All @@ -29,10 +30,12 @@ pub(crate) fn run(args: CheckCommand, config: NargoConfig) -> Result<(), CliErro
pub fn check_from_path<P: AsRef<Path>>(p: P, allow_warnings: bool) -> 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(allow_warnings).is_err() {
if driver.check_crate(&config).is_err() {
std::process::exit(1);
}

Expand Down
8 changes: 7 additions & 1 deletion crates/nargo/src/cli/compile_cmd.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use acvm::acir::circuit::Circuit;
use acvm::ProofSystemCompiler;
use noirc_driver::CompileOptionsBuilder;
use std::path::{Path, PathBuf};

use clap::Args;
Expand Down Expand Up @@ -59,7 +60,12 @@ pub fn compile_circuit<P: AsRef<Path>>(
let mut driver = Resolver::resolve_root_config(program_dir.as_ref(), backend.np_language())?;
add_std_lib(&mut driver);

driver.into_compiled_program(show_ssa, allow_warnings).map_err(|_| std::process::exit(1))
let config = CompileOptionsBuilder::default()
.allow_warnings(allow_warnings)
.show_ssa(show_ssa)
.build()
.unwrap();
TomAFrench marked this conversation as resolved.
Show resolved Hide resolved
driver.into_compiled_program(&config).map_err(|_| std::process::exit(1))
}

fn preprocess_with_path<P: AsRef<Path>>(
Expand Down
17 changes: 11 additions & 6 deletions crates/nargo/src/cli/test_cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::{collections::BTreeMap, io::Write, path::Path};

use acvm::{PartialWitnessGenerator, ProofSystemCompiler};
use clap::Args;
use noirc_driver::Driver;
use noirc_driver::{CompileOptions, CompileOptionsBuilder, Driver};
use noirc_frontend::node_interner::FuncId;
use termcolor::{Color, ColorChoice, ColorSpec, StandardStream, WriteColor};

Expand Down Expand Up @@ -39,10 +39,16 @@ fn run_tests(
) -> Result<(), CliError> {
let backend = crate::backends::ConcreteBackend;

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

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

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

Expand All @@ -58,7 +64,7 @@ fn run_tests(
writeln!(writer, "Testing {test_name}...").expect("Failed to write to stdout");
writer.flush().ok();

match run_test(test_name, test_function, &driver, allow_warnings, show_output) {
match run_test(test_name, test_function, &driver, &config) {
Ok(_) => {
writer.set_color(ColorSpec::new().set_fg(Some(Color::Green))).ok();
writeln!(writer, "ok").ok();
Expand Down Expand Up @@ -87,13 +93,12 @@ fn run_test(
test_name: &str,
main: FuncId,
driver: &Driver,
allow_warnings: bool,
show_output: bool,
config: &CompileOptions,
) -> Result<(), CliError> {
let backend = crate::backends::ConcreteBackend;

let program = driver
.compile_no_check(false, allow_warnings, Some(main), show_output)
.compile_no_check(config, Some(main))
.map_err(|_| CliError::Generic(format!("Test '{test_name}' failed to compile")))?;

let mut solved_witness = BTreeMap::new();
Expand Down
1 change: 1 addition & 0 deletions crates/noirc_driver/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,4 @@ acvm.workspace = true
fm.workspace = true
serde.workspace = true
dirs.workspace = true
derive_builder = "0.12.0"
41 changes: 29 additions & 12 deletions crates/noirc_driver/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
use acvm::acir::circuit::Circuit;

use acvm::Language;
use derive_builder::Builder;
use fm::FileType;
use noirc_abi::Abi;
use noirc_errors::{reporter, ReportedError};
Expand All @@ -19,6 +20,16 @@ pub struct Driver {
language: Language,
}

#[non_exhaustive]
#[derive(Debug, Default, Builder)]
#[builder(default)]
pub struct CompileOptions {
TomAFrench marked this conversation as resolved.
Show resolved Hide resolved
pub show_ssa: bool,
pub allow_warnings: bool,

pub show_output: bool,
}

#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct CompiledProgram {
pub circuit: Circuit,
Expand All @@ -38,7 +49,9 @@ impl Driver {
let mut driver = Driver::new(&np_language);
driver.create_local_crate(root_file, CrateType::Binary);

driver.into_compiled_program(false, false).unwrap_or_else(|_| std::process::exit(1))
driver
.into_compiled_program(&CompileOptions::default())
.unwrap_or_else(|_| std::process::exit(1))
}

/// Compiles a file and returns true if compilation was successful
Expand Down Expand Up @@ -125,10 +138,11 @@ impl Driver {

/// Run the lexing, parsing, name resolution, and type checking passes,
/// returning Err(FrontendError) and printing any errors that were found.
pub fn check_crate(&mut self, allow_warnings: bool) -> Result<(), ReportedError> {
pub fn check_crate(&mut self, options: &CompileOptions) -> Result<(), ReportedError> {
let mut errs = vec![];
CrateDefMap::collect_defs(LOCAL_CRATE, &mut self.context, &mut errs);
let error_count = reporter::report_all(&self.context.file_manager, &errs, allow_warnings);
let error_count =
reporter::report_all(&self.context.file_manager, &errs, options.allow_warnings);
reporter::finish_report(error_count)
}

Expand All @@ -145,22 +159,19 @@ impl Driver {

pub fn into_compiled_program(
mut self,
show_ssa: bool,
allow_warnings: bool,
options: &CompileOptions,
) -> Result<CompiledProgram, ReportedError> {
self.check_crate(allow_warnings)?;
self.compile_no_check(show_ssa, allow_warnings, None, true)
self.check_crate(options)?;
self.compile_no_check(options, None)
}

/// Compile the current crate. Assumes self.check_crate is called beforehand!
#[allow(deprecated)]
pub fn compile_no_check(
&self,
show_ssa: bool,
allow_warnings: bool,
options: &CompileOptions,
// Optional override to provide a different `main` function to start execution
main_function: Option<FuncId>,
show_output: bool,
) -> Result<CompiledProgram, ReportedError> {
// Find the local crate, one should always be present
let local_crate = self.context.def_map(LOCAL_CRATE).unwrap();
Expand All @@ -183,14 +194,20 @@ impl Driver {
let np_language = self.language.clone();
let blackbox_supported = acvm::default_is_black_box_supported(np_language.clone());

match create_circuit(program, np_language, blackbox_supported, show_ssa, show_output) {
match create_circuit(
program,
np_language,
blackbox_supported,
options.show_ssa,
options.show_output,
) {
Ok((circuit, abi)) => Ok(CompiledProgram { circuit, abi }),
Err(err) => {
// The FileId here will be the file id of the file with the main file
// Errors will be shown at the call site without a stacktrace
let file = err.location.map(|loc| loc.file);
let files = &self.context.file_manager;
let error = reporter::report(files, &err.into(), file, allow_warnings);
let error = reporter::report(files, &err.into(), file, options.allow_warnings);
reporter::finish_report(error as u32)?;
Err(ReportedError)
}
Expand Down
5 changes: 3 additions & 2 deletions crates/noirc_driver/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use noirc_driver::Driver;
use noirc_driver::{CompileOptions, Driver};
use noirc_frontend::graph::{CrateType, LOCAL_CRATE};
fn main() {
const EXTERNAL_DIR: &str = "dep_b/lib.nr";
Expand All @@ -18,5 +18,6 @@ fn main() {
driver.add_dep(LOCAL_CRATE, crate_id1, "coo4");
driver.add_dep(LOCAL_CRATE, crate_id2, "coo3");

driver.into_compiled_program(false, false).ok();
let config = CompileOptions::default();
driver.into_compiled_program(&config).ok();
}