Skip to content

Commit

Permalink
Add cargo-build-bpf
Browse files Browse the repository at this point in the history
  • Loading branch information
mvines committed Oct 22, 2020
1 parent e6b821c commit c648ddc
Show file tree
Hide file tree
Showing 15 changed files with 474 additions and 50 deletions.
28 changes: 24 additions & 4 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ members = [
"ramp-tps",
"runtime",
"sdk",
"sdk/cargo-build-bpf",
"scripts",
"stake-accounts",
"stake-monitor",
Expand Down
5 changes: 5 additions & 0 deletions cargo-build-bpf
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/usr/bin/env bash

here=$(dirname "$0")
set -x
exec cargo run --manifest-path $here/sdk/cargo-build-bpf/Cargo.toml -- --bpf-sdk $here/sdk/bpf "$@"
1 change: 1 addition & 0 deletions ci/nits.sh
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ declare print_free_tree=(
':runtime/src/**.rs'
':sdk/bpf/rust/rust-utils/**.rs'
':sdk/**.rs'
':^sdk/cargo-build-bpf/**.rs'
':^sdk/src/program_option.rs'
':^sdk/src/program_stubs.rs'
':programs/**.rs'
Expand Down
2 changes: 2 additions & 0 deletions frozen-abi/Xargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[target.bpfel-unknown-unknown.dependencies.std]
features = []
1 change: 1 addition & 0 deletions scripts/cargo-install-all.sh
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ else


BINS=(
cargo-build-bpf
solana
solana-bench-exchange
solana-bench-tps
Expand Down
39 changes: 39 additions & 0 deletions sdk/bpf/env.sh
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/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"
40 changes: 6 additions & 34 deletions sdk/bpf/rust/build.sh
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; }
29 changes: 29 additions & 0 deletions sdk/bpf/rust/xargo-build.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#!/usr/bin/env bash

bpf_sdk=$(cd "$(dirname "$0")/.." && pwd)
# shellcheck source=sdk/bpf/env.sh
source "$bpf_sdk"/env.sh

set -e
(
while true; do
if [[ -r Xargo.toml ]]; then
break;
fi
if [[ $PWD = / ]]; then
cat <<EOF
Error: Failed to find Xargo.toml
Please create a Xargo.toml file in the same directory as your Cargo.toml with
the following contents:
[target.bpfel-unknown-unknown.dependencies.std]
features = []
EOF
exit 1
fi
cd ..
done
)
exec "$XARGO" build --target "$XARGO_TARGET" --release "$@"
48 changes: 48 additions & 0 deletions sdk/bpf/scripts/dump.sh
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
dump=$2
if [[ -z $so ]] || [[ -z $dump ]]; then
echo "Usage: $0 bpf-program.so dump.txt"
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_mangled=$dump.mangled

(
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 "Wrote $dump"
29 changes: 17 additions & 12 deletions sdk/bpf/scripts/install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -84,19 +84,24 @@ clone() {
}

# Install xargo
(
set -ex
# shellcheck disable=SC2154
if [[ -n $rust_stable ]]; then
cargo +"$rust_stable" install xargo
else
cargo install xargo
version=0.3.22
if [[ ! -e xargo-$version.md ]] || [[ ! -x bin/xargo ]]; then
(
args=()
# shellcheck disable=SC2154
if [[ -n $rust_stable ]]; then
args+=(+"$rust_stable")
fi
args+=(install xargo --version "$version" --root .)
set -ex
cargo "${args[@]}"
./bin/xargo --version >xargo-$version.md 2>&1
)
exitcode=$?
if [[ $exitcode -ne 0 ]]; then
rm -rf xargo-$version.md
exit 1
fi
xargo --version >xargo.md 2>&1
)
# shellcheck disable=SC2181
if [[ $? -ne 0 ]]; then
exit 1
fi

# Install Criterion
Expand Down
6 changes: 6 additions & 0 deletions sdk/bpf/scripts/objcopy.sh
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 "$@"
17 changes: 17 additions & 0 deletions sdk/bpf/scripts/strip.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#!/usr/bin/env bash

so=$1
if [[ ! -r $so ]]; then
echo "Error: file not found: $so"
exit 1
fi
so_stripped=$2
if [[ -z $so_stripped ]]; then
echo "Usage: $0 unstripped.so stripped.so"
exit 1
fi

bpf_sdk=$(cd "$(dirname "$0")/.." && pwd)
# shellcheck source=sdk/bpf/env.sh
source "$bpf_sdk"/env.sh
"$bpf_sdk"/dependencies/llvm-native/bin/llvm-objcopy --strip-all "$so" "$so_stripped"
20 changes: 20 additions & 0 deletions sdk/cargo-build-bpf/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
[package]
name = "solana-cargo-build-bpf"
version = "1.5.0"
description = "Compile a local package and all of its dependencies using the Solana BPF SDK"
authors = ["Solana Maintainers <[email protected]>"]
repository = "https://github.com/solana-labs/solana"
homepage = "https://solana.com/"
license = "Apache-2.0"
edition = "2018"

[dependencies]
clap = "2.33.3"
cargo_metadata = "0.12.0"

[features]
program = []

[[bin]]
name = "cargo-build-bpf"
path = "src/main.rs"
Loading

0 comments on commit c648ddc

Please sign in to comment.