File pre_checkin.sh of Package JeOS

#!/bin/bash
set -e

CPP=cpp
if [ -e /usr/bin/cpp-4.6 ]; then
    # SLES11 has cpp4.3 as default which generates spurious blank lines
    CPP=cpp-4.6
fi

headversion=$(date -d "$(head -n 2 JeOS.changes | tail -n 1 | cut -d- -f1 )" -u +%Y.%m.%d)

# Used to check which board are Tumbleweed only (mainly due to arm-trusted-firmware being in TW only)
is_tumbleweed=1

## armv6 images
armv6_gfx_images=""
armv6_jeos_images="rootfs raspberrypi1"

## armv7 GFX images
armv7_gfx_images="rootfs"
# Allwinner
armv7_gfx_images="$armv7_gfx_images sinovoipbpim2plus"
armv7_gfx_images="$armv7_gfx_images sinovoipbpim2zero"
# Broadcom
armv7_gfx_images="$armv7_gfx_images raspberrypi2"
# Nvidia
armv7_gfx_images="$armv7_gfx_images paz00"
# NXP
armv7_gfx_images="$armv7_gfx_images cuboxi sabrelite"
# Samsung
armv7_gfx_images="$armv7_gfx_images arndale chromebook"
# Texas Instruments
armv7_gfx_images="$armv7_gfx_images "

## armv7 JeOS only images
armv7_jeos_images="$armv7_gfx_images"
# Allwinner
armv7_jeos_images="$armv7_jeos_images a13olinuxino a20olinuxinolime a20olinuxinomicro bananapi cubieboard cubieboard2 cubietruck nanopineo nanopineoair olinuxinolime olinuxinolime2"
# Calxeda
armv7_jeos_images="$armv7_jeos_images midway-pxe"
# Intel
armv7_jeos_images="$armv7_jeos_images socfpgade0nanosoc"
# NXP
armv7_jeos_images="$armv7_jeos_images loco udooneo"
# Marvell
armv7_jeos_images="$armv7_jeos_images clearfog cubox"
# MediaTek
armv7_jeos_images="$armv7_jeos_images bananapiR2"
# Rockchip
if [[ $is_tumbleweed == 1 ]]; then
    armv7_jeos_images="$armv7_jeos_images fireflyrk3288 tinker"
fi
# Samsung
armv7_jeos_images="$armv7_jeos_images odroid odroidxu3"
# Texas Instruments
armv7_jeos_images="$armv7_jeos_images beagle beaglebone panda wega"
# Zynq
armv7_jeos_images="$armv7_jeos_images zturn"
# Generic EFI images
armv7_jeos_images="$armv7_jeos_images efi-pxe"

## aarch64 GFX images
aarch64_gfx_images="rootfs efi"
# Allwinner
aarch64_gfx_images="$aarch64_gfx_images pinephone"
# Broadcom
aarch64_gfx_images="$aarch64_gfx_images raspberrypi"
# Hisilicon
aarch64_gfx_images="$aarch64_gfx_images hikey"
# Rockchip
if [[ $is_tumbleweed == 1 ]]; then
    aarch64_gfx_images="$aarch64_gfx_images pinebookpro nanopim4b nanopct4"
fi

## aarch64 JeOS only images
aarch64_jeos_images="$aarch64_gfx_images efi-pxe efi-vmx m400-pxe"
# Allwinner
if [[ $is_tumbleweed == 1 ]]; then
    aarch64_jeos_images="$aarch64_jeos_images pine64 orangepizero2"
fi
# Amlogic
aarch64_jeos_images="$aarch64_jeos_images nanopik2 odroidc2 odroidc4 radxazero"
# Hisilicon
aarch64_jeos_images="$aarch64_jeos_images hikey960"
# Rockchip
if [[ $is_tumbleweed == 1 ]]; then
    aarch64_jeos_images="$aarch64_jeos_images rock64 rockpi4 rockpro64"
fi
# Xilinx
aarch64_jeos_images="$aarch64_jeos_images ultra96 zynqmp"

## aarch64 devel images
aarch64_devel_images="efi"

## x86_64 images
x86_64_gfx_images="rootfs efi"
x86_64_jeos_images="$x86_64_gfx_images efi-pxe"
x86_64_devel_images="efi"

## riscv64 images
riscv64_gfx_images="rootfs efi"
# SiFive
riscv64_gfx_images="$riscv64_gfx_images hifiveunmatched"
# StarFive
riscv64_gfx_images="$riscv64_gfx_images starfivejh7100"
riscv64_jeos_images="$riscv64_gfx_images efi-pxe"
# SiFive
riscv64_jeos_images="$riscv64_jeos_images hifiveunleashed"
riscv64_devel_images="efi"

echo "<multibuild>" > _multibuild

full_image_list=""
if [[ $is_tumbleweed == 1 ]]; then
    arch_list="armv6 armv7 aarch64 x86_64 riscv64"
else
    # armv6 and x86_64 disabled for Leap 15.3+
    arch_list="armv7 aarch64 riscv64"
fi
for arch in $arch_list; do
    image_list=""

    for img in jeos gfx devel; do
        eval ${img}_images=\"\$${arch}_${img}_images\"
    done
    for machinetype in $jeos_images; do
        image_list="$image_list JeOS-$machinetype"
    done
    for machinetype in $devel_images; do
        image_list="$image_list JeOS-$machinetype-devel"
    done
    for machinetype in $gfx_images; do
        image_list="$image_list E20-$machinetype"
        image_list="$image_list LXQT-$machinetype"
        image_list="$image_list XFCE-$machinetype"
        image_list="$image_list X11-$machinetype"
        image_list="$image_list KDE-$machinetype"
        image_list="$image_list GNOME-$machinetype"
        if [[ $machinetype == *pinephone ]]; then
            # PHOSH image only for the pinephone
            image_list="$image_list PHOSH-$machinetype PLAMO-$machinetype"
        fi
    done

    echo "Arch: $arch - image_list: $image_list"

    for i in $image_list; do
        if [[ $i == XFCE-* ]]; then
            image_type=TYPE_XFCE
            image_type_string="XFCE"
            flavor="${i/XFCE-/}"
        elif [[ $i == E20-* ]]; then
            image_type=TYPE_E20
            image_type_string="E20"
            flavor="${i/E20-/}"
        elif [[ $i == LXQT-* ]]; then
            image_type=TYPE_LXQT
            image_type_string="LXQT"
            flavor="${i/LXQT-/}"
        elif [[ $i == JeOS-* ]]; then
            image_type=TYPE_JEOS
            image_type_string="JeOS"
            flavor="${i/JeOS-/}"
        elif [[ $i == X11-* ]]; then
            image_type=TYPE_X11
            image_type_string="X11"
            flavor="${i/X11-/}"
	elif [[ $i == KDE-* ]]; then
            image_type=TYPE_KDE
            image_type_string="KDE"
            flavor="${i/KDE-/}"
	elif [[ $i == GNOME-* ]]; then
            image_type=TYPE_GNOME
            image_type_string="GNOME"
            flavor="${i/GNOME-/}"
        elif [[ $i == PHOSH-* ]]; then
            image_type=TYPE_PHOSH
            image_type_string="PHOSH"
            flavor="${i/PHOSH-/}"
        elif [[ $i == PLAMO-* ]]; then
            image_type=TYPE_PLAMO
            image_type_string="PLAMO"
            flavor="${i/PLAMO-/}"
        else
            echo "Unknown image type: $i"
            exit 1
        fi

        pxe=
        if [[ $i == *-pxe ]]; then
            flavor="${flavor/-pxe/}"
            pxe=1
        fi

        vmx=
        if [[ $i == *-vmx ]]; then
            flavor="${flavor/-vmx/}"
            vmx=1
        fi

        with_devel=
        if [[ $i == *-devel ]]; then
            flavor="${flavor/-devel/}"
            image_type_string+="-devel"
            with_devel=1
        fi

        # special cases: JeOS-rootfs is called "JeOS" as package and add .armv6 to armv6 rootfs
        if [ "$arch" = "armv7" ]; then
            # No suffix for armv7
            suffix=""
        else
            suffix=".$arch"
        fi
        if [ "$i" = "JeOS-rootfs" ]; then
            i="JeOS"$suffix
        elif [[ "$i" == *"-rootfs"* ]]; then
            i=$i$suffix
        elif [ "$flavor" == "efi" ] || [ "$flavor" == "raspberrypi" ]; then
            i=$i$suffix
        fi
        
        full_image_list="$full_image_list $i"
        
        # Determine if we're a contrib package
        CONTRIB_REPO=
        while read image contrib; do
            if [ "$flavor" = "$image" ]; then
                CONTRIB_REPO="$contrib"
            fi
        done < contribs

        # create kiwi description
        image_type_info="-DIMAGE_TYPE=$image_type -DIMAGE_TYPE_STRING=$image_type_string -DIS_ARCH_$arch=1 -DCHANGED=$headversion"
        [ "$pxe" ] && image_type_info="$image_type_info -DUSE_PXE"
        [ "$vmx" ] && image_type_info="$image_type_info -DUSE_VMX"
        [ "$with_devel" ] && image_type_info="$image_type_info -DUSE_DEVEL_PACKAGES"
        t=$(mktemp)
        echo "Formatting $i"
        flavor_type=$flavor
        if [ ! "$CONTRIB_REPO" ]; then
            $CPP $image_type_info -DIS_FLAVOR_$flavor=1 -DFLAVOR_TYPE=$flavor_type -P Images.kiwi.in -o $t
        else
            # contribs are Tumbleweed only
            if [[ $is_tumbleweed == 1 ]]; then
                $CPP $image_type_info -DIS_FLAVOR_$flavor=1 -DFLAVOR_TYPE=$flavor_type -DCONTRIB_REPO -P Images.kiwi.in -o $t
            else
                echo "Skip $flavor_type as it is from contrib"
            fi
        fi
        if [ -s $t ]; then
            # replace defines that cpp would ignore
            sed -i "s/FLAVOR_TYPE/$flavor_type/g;s/IMAGE_TYPE/$image_type/g;s/IMAGE_TYPE_STRING/$image_type_string/g;s/ARCH/$arch/g;s/KERNEL_CMDLINE_DEFAULT/loglevel=3 splash=silent systemd.show_status=1/g" $t
            # this will abort if there's an error (see set -e)
            xmllint --format $t --output $i.kiwi || break
            rm -f $t
        fi
        # get BOOTKERNEL var
        bootkernel=$(grep '<package name="kernel-' $i.kiwi | grep "bootinclude" | grep -vE "extra|optional" | sed 's/.*kernel-//;s/".*$//')

        

        # Add all non-contrib builds to the package build list
        if [ ! "$CONTRIB_REPO" ]; then
	    if [[ "$arch" == "armv7" ]] && [[ "$i" == "GNOME-"* ]] && [[ "$i" != "GNOME-rootfs"* ]] && [[ "$i" != "GNOME-efi"* ]]; then
		# GNOME is very slow but GNOME images may help to debug problems found on openQA, 
		# so do not build images in Factory:ARM, but allow local build
		echo "Skipping armv7 GNOME image in multibuild: $i"
	    else
		if [[ "$arch" == "armv7" ]] && [[ "$i" == "KDE-"* ]] && [[ "$i" != "KDE-rootfs"* ]] && [[ "$i" != "KDE-efi"* ]]; then
			# KDE is slow but KDE images may help to debug problems found on openQA, 
			# so do not build images in Factory:ARM, but allow local build
			echo "Skipping armv7 KDE images in multibuild: $i"
		else
			echo "<package>$i</package>" >> _multibuild
		fi
	    fi
	    
        fi

        # Create uboot-setup-*.tgz

        if [ "$flavor" != "rootfs" ]; then
	    # Check if editbootconfig var is used or not
            if ! [ "" = "$(grep 'editbootconfig' $i.kiwi)" ]; then
		    use_editbootconfig=1
	    else
		    # Clear vars (may be set in previous loop)
		    use_editbootconfig=
	    fi
            # create uboot scripts
            sed "s/FLAVOR/$flavor_type/g;s/TARGET/firstboot/g;s/BOOTKERNEL/$bootkernel/g;s/IS_FIRSTBOOT//g;" uboot-image-install.in > uboot-image-$flavor_type-install
            if [ "$use_editbootconfig" ]; then
		    sed "s/FLAVOR/$flavor_type/g;s/TARGET/firstboot/g;s/BOOTKERNEL/$bootkernel/g;s/IS_FIRSTBOOT//g;" uboot-image-setup.in > uboot-image-$flavor_type-setup
            fi
            rm -rf x y
            mkdir -p x/kiwi-hooks
            # kiwi 7.x
            sed "s/FLAVOR/$flavor/g;s/TARGET/boot/g;s/BOOTKERNEL/$bootkernel/g;s/IS_FIRSTBOOT/1/g;" installUBoot.sh > x/kiwi-hooks/installUBoot.sh
            if [ "$use_editbootconfig" ]; then
		    sed "s/FLAVOR/$flavor/g;s/TARGET/boot/g;s/BOOTKERNEL/$bootkernel/g;s/IS_FIRSTBOOT/1/g;" uboot-image-setup.in > x/kiwi-hooks/setupUBoot.sh
	    fi
            # kiwi 8.x
            sed "s/FLAVOR/$flavor/g;s/TARGET/boot/g;s/BOOTKERNEL/$bootkernel/g;s/IS_FIRSTBOOT/1/g;" installUBoot.sh > x/kiwi-hooks/postInstallBootLoader.sh
            if [ "$use_editbootconfig" ]; then
		    sed "s/FLAVOR/$flavor/g;s/TARGET/boot/g;s/BOOTKERNEL/$bootkernel/g;s/IS_FIRSTBOOT/1/g;" uboot-image-setup.in > x/kiwi-hooks/postSetupBootLoader.sh
	    fi
            # kiwi 7.x/8.x
            sed "s/FLAVOR/$flavor/g;s/TARGET/boot/g;s/BOOTKERNEL/$bootkernel/g;s/IS_FIRSTBOOT/1/g;" uboot-image-install.in > x/kiwi-hooks/preCallInit.sh
            chmod +x x/kiwi-hooks/*
            
            TGZ=uboot-setup-$flavor_type.tgz
            # Check if uboot-setup-*.tgz is used or not (needed for non dracut 1st boot)
            if grep -q "<archive name=\"uboot-setup-" $i.kiwi; then
                # compare to the old tar ball
                if [ -f $TGZ ]; then
                    mkdir -p y
                    tar -xzf $TGZ -C y
                    if ! diff -br x y > /dev/null; then
                        tar -czf $TGZ --owner root --group root -C x kiwi-hooks
                    fi
                    rm -rf x y
                else
                    tar -czf $TGZ --owner root --group root -C x kiwi-hooks
                    rm -rf x
                fi
            else
                # Clean-up
                rm -rf x y
                if [ -f $TGZ ]; then
                    rm $TGZ
                fi
            fi
            
        fi

        # Create contrib-repo-*.tgz

        if [ "$flavor" != "rootfs" ]; then
            rm -rf x y
            mkdir -p x/kiwi-hooks

            # add contrib repo name
            if [ "$CONTRIB_REPO" ]; then
                echo "$CONTRIB_REPO" > x/kiwi-hooks/contrib_repo
            fi
            
            TGZ=contrib-repo-$flavor_type.tgz
            # Check if contrib-repo-*.tgz is used or not (needed for contribs)
            if grep -q "<archive name=\"contrib-repo-" $i.kiwi; then
                # compare to the old tar ball
                if [ -f $TGZ ]; then
                    mkdir -p y
                    tar -xzf $TGZ -C y
                    if ! diff -br x y > /dev/null; then
                        tar -czf $TGZ --owner root --group root -C x kiwi-hooks
                    fi
                    rm -rf x y
                else
                    tar -czf $TGZ --owner root --group root -C x kiwi-hooks
                    rm -rf x
                fi
            else
                # Clean-up
                rm -rf x y
                if [ -f $TGZ ]; then
                    rm $TGZ
                fi
            fi
            
        fi
    done
done

echo "</multibuild>" >> _multibuild


# Update _constraints to relax constraints for JeOS*
echo "Updating _constraints"
echo '<?xml version="1.0"?>
<constraints>
  <hardware>
    <physicalmemory>
      <size unit="M">2000</size>
    </physicalmemory>
    <disk>
      <size unit="G">20</size>
    </disk>
  </hardware>
  <overwrite>
    <conditions>' > _constraints
for i in $full_image_list; do
    if [[ $i == JeOS-* ]]; then
        echo "      <package>JeOS:$i</package>" >> _constraints
    fi
done
echo '    </conditions>
    <hardware>
      <disk>
         <size unit="M">8900</size>
      </disk>
    </hardware>
  </overwrite>
</constraints>' >> _constraints
openSUSE Build Service is sponsored by