# This script automate build of HURD cross compiler,
# HURD and GNU LibC.
#
# Based on information provided by Jeff Bailey at
# http://hurddocs.sourceforge.net/howto/cross.html
# and hurd/INSTALL* files.
#
# Please send comments and suggestions to:
# "Arkadi E. Shishlov" [arkadi at it.lv]
#
# Please read this file to the end - it contain
# essential comments!
#
# Tested on Linux only. Other OSes require changes in
# this script.
#
# You need:
#   binutils
#   glibc
#   gcc-core (only core w/o c++/objc/f)
#   (other gcc stuff can be built later when libs are in place?)
#   hurd (from CVS)
#   gnumach (from CVS)
#   mig (from CVS)
#
# Set following variables:
# $H - cross tools install root.
# $B - build directory.
#
# Probably you want to adjust c_debug parameter.
#
# Always build LibC with at least -O optimizations or
# you'll get unresolved symbols!
#
## OBSOLETED:
## There are still problems compiling HURD and GNUmach
## with GCC 2.95 and higher.
## So probably you want patch, that allow GNUmach to be
## compiled by GCC 2.95 or higher, or build EGCS 1.1.2
## in additional to GCC - watch for $wantEGCS in script.
#
# Create similar structure under $B (it's my build dir):
#   binutils -> ../gnu/rel/binutils-2.10.1/
#   gcc -> ../gnu/rel/gcc-2.95.2/
#   glibc -> ../gnu/libc/
#   gnumach -> ../gnu/gnumach/
#   gnumach-smp -> ../gnu/gnumach-smp/
#   hurd -> ../gnu/hurd/
#   mig -> ../gnu/mig/
#   oskit -> ../oskit-20000505/
#   oskit-mach -> ../gnu/oskit-mach/
#   oskit2 -> ../oskit-20010214/


H=/home/arkadi/opt/hurd-cross
B=/home/arkadi/work/build
gcc_ver=2.95.3
# If you want to build EGCS - uncomment this line.
#wantEGCS=1

export PATH="$H/bin:$PATH"

c_debug="-march=i586 -O -g -pipe"
c_opt="-march=i586 -O2 -fomit-frame-pointer -pipe"

#unset CFLAGS
#unset CXXFLAGS
#export CFLAGS="$c_opt"
#export CXXFLAGS="$c_opt"
export C_INCLUDE_PATH=$H/i586-pc-gnu/include

#L=/home/arkadi/opt/hurd-cross/lib
#export LIBRARY_PATH=$L
#export LD_LIBRARY_PATH=$L
#export LD_RUN_PATH=$L

# This doesn't work. Every package require separate approach.
#function make () { /usr/bin/make -j -l 10 $* }

/bin/rm -rf $H \
       $B/binutils.build \
       $B/egcs.build \
       $B/gcc.build \
       $B/gnumach.build \
       $B/mig.build \
       $B/hurd.build \
       $B/glibc.build
/bin/rm -f $B/*.log.*
/bin/mkdir -p $H/bin


export CFLAGS="$c_opt"

mkdir $B/binutils.build
cd $B/binutils.build
../binutils/configure --prefix=$H --target=i586-pc-gnu -v >../binutils.log.configure 2>&1
make         >../binutils.log.make 2>&1
make check   >../binutils.log.make-check 2>&1
make install >../binutils.log.make-install 2>&1
cd ..


if [ $wantEGCS ]; then
    mkdir $B/egcs.build
    cd $B/egcs.build
    ../egcs/configure --prefix=$H --target=i586-pc-gnu --with-gnu-as --with-gnu-ld >../egcs.log.configure 2>&1
    make -k >../egcs.log.make-k 2>&1
    make -k install >../egcs.log.make-k-install 2>&1
    cd ..
    mv $H/bin/i586-pc-gnu-gcc $H/bin/i586-pc-gnu-egcs
fi;

# how to build libgcc.a with debug?
mkdir $B/gcc.build
cd $B/gcc.build
../gcc/configure --prefix=$H --target=i586-pc-gnu --with-gnu-as --with-gnu-ld --enable-languages= >../gcc.log.configure 2>&1
make -k >../gcc.log.make-k 2>&1
make -k install >../gcc.log.make-k-install 2>&1
/bin/ln -s $H/i586-pc-gnu $H/lib/gcc-lib/i586-pc-gnu/$gcc_ver/i586-pc-gnu
cd ..


mkdir $B/gnumach.build
cd $B/gnumach.build
../gnumach/configure --build=i686-pc-linux-gnu --host=i586-pc-gnu >../gnumach.log.configure 2>&1
make -k install-headers prefix=$H/i586-pc-gnu >../gnumach.log.make-k-install-headers 2>&1
cd ..


mkdir $B/mig.build
cd $B/mig.build
../mig/configure --target=i586-pc-gnu --host=i686-pc-linux-gnu --prefix=$H >../mig.log.configure 2>&1
make >../mig.log.make 2>&1
make install >../mig.log.make-install 2>&1
ln -s i586-pc-gnu-mig $H/bin/mig
cd ..


export CFLAGS="$c_debug"

mkdir $B/hurd.build
cd $B/hurd.build
../hurd/configure --build=i686-pc-linux-gnu --host=i586-pc-gnu --prefix=$H/i586-pc-gnu --disable-profile >../hurd.log.configure 2>&1
make install-headers no_deps=t >../hurd.log.make-install-headers-no_deps 2>&1
cd ..


mkdir $B/glibc.build
cd $B/glibc.build
#../glibc/configure --without-cvs --enable-add-ons=crypt --build=i686-pc-linux-gnu --host=i586-pc-gnu --prefix= --disable-profile >../glibc.log.configure 2>&1
../glibc/configure --without-cvs --build=i686-pc-linux-gnu --host=i586-pc-gnu --prefix= --disable-profile >../glibc.log.configure 2>&1
make -k install-headers install_root=$H/i586-pc-gnu >../glibc.log.make-install-headers 2>&1
/bin/cp ../glibc/include/features.h $H/i586-pc-gnu/include/features.h
/bin/mkdir $H/i586-pc-gnu/include/gnu
/bin/touch $H/i586-pc-gnu/include/gnu/stubs.h
cd ..


export CFLAGS="$c_opt"

if [ $wantEGCS ]; then
    cd $B/egcs.build
    make >../egcs.log.make 2>&1
    make install >../egcs.log.make-install 2>&1
    cd ..
fi;


cd $B/gcc.build
make >../gcc.log.make 2>&1
make install >../gcc.log.make-install 2>&1
cd ..


export CFLAGS="$c_debug"

cd $B/glibc.build
make >../glibc.log.make 2>&1
make install install_root=$H/i586-pc-gnu >../glibc.log.make-install 2>&1
# libc.so ld script points to library in /lib, not in local dir, fix it
perl -pi~ -e 's|/lib/||g;' $H/i586-pc-gnu/lib/libc.so
cd ..


# configure and build from scratch
/bin/rm -rf $B/hurd.build
mkdir -p $B/hurd.build
cd $B/hurd.build
../hurd/configure --build=i686-pc-linux-gnu --host=i586-pc-gnu --disable-profile >../hurd.log.configure2 2>&1
make >../hurd.log.make2 2>&1
# this will fail if you're not root because
# some Makefiles supply -o root to install
# just do something like this:
#   $ cd $B/hurd
#   $ perl -pi~ -e 's/-o root//g;' daemons/Makefile utils/Makefile
mkdir $H/i586-pc-gnu/boot
make install prefix=$H/i586-pc-gnu>../hurd.log.make-install 2>&1
cd ..

echo "
You should add
  $H/bin
to you PATH in order to use cross-compiler.
Hurd and LibC binaries located under
  $H/i586-pc-gnu
Copy these binaries on live hurd system to test it."
