solana-program-library/do.sh

213 lines
5.2 KiB
Bash
Raw Normal View History

2020-05-29 15:00:47 -07:00
#!/usr/bin/env bash
2020-08-20 11:36:49 -07:00
CALLER_PWD=$PWD
2020-05-29 15:00:47 -07:00
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
}
2020-08-07 11:03:55 -07:00
sdkDir=bin/bpf-sdk
2020-05-29 15:00:47 -07:00
profile=bpfel-unknown-unknown/release
2020-08-08 00:07:43 -07:00
readCargoVariable() {
declare variable="$1"
declare Cargo_toml="$2"
while read -r name equals value _; do
if [[ $name = "$variable" && $equals = = ]]; then
echo "${value//\"/}"
return
2020-08-08 00:07:43 -07:00
fi
done < <(cat "$Cargo_toml")
echo "Unable to locate $variable in $Cargo_toml" 1>&2
}
2020-05-29 15:00:47 -07:00
perform_action() {
set -e
# Use relative path if arg starts with "."
if [[ $2 == .* ]]; then
projectDir="$CALLER_PWD"/$2
else
projectDir="$PWD"/$2
fi
2020-08-07 10:09:06 -07:00
targetDir="$PWD"/target
features=
2020-08-08 00:07:43 -07:00
crateName="$(readCargoVariable name "$projectDir/Cargo.toml")"
so_path="$targetDir/$profile"
so_name="${crateName//\-/_}"
so_name_unstripped="${so_name}_unstripped"
2020-08-08 00:07:43 -07:00
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
echo "build $crateName ($projectDir)"
"$sdkDir"/rust/build.sh "$projectDir"
cp "$so_path/${so_name}.so" "$so_path/${so_name_unstripped}.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"
2020-08-08 00:07:43 -07:00
echo "build-lib $crateName ($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"
2020-08-08 00:07:43 -07:00
echo "clippy $crateName ($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"
2020-08-08 00:07:43 -07:00
echo "generating docs $crateName ($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
2020-10-13 12:49:46 -07:00
# - readelf
2020-05-29 15:00:47 -07:00
# - rustfilt
2020-10-13 12:49:46 -07:00
if [[ -f "$projectDir"/Xargo.toml ]]; then
if ! which rustfilt > /dev/null; then
echo "Error: rustfilt not found. It can be installed by running: cargo install rustfilt"
exit 1
fi
if ! which 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
(
cd "$CALLER_PWD"
"$0" build "$2"
)
2020-10-13 12:49:46 -07:00
echo "dump $crateName ($projectDir)"
so="$so_path/${so_name}.so"
2020-05-29 15:00:47 -07:00
if [[ ! -r "$so" ]]; then
echo "Error: No dump created, cannot read $so"
exit 1
fi
2020-07-23 12:00:51 -07:00
dump="$so_path/${so_name}_dump"
(
set -x
ls -la "$so" > "${dump}_mangled.txt"
readelf -aW "$so" >>"${dump}_mangled.txt"
2020-05-29 15:00:47 -07:00
"$sdkDir/dependencies/llvm-native/bin/llvm-objdump" \
-print-imm-hex \
--source \
--disassemble \
"$so" \
>> "${dump}_mangled.txt"
sed s/://g <"${dump}_mangled.txt" | rustfilt >"${dump}.txt"
)
if [[ -f "$dump.txt" ]]; then
echo "Created $dump.txt"
2020-05-29 15:00:47 -07:00
else
echo "Error: No dump created"
2020-10-13 12:49:46 -07:00
exit 1
2020-05-29 15:00:47 -07:00
fi
2020-10-13 12:49:46 -07:00
else
echo "$projectDir does not contain a program, skipping"
fi
2020-05-29 15:00:47 -07:00
;;
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)
2020-08-07 11:03:55 -07:00
./bpf-sdk-install.sh
2020-07-28 16:33:34 -07:00
./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-08-08 00:07:43 -07:00
for project in */program*; 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
2020-08-08 00:07:43 -07:00
if [[ -d $2/program ]]; then
perform_action "$1" "$2/program" "${@:3}"
else
perform_action "$1" "$2" "${@:3}"
fi
fi
2020-10-13 12:49:46 -07:00
exit 0