-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcargo
executable file
·319 lines (296 loc) · 8.86 KB
/
cargo
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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
#!/bin/sh
#/ Usage: cargo <verb> [args...]
#/ config List, get, or set a config option.
#/ [key] Option to get/set
#/ [val] Value to set to
#/ install Install a package.
#/ [-s userid] Require signature by userid.
#/ <pkgref>
#/ make-manifest Generate a manifest.
#/ [-s userid] Sign as userid.
#/ <crate-file>
#/ <manifest-file>
#/ [files...] Additional files to include.
#/ uninstall Uninstall a package
#/ <pkgname>
#/
#/ A 'pkgref' is any of:
#/ github:<username>/<reponame>[@<commit-id>] Fetch source from github.
#/ file:<filepath>[@<commit-id>] Fetch source from local fs.
#/ <url> Fetch source from tarball at url.
#/ <uuid>[@<commit-id>] Lookup uuid from global registry.
#/
#/ Examples:
#/ cargo config rustc /path/to/rustc
#/ cargo install github:elly/rust-hello
#/ cargo install file:$HOME/tmp/package.tar
#/ cargo install uuid:83941200-18b3-41b1-8316-b98a2950b33a
#/ cargo install http://www.leptoquark.net/~elly/rust-hello.tar
#/ cargo install -s [email protected] http://www.leptoquark.net/~elly/rust-hello.tar
#/ cargo make-manifest hello.rc manifest
#/
#/ Config keys:
#/ rustc = /path/to/rustc
#/ makeopts = options-to-pass-to-make
die() {
echo "$*"
exit 1
}
make_workdir() {
mkdir -p "$HOME/.cargo/work"
mktemp -d "$HOME/.cargo/work/$1-XXXXXXXX"
}
make_fixdir() {
local d="$HOME/.cargo/pkg/$1-$2"
rm -rf "$d"
mkdir -p "$d"
echo "$d"
}
# Check for rustc in the config file, or check for it in the path and store its
# location in the config file. If we can't find it, die.
need_rustc() {
if [ -n "$(get_config rustc)" ]; then return 0; fi
rustc=$(which rustc)
if [ -n "$rustc" ]; then
echo "Found rustc at $rustc"
set_config rustc "$rustc"
return 0
fi
die "Need rustc. Try '$0 config rustc <path>'."
}
# Write value $2 for config key $1.
set_config() {
mkdir -p "$HOME/.cargo"
touch "$HOME/.cargo/config"
sed -i -e "s/^$1: //;" "$HOME/.cargo/config"
echo "$1: $2" >> "$HOME/.cargo/config"
}
# Read the value of config key $1.
get_config() {
mkdir -p "$HOME/.cargo"
touch "$HOME/.cargo/config"
sed -n "/^$1: /{s/^$1: //;p}" "$HOME/.cargo/config"
}
# Dump the config file.
list_config() {
cat "$HOME/.cargo/config"
}
# Read the value of a config key in a manifest.
get_manifest() {
sed -n "/^$1: /{s/^$1: //;p}" manifest
}
# Support two kinds of key checks:
# user@host - looked up in existing keyring
# short keyid - existing keyring
# Argument is one of these; we operate on the manifest{,.sig} in the current
# directory.
check_sig_specific() {
r=$(gpg --verify manifest.sig manifest 2>&1)
if ! echo "$r" | grep -q "Good signature from"; then
echo "Bad signature! GPG output:"
echo "$r"
die
fi
if echo "$r" | grep -q "^gpg: Signature made .* key ID $1"; then
echo "Good signature from $1."
return 0
fi
if echo "$r" | grep -q "^gpg: Good signature from .* <$1>"; then
echo "Good signature from $1."
return 0
fi
if echo "$r" | grep -q "^gpg: Good signature from"; then
echo "Signature from wrong key?"
echo "$r" | grep "^gpg: Good signature from"
die
fi
echo "Unparseable gpg output:"
echo "$r"
die
}
# Read hashes out of the manifest (key-val pairs starting with 'hash-') and
# verify them.
check_hashes() {
sed -n '/^hash-/{s/^hash-\([^:]\+\): \(.\+\)/\2 \1/;p}' manifest \
| while read line; do
echo "$line" | sha256sum -c --quiet || die "cksum wrong: $line"
done
echo "Checksums match."
}
# Install a package from source, which is unpacked in the current directory.
# Read the name and version out of the manifest, then build, optionally test,
# and install the package, and symlink any libraries it creates into place.
# The directory structure we expect is:
# /manifest: Key-value file with at least keys 'name' and 'version'.
c_install_source() {
if [ ! -z "$NEEDED_SIGNER" ]; then
check_sig_specific "$NEEDED_SIGNER"
check_hashes
fi
local name=$(get_manifest name)
local vers=$(get_manifest vers)
local bootstrap=$(get_manifest bootstrap)
local build=$(get_manifest build)
local test=$(get_manifest test)
local install=$(get_manifest install)
local makeopts=$(get_config makeopts)
[ -z "$name" ] && die "required property 'name' missing"
[ -z "$vers" ] && die "required property 'vers' missing"
[ -z "$build" ] && build="make $makeopts"
[ -z "$install" ] && install="make $makeopts install"
[ -z "$bootstrap" ] && need_rustc
rustc=$(get_config rustc)
destdir=$(make_fixdir "$name" "$vers")
if [ -x ./configure ]; then
RUSTC="$rustc" ./configure --prefix="$destdir"
fi
RUSTC="$rustc" PREFIX="$destdir" $build || die "build failed: $build"
if [ ! -z "$test" ]; then
RUSTC="$rustc" PREFIX="$destdir" $test || die "test failed: $test"
fi
RUSTC="$rustc" PREFIX="$destdir" $install \
|| die "install failed: $install"
local workdir="$PWD"
cd "$destdir"
for x in *.so; do
if [ -x "$x" ]; then
mkdir -p "$HOME/.cargo/libs"
dname=$(sha1sum "$x" | cut -f1 -d' ')
ln -sf "$destdir/$x" "$HOME/.cargo/libs/$dname-$x.so"
fi
done
rm -rf "$workdir"
}
# Install a package from github. $1 is a string of the form
# github:<username>/<reponame>[@<commit>]. Make a local clone of the source,
# check out the specified commit if needed, and do a source install.
c_install_github() {
local urepo=$(echo "$1" | sed -e 's/^github://')
local repopath=$(echo "$urepo" | cut -f1 -d@)
if echo "$1" | grep -q '@'; then
local commit=$(echo "$urepo" | cut -f2 -d@)
fi
local pkgname=$(echo "$repopath" | cut -f2 -d/)
d=$(make_workdir "$pkgname")
cd "$d"
git clone "git://github.com/$repopath" "$d" || die "git clone failed"
if [ ! -z "$commit" ]; then
git checkout "$commit" || die "git checkout $commit failed"
fi
c_install_source
}
# Install a package from a local file. The tarball must have a directory
# component before the expected source layout (no tarbombs!).
c_install_file() {
local filename=$(echo "$1" | sed -e 's/^file://')
local pkgname=$(basename "$filename" | cut -f1 -d.)
d=$(make_workdir "$pkgname")
cd "$d"
tar --strip-components=1 -xf "$filename" || die "tar xf '$filename' failed"
c_install_source
}
# Install a package by uuid. Look the uuid up in a global database to resolve it
# to one of the other install methods, then run that other method.
# XXX: This should probably be hosted on rust-lang.org or something, or really
# anywhere but elly's machine.
c_install_uuid() {
local uuid=$(echo "$1" | sed -e 's/^uuid://' | cut -f1 -d@)
if echo "$1" | grep -q "@"; then
local commit=$(echo "$1" | cut -f2 -d@)
fi
URL="http://www.leptoquark.net/~elly/cargo-uuids"
local res=$(wget -qO- "$URL" | sed -n "/^$uuid: /{s/^$uuid: //;p}")
echo "Resolved: $uuid -> $res"
[ -n "$commit" ] && res="$res@$commit"
c_install "$res"
}
# Install a package by url of source tarball. Fetch, extract, and do a source
# install.
c_install_url() {
local pkgname=$(basename "$1")
d=$(make_workdir "$pkgname")
cd "$d"
wget "$1"
tar --strip-components=1 -xf * || die "no tar files to extract"
c_install_source
}
# Do an install.
c_install() {
if [ "$1" = "-s" ]; then
[ -z "$2" ] && usage
NEEDED_SIGNER="$2"
shift; shift
fi
if echo "$1" | grep -q '^github:'; then
c_install_github "$@"
elif echo "$1" | grep -q '^file:'; then
c_install_file "$@"
elif echo "$1" | grep -q '^uuid:'; then
c_install_uuid "$@"
elif echo "$1" | grep -q '://'; then
c_install_url "$@"
fi
}
# Generate a manifest file from a .rc file.
# XXX: Don't parse the crate file this way!
read_rc_attr() {
sed -n "/$2 = \"/{s/.*$2 = \"\([^\"]*\)\".*/\1/;p}" "$1"
}
# Generate a manifest file. $1 is the crate file (the .rc file), $2 is the
# manifest file to write. Any additional args are included in the set to be
# hashed as filenames.
c_make_manifest() {
if [ "$1" = "-s" ]; then
[ -z "$2" ] && usage
signas="$2"
shift; shift
fi
name=$(read_rc_attr "$1" name)
vers=$(read_rc_attr "$1" vers)
local manifest="$2"
echo "name: $name" > "$manifest"
echo "vers: $vers" >> "$manifest"
shift; shift
[ -z "$signas" ] && return 0
for x in $*; do
h=$(sha256sum "$x" | cut -f1 -d' ')
echo "hash-$x: $h" >> "$manifest"
done
find . -name '*.rs' -o -name '*.rc' -o -iname '*.mk' \
-o -iname 'makefile' | while read x; do
x=$(echo "$x" | sed -e 's/^\.\///')
h=$(sha256sum "$x" | cut -f1 -d' ')
echo "hash-$x: $h" >> "$manifest"
done
rm -f manifest.sig
gpg -u "$signas" --detach-sign --armor -o manifest.sig manifest
}
c_uninstall() {
echo "Not implemented yet, sorry :(."
}
usage() {
sed -n -e '/^#\//{s/^#\/ \?//;p}' $0
exit 1
}
[ -z "$1" ] && usage
cmd="$1"; shift
case "$cmd" in
config)
[ -z "$1" ] && list_config
[ -z "$2" ] && get_config "$1"
set_config "$1" "$2"
;;
install)
[ -z "$1" ] && usage
c_install "$@"
;;
make-manifest)
c_make_manifest "$@"
;;
uninstall)
c_uninstall "$@"
;;
*)
usage
;;
esac