-
Notifications
You must be signed in to change notification settings - Fork 204
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add MVP
nargo export
command (#3870)
# Description ## Problem\* Resolves <!-- Link to GitHub Issue --> ## Summary\* This PR adds the skeleton of a `nargo export` command which compiles all of the functions in a library crate with the `#[export]` attribute into an `export` directory. This is intended to be then fed into `noir-codegen` in order to generate a TS library for executing these functions. ## Additional Context ## Documentation\* Check one: - [ ] No documentation needed. - [ ] Documentation included in this PR. - [ ] **[Exceptional Case]** Documentation to be submitted in a separate PR. # PR Checklist\* - [ ] I have tested the changes locally. - [ ] I have formatted the changes with [Prettier](https://prettier.io/) and/or `cargo fmt` on default settings. --------- Co-authored-by: kevaundray <[email protected]>
- Loading branch information
1 parent
d731103
commit fbb51ed
Showing
16 changed files
with
205 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
use nargo::errors::CompileError; | ||
use noirc_errors::FileDiagnostic; | ||
use rayon::prelude::*; | ||
|
||
use fm::FileManager; | ||
use iter_extended::try_vecmap; | ||
use nargo::insert_all_files_for_workspace_into_file_manager; | ||
use nargo::package::Package; | ||
use nargo::prepare_package; | ||
use nargo::workspace::Workspace; | ||
use nargo_toml::{get_package_manifest, resolve_workspace_from_toml, PackageSelection}; | ||
use noirc_driver::{ | ||
compile_no_check, file_manager_with_stdlib, CompileOptions, CompiledProgram, | ||
NOIR_ARTIFACT_VERSION_STRING, | ||
}; | ||
|
||
use noirc_frontend::graph::CrateName; | ||
|
||
use clap::Args; | ||
|
||
use crate::backends::Backend; | ||
use crate::errors::CliError; | ||
|
||
use super::check_cmd::check_crate_and_report_errors; | ||
|
||
use super::compile_cmd::report_errors; | ||
use super::fs::program::save_program_to_file; | ||
use super::NargoConfig; | ||
|
||
/// Exports functions marked with #[export] attribute | ||
#[derive(Debug, Clone, Args)] | ||
pub(crate) struct ExportCommand { | ||
/// The name of the package to compile | ||
#[clap(long, conflicts_with = "workspace")] | ||
package: Option<CrateName>, | ||
|
||
/// Compile all packages in the workspace | ||
#[clap(long, conflicts_with = "package")] | ||
workspace: bool, | ||
|
||
#[clap(flatten)] | ||
compile_options: CompileOptions, | ||
} | ||
|
||
pub(crate) fn run( | ||
_backend: &Backend, | ||
args: ExportCommand, | ||
config: NargoConfig, | ||
) -> Result<(), CliError> { | ||
let toml_path = get_package_manifest(&config.program_dir)?; | ||
let default_selection = | ||
if args.workspace { PackageSelection::All } else { PackageSelection::DefaultOrAll }; | ||
let selection = args.package.map_or(default_selection, PackageSelection::Selected); | ||
|
||
let workspace = resolve_workspace_from_toml( | ||
&toml_path, | ||
selection, | ||
Some(NOIR_ARTIFACT_VERSION_STRING.to_owned()), | ||
)?; | ||
|
||
let mut workspace_file_manager = file_manager_with_stdlib(&workspace.root_dir); | ||
insert_all_files_for_workspace_into_file_manager(&workspace, &mut workspace_file_manager); | ||
|
||
let library_packages: Vec<_> = | ||
workspace.into_iter().filter(|package| package.is_library()).collect(); | ||
|
||
library_packages | ||
.par_iter() | ||
.map(|package| { | ||
compile_exported_functions( | ||
&workspace_file_manager, | ||
&workspace, | ||
package, | ||
&args.compile_options, | ||
) | ||
}) | ||
.collect() | ||
} | ||
|
||
fn compile_exported_functions( | ||
file_manager: &FileManager, | ||
workspace: &Workspace, | ||
package: &Package, | ||
compile_options: &CompileOptions, | ||
) -> Result<(), CliError> { | ||
let (mut context, crate_id) = prepare_package(file_manager, package); | ||
check_crate_and_report_errors( | ||
&mut context, | ||
crate_id, | ||
compile_options.deny_warnings, | ||
compile_options.disable_macros, | ||
compile_options.silence_warnings, | ||
)?; | ||
|
||
let exported_functions = context.get_all_exported_functions_in_crate(&crate_id); | ||
|
||
let exported_programs = try_vecmap( | ||
exported_functions, | ||
|(function_name, function_id)| -> Result<(String, CompiledProgram), CompileError> { | ||
// TODO: We should to refactor how to deal with compilation errors to avoid this. | ||
let program = compile_no_check(&context, compile_options, function_id, None, false) | ||
.map_err(|error| vec![FileDiagnostic::from(error)]); | ||
|
||
let program = report_errors( | ||
program.map(|program| (program, Vec::new())), | ||
file_manager, | ||
compile_options.deny_warnings, | ||
compile_options.silence_warnings, | ||
)?; | ||
|
||
Ok((function_name, program)) | ||
}, | ||
)?; | ||
|
||
let export_dir = workspace.export_directory_path(); | ||
for (function_name, program) in exported_programs { | ||
save_program_to_file(&program.into(), &function_name.parse().unwrap(), &export_dir); | ||
} | ||
Ok(()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,4 @@ | ||
crs | ||
lib | ||
|
||
!test/*/target | ||
test/codegen |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 2 additions & 2 deletions
4
...ng/noir_codegen/test/assert_lt/Nargo.toml → ...ing/noir_codegen/test/test_lib/Nargo.toml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
[package] | ||
name = "assert_lt" | ||
type = "bin" | ||
name = "test_lib" | ||
type = "lib" | ||
authors = [""] | ||
[dependencies] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters