#!/bin/sh
# Top level jenkins script to build for promoted AMDT libraries, and verify
# that the built results have all of the expected bits.
# Performs builds for 32 and 64 bit versions
# An optional parameter is the build number.
#   If no build number is passed in, do not modify the version file
# Also generate CodeXL-Linux[x86|x86_64].xml file for the updater

export AMD_PERFORCE_ROOT=$WORKSPACE
export AMD_AMDLIBS=$AMD_PERFORCE_ROOT/main/Common/Lib/AMD
export AMD_EXTLIBS=$AMD_PERFORCE_ROOT/main/Common/Lib/Ext
export AMD_OUTPUT=$AMD_PERFORCE_ROOT/main/Output
export AMD_CODEXL=$AMD_PERFORCE_ROOT/main/CodeXL
export AMD_WS_LINUX_UTIL_DIR=$AMD_CODEXL/Util/linux

export NARGS=$#
export VERSION_MAJOR=0
export VERSION_MINOR=9
export VERSION_BUILD=0
export VERSION_UPDATE=0
export VERSIONINFO_FILE=${AMD_CODEXL}/../CommonProjects/VersionInfo/VersionInfo.h

export CONFIG_FILE_BASE=CodeXL-Linux

NUM_ERRORS=0
SETUP_RC=0
BUILD_DEBUG_RC=0
BUILD_RELEASE_RC=0

# IS_QUICK_BOUNDED_BUILD - a flag to use to determine if this is an unofficial
# check build
export IS_QUICK_BOUNDED_BUILD=0

if [ ${NARGS} -gt 1 ]
then
    echo "Usage: $0 [optional build number]"
    echo "If the build number is 0 (zero) a compile-only (x86_64, debug) build is performed."
    echo "No packaging is performed at all, no matter what"
    exit 1
fi
# if a build number is passed in, set the version information.
# the default is to leave it in the original form (meaning a local developer build)
if [ ${NARGS} -eq 1 ]
then
    VERSION_BUILD=$1
    if [ "${VERSION_BUILD}" = "0" ]
    then
        IS_QUICK_BOUNDED_BUILD=1
    fi
    # Edit the version file - in situ.  A different approach would be to
    # dynamically generate said file (perhaps in the future)
    chmod 666 ${VERSIONINFO_FILE}
    CHM_RC=$?
    NUM_ERRORS=`expr ${NUM_ERRORS} + ${CHM_RC}`
    if [ ${CHM_RC} -ne 0 ]
    then
        echo "*** ERROR ***"
        echo "*** Unable to modify ${VERSIONINFO_FILE} ***"
        echo "*** The default version numbers will be used ***"
    else
        # The pattern to match on in the file is 1,0,0,0
        # meaning major,minor,build,update
        OLDPAT=0,0,0,0
        NEWPAT=${VERSION_MAJOR},${VERSION_MINOR},${VERSION_BUILD},${VERSION_UPDATE}
        # And mangle
        sed -i "s/${OLDPAT}/${NEWPAT}/g" ${VERSIONINFO_FILE}
        SED_RC=$?
        NUM_ERRORS=`expr ${NUM_ERRORS} + ${SED_RC}`
        if [ ${SED_RC} -ne 0 ]
        then
            echo "*** ERROR ***"
            echo "*** Unable to modify fields in ${VERSIONINFO_FILE} ***"
            echo "*** The build may fail due to compile errors ***"
        fi
    fi
fi

# Figure out how much parallelism we can use.  2x cores on a really fast system
# The build system is pretty heavily used - both for tests and builds, so dial
# this back to be just the number of cores
NCPUS=`grep processor /proc/cpuinfo | wc | awk '{print $1}'`
if [ $? -ne 0 ]
then
    echo "Warning - Unable to determine the number of CPU cores - assuming one core"
    NCPUS=1
fi
# If the system is capable of more cores, re-enable this
NCPUS=`expr ${NCPUS} \* 2`
echo "Building with -j ${NCPUS}"

# The bulk of the work is done here
# The only parameter is the architecture name (x86 or x86_64)
RunTheBuild()
{
    ARCHTYPE=$1

    cd $AMD_WS_LINUX_UTIL_DIR
    AMD_OUTPUT=$AMD_PERFORCE_ROOT/main/Output_${ARCHTYPE}
    mkdir -p ${AMD_OUTPUT}
    /usr/bin/time -p bash ./copyLibrariesToOutputForCodeXLFull ${ARCHTYPE}
    SETUP_RC=$?
    NUM_ERRORS=`expr ${NUM_ERRORS} + ${SETUP_RC}`
    if [ ${SETUP_RC} -ne 0 ]
    then
        echo "*** ERROR ***"
        echo "*** the copyLibraries step failed for the ${ARCHTYPE} part of the build ***"
        echo "*** Skipping the build itself for ${ARCHTYPE} ***"
        BUILD_RELEASE_RC=0
    else
        # build release first, followed by the debug build
        # Now that we have a stable build, we can fail out on the first error.
        if [ ${IS_QUICK_BOUNDED_BUILD} -eq 0 ]
        then
	        echo ""
	        echo " Building:  /usr/bin/time -p sh $AMD_WS_LINUX_UTIL_DIR/buildCodeXLFullLinuxProjects CXL_build=release build=release release=1 arch=${ARCHTYPE} -Q install -j ${NCPUS}"
	        echo "================================================================================================"
            /usr/bin/time -p sh $AMD_WS_LINUX_UTIL_DIR/buildCodeXLFullLinuxProjects CXL_build=release build=release release=1 arch=${ARCHTYPE} -Q install -j ${NCPUS} > ${AMD_OUTPUT}/CodeXLLinux_${ARCHTYPE}_release.log 2>&1
            BUILD_RELEASE_RC=$?
        fi

        # build the debug version
	    echo ""
	    echo " Building: /usr/bin/time -p sh $AMD_WS_LINUX_UTIL_DIR/buildCodeXLFullLinuxProjects CXL_build=debug build=debug arch=${ARCHTYPE} -Q install -j ${NCPUS}"
	    echo "================================================================================================"
        /usr/bin/time -p sh $AMD_WS_LINUX_UTIL_DIR/buildCodeXLFullLinuxProjects CXL_build=debug build=debug arch=${ARCHTYPE} -Q install -j ${NCPUS} > ${AMD_OUTPUT}/CodeXLLinux_${ARCHTYPE}_debug.log 2>&1
        BUILD_DEBUG_RC=$?
    fi

    # Copy the logs to the top level
    cp ${AMD_OUTPUT}/CodeXL*.log ${AMD_OUTPUT}/../..

    NUM_ERRORS=`expr ${NUM_ERRORS} + ${BUILD_RELEASE_RC} + ${BUILD_DEBUG_RC}`
    if [ ${SETUP_RC} -ne 0 -o ${BUILD_RELEASE_RC} -ne 0 -o ${BUILD_DEBUG_RC} -ne 0 ]
    then
        echo "*** ERROR ***"
        echo "*** The setup (copying of libraries) had ${SETUP_RC} errors ***"
        echo "*** The debug build had ${BUILD_DEBUG_RC} errors ***"
        echo "*** The release build had ${BUILD_RELEASE_RC} errors ***"
    else
	echo "Build succeeded"
    fi

}

PackageTheBuild()
{
    cd $AMD_PERFORCE_ROOT/main

    # Platform Decision 7 Aug 2012: No 32-bit support in V1.0
    # Cleanup: delete objects
    # rm -rf Output_x86/release/obj
    # rm -rf Output_x86/debug/obj
    rm -rf Output_x86_64/release/obj
    rm -rf Output_x86_64/debug/obj

    # Preserve the debug content, but do not copy them for inclusion into the tarball
    mkdir -p CodeXL-Full
    # if [ ${IS_QUICK_BOUNDED_BUILD} -eq 0 ]
    # then
    #     mkdir -p CodeXL-Full/Output_x86
    #     cp -rp Output_x86/release CodeXL-Full/Output_x86
    # fi
    mkdir -p CodeXL-Full/Output_x86_64
    cp -rp Output_x86_64/release CodeXL-Full/Output_x86_64
    tar czf CodeXL-Full-${VERSION_BUILD}.tar.gz CodeXL-Full
}

# Main body of doing the build
# Platform Decision 7 Aug 2012: No 32-bit support in V1.0
# if [ ${IS_QUICK_BOUNDED_BUILD} -eq 0 ]
# then
#     RunTheBuild x86
# fi
RunTheBuild x86_64

if [ ${NUM_ERRORS} -ne 0 ]
then
    echo "*** The build failed ***"
    exit 1
else
    PackageTheBuild
    echo "*** The build succeeded ***"
    exit 0
fi
