# --- SDE-COPYRIGHT-NOTE-BEGIN ---
# This copyright note is auto-generated by ./scripts/Create-CopyPatch.
#
# Filename: package/.../ccache/parse-config
# Copyright (C) 2006 - 2007 The OpenSDE Project
# Copyright (C) 2004 - 2006 The T2 SDE Project
#
# More information can be found in the files COPYING and README.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; version 2 of the License. A copy of the
# GNU General Public License can be found in the file COPYING.
# --- SDE-COPYRIGHT-NOTE-END ---

if [ "$SDECFG_PKG_CCACHE_USEIT" != 1 ]; then
	true	# skip
elif [ -z "`type -p ccache`" ]; then
	echo_warning 'Compiler cache disabled! (ccache not available)'
elif [ "$pkg" == gcc ]; then
	echo_warning 'Compiler cache disabled! (package incompatible)'
elif [ "$SDECFG_DEBUG" == 1 ]; then
	echo_warning 'Compiler cache disabled! (package build with debug symbols)'
else
	unset ${!CCACHE_*}

	# wrap the sytem compiler
	ccache_dir=ccache
	if atstage toolchain; then
		x=`/bin/uname -m | uname2arch`
		var_append ccache_dir - $x
	fi

	var_append ccache_dir - $arch

	# do not trash the cache with feature-less built packages
	[ $stagelevel -le 2 ] && var_append ccache_dir - $stagelevel

	export CCACHE_DIR="$base/build/$ccache_dir/$pkg"
	export CCACHE_DONT_STAT_COMPILER=1
	export CCACHE_UNIFY=1 # needed to e.g. strip comments
	#export CCACHE_LOGFILE="$CCACHE_DIR.log"

	# In the chroot sandbox we need to wire the cccache-$arch out by using
	# the existing bind mount. We only do this, if we are not doing a
	# manual stage9 build ...
	if atstage native && [ "$ROCK_THIS_IS_CHROOT" ]; then
		mkdir -p $base/loop/build/$ccache_dir
		ln -snf ../loop/build/$ccache_dir $base/build/$ccache_dir
	fi

	# we instruct the flist wrapper to filter out ccache access, to
	# further reduce flist post processing time for big packages
	var_append FLWRAPPER_FILTERDIR ':' "$CCACHE_DIR"

	mkdir -p "$CCACHE_DIR"

	if [ "$SDECFG_PKG_CCACHE_CLEANUP" != 0 ]; then
		# we need to know if atime tests work or not
		echo > $CCACHE_DIR/.timestamp
		echo > $CCACHE_DIR/.timestamp-reference
	fi

	ccache -M ${SDECFG_PKG_CCACHE_MAX:-1000M} > /dev/null

	# just using the CC_WRAPPER would not wrap KCC ... - thus we use GCC for C and C++
	# and KCC for the kernel builds ...
	if ! atstage toolchain; then
		var_insert KCC_WRAPPER_OTHERS ":" "ccache"
		var_insert GCC_WRAPPER_OTHERS ":" "ccache"
	else
		var_insert SYSCC_WRAPPER_OTHERS ":" "ccache"
	fi

	eval "$( ccache -s | gawk '/cache hit/ { hit=$3; }
			/cache miss/ { miss=$3; }
			END { if (hit+miss == 0) print "x=0.00";
			else printf "x=%.2f\n", hit*100 / (hit+miss);
			print "ccache_hit=" hit "; ccache_miss=" miss; }' )"
	y=$( du -sh "$CCACHE_DIR" | cut -f1 )
	echo_status "Compiler Cache enabled: $x% ($y) cache hits so far"

	ccache_print_status() {
		eval "$( ccache -s | gawk '/cache hit/ { hit=$3-'$ccache_hit'; }
			/cache miss/ { miss=$3-'$ccache_miss'; }
			END { if (hit+miss == 0) print "x=0.00";
			else printf "x=%.2f\n", hit*100 / (hit+miss);
			print "ccache_hit=" hit "; ccache_miss=" miss; }' )"
		y=$( du -sh "$CCACHE_DIR" | cut -f1 )
		echo_status "Compiler Cache Hits while pkg build: $x% (`
			`$ccache_hit hits, $ccache_miss misses, $y)"

		if [ "$SDECFG_PKG_CCACHE_CLEANUP" != 0 -a -f "$root/var/adm/logs/$stagelevel-$xpkg.log" ]; then
			# test if fs supports atime
			cat $CCACHE_DIR/.timestamp > /dev/null
			if [ "$( find $CCACHE_DIR/.timestamp -anewer $CCACHE_DIR/.timestamp-reference )" ]
			then
				# remove non-hits from the ccache cache ...
				#
				find $CCACHE_DIR -type f ! -anewer $CCACHE_DIR/.timestamp-reference -a ! -name '*.stderr' |
				while read fn ;do
					rm -f $fn{,.stderr} 2>/dev/null
				done
				echo_status "Compiler Cache reduced to $( du -sh "$CCACHE_DIR" | cut -f1 )."
			else
				echo_warning "No atime support available, Compiler Cache clean up not possible."
			fi
		fi
	}

	hook_add finish 9 ccache_print_status

	# last but not least make sure the package does not depend on ccache
	var_append flistrfilter "|" ".*bin/ccache$"
fi