Changes to crosstool-0.42 by Martin Guy <martinwguy@yahoo.it>
Version 2 :)

crosstool.sh:

 * Add new config variable GLIBCPORTS_FILENAME.
   If it is set, we download the tarball, unpack it, move its contents into
   $GLIBC_DIR/ports, and add "ports" to GLIBC_ADDON_OPTIONS if that is unset.

 * Integrate the meaningful content of Khem Raj's NPTL patches and
   Steve Papacharalambous's ARM GNUEABI glibc-2.4 patches in a way that
   doesn't break non-NPTL builds or backward compatability in the dat files.
   To do an NPTL build, just set GLIBC_ADDON_OPTIONS=nptl
	
 * Accept "GLIBC_ADDON_OPTIONS=whatever" as well as old-style
   "GLIBC_ADDON_OPTIONS==whatever"

 * Added TARGET_CFLAGS also in glibc-header-construction phase to stop
   #error messages complaining.

getandpatch.sh:

 * Don't automatically derive glibc-linuxthreads filename from GLIBC_DIR
   because it doesn't exist from glibc-2.4. This breaks nothing because
   all .dat files give GLIBCTHREADS_FILENAME explicitly except cygwin,
   which doesn't want it.

 * Add support for glibc-2.3-200* and glibc-ports-200* snapshots

I have tested these changes with demo-arm (gcc-4.1.0+libc-2.3.2),
and demo-ppc750-tls and they also build latest-ARM-EABI too (for C and C++).

diff -urN crosstool-0.42/crosstool.sh crosstool-0.42-mg2/crosstool.sh
--- crosstool-0.42/crosstool.sh	2006-02-26 02:14:13.000000000 +0000
+++ crosstool-0.42-mg2/crosstool.sh	2006-04-14 16:05:33.000000000 +0100
@@ -69,40 +69,74 @@
 BUILD=${GCC_BUILD-`$TOP_DIR/config.guess`}
 test -z "$BUILD" && abort "bug: BUILD not set?!"
 
+# Local variable to say whether the nptl addon is being used.
+# Users set it by including "nptl" in the GLIBC_ADDON_OPTIONS
+unset with_nptl
+
 if test -z "${GLIBC_ADDON_OPTIONS}"; then
-   echo "GLIBC_ADDON_OPTIONS not set, so guessing addons from GLIBCTHREADS_FILENAME and GLIBCCRYPT_FILENAME"
-   # this is lame, need to fix this for nptl later?
-   # (nptl is an addon, but it's shipped in the main tarball)
+   echo "GLIBC_ADDON_OPTIONS not set, so guessing addons from GLIBC*_FILENAME"
    GLIBC_ADDON_OPTIONS="="
+   case "${GLIBCPORTS_FILENAME}" in
+     *-ports-*) GLIBC_ADDON_OPTIONS="${GLIBC_ADDON_OPTIONS}ports," ;;
+   esac
    case "${GLIBCTHREADS_FILENAME}" in
      *linuxthreads*) GLIBC_ADDON_OPTIONS="${GLIBC_ADDON_OPTIONS}linuxthreads," ;;
    esac
    # crypt is only an addon for glibc-2.1.x
-   test -z "${GLIBCCRYPT_FILENAME}"   || GLIBC_ADDON_OPTIONS="${GLIBC_ADDON_OPTIONS}crypt,"
+   if [ "${GLIBCCRYPT_FILENAME}" ]; then
+	GLIBC_ADDON_OPTIONS="${GLIBC_ADDON_OPTIONS}crypt,"
+   fi
+else
+   # GLIBC_ADDON_OPTIONS is set. Preen it and scan it for interesting items.
+
+   # Allow GLIBC_ADDON_OPTIONS=nptl as well as old "GLIBC_ADDON_OPTIONS="=nptl"
+   case "$GLIBC_ADDON_OPTIONS" in
+   =*)	;;
+   *) GLIBC_ADDON_OPTIONS="=$GLIBC_ADDON_OPTIONS" ;;
+   esac
+
+   case "$GLIBC_ADDON_OPTIONS" in
+   *nptl*)
+	with_nptl=1 ;;
+   esac
 fi
 
-# Add some default glibc config options if not given by user.  These used to be hardcoded.
+# Add some default glibc config options if not given by user.
 DEFAULT_GLIBC_EXTRA_CONFIG=""
-case "${GLIBC_EXTRA_CONFIG}" in
-*enable-kernel*) ;;
-*) DEFAULT_GLIBC_EXTRA_CONFIG="${DEFAULT_GLIBC_EXTRA_CONFIG} --enable-kernel=2.4.3"
-esac
-case "${GLIBC_EXTRA_CONFIG}" in
-*-tls*) ;;
-*) DEFAULT_GLIBC_EXTRA_CONFIG="${DEFAULT_GLIBC_EXTRA_CONFIG} --without-tls"
-esac
-case "${GLIBC_EXTRA_CONFIG}" in
-*-__thread*) ;;
-*) DEFAULT_GLIBC_EXTRA_CONFIG="${DEFAULT_GLIBC_EXTRA_CONFIG} --without-__thread"
-esac
+
+if [ "$with_nptl" ] ; then
+    # Set default options for NPTL
+    case "$GLIBC_EXTRA_CONFIG" in
+    *enable-kernel*) ;;
+    *) DEFAULT_GLIBC_EXTRA_CONFIG="$DEFAULT_GLIBC_EXTRA_CONFIG --enable-kernel=2.6.4"
+    esac
+    case "$GLIBC_EXTRA_CONFIG" in
+    *-tls*) ;;
+    *) DEFAULT_GLIBC_EXTRA_CONFIG="$DEFAULT_GLIBC_EXTRA_CONFIG --with-tls"
+    esac
+    case "$GLIBC_EXTRA_CONFIG" in
+    *-__thread*) ;;
+    *) DEFAULT_GLIBC_EXTRA_CONFIG="$DEFAULT_GLIBC_EXTRA_CONFIG --with-__thread"
+    esac
+else
+    # Non-NPTL defaults
+    case "$GLIBC_EXTRA_CONFIG" in
+    *enable-kernel*) ;;
+    *) DEFAULT_GLIBC_EXTRA_CONFIG="$DEFAULT_GLIBC_EXTRA_CONFIG --enable-kernel=2.4.3"
+    esac
+    case "$GLIBC_EXTRA_CONFIG" in
+    *-tls*) ;;
+    *) DEFAULT_GLIBC_EXTRA_CONFIG="$DEFAULT_GLIBC_EXTRA_CONFIG --without-tls"
+    esac
+    case "$GLIBC_EXTRA_CONFIG" in
+    *-__thread*) ;;
+    *) DEFAULT_GLIBC_EXTRA_CONFIG="$DEFAULT_GLIBC_EXTRA_CONFIG --without-__thread"
+    esac
+fi
 
 # One is forbidden
 test -z "${LD_LIBRARY_PATH}" || abort  "glibc refuses to build if LD_LIBRARY_PATH is set.  Please unset it before running this script."
 
-# And one is derived if unset.
-test -z "${GLIBCTHREADS_FILENAME}" &&
-GLIBCTHREADS_FILENAME=`echo $GLIBC_DIR | sed 's/glibc-/glibc-linuxthreads-/'`
-
 # Check for a few prerequisites that have tripped people up.
 awk '/x/' < /dev/null  || abort "You need awk to build a toolchain."
 test -z "${CFLAGS}"    || abort "Don't set CFLAGS, it screws up the build"
@@ -421,6 +455,8 @@
             ${GLIBC_DIR}/configure --prefix=/usr \
             --build=$BUILD --host=$TARGET \
             --without-cvs --disable-sanity-checks --with-headers=$HEADERDIR \
+	    --enable-add-ons$GLIBC_ADDON_OPTIONS \
+	    $GLIBC_EXTRA_CONFIG $DEFAULT_GLIBC_EXTRA_CONFIG \
             --enable-hacker-mode
     fi
 
@@ -463,6 +499,41 @@
     # so uncomment this if you need it
     #cp misc/syscall-list.h $HEADERDIR/bits/syscall.h
 
+    if [ "$with_nptl" ]; then
+	# To build gcc with thread support requires real pthread headers. These
+	# will have to manually be copied from under the tree of the desired
+	# target pthread implementation.
+	case "${ARCH}" in
+	ppc)
+	    ARCH_DIR="powerpc"
+	    ;;
+	*)
+	    ARCH_DIR="${ARCH}"
+	    ;;
+	esac
+
+	cp ${GLIBC_DIR}/nptl/sysdeps/pthread/pthread.h $HEADERDIR/pthread.h
+
+	# First check ports collection, then main dir
+	cp ${GLIBC_DIR}/ports/sysdeps/unix/sysv/linux/${ARCH_DIR}/nptl/bits/pthreadtypes.h $HEADERDIR/bits/pthreadtypes.h || \
+	cp ${GLIBC_DIR}/nptl/sysdeps/unix/sysv/linux/${ARCH_DIR}/bits/pthreadtypes.h $HEADERDIR/bits/pthreadtypes.h
+
+	# On s390, powerpc and sparc we also require bits/wordsize.h.
+	case $TARGET in
+	sparc* | s390* | powerpc* )
+	    case $TARGET in
+	    sparc64* )   wordsize_h=sysdeps/sparc/sparc64/bits/wordsize.h ;;
+	    sparc* )     wordsize_h=sysdeps/sparc/sparc32/bits/wordsize.h ;;
+	    s390x* )     wordsize_h=sysdeps/s390/s390x/bits/wordsize.h ;;
+	    s390* )      wordsize_h=sysdeps/s390/s390/bits/wordsize.h ;;
+	    powerpc64* ) wordsize_h=sysdeps/powerpc/powerpc64/bits/wordsize.h ;;
+	    powerpc* )   wordsize_h=sysdeps/powerpc/powerpc32/bits/wordsize.h ;;
+	    esac
+	    test ! -f $HEADERDIR/bits/wordsize.h && cp ${GLIBC_DIR}/${wordsize_h} $HEADERDIR/bits/wordsize.h
+	   ;;
+	esac
+    fi
+
     cd ..
 fi
 
@@ -486,7 +557,7 @@
         ${GCC_EXTRA_CONFIG} \
         ${GCC_SYSROOT_ARG_CORE} \
         --disable-nls \
-        --enable-threads=no \
+        --enable-threads=posix \
         --enable-symvers=gnu \
         --enable-__cxa_atexit \
         --enable-languages=c \
@@ -501,8 +572,127 @@
 
 logresult gcc-core $CORE_PREFIX/bin/${TARGET}-gcc${EXEEXT}
 
+# Following extra steps required for building an NPTL enabled glibc.
+if [ "$with_nptl" ] ; then
+    #---------------------------------------------------------
+    echo "Build glibc startfiles (required for shared libgcc)"
+
+    mkdir -p build-glibc-startfiles; cd build-glibc-startfiles
+
+    # sh4 really needs to set configparms as of gcc-3.4/glibc-2.3.2
+    # note: this is awkward, doesn't work well if you need more than one line in configparms
+    echo ${GLIBC_CONFIGPARMS} > configparms
+
+    echo "libc_cv_forced_unwind=yes" > config.cache
+    echo "libc_cv_c_cleanup=yes" >> config.cache
+    # this here is moot, currently you cannot build nptl for sparc64
+    case ${TARGET} in
+        sparc64* ) echo "libc_cv_sparc64_tls=yes" >> config.cache ;;
+    esac
+
+    if test '!' -f Makefile; then
+        # Configure with --prefix the way we want it on the target...
+        # There are a whole lot of settings here.  You'll probably want
+        # to read up on what they all mean, and customize a bit.
+        # e.g. I picked --enable-kernel=2.4.3 here just because it's the kernel Bill 
+        # used in his example gcc2.95.3 script.  That means some backwards compatibility 
+        # stuff is turned on in glibc that you may not need if you're using a newer kernel.
+        # Compare these options with the ones used when installing the glibc headers above - they're different.
+        # Adding "--without-gd" option to avoid error "memusagestat.c:36:16: gd.h: No such file or directory" 
+        # See also http://sources.redhat.com/ml/libc-alpha/2000-07/msg00024.html. 
+        # Set BUILD_CC, or you won't be able to build datafiles
+        # Set --build, else glibc-2.3.2 will think you're not cross-compiling, and try to run the test programs
+
+        BUILD_CC=gcc CFLAGS="$TARGET_CFLAGS" CC="${TARGET}-gcc $GLIBC_EXTRA_CC_ARGS" \
+        AR=${TARGET}-ar RANLIB=${TARGET}-ranlib \
+            ${GLIBC_DIR}/configure --prefix=/usr \
+	    --build=$BUILD --host=$TARGET \
+            ${GLIBC_EXTRA_CONFIG} ${DEFAULT_GLIBC_EXTRA_CONFIG} \
+            --enable-kernel=${GLIBC_MIN_KERNEL} \
+            --without-cvs --disable-profile --disable-debug --without-gd \
+            $SHARED_MODE \
+            --enable-add-ons${GLIBC_ADDON_OPTIONS} --with-headers=$HEADERDIR \
+            --cache-file=config.cache
+    fi
+
+    #TODO: should check whether slibdir has been set in configparms to */lib64
+    #      and copy the startfiles into the appropriate libdir.
+    make csu/subdir_lib
+    
+    test -z "${USE_SYSROOT}" &&
+    cp -fp csu/crt[1in].o ${SYSROOT}/lib/ ||
+    cp -fp csu/crt[1in].o ${SYSROOT}/usr/lib/ 
+
+    cd ..
+
+    #---------------------------------------------------------
+    echo "Build gcc-core w shared libgcc"
+
+    mkdir -p build-gcc-core-shared; cd build-gcc-core-shared
+
+    # Use --with-local-prefix so older gccs don't look in /usr/local (http://gcc.gnu.org/PR10532)
+
+    if test '!' -f Makefile; then
+        ${GCC_DIR}/configure $CANADIAN_BUILD --target=$TARGET --host=$GCC_HOST --prefix=$PREFIX \
+	    --with-local-prefix=${SYSROOT} \
+	    --disable-multilib \
+            ${GCC_EXTRA_CONFIG} \
+	    ${GCC_SYSROOT_ARG_CORE} \
+	    --disable-nls \
+	    --enable-symvers=gnu \
+	    --enable-__cxa_atexit \
+            --enable-languages=c \
+            --enable-shared
+    fi
+
+    # HACK: we need to override SHLIB_LC from gcc/config/t-slibgcc-elf-ver or
+    #       gcc/config/t-libunwind so -lc is removed from the link for 
+    #       libgcc_s.so, as we do not have a target -lc yet.
+    #       This is not as ugly as it appears to be ;-) All symbols get resolved
+    #       during the glibc build, and we provide a proper libgcc_s.so for the
+    #       cross toolchain during the final gcc build.
+    #
+    #       As we cannot modify the source tree, nor override SHLIB_LC itself
+    #       during configure or make, we have to edit the resultant 
+    #       gcc/libgcc.mk itself to remove -lc from the link.
+    #       This causes us to have to jump through some hoops...
+    #
+    #       To produce libgcc.mk to edit we firstly require libiberty.a,
+    #       so we configure then build it.
+    #       Next we have to configure gcc, create libgcc.mk then edit it...
+    #       So much easier if we just edit the source tree, but hey...
+    if test '!' -f ${GCC_DIR}/gcc/BASE-VER; then
+        make configure-libiberty
+        make -C libiberty libiberty.a
+        make configure-gcc
+    else
+	if test -d ${GCC_DIR}/libdecnumber; then
+	    make configure-libdecnumber
+	    make all-libdecnumber
+	fi
+        make configure-gcc
+	make configure-libcpp
+	make configure-build-libiberty
+	make all-libcpp
+	make all-build-libiberty
+    fi
+    make -C gcc libgcc.mk
+
+    if test '!' -f gcc/libgcc.mk-ORIG ; then cp -p gcc/libgcc.mk gcc/libgcc.mk-ORIG; fi
+    sed 's@-lc@@g' < gcc/libgcc.mk-ORIG > gcc/libgcc.mk
+
+    test "$CANADIAN_BUILD" = "" || make $PARALLELMFLAGS all-build-libiberty || true
+    make $PARALLELMFLAGS all-gcc 
+    make install-gcc
+
+    cd ..
+
+    logresult gcc-core-shared ${PREFIX}/bin/${TARGET}-gcc${EXEEXT} 
+
+fi
+
 #---------------------------------------------------------
-echo Build glibc and linuxthreads
+echo Build glibc
 
 mkdir -p build-glibc; cd build-glibc
 
@@ -510,6 +700,16 @@
 # note: this is awkward, doesn't work well if you need more than one line in configparms
 echo ${GLIBC_CONFIGPARMS} > configparms
 
+if [ "$with_nptl" ] ; then
+    # Following configure tests fail while cross-compiling
+    echo "libc_cv_forced_unwind=yes" > config.cache
+    echo "libc_cv_c_cleanup=yes" >> config.cache
+    # The following is moot, currently you cannot build nptl for sparc64
+    case ${TARGET} in
+        sparc64* ) echo "libc_cv_sparc64_tls=yes" >> config.cache ;;
+    esac
+fi
+
 if test '!' -f Makefile; then
     # Configure with --prefix the way we want it on the target...
     # There are a whole lot of settings here.  You'll probably want
@@ -527,6 +727,7 @@
         ${GLIBC_EXTRA_CONFIG} ${DEFAULT_GLIBC_EXTRA_CONFIG} \
         --without-cvs --disable-profile --disable-debug --without-gd \
         $SHARED_MODE \
+	--cache-file=config.cache \
         --enable-add-ons${GLIBC_ADDON_OPTIONS} --with-headers=$HEADERDIR
 fi
 
diff -urN crosstool-0.42/getandpatch.sh crosstool-0.42-mg2/getandpatch.sh
--- crosstool-0.42/getandpatch.sh	2006-02-27 19:22:26.000000000 +0000
+++ crosstool-0.42-mg2/getandpatch.sh	2006-04-14 15:24:50.000000000 +0100
@@ -32,9 +32,6 @@
   test -z "${GLIBC_DIR}"        && abort "Please set GLIBC_DIR to the bare filename of the glibc tarball or directory"
   test -z "${LINUX_SANITIZED_HEADER_DIR}" && echo "Not downloading linux-libc-headers. Set LINUX_SANITIZED_HEADER_DIR to do so"
   test -z "${LINUX_DIR}"        && echo "Not downloading kernel sources. Set LINUX_DIR if you want to do so"
-  # And one is derived if not set explicitly.
-  test -z "${GLIBCTHREADS_FILENAME}" &&
-  GLIBCTHREADS_FILENAME=`echo $GLIBC_DIR | sed 's/glibc-/glibc-linuxthreads-/'`
 fi
 
 test -z "${TARBALLS_DIR}"     && abort "Please set TARBALLS_DIR to the directory to download tarballs to."
@@ -145,6 +142,18 @@
         abort "Unrecognized suffix for tarball $ARCHIVE_NAME" ;;
     esac
 
+    # ports dir contents need to be moved to "ports"
+    case $BASENAME in
+    glibc*-ports-*)
+	# Bomb-proofing in case "ports" dir already exists
+	if [ -d ports ]; then
+		abort "ports directory already exists in $GLIBC_DIR"
+	else
+		mv $BASENAME ports
+	fi
+	;;
+    esac
+
     # Fix path of old linux source trees
     if [ -d linux ]; then
         mv linux $BASENAME
@@ -202,7 +211,7 @@
 # No glibc for cygwin.
 if test "${CYGWIN_DIR}" = ""; then
    case $GLIBC_DIR in
-      glibc-200*) 
+      glibc*-200*) 
   	  getUnpackAndPatch \
 		ftp://gcc.gnu.org/pub/glibc/snapshots/$GLIBC_DIR.tar.bz2 \
 		ftp://gcc.gnu.org/pub/glibc/snapshots/$GLIBC_DIR.tar.gz ;;
@@ -264,12 +273,25 @@
 	http://ep09.pld-linux.org/~mmazur/linux-libc-headers/${LINUX_SANITIZED_HEADER_DIR}.tar.bz2 \
 	ftp://ftp.lfs-matrix.net/pub/linux-libc-headers/${LINUX_SANITIZED_HEADER_DIR}.tar.bz2
   # Glibc addons must come after glibc
+  test -n "${GLIBCTHREADS_FILENAME}" &&
   getUnpackAndPatch     \
        ftp://ftp.gnu.org/pub/gnu/glibc/$GLIBCTHREADS_FILENAME.tar.bz2 \
        ftp://ftp.gnu.org/pub/gnu/glibc/$GLIBCTHREADS_FILENAME.tar.gz \
        ftp://gcc.gnu.org/pub/glibc/releases/$GLIBCTHREADS_FILENAME.tar.bz2 \
        ftp://gcc.gnu.org/pub/glibc/releases/$GLIBCTHREADS_FILENAME.tar.gz 
-
+  test -n "${GLIBCPORTS_FILENAME}" &&
+  case $GLIBCPORTS_FILENAME in
+      glibc*-ports-200*) 
+  	  getUnpackAndPatch \
+		ftp://gcc.gnu.org/pub/glibc/snapshots/$GLIBCPORTS_FILENAME.tar.bz2 \
+		ftp://gcc.gnu.org/pub/glibc/snapshots/$GLIBCPORTS_FILENAME.tar.gz ;;
+      *)  
+  	  getUnpackAndPatch \
+		ftp://ftp.gnu.org/pub/gnu/glibc/$GLIBCPORTS_FILENAME.tar.bz2 \
+		ftp://ftp.gnu.org/pub/gnu/glibc/$GLIBCPORTS_FILENAME.tar.gz \
+		ftp://gcc.gnu.org/pub/glibc/releases/$GLIBCPORTS_FILENAME.tar.bz2 \
+		ftp://gcc.gnu.org/pub/glibc/releases/$GLIBCPORTS_FILENAME.tar.gz ;;
+  esac
   test x$GLIBCCRYPT_FILENAME = x || getUnpackAndPatch     ftp://ftp.gnu.org/pub/gnu/glibc/$GLIBCCRYPT_FILENAME.tar.gz ftp://ftp.gnu.org/pub/gnu/glibc/$GLIBCCRYPT_FILENAME.tar.bz2
 fi
 
diff -urN crosstool-0.42/patches/glibc-2.4/make-install-lib-all.patch crosstool-0.42-mg2/patches/glibc-2.4/make-install-lib-all.patch
--- crosstool-0.42/patches/glibc-2.4/make-install-lib-all.patch	1970-01-01 01:00:00.000000000 +0100
+++ crosstool-0.42-mg2/patches/glibc-2.4/make-install-lib-all.patch	2006-04-14 15:30:38.000000000 +0100
@@ -0,0 +1,26 @@
+From http://svn.exactcode.de/t2/trunk/package/base/glibc32/make-install-lib-all.patch
+Rule to install all needed libraries, not just the ones installed by install-lib,
+yet not install programs.  
+Needed because we can't use the main install target, as we can't build programs before
+we have the final gcc installed; linking fails because libeh.a is not present,
+and glibc insists on linking programs with that library.
+
+diff -Naur glibc-2.3.4.orig/Makerules glibc-2.3.4/Makerules
+--- glibc-2.3.4.orig/Makerules	2004-12-15 20:52:39.000000000 +0200
++++ glibc-2.3.4/Makerules	2005-02-19 15:16:31.415125176 +0200
+@@ -844,6 +844,13 @@
+ installed-libcs := $(foreach o,$(filter-out .os,$(object-suffixes-for-libc)),\
+ 			     $(inst_libdir)/$(patsubst %,$(libtype$o),\
+ 						     $(libprefix)$(libc-name)))
++
++install-lib-all: $(inst_slibdir)/libc.so$(libc.so-version) \
++		$(inst_slibdir)/libc-$(version).so \
++		$(inst_libdir)/libc.so \
++		$(inst_libdir)/libc.a \
++		install-lib
++
+ install: $(installed-libcs)
+ $(installed-libcs): $(inst_libdir)/lib$(libprefix)%: lib $(+force)
+ 	$(make-target-directory)
+
+Signed-off-by: Robert P. J. Day <rpjday@mindspring.com>
