#!/bin/sh

################################################################################
#      This file is part of OpenELEC - http://www.openelec.tv
#      Copyright (C) 2009-2012 Stephan Raue (stephan@openelec.tv)
#
#  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; either version 2, or (at your option)
#  any later version.
#
#  This Program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
#  GNU General Public License for more details.
#
#  You should have received a copy of the GNU General Public License
#  along with OpenELEC.tv; see the file COPYING.  If not, write to
#  the Free Software Foundation, 51 Franklin Street, Suite 500, Boston, MA 02110, USA.
#  http://www.gnu.org/copyleft/gpl.html
################################################################################

. config/options $1

# Fails to compile with GCC's link time optimization.
  strip_lto

# Filter out some problematic *FLAGS
  CFLAGS=`echo $CFLAGS | sed -e "s|-ffast-math||g"`
  CFLAGS=`echo $CFLAGS | sed -e "s|-Ofast|-O2|g"`
  CFLAGS=`echo $CFLAGS | sed -e "s|-O.|-O2|g"`

  LDFLAGS=`echo $LDFLAGS | sed -e "s|-ffast-math||g"`
  LDFLAGS=`echo $LDFLAGS | sed -e "s|-Ofast|-O2|g"`
  LDFLAGS=`echo $LDFLAGS | sed -e "s|-O.|-O2|g"`

  LDFLAGS=`echo $LDFLAGS | sed -e "s|-Wl,--as-needed||"`

  unset LD_LIBRARY_PATH

# set some CFLAGS we need
  CFLAGS="$CFLAGS -g -fno-stack-protector -fgnu89-inline"

# dont build parallel
  MAKEFLAGS=-j1

export BUILD_CC=$HOST_CC
export OBJDUMP_FOR_HOST=objdump

if [ "$DEBUG" = yes ]; then
  DEBUG_CONFIG="--enable-debug"
else
  DEBUG_CONFIG="--disable-debug"
  DEBUG_OPTIONS="  OPTION_EGLIBC_MEMUSAGE = n"
fi

NSS_CONF_DIR=`ls -d $ROOT/$BUILD/eglibc*/nss`

cd $PKG_BUILD

autoconf

mkdir -p objdir-$1 && cd objdir-$1

# OPTION_EGLIBC_NSSWITCH_FIXED_CONFIG = $NSS_CONF_DIR/fixed-nsswitch.conf
# OPTION_EGLIBC_NSSWITCH_FIXED_FUNCTIONS = $NSS_CONF_DIR/fixed-nsswitch.functions

cat >option-groups.config <<EOF

  OPTION_EGLIBC_ADVANCED_INET6 = n

# needed for connman:
  OPTION_EGLIBC_BACKTRACE = y

  OPTION_EGLIBC_BIG_MACROS = n
  OPTION_EGLIBC_BSD = n

# needed for xf86-video-fglrx:
  OPTION_EGLIBC_CATGETS = y

# libiconv replacement:
  OPTION_EGLIBC_CHARSETS = y

  OPTION_EGLIBC_DB_ALIASES = n
  OPTION_EGLIBC_LOCALES = n

# needed for example with glib and Python:
  OPTION_EGLIBC_LOCALE_CODE = y

  OPTION_EGLIBC_NIS = y
  OPTION_EGLIBC_NSSWITCH = y
  OPTION_EGLIBC_RCMD = n

# needed by eglibc byself (todo):
  OPTION_EGLIBC_RTLD_DEBUG = y

# needed for speed (optionally/todo)
  OPTION_POSIX_REGEXP_GLIBC = y

# needed for PAM and Mysql:
  OPTION_EGLIBC_GETLOGIN = y

# needed for systemd and dropbear:
  OPTION_EGLIBC_UTMP = y
  OPTION_EGLIBC_UTMPX = y

# debugging options:
  $DEBUG_OPTIONS
EOF

cat >config.cache <<EOF
ac_cv_header_cpuid_h=yes
libc_cv_forced_unwind=yes
libc_cv_c_cleanup=yes
libc_cv_gnu89_inline=yes
libc_cv_ssp=no
libc_cv_ctors_header=yes
EOF

cat >configparms <<EOF
slibdir=/lib
libdir=/usr/lib
EOF

../configure --host=$TARGET_NAME \
             --build=$HOST_NAME \
             --prefix=/usr \
             --libexecdir=/usr/lib/eglibc \
             --cache-file=config.cache \
             --disable-profile \
             --disable-sanity-checks \
             --enable-add-ons \
             --enable-bind-now \
             --with-elf \
             --with-tls \
             --enable-kernel=2.6.39 \
             --with-__thread \
             --with-binutils=$BUILD/toolchain/bin \
             --with-headers=$SYSROOT_PREFIX/usr/include \
             --without-cvs \
             --without-gd \
             $DEBUG_CONFIG \

make
make install_root=$SYSROOT_PREFIX install
