Skip to content

Commit

Permalink
cargo-build-sbf, cargo-test-sbf: add --arch option
Browse files Browse the repository at this point in the history
--arch allows selecting the target SBF version. See
anza-xyz/llvm-project#26.
  • Loading branch information
alessandrod authored and dmakarov committed Jul 11, 2022
1 parent a3b0943 commit f123d04
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 2 deletions.
24 changes: 24 additions & 0 deletions sdk/cargo-build-sbf/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ struct Config<'a> {
verbose: bool,
workspace: bool,
jobs: Option<String>,
arch: &'a str,
}

impl Default for Config<'_> {
Expand All @@ -56,6 +57,7 @@ impl Default for Config<'_> {
verbose: false,
workspace: false,
jobs: None,
arch: "sbf",
}
}
}
Expand Down Expand Up @@ -527,6 +529,17 @@ fn build_sbf_package(config: &Config, target_directory: &Path, package: &cargo_m
debug!("RUSTFLAGS={}", &rustflags);
};

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",
Expand All @@ -535,6 +548,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");
}
Expand Down Expand Up @@ -818,6 +834,13 @@ fn main() {
.validator(|val| val.parse::<usize>().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");
Expand Down Expand Up @@ -854,6 +877,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<PathBuf> = matches.value_of_t("manifest_path").ok();
build_sbf(config, manifest_path);
Expand Down
17 changes: 15 additions & 2 deletions sdk/cargo-test-sbf/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use {
},
};

struct Config {
struct Config<'a> {
sbf_sdk: Option<String>,
sbf_out_dir: Option<String>,
cargo: PathBuf,
Expand All @@ -25,9 +25,10 @@ struct Config {
verbose: bool,
workspace: bool,
jobs: Option<String>,
arch: &'a str,
}

impl Default for Config {
impl Default for Config<'_> {
fn default() -> Self {
Self {
sbf_sdk: None,
Expand All @@ -44,6 +45,7 @@ impl Default for Config {
verbose: false,
workspace: false,
jobs: None,
arch: "sbf",
}
}
}
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -311,6 +316,13 @@ fn main() {
.validator(|val| val.parse::<usize>().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")
Expand All @@ -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()
};

Expand Down

0 comments on commit f123d04

Please sign in to comment.