Hi all,

I would like to build GCC from source, using MinGW/MSYS on Windows. I have tried many times, but without success. I'll describe the most recent method I used below, and would be very grateful if anyone could tell me what it is that I'm doing wrong.

I have searched for recent guides on how to accomplish this goal, but couldn't find much. I mostly worked of the instructions from the following two pages.

(I followed the steps in the second link to the letter, but still ended up with an error during the 'make' stage for GCC.)

So here's what I tried, and had expected to work.

1. To get a working MinGW/MSYS install, I download mingw-get-0.1-mingw32-alpha-5-bin.tar.gz and unpack it to an arbitrary directory, let's say e:\temp\mingw.
I create a batch script in that same directory which sets the path as follows, to include the bin directory where mingw-get.exe is located:
copies defaults.xml to profile.xml in the var\lib\mingw-get\data directory, and then runs the following three commands, repeating each one until it finishes without errors, before proceeding with the next one.

mingw-get update
mingw-get install mingw32-base
mingw-get install mingw-dtk

I believe these two meta-packages should get me all the tools I need for building GCC.

(For ease of starting MSYS, I then create a batch script in that same top directory, which sets the path and runs the msys startup script, like this:

%~dp0msys\1.0\msys.bat )

2. I download the w32api-dev, mingw-runtime-dev, and binutils packages from the MinGW Sourceforge page, and download the latest sources for gcc-core, gmp, mpfr, and mpc from their respective project sites, putting them all in one directory (not necessary of course, but easier for the following step). Let's say I place them in e:\temp\packages.

3. I now run the shell script gcc-mingw.sh, which basically does the following:
- Extract win32api, mingwrt, and binutils to /mingw
- Extract and arrange the sources for gcc-core, gmp, mpfr, and mpc
- Create a build dir, and run configure, make and make install from there. (This fails during 'make'.)

This is the contents of my gcc-mingw.sh script.
(I don't have much experience with bash scripting, so please try not to laugh too hard if I've done things in funny ways. ;) )



# Extract w32api, mingw-runtime, and binutils.
{ rm -rf /mingw && mkdir /mingw && cd /mingw; }  || \
  { echo Creation of /mingw failed.; exit; }
{ tar -xaf ${PKGDIR}/w32api-${W32API_VERSION}-mingw32-dev.tar.*                 && \
  tar -xaf ${PKGDIR}/mingwrt-${MINGWRT_VERSION}-mingw32-dev.tar.*               && \
  tar -xaf ${PKGDIR}/binutils-${BINUTILS_VERSION}-mingw32-bin.tar.*; }          || \
  { echo Extraction of w32api, mingw-runtime, and/or binutils failed.; exit; }

# Extract sources.
{ { test -d ${WORKDIR} || mkdir ${WORKDIR}; } && cd ${WORKDIR}; } || \
  { echo Creation of ${WORKDIR} failed.; exit; }
{ tar -xaf ${PKGDIR}/gcc-core-${GCC_VERSION}.tar.*  && \
  tar -xaf ${PKGDIR}/gmp-${GMP_VERSION}.tar.*       && \
  tar -xaf ${PKGDIR}/mpfr-${MPFR_VERSION}.tar.*     && \
  tar -xaf ${PKGDIR}/mpc-${MPC_VERSION}.tar.*; }    || \
  { echo Extraction of gcc failed.; exit; }

# Arrange sources.
rm -rf src || { echo Removal of ${WORKDIR}/src failed; exit; }
{ mv gcc-${GCC_VERSION} src         && \
  mv gmp-${GMP_VERSION} src/gmp     && \
  mv mpfr-${MPFR_VERSION} src/mpfr  && \
  mv mpc-${MPC_VERSION} src/mpc; }  || \
  { echo Arranging of sources failed.; exit; }

# Configure.
{ rm -rf {build,dest} && mkdir {build,dest} && cd build; }       || \
  { echo Creation of ${WORKDIR}/\{build,dest\} failed.; exit; }
../src/configure     \
  --prefix=/mingw    \
  --host=mingw32     \
  --build=mingw32    \
  --target=mingw32   \
  --with-as=/mingw/bin/as.exe  \
  --with-ld=/mingw/bin/ld.exe  \
  --with-gnu-as                \
  --with-gnu-ld                \
  --disable-nls                \
  --enable-languages=c         \
  --disable-win32-registry     \
  --enable-version-specific-runtime-libs  || \
  { echo Configure failed.; exit; }

# Build and install.
make || { echo EPIC FAIL; exit; }
make DESTDIR=${WORKDIR} install || { echo Install failed.; exit; }

# Cleanup.
rm -rf {/mingw,build,src}
mv mingw gcc
cd gcc
tar -caf ../gcc-${VERSION}.tar.bz2 *

This fails during the 'make' stage, with the following error:

E:\Temp\mingw\msys\1.0\mingw\bin\ld.exe: cannot find dllcrt2.o: No such file or
collect2: ld returned 1 exit status
make[3]: *** [libgcc_s.dll] Error 1
make[3]: Leaving directory `/e/temp/gcc/build/mingw32/libgcc'
make[2]: *** [all-stage1-target-libgcc] Error 2
make[2]: Leaving directory `/e/temp/gcc/build'
make[1]: *** [stage1-bubble] Error 2
make[1]: Leaving directory `/e/temp/gcc/build'
make: *** [all] Error 2

So, ld.exe can't find dllcrt2.o (which is in /mingw/lib).
Next I tried to use a version of binutils I built myself from source, using the following commands:

../src/configure                 \
  --prefix=${WORKDIR}/dest       \
  --with-lib-path=/mingw/lib     \
make install

However, this resulted in exactly the same error. How do I get ld.exe to look in /mingw/lib? Or is there some other mistake I am making in the whole process?
(I tried copying dllcrt2.o to the directory /e/temp/gcc/build/mingw32/libgcc, but this just produced a new error where ld.exe was unable to locate -lmingw32.)

I would highly appreciate any help on this subject. I'm getting very frustrated with it all.
Is there anyone who succeeds in building gcc from source, with MinGW/MSYS on Windows, and could show me how they do it?