refactor cross build code
authorLudwig Nussel <ludwig.nussel@suse.de>
Wed, 31 Mar 2010 06:45:44 +0000 (08:45 +0200)
committerLudwig Nussel <ludwig.nussel@suse.de>
Wed, 31 Mar 2010 09:06:35 +0000 (11:06 +0200)
build
common_functions
init_buildsystem

diff --git a/build b/build
index 124adf1..e95a570 100755 (executable)
--- a/build
+++ b/build
@@ -80,6 +80,9 @@ OVERLAY=
 RSYNCSRC=
 RSYNCDEST=
 RSYNCDONE=
+: ${EMULATOR_ARCHS:="armv4l armv5el armv7el sh4 ppc ppc64 mips mipsel"}
+
+export EMULATOR_ARCHS
 
 # This is for insserv
 export YAST_IS_RUNNING=instsys
@@ -1267,6 +1270,12 @@ for SPECFILE in "${SPECFILES[@]}" ; do
            umount $BUILD_ROOT || cleanup_and_exit 1
        fi
 
+       if check_use_emulator; then
+           vm_init_script="/.build/initscript_qemu_vm"
+       else
+           vm_init_script="/.build/build"
+       fi
+
        if [ "$VM_TYPE" = 'xen' ]; then
                XMROOT=file:$VM_IMAGE
                XMROOT=${XMROOT/#file:\/dev/phy:/dev}
@@ -1280,15 +1289,6 @@ for SPECFILE in "${SPECFILES[@]}" ; do
                XENID="${VM_IMAGE%/root}"
                XENID="${XENID%/tmpfs}"
                XENID="${XENID##*/}"
-               CROSS_INIT_SCRIPT="/.build/build"
-               # to run the qemu initialization in the XEN chroot, we need to register it with a statically build shell
-               if [ x"$BUILD_ARCH" == xarmv4l -o x"$BUILD_ARCH" == xarmv5el -o x"$BUILD_ARCH" == xarmv7el -o x"$BUILD_ARCH" == xsh4 -o x"$BUILD_ARCH" == xppc -o x"$BUILD_ARCH" == xppc64 -o x"$BUILD_ARCH" == xmips -o x"$BUILD_ARCH" == xmipsel ]; then
-                   if [  -n "$(uname -m | grep '[x3-6]86')" ]; then
-                       if [ -e /bin/bash-static -a -e /bin/mount-static ]; then
-                           CROSS_INIT_SCRIPT="/.build/initscript_qemu_vm"
-                       fi
-                   fi
-               fi
 
                echo "booting XEN kernel ..."
                if xm list "build:$XENID" >/dev/null 2>&1 ; then
@@ -1296,7 +1296,7 @@ for SPECFILE in "${SPECFILES[@]}" ; do
                   echo "Please report to your server admin, there might be multiple services running for same domain"
                   cleanup_and_exit 3
                fi
-               set -- xm create -c $BUILD_DIR/xen.conf name="build:$XENID" ${MEMSIZE:+memory=$MEMSIZE} $XMROOT $XMSWAP extra="quiet init="$CROSS_INIT_SCRIPT" panic=1 console=ttyS0"
+               set -- xm create -c $BUILD_DIR/xen.conf name="build:$XENID" ${MEMSIZE:+memory=$MEMSIZE} $XMROOT $XMSWAP extra="quiet init="$vm_init_script" panic=1 console=ttyS0"
                if test "$PERSONALITY" != 0 ; then
                    # have to switch back to PER_LINUX to make xm work
                    set -- linux64 "$@"
@@ -1305,7 +1305,7 @@ for SPECFILE in "${SPECFILES[@]}" ; do
                "$@" || cleanup_and_exit 3
        elif [ "$VM_TYPE" = 'uml' ]; then
                echo "booting UML kernel ..."
-               set -- $uml_kernel initrd=$uml_initrd root=/ubda init=/.build/build panic=1 quiet ubd0=$VM_IMAGE ${MEMSIZE:+mem=$MEMSIZE}
+               set -- $uml_kernel initrd=$uml_initrd root=/ubda init="$vm_init_script" panic=1 quiet ubd0=$VM_IMAGE ${MEMSIZE:+mem=$MEMSIZE}
                echo "$@"
                "$@"
        elif [ "$VM_TYPE" = 'qemu' -o "$VM_TYPE" = 'kvm' ]; then
@@ -1323,22 +1323,10 @@ for SPECFILE in "${SPECFILES[@]}" ; do
                        qemu_disks[${#qemu_disks[@]}]="$VM_SWAP"
                    fi
                fi
-                CROSS_INIT_SCRIPT="/.build/build"
-                # to run the qemu initialization in the XEN chroot, we need to register it with a statically build shell
-                if [ x"$BUILD_ARCH" == xarmv4l -o x"$BUILD_ARCH" == xarmv5el -o x"$BUILD_ARCH" == xarmv6el -o x"$BUILD_ARCH" == xarmv7el -o x"$BUILD_ARCH" == xarmv8el -o x"$BUILD_ARCH" == xsh4 -o x"$BUILD_ARCH" == xppc -o x"$BUILD_ARCH" == xppc64 -o x"$BUILD_ARCH" == xmips -o x"$BUILD_ARCH" == xmipsel ]; then
-                    echo "Building for arch: $BUILD_ARCH"
-                    if [  -n "$(uname -m | grep '[x3-6]86')" ]; then
-                        echo "         on arch: $(uname -m | grep '[x3-6]86')"
-                        if [ -e /bin/bash-static -a -e /bin/mount-static ]; then
-                            echo "         using cross-init"
-                            CROSS_INIT_SCRIPT="/.build/initscript_qemu_vm"
-                        fi
-                    fi
-                fi
                set -- $qemu_bin -no-reboot -nographic -net none -serial stdio \
                    -kernel $qemu_kernel \
                    -initrd $qemu_initrd \
-                   -append "root=$qemu_rootdev panic=1 quiet noapic rw console=ttyS0 init=$CROSS_INIT_SCRIPT" \
+                   -append "root=$qemu_rootdev panic=1 quiet noapic rw console=ttyS0 init=$vm_init_script" \
                    ${MEMSIZE:+-m $MEMSIZE} \
                    "${qemu_disks[@]}"
 
@@ -1361,16 +1349,7 @@ for SPECFILE in "${SPECFILES[@]}" ; do
                LXCID=${BUILD_ROOT##*/}
                lxc-destroy -n "$LXCID" >/dev/null 2>&1 || true
                lxc-create -n "$LXCID" -f "$LXCCONF" || cleanup_and_exit 1
-                CROSS_INIT_SCRIPT="/.build/build"
-                # to run the qemu initialization in the XEN chroot, we need to register it with a statically build shell
-                if [ x"$BUILD_ARCH" == xarmv4l -o x"$BUILD_ARCH" == xarmv5el -o x"$BUILD_ARCH" == xarmv7el -o x"$BUILD_ARCH" == xsh4 -o x"$BUILD_ARCH" == xppc -o x"$BUILD_ARCH" == xppc64 -o x"$BUILD_ARCH" == xmips -o x"$BUILD_ARCH" == xmipsel ]; then
-                    if [  -n "$(uname -m | grep '[x3-6]86')" ]; then
-                        if [ -e /bin/bash-static -a -e /bin/mount-static ]; then
-                            CROSS_INIT_SCRIPT="/.build/initscript_qemu_vm"
-                        fi
-                    fi
-                fi
-               lxc-start -n "$LXCID" "$CROSS_INIT_SCRIPT"
+               lxc-start -n "$LXCID" "$vm_init_script"
                BUILDSTATUS="$?"
                test "$BUILDSTATUS" != 255 || BUILDSTATUS=3
                cleanup_and_exit "$BUILDSTATUS"
index 7353db1..df6a0c8 100755 (executable)
@@ -39,3 +39,34 @@ check_exit()
        cleanup_and_exit 1
     fi
 }
+
+is_emulator_arch()
+{
+    local arch
+    for arch in $EMULATOR_ARCHS; do
+       if test "$BUILD_ARCH" = "$arch"; then
+           return 0
+       fi
+    done
+    return 1
+}
+
+check_use_emulator()
+{
+    is_emulator_arch || return
+
+    # to run the qemu initialization in the XEN chroot, we need to register it with a statically build shell
+    case "$BUILD_HOST_ARCH" in
+       i?86|x86_64)
+           if test -e /bin/bash-static \
+               -a -e /bin/mount-static \
+               -a -e /usr/sbin/qemu-binfmt-conf.sh; then
+               return 0
+           else
+               # XXX: error?
+               echo "Warning: cross compile not possible due to missing static binaries"
+           fi
+       ;;
+    esac
+    return 1
+}
index ff1030f..3d20fba 100755 (executable)
@@ -304,30 +304,40 @@ function validate_cache_file {
     fi
 }
 
-function register_qemu_user {
 #
 # Detect cross-build (fixed code) and copy qemus for cross-build emulation mode
 # And copy also a static host binary shell to run the init scripts in the target chroot
 #
-if [ x"$BUILD_ARCH" == xarmv4l -o x"$BUILD_ARCH" == xarmv5el -o x"$BUILD_ARCH" == xarmv7el -o x"$BUILD_ARCH" == xsh4 -o x"$BUILD_ARCH" == xppc -o x"$BUILD_ARCH" == xppc64 -o x"$BUILD_ARCH" == xmips -o x"$BUILD_ARCH" == xmipsel ]; then
-    if [  -n "$(uname -m | grep '[x3-6]86')" ]; then
-       if [ -e /usr/sbin/qemu-binfmt-conf.sh ]; then
-           (mkdir -p  $BUILD_ROOT/usr/bin $BUILD_ROOT/usr/sbin && set -x && cp /usr/bin/qemu-* $BUILD_ROOT/usr/bin && cp /usr/sbin/qemu-binfmt-conf.sh $BUILD_ROOT/usr/sbin)
-       fi
-       # To initially run the init script, there needs to be also a static bash to register qemu in this chroot and though to start up /.build/build
-       if [ -e /bin/bash-static -a -e /bin/mount-static ]; then
-           (mkdir -p  $BUILD_ROOT/bin && set -x && cp /bin/bash-static $BUILD_ROOT/bin && cp /bin/mount-static $BUILD_ROOT/bin && chmod a+x $BUILD_ROOT/bin/*-static )
-       fi
-       if [ -e /proc/sys/fs/binfmt_misc/arm -o -e /proc/sys/fs/binfmt_misc/armeb -o -e /proc/sys/fs/binfmt_misc/sh4 -o -e /proc/sys/fs/binfmt_misc/mips -o -e /proc/sys/fs/binfmt_misc/ppc -o -e /proc/sys/fs/binfmt_misc/ppc64 ]; then
-           echo "qemu-* already registered"
-       else
-           if [ -e /usr/sbin/qemu-binfmt-conf.sh ]; then
-               echo "qemu-* will be registered"
-               (set -x && /usr/sbin/qemu-binfmt-conf.sh)
-           fi
-       fi
+copy_qemu()
+{
+    check_use_emulator || return
+
+    echo "copying qemu"
+    mkdir -p  $BUILD_ROOT/usr/bin $BUILD_ROOT/usr/sbin
+    install -m755 /usr/bin/qemu-* $BUILD_ROOT/usr/bin
+    install -m755 /usr/sbin/qemu-binfmt-conf.sh $BUILD_ROOT/usr/sbin
+    # To initially run the init script, there needs to be also a
+    # static bash to register qemu in this chroot and to start up
+    # /.build/build
+    mkdir -p  $BUILD_ROOT/bin
+    install -m755 /bin/bash-static $BUILD_ROOT/bin
+    install -m755 /bin/mount-static $BUILD_ROOT/bin
+    # XXX: not needed if vm is used
+    if ! check_binfmt_registered; then
+       echo "registering binfmt handlers"
+       /usr/sbin/qemu-binfmt-conf.sh
     fi
-fi
+}
+
+check_binfmt_registered()
+{
+    local arch
+    for arch in arm $EMULATOR_ARCHS; do
+       if test -e /proc/sys/fs/binfmt_misc/$arch; then
+           return 0
+       fi
+    done
+    return 1
 }
 
 fail_exit()
@@ -403,7 +413,7 @@ fi
 # register the QEMU emulator
 # reset mmap_min_addr for QEMU
 #
-register_qemu_user
+copy_qemu
 
 #
 # store that we start to build system