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

# Note: this is a copy of sparc64/parse-config

alter_arch() {
	echo_status "Altering arch_{machine,target} to powerpc ..."
	# tweak target and other defines
	atstage native && arch_build=${arch_build/64/}
	arch_machine=${arch_machine/64/}
	arch_target=${arch_target/64/}
}

powerpc64_build32() {
    # fork, make a copy of all variables, so we fully restore to normal
    (
	alter_arch
	arch_sizeof_long=4 arch_sizeof_char_p=4
	var_append GCC_WRAPPER_INSERT ' ' '-m32'
	var_append CPP_WRAPPER_INSERT ' ' '-m32'
	function set_confopt () { : ; } # do not reset the confopt, ...
	build_package $pkg $pkg.conf
    )

    # let it continue to build just normal, as if "nothing" happened
    make distclean
}

# default to 32-bit?
if [ "$SDECFG_POWERPC64_32" = 1 ]; then
    # dietlibc?
    x="${pkg//-/_}" ; x="${x//+/_}"
    eval x=\$SDECFG_DIETLIBC_$x
    if [ $pkg = "dietlibc" -o "$x" = 1 ]; then
        echo_status "Building package for 64bit ..."
        var_append GCC_WRAPPER_INSERT ' ' '-m64'
    else
      case $pkg in
	glibc32|linux-header|linux|kmod|module-init-tools|yaboot|grub2)
		# usually nothing, except:
		[ $pkg = kmod ] && hook_add preconf 5 'libdir=${libdir%64}'
		;;
	glibc)
		# as we otherwise default to 32-bit
		var_append GCC_WRAPPER_INSERT ' ' '-m64'
		;;
	binutils|gcc|gdb|gcc-spe)
		#alter_arch # to 32-bit "powerpc"
		#arch_target=${arch_target/powerpc/powerpc64}
		;;
	*)
		alter_arch # to 32-bit "powerpc"
		;;
      esac
    fi
elif [ "$SDECFG_MULTILIB" = 1 ]; then
    case $pkg in
	util-linux|e2fsprogs)
		# we need a 32bit lib for yaboot :-(
		pkginstalled yaboot && hook_add preconf 1 "powerpc64_build32"
		;;
    esac
fi
