-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdo.sh
executable file
·212 lines (197 loc) · 5.19 KB
/
do.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
#!/usr/bin/env bash
CALLER_PWD=$PWD
cd "$(dirname "$0")"
usage() {
cat <<EOF
Usage: do.sh <action> <project> <action specific arguments>
Supported actions:
build
build-lib
clean
clippy
doc
dump
fmt
test
update
Supported projects:
all
any directory containing a Cargo.toml file
EOF
}
sdkDir=bin/bpf-sdk
profile=bpfel-unknown-unknown/release
readCargoVariable() {
declare variable="$1"
declare Cargo_toml="$2"
while read -r name equals value _; do
if [[ $name = "$variable" && $equals = = ]]; then
echo "${value//\"/}"
return
fi
done < <(cat "$Cargo_toml")
echo "Unable to locate $variable in $Cargo_toml" 1>&2
}
perform_action() {
set -e
# Use relative path if arg starts with "."
if [[ $2 == .* ]]; then
projectDir="$CALLER_PWD"/$2
else
projectDir="$PWD"/$2
fi
targetDir="$PWD"/target
features=
crateName="$(readCargoVariable name "$projectDir/Cargo.toml")"
so_path="$targetDir/$profile"
so_name="${crateName//\-/_}"
so_name_unstripped="${so_name}_unstripped"
if [[ -f "$projectDir"/Xargo.toml ]]; then
features="--features=program"
fi
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
;;
build-lib)
(
cd "$projectDir"
echo "build-lib $crateName ($projectDir)"
export RUSTFLAGS="${@:3}"
cargo build
)
;;
clean)
"$sdkDir"/rust/clean.sh "$projectDir"
;;
clippy)
(
cd "$projectDir"
echo "clippy $crateName ($projectDir)"
cargo +nightly clippy $features ${@:3}
)
;;
doc)
(
cd "$projectDir"
echo "generating docs $crateName ($projectDir)"
cargo doc ${@:3}
)
;;
dump)
# Dump depends on tools that are not installed by default and must be installed manually
# - readelf
# - rustfilt
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"
)
echo "dump $crateName ($projectDir)"
so="$so_path/${so_name}.so"
if [[ ! -r "$so" ]]; then
echo "Error: No dump created, cannot read $so"
exit 1
fi
dump="$so_path/${so_name}_dump"
(
set -x
ls -la "$so" > "${dump}_mangled.txt"
readelf -aW "$so" >>"${dump}_mangled.txt"
"$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"
else
echo "Error: No dump created"
exit 1
fi
else
echo "$projectDir does not contain a program, skipping"
fi
;;
fmt)
(
cd "$projectDir"
echo "formatting $projectDir"
cargo fmt ${@:3}
)
;;
help)
usage
exit
;;
test)
(
cd "$projectDir"
echo "test $projectDir"
cargo test $features ${@:3}
)
;;
update)
./bpf-sdk-install.sh
./do.sh clean all
;;
*)
echo "Error: Unknown command"
usage
exit
;;
esac
}
set -e
if [[ $1 == "update" ]]; then
perform_action "$1"
exit
else
if [[ "$#" -lt 2 ]]; then
usage
exit
fi
if [[ ! -d "$sdkDir" ]]; then
./do.sh update
fi
fi
if [[ $2 == "all" ]]; then
# Perform operation on all projects
for project in */program*; do
if [[ -f "$project"/Cargo.toml ]]; then
perform_action "$1" "${project%/}" ${@:3}
else
continue
fi
done
else
# Perform operation on requested project
if [[ -d $2/program ]]; then
perform_action "$1" "$2/program" "${@:3}"
else
perform_action "$1" "$2" "${@:3}"
fi
fi
exit 0