Skip to content

Commit

Permalink
cargo-build-bpf: 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 committed Mar 3, 2022
1 parent 61d7bdd commit 4059fa4
Showing 1 changed file with 19 additions and 4 deletions.
23 changes: 19 additions & 4 deletions sdk/cargo-build-bpf/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ struct Config<'a> {
offline: bool,
verbose: bool,
workspace: bool,
arch: &'a str,
}

impl Default for Config<'_> {
Expand All @@ -53,6 +54,7 @@ impl Default for Config<'_> {
offline: false,
verbose: false,
workspace: false,
arch: "sbf",
}
}
}
Expand Down Expand Up @@ -516,11 +518,13 @@ fn build_bpf_package(config: &Config, target_directory: &Path, package: &cargo_m
env::set_var("OBJDUMP", llvm_bin.join("llvm-objdump"));
env::set_var("OBJCOPY", llvm_bin.join("llvm-objcopy"));

let mut rustflags = env::var("RUSTFLAGS").ok().unwrap_or_else(String::new);
if config.arch == "sbfv2" {
rustflags = format!("{} {}", "-C target_cpu=sbfv2", rustflags);
env::set_var("RUSTFLAGS", &rustflags);
}
if config.verbose {
println!(
"RUSTFLAGS={}",
env::var("RUSTFLAGS").ok().as_deref().unwrap_or("")
);
println!("RUSTFLAGS={}", rustflags);
};

let cargo_build = PathBuf::from("cargo");
Expand All @@ -531,6 +535,9 @@ fn build_bpf_package(config: &Config, target_directory: &Path, package: &cargo_m
"bpfel-unknown-unknown",
"--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 @@ -793,6 +800,13 @@ fn main() {
.alias("all")
.help("Build all BPF packages in the workspace"),
)
.arg(
Arg::with_name("arch")
.long("arch")
.possible_values(&["sbf", "sbfv2"])
.default_value("sbf")
.help("Build for the given SBF version"),
)
.get_matches_from(args);

let bpf_sdk = value_t_or_exit!(matches, "bpf_sdk", PathBuf);
Expand Down Expand Up @@ -829,6 +843,7 @@ fn main() {
offline: matches.is_present("offline"),
verbose: matches.is_present("verbose"),
workspace: matches.is_present("workspace"),
arch: matches.value_of("arch").unwrap(),
};
let manifest_path = value_t!(matches, "manifest_path", PathBuf).ok();
build_bpf(config, manifest_path);
Expand Down

0 comments on commit 4059fa4

Please sign in to comment.