How to build a clean gcc4 on systems with only ancient gcc versions available
Building gcc4 with gcc3
Compiling gcc4 on an system with only old gcc versions available, can be tricky. Here is an HowTo for compiling gcc4.4 on Solaris 10, which only ships 3.4.3. It will be similar for other systems with an older gcc versions.
To have a clean build of gcc, i decided to do 3 build cycles. The first build will be installed to /opt/gcc4stage and certainly unclean, since it links on gcc3 libs. The second build will use the gcc4stage to compile /opt/gcc4.4 and the final step is a rebuild of gcc4.4 using the gcc build from the second build.
Versions
First you need to decide, which dependency versions of gcc you like to use. I propose to use a minimal set, if you like to use CLooG, you will not be able to compile it using gcc3, thus you can add this in to the last step. I've choosen to build gcc-4.4.6 with gmp-4.3.2, mpfr-2.4.2 and mpc-0.8.1. Newer versions also didn't compile with gcc3, you can use newer versions for the last step, if you wish to. Be aware, that this build is for Solaris x86, i compile a 32bit gcc, but it will also be usable for building 64bit binaries. If you want to build for an other plattform, make sure you change the build parameters.
Preparations
Make sure you've gcc in your PATH (/usr/sfw/bin on Solaris), also you'll need ld and gmake. You will need write access to /opt/gcc4stage and /opt/gcc4.4, you can run just gmake install as root, or make the directories writable to your user. I'll use LD_LIBRARY_PATH a few times during the build process, do never do this by default, it will break your system if you define it as global variable. Make sure, that there is no LD_LIBRARY_PATH in your build env and gcc3, gmake, etc. is working without. you can download the sources of gcc and it's dependencies from the gnu.org ftp server. I assume that you use a separated working directory for each build, and you've axtracted the sources into each of the 3 working directories.
Stage 1
CC=gcc CXX=g++ CFLAGS="-I/opt/gcc4stage/include" CXXFLAGS="-I/opt/gcc4stage/include" LDFLAGS="-L/opt/gcc4stage/lib -R/opt/gcc4stage/lib" LD_LIBRARY_PATH="/opt/gcc4stage/lib" WDIR="$PWD" export CC CXX CFLAGS CXXFLAGS LDFLAGS LD_LIBRARY_PATH WDIR cd "$WDIR"/gmp-4.3.2 ./configure --prefix=/opt/gcc4stage --disable-static --with-pic --enable-cxx --build=i386-pc-solaris2.10 \ && gmake && gmake install && echo "ALL done" cd "$WDIR"/mpfr-2.4.2 ./configure --prefix=/opt/gcc4stage \ --with-gmp=/opt/gcc4stage --disable-static --build=i386-pc-solaris2.10 \ && gmake && gmake install && echo "ALL done" cd "$WDIR"/mpc-0.8.1 ./configure --prefix=/opt/gcc4stage \ --with-gmp=/opt/gcc4stage --with-mpfr=/opt/gcc4stage --build=i386-pc-solaris2.10 \ && gmake && gmake install && echo "ALL done" mkdir "$WDIR"/build-gcc cd "$WDIR"/build-gcc ../gcc-4.4.6/configure --prefix=/opt/gcc4stage --with-mpfr=/opt/gcc4stage --with-gmp=/opt/gcc4stage \ --with-mpc=/opt/gcc4stage --without-ppl --without-cloog --with-gnu-as --with-as=/usr/sfw/bin/gas \ --with-ld=/usr/ccs/bin/ld --enable-stage1-languages=c --enable-languages=c,c++,objc,fortran \ --with-stage1-ldflags="-R/opt/gcc4stage" --with-boot-ldflags="-R/opt/gcc4stage" \ --build=i386-pc-solaris2.10 \ && gmake && gmake install && echo "ALL done"
Stage 2
Now that you've a working gcc4 in /opt/gcc4stage, you can use that gcc for compiling gcc. Make sure you reset the envronment variables and change to a clean working directory. also we will use make check now, i dropped it for the first stage since it's not working anyway. When this build is completed, the stage 1 gcc is not longer needed and thus i move it away to make sure, that noone is able to use it anymore.
CC=/opt/gcc4stage/bin/gcc CXX=/opt/gcc4stage/bin/g++ CFLAGS="-I/opt/gcc4.4/include" CXXFLAGS="-I/opt/gcc4.4/include" LDFLAGS="-L/opt/gcc4.4/lib -R/opt/gcc4.4/lib" LD_LIBRARY_PATH="/opt/gcc4.4/lib:/opt/gcc4stage/lib" WDIR="$PWD" export CC CXX CFLAGS CXXFLAGS LDFLAGS LD_LIBRARY_PATH WDIR cd "$WDIR"/gmp-4.3.2 ./configure --prefix=/opt/gcc4.4 --disable-static \ --with-pic --enable-cxx --build=i386-pc-solaris2.10 \ && gmake && gmake install && gmake check && echo "ALL done" cd "$WDIR"/mpfr-2.4.2 ./configure --prefix=/opt/gcc4.4 --with-gmp=/opt/gcc4.4 \ --disable-static --build=i386-pc-solaris2.10 \ && gmake && gmake install && gmake check && echo "ALL done" cd "$WDIR"/mpc-0.8.1 ./configure --prefix=/opt/gcc4.4 --with-gmp=/opt/gcc4.4 \ --with-mpfr=/opt/gcc4.4 --build=i386-pc-solaris2.10 --build=i386-pc-solaris2.10 \ && gmake && gmake install && gmake check && echo "ALL done" mkdir "$WDIR"/build-gcc cd "$WDIR"/build-gcc ../gcc-4.4.6/configure --prefix=/opt/gcc4.4 --with-mpfr=/opt/gcc4.4 \ --with-gmp=/opt/gcc4.4 --with-mpc=/opt/gcc4.4 --without-ppl --without-cloog \ --with-gnu-as --with-as=/usr/sfw/bin/gas --with-ld=/usr/ccs/bin/ld \ --enable-stage1-languages=c --enable-languages=c,c++,objc,fortran \ --with-stage1-ldflags="-R/opt/gcc4.4" --with-boot-ldflags="-R/opt/gcc4.4" \ --build=i386-pc-solaris2.10 \ && gmake && gmake install && echo "ALL done" mv /opt/gcc4stage /opt/gcc4_to_remove
Final build
If you want to switch Versions or add dependencies, this would be the right place. This is the last step of our gcc build, thus we will add some flags to gmake that will be necessary to run gcc without LD_LIBRARY_PATH or ld.so.conf entries. Also we add -R/opt/gcc4.4/lib to the gcc specs file, thus executables created by this gcc will search for the gcc libraries in /opt/gcc4.4/lib by default.
CC=/opt/gcc4.4/bin/gcc CXX=/opt/gcc4.4/bin/g++ CFLAGS="-I/opt/gcc4.4/include" CXXFLAGS="-I/opt/gcc4.4/include" LDFLAGS="-L/opt/gcc4.4/lib -R/opt/gcc4.4/lib" LD_LIBRARY_PATH="/opt/gcc4.4/lib" WDIR="$PWD" export CC CXX CFLAGS CXXFLAGS LDFLAGS LD_LIBRARY_PATH WDIR /opt/gcc4.4/bin/gcc -dumpspecs | sed 's!:-lgcc!:-R/opt/gcc4.4/lib -lgcc!g' \ > /opt/gcc4.4/lib/gcc/i386-pc-solaris2.10/4.4.6/specs cd "$WDIR"/gmp-4.3.2 ./configure --prefix=/opt/gcc4.4 --disable-static \ --with-pic --enable-cxx --build=i386-pc-solaris2.10 \ && gmake && gmake install && gmake check && echo "ALL done" cd "$WDIR"/mpfr-2.4.2 ./configure --prefix=/opt/gcc4.4 --with-gmp=/opt/gcc4.4 \ --disable-static --build=i386-pc-solaris2.10 \ && gmake && gmake install && gmake check && echo "ALL done" cd "$WDIR"/mpc-0.8.1 ./configure --prefix=/opt/gcc4.4 --with-gmp=/opt/gcc4.4 \ --with-mpfr=/opt/gcc4.4 --build=i386-pc-solaris2.10 --build=i386-pc-solaris2.10 \ && gmake && gmake install && gmake check && echo "ALL done" mkdir "$WDIR"/build-gcc cd "$WDIR"/build-gcc ../gcc-4.4.6/configure --prefix=/opt/gcc4.4 --with-mpfr=/opt/gcc4.4 \ --with-gmp=/opt/gcc4.4 --with-mpc=/opt/gcc4.4 --without-ppl --without-cloog \ --with-gnu-as --with-as=/usr/sfw/bin/gas --with-ld=/usr/ccs/bin/ld \ --enable-stage1-languages=c --enable-languages=c,c++,objc,fortran \ --with-stage1-ldflags="-R/opt/gcc4.4" --with-boot-ldflags="-R/opt/gcc4.4" \ --build=i386-pc-solaris2.10 \ && gmake LDFLAGS_FOR_TARGET="$LDFLAGS" LDFLAGS_FOR_BUILD="$LDFLAGS" BOOT_LDFLAGS="$LDFLAGS" \ && gmake install && echo "ALL done" /opt/gcc4.4/bin/gcc -dumpspecs | sed 's!:-lgcc!:-R/opt/gcc4.4/lib -lgcc!g' \ > /opt/gcc4.4/lib/gcc/i386-pc-solaris2.10/4.4.6/specs