#!/bin/sh

. config/path

KCONFIG=build/config/Kconfig.arch

cat > $KCONFIG <<EOF
############################################
# This file is autogenerated, do not edit! #
############################################

#######################
# Target architecture #
#######################
choice
  prompt "Target architecture"
  default TARGET_ARCH_i386
  help
    Target system we want to cross compile for

EOF

for i in config/platforms/*; do
  arch=`basename $i`
  archs="$archs $arch"
  unset ARCH_NAME
  unset ARCH_DESC
  unset ARCH_HELP
  . config/platforms/$arch/meta
  [ -z "$ARCH_DESC" ] && ARCH_DESC="$ARCH_NAME"
  cat >> $KCONFIG <<EOF
config TARGET_ARCH_$arch
  bool "$ARCH_DESC"
  help
    $ARCH_HELP

EOF
done

cat >> $KCONFIG <<EOF
endchoice

config OPT_TARGET_ARCH
  string
EOF

for arch in $archs; do
  echo "  default \"$arch\" if TARGET_ARCH_$arch" >> $KCONFIG
done

cat >> $KCONFIG <<EOF

#######################
# Target sub-arch     #
#######################
choice
  prompt "Target sub-architecture"
  default TARGET_SUBARCH_i586 if TARGET_ARCH_i386
  default TARGET_SUBARCH_armv7 if TARGET_ARCH_arm
  help
    Target subsystem we want to cross compile for

EOF

for arch in $archs; do
  unset ARCH_NAME
  unset ARCH_SUBARCHS
  . config/platforms/$arch/meta
  [ -z "$ARCH_SUBARCHS" ] && ARCH_SUBARCHS=$arch
  for subarch in $ARCH_SUBARCHS; do
    subarchs="$subarchs $subarch"
    cat >> $KCONFIG <<EOF
config TARGET_SUBARCH_$subarch
  bool "$subarch"
  depends on TARGET_ARCH_$arch
EOF
    desc=`valueof ARCH_DESC_$subarch`
    [ -n "$desc" ] && cat >> $KCONFIG <<EOF
  help
   $desc

EOF
  done
done
cat >> $KCONFIG <<EOF
endchoice

config OPT_TARGET_SUBARCH
  string
EOF

for subarch in $subarchs; do
  echo "  default \"$subarch\" if TARGET_SUBARCH_$subarch" >> $KCONFIG
done

cat >> $KCONFIG <<EOF

#######################
# Target platform     #
#######################
source "../../config/Kconfig.platform"

#######################
# Target machine      #
#######################
source "../../config/Kconfig.machine"

#######################
# Target CPU          #
#######################
choice TARGET_CPU
  prompt "Target CPU"
  default TARGET_CPU_generic if (TARGET_ARCH_i386 || TARGET_ARCH_x86_64)
  default TARGET_CPU_powerpc if TARGET_ARCH_powerpc
  default TARGET_CPU_powerpc64 if TARGET_ARCH_powerpc64
  default TARGET_CPU_arm926ej-s if TARGET_SUBARCH_armv5
  default TARGET_CPU_arm1136j-s if TARGET_SUBARCH_armv6
  default TARGET_CPU_cortex-a8 if TARGET_SUBARCH_armv7
  default TARGET_CPU_mips32 if TARGET_ARCH_mips
  help
    This option controls which processor should be targeted for the generated
    code.

EOF

for arch in $archs; do
  unset ARCH_NAME
  unset ARCH_SUBARCHS
  unset ARCH_CPUS
  . config/platforms/$arch/meta
  for cpu in $ARCH_CPUS; do
    cpus="$cpus $cpu"
    cat >> $KCONFIG <<EOF
config TARGET_CPU_$cpu
  bool "$cpu"
  depends on TARGET_ARCH_$arch
EOF
  done
  for subarch in $ARCH_SUBARCHS; do
    subarch_cpus="`valueof ARCH_CPUS_$subarch`"
    for cpu in $subarch_cpus; do
           cpus="$cpus $cpu"
       cat >> $KCONFIG <<EOF
config TARGET_CPU_$cpu
  bool "$cpu"
  depends on TARGET_SUBARCH_$subarch
EOF
    done
  done
done

cat >> $KCONFIG <<EOF
endchoice

config OPT_TARGET_CPU
  string
EOF

for cpu in $cpus; do
  echo "  default \"$cpu\" if TARGET_CPU_$cpu" >> $KCONFIG
done
