Home | History | Annotate | Download | only in asmack-master
      1 #!/bin/bash
      2 
      3 svnfetch() {
      4     REV="${3:-HEAD}"
      5     echo "Fetching from ${1} to ${2} at revision ${REV}"
      6     cd $SRC_DIR
      7     if ! [ -f "${2}/.svn/entries" ]; then
      8 	mkdir "${2}"
      9 	cd "${2}"
     10 	svn co --non-interactive --trust-server-cert "${1}" -r "${REV}" "."
     11     else
     12 	cd "${2}"
     13 	svn cleanup
     14 	svn up -r "${REV}"
     15     fi
     16 }
     17 
     18 gitfetch() {
     19     echo "Fetching ${2} branch/commit from ${1} to ${3} via git"
     20     cd $SRC_DIR
     21     if ! [ -f "${3}/.git/config" ]; then
     22 	git clone "${1}" "${3}"
     23 	cd "${3}"
     24 	git checkout "${2}"
     25     else
     26 	cd "${3}"
     27 	git fetch
     28 	git checkout "${2}"
     29     fi
     30 
     31     if [ $? -ne 0 ]; then
     32 	exit
     33     fi
     34 }
     35 
     36 hgfetch() {
     37 (
     38   echo "Fetching ${2} branch from ${1} to ${3} via mercurial"
     39   cd src
     40   if [ -e "${2}/.hg" ] ; then
     41       cd ${2}
     42       hg pull
     43   else
     44       hg clone "${1}" "${2}"
     45   fi
     46   hg up -r ${3}
     47 )
     48     if [ $? -ne 0 ]; then
     49 	exit
     50     fi
     51 }
     52 
     53 
     54 testsmackgit() {
     55     cd $SRC_DIR
     56     if [ -f .used-smack-git-repo ] && [ $(cat .used-smack-git-repo) != $SMACK_REPO ] ; then
     57 	    echo "Used smack repository has changed!"
     58 	    echo "Old: $(cat .used-smack-git-repo) New: ${SMACK_REPO}."
     59 	    echo "Deleting old local copy"
     60 	    rm -rf smack
     61     fi
     62     echo "${SMACK_REPO}" > .used-smack-git-repo
     63 }
     64 
     65 fetchall() {
     66     echo "## Step 15: fetching sources"
     67     if $SMACK_LOCAL ; then
     68 	# always clean the local copy first
     69 	rm -rf ${SRC_DIR}/smack
     70 	mkdir ${SRC_DIR}/smack
     71 	cd $SMACK_REPO
     72 	git archive $SMACK_BRANCH | tar -x -C ${SRC_DIR}/smack
     73 	if [ $? -ne 0 ]; then
     74 	    exit
     75 	fi
     76     else
     77 	execute gitfetch "$SMACK_REPO" "$SMACK_BRANCH" "smack"
     78     fi
     79 
     80     if ! $UPDATE_REMOTE ; then
     81 	echo "Won't update or fetch third party resources"
     82 	wait
     83 	return
     84     fi
     85 
     86     execute svnfetch "http://svn.apache.org/repos/asf/qpid/trunk/qpid/java/management/common/src/main/" "qpid"
     87     execute svnfetch "http://svn.apache.org/repos/asf/harmony/enhanced/java/trunk/classlib/modules/auth/src/main/java/common/" "harmony" 
     88     execute svnfetch "https://dnsjava.svn.sourceforge.net/svnroot/dnsjava/trunk" "dnsjava"
     89     execute gitfetch "git://kenai.com/jbosh~origin" "master" "jbosh"
     90     # jldap doesn't compile with the latest version (missing deps?), therefore it's a fixed version for now
     91     #  execute gitfetch "git://git.openldap.org/openldap-jldap.git" "master" "novell-openldap-jldap"
     92     wait
     93 }
     94 
     95 createVersionTag() {
     96     # Skip this step is no version tag is given
     97     [[ -z $VERSION_TAG ]] && return
     98 
     99     local v
    100     cat <<EOF  > $TAG_FILE
    101 #!/bin/bash
    102 
    103 # This file contains the version information of the components that
    104 # were used to build this aSmack version
    105 
    106 declare -g -A COMPONENT_VERSIONS
    107 EOF
    108 
    109     for d in $(ls $SRC_DIR) ; do
    110 	cd $SRC_DIR
    111 
    112 	# Don't record the components version for static-src
    113 	for static in $(ls ${ASMACK_BASE}/static-src) ; do
    114 	    # Don't record the version if it's from the static sources
    115 	    [ $d == $static ] && continue
    116 	done
    117 
    118 	if [[ -d $d/.git ]] ; then
    119 	    v=$(cd $d && git rev-parse HEAD)
    120 	    key=$d
    121 	    COMPONENT_VERSIONS["$d"]=$v
    122 	elif [[ -d $d/.svn ]] ; then
    123 	    v=$(cd $d && svn info |grep Revision |cut -f 2 -d ' ')
    124 	    key=$d
    125 	    COMPONENT_VERSIONS["$d"]=$v
    126 	fi
    127     done
    128 
    129     if $SMACK_LOCAL ; then
    130 	cd $SMACK_REPO
    131 	v=$(git rev-parse HEAD)
    132 	COMPONENT_VERSIONS[smack]=$v
    133     fi
    134 
    135     cd ${ASMACK_BASE}
    136     v=$(git rev-parse HEAD)
    137     COMPONENT_VERSIONS[asmack]=$v
    138 
    139     for i in "${!COMPONENT_VERSIONS[@]}" ; do
    140 	echo "COMPONENT_VERSIONS[$i]=${COMPONENT_VERSIONS[$i]}" >> $TAG_FILE
    141     done
    142 }
    143 
    144 copyfolder() {
    145   cd ${ASMACK_BASE}
    146   (
    147     cd "${1}"
    148     tar -cSsp --exclude-vcs "${3}"
    149   ) | (
    150     cd "${2}"
    151     tar -xSsp
    152   )
    153   wait
    154 }
    155 
    156 createbuildsrc() {
    157   echo "## Step 20: creating build/src"
    158   cd "${ASMACK_BASE}"
    159   rm -rf build/src
    160   mkdir -p build/src/trunk
    161 
    162   execute copyfolder "src/smack/source/" "build/src/trunk" "."
    163   execute copyfolder "src/qpid/java" "build/src/trunk" "org/apache/qpid/management/common/sasl"
    164   execute copyfolder "src/novell-openldap-jldap" "build/src/trunk" "."
    165   execute copyfolder "src/dnsjava"  "build/src/trunk" "org"
    166   execute copyfolder "src/harmony" "build/src/trunk" "."
    167   execute copyfolder "src/jbosh/src/main/java" "build/src/trunk" "."
    168   if $BUILD_JINGLE ; then
    169     execute copyfolder "src/smack/jingle/extension/source/" "build/src/trunk" "."
    170   fi
    171   wait
    172   # custom overwrites some files from smack, so this has to be done as last
    173   copyfolder "src/custom" "build/src/trunk" "."
    174 }
    175 
    176 patchsrc() {
    177     echo "## Step 25: patch build/src"
    178     cd ${ASMACK_BASE}/build/src/trunk/
    179     for PATCH in `(cd "../../../${1}" ; find -maxdepth 1 -type f)|sort` ; do
    180 	echo $PATCH
    181 	if [[ $PATCH == *.sh ]]; then
    182 	    "../../../${1}/$PATCH" || exit 1
    183 	elif [[ $PATCH == *.patch ]]; then
    184 	    patch -p0 < "../../../${1}/$PATCH" || exit 1
    185 	fi
    186     done
    187 }
    188 
    189 build() {
    190   echo "## Step 30: compile"
    191   buildandroid
    192   if [ $? -ne 0 ]; then
    193       exit 1
    194   fi
    195 }
    196 
    197 buildandroid() {
    198     local sdklocation
    199     local version
    200     local sdks
    201     local minSdkVer=8
    202 
    203     cd $ASMACK_BASE
    204 
    205     if [ ! -f local.properties ] ; then
    206 	echo "Could not find local.properties file"
    207 	echo "See local.properties.example"
    208 	exit 1
    209     fi
    210 
    211     sdklocation=$(grep sdk-location local.properties| cut -d= -f2)
    212     if [ -z "$sdklocation" ] ; then
    213 	echo "Android SDK not found. Don't build android version"
    214 	exit 1
    215     fi
    216     for f in ${sdklocation/\$\{user.home\}/$HOME}/platforms/* ; do
    217 	version=`basename $f`
    218 	if [[ "$version" != android-* ]] ; then
    219 	    echo "$sdklocation contains no Android SDKs"
    220 	    exit 1
    221 	fi
    222 	if [[ ${version#android-} -ge $minSdkVer ]] ; then
    223 	    if [ -n $BUILD_ANDROID_VERSIONS ] ; then
    224 		for build_version in $BUILD_ANDROID_VERSIONS ; do
    225 		    [ ${version#android-} != $build_version ] && continue 2
    226 		done
    227 	    fi
    228 	    echo "Building for ${version}"
    229 	    sdks="${sdks} ${version}\n"
    230 	fi
    231 
    232     done
    233 
    234     if [ -z "${sdks}" ] ; then
    235 	echo "No SDKs of a suitable minimal API (${minSdkVer}) version found"
    236 	exit 1
    237     fi
    238 
    239     local asmack_suffix
    240     if [[ -n ${VERSION_TAG} ]] && [[ -n ${1} ]] ; then
    241 	asmack_suffix="${1}-${VERSION_TAG}"
    242     elif [[ -n ${VERSION_TAG} ]] ; then
    243 	asmack_suffix="-${VERSION_TAG}"
    244     else
    245 	asmack_suffix="${1}"
    246     fi
    247     if ! echo -e ${sdks} \
    248 	| xargs -I{} -n 1 $XARGS_ARGS ant \
    249 		-Dandroid.version={} \
    250 		-Djar.suffix="${asmack_suffix}" \
    251 		compile-android ; then
    252 	exit 1
    253     fi
    254 }
    255 
    256 buildcustom() {
    257   for dir in `find patch -maxdepth 1 -mindepth 1 -type d`; do
    258     buildsrc
    259     patchsrc "patch"
    260     if $BUILD_JINGLE ; then
    261       patchsrc "jingle"
    262       JINGLE_ARGS="-Djingle=lib/jstun.jar"
    263     fi
    264     patchsrc "${dir}"
    265     local custom
    266     custom=$(echo ${dir} | sed 's:patch/:-:')
    267     ant -Djar.suffix="${custom}" $JINGLE_ARGS
    268     buildandroid "${custom}"
    269   done
    270 }
    271 
    272 parseopts() {
    273     while getopts a:b:r:t:cdhjpux OPTION "$@"; do
    274 	case $OPTION in
    275 	    a)
    276 		BUILD_ANDROID_VERSIONS="${OPTARG}"
    277 		;;
    278 	    r)
    279 		SMACK_REPO="${OPTARG}"
    280 		;;
    281 	    b)
    282 		SMACK_BRANCH="${OPTARG}"
    283 		;;
    284 	    d)
    285 		set -x
    286 		XARGS_ARGS="-t"
    287 		;;
    288 	    j)
    289 		BUILD_JINGLE=true
    290 		;;
    291 	    u)
    292 		UPDATE_REMOTE=false
    293 		;;
    294 	    c)
    295 		BUILD_CUSTOM=true
    296 		;;
    297 	    p)
    298 		PARALLEL_BUILD=true
    299 		;;
    300 	    t)
    301 		VERSION_TAG="${OPTARG}"
    302 		;;
    303 	    x)
    304 		PUBLISH_RELEASE=true
    305 		;;
    306 	    h)
    307 		echo "$0 -d -c -u -j -r <repo> -b <branch>"
    308 		echo "-d: Enable debug"
    309 		echo "-j: Build jingle code"
    310 		echo "-c: Apply custom patchs from patch directory"
    311 		echo "-u: DON'T update remote third party resources"
    312 		echo "-r <repo>: Git repository (can be local or remote) for underlying smack repository"
    313 		echo "-b <branch>: Git branch used to build aSmack from underlying smack repository"
    314 		echo "-p use parallel build where possible"
    315 		echo "-t <version>: Create a new version tag. You should build aSmack before calling this"
    316 		echo "-x: Publish the release"
    317 		echo "-a <SDK Version(s)>: Build only for the given Android SDK versions"
    318 		exit
    319 		;;
    320 	esac
    321     done
    322 }
    323 
    324 prepareRelease() {
    325     if [[ -z ${VERSION_TAG} ]]; then
    326 	echo "Version tag is not set. Not going to prepare a release"
    327 	return
    328     fi
    329 
    330     if [ -d $RELEASE_DIR ] ; then
    331 	rm -rf $RELEASE_DIR
    332     fi
    333     mkdir -p $RELEASE_DIR
    334 
    335     mv ${ASMACK_BASE}/build/*.{jar,zip} ${RELEASE_DIR}/
    336     cp $TAG_FILE ${RELEASE_DIR}/
    337     cp ${ASMACK_BASE}/CHANGELOG ${RELEASE_DIR}
    338 
    339     if [ -n $GPG_KEY ] ; then
    340 	find $RELEASE_DIR -maxdepth 1 -and \( -name '*.jar' -or -name '*.zip' \) -print0 \
    341 	    | xargs -n 1 -0 $XARGS_ARGS gpg --local-user $GPG_KEY --detach-sign
    342     fi
    343 
    344     find $RELEASE_DIR -maxdepth 1 -and \( -name '*.jar' -or -name '*.zip' \) -print0 \
    345 	| xargs -I{} -n 1 -0 $XARGS_ARGS sh -c 'md5sum {} > {}.md5'
    346 
    347     local release_readme
    348     release_readme=${RELEASE_DIR}/README
    349 
    350     sed \
    351 	-e "s/\$VERSION_TAG/${VERSION_TAG}/" \
    352 	-e "s/\$BUILD_DATE/${BUILD_DATE}/" \
    353 	README.asmack > $release_readme
    354 
    355     # Pretty print the component versions at the end of README
    356     # Note that there is an exclamation mark at the beginning of the
    357     # associative array to access the keys
    358     for i in "${!COMPONENT_VERSIONS[@]}" ; do
    359 	local tabs
    360 	if [[ ${#i} -le 6 ]] ; then
    361 	    tabs="\t\t"
    362 	else
    363 	    tabs="\t"
    364 	fi
    365 	echo -e "${i}:${tabs}${COMPONENT_VERSIONS[$i]}" >> $release_readme
    366     done
    367 }
    368 
    369 publishRelease() {
    370     if [[ -z ${VERSION_TAG} ]]; then
    371 	echo "Version tag is not set. Not going to prepare a release"
    372 	return
    373     fi
    374 
    375     if [[ -z ${PUBLISH_RELEASE} ]]; then
    376 	echo "User doesn't want to publish this release"
    377 	return
    378     fi
    379 
    380     if [[ -z $PUBLISH_HOST || -z $PUBLISH_DIR ]]; then
    381 	echo "WARNING: Not going to publish this release as either $PUBLISH_HOST or $PUBLISH_DIR is not set"
    382 	return
    383     fi
    384 
    385     cd ${ASMACK_RELEASES}
    386     cat <<EOF | sftp $PUBLISH_HOST
    387 rm ${PUBLISH_DIR}/${VERSION_TAG}/*
    388 mkdir ${PUBLISH_DIR}/${VERSION_TAG}
    389 put -r $VERSION_TAG $PUBLISH_DIR
    390 EOF
    391 }
    392 
    393 islocalrepo() {
    394     local R="^(git|ssh)"
    395     if [[ $1 =~ $R ]]; then
    396 	return 1
    397     else
    398 	return 0
    399     fi
    400 }
    401 
    402 initialize() {
    403     echo "## Step 00: initialize"
    404     if ! [ -d build/src/trunk ]; then
    405 	mkdir -p build/src/trunk
    406     fi
    407     if [ ! -d src/ ]; then
    408 	mkdir src
    409     fi
    410     find build \( -name '*.jar' -or -name '*.zip' \) -print0 | xargs -0 rm -f
    411     rm -rf src/custom
    412 }
    413 
    414 cleanup() {
    415     echo "## Deleting all temporary files"
    416     rm -rf build
    417     rm -rf src
    418 }
    419 
    420 copystaticsrc() {
    421     cp -ur static-src/* src/
    422 }
    423 
    424 cmdExists() {
    425     command -v $1 &> /dev/null
    426     return $?
    427 }
    428 
    429 prettyPrintSeconds() {
    430     local ttime
    431     if (( $1 > 59 )); then
    432 	ttime=$(printf "%dm %ds\n" $(($1/60%60)) $(($1%60)) )
    433     else
    434 	ttime=$(printf "%ds\n" $(($1)) )
    435     fi
    436     echo "Execution took $ttime"
    437 }
    438 
    439 execute() {
    440     if [ -n "$BACKGROUND" ]; then
    441 	"$@" &
    442     else
    443 	"$@"
    444     fi
    445 }
    446 
    447 setdefaults() {
    448     # Default configuration, can be changed with script arguments
    449     SMACK_REPO=git://github.com/Flowdalic/smack.git
    450     SMACK_BRANCH=master
    451     SMACK_LOCAL=false
    452     UPDATE_REMOTE=true
    453     BUILD_CUSTOM=false
    454     BUILD_JINGLE=false
    455     JINGLE_ARGS=""
    456     PARALLEL_BUILD=false
    457     VERSION_TAG=""
    458     PUBLISH_RELEASE=""
    459     PUBLISH_HOST=""
    460     PUBLISH_DIR=""
    461     BUILD_ANDROID_VERSIONS=""
    462 
    463     # Often used variables
    464     ASMACK_BASE=$(pwd)
    465     ASMACK_RELEASES=${ASMACK_BASE}/releases
    466     SRC_DIR=${ASMACK_BASE}/src
    467     VERSION_TAG_DIR=${ASMACK_BASE}/version-tags
    468     STARTTIME=$(date -u "+%s")
    469     BUILD_DATE=$(date)
    470     # Declare an associative array that is in global scope ('-g')
    471     declare -g -A COMPONENT_VERSIONS
    472 }
    473 
    474 parseconfig() {
    475     if [ -f ${ASMACK_BASE}/config ]; then
    476 	source ${ASMACK_BASE}/config
    477     fi
    478 }
    479 
    480 setconfig() {
    481     if [ ${PARALLEL_BUILD} == "true" ]; then
    482 	XARGS_ARGS="${XARGS_ARGS} -P4"
    483 	BACKGROUND="true"
    484     else
    485 	XARGS_ARGS=""
    486 	BACKGROUND=""
    487     fi
    488 
    489     if islocalrepo $SMACK_REPO; then
    490 	SMACK_LOCAL=true
    491 	SMACK_REPO=`readlink -f $SMACK_REPO`
    492     fi
    493 
    494     if [[ -n ${VERSION_TAG} ]]; then
    495 	if ! grep ${VERSION_TAG} CHANGELOG; then
    496 	    echo "Could not find the tag in the CHANGELOG file. Please write a short summary of changes"
    497 	    exit 1
    498 	fi
    499 	if ! git diff --exit-code; then
    500 	    echo "Unstaged changes found, please stages your changes"
    501 	    exit 1
    502 	fi
    503 	if ! git diff --cached --exit-code; then
    504 	    echo "Staged, but uncommited changes found, please commit"
    505 	    exit 1
    506 	fi
    507 	RELEASE_DIR=${ASMACK_RELEASES}/${VERSION_TAG}
    508 	TAG_FILE=${VERSION_TAG_DIR}/${VERSION_TAG}.tag
    509     fi
    510 }
    511 
    512 printconfig() {
    513     echo "Smack git repository $SMACK_REPO with branch $SMACK_BRANCH"
    514     echo -e "SMACK_LOCAL:$SMACK_LOCAL\tUPDATE_REMOTE:$UPDATE_REMOTE\tBUILD_CUSTOM:$BUILD_CUSTOM\tBUILD_JINGLE:$BUILD_JINGLE"
    515     echo -e "PARALLEL_BUILD:$PARALLEL_BUILD\tBASE:$ASMACK_BASE"
    516 }
    517 
    518 checkPrerequisites() {
    519     if [[ $BASH_VERSION < 4 ]] ; then
    520 	echo "aSmack's build.bash needs at least bash version 4"
    521 	exit 1
    522     fi
    523 
    524     if ! tar --version |grep GNU &> /dev/null ; then
    525 	echo "aSmack's build.bash needs GNU tar"
    526 	exit 1
    527     fi
    528 }
    529 
    530 # Main
    531 
    532 setdefaults
    533 parseopts $@
    534 checkPrerequisites
    535 parseconfig
    536 setconfig
    537 printconfig
    538 initialize
    539 copystaticsrc
    540 testsmackgit
    541 fetchall
    542 createVersionTag
    543 createbuildsrc
    544 patchsrc "patch"
    545 
    546 ##
    547 ## BEGIN Modification for android platform build system
    548 ##
    549 echo done with android modifications
    550 exit
    551 ##
    552 ## END Modification for android platform build system
    553 ##
    554 
    555 if $BUILD_JINGLE ; then
    556   patchsrc "jingle"
    557   JINGLE_ARGS="-Djingle=lib/jstun.jar"
    558 fi
    559 build
    560 
    561 if $BUILD_CUSTOM ; then
    562     buildcustom
    563 fi
    564 
    565 if cmdExists advzip ; then
    566   echo "advzip found, compressing files"
    567   find build \( -name '*.jar' -or -name '*.zip' \) -print0 | xargs -n 1 -0 $XARGS_ARGS advzip -z4
    568 else
    569   echo "Could not find the advzip command."
    570   echo "advzip will further reduce the size of the generated jar and zip files,"
    571   echo "consider installing advzip"
    572 fi
    573 
    574 prepareRelease
    575 publishRelease
    576 
    577 STOPTIME=$(date -u "+%s")
    578 RUNTIME=$(( $STOPTIME - $STARTTIME ))
    579 prettyPrintSeconds $RUNTIME
    580 printconfig
    581