Skip to content

Commit

Permalink
Add --bpf-out-dir argument to control where the final build products …
Browse files Browse the repository at this point in the history
…land
  • Loading branch information
mvines committed Oct 23, 2020
1 parent 90e6812 commit d831670
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 22 deletions.
19 changes: 1 addition & 18 deletions programs/bpf/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,6 @@ fn main() {
let install_dir =
"target/".to_string() + &env::var("PROFILE").unwrap() + &"/bpf".to_string();

assert!(Command::new("mkdir")
.arg("-p")
.arg(&install_dir)
.status()
.expect("Unable to create BPF install directory")
.success());

let rust_programs = [
"128bit",
"alloc",
Expand Down Expand Up @@ -96,20 +89,10 @@ fn main() {
);
assert!(Command::new("bash")
.current_dir(format!("rust/{}", program))
.args(&["../../../../cargo-build-bpf"])
.args(&["../../../../cargo-build-bpf", "--bpf-out-dir", &install_dir])
.status()
.expect("Error calling cargo-build-bpf from build.rs")
.success());
let src = format!(
"rust/{0}/solana_bpf_rust_{0}.so",
program,
);
assert!(Command::new("mv")
.arg(&src)
.arg(&install_dir)
.status()
.unwrap_or_else(|_| panic!("Failed to cp {} to {}", src, install_dir))
.success());
}

rerun_if_changed(&[], &["rust", "../../sdk", &install_dir], &["/target/"]);
Expand Down
5 changes: 5 additions & 0 deletions sdk/bpf/scripts/dump.sh
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ if ! command -v readelf > /dev/null; then
exit 1
fi

set -e
out_dir=$(dirname "$dump")
if [[ ! -d $out_dir ]]; then
mkdir -p "$out_dir"
fi
dump_mangled=$dump.mangled

(
Expand Down
6 changes: 6 additions & 0 deletions sdk/bpf/scripts/strip.sh
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,10 @@ fi
bpf_sdk=$(cd "$(dirname "$0")/.." && pwd)
# shellcheck source=sdk/bpf/env.sh
source "$bpf_sdk"/env.sh

set -e
out_dir=$(dirname "$so_stripped")
if [[ ! -d $out_dir ]]; then
mkdir -p "$out_dir"
fi
"$bpf_sdk"/dependencies/llvm-native/bin/llvm-objcopy --strip-all "$so" "$so_stripped"
30 changes: 26 additions & 4 deletions sdk/cargo-build-bpf/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ use std::{

struct Config {
bpf_sdk: PathBuf,
bpf_out_dir: PathBuf,
dump: bool,
features: Vec<String>,
manifest_path: Option<PathBuf>,
Expand All @@ -22,11 +23,12 @@ impl Default for Config {
fn default() -> Self {
Self {
bpf_sdk: env::current_exe()
.expect("Unable to get current directory")
.expect("Unable to get current executable")
.parent()
.expect("Unable to get parent directory")
.to_path_buf()
.join("sdk/bpf"),
bpf_out_dir: env::current_dir().expect("Unable to get current directory"),
features: vec![],
manifest_path: None,
no_default_features: false,
Expand Down Expand Up @@ -165,8 +167,10 @@ fn build_bpf(config: Config) {

if let Some(program_name) = program_name {
let program_unstripped_so = target_build_directory.join(&format!("{}.so", program_name));
let program_dump = PathBuf::from(format!("{}-dump.txt", program_name));
let program_so = PathBuf::from(format!("{}.so", program_name));
let program_dump = config
.bpf_out_dir
.join(&format!("{}-dump.txt", program_name));
let program_so = config.bpf_out_dir.join(&format!("{}.so", program_name));

spawn(
&config.bpf_sdk.join("scripts/strip.sh"),
Expand All @@ -185,7 +189,9 @@ fn build_bpf(config: Config) {
}

fn main() {
let default_bpf_sdk = format!("{}", Config::default().bpf_sdk.display());
let default_config = Config::default();
let default_bpf_sdk = format!("{}", default_config.bpf_sdk.display());
let default_bpf_out_dir = format!("{}", default_config.bpf_out_dir.display());

let mut args = env::args().collect::<Vec<_>>();
// When run as a cargo subcommand, the first program argument is the subcommand name.
Expand Down Expand Up @@ -234,9 +240,18 @@ fn main() {
.takes_value(true)
.help("Path to Cargo.toml"),
)
.arg(
Arg::with_name("bpf_out_dir")
.long("bpf-out-dir")
.value_name("DIRECTORY")
.takes_value(true)
.default_value(&default_bpf_out_dir)
.help("Place final BPF build artifacts in this directory"),
)
.get_matches_from(args);

let bpf_sdk = value_t_or_exit!(matches, "bpf_sdk", PathBuf);
let bpf_out_dir = value_t_or_exit!(matches, "bpf_out_dir", PathBuf);

let config = Config {
bpf_sdk: fs::canonicalize(&bpf_sdk).unwrap_or_else(|err| {
Expand All @@ -247,6 +262,13 @@ fn main() {
);
exit(1);
}),
bpf_out_dir: if bpf_out_dir.is_absolute() {
bpf_out_dir
} else {
env::current_dir()
.expect("Unable to get current working directory")
.join(bpf_out_dir)
},
dump: matches.is_present("dump"),
features: values_t!(matches, "features", String)
.ok()
Expand Down

0 comments on commit d831670

Please sign in to comment.