From ab6802bd2d6741908c98fe02a765948cb263f4f6 Mon Sep 17 00:00:00 2001 From: Alessandro Decina Date: Thu, 3 Mar 2022 17:51:08 +1100 Subject: [PATCH] cargo-build-sbf, cargo-test-sbf: add --arch option --arch allows selecting the target SBF version. See https://github.com/solana-labs/llvm-project/pull/26. --- sdk/cargo-build-sbf/src/main.rs | 24 ++++++++++++++++++++++++ sdk/cargo-test-sbf/src/main.rs | 17 +++++++++++++++-- 2 files changed, 39 insertions(+), 2 deletions(-) diff --git a/sdk/cargo-build-sbf/src/main.rs b/sdk/cargo-build-sbf/src/main.rs index d4135727b7..3c15d1f98d 100644 --- a/sdk/cargo-build-sbf/src/main.rs +++ b/sdk/cargo-build-sbf/src/main.rs @@ -33,6 +33,7 @@ struct Config<'a> { verbose: bool, workspace: bool, jobs: Option, + arch: &'a str, } impl Default for Config<'_> { @@ -57,6 +58,7 @@ impl Default for Config<'_> { verbose: false, workspace: false, jobs: None, + arch: "sbf", } } } @@ -542,6 +544,17 @@ fn build_sbf_package(config: &Config, target_directory: &Path, package: &cargo_m env::remove_var("RUSTC") } + let mut target_rustflags = env::var("CARGO_TARGET_SBF_SOLANA_SOLANA_RUSTFLAGS") + .ok() + .unwrap_or_default(); + if config.arch == "sbfv2" { + target_rustflags = format!("{} {}", "-C target_cpu=sbfv2", target_rustflags); + env::set_var( + "CARGO_TARGET_SBF_SOLANA_SOLANA_RUSTFLAGS", + &target_rustflags, + ); + } + let cargo_build = PathBuf::from("cargo"); let mut cargo_build_args = vec![ "+sbf", @@ -550,6 +563,9 @@ fn build_sbf_package(config: &Config, target_directory: &Path, package: &cargo_m "sbf-solana-solana", "--release", ]; + if config.arch == "sbfv2" { + cargo_build_args.push("-Zbuild-std=std,panic_abort"); + } if config.no_default_features { cargo_build_args.push("--no-default-features"); } @@ -833,6 +849,13 @@ fn main() { .validator(|val| val.parse::().map_err(|e| e.to_string())) .help("Number of parallel jobs, defaults to # of CPUs"), ) + .arg( + Arg::new("arch") + .long("arch") + .possible_values(&["sbf", "sbfv2"]) + .default_value("sbf") + .help("Build for the given SBF version"), + ) .get_matches_from(args); let sbf_sdk: PathBuf = matches.value_of_t_or_exit("sbf_sdk"); @@ -869,6 +892,7 @@ fn main() { verbose: matches.is_present("verbose"), workspace: matches.is_present("workspace"), jobs: matches.value_of_t("jobs").ok(), + arch: matches.value_of("arch").unwrap(), }; let manifest_path: Option = matches.value_of_t("manifest_path").ok(); if config.verbose { diff --git a/sdk/cargo-test-sbf/src/main.rs b/sdk/cargo-test-sbf/src/main.rs index e5e76574c0..90e6dab095 100644 --- a/sdk/cargo-test-sbf/src/main.rs +++ b/sdk/cargo-test-sbf/src/main.rs @@ -10,7 +10,7 @@ use { }, }; -struct Config { +struct Config<'a> { sbf_sdk: Option, sbf_out_dir: Option, cargo: PathBuf, @@ -25,9 +25,10 @@ struct Config { verbose: bool, workspace: bool, jobs: Option, + arch: &'a str, } -impl Default for Config { +impl Default for Config<'_> { fn default() -> Self { Self { sbf_sdk: None, @@ -44,6 +45,7 @@ impl Default for Config { verbose: false, workspace: false, jobs: None, + arch: "sbf", } } } @@ -129,6 +131,9 @@ fn test_sbf_package(config: &Config, target_directory: &Path, package: &cargo_me build_sbf_args.push("--sbf-out-dir"); build_sbf_args.push(&sbf_out_dir); + build_sbf_args.push("--arch"); + build_sbf_args.push(config.arch); + spawn( &config.cargo_build_sbf, &build_sbf_args, @@ -311,6 +316,13 @@ fn main() { .validator(|val| val.parse::().map_err(|e| e.to_string())) .help("Number of parallel jobs, defaults to # of CPUs"), ) + .arg( + Arg::new("arch") + .long("arch") + .possible_values(&["sbf", "sbfv2"]) + .default_value("sbf") + .help("Build for the given SBF version"), + ) .arg( Arg::new("extra_cargo_test_args") .value_name("extra args for cargo test and the test binary") @@ -337,6 +349,7 @@ fn main() { verbose: matches.is_present("verbose"), workspace: matches.is_present("workspace"), jobs: matches.value_of_t("jobs").ok(), + arch: matches.value_of("arch").unwrap(), ..Config::default() };