#!/bin/bash
# BEGIN_ICS_COPYRIGHT8 ****************************************
# 
# Copyright (c) 2015-2017, 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   ****************************************
##
## rpm_runmake
## -----------
## Perform a full build
## This is used within the FF src rpm by the ff_build.sh script
## and for in-place builds its invoked directly by runmake
## When this is invoked, the current directory will be the TL_DIR for
## the source tree of FF
##
## Usage:
##	rpm_runmake [-B 'build_config'] [-n]
##			or
##	rpm_runmake -r [-B 'build_config'] [-C] [-n]
##
## Arguments:
##	-B build_config	release type to build, (debug/release). default is release
##	-r 		rerun the build in place without doing a new checkout/tag
##	-C		don't clobber prior to the rerun
##	-n		No build.  Useful for checking build machine
##
## 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.
##
##	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.

# 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}

Usage()
{
	# include "ERROR" in message so weeklybuild catches it in error log
	echo "ERROR: rpm_runmake failed" >&2
	echo "Usage: rpm_runmake [-B 'build_config'] [-n]" >&2
	echo "            OR" >&2
	echo "       rpm_runmake -r [-B 'build_config'] [-C] [-n]" >&2
	exit 2
}

showbuild()
{
	# output summary of what we are building to stdout
	echo "Building for $SUBPRODUCT $BUILD_TARGET $BUILD_CONFIG $(date)..."
	#echo "Building for $BUILD_TARGET_OS $BUILD_TARGET $BUILD_TARGET_OS_VERSION $BUILD_CONFIG $(date)..."
}

build_config=
rerun=n
Cflag=n
while getopts B:rCn param
do
	case $param in
	r)
		rerun=y;;
	B)
		build_config="$OPTARG";;
	C)
		Cflag=y;;
	n)
		nflag=y;;
	?)
		Usage
	esac
done
shift $(($OPTIND -1))

if [ $# != 0 ]
then
		Usage
fi

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

export PROJ_FILE_DIR=OpenIb_Host

echo "Stack to build for: $BUILD_WITH_STACK"

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

# when inplace developer builds have been done, the src rpm will include
# prebuilt code.  if the -C option is not specified (via BUILD_ARGS)
# clobber any such files if they are present (indicated by builtinplace file).
# The clobber allows a src.rpm packaged with an in-place build to still
# generate normally when -C not given.
# No need to check rerun flag because for rpms we always start fresh from what
# was inside the src rpm and for runmake we can test built* to decide if the
# clobber is needed
clobber_arg=
if [ "$Cflag" != y ]
then
	# force full rebuild
	if [ -e $TL_DIR/builtinplace$BUILT_SUFFIX \
		-o  -e $TL_DIR/builtinclude$BUILT_SUFFIX \
		-o  -e $TL_DIR/builtlibs$BUILT_SUFFIX \
		-o  -e $TL_DIR/builtbin$BUILT_SUFFIX ]
	then
		export REMOVE_DEPENDS=yes
		clobber_arg="clobber clobber_stage clobber_release"
	fi
fi
rm -rf packaged_files dist_files

# export DATE for use by prep so all files get same date/time stamp
DATE=${DATE:-"`date +'%m/%d/%y %H:%M'`"}

# Do the actual MAKE
showbuild
env
make $clobber_arg stage
echo "Done $BUILD_TARGET_OS $BUILD_TARGET Fast Fabric $SUBPRODUCT $(date)"
echo "=============================================================="

echo "Preping Files for $BUILD_TARGET_OS $BUILD_TARGET $(date)..."
# run prep and package once at end
# TBD - move package into outer final_package step and remove from here
# consolidate Makefile accordingly to have one package target
make prepfiles package
