forked from openSUSE/obs-build
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcommon_functions
executable file
·274 lines (252 loc) · 6.72 KB
/
common_functions
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
#!/bin/bash
: ${CACHE_DIR:=/var/cache/build}
set_build_arch()
{
: ${BUILD_HOST_ARCH:=`uname -m`}
if [ -z "$BUILD_ARCH" ]; then
BUILD_ARCH="$BUILD_HOST_ARCH"
test i686 != "$BUILD_ARCH" || BUILD_ARCH=i586 # XXX: why?
fi
case $BUILD_ARCH in
i686) BUILD_ARCH="i686:i586:i486:i386" ;;
i586) BUILD_ARCH="i586:i486:i386" ;;
i486) BUILD_ARCH="i486:i386" ;;
i386) BUILD_ARCH="i386" ;;
x86_64) BUILD_ARCH="x86_64:i686:i586:i486:i386" ;;
sparc64v) BUILD_ARCH="sparc64v:sparc64:sparcv9v:sparcv9:sparcv8:sparc" ;;
sparc64) BUILD_ARCH="sparc64:sparcv9:sparcv8:sparc" ;;
sparcv9v) BUILD_ARCH="sparcv9v:sparcv9:sparcv8:sparc" ;;
sparcv9) BUILD_ARCH="sparcv9:sparcv8:sparc" ;;
sparcv8) BUILD_ARCH="sparcv8:sparc" ;;
sparc) BUILD_ARCH="sparc" ;;
esac
if test "$BUILD_ARCH" != "${BUILD_ARCH#i686}" ; then
cpuflags=`grep ^flags /proc/cpuinfo`
cpuflags="$cpuflags "
if test "$cpuflags" = "${cpuflags/ cx8 /}" -o "$cpuflags" = "${cpuflags/ cmov /}"; then
echo "Your cpu doesn't support i686 rpms. Exit."
cleanup_and_exit 1
fi
fi
}
check_exit()
{
if test -e $BUILD_ROOT/exit; then
echo "exit ..."
cleanup_and_exit 1
fi
}
is_emulator_arch()
{
local arch
for arch in $EMULATOR_ARCHS; do
if test "$BUILD_ARCH" = "$arch" -a "$BUILD_HOST_ARCH" != "$arch"; then
return 0
fi
done
return 1
}
internal_set_sb2_settings() {
SB2_TOOLCHAIN="/unknown"
SB2_INSTALLMODE="obs-rpm-install"
SB2_DEFAULTMODE="obs-rpm-build"
SB2_SYSROOT="-C --sysroot=/ -L --sysroot=/"
SB2_MAPPING_METHOD="both"
SB2_DEBUG=""
SB2_QEMU="/unknown"
while test -n "$1" ; do
case "$1" in
--toolchain)
shift
SB2_TOOLCHAIN=$1
shift
;;
--installmode)
shift
SB2_INSTALLMODE=$1
shift
;;
--defaultmode)
shift
SB2_DEFAULTMODE=$1
shift
;;
--mappingmethod)
shift
SB2_MAPPING_METHOD=$1
shift
;;
--bootstrap)
shift
SB2_SYSROOT=""
;;
--qemu)
shift
SB2_QEMU=$1
shift
;;
--infologs)
shift
SB2_DEBUG="-q -L info"
;;
--debug)
shift
SB2_DEBUG="-q -d"
;;
*)
break
;;
esac
done
if [ "x$SB2_TOOLCHAIN" = "x/unknown" ]; then
echo "Please provide a proper SB2 toolchain in SB2flags. exiting.."
cleanup_and_exit 1
fi
}
cleanup_sb2() {
if [ ! -z "$BUILD_SB2FLAGS" ]; then
if [ -e /home/abuild/sb2-session-needroot ]; then
su -c "cd $PWD; /usr/bin/sb2 -t oscbuild -D /home/abuild/sb2-session-needroot" - abuild
fi
if [ -e /home/abuild/sb2-session-needuser ]; then
su -c "cd $PWD; /usr/bin/sb2 -t oscbuild -D /home/abuild/sb2-session-needuser" - abuild
fi
fi
}
shellquote()
{
for arg; do
arg=${arg/\\/\\\\}
arg=${arg/\$/\\\$}
arg=${arg/\"/\\\"}
arg=${arg/\`/\\\`}
echo -n " \"$arg\""
done
}
# chroot-like wrapper, enter_target <mode: needroot|user> $TARGET commands
# needroot - operations that need to be done as root, like unpacking into file system, etc
# needuser - operations that will be done as non-root user
enter_target() {
if [ -z "$BUILD_SB2FLAGS" ]; then
if [ x$1 == xneeduser ]; then
shift
PARAMS=`shellquote "$@"`
chroot $BUILD_TARGET su -c "$PARAMS" - $BUILD_USER
elif [ x$1 == xneedscript ]; then
shift
SCRIPT=$1
shift
chroot $BUILD_TARGET su -s $SCRIPT $BUILD_USER -- "$@"
else
shift
chroot $BUILD_TARGET "$@"
fi
else
if [ -z "$VM_TYPE" ]; then
echo "SB2 currently does not work with chroots.."
cleanup_and_exit 1
fi
if [ -z "$SB2_TOOLCHAIN" ]; then
internal_set_sb2_settings $BUILD_SB2FLAGS
fi
if [ ! -e $BUILD_TARGET/.sb2inited ]; then
ORIG=$PWD
cd $BUILD_TARGET
su -c "cd $PWD; /usr/bin/sb2-init $SB2_SYSROOT -c $SB2_QEMU -n -N -m $SB2_DEFAULTMODE -t / oscbuild $SB2_TOOLCHAIN" - abuild
su -c "cd $PWD; /usr/bin/sb2-config -t oscbuild setenv SBOX_MAPPING_METHOD $SB2_MAPPING_METHOD" - abuild
cd $ORIG
touch $BUILD_TARGET/.sb2inited
fi
if [ x$1 = xneedscript ]; then
PARAM=needuser
else
PARAM=$1
fi
case "$PARAM" in
needroot)
shift
ORIG=$PWD
cd /target
if [ ! -e /home/abuild/sb2-session-needroot ]; then
SESSION="-S /home/abuild/sb2-session-needroot -t oscbuild"
else
SESSION="-J /home/abuild/sb2-session-needroot"
fi
su -c "cd $PWD; /usr/bin/sb2-config -t oscbuild setenv HOME /root" - abuild
su -c "cd $PWD; /usr/bin/sb2-config -t oscbuild setenv USER root" - abuild
PARAMS=`shellquote "$@"`
su -c "cd $PWD; /usr/bin/sb2 $SESSION $SB2_DEBUG -m $SB2_INSTALLMODE -R $PARAMS" - abuild
RET=$?
cd $ORIG
return $RET
;;
needuser)
# Debugging
shift
ORIG=$PWD
cd /target
if [ ! -e /home/abuild/sb2-session-needuser ]; then
SESSION="-S /home/abuild/sb2-session-needuser -t oscbuild"
else
SESSION="-J /home/abuild/sb2-session-needuser"
fi
PARAMS=`shellquote "$@"`
su -c "cd $PWD; /usr/bin/sb2-config -t oscbuild setenv HOME /home/abuild" - abuild
su -c "cd $PWD; /usr/bin/sb2-config -t oscbuild setenv USER abuild" - abuild
su -c "cd $PWD; /usr/bin/sb2 $SESSION $SB2_DEBUG -m $SB2_DEFAULTMODE $PARAMS" - abuild
RET=$?
cd $ORIG
return $RET
;;
*)
echo "Unknown parameter $1" >&2
;;
esac
fi
}
check_use_emulator()
{
is_emulator_arch || return
if [ -z "$VM_TYPE" ]; then
return 0
fi
# to run the qemu initialization in the XEN chroot, we need to
# register it with a static program or shell script
case "$BUILD_HOST_ARCH" in
i?86|x86_64)
if test -e /usr/lib/build/initvm && \
test -e /usr/lib/build/qemu-reg -o -e /.build/qemu-reg; then
return 0 # prefer initvm to handle registration
elif test -e /bin/bash-static \
-a -e /bin/mount-static \
-a -e /usr/sbin/qemu-binfmt-conf.sh; then
return 0 # as backup use /usr/sbin/qemu-binfmt.conf.sh
elif [ -n "$BUILD_SB2FLAGS" ]; then
return 0 # Use sb2
else
# XXX: error?
echo "Warning: cross compile not possible due to missing static binaries"
fi
;;
esac
return 1
}
# usage:
# progress_setup LIST
# for I in $LIST; do
# progress_step LIST
# action $I
# done
# $1 name of a textual list
progress_setup() {
eval "$1__ARRAY__=(\$$1)"
eval "$1__INDEX__=1"
eval "$1__LENGTH__=\${#$1__ARRAY__[@]}"
}
# $1 name of a textual list
# $2 optional, printf format for 2 numeric arguments (current, total)
progress_step() {
local IDX=$1__INDEX__
local LEN=$1__LENGTH__
printf "${2-[%d/%d] }" $(($IDX++)) ${!LEN}
}