-
Notifications
You must be signed in to change notification settings - Fork 28
/
cook.sh
executable file
·372 lines (341 loc) · 11.1 KB
/
cook.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
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
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
#!/usr/bin/env bash
set -e
shopt -s nullglob
source config.sh
# Variables to be overriden by recipes
export BINDIR=bin
export CARGO=(env RUSTFLAGS="$PREFIX_RUSTFLAGS -C link-arg=-zmuldefs" cargo)
export CARGOBUILD=rustc
export CARGOFLAGS=
export DEBUG=
export EXAMPLES=
export PREPARE_COPY=1
if hash sha256sum 2>/dev/null
then
SHASUM="sha256sum"
else
SHASUM="shasum -a 256"
fi
function usage {
echo "cook.sh $1 <op>" >&2
echo " dist" >&2
echo " distclean" >&2
echo " build" >&2
echo " clean" >&2
echo " fetch" >&2
echo " unfetch" >&2
echo " pkg" >&2
echo " unpkg" >&2
echo " prepare" >&2
echo " unprepare" >&2
echo " stage" >&2
echo " unstage" >&2
echo " version" >&2
}
function op {
if [ ! "$COOK_QUIET" = "1" ]
then
echo -e "\033[01;38;5;215mcook - $1 $2\033[0m" >&2
fi
case "$2" in
dist)
op $1 prepare
op $1 build
op $1 stage
op $1 pkg
;;
distclean)
op $1 unpkg
op $1 unstage
op $1 unprepare
;;
fetch)
skip=0
if [ "$(type -t recipe_fetch)" = "function" ]
then
recipe_fetch
fi
if [ "$skip" -eq "0" ]
then
if [ -n "$TAR" ]
then
if [ ! -f source.tar ]
then
wget "$TAR" --continue -O source.tar.tmp
mv source.tar.tmp source.tar
fi
if [ -n "$TAR_SHA256" ]
then
$SHASUM -c <<< "${TAR_SHA256} source.tar"
fi
if [ ! -d source ]
then
mkdir source
tar xvf source.tar -C source --strip-components 1
fi
elif [ -n "$GIT" ]
then
if [ ! -d source ]
then
if [ -n "$BRANCH" ]
then
git clone --recursive "$GIT" -b "$BRANCH" source
else
git clone --recursive "$GIT" source
fi
fi
pushd source > /dev/null
git remote set-url origin "$GIT"
git fetch origin
if [ -n "$GIT_UPSTREAM" ]
then
git remote set-url upstream "$GIT_UPSTREAM" &> /dev/null ||
git remote add upstream "$GIT_UPSTREAM"
git fetch upstream
fi
ORIGIN_BRANCH="$(git branch --remotes | grep '^ origin/HEAD -> ' | cut -d ' ' -f 5-)"
if [ -n "$BRANCH" ]
then
ORIGIN_BRANCH="origin/$BRANCH"
fi
if [ "$(git rev-parse HEAD)" != "$(git rev-parse $ORIGIN_BRANCH)" ]
then
git checkout -B "$(echo "$ORIGIN_BRANCH" | cut -d / -f 2-)" "$ORIGIN_BRANCH"
fi
git submodule sync --recursive
git submodule update --init --recursive
popd > /dev/null
fi
fi
;;
unfetch)
rm -rfv source source.tar
;;
prepare)
skip=0
if [ "$(type -t recipe_prepare)" = "function" ]
then
recipe_prepare
fi
if [ "$skip" -eq "0" ]
then
rm -rf "${COOKBOOK_SYSROOT}"
mkdir "${COOKBOOK_SYSROOT}"
if [ ${#BUILD_DEPENDS} -gt 0 ]
then
pushd $ROOT
./repo.sh "${BUILD_DEPENDS[@]}"
popd
for i in "${BUILD_DEPENDS[@]}"
do
pkgar \
extract \
"${COOKBOOK_SYSROOT}" \
--archive "$REPO/$i.pkgar" \
--pkey "${ROOT}/build/id_ed25519.pub.toml"
done
fi
rm -rf "${COOKBOOK_BUILD}"
if [ "$PREPARE_COPY" -eq "0" ]
then
mkdir "${COOKBOOK_BUILD}"
else
cp -Rp source "${COOKBOOK_BUILD}"
fi
for patch in *.patch
do
patch -p1 -d "${COOKBOOK_BUILD}" < "$patch"
done
fi
;;
unprepare)
rm -rf "${COOKBOOK_BUILD}"
rm -rf "${COOKBOOK_SYSROOT}"
;;
version)
pushd "${COOKBOOK_BUILD}" > /dev/null
skip=0
if [ "$(type -t recipe_version)" = "function" ]
then
recipe_version
fi
if [ "$skip" -eq "0" ]
then
# there's an unstable built-in cargo config command, so hack around it
cargo-config config package.version | tr -d '"'
fi
popd > /dev/null
;;
gitversion)
if [ -d "${COOKBOOK_BUILD}"/.git ]
then
echo "$(op $1 version)-$(git -C "${COOKBOOK_BUILD}" rev-parse --short HEAD)"
else
op $1 version
fi
;;
build)
pushd "${COOKBOOK_BUILD}" > /dev/null
skip=0
if [ "$(type -t recipe_build)" = "function" ]
then
recipe_build
fi
release_flag="--release"
if [ "$DEBUG" == 1 ]
then
release_flag=
fi
if [ -n "$CARGO_PACKAGE" ]; then
package_flag="--package=$CARGO_PACKAGE"
else
package_flag=
fi
if [ "$skip" -eq "0" ]
then
"${CARGO[@]}" "$CARGOBUILD" --target "$TARGET" $release_flag $package_flag $CARGOFLAGS
fi
popd > /dev/null
;;
clean)
pushd "${COOKBOOK_BUILD}" > /dev/null
skip=0
if [ "$(type -t recipe_clean)" = "function" ]
then
recipe_clean
fi
if [ "$skip" -eq "0" ]
then
"${CARGO[@]}" clean
fi
popd > /dev/null
;;
stage)
op $1 unstage
mkdir -p "${COOKBOOK_STAGE}"
stage="$(realpath "${COOKBOOK_STAGE}")"
source="$(realpath source)"
pushd "${COOKBOOK_BUILD}" > /dev/null
skip=0
if [ "$(type -t recipe_stage)" = "function" ]
then
recipe_stage "$stage"
fi
if [ "$skip" -eq "0" ]
then
#TODO "${CARGO[@]}" install --root "$stage" $CARGOFLAGS
if [ "$DEBUG" == 1 ]
then
build=debug
else
build=release
fi
bins="$(find target/$TARGET/$build/ -maxdepth 1 -type f ! -name '*.*')"
if [ -z "$bins" ] || [ "$EXAMPLES" == 1 ]
then
example=true
bins="$bins $(find target/$TARGET/$build/examples/ -maxdepth 1 -type f ! -name '*.*' ! -name '*-*' \
2> /dev/null || true)"
fi
if [ -n "$bins" ]
then
if [ -n "$example" ] && [ "$EXAMPLES" != 1 ]
then
echo "$(tput bold)Note$(tput sgr0): No binaries detected, using example binaries"
fi
mkdir -p "$stage/$BINDIR"
for bin in $bins
do
if [ "$DEBUG" == 1 ]
then
cp -v "$bin" "$stage/$BINDIR/$(basename $bin)"
else
"${STRIP}" -v "$bin" -o "$stage/$BINDIR/$(basename $bin)"
fi
done
else
echo "$(tput bold)Warning$(tput sgr0): Recipe does not have any binaries" >&2
fi
docgen "$source" "$stage/ref"
fi
popd > /dev/null
;;
unstage)
rm -rfv "${COOKBOOK_STAGE}"
;;
pkg)
pkgar \
create \
--archive "${COOKBOOK_STAGE}.pkgar" \
--skey "${ROOT}/build/id_ed25519.toml" \
"${COOKBOOK_STAGE}"
# Generate stage.toml
echo "name = \"$1\"" > "${COOKBOOK_STAGE}.toml"
echo "version = \"$(op $1 version)\"" >> "${COOKBOOK_STAGE}.toml"
echo "target = \"$TARGET\"" >> "${COOKBOOK_STAGE}.toml"
# Add runtime dependencies to package if they exist
if [ -n "$DEPENDS" ]
then
# Remove leading and trailing whitespace, replace whitespace between
# package names with commas, and surround package names with quotes
dependencies=$(echo -e "$DEPENDS" | sed -E 's/^[[:space:]]*//;s/[[:space:]]*$//;s/[[:space:]]+/,/g;s/[^, ][^, ]*/"&"/g')
echo "depends = [$dependencies]" >> "${COOKBOOK_STAGE}.toml"
else
echo "depends = []" >> "${COOKBOOK_STAGE}.toml"
fi
;;
unpkg)
rm -fv "${COOKBOOK_STAGE}.pkgar" "${COOKBOOK_STAGE}.toml"
;;
*)
usage $1
;;
esac
}
if [ -n "$1" ]
then
if (echo "$1" | grep '.*/.*' >/dev/null); then
recipe_name=$(basename "$1")
recipe_path="recipes/$1"
else
recipe_name="$1"
recipe_path=`target/release/find_recipe $recipe_name`
fi
if [ -d "$ROOT/$recipe_path" ]
then
export COOKBOOK_RECIPE="${ROOT}/$recipe_path"
TARGET_DIR="${COOKBOOK_RECIPE}/target/${TARGET}"
mkdir -p "${TARGET_DIR}"
export COOKBOOK_BUILD="${TARGET_DIR}/build"
export COOKBOOK_STAGE="${TARGET_DIR}/stage"
export COOKBOOK_SOURCE="${COOKBOOK_RECIPE}/source"
export COOKBOOK_SYSROOT="${TARGET_DIR}/sysroot"
export PKG_CONFIG_ALLOW_CROSS=1
export PKG_CONFIG_PATH=
export PKG_CONFIG_LIBDIR="${COOKBOOK_SYSROOT}/lib/pkgconfig"
export PKG_CONFIG_SYSROOT_DIR="${COOKBOOK_SYSROOT}"
cd "${COOKBOOK_RECIPE}"
if [ -e recipe.sh ]; then
source recipe.sh
fi
ops=()
for arg in "${@:2}"
do
if [ "$arg" == "--debug" ]
then
DEBUG=1
else
ops[${#ops[@]}]="$arg"
fi
done
for i in "${ops[@]}"
do
op "$recipe_name" "$i"
done
else
echo "cook.sh: recipe '$recipe_name' at not found" >&2
exit 1
fi
else
usage "{package}"
fi