solana-program-library/do.sh

175 lines
3.9 KiB
Bash
Raw Normal View History

2020-05-29 15:00:47 -07:00
#!/usr/bin/env bash
cd "$(dirname "$0")"
usage() {
cat <<EOF
Usage: do.sh <action> <project> <action specific arguments>
2020-05-29 15:00:47 -07:00
Supported actions:
build
build-lib
2020-05-29 15:00:47 -07:00
clean
clippy
2020-06-17 16:13:48 -07:00
doc
dump
2020-05-29 15:00:47 -07:00
fmt
2020-06-17 16:13:48 -07:00
test
update
Supported projects:
all
any directory containing a Cargo.toml file
2020-05-29 15:00:47 -07:00
EOF
}
sdkParentDir=bin
sdkDir="$sdkParentDir"/bpf-sdk
2020-05-29 15:00:47 -07:00
profile=bpfel-unknown-unknown/release
perform_action() {
set -e
projectDir="$PWD"/$2
targetDir="$projectDir"/target
features=
if [[ -f "$projectDir"/Xargo.toml ]]; then
features="--features=program"
fi
2020-05-29 15:00:47 -07:00
case "$1" in
build)
if [[ -f "$projectDir"/Xargo.toml ]]; then
"$sdkDir"/rust/build.sh "$projectDir"
so_path="$targetDir/$profile"
so_name="spl_${2//\-/_}"
cp "$so_path/${so_name}.so" "$so_path/${so_name}_debug.so"
"$sdkDir"/dependencies/llvm-native/bin/llvm-objcopy --strip-all "$so_path/${so_name}.so" "$so_path/$so_name.so"
else
echo "$projectDir does not contain a program, skipping"
fi
2020-05-29 15:00:47 -07:00
;;
build-lib)
(
cd "$projectDir"
echo "build $projectDir"
export RUSTFLAGS="${@:3}"
cargo build
)
;;
2020-05-29 15:00:47 -07:00
clean)
"$sdkDir"/rust/clean.sh "$projectDir"
2020-05-29 15:00:47 -07:00
;;
clippy)
(
cd "$projectDir"
echo "clippy $projectDir"
cargo +nightly clippy $features ${@:3}
)
2020-05-29 15:00:47 -07:00
;;
2020-06-16 21:29:21 -07:00
doc)
(
cd "$projectDir"
echo "generating docs $projectDir"
cargo doc ${@:3}
2020-06-16 21:29:21 -07:00
)
;;
2020-05-29 15:00:47 -07:00
dump)
# Dump depends on tools that are not installed by default and must be installed manually
# - greadelf
# - rustfilt
(
pwd
"$0" build "$2"
2020-05-29 15:00:47 -07:00
so_path="$targetDir/$profile"
2020-07-23 12:00:51 -07:00
so_name="spl_${2//\-/_}"
2020-05-29 15:00:47 -07:00
so="$so_path/${so_name}_debug.so"
2020-07-23 12:00:51 -07:00
dump="$so_path/${so_name}_dump"
echo $so_path
echo $so_name
echo $so
echo $dump
2020-05-29 15:00:47 -07:00
if [ -f "$so" ]; then
ls \
-la \
"$so" \
2020-07-23 12:00:51 -07:00
>"${dump}_mangled.txt"
2020-05-29 15:00:47 -07:00
greadelf \
-aW \
"$so" \
2020-07-23 12:00:51 -07:00
>>"${dump}_mangled.txt"
2020-05-29 15:00:47 -07:00
"$sdkDir/dependencies/llvm-native/bin/llvm-objdump" \
-print-imm-hex \
--source \
--disassemble \
"$so" \
2020-07-23 12:00:51 -07:00
>>"${dump}_mangled.txt"
2020-05-29 15:00:47 -07:00
sed \
s/://g \
2020-07-23 12:00:51 -07:00
<"${dump}_mangled.txt" |
2020-06-24 15:05:22 -07:00
rustfilt \
2020-06-26 09:05:29 -07:00
>"${dump}.txt"
2020-05-29 15:00:47 -07:00
else
echo "Warning: No dump created, cannot find: $so"
fi
)
;;
2020-07-28 16:33:34 -07:00
fmt)
(
cd "$projectDir"
echo "formatting $projectDir"
cargo fmt ${@:3}
)
;;
2020-05-29 15:00:47 -07:00
help)
usage
exit
;;
2020-07-28 16:33:34 -07:00
test)
(
cd "$projectDir"
echo "test $projectDir"
cargo test $features ${@:3}
2020-07-28 16:33:34 -07:00
)
;;
update)
mkdir -p $sdkParentDir
./bpf-sdk-install.sh $sdkParentDir
./do.sh clean all
;;
2020-05-29 15:00:47 -07:00
*)
echo "Error: Unknown command"
usage
exit
;;
esac
}
set -e
2020-06-26 09:05:29 -07:00
if [[ $1 == "update" ]]; then
perform_action "$1"
exit
2020-06-26 09:05:29 -07:00
else
if [[ "$#" -lt 2 ]]; then
usage
exit
fi
2020-06-24 15:05:22 -07:00
if [[ ! -d "$sdkDir" ]]; then
./do.sh update
fi
fi
if [[ $2 == "all" ]]; then
2020-06-24 15:05:22 -07:00
# Perform operation on all projects
2020-06-26 09:05:29 -07:00
for project in */; do
if [[ -f "$project"Cargo.toml ]]; then
perform_action "$1" "${project%/}" ${@:3}
2020-06-26 09:05:29 -07:00
else
2020-06-24 15:05:22 -07:00
continue
2020-06-26 09:05:29 -07:00
fi
done
else
2020-06-24 15:05:22 -07:00
# Perform operation on requested project
perform_action "$1" "$2" "${@:3}"
fi