Skip to content

Commit

Permalink
feat(nargo): split nargo into core and cli packages (#1065)
Browse files Browse the repository at this point in the history
* feat(nargo): perform initial split into core + cli

* chore: split manifest errors from definition

* chore: rename `nargo` to `nargo_cli` and `nargo_core` to `nargo`

* chore: add comment for binary renaming
  • Loading branch information
TomAFrench authored Mar 30, 2023
1 parent fd3c99a commit 7c388f9
Show file tree
Hide file tree
Showing 221 changed files with 300 additions and 239 deletions.
19 changes: 15 additions & 4 deletions Cargo.lock

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

4 changes: 3 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,14 @@ members = [
"crates/noirc_errors",
"crates/noirc_driver",
"crates/nargo",
"crates/nargo_cli",
"crates/fm",
"crates/arena",
"crates/noirc_abi",
"crates/iter-extended",
"crates/wasm",
]
default-members = ["crates/nargo"]
default-members = ["crates/nargo_cli"]

[workspace.package]
# x-release-please-start-version
Expand All @@ -28,6 +29,7 @@ arena = { path = "crates/arena" }
fm = { path = "crates/fm" }
iter-extended = { path = "crates/iter-extended" }
nargo = { path = "crates/nargo" }
nargo_cli = { path = "crates/nargo_cli" }
noirc_abi = { path = "crates/noirc_abi" }
noirc_driver = { path = "crates/noirc_driver" }
noirc_errors = { path = "crates/noirc_errors" }
Expand Down
21 changes: 0 additions & 21 deletions crates/nargo/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,39 +9,18 @@ edition.workspace = true

[build-dependencies]
rustc_version = "0.4.0"
build-data = "0.1.3"

[dependencies]
clap.workspace = true
dirs.workspace = true
url.workspace = true
iter-extended.workspace = true
noirc_driver.workspace = true
noirc_frontend.workspace = true
noirc_abi.workspace = true
acvm.workspace = true
cfg-if.workspace = true
toml.workspace = true
serde.workspace = true
thiserror.workspace = true
const_format = "0.2.30"
hex = "0.4.2"
serde_json = "1.0"
termcolor = "1.1.2"
tempdir = "0.3.7"
color-eyre = "0.6.2"


# Backends
aztec_backend = { optional = true, package = "barretenberg_static_lib", git = "https://github.com/noir-lang/aztec_backend", rev = "26178359a2251e885f15f0a4d1a686afda04aec9" }
aztec_wasm_backend = { optional = true, package = "barretenberg_wasm", git = "https://github.com/noir-lang/aztec_backend", rev = "26178359a2251e885f15f0a4d1a686afda04aec9" }
marlin_arkworks_backend = { optional = true, git = "https://github.com/noir-lang/marlin_arkworks_backend", rev = "144378edad821bfaa52bf2cacca8ecc87514a4fc" }

[dev-dependencies]
assert_cmd = "2.0.8"
assert_fs = "1.0.10"
predicates = "2.1.5"

[features]
default = ["plonk_bn254"]
# The plonk backend can only use bn254, so we do not specify the field
Expand Down
4 changes: 0 additions & 4 deletions crates/nargo/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,4 @@ fn check_rustc_version() {

fn main() {
check_rustc_version();

build_data::set_GIT_COMMIT();
build_data::set_GIT_DIRTY();
build_data::no_debug_rebuilds();
}
6 changes: 3 additions & 3 deletions crates/nargo/src/backends.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
cfg_if::cfg_if! {
if #[cfg(feature = "plonk_bn254")] {
pub(crate) use aztec_backend::Plonk as ConcreteBackend;
pub use aztec_backend::Plonk as ConcreteBackend;
} else if #[cfg(feature = "plonk_bn254_wasm")] {
pub(crate) use aztec_wasm_backend::Plonk as ConcreteBackend;
pub use aztec_wasm_backend::Plonk as ConcreteBackend;
} else if #[cfg(feature = "marlin")] {
// R1CS_MARLIN_ARKWORKS
pub(crate) use marlin_arkworks_backend::Marlin as ConcreteBackend;
pub use marlin_arkworks_backend::Marlin as ConcreteBackend;
} else {
compile_error!("please specify a backend to compile with");
}
Expand Down
87 changes: 2 additions & 85 deletions crates/nargo/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,88 +6,5 @@
//! This name was used because it sounds like `cargo` and
//! Noir Package Manager abbreviated is npm, which is already taken.

use noirc_frontend::graph::CrateType;
use std::{
fs::ReadDir,
path::{Path, PathBuf},
};

mod backends;
pub mod cli;
mod constants;
mod errors;
mod git;
mod manifest;
mod resolver;

use manifest::InvalidPackageError;

fn nargo_crates() -> PathBuf {
dirs::home_dir().unwrap().join("nargo")
}

/// Returns the path of the root directory of the package containing `current_path`.
///
/// Returns a `CliError` if no parent directories of `current_path` contain a manifest file.
fn find_package_root(current_path: &Path) -> Result<PathBuf, InvalidPackageError> {
let manifest_path = find_package_manifest(current_path)?;

let package_root =
manifest_path.parent().expect("infallible: manifest file path can't be root directory");

Ok(package_root.to_path_buf())
}

/// Returns the path of the manifest file (`Nargo.toml`) of the package containing `current_path`.
///
/// Returns a `CliError` if no parent directories of `current_path` contain a manifest file.
fn find_package_manifest(current_path: &Path) -> Result<PathBuf, InvalidPackageError> {
current_path
.ancestors()
.find_map(|dir| find_file(dir, "Nargo", "toml"))
.ok_or_else(|| InvalidPackageError::MissingManifestFile(current_path.to_path_buf()))
}

fn lib_or_bin(current_path: &Path) -> Result<(PathBuf, CrateType), InvalidPackageError> {
// A library has a lib.nr and a binary has a main.nr
// You cannot have both.
let src_path = find_dir(current_path, "src")
.ok_or_else(|| InvalidPackageError::NoSourceDir(current_path.to_path_buf()))?;

let lib_nr_path = find_file(&src_path, "lib", "nr");
let bin_nr_path = find_file(&src_path, "main", "nr");
match (lib_nr_path, bin_nr_path) {
(Some(_), Some(_)) => Err(InvalidPackageError::ContainsMultipleCrates),
(None, Some(path)) => Ok((path, CrateType::Binary)),
(Some(path), None) => Ok((path, CrateType::Library)),
(None, None) => Err(InvalidPackageError::ContainsZeroCrates),
}
}

// Looks for file named `file_name` in path
fn find_file<P: AsRef<Path>>(path: P, file_name: &str, extension: &str) -> Option<PathBuf> {
let entries = list_files_and_folders_in(path)?;
let file_name = format!("{file_name}.{extension}");

find_artifact(entries, &file_name)
}

// Looks for directory named `dir_name` in path
fn find_dir<P: AsRef<Path>>(path: P, dir_name: &str) -> Option<PathBuf> {
let entries = list_files_and_folders_in(path)?;
find_artifact(entries, dir_name)
}

// There is no distinction between files and folders
fn find_artifact(entries: ReadDir, artifact_name: &str) -> Option<PathBuf> {
let entry = entries
.into_iter()
.flatten()
.find(|entry| entry.file_name().to_str() == Some(artifact_name))?;

Some(entry.path())
}

fn list_files_and_folders_in<P: AsRef<Path>>(path: P) -> Option<ReadDir> {
std::fs::read_dir(path).ok()
}
pub mod backends;
pub mod manifest;
102 changes: 0 additions & 102 deletions crates/nargo/src/manifest.rs

This file was deleted.

26 changes: 26 additions & 0 deletions crates/nargo/src/manifest/errors.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
use std::path::PathBuf;
use thiserror::Error;

/// Errors covering situations where a package is either missing or malformed.
#[derive(Debug, Error)]
pub enum InvalidPackageError {
/// Package doesn't have a manifest file
#[error("cannot find a Nargo.toml in {}", .0.display())]
MissingManifestFile(PathBuf),

/// Package manifest is unreadable.
#[error("Nargo.toml is badly formed, could not parse.\n\n {0}")]
MalformedManifestFile(#[from] toml::de::Error),

/// Package does not contain Noir source files.
#[error("cannot find src directory in path {}", .0.display())]
NoSourceDir(PathBuf),

/// Package has neither of `main.nr` and `lib.nr`.
#[error("package must contain either a `lib.nr`(Library) or a `main.nr`(Binary).")]
ContainsZeroCrates,

/// Package has both a `main.nr` (for binaries) and `lib.nr` (for libraries)
#[error("package cannot contain both a `lib.nr` and a `main.nr`")]
ContainsMultipleCrates,
}
Loading

0 comments on commit 7c388f9

Please sign in to comment.