diff --git a/Cargo.lock b/Cargo.lock index e6e57909..73eed79c 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -5701,6 +5701,7 @@ name = "xtask" version = "0.1.0" dependencies = [ "anyhow", + "clap", "xshell", ] diff --git a/Cargo.toml b/Cargo.toml index 9f8943a9..c9746b1a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -21,17 +21,12 @@ incremental = true lto = "fat" opt-level = 3 panic = "unwind" -strip = true - -# For testing cargo bloat -[profile.release-no-strip] -inherits = "release" -strip = false # This one optimizes binary size [profile.release-small] inherits = "release" opt-level = "z" +strip = true [profile.release-big] inherits = "release" @@ -39,7 +34,6 @@ codegen-units = 16 incremental = true lto = "thin" opt-level = 1 -panic = "unwind" [workspace.dependencies] aleo-std = "=0.1.24" diff --git a/Makefile.toml b/Makefile.toml deleted file mode 100644 index 9a6b779f..00000000 --- a/Makefile.toml +++ /dev/null @@ -1,226 +0,0 @@ -[config] -default_to_workspace = false -skip_core_tasks = true - -[tasks.default] -command = "cargo" -args = ["make", "--list-all-steps"] - -[tasks.install-cranelift] -description = "Install the Cranelift toolchain" -toolchain = "nightly" -install_crate = { crate_name = "rustc_codegen_cranelift", rustup_component_name = "rustc-codegen-cranelift-preview" } - -[tasks.fmt] -description = "Format the codebase" -toolchain = "nightly" -install_crate = { crate_name = "fmt", rustup_component_name = "rustfmt", binary = "rustfmt", test_arg = "--help" } -command = "cargo" -args = ["fmt"] - -[tasks.check-fmt] -description = "Check the codebase formatting" -toolchain = "nightly" -install_crate = { crate_name = "fmt", rustup_component_name = "rustfmt", binary = "rustfmt", test_arg = "--help" } -command = "cargo" -args = ["fmt", "--", "--check"] - -[tasks.lint] -description = "Lint the codebase" -toolchain = "nightly" -install_crate = { crate_name = "clippy", rustup_component_name = "clippy", binary = "cargo-clippy", test_arg = "--help" } -dependencies = ["install-cranelift"] -env = { RUSTFLAGS = "-Zcodegen-backend=cranelift" } -command = "cargo" -args = ["clippy", "--all-features", "--", "-D", "warnings", "--no-deps"] - -[tasks.check-unused-deps] -description = "Check for unused dependencies" -install_crate = "cargo-machete" -command = "cargo" -args = ["machete"] - -[tasks.check] -description = "Run all checks" -# Have to do this here weirdly -toolchain = "nightly" -dependencies = ["check-fmt", "lint", "check-unused-deps"] - -[tasks.clean] -description = "Clean the project" -command = "cargo" -args = ["clean"] - -[tasks.test] -description = "Run all tests" -toolchain = "nightly" -install_crate = "nextest" -dependencies = ["install-cranelift"] -env = { RUSTFLAGS = "-Zcodegen-backend=cranelift" } -command = "cargo" -args = ["nextest", "run", "--all", "--verbose", "--fail-fast"] - -[tasks.snops-dev] -description = "Live reload the Snops server" -toolchain = "nightly" -command = "cargo" -env = { SNOPS_LOG = "trace", RUSTFLAGS = "-Zcodegen-backend=cranelift" } -watch = { watch = ["./crates/snops"] } -args = ["run", "--bin", "snops"] - -[tasks.install-rust-src] -description = "Install the Rust source code" -install_crate = { rustup_component_name = "rust-src" } - -[tasks.install-upx] -description = "Install UPX. Requires sudo and wget to be installed." -condition = { platforms = ["linux"] } -install_script = ''' -if command -v upx > /dev/null 2>&1; then - echo "UPX is already installed." - exit 0 -fi - -wget https://github.com/upx/upx/releases/download/v4.2.3/upx-4.2.3-amd64_linux.tar.xz -tar -xf upx-4.2.3-amd64_linux.tar.xz -cp ./upx-4.2.3-amd64_linux/upx /usr/local/bin/ -rm -rf upx-4.2.3-amd64_linux.tar.xz upx-4.2.3-amd64_linux/ -''' - -[tasks.clean-compressed] -description = "Clean the compressed binaries" -command = "rm" -args = ["-rf", "./target/*-compressed"] - -[tasks.build-aot] -description = "Build the AOT binary" -toolchain = "nightly" -dependencies = ["install-rust-src"] -env = { RUSTFLAGS = "-Zlocation-detail=none" } -command = "cargo" -args = [ - "build", - "-p", - "snarkos-aot", - "-Zbuild-std=std", - "--profile", - "release-small", - "--target", - "x86_64-unknown-linux-gnu", -] - -[tasks.compress-aot] -description = "Compress the AOT binary" -dependencies = ["build-aot"] -command = "upx" -args = [ - "--best", - "-f", - "--lzma", - "-o", - "./target/snarkos-aot-compressed", - "./target/x86_64-unknown-linux-gnu/release-small/snarkos-aot", -] - -[tasks.build-snops] -description = "Build the Snops server binary" -toolchain = "nightly" -dependencies = ["install-rust-src"] -env = { RUSTFLAGS = "-C panic=abort -Zlocation-detail=none" } -command = "cargo" -args = [ - "build", - "-p", - "snops", - "-Zbuild-std=std,panic_abort", - "-Zbuild-std-features=panic_immediate_abort", - "--profile", - "release-big", - "--target", - "x86_64-unknown-linux-gnu", -] - -[tasks.compress-snops] -description = "Compress the Snops server binary" -dependencies = ["build-snops"] -command = "upx" -args = [ - "--best", - "-f", - "--lzma", - "-o", - "./target/snops-compressed", - "./target/x86_64-unknown-linux-gnu/release/snops", -] - -[tasks.build-snops-agent] -description = "Build the Snops agent binary" -toolchain = "nightly" -dependencies = ["install-rust-src"] -env = { RUSTFLAGS = "-C panic=abort -Zlocation-detail=none" } -command = "cargo" -args = [ - "build", - "-p", - "snops-agent", - "-Zbuild-std=std,panic_abort", - "-Zbuild-std-features=panic_immediate_abort", - "--profile", - "release-small", - "--target", - "x86_64-unknown-linux-gnu", -] - -[tasks.compress-snops-agent] -description = "Compress the Snops agent binary" -dependencies = ["build-snops-agent"] -command = "upx" -args = [ - "--best", - "-f", - "--lzma", - "-o", - "./target/snops-agent-compressed", - "./target/x86_64-unknown-linux-gnu/release-small/snops-agent", -] - -[tasks.build-snops-cli] -description = "Build the Snops cli binary" -toolchain = "nightly" -dependencies = ["install-rust-src"] -env = { RUSTFLAGS = "-C panic=abort -Zlocation-detail=none" } -command = "cargo" -args = [ - "build", - "-p", - "snops-cli", - "-Zbuild-std=std,panic_abort", - "-Zbuild-std-features=panic_immediate_abort", - "--profile", - "release-big", - "--target", - "x86_64-unknown-linux-gnu", -] - -[tasks.compress-snops-cli] -description = "Compress the Snops cli binary" -dependencies = ["build-snops-cli"] -command = "upx" -args = [ - "--best", - "-f", - "--lzma", - "-o", - "./target/snops-cli-compressed", - "./target/x86_64-unknown-linux-gnu/release/snops-cli", -] - -[tasks.build] -description = "Build and compress all binaries" -dependencies = [ - "clean-compressed", - "compress-aot", - "compress-snops", - "compress-snops-agent", - "compress-snops-cli", -] diff --git a/crates/snops/src/persist/storage.rs b/crates/snops/src/persist/storage.rs index c3eb39cc..e25422b0 100644 --- a/crates/snops/src/persist/storage.rs +++ b/crates/snops/src/persist/storage.rs @@ -118,7 +118,7 @@ impl PersistStorage { // load accounts json for name in &self.accounts { - let path = storage_path.join(&format!("{name}.json")); + let path = storage_path.join(format!("{name}.json")); let res = if *name == *ACCOUNTS_KEY_ID { read_to_addrs(pick_additional_addr, &path).await diff --git a/crates/snops/src/schema/storage/mod.rs b/crates/snops/src/schema/storage/mod.rs index edd59549..d7f51002 100644 --- a/crates/snops/src/schema/storage/mod.rs +++ b/crates/snops/src/schema/storage/mod.rs @@ -427,7 +427,7 @@ impl Document { if let Some(generation) = &self.generate { for (name, account) in &generation.accounts { - let path = base.join(&format!("{}.json", name)); + let path = base.join(format!("{}.json", name)); if !path.exists() { info!("generating accounts for {name}"); diff --git a/crates/xtask/Cargo.toml b/crates/xtask/Cargo.toml index 5833a6f5..1afeccbc 100644 --- a/crates/xtask/Cargo.toml +++ b/crates/xtask/Cargo.toml @@ -8,3 +8,4 @@ edition = "2021" [dependencies] anyhow.workspace = true xshell = "0.2" +clap.workspace = true diff --git a/crates/xtask/src/main.rs b/crates/xtask/src/main.rs index 2c39617b..bdd1b1bd 100644 --- a/crates/xtask/src/main.rs +++ b/crates/xtask/src/main.rs @@ -1,10 +1,162 @@ #![allow(dead_code)] -use std::{env, process::Command}; - -use anyhow::{bail, Context, Result}; +use anyhow::{Context, Result}; +use clap::{Parser, Subcommand, ValueEnum}; use xshell::{cmd, Shell}; +#[derive(Parser)] +struct Args { + #[clap(subcommand)] + subcommand: Command, +} + +#[derive(Subcommand)] +enum Command { + /// Generates markdown files for all the CLIs in the codebase. + Clipages, + /// Generates manpages for all the CLIs in the codebase. + Manpages, + /// Format the codebase + Fmt { + /// Check if the codebase is formatted rather than formatting it + #[clap(short, long)] + check: bool, + }, + /// Lint the codebase with clippy + Lint, + /// Checks for unused dependencies in the codebase + UDeps { + /// Fix unused dependencies + #[clap(short, long)] + fix: bool, + }, + + #[cfg(target_os = "linux")] + /// Install's UPX only on linux. + InstallUpx, + /// Builds the project + Build(Build), + /// For watching the project and auto-rebuilding + Dev { target: BuildTarget }, +} + +#[derive(Parser)] +struct Build { + #[clap(long)] + compress: bool, + #[clap(short, long, default_value = "release-big")] + profile: Profile, + #[clap(long)] + cranelift: bool, + target: BuildTarget, +} + +impl Build { + fn run(self, sh: &Shell) -> Result<()> { + let profile = self.profile.as_ref(); + let package = self.target.as_ref(); + + // if crane lift is enabled, we need to build with nightly + let cmd = if self.cranelift { + cmd!(sh, "cargo +nightly build") + } else { + cmd!(sh, "cargo build") + }; + let cmd = cmd.arg("--profile").arg(profile).arg("-p").arg(package); + + // This is broken idk why + // // if cranelift is enabled, and the target is not AOT, we can pass additional + // // flags + // let cmd = if !matches!(self.target, BuildTarget::Aot) && self.cranelift { + // cmd.arg("-Zbuild-std=std,panic_abort") + // .arg("-Zbuild-std-features=panic_immediate_abort") + // } else { + // cmd + // }; + + // if cranelift is enabled we need to set the env var, and also specify the + // target + let cmd = if self.cranelift { + // -C panic=abort + cmd.env( + "RUSTFLAGS", + "-Zlocation-detail=none -Zcodegen-backend=cranelift", + ) + .arg("--target") + .arg("x86_64-unknown-linux-gnu") + } else { + cmd + }; + + cmd.run()?; + + if self.compress { + let profile = if profile == "dev" { "debug" } else { profile }; + + cmd!(sh, "upx --best -f --lzma -o ./target/{package}-compressed ./target/x86_64-unknown-linux-gnu/{profile}/{package}").run()?; + } + + Ok(()) + } +} + +#[derive(Clone, ValueEnum)] +enum BuildTarget { + Aot, + Snops, + SnopsAgent, + SnopsCli, +} + +impl AsRef for BuildTarget { + fn as_ref(&self) -> &str { + match self { + BuildTarget::Aot => "snarkos-aot", + BuildTarget::Snops => "snops", + BuildTarget::SnopsAgent => "snops-agent", + BuildTarget::SnopsCli => "snops-cli", + } + } +} + +#[derive(Clone, ValueEnum)] +enum Profile { + ReleaseBig, + ReleaseSmall, + Dev, +} + +impl AsRef for Profile { + fn as_ref(&self) -> &str { + match self { + Profile::ReleaseBig => "release-big", + Profile::ReleaseSmall => "release-small", + Profile::Dev => "dev", + } + } +} + +impl Command { + fn run(self, sh: &Shell) -> Result<()> { + match self { + Command::Clipages => clipages(sh), + Command::Manpages => manpages(sh), + Command::Fmt { check } => fmt(sh, check), + Command::Lint => cmd!( + sh, + "cargo +nightly clippy --all-targets --all-features -- -D warnings" + ) + .run() + .context("Running clippy"), + Command::UDeps { fix } => udeps(sh, fix), + #[cfg(target_os = "linux")] + Command::InstallUpx => install_upx(sh), + Command::Build(build) => build.run(sh), + Command::Dev { target } => dev(sh, target), + } + } +} + fn main() { if let Err(e) = try_main() { eprintln!("{e}"); @@ -12,30 +164,20 @@ fn main() { } } -const TASKS: &[&str] = &["help", "clipages", "manpages"]; - fn try_main() -> Result<()> { + let sh = Shell::new()?; + // Ensure our working directory is the toplevel { - let toplevel_path = Command::new("git") - .args(["rev-parse", "--show-toplevel"]) - .output() - .context("Invoking git rev-parse")?; - if !toplevel_path.status.success() { - bail!("Failed to invoke git rev-parse"); - } - let path = String::from_utf8(toplevel_path.stdout)?; + let path = cmd!(&sh, "git rev-parse --show-toplevel") + .read() + .context("Failed to invoke git rev-parse")?; std::env::set_current_dir(path.trim()).context("Changing to toplevel")?; } - let task = env::args().nth(1); - let sh = Shell::new()?; - match task.as_deref() { - Some("help") => print_help()?, - Some("clipages") => clipages(&sh)?, - Some("manpages") => manpages(&sh)?, - _ => print_help()?, - } + let args = Args::parse(); + + args.subcommand.run(&sh)?; Ok(()) } @@ -60,10 +202,70 @@ fn manpages(sh: &Shell) -> Result<()> { Ok(()) } -fn print_help() -> Result<()> { - println!("Tasks:"); - for name in TASKS { - println!(" - {name}"); +fn fmt(sh: &Shell, check: bool) -> Result<()> { + let cmd = cmd!(sh, "cargo +nightly fmt --all"); + let cmd = if check { cmd.arg("-- --check") } else { cmd }; + + cmd.run()?; + Ok(()) +} + +fn install_cargo_subcommands(sh: &Shell, subcmd: &'static str) -> Result<()> { + cmd!(sh, "cargo install {subcmd} --locked").run()?; + Ok(()) +} + +fn udeps(sh: &Shell, fix: bool) -> Result<()> { + install_cargo_subcommands(sh, "cargo-machete")?; + let cmd = cmd!(sh, "cargo-machete"); + let cmd = if fix { cmd.arg("fix") } else { cmd }; + cmd.run()?; + Ok(()) +} + +#[cfg(target_os = "linux")] +fn install_upx(sh: &Shell) -> Result<()> { + // Check if upx is already installed and return early if it is + if !cmd!(sh, "command -v upx").read()?.is_empty() { + return Ok(()); } + + cmd!( + sh, + "wget https://github.com/upx/upx/releases/download/v4.2.3/upx-4.2.3-amd64_linux.tar.xz" + ) + .run()?; + cmd!(sh, "tar -xf upx-4.2.3-amd64_linux.tar.xz").run()?; + cmd!(sh, "cp ./upx-4.2.3-amd64_linux/upx /usr/local/bin/").run()?; + cmd!( + sh, + "rm -rf upx-4.2.3-amd64_linux.tar.xz upx-4.2.3-amd64_linux/" + ) + .run()?; + Ok(()) +} + +fn dev(sh: &Shell, target: BuildTarget) -> Result<()> { + install_cargo_subcommands(sh, "cargo-watch")?; + + match target { + BuildTarget::Aot => cmd!( + sh, + "cargo watch -x 'build -p snarkos-aot --profile release-big' -w ./crates/aot" + ) + .run(), + BuildTarget::Snops => cmd!(sh, "cargo watch -x 'run -p snops' -w ./crates/snops").run(), + BuildTarget::SnopsAgent => cmd!( + sh, + "cargo watch -x 'build -p snops-agent --profile release-big' -w ./crates/snops-agent" + ) + .run(), + BuildTarget::SnopsCli => cmd!( + sh, + "cargo watch -x 'build -p snops-cli' -w ./crates/snops-cli" + ) + .run(), + }?; + Ok(()) }