#!/bin/bash
# BEGIN_ICS_COPYRIGHT8 ****************************************
# 
# Copyright (c) 2015, Intel Corporation
# 
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
# 
#     * Redistributions of source code must retain the above copyright notice,
#       this list of conditions and the following disclaimer.
#     * Redistributions in binary form must reproduce the above copyright
#       notice, this list of conditions and the following disclaimer in the
#       documentation and/or other materials provided with the distribution.
#     * Neither the name of Intel Corporation nor the names of its contributors
#       may be used to endorse or promote products derived from this software
#       without specific prior written permission.
# 
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
# 
# END_ICS_COPYRIGHT8   ****************************************
##
## runmake
## -----------
## Perform a full build
## This is used by the weeklybuild script to provide product specific
## build operations for product's whose build is more complex
## than simply a "make stage prepfiles"
## When this is invoked, the current directory will be the TL_DIR for a
## full checkout of the product to be built
##
## Usage:
##	runmake [-B 'build_config ...'] [-V 'os_version os_version ...']
##			[-f options_file] [-T PROJ_FILE_DIR]
##			or
##	runmake -r [-iCRP] [-B 'build_config ...'] [-V 'os_version os_version ...']
##			[-f options_file] [-T PROJ_FILE_DIR]
##
## Arguments:
##	-f options_file - file to use prior	to BuildOptons
##	-B build_config	release types to build, (debug/release). default is
##		based on $BUILD_TARGET_OS/$BUILD_TARGET.BuildOptions
##		or $BUILD_TARGET_OS/$BUILD_TARGET.Supported
##	-V os_version	operating system versions to build for, default is
##		based on $BUILD_TARGET_OS/$BUILD_TARGET.BuildOptions
##		or $BUILD_TARGET_OS/$BUILD_TARGET.Supported
##		if more than 1 version is specified, must be separated with spaces
##		and quoted
##		This should be a version string such as 2.4.7-10
##	-r 		rerun the build in place without doing a new checkout/tag
##	-i 		allow incremental builds by building in-place before package rpm
##	-R 		skip building rpm, just leave in-place files
##	-C		don't clobber prior to the rerun
##	-P		skip building and packaging IntelOPA-Tests
##	-O		Override to build despite kernel install not matching
##			Config file
##	-n		No build.  Useful for checking build machine
##	-T PROJ_FILE_DIR - PROJ_FILE_DIR value to use, useful when doing a build
##		within an ALL checkout
##
## In addition, the following environment variables are expected to be set:
##	RELEASE_TAG, BRANCH_TAG, BUILD_TARGET, PRODUCT, RELEASE_HOME, BUILD_CONFIG 
##	The target command is expected to have already been run to set up the
##	environment for the specified target
##
##	re-run can be used for 2 situations:
##	1. re-run a failed build to correct incorrect weeklybuild arguments
##		or other non-source code related problems (out of disk space)
##	2. re-run a build for a different BUILD_TARGET_OS_VERSION while using the same
##		source code image/tag
##	If the source code needs to be corrected, a full weeklybuild should be run
##	after the correction such that the code is properly tagged.
##
## For developer builds (such as via dobuildall), the following combinations
## of the -r, -i, -C and -R options are useful
##	-r  - existing typical mode for developer builds
##			- clobbers any existing built files
##			- creates a src.rpm
##			- builds in rpmbuild tree
##			- creates a resulting binary rpm
##			- IntelOpa-Tests built in a tests_build area
##	-r -C - for opa FF & mpi_apps, behaves same as -r above except skips clobber
##			- IntelOpa-Tests tree is not cleaned, so will do incremental build
##	-r -i [-C] - incremental build mode for developer builds
##			- clobbers any existing built files (unless -C specified)
##			- builds FF code in place in source tree
##			- src rpm contains pre-built code
##			- rpmbuild ends up using pre-built code, creates binary rpm
##			- IntelOpa-Tests built in a tests_build area using
##				pre-built code from FF and previous tests build
##			After this build, the developer may do incremental sub-tree builds
##			using . OpenIb_Hosts/setenv and then running make in the subtree
##			For subtree builds in tests_build, also export PROJ_FILE_DIR=.
##	-r -i [-C] -R - incremental build mode for developer builds with no rpms
##			- same as -r -i [-C] except no rpms are created
##	none of these options - typical production build
##			- expects a fresh un-built checkout of code tree
##			- creates a src.rpm
##			- builds in rpmbuild tree
##			- creates a resulting binary rpm
##			- IntelOpa-Tests built in a tests_build area
##	if developer does not need an updated build of IntelOPA-Tests, the -P option
##	may be used to further speed up the build
##
## Recommended developer sequence of builds when debugging new code
## -r -i -C -R - performs the fastest full build possible, generates no rpms
##			It is recommended to omit the -C option on 1st build so the tree
##			is clobbered to be sure you start fresh.
##			iterate on this and code changes until the basic build works
##			and syntax errors and warnings are all resolved
##			For unit tests only needing a few binaries, they can also be
##			directly tested.
##			If IntelOPA-Tests is not needed also use -P option
##			after initial attempt, if compile errors are limited to a subtree:
##				create a new bash shell, use . ./OpenIb_Host/setenv and then
##				iterate on make in the subtree and code changes until compiles
## -r -i -C - performs a build to generate rpms
##			install the rpms and continue testing
##			if focused on a few executables,:
##				create a new bash shell, use . ./OpenIb_Host/setenv and then
##				iterate on make in the subtree and code changes and test of
##				the individual binary until works properly
## -r 		- perform a full build and generate rpms
##			install the rpms, confirm clean build, proper install and function
##
##	Since weeklybuild and target commands always set BUILD_CONFIG, this
##	script ignores BUILD_CONFIG.  As such the BuildOptions file
##	or the -B argument completely control BUILD_CONFIG for this product.

. $ICSBIN/funcs.sh

# a given release will be targeted for a specific version of ofed
# however we allow build.config to override if needed
export OFED_STACK_PREFIX=${OFED_STACK_PREFIX:-/usr/ofed-1.5.2}
export BUILD_TARGET=${BUILD_TARGET:-X86_64}

Usage()
{
	# include "ERROR" in message so weeklybuild catches it in error log
	echo "ERROR: runmake failed" >&2
	echo "Usage: runmake [-B 'build_config ...'] [-V 'os_version os_version ...']" >&2
 	echo "               [-f options_file] [-T PROJ_FILE_DIR]" >&2
	echo "            OR" >&2
	echo "       runmake -r [-iCRP] [-B 'build_config ...'] [-V 'os_version os_version ...']" >&2
 	echo "               [-f options_file] [-T PROJ_FILE_DIR]" >&2
	exit 2
}

get_option()
{
	# determine the given option line in BuildOptions
	# if not found in BuildOptions, use Supported
	# $1 = option name
	# $2 = default value if not specified in files
	value=
	if [ $fflag = y ]
	then
		value=`grep "^$1" $build_options 2>/dev/null|cut -f2 -d:`
	fi
	if [ -z "$value" -a -f $TL_DIR/$(basename $PWD)/$BUILD_TARGET_OS/${BUILD_TARGET}.BuildOptions ]
	then
		value=`grep "^$1:" $TL_DIR/$(basename $PWD)/$BUILD_TARGET_OS/${BUILD_TARGET}.BuildOptions 2>/dev/null|cut -f2 -d:`
	fi
	if [ -z "$value" -a -f $TL_DIR/$(basename $PWD)/$BUILD_TARGET_OS/${BUILD_TARGET}.Supported ]
	then
		value=`grep "^$1:" $TL_DIR/$(basename $PWD)/$BUILD_TARGET_OS/${BUILD_TARGET}.Supported 2>/dev/null|cut -f2 -d:`	
	fi
	if [ -z "$value" ]
	then
		value="$2"
	fi
	echo "$value"
	return 0
}

setbuildconfig()
{
	# determine appropriate default build config

	if [ -z "$build_configs" ]
	then
		#if [ ! -z "$BUILD_CONFIG" ]
		#then
		#	# weeklybuild environment can override defaults
		#	export build_configs="$BUILD_CONFIG"
		#else
		build_configs=$(get_option BUILD_CONFIG debug)
	fi
}

# TBD - dead code, this and -O flag are not used
setosversion()
{
	# determine appropriate default os version(s)

	if [ -z "$os_versions" ]
	then
		os_vendor_ver=`echo $BUILD_PLATFORM_OS_VENDOR_VERSION | tr '.' '_'`
		os_tmp=$(get_option BUILD_TARGET_OS_VERSION_$BUILD_TARGET_OS_VENDOR$os_vendor_ver "")
		if [ "$BUILD_PLATFORM_OS_VENDOR" == apple ]
		then
			os_versions="$os_tmp"
		else
			for rel in $os_tmp
			do
				if [ -d /lib/modules/$rel/build ]
				then
					os_versions="$os_versions $rel"
				else
					if [ -z "$Oflag" ]
					then
						echo "ERROR: kernel for $rel not installed"
						exit 1
					fi
				fi
			done
		fi
	fi
}

build_configs=
os_versions=
rerun=n
Cflag=n
Bflag=n
Vflag=n
fflag=n
nflag=n
inplace=n
rpm=y
buildtests=y
build_options=
proj_file_dir=OpenIb_Host
while getopts f:B:V:riRPCOnT: param
do
	case $param in
	V)
		Vflag=y
                os_versions="`echo $OPTARG | sed 's/,/ /g'`";;
	B)
		Bflag=y
		build_configs="$OPTARG";;
	f)
		fflag=y
		build_options="$OPTARG";;
	r)
		rerun=y;;
	C)
		Cflag=y;;
	i)
		inplace=y;;
	R)
		rpm=n;;
	P)
		buildtests=n;;
	O)
		Oflag=y;;
	n)
		nflag=y;;
	T)	proj_file_dir="$OPTARG";;
	?)
		Usage
	esac
done
shift $(($OPTIND -1))

if [ $# != 0 ]
then
		Usage
fi

if [ -z "$RELEASE_TAG" ]
then
	export RELEASE_TAG=`patch_engineer_version|cut -d"." -f1`
	echo "Defaulting RELEASE_TAG to $RELEASE_TAG"
	# engineers will always forget this flag, since clobber is fast
	# default to a rerun which will clobber first unless -C specified
	rerun=y
fi

# these are special cases
if [ "$SUBPRODUCT" = "GPL" ]
then
	echo "ERROR: GPL build not supported for OPENIB_HOST at this time" >&2
	exit 1
	dir=$PWD
	cd ..
	rm -rf C_* SH_* GPL_* LGPL_* InfiniSer*
	$dir/MakeTools/gpl_source.sh $RELEASE_TAG
	# gpl_source script produces packaged_files
	cp $dir/packaged_files $dir/dist_files
	exit $?
elif [ "$SUBPRODUCT" = "SRC" ]
then
	COPYRIGHT_NOTICE=${COPYRIGHT_NOTICE:-CodeTemplates/copyright.txt}
	insert_notice $COPYRIGHT_NOTICE
	dir=$PWD
	( cd ..; tar cvfz InfiniServFFSrc.$RELEASE_TAG.tgz --exclude CVS --exclude '*.bak' `basename $dir`; [ $? = 0 ] || echo "ERROR: tar command failed" )
	echo "../InfiniServFFSrc.$RELEASE_TAG.tgz" > packaged_files
	echo "../InfiniServFFSrc.$RELEASE_TAG.tgz" > dist_files
	exit $?
elif [ "$SUBPRODUCT" = "TESTS" ]
then
	settl
	PRODUCT_DIR=$PWD
	cd $TL_DIR
	dir=release.$PRODUCT.$SUBPRODUCT
	mkdir $dir
	cp -R HostTestCases $dir/HostTests
	cp -R Makerules MakeTemplates MakeTools TestTools TestOnly CodeTemplates $dir/HostTests
	( cd $dir; tar cvfz IntelOPA-IntTests.$RELEASE_TAG.tgz --exclude CVS --exclude '*.bak' HostTests; [ $? = 0 ] || echo "ERROR: tar command failed" )
	echo "$TL_DIR/$dir/IntelOPA-IntTests.$RELEASE_TAG.tgz" > $PRODUCT_DIR/packaged_files
	# this should not be distributed, so create an empty dist_files
	> $PRODUCT_DIR/dist_files
	exit $?
elif [ "$SUBPRODUCT" = "OPEN12200" ]
then
	echo "ERROR: OPEN12200 build no longer supported for OPENIB_HOST" >&2
	exit 1
elif [ "$SUBPRODUCT" = "MCLX" ]
then
	export BUILD_BRAND="Mercury Computer Systems, Inc."
fi

resetct
resetbsp
if [ "$BUILD_TARGET_TOOLCHAIN" == "INTEL" ]
then
	target $BUILD_TARGET_TOOLCHAIN
else
	target $BUILD_TARGET
fi

# determine the correct default build config(s) if any
setbuildconfig

echo "---------------------------------------"
showenv
echo "---------------------------------------"

# the kernel rev is not important.  We simply use the kernel rev
# of the running kernel.  While BUILD_TARGET_OS_VERSION is needed by Makerules
# it will have no impact on what is actually built
export BUILD_TARGET_OS_VERSION=${BUILD_TARGET_OS_VERSION:-`uname -r`}
setver $BUILD_TARGET_OS_VENDOR $BUILD_TARGET_OS_VERSION

if [ -z "$build_configs" ]
then
	# default to release build
	export BUILD_CONFIG=release
elif [ "$build_configs" != debug -a "$build_configs" != release ]
then
	Usage
else
	export BUILD_CONFIG="$build_configs"
fi

export PROJ_FILE_DIR=$proj_file_dir
echo "Using PROJ_FILE_DIR=$PROJ_FILE_DIR"

if [ -z "$build_configs" ]
then
	# default to release build
	#setrel
	echo "$0: ERROR: Unable to determine BUILD_CONFIGs to build" >&2
	exit 1
fi
#if [ -z "$os_versions" ]
#then
#	echo "$0: ERROR: Unable to determine os version to build" >&2
#	exit 1
#fi
if [ ! -d $OFED_STACK_PREFIX ]
then
	echo "$0: ERROR: OFED stack not found: $OFED_STACK_PREFIX" >&2
	exit 1
fi
echo "Building for OFED: $OFED_STACK_PREFIX" >&2

if [ "$nflag" = "y" ]
then
	exit 0
fi

if [ -z "$RELEASE_TAG" ]
then
	export MODULEVERSION=`patch_engineer_version`
	export RPM_VER=`echo $MODULEVERSION|cut -d"." -f1-2` 
	export RPM_REL=`echo $MODULEVERSION.999.999|cut -d"." -f3`
else
	export MODULEVERSION=`patch_version $RELEASE_TAG`
	export RPM_VER=`echo $MODULEVERSION|cut -d"." -f1-4`
	export RPM_REL=`echo $MODULEVERSION.999.999.999.999|cut -d"." -f5`
fi
export MKRPM_VER=$RPM_VER
export MKRPM_REL=$RPM_REL`rpm --eval %{?dist}`

# even if building rpms, clobber to cleanup any previous in-place builds
# so they don't end up inside the src.rpm when not wanted
# clean up from prior build when rebuilding
if [ "$rerun" = y -a "$Cflag" != y ]
then
	# force full rebuild
	# this clobber is only necessary if we have ever built inplace
	# otherwise each rpm build will get a fresh workplace for its build
	# so to save some time, only do the clobber if needed
	for build_config in $build_configs
	do
		export BUILD_CONFIG=$build_config
		#if [ -e $TL_DIR/builtinplace.$PRODUCT.$BUILD_CONFIG \
		#	-o  -e $TL_DIR/builtinclude.$PRODUCT.$BUILD_CONFIG \
		#	-o  -e $TL_DIR/builtlibs.$PRODUCT.$BUILD_CONFIG \
		#	-o  -e $TL_DIR/builtbin.$PRODUCT.$BUILD_CONFIG ]
		#then
			export REMOVE_DEPENDS=yes
			make clobber clobber_stage clobber_release
			rm -f $TL_DIR/builtinplace.$PRODUCT.$BUILD_CONFIG
		#fi
	done
fi
clobber_arg=

rm -rf $TL_DIR/$PROJ_FILE_DIR/packaged_files $TL_DIR/$PROJ_FILE_DIR/dist_files

args=
if [ "$inplace" = y ]
then
	echo "Building $build_configs for allkernels for $BUILD_TARGET_OS $BUILD_TARGET ..."
	echo "==============================================================================="
	for build_config in $build_configs
	do
		# setup environment using BUILD_CONFIG
		export BUILD_CONFIG=$build_config

		#for os_version in $os_versions
		#do
			#setver $BUILD_TARGET_OS_VENDOR $os_version
			echo > $TL_DIR/builtinplace.$PRODUCT.$BUILD_CONFIG
			./rpm_runmake -r -C -B $BUILD_CONFIG
		#done
	done
	args="-r -C"	# we pre-built, so skip clobber in rpmbuild
fi

if [ "$BUILD_TARGET_OS" != "VXWORKS" -a "$rpm" = y ]
then
	echo "# Adjust the environment variables if necessary" > build.env
	echo "export PRODUCT=${PRODUCT:-OPENIB_FF}" >> build.env
	echo "export RELEASE_TAG=${RELEASE_TAG}" >> build.env
	echo "export BUILD_CONFIG=\${BUILD_CONFIG:-\"${BUILD_CONFIG}\"}" >> build.env
	echo "export BUILD_WITH_STACK=${BUILD_WITH_STACK}" >> build.env
	echo "export MODULEVERSION=${MODULEVERSION}" >> build.env

	./package_script.sh $args

	make final_package
fi

if [ "$BUILD_TARGET_OS" != "VXWORKS" -a "$buildtests" = y ]
then
	echo "Building IntelOPA-Tests $build_configs for allkernels for $BUILD_TARGET_OS $BUILD_TARGET ..."
	echo "==============================================================================="
	./build_tests.sh $args
	echo "Done IntelOPA-Tests $BUILD_TARGET_OS $BUILD_TARGET allkernels $(date)"
	echo "==============================================================================="
fi
exit 0
