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

cli: Add arch option to use build-sbf or build-bpf #2398

Merged
merged 2 commits into from
Mar 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ The minor version will be incremented upon a breaking change and the patch versi
- client: Add support for multithreading to the rust client: use flag `--multithreaded` ([#2321](https://github.com/coral-xyz/anchor/pull/2321)).
- client: Add `async_rpc` a method which returns a nonblocking solana rpc client ([2322](https://github.com/coral-xyz/anchor/pull/2322)).
- avm, cli: Use the `rustls-tls` feature of `reqwest` so that users don't need OpenSSL installed ([#2385](https://github.com/coral-xyz/anchor/pull/2385)).
- cli: Add `--arch sbf` option to compile programs using `cargo build-sbf` ([#2398](https://github.com/coral-xyz/anchor/pull/2398)).

### Fixes

Expand Down
14 changes: 14 additions & 0 deletions cli/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,20 @@ pub enum BootstrapMode {
Debian,
}

#[derive(ValueEnum, Parser, Clone, PartialEq, Eq, Debug)]
pub enum ProgramArch {
Bpf,
Sbf,
}
impl ProgramArch {
pub fn build_subcommand(&self) -> &str {
match self {
Self::Bpf => "build-bpf",
Self::Sbf => "build-sbf",
}
}
}

#[derive(Debug, Clone)]
pub struct BuildConfig {
pub verifiable: bool,
Expand Down
69 changes: 63 additions & 6 deletions cli/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::config::{
AnchorPackage, BootstrapMode, BuildConfig, Config, ConfigOverride, Manifest, ProgramDeployment,
ProgramWorkspace, ScriptsConfig, TestValidator, WithPath, SHUTDOWN_WAIT, STARTUP_WAIT,
AnchorPackage, BootstrapMode, BuildConfig, Config, ConfigOverride, Manifest, ProgramArch,
ProgramDeployment, ProgramWorkspace, ScriptsConfig, TestValidator, WithPath, SHUTDOWN_WAIT,
STARTUP_WAIT,
};
use anchor_client::Cluster;
use anchor_lang::idl::{IdlAccount, IdlInstruction, ERASED_AUTHORITY};
Expand Down Expand Up @@ -110,6 +111,9 @@ pub enum Command {
/// Suppress doc strings in IDL output
#[clap(long)]
no_docs: bool,
/// Architecture to use when building the program
#[clap(value_enum, long, default_value = "bpf")]
arch: ProgramArch,
},
/// Expands macros (wrapper around cargo expand)
///
Expand Down Expand Up @@ -144,6 +148,9 @@ pub enum Command {
/// verifiable builds. Only works for debian-based images.
#[clap(value_enum, short, long, default_value = "none")]
bootstrap: BootstrapMode,
/// Architecture to use when building the program
#[clap(value_enum, long, default_value = "bpf")]
arch: ProgramArch,
/// Environment variables to pass into the docker container
#[clap(short, long, required = false)]
env: Vec<String>,
Expand Down Expand Up @@ -174,6 +181,9 @@ pub enum Command {
/// use this to save time when running test and the program code is not altered.
#[clap(long)]
skip_build: bool,
/// Architecture to use when building the program
#[clap(value_enum, long, default_value = "bpf")]
arch: ProgramArch,
/// Flag to keep the local validator running after tests
/// to be able to check the transactions.
#[clap(long)]
Expand Down Expand Up @@ -260,6 +270,9 @@ pub enum Command {
/// use this to save time when publishing the program
#[clap(long)]
skip_build: bool,
/// Architecture to use when building the program
#[clap(value_enum, long, default_value = "bpf")]
arch: ProgramArch,
},
/// Keypair commands.
Keys {
Expand All @@ -280,6 +293,9 @@ pub enum Command {
/// no "CHECK" comments where normally required
#[clap(long)]
skip_lint: bool,
/// Architecture to use when building the program
#[clap(value_enum, long, default_value = "bpf")]
arch: ProgramArch,
/// Environment variables to pass into the docker container
#[clap(short, long, required = false)]
env: Vec<String>,
Expand Down Expand Up @@ -412,6 +428,7 @@ pub fn entry(opts: Opts) -> Result<()> {
env,
skip_lint,
no_docs,
arch,
} => build(
&opts.cfg_override,
idl,
Expand All @@ -427,6 +444,7 @@ pub fn entry(opts: Opts) -> Result<()> {
env,
cargo_args,
no_docs,
arch,
),
Command::Verify {
program_id,
Expand All @@ -437,6 +455,7 @@ pub fn entry(opts: Opts) -> Result<()> {
env,
cargo_args,
skip_build,
arch,
} => verify(
&opts.cfg_override,
program_id,
Expand All @@ -447,6 +466,7 @@ pub fn entry(opts: Opts) -> Result<()> {
env,
cargo_args,
skip_build,
arch,
),
Command::Clean => clean(&opts.cfg_override),
Command::Deploy {
Expand All @@ -473,6 +493,7 @@ pub fn entry(opts: Opts) -> Result<()> {
env,
cargo_args,
skip_lint,
arch,
} => test(
&opts.cfg_override,
skip_deploy,
Expand All @@ -484,6 +505,7 @@ pub fn entry(opts: Opts) -> Result<()> {
args,
env,
cargo_args,
arch,
),
#[cfg(feature = "dev")]
Command::Airdrop { .. } => airdrop(&opts.cfg_override),
Expand All @@ -499,21 +521,31 @@ pub fn entry(opts: Opts) -> Result<()> {
env,
cargo_args,
skip_build,
} => publish(&opts.cfg_override, program, env, cargo_args, skip_build),
arch,
} => publish(
&opts.cfg_override,
program,
env,
cargo_args,
skip_build,
arch,
),
Command::Keys { subcmd } => keys(&opts.cfg_override, subcmd),
Command::Localnet {
skip_build,
skip_deploy,
skip_lint,
env,
cargo_args,
arch,
} => localnet(
&opts.cfg_override,
skip_build,
skip_deploy,
skip_lint,
env,
cargo_args,
arch,
),
Command::Account {
account_type,
Expand Down Expand Up @@ -825,6 +857,7 @@ pub fn build(
env_vars: Vec<String>,
cargo_args: Vec<String>,
no_docs: bool,
arch: ProgramArch,
) -> Result<()> {
// Change to the workspace member directory, if needed.
if let Some(program_name) = program_name.as_ref() {
Expand Down Expand Up @@ -872,6 +905,7 @@ pub fn build(
cargo_args,
skip_lint,
no_docs,
arch,
)?,
// If the Cargo.toml is at the root, build the entire workspace.
Some(cargo) if cargo.path().parent() == cfg.path().parent() => build_all(
Expand All @@ -886,6 +920,7 @@ pub fn build(
cargo_args,
skip_lint,
no_docs,
arch,
)?,
// Cargo.toml represents a single package. Build it.
Some(cargo) => build_cwd(
Expand All @@ -900,6 +935,7 @@ pub fn build(
cargo_args,
skip_lint,
no_docs,
&arch,
)?,
}

Expand All @@ -921,6 +957,7 @@ fn build_all(
cargo_args: Vec<String>,
skip_lint: bool,
no_docs: bool,
arch: ProgramArch,
) -> Result<()> {
let cur_dir = std::env::current_dir()?;
let r = match cfg_path.parent() {
Expand All @@ -939,6 +976,7 @@ fn build_all(
cargo_args.clone(),
skip_lint,
no_docs,
&arch,
)?;
}
Ok(())
Expand All @@ -962,13 +1000,14 @@ fn build_cwd(
cargo_args: Vec<String>,
skip_lint: bool,
no_docs: bool,
arch: &ProgramArch,
) -> Result<()> {
match cargo_toml.parent() {
None => return Err(anyhow!("Unable to find parent")),
Some(p) => std::env::set_current_dir(p)?,
};
match build_config.verifiable {
false => _build_cwd(cfg, idl_out, idl_ts_out, skip_lint, cargo_args),
false => _build_cwd(cfg, idl_out, idl_ts_out, skip_lint, arch, cargo_args),
true => build_cwd_verifiable(
cfg,
cargo_toml,
Expand All @@ -979,6 +1018,7 @@ fn build_cwd(
env_vars,
cargo_args,
no_docs,
arch,
),
}
}
Expand All @@ -996,6 +1036,7 @@ fn build_cwd_verifiable(
env_vars: Vec<String>,
cargo_args: Vec<String>,
no_docs: bool,
arch: &ProgramArch,
) -> Result<()> {
// Create output dirs.
let workspace_dir = cfg.path().parent().unwrap().canonicalize()?;
Expand All @@ -1018,6 +1059,7 @@ fn build_cwd_verifiable(
stderr,
env_vars,
cargo_args,
arch,
);

match &result {
Expand Down Expand Up @@ -1066,6 +1108,7 @@ fn docker_build(
stderr: Option<File>,
env_vars: Vec<String>,
cargo_args: Vec<String>,
arch: &ProgramArch,
) -> Result<()> {
let binary_name = Manifest::from_path(&cargo_toml)?.lib_name()?;

Expand Down Expand Up @@ -1120,6 +1163,7 @@ fn docker_build(
stderr,
env_vars,
cargo_args,
arch,
)
});

Expand Down Expand Up @@ -1184,6 +1228,7 @@ fn docker_build_bpf(
stderr: Option<File>,
env_vars: Vec<String>,
cargo_args: Vec<String>,
arch: &ProgramArch,
) -> Result<()> {
let manifest_path =
pathdiff::diff_paths(cargo_toml.canonicalize()?, cfg_parent.canonicalize()?)
Expand All @@ -1194,6 +1239,8 @@ fn docker_build_bpf(
manifest_path.display()
);

let subcommand = arch.build_subcommand();

// Execute the build.
let exit = std::process::Command::new("docker")
.args([
Expand All @@ -1209,7 +1256,7 @@ fn docker_build_bpf(
.args([
container_name,
"cargo",
"build-bpf",
subcommand,
"--manifest-path",
&manifest_path.display().to_string(),
])
Expand Down Expand Up @@ -1299,10 +1346,12 @@ fn _build_cwd(
idl_out: Option<PathBuf>,
idl_ts_out: Option<PathBuf>,
skip_lint: bool,
arch: &ProgramArch,
cargo_args: Vec<String>,
) -> Result<()> {
let subcommand = arch.build_subcommand();
let exit = std::process::Command::new("cargo")
.arg("build-bpf")
.arg(subcommand)
.args(cargo_args)
.stdout(Stdio::inherit())
.stderr(Stdio::inherit())
Expand Down Expand Up @@ -1356,6 +1405,7 @@ fn verify(
env_vars: Vec<String>,
cargo_args: Vec<String>,
skip_build: bool,
arch: ProgramArch,
) -> Result<()> {
// Change to the workspace member directory, if needed.
if let Some(program_name) = program_name.as_ref() {
Expand Down Expand Up @@ -1384,6 +1434,7 @@ fn verify(
env_vars,
cargo_args,
false,
arch,
)?;
}
std::env::set_current_dir(cur_dir)?;
Expand Down Expand Up @@ -2229,6 +2280,7 @@ fn test(
extra_args: Vec<String>,
env_vars: Vec<String>,
cargo_args: Vec<String>,
arch: ProgramArch,
) -> Result<()> {
let test_paths = tests_to_run
.iter()
Expand Down Expand Up @@ -2257,6 +2309,7 @@ fn test(
env_vars,
cargo_args,
false,
arch,
)?;
}

Expand Down Expand Up @@ -3261,6 +3314,7 @@ fn publish(
env_vars: Vec<String>,
cargo_args: Vec<String>,
skip_build: bool,
arch: ProgramArch,
) -> Result<()> {
// Discover the various workspace configs.
let cfg = Config::discover(cfg_override)?.expect("Not in workspace.");
Expand Down Expand Up @@ -3392,6 +3446,7 @@ fn publish(
env_vars,
cargo_args,
true,
arch,
)?;
}

Expand Down Expand Up @@ -3474,6 +3529,7 @@ fn localnet(
skip_lint: bool,
env_vars: Vec<String>,
cargo_args: Vec<String>,
arch: ProgramArch,
) -> Result<()> {
with_workspace(cfg_override, |cfg| {
// Build if needed.
Expand All @@ -3493,6 +3549,7 @@ fn localnet(
env_vars,
cargo_args,
false,
arch,
)?;
}

Expand Down