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

split_debug() {
	local tmp="$(mktemp)"	# intermediate file
	pushd $root

	echo_status "Splitting debug information ..."

	# guard against already existing debug files
	grep -v "$SDECFG_DEBUG_PATH/.*[.]debug$" $builddir/flist.txt |
	while read f; do
		if [[ $SDECFG_DEBUG_PATH = /* ]]; then
			local dbg="$root$SDECFG_DEBUG_PATH/$f.debug"
		else
			local dbg="$root/${f%/*}/$SDECFG_DEBUG_PATH/${f##*/}.debug"
		fi

		# skip if not an object file
		$OBJCOPY --only-keep-debug "$f" "$tmp" 2>/dev/null || continue

		# create the debug destination dir
		mkdir -p ${dbg%/*}
		mv "$tmp" "$dbg"
		$OBJCOPY --strip-debug "$f"
		$OBJCOPY --add-gnu-debuglink="$dbg" "$f"
		add_flist "$dbg"
	done

	rm $tmp
	popd
}

if [ "$SDECFG_DEBUG" = 2 ]; then
	hook_add postflist 5 split_debug
fi
