-
Notifications
You must be signed in to change notification settings - Fork 13
/
mkpackage.bash
executable file
·368 lines (300 loc) · 9.54 KB
/
mkpackage.bash
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
#! /bin/bash
###########################################################################
# GLUED: GNU/Linux Uniform Environment Distribution #
# Copyright (C) 2007-2021 Universidade do Porto - Faculdade de Engenharia #
# Laboratório de Sistemas e Tecnologia Subaquática (LSTS) #
###########################################################################
# This program is free software; you can redistribute it and/or modify #
# it under the terms of the GNU General Public License as published by #
# the Free Software Foundation; either version 2 of the License, or (at #
# your option) any later version. #
# #
# This program is distributed in the hope that it will be useful, but #
# WITHOUT ANY WARRANTY; without even the implied warranty of #
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU #
# General Public License for more details. #
# #
# You should have received a copy of the GNU General Public License #
# along with this program; if not, write to the Free Software #
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA #
# 02110-1301 USA. #
###########################################################################
# Author: Ricardo Martins #
###########################################################################
source "functions.bash"
download_tool()
{
wget -c "$1" -O "$2"
if [ $? -eq 0 ]; then
return 0
fi
curl -f -C - "$1" -o "$2"
if [ $? -eq 0 ]; then
return 0
fi
rm -f "$2"
return 1
}
md5sum_tool()
{
sum="$(md5sum < "$cfg_dir_downloads/$file" 2> /dev/null | cut -f1 -d' ')"
if [ -n "$sum" ]; then
echo "$sum"
fi
sum="$(md5 < "$cfg_dir_downloads/$file" 2> /dev/null)"
if [ -n "$sum" ]; then
echo "$sum"
fi
}
download()
{
n=0; while [ -n "${url[$n]}" ]; do
u="${url[$n]}"
s="${md5[$n]}"
file=$(basename "$u")
if [ -f "$cfg_dir_downloads/$file" ]; then
md5=$(md5sum_tool "$cfg_dir_downloads/$file")
if [ "$s" = "$md5" ]; then
let n++
continue
fi
fi
# First try LSTS mirror.
lsts_url="https://www.lsts.pt/glued/validPackages/$(basename $u)"
download_tool "$lsts_url" "$cfg_dir_downloads/$file"
if [ $? -ne 0 ]; then
# Then try OceanScan-MST mirror.
omst_url="http://www.omst.pt/glued/$(basename $u)"
download_tool "$omst_url" "$cfg_dir_downloads/$file"
if [ $? -ne 0 ]; then
# On failure try upstream URL.
download_tool "$u" "$cfg_dir_downloads/$file"
if [ $? -ne 0 ]; then
echo "ERROR: download failed"
exit 1
fi
fi
fi
md5="$(md5sum_tool "$cfg_dir_downloads/$file")"
if [ "$s" != "$md5" ]; then
echo "ERROR: MD5 checksum mismatch: $s vs $md5"
return 1
fi
let n++
done
n=0; while [ -n "${svn[$n]}" ]; do
u="$(echo ${svn[$n]} | cut -f1 -d'#')"
r="$(echo ${svn[$n]} | cut -f2 -d'#')"
dir=$(basename $u)
svn export -r "$r" "$u" "$cfg_dir_downloads/$dir"
let n++
done
n=0; while [ -n "${git[$n]}" ]; do
u="$(echo ${git[$n]} | cut -f1 -d'#')"
b="$(echo ${git[$n]} | cut -f2 -d'#')"
dir=$(basename $u)
des="${cfg_dir_builds}/$pkg/$dir-git"
if [[ "$u" == git* ]]; then
git clone --verbose --branch "$b" "$u" "$des"
else
git clone --verbose "$u" "$des"
fi
let n++
done
return 0
}
post_unpack()
{
echo "using dummy 'post_unpack' hook."
}
unpack()
{
n=0; while [ -n "${url[$n]}" ]; do
u="${url[$n]}"
s="${md5[$n]}"
file=$(basename "$u")
case $file in
*tar*|*tgz|*tbz*)
tar -C "$cfg_dir_builds/$pkg" -x -f "$cfg_dir_downloads/$file" || exit 1
;;
*zip)
unzip "$cfg_dir_downloads/$file" -d "$cfg_dir_builds/$pkg" || exit 1
;;
*)
echo "File $file can't be handled."
;;
esac
let n++
done
dir_gnu_cfg="$cfg_dir_toolchain/share/gnu-config"
if [ "$cfg_dir_cfg/config.sub" ]; then
find "$cfg_dir_builds/$pkg" -name config.sub -exec install -v -m 0755 "$dir_gnu_cfg/config.sub" '{}' \;
fi
if [ "$cfg_dir_cfg/config.guess" ]; then
find "$cfg_dir_builds/$pkg" -name config.guess -exec install -v -m 0755 "$dir_gnu_cfg/config.guess" '{}' \;
fi
return 0
}
refresh()
{
echo "Using dummy 'refresh' rule."
}
configure()
{
echo "Using dummy 'configure' rule."
}
build()
{
echo "Using dummy 'build' rule."
}
host_install()
{
echo "Using dummy 'host_install' rule."
}
target_install()
{
echo "Using dummy 'target_install' rule."
}
postconfigure()
{
echo "Using dummy 'postconfigure' rule."
}
perform_clean()
{
rm -rf "$cfg_dir_builds/$pkg/$pkg_var"
}
perform_all()
{
start="$(date +%s)"
nfo1 "$pkg / $pkg_var"
export pkg_build_dir="$cfg_dir_builds/$pkg/$pkg_var"
for rule in download unpack post_unpack refresh configure build host_install target_install postconfigure; do
case $rule in
download | unpack | post_unpack)
marker="$cfg_dir_builds/$pkg/.$rule"
;;
*)
marker="$cfg_dir_builds/$pkg/$pkg_var/.$rule"
;;
esac
mkdir -p "$cfg_dir_builds/$pkg/$pkg_var" && cd "$cfg_dir_builds/$pkg/$pkg_var"
if [ -z "$build_dir" ]; then
build_dir="$pkg-$version"
fi
if [ -n "$build_dir" ]; then
mkdir -p "$cfg_dir_builds/$pkg/$build_dir" && cd "$cfg_dir_builds/$pkg/$build_dir"
fi
if [ -n "$build_always" ] || [ "$rule" = 'refresh' ]; then
nfo2 "$rule"
$rule > "$cfg_dir_builds/$pkg/$pkg_var/$rule.log" 2>&1
else
if ! [ -f "$marker" ]; then
nfo2 "$rule"
$rule > "$cfg_dir_builds/$pkg/$pkg_var/$rule.log" 2>&1
if [ $? -eq 0 ]; then
touch "$marker"
else
err "failed to execute rule $rule of $pkg / $pkg_var"
tail "$cfg_dir_builds/$pkg/$pkg_var/$rule.log"
exit 1
fi
fi
fi
done
elapsed=$[ $(date +%s)-$start ]
ok "completed in ${elapsed}s"
touch "$cfg_dir_builds/$pkg/$pkg_var/.complete"
}
# Check shell.
if [ -z "$BASH_VERSION" ]; then
echo "ERROR: you must use bash to run this script."
exit 1
fi
# Check command line arguments.
if [ $# -lt 2 ]; then
echo "Usage: $0 <config> <package> [all|clean|download|unpack|configure|build|install]"
exit 1
fi
# Read system configuration file.
if ! [ -f "$1" ]; then
echo "ERROR: invalid configuration file '$1'"
exit 1
fi
# Read system configuration file.
source "$1"
# Unset variables that may be a problem to this script.
unset LD_LIBRARY_PATH
unset CFLAGS
unset LDFLAGS
unset CXXFLAGS
export PKG_CONFIG_PATH="$cfg_dir_toolchain_sysroot/usr/lib/pkgconfig"
export LD_LIBRARY_PATH="$cfg_dir_toolchain/lib"
# Sanitize PATH.
paths="$(echo $PATH | sed 's%/\{1,\}%/%g')"
clean_path=""
while [ -n "$paths" ]; do
path=$(echo "$paths" | cut -f1 -d:)
paths=$(echo "$paths" | cut -f2- -d:)
if [ "$path" = "$paths" ]; then
paths=""
fi
if [ "$path" = "" ] || [ "$path" = "." ] || [ "$path" = "./" ]; then
continue
fi
if [ "$path" = "$cfg_dir_toolchain/$cfg_target_canonical/bin" ]; then
continue
fi
if [ "$path" = "$cfg_sys_family/toolchain/$cfg_target_canonical/bin" ]; then
continue
fi
clean_path="$clean_path:$path"
done
export PATH="$cfg_dir_toolchain/bin$clean_path"
pkg="$(echo $2 | cut -f1 -d'/')"
pkg_var="$(echo $2 | cut -f2 -d'/')"
if [ "$pkg_var" = "$pkg" ]; then
pkg_var='default'
fi
export pkg
export pkg_var
export pkg_common="$cfg_dir_rules/$pkg/common.bash"
if ! [ -d "$cfg_dir_rules/$pkg" ]; then
echo "ERROR: package '$pkg' does not exist."
exit 1
fi
if ! [ -f "$cfg_dir_rules/$pkg/$pkg_var.bash" ]; then
echo "ERROR: variant '$pkg_var' of package '$pkg' does not exist."
exit 1
fi
if [ -z "$3" ]; then
rule="all"
else
rule="$3"
fi
mkdir -p "$cfg_dir_downloads" "$cfg_dir_rootfs" "$cfg_dir_toolchain" "$cfg_dir_builds/$pkg"
export pkg_dir="$cfg_dir_rules/$pkg"
. "$cfg_dir_rules/$pkg/$pkg_var.bash"
# Postconfiguration:
if [ -e "$cfg_dir_postconfiguration/$pkg/$cfg_sys_name.bash" ]; then
# echo "ERROR: postconfiguration exists '$pkg'."
# echo "$cfg_dir_postconfiguration/$pkg/$cfg_sys_name.bash"
# exit 1
. "$cfg_dir_postconfiguration/$pkg/$cfg_sys_name.bash"
fi
# Handle dependencies.
n=0; while [ -n "${requires[$n]}" ]; do
req="${requires[$n]}"
let n++
if [ -z "$req" ]; then
break
fi
if [ -f "$cfg_dir_builds/$req/.complete" ]; then
continue
fi
"$0" "$1" "$req"
if [ $? -ne 0 ]; then
err "failed to build dependency for package $pkg / $pkg_var"
exit 1
fi
done
perform_"$rule"