#!/bin/bash
# Copyright (c) 2000-2020 Synology Inc. All rights reserved.

if ! checkPlatformFunction SYNO_FEA_SUPPORT_FLASHCACHE; then
	SkipThisProject
	return
fi

. ${ScriptsDir}/include/exports

case ${MakeClean} in
	[Yy][Ee][Ss])
		make $MAKE_FLAGS KERNEL_TREE="$KSRC" clean
		;;
esac

case ${CleanOnly} in
	[Yy][Ee][Ss])
		return
		;;
esac

build_log=./flashcache-build.log

# Note: install script can't get SYNO_KERNEL_VERSION, just pass it from here
if [ "$SYNO_KERNEL_VERSION" = "linux-4.4.x" -o "$SYNO_KERNEL_VERSION" = "linux-3.10.x" ] ; then
	Support4KCache=1
else
	Support4KCache=0
fi

env CC=${CC} AR="${AR}" RANLIB=${RANLIB} STRIP=${STRIP} \
make $MAKE_FLAGS KERNEL_TREE="$KSRC" SUPPORT_4K_CACHE="$Support4KCache" 2>&1 | tee $build_log

# Check for "WARNING: symbol undefined!" messages to avoid write a
# function prototype in header but not implement it
if grep "WARNING:.* undefined!" $build_log ; then
	echo "Error: Get WARNING messages"
else
	rm -f $build_log
fi

if test "${BUILD_ARCH}" = "32" ; then
	# For 32 bit platform
	if find . -name '*.ko' | xargs ${ToolChainPrefix}objdump -DS | grep -q "__aeabi_uldivmod"; then
		echo -e "Error: 64 bit division/modulo check FAILED: " \
			"There are 64 bit division or modulo in kernel module"\
			"which will cause module insertion fail in 32 bit platform."
	fi
fi
