update denpendencies version
[tools/build.git] / common_functions
1 #!/bin/bash
2
3 ################################################################
4 #
5 # Copyright (c) 1995-2014 SUSE Linux Products GmbH
6 #
7 # This program is free software; you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License version 2 or 3 as
9 # published by the Free Software Foundation.
10 #
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 # GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License
17 # along with this program (see the file COPYING); if not, write to the
18 # Free Software Foundation, Inc.,
19 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
20 #
21 ################################################################
22
23 build_host_arch() {
24     : ${BUILD_HOST_ARCH:=`uname -m`}
25     # the linux kernel only knows armv7l, armv7hl is a userland definition
26     test armv7l == "$BUILD_HOST_ARCH" && BUILD_HOST_ARCH=armv7hl
27
28     BUILD_INITVM_ARCH="$BUILD_HOST_ARCH"
29     # avoid multiple initvm.* helpers for i586 and i686
30     test i686 != "$BUILD_INITVM_ARCH" || BUILD_INITVM_ARCH=i586
31 }
32
33 extend_build_arch() {
34     case $BUILD_ARCH in
35       aarch64) BUILD_ARCH="aarch64:aarch64_ilp32:armv8l" ;;
36       aarch64_ilp32) BUILD_ARCH="aarch64_ilp32:aarch64:armv8l" ;;
37       armv8l) BUILD_ARCH="armv8l" ;; # armv8l is aarch64 in 32bit mode. not a superset of armv7
38       armv7hl) BUILD_ARCH="armv7hl:armv7l:armv6hl:armv6l:armv5tel" ;;
39       armv7l) BUILD_ARCH="armv7l:armv6l:armv5tel" ;;
40       armv6hl) BUILD_ARCH="armv6hl:armv6l:armv5tel" ;;
41       armv6l) BUILD_ARCH="armv6l:armv5tel" ;;
42       armv5tel) BUILD_ARCH="armv5tel" ;;
43       m68k) BUILD_ARCH="m68k" ;;
44       mips64) BUILD_ARCH="mips64:mips" ;;
45       mips) BUILD_ARCH="mips" ;;
46       i686) BUILD_ARCH="i686:i586:i486:i386" ;;
47       i586) BUILD_ARCH="i586:i486:i386" ;;
48       i486) BUILD_ARCH="i486:i386" ;;
49       i386) BUILD_ARCH="i386" ;;
50       ia64) BUILD_ARCH="ia64" ;;
51       parisc64) BUILD_ARCH="hppa64:hppa" ;;
52       parisc) BUILD_ARCH="hppa" ;;
53       ppc) BUILD_ARCH="ppc" ;;
54       ppc64) BUILD_ARCH="ppc64:ppc" ;;
55       ppc64le) BUILD_ARCH="ppc64le" ;;
56       s390x) BUILD_ARCH="s390x:s390" ;;
57       s390) BUILD_ARCH="s390" ;;
58       sparc64v) BUILD_ARCH="sparc64v:sparc64:sparcv9v:sparcv9:sparcv8:sparc" ;;
59       sparc64) BUILD_ARCH="sparc64:sparcv9:sparcv8:sparc" ;;
60       sparcv9v) BUILD_ARCH="sparcv9v:sparcv9:sparcv8:sparc" ;;
61       sparcv9) BUILD_ARCH="sparcv9:sparcv8:sparc" ;;
62       sparcv8) BUILD_ARCH="sparcv8:sparc" ;;
63       sparc) BUILD_ARCH="sparc" ;;
64       x86_64) BUILD_ARCH="x86_64:i686:i586:i486:i386" ;;
65     esac
66 }
67
68 set_build_arch() {
69     build_host_arch
70     if test -z "$BUILD_ARCH" ; then
71         BUILD_ARCH="$BUILD_HOST_ARCH"
72     fi
73     extend_build_arch
74     if test "$BUILD_ARCH" != "${BUILD_ARCH#i686}" ; then
75         cpuflags=`grep ^flags /proc/cpuinfo`
76         cpuflags="$cpuflags "
77         if test "$cpuflags" = "${cpuflags/ cx8 /}" -o "$cpuflags" = "${cpuflags/ cmov /}"; then
78             echo "Your cpu doesn't support i686 rpms. Exit."
79             cleanup_and_exit 1
80         fi
81     fi
82 }
83
84 check_exit() {
85     if test -e $BUILD_ROOT/exit; then
86         echo "exit ..."
87         cleanup_and_exit 1
88     fi
89 }
90
91 check_use_emulator() {
92     INITVM_NAME=
93     # check if the extended host arch contains the build arch
94     local old_build_arch="$BUILD_ARCH"
95     local arch="${BUILD_ARCH%%:*}"
96     BUILD_ARCH="$BUILD_HOST_ARCH"
97     extend_build_arch
98     BUILD_ARCH=":$BUILD_ARCH:"
99     if test "$BUILD_ARCH" != "${BUILD_ARCH/:$arch:/}" ; then
100         # native supported arch, no emulator
101         BUILD_ARCH="$old_build_arch"
102         return 1
103     fi
104     BUILD_ARCH="$old_build_arch"
105
106     # to run the qemu initialization in the vm, we need to
107     # register it with a static program or shell script
108     INITVM_NAME="initvm.$BUILD_INITVM_ARCH"
109     if test -e "$BUILD_DIR/$INITVM_NAME" -a -e "$BUILD_DIR/qemu-reg" ; then
110         chmod 0755 "$BUILD_DIR/$INITVM_NAME"
111         return 0        # chroot build, we need to run
112     fi
113     # XXX: error?
114     echo "Warning: cross compile not possible due to missing static binaries. please install build-initvm package for that purpose."
115     echo "         check that the right architecture is available for your build host, you need $INITVM_NAME for this one."
116     INITVM_NAME=
117     return 1
118 }
119
120 # usage:
121 # progress_setup LIST
122 # for I in $LIST; do
123 #    progress_step LIST
124 #    action $I 
125 # done
126
127 # $1 name of a textual list
128 progress_setup() {
129     eval "$1__ARRAY__=(\$$1)"
130     eval "$1__INDEX__=1"
131     eval "$1__LENGTH__=\${#$1__ARRAY__[@]}"
132 }
133
134 # $1 name of a textual list
135 # $2 optional, printf format for 2 numeric arguments (current, total)
136 progress_step() {
137     local IDX=$1__INDEX__
138     local LEN=$1__LENGTH__
139     printf "${2-[%d/%d] }" $(($IDX++)) ${!LEN}
140 }