-
Notifications
You must be signed in to change notification settings - Fork 4.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
167 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
[target.bpfel-unknown-unknown.dependencies.std] | ||
features = [] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
#!/usr/bin/env bash | ||
|
||
usage() { | ||
cat <<EOF | ||
cargo-build-bpf | ||
Compile a local package and all of its dependencies as Solana BPF | ||
USAGE: | ||
cargo build-bpf [OPTIONS] | ||
OPTIONS: | ||
-h, --help Prints help information | ||
... All other options are passed verbatum to "xargo build" | ||
EOF | ||
exit 0 | ||
} | ||
|
||
if [[ $1 = build-bpf ]]; then | ||
# When run as `cargo build-bpf`, "build-bpf" is set as arg 1. Remove it | ||
shift | ||
fi | ||
|
||
bpf_sdk=$(cd "$(dirname "$0")"/sdk/bpf && pwd) | ||
|
||
args=() | ||
while [[ -n $1 ]]; do | ||
if [[ $1 = -h ]] || [[ $1 = --help ]]; then | ||
usage | ||
fi | ||
args+=("$1") | ||
shift 1 | ||
done | ||
|
||
if [[ ! -d $bpf_sdk ]]; then | ||
echo "Error: not a directory: $bpf_sdk" | ||
exit 1 | ||
fi | ||
|
||
exec "$bpf_sdk"/rust/xargo-build.sh "${args[@]}" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
# | ||
# Configures the BPF SDK environment | ||
# | ||
|
||
if [ -z "$bpf_sdk" ]; then | ||
bpf_sdk=. | ||
fi | ||
|
||
# Ensure the sdk is installed | ||
"$bpf_sdk"/scripts/install.sh | ||
|
||
# Use the SDK's version of llvm to build the compiler-builtins for BPF | ||
export CC="$bpf_sdk/dependencies/llvm-native/bin/clang" | ||
export AR="$bpf_sdk/dependencies/llvm-native/bin/llvm-ar" | ||
export OBJDUMP="$bpf_sdk/dependencies/llvm-native/bin/llvm-objdump" | ||
export OBJCOPY="$bpf_sdk/dependencies/llvm-native/bin/llvm-objcopy" | ||
|
||
# Use the SDK's version of Rust to build for BPF | ||
export RUSTUP_TOOLCHAIN=bpf | ||
export RUSTFLAGS=" | ||
-C lto=no \ | ||
-C opt-level=2 \ | ||
-C link-arg=-z -C link-arg=notext \ | ||
-C link-arg=-T$bpf_sdk/rust/bpf.ld \ | ||
-C link-arg=--Bdynamic \ | ||
-C link-arg=-shared \ | ||
-C link-arg=--entry=entrypoint \ | ||
-C link-arg=-no-threads \ | ||
-C linker=$bpf_sdk/dependencies/llvm-native/bin/ld.lld" | ||
|
||
# CARGO may be set if run from within cargo, causing | ||
# incompatibilities between cargo and xargo versions | ||
unset CARGO | ||
|
||
export XARGO="$bpf_sdk"/dependencies/xargo/bin/xargo | ||
export XARGO_TARGET=bpfel-unknown-unknown | ||
export XARGO_HOME="$bpf_sdk/dependencies/xargo" | ||
export XARGO_RUST_SRC="$bpf_sdk/dependencies/rust-bpf-sysroot/src" | ||
export RUST_COMPILER_RT_ROOT="$bpf_sdk/dependencies/rust-bpf-sysroot/src/compiler-rt" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,49 +1,21 @@ | ||
#!/usr/bin/env bash | ||
|
||
if [ "$#" -ne 1 ]; then | ||
if [[ "$#" -ne 1 ]]; then | ||
echo "Error: Must provide the full path to the project to build" | ||
exit 1 | ||
fi | ||
if [ ! -f "$1/Cargo.toml" ]; then | ||
if [[ ! -f "$1/Cargo.toml" ]]; then | ||
echo "Error: Cannot find project: $1" | ||
exit 1 | ||
fi | ||
bpf_sdk=$(cd "$(dirname "$0")/.." && pwd) | ||
|
||
echo "Building $1" | ||
set -e | ||
|
||
bpf_sdk=$(cd "$(dirname "$0")/.." && pwd) | ||
|
||
# Ensure the sdk is installed | ||
"$bpf_sdk"/scripts/install.sh | ||
|
||
# Use the SDK's version of llvm to build the compiler-builtins for BPF | ||
export CC="$bpf_sdk/dependencies/llvm-native/bin/clang" | ||
export AR="$bpf_sdk/dependencies/llvm-native/bin/llvm-ar" | ||
|
||
# Use the SDK's version of Rust to build for BPF | ||
export RUSTUP_TOOLCHAIN=bpf | ||
export RUSTFLAGS=" | ||
-C lto=no \ | ||
-C opt-level=2 \ | ||
-C link-arg=-z -C link-arg=notext \ | ||
-C link-arg=-T$bpf_sdk/rust/bpf.ld \ | ||
-C link-arg=--Bdynamic \ | ||
-C link-arg=-shared \ | ||
-C link-arg=--entry=entrypoint \ | ||
-C link-arg=-no-threads \ | ||
-C linker=$bpf_sdk/dependencies/llvm-native/bin/ld.lld" | ||
|
||
# CARGO may be set if build.sh is run from within cargo, causing | ||
# incompatibilities between cargo and xargo versions | ||
unset CARGO | ||
|
||
# Setup xargo | ||
export XARGO_HOME="$bpf_sdk/dependencies/xargo" | ||
export XARGO_RUST_SRC="$bpf_sdk/dependencies/rust-bpf-sysroot/src" | ||
export RUST_COMPILER_RT_ROOT="$bpf_sdk/dependencies/rust-bpf-sysroot/src/compiler-rt" | ||
# shellcheck source=sdk/bpf/env.sh | ||
source "$bpf_sdk"/env.sh | ||
|
||
cd "$1" | ||
xargo build --target bpfel-unknown-unknown --release --no-default-features --features program | ||
"$XARGO" build --target "$XARGO_TARGET" --release --no-default-features --features program | ||
|
||
{ { set +x; } 2>/dev/null; echo Success; } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
#!/usr/bin/env bash | ||
|
||
bpf_sdk=$(cd "$(dirname "$0")/.." && pwd) | ||
# shellcheck source=sdk/bpf/env.sh | ||
source "$bpf_sdk"/env.sh | ||
set -x | ||
exec "$XARGO" build --target "$XARGO_TARGET" --release "$@" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
#!/usr/bin/env bash | ||
|
||
bpf_sdk=$(cd "$(dirname "$0")/.." && pwd) | ||
# shellcheck source=sdk/bpf/env.sh | ||
source "$bpf_sdk"/env.sh | ||
|
||
so="$1" | ||
if [[ -z $so ]]; then | ||
echo "Usage: $0 bpf-program.so" | ||
exit 1 | ||
fi | ||
|
||
if [[ ! -r $so ]]; then | ||
echo "Error: File not found or readable: $so" | ||
exit 1 | ||
fi | ||
|
||
if ! command -v rustfilt > /dev/null; then | ||
echo "Error: rustfilt not found. It can be installed by running: cargo install rustfilt" | ||
exit 1 | ||
fi | ||
if ! command -v readelf > /dev/null; then | ||
if [[ $(uname) = Darwin ]]; then | ||
echo "Error: readelf not found. It can be installed by running: brew install binutils" | ||
else | ||
echo "Error: readelf not found." | ||
fi | ||
exit 1 | ||
fi | ||
|
||
dump="${so%.*}"_dump.txt | ||
dump_mangled=$(basename "$so")_dump-mangled.txt | ||
|
||
( | ||
set -ex | ||
ls -la "$so" > "$dump_mangled" | ||
readelf -aW "$so" >>"$dump_mangled" | ||
"$OBJDUMP" -print-imm-hex --source --disassemble "$so" >> "$dump_mangled" | ||
sed s/://g < "$dump_mangled" | rustfilt > "$dump" | ||
) | ||
rm -f "$dump_mangled" | ||
|
||
if [[ ! -f "$dump" ]]; then | ||
echo "Error: Failed to create $dump" | ||
exit 1 | ||
fi | ||
|
||
echo "Created $dump" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
#!/usr/bin/env bash | ||
|
||
bpf_sdk=$(cd "$(dirname "$0")/.." && pwd) | ||
# shellcheck source=sdk/bpf/env.sh | ||
source "$bpf_sdk"/env.sh | ||
exec "$bpf_sdk"/dependencies/llvm-native/bin/llvm-objcopy "$@" |