Skip to content

Commit 06c4232

Browse files
authored
cli: Add arch option to use build-sbf or build-bpf (solana-foundation#2398)
1 parent 45fa9bd commit 06c4232

File tree

3 files changed

+78
-6
lines changed

3 files changed

+78
-6
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ The minor version will be incremented upon a breaking change and the patch versi
2323
- client: Add support for multithreading to the rust client: use flag `--multithreaded` ([#2321](https://github.com/coral-xyz/anchor/pull/2321)).
2424
- client: Add `async_rpc` a method which returns a nonblocking solana rpc client ([2322](https://github.com/coral-xyz/anchor/pull/2322)).
2525
- 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)).
26+
- cli: Add `--arch sbf` option to compile programs using `cargo build-sbf` ([#2398](https://github.com/coral-xyz/anchor/pull/2398)).
2627

2728
### Fixes
2829

cli/src/config.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -330,6 +330,20 @@ pub enum BootstrapMode {
330330
Debian,
331331
}
332332

333+
#[derive(ValueEnum, Parser, Clone, PartialEq, Eq, Debug)]
334+
pub enum ProgramArch {
335+
Bpf,
336+
Sbf,
337+
}
338+
impl ProgramArch {
339+
pub fn build_subcommand(&self) -> &str {
340+
match self {
341+
Self::Bpf => "build-bpf",
342+
Self::Sbf => "build-sbf",
343+
}
344+
}
345+
}
346+
333347
#[derive(Debug, Clone)]
334348
pub struct BuildConfig {
335349
pub verifiable: bool,

cli/src/lib.rs

Lines changed: 63 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use crate::config::{
2-
AnchorPackage, BootstrapMode, BuildConfig, Config, ConfigOverride, Manifest, ProgramDeployment,
3-
ProgramWorkspace, ScriptsConfig, TestValidator, WithPath, SHUTDOWN_WAIT, STARTUP_WAIT,
2+
AnchorPackage, BootstrapMode, BuildConfig, Config, ConfigOverride, Manifest, ProgramArch,
3+
ProgramDeployment, ProgramWorkspace, ScriptsConfig, TestValidator, WithPath, SHUTDOWN_WAIT,
4+
STARTUP_WAIT,
45
};
56
use anchor_client::Cluster;
67
use anchor_lang::idl::{IdlAccount, IdlInstruction, ERASED_AUTHORITY};
@@ -110,6 +111,9 @@ pub enum Command {
110111
/// Suppress doc strings in IDL output
111112
#[clap(long)]
112113
no_docs: bool,
114+
/// Architecture to use when building the program
115+
#[clap(value_enum, long, default_value = "bpf")]
116+
arch: ProgramArch,
113117
},
114118
/// Expands macros (wrapper around cargo expand)
115119
///
@@ -144,6 +148,9 @@ pub enum Command {
144148
/// verifiable builds. Only works for debian-based images.
145149
#[clap(value_enum, short, long, default_value = "none")]
146150
bootstrap: BootstrapMode,
151+
/// Architecture to use when building the program
152+
#[clap(value_enum, long, default_value = "bpf")]
153+
arch: ProgramArch,
147154
/// Environment variables to pass into the docker container
148155
#[clap(short, long, required = false)]
149156
env: Vec<String>,
@@ -174,6 +181,9 @@ pub enum Command {
174181
/// use this to save time when running test and the program code is not altered.
175182
#[clap(long)]
176183
skip_build: bool,
184+
/// Architecture to use when building the program
185+
#[clap(value_enum, long, default_value = "bpf")]
186+
arch: ProgramArch,
177187
/// Flag to keep the local validator running after tests
178188
/// to be able to check the transactions.
179189
#[clap(long)]
@@ -260,6 +270,9 @@ pub enum Command {
260270
/// use this to save time when publishing the program
261271
#[clap(long)]
262272
skip_build: bool,
273+
/// Architecture to use when building the program
274+
#[clap(value_enum, long, default_value = "bpf")]
275+
arch: ProgramArch,
263276
},
264277
/// Keypair commands.
265278
Keys {
@@ -280,6 +293,9 @@ pub enum Command {
280293
/// no "CHECK" comments where normally required
281294
#[clap(long)]
282295
skip_lint: bool,
296+
/// Architecture to use when building the program
297+
#[clap(value_enum, long, default_value = "bpf")]
298+
arch: ProgramArch,
283299
/// Environment variables to pass into the docker container
284300
#[clap(short, long, required = false)]
285301
env: Vec<String>,
@@ -412,6 +428,7 @@ pub fn entry(opts: Opts) -> Result<()> {
412428
env,
413429
skip_lint,
414430
no_docs,
431+
arch,
415432
} => build(
416433
&opts.cfg_override,
417434
idl,
@@ -427,6 +444,7 @@ pub fn entry(opts: Opts) -> Result<()> {
427444
env,
428445
cargo_args,
429446
no_docs,
447+
arch,
430448
),
431449
Command::Verify {
432450
program_id,
@@ -437,6 +455,7 @@ pub fn entry(opts: Opts) -> Result<()> {
437455
env,
438456
cargo_args,
439457
skip_build,
458+
arch,
440459
} => verify(
441460
&opts.cfg_override,
442461
program_id,
@@ -447,6 +466,7 @@ pub fn entry(opts: Opts) -> Result<()> {
447466
env,
448467
cargo_args,
449468
skip_build,
469+
arch,
450470
),
451471
Command::Clean => clean(&opts.cfg_override),
452472
Command::Deploy {
@@ -473,6 +493,7 @@ pub fn entry(opts: Opts) -> Result<()> {
473493
env,
474494
cargo_args,
475495
skip_lint,
496+
arch,
476497
} => test(
477498
&opts.cfg_override,
478499
skip_deploy,
@@ -484,6 +505,7 @@ pub fn entry(opts: Opts) -> Result<()> {
484505
args,
485506
env,
486507
cargo_args,
508+
arch,
487509
),
488510
#[cfg(feature = "dev")]
489511
Command::Airdrop { .. } => airdrop(&opts.cfg_override),
@@ -499,21 +521,31 @@ pub fn entry(opts: Opts) -> Result<()> {
499521
env,
500522
cargo_args,
501523
skip_build,
502-
} => publish(&opts.cfg_override, program, env, cargo_args, skip_build),
524+
arch,
525+
} => publish(
526+
&opts.cfg_override,
527+
program,
528+
env,
529+
cargo_args,
530+
skip_build,
531+
arch,
532+
),
503533
Command::Keys { subcmd } => keys(&opts.cfg_override, subcmd),
504534
Command::Localnet {
505535
skip_build,
506536
skip_deploy,
507537
skip_lint,
508538
env,
509539
cargo_args,
540+
arch,
510541
} => localnet(
511542
&opts.cfg_override,
512543
skip_build,
513544
skip_deploy,
514545
skip_lint,
515546
env,
516547
cargo_args,
548+
arch,
517549
),
518550
Command::Account {
519551
account_type,
@@ -825,6 +857,7 @@ pub fn build(
825857
env_vars: Vec<String>,
826858
cargo_args: Vec<String>,
827859
no_docs: bool,
860+
arch: ProgramArch,
828861
) -> Result<()> {
829862
// Change to the workspace member directory, if needed.
830863
if let Some(program_name) = program_name.as_ref() {
@@ -872,6 +905,7 @@ pub fn build(
872905
cargo_args,
873906
skip_lint,
874907
no_docs,
908+
arch,
875909
)?,
876910
// If the Cargo.toml is at the root, build the entire workspace.
877911
Some(cargo) if cargo.path().parent() == cfg.path().parent() => build_all(
@@ -886,6 +920,7 @@ pub fn build(
886920
cargo_args,
887921
skip_lint,
888922
no_docs,
923+
arch,
889924
)?,
890925
// Cargo.toml represents a single package. Build it.
891926
Some(cargo) => build_cwd(
@@ -900,6 +935,7 @@ pub fn build(
900935
cargo_args,
901936
skip_lint,
902937
no_docs,
938+
&arch,
903939
)?,
904940
}
905941

@@ -921,6 +957,7 @@ fn build_all(
921957
cargo_args: Vec<String>,
922958
skip_lint: bool,
923959
no_docs: bool,
960+
arch: ProgramArch,
924961
) -> Result<()> {
925962
let cur_dir = std::env::current_dir()?;
926963
let r = match cfg_path.parent() {
@@ -939,6 +976,7 @@ fn build_all(
939976
cargo_args.clone(),
940977
skip_lint,
941978
no_docs,
979+
&arch,
942980
)?;
943981
}
944982
Ok(())
@@ -962,13 +1000,14 @@ fn build_cwd(
9621000
cargo_args: Vec<String>,
9631001
skip_lint: bool,
9641002
no_docs: bool,
1003+
arch: &ProgramArch,
9651004
) -> Result<()> {
9661005
match cargo_toml.parent() {
9671006
None => return Err(anyhow!("Unable to find parent")),
9681007
Some(p) => std::env::set_current_dir(p)?,
9691008
};
9701009
match build_config.verifiable {
971-
false => _build_cwd(cfg, idl_out, idl_ts_out, skip_lint, cargo_args),
1010+
false => _build_cwd(cfg, idl_out, idl_ts_out, skip_lint, arch, cargo_args),
9721011
true => build_cwd_verifiable(
9731012
cfg,
9741013
cargo_toml,
@@ -979,6 +1018,7 @@ fn build_cwd(
9791018
env_vars,
9801019
cargo_args,
9811020
no_docs,
1021+
arch,
9821022
),
9831023
}
9841024
}
@@ -996,6 +1036,7 @@ fn build_cwd_verifiable(
9961036
env_vars: Vec<String>,
9971037
cargo_args: Vec<String>,
9981038
no_docs: bool,
1039+
arch: &ProgramArch,
9991040
) -> Result<()> {
10001041
// Create output dirs.
10011042
let workspace_dir = cfg.path().parent().unwrap().canonicalize()?;
@@ -1018,6 +1059,7 @@ fn build_cwd_verifiable(
10181059
stderr,
10191060
env_vars,
10201061
cargo_args,
1062+
arch,
10211063
);
10221064

10231065
match &result {
@@ -1066,6 +1108,7 @@ fn docker_build(
10661108
stderr: Option<File>,
10671109
env_vars: Vec<String>,
10681110
cargo_args: Vec<String>,
1111+
arch: &ProgramArch,
10691112
) -> Result<()> {
10701113
let binary_name = Manifest::from_path(&cargo_toml)?.lib_name()?;
10711114

@@ -1120,6 +1163,7 @@ fn docker_build(
11201163
stderr,
11211164
env_vars,
11221165
cargo_args,
1166+
arch,
11231167
)
11241168
});
11251169

@@ -1184,6 +1228,7 @@ fn docker_build_bpf(
11841228
stderr: Option<File>,
11851229
env_vars: Vec<String>,
11861230
cargo_args: Vec<String>,
1231+
arch: &ProgramArch,
11871232
) -> Result<()> {
11881233
let manifest_path =
11891234
pathdiff::diff_paths(cargo_toml.canonicalize()?, cfg_parent.canonicalize()?)
@@ -1194,6 +1239,8 @@ fn docker_build_bpf(
11941239
manifest_path.display()
11951240
);
11961241

1242+
let subcommand = arch.build_subcommand();
1243+
11971244
// Execute the build.
11981245
let exit = std::process::Command::new("docker")
11991246
.args([
@@ -1209,7 +1256,7 @@ fn docker_build_bpf(
12091256
.args([
12101257
container_name,
12111258
"cargo",
1212-
"build-bpf",
1259+
subcommand,
12131260
"--manifest-path",
12141261
&manifest_path.display().to_string(),
12151262
])
@@ -1299,10 +1346,12 @@ fn _build_cwd(
12991346
idl_out: Option<PathBuf>,
13001347
idl_ts_out: Option<PathBuf>,
13011348
skip_lint: bool,
1349+
arch: &ProgramArch,
13021350
cargo_args: Vec<String>,
13031351
) -> Result<()> {
1352+
let subcommand = arch.build_subcommand();
13041353
let exit = std::process::Command::new("cargo")
1305-
.arg("build-bpf")
1354+
.arg(subcommand)
13061355
.args(cargo_args)
13071356
.stdout(Stdio::inherit())
13081357
.stderr(Stdio::inherit())
@@ -1356,6 +1405,7 @@ fn verify(
13561405
env_vars: Vec<String>,
13571406
cargo_args: Vec<String>,
13581407
skip_build: bool,
1408+
arch: ProgramArch,
13591409
) -> Result<()> {
13601410
// Change to the workspace member directory, if needed.
13611411
if let Some(program_name) = program_name.as_ref() {
@@ -1384,6 +1434,7 @@ fn verify(
13841434
env_vars,
13851435
cargo_args,
13861436
false,
1437+
arch,
13871438
)?;
13881439
}
13891440
std::env::set_current_dir(cur_dir)?;
@@ -2229,6 +2280,7 @@ fn test(
22292280
extra_args: Vec<String>,
22302281
env_vars: Vec<String>,
22312282
cargo_args: Vec<String>,
2283+
arch: ProgramArch,
22322284
) -> Result<()> {
22332285
let test_paths = tests_to_run
22342286
.iter()
@@ -2257,6 +2309,7 @@ fn test(
22572309
env_vars,
22582310
cargo_args,
22592311
false,
2312+
arch,
22602313
)?;
22612314
}
22622315

@@ -3261,6 +3314,7 @@ fn publish(
32613314
env_vars: Vec<String>,
32623315
cargo_args: Vec<String>,
32633316
skip_build: bool,
3317+
arch: ProgramArch,
32643318
) -> Result<()> {
32653319
// Discover the various workspace configs.
32663320
let cfg = Config::discover(cfg_override)?.expect("Not in workspace.");
@@ -3392,6 +3446,7 @@ fn publish(
33923446
env_vars,
33933447
cargo_args,
33943448
true,
3449+
arch,
33953450
)?;
33963451
}
33973452

@@ -3474,6 +3529,7 @@ fn localnet(
34743529
skip_lint: bool,
34753530
env_vars: Vec<String>,
34763531
cargo_args: Vec<String>,
3532+
arch: ProgramArch,
34773533
) -> Result<()> {
34783534
with_workspace(cfg_override, |cfg| {
34793535
// Build if needed.
@@ -3493,6 +3549,7 @@ fn localnet(
34933549
env_vars,
34943550
cargo_args,
34953551
false,
3552+
arch,
34963553
)?;
34973554
}
34983555

0 commit comments

Comments
 (0)