jenkins: extract common parts of build scripts to separate file

Have all complexity in one common shell script, greatly simplify the individual
scripts.

This allows to provide a specific branch or git hash to build instead of
current master. Some scripts allowed to provide branch names before, this now
also allows using git hashes directly.

Environment variables can be used to override the git hash/branch to use for
specific repositories.

Motivation for this patch: we need this to investigate failure causes more
easily.

Change-Id: I5ac2f90d006a1b2f6c246976346d852a70c89089
diff --git a/contrib/jenkins-build-osmo-bts-sysmo.sh b/contrib/jenkins-build-osmo-bts-sysmo.sh
index 9838834..dad2151 100755
--- a/contrib/jenkins-build-osmo-bts-sysmo.sh
+++ b/contrib/jenkins-build-osmo-bts-sysmo.sh
@@ -1,92 +1,23 @@
 #!/bin/sh
 set -e -x
 
-deps="
-libosmocore
-libosmo-abis
-osmo-bts
-"
+poky="/opt/poky/1.5.4"
+. "$poky/environment-setup-armv5te-poky-linux-gnueabi"
+
+# Cross-compilation: all installations need to be put in the sysmo SDK sysroot
+export DESTDIR="$poky/sysroots/armv5te-poky-linux-gnueabi"
 
 base="$PWD"
-rm -f "$base/osmo-bts-sysmo.*.tgz"
-
-have_repo() {
-	repo="$1"
-	cd "$base"
-	if [ ! -d "$repo" ]; then
-		git clone "git://git.osmocom.org/$repo" "$repo"
-	fi
-	cd "$repo"
-	git clean -dxf
-	git fetch origin
-	git reset --hard origin/master
-	git rev-parse HEAD
-	cd "$base"
-}
-
-for dep in $deps; do
-    have_repo "$dep"
-done
+name="osmo-bts-sysmo"
+prefix="/usr/local/jenkins-build/inst-$name"
+prefix_real="$DESTDIR$prefix"
+. "$(dirname "$0")/jenkins-build-common.sh"
 
 # for gsm_data_shared.h
 have_repo openbsc
 
-. /opt/poky/1.5.4/environment-setup-armv5te-poky-linux-gnueabi
+build_repo libosmocore --disable-pcsc
+build_repo libosmo-abis
+build_repo osmo-bts --enable-sysmocom-bts --with-openbsc=$base/openbsc/openbsc/include
 
-# Cross-compilation: all installations need to be put in the sysmo SDK sysroot
-export DESTDIR="/opt/poky/1.5.4/sysroots/armv5te-poky-linux-gnueabi"
-
-prefix_base="/usr/local/jenkins-build"
-prefix_base_real="$DESTDIR$prefix_base"
-rm -rf "$prefix_base_real"
-
-prefix="$prefix_base/inst-osmo-bts-sysmo"
-prefix_real="$DESTDIR$prefix"
-mkdir -p "$prefix_real"
-
-# Installation in non-system dir, but keep the PKG_CONFIG_PATH from the SDK:
-export PKG_CONFIG_PATH="$prefix_real/lib/pkgconfig:$PKG_CONFIG_PATH"
-
-env
-
-for dep in $deps; do
-	set +x; echo "
-
-====================== $dep
-
-"; set -x
-
-        cd "$base/$dep"
-        rm -rf *
-        git checkout .
-
-        echo "$(git rev-parse HEAD) $dep" >> "$prefix_real/osmo-bts-sysmo_git_hashes.txt"
-
-        autoreconf -fi
-
-        config_opts=""
-        case "$dep" in
-        'libosmocore')    config_opts="--disable-pcsc" ;;
-        'osmo-bts')       config_opts="--enable-sysmocom-bts --with-openbsc=$base/openbsc/openbsc/include" ;;
-        esac
-
-	set +x;	echo; echo; set -x
-        ./configure --prefix="$prefix" $CONFIGURE_FLAGS $config_opts
-	set +x;	echo; echo; set -x
-        make -j8
-	set +x;	echo; echo; set -x
-        make install
-done
-
-# don't package documentation -- the libosmocore docs can be up to 16 Mb large,
-# a significant amount compared to the binaries
-rm -rf "$prefix_real/share/doc"
-
-# build the archive that is going to be copied to the tester and then to the BTS
-rm "$base"/*.tgz "$base"/*.md5 || true
-cd "$prefix_real"
-this="osmo-bts-sysmo.build-${BUILD_NUMBER}"
-tar="${this}.tgz"
-tar czf "$base/$tar" *
-cd "$base"
-md5sum "$tar" > "${this}.md5"
+create_bin_tgz