# --- T2-COPYRIGHT-NOTE-BEGIN ---
# This copyright note is auto-generated by ./scripts/Create-CopyPatch.
# 
# T2 SDE: package/.../ccache/parse-config
# Copyright (C) 2004 - 2005 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.
# --- T2-COPYRIGHT-NOTE-END ---

if [ "$SDECFG_PKG_CCACHE_USEIT" = 1 -a "`type -p ccache`" -a "$pkg" != gcc ]
then
	unset ${!CCACHE_*}

	# wrap the sytem compiler
	ccache_dir=ccache
	if atstage toolchain; then
		x=`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"
	# we need to know if atime tests work or not
	echo > $CCACHE_DIR/.timestamp
	echo > $CCACHE_DIR/.timestamp-reference
	ccache -M $SDECFG_PKG_CCACHE_MAX > /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; }' )"
	echo_status "Compiler Cache enabled: $x% 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; }' )"
		echo_status "Compiler Cache Hits while pkg build: $x% (`
			`$ccache_hit hits, $ccache_miss misses)"

		# 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 ...
		#
		echo_status "Cleaning up ccache ..."
		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
		else
		echo_warning "No atime support available, Compiler Cache clean up not possible."
		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

