#!/bin/bash
# TODO: consider adding -D_FORTIFY_SOURCE=2 to default cflags

# Turtle Kevux Information
distribution_name="turtle"
cd_distribution_name="Turtle"
turtle_version="0.9.5"
archive_formats="txz tzo tbz bz2 tgz tbz tlz tar.gz tar.bz2 tar.lzo .tar.xz"
system_version_file=/etc/turtle-versions
root_user_name=turtle
root_user_name_length=$(echo -n $root_user_name | wc -m)
root_user_home_dir=/home/users/$root_user_name
target_system_path=/bin:/sbin:/toolchain/bin:/package/bin
target_include_path=/toolchain/include/
target=x86_64-pc-linux-uclibc
#target=i686-pc-linux-uclibc
#target=powerpc-linux-uclibc
ldso=ld-uClibc.so.0
the_time_zone=CST6CDT
cdrom_squash_format=xz # gzip, xz, lzo
cdrom_mini_squash_format=xz # same as cdrom_squash_format but is used for the mini-cdrom that is auto-created
initrd_compression_command='lzop -9'
initrd_compression_extension=.lzo
initrd_deccompression_command='lzop -d'

# uClibc tweaks for different architectures
# for x86 set this to "", for x86_64 set this to -x86_64
uclibc_config_extension=-x86_64
#uclibc_config_extension=

# The glibc compatible ld native linker is needed by wine
# this linker uses a rather insecure approach to load linker files that allows setuid binaries to be executed from a simple ldd command
# without this, wine cannot find its shared libraries (the uClibc config option is called: LDSO_LDD_SUPPORT)
#uclibc_alternate_config=-debug
#uclibc_alternate_config=-insecure
uclibc_alternate_config=

# New Kernel build type options: desktop-x86, server-x86, xo-1_0, alix-2d3, server-x86_64, desktop-x86_64, boot-x86
# New Kernel build types not yet supported but planned: desktop-mips, server-mips, desktop-mips_64, server-mips_64
kernel_build_type="server-x86_64"
kernel_build_tools_type="server-x86_64"
kernel_build_architecture=x86_64 # x86, x86_64, mips, mips_64

# The gmp library has a habit of using non-portable code (assembly)
# this breaks just about everything because it assumes that the host machine is the architecture to build for
# unless you are using a non-x86 architecture use the default of: i386-pc-linux-gnu
# for a PS3 (or other PowerPC architecture), use: powerpc-linux-gnu
# apparently, setting none-none-none seems to be a way to disable ASM and use generic c (aka portable!)
gmp_build_type=none-none-none
#gmp_build_type=i386-pc-linux-gnu

# Some projects require one to explicitly pass some sort of --enable-64bit for 64-bit support
# if wine has 64-bit enabled, then only 64-bit windows binaries will be supported.
#wine_64bit_mode=
wine_64bit_mode="--enable-win64"
#nspr_64bit_mode=
nspr_64bit_mode="--enable-64bit"
#nss_64bit_mode=
nss_64bit_mode='USE_64="1"'

# Host System Dependencies
#host_root_name=turtle
if [[ -f /etc/passwd ]] ; then
  host_root_name=$(grep -m 1 -o '^\<[^:]*\>:[^:]*:0:0:' /etc/passwd | grep -o '^[^:]*')
else
  host_root_name=turtle
fi

host_system_path=/bin:/sbin:/toolchain/bin:/toolchain/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin
host_include_path=/toolchain/include/
host_system_lib_path=/lib:/usr/lib:/usr/local/lib
glibc_host="no" # "yes" or "no"

# Documentation settings
documentation_directory=/documentation/
#desired_documentation_doc_types="man info doc html kevux"
desired_documentation_doc_types="kevux"
desired_documentation_classifications="core standard network servers multimedia games xorg compilation applications"

# Paths
target_system=/new/
target_toolchain=/toolchain/
target_package=/package/

# pre-defined variables
SH_ONLY="--disable-static --enable-shared"
ST_ONLY="--enable-static --disable-shared"
SH_N_ST="--enable-static --enable-shared"
TARGET="--target=$target --build=$target --host=$target"

# tweak the environment
set +h
umask 0027
export LC_ALL=POSIX

# enforce any specific optimizations
# if you wish to compile this optimized for your specific cpu, use: -march=native
# known -march=/-mtune= types: generic (-mtune only), native, i386, i486, pentium, pentium-mmx, pentiumpro, i686, pentium2, pentium3, pentium3m, , pentium4, prescott, nocona, core2, k6, k6-2, athlon, athlon-4, k8, k8-sse3, barcelona, winchip-c6, winchip2, c3, c3-2, geode
# known -mfpmath options: 387, sse, sse,387, sse+387, both
# known mips -march=/-mtune= types: native, mips1, mips2, mips3, mips4, mips32, mips32r2, mips64, mips64r2
# use sse for pentium3+ and 387 for older version (both is experimental and likely to increase memory usage of registers)
# 32-bit default: -m96bit-long-double, newer 64-bit default: -m128bit-long-double
# -malign-double increases performance at the cost of more memory usage
# also useful: -mmmx, -msse, -msse2, -msse3, -msse4, -maes
#export CFLAGS="-O2 -march=i686 -mmmx "
#export CXXFLAGS="-O2 -march=i686 -mmmx "
#export CFLAGS="-O2 -march=i686 -mmmx -msse -mfpmath=sse "
#export CXXFLAGS="-O2 -march=i686 -mmmx -msse -mfpmath=sse "
#export CFLAGS="-O2 -mmmx -msse -msse2 -mfpmath=sse "
#export CXXFLAGS="-O2 -mmmx -msse -msse2 -mfpmath=sse "
#export CFLAGS="-O2 -march=athlon-xp -mmmx -msse -mfpmath=sse "
#export CXXFLAGS="-O2 -mmmx -march=athlon-xp -msse -mfpmath=sse "
#export CFLAGS="-O2 -march=pentium4 -mmmx -msse -msse2 -mfpmath=sse "
#export CXXFLAGS="-O2 -march=pentium4 -mmmx -msse -msse2 -mfpmath=sse "
#export CFLAGS="-O2 -mtune=geode -mmmx -m3dnow " # olpc xo-1_0
#export CXXFLAGS="-O2 -mtune=geode -mmmx -m3dnow " # olpc xo-1_0
#export CFLAGS="-O2 -mtune=geode -mmmx -m3dnow " # alix-2d3
#export CXXFLAGS="-O2 -mtune=geode -mmmx -m3dnow " # alix-2d3
export CFLAGS="-O2 -march=athlon64 -mmmx -msse -msse2 -mfpmath=sse -mcx16 -m128bit-long-double " # 64-bit
export CXXFLAGS="-O2 -march=athlon64 -mmmx -msse -msse2 -mfpmath=sse -mcx16 -m128bit-long-double " # 64-bit

# the libdrm system has a problem where the architecture must be specified for the intel and radeon drivers
#libdrm_architecture="-O2 -march=i686 -mmmx -msse -mfpmath=sse"
#libdrm_architecture="-O2 -march=i686 -mmmx -msse -msse2 -mfpmath=sse"
#libdrm_architecture="-O2 -march=athlon-xp -mmmx -msse -mfpmath=sse"
#libdrm_architecture="-O2 -march=pentium4 -mmmx -msse -msse2 -mfpmath=sse"
#libdrm_architecture="-O2 -march=geode -mmmx -m3dnow" # olpc xo-1_0
libdrm_architecture="-O2 -march=athlon64 -mmmx -msse -msse2 -mfpmath=sse -mcx16 -m128bit-long-double" # 64-bit

# Mesa
#mesa_cflags="-O2 -march=i686 -mmmx -msse -mfpmath=sse"
#mesa_cflags="-O2 -march=i686 -mmmx -msse -msse2 -mfpmath=sse"
#mesa_cflags="-O2 -march=athlon-xp -mmmx -msse -mfpmath=sse"
#mesa_cflags="-O2 -march=pentium4 -mmmx -msse -msse2 -mfpmath=sse"
#mesa_cflags="-O2 -march=geode -mmmx -m3dnow" # olpc xo-1_0
mesa_cflags="-O2 -march=athlon64 -mmmx -msse -msse2 -mfpmath=sse -mcx16 -m128bit-long-double" # 64-bit

# the libdrm system has a problem where the architecture must be specified for the intel and radeon drivers
#ffmpeg_cflags="-O2 -march=i686 -mmmx"
#ffmpeg_cflags="-O2 -march=i686 -mmmx -msse -mfpmath=sse"
#ffmpeg_cflags="-O2 -march=i686 -mmmx -msse -msse2 -mfpmath=sse"
#ffmpeg_cflags="-O2 -march=athlon-xp -mmmx -msse -mfpmath=sse"
#ffmpeg_cflags="-O2 -march=pentium4 -mmmx -msse -msse2 -mfpmath=sse"
#ffmpeg_cflags="-march=geode -mmmx -m3dnow" # olpc xo-1_0
ffmpeg_cflags="-march=athlon64 -mmmx -msse -msse2 -mfpmath=sse -mcx16 -m128bit-long-double" # 64-bit

# QEMU CFLAGS, in some cases (such as compiling x86 on an x86_64 bit host natively) -march=i686 needs to be passed to prevent an undefined reference to '__sync_fetch_and_and_4'
# warning: qemu stopped functioning properly and I am starting to suspect it may be related to these optimizations
#qemu_cflags=
#qemu_cflags="-O2 -march=i686"
#qemu_cflags="-O2 -march=i686 -mmmx -msse -mfpmath=sse"
#qemu_cflags="-O2 -march=i686 -mmmx -msse -msse2 -mfpmath=sse"
#qemu_cflags="-O2 -march=athlon-xp -mmmx -msse -mfpmath=sse"
#qemu_cflags="-O2 -march=pentium4 -mmmx -msse -msse2 -mfpmath=sse"
#qemu_cflags="-O2 -march=geode -mmmx -m3dnow" # olpc xo-1_0
qemu_cflags="-O2 -march=athlon64 -mmmx -msse -msse2 -mfpmath=sse -mcx16 -m128bit-long-double" # 64-bit

# Openssl architecture flags, common options are: linux-generic32, linux-generic64
#openssl_architecture=linux-generic32
openssl_architecture=linux-generic64
#openssl_machine=x86
openssl_machine=x86_64

# cflags to optimize uclibc
uclibc_optimization="-O2"
#uclibc_cpu_cflags="-march=i686 -mmmx"
#uclibc_cpu_cflags="-march=i686 -mmmx -msse -mfpmath=sse"
#uclibc_cpu_cflags="-march=i686 -mmmx -msse -msse2 -mfpmath=sse"
#uclibc_cpu_cflags="-march=athlon-xp -mmmx -msse -mfpmath=sse"
#uclibc_cpu_cflags="-march=pentium4 -mmmx -msse -msse2 -mfpmath=sse"
#uclibc_cpu_cflags="-march=i686 -mmmx -m3dnow" # olpc xo-1_0
#uclibc_cpu_cflags="-march=i686 -mmmx -m3dnow" # alix-2d3
uclibc_cpu_cflags="-march=athlon64 -mmmx -msse -msse2 -mfpmath=sse -mcx16 -m128bit-long-double" # 64-bit

# these might be overridden by the local setting scripts, namely settings-tools
PATH=$target_system_path:${TO}bin
BCONF="--prefix=/ --includedir=${target_toolchain}include --bindir=/bin --sbindir=/sbin --localstatedir=/var --datadir=/share --sysconfdir=/etc --libexecdir=/lib --mandir=/documentation/man/"
CONF="$BCONF $SH_ONLY --with-pic"
BTCONF="--prefix=$target_toolchain --includedir=${target_toolchain}include --libexecdir=${target_toolchain}lib"
TCONF="$BTCONF $SH_ONLY --with-pic"
PCONF="--prefix=$target_package --includedir=${target_package}include --libexecdir=${target_package}lib --enable-static --disable-shared --with-pic"
DOCDIR="--docdir=${documentation_directory}"
