3 # This is my script for building a complete MinGW cross-compiler toolchain
4 # that runs under Linux to produce executables that run under Windows. It
5 # probably works (or can easily be adapted to work) under any unix system.
7 # It is based in large part on Sam Lantinga's script, which in turn was
8 # based partly on Ray Kelm's script, which in turn was built on
9 # Mo Dejong's script for doing the same, but with some added fixes.
12 # 1. Adapted the script to the new packaging of MinGW GCC, which is
13 # currently split into core and auxiliary components.
14 # 2. The script now determines the GCC and BINUTILS directory name
15 # directly from the tar file contents. This gets around common
16 # problems due to the directory names not always following the
18 # 3. Grouped together and simplified the macros that users need to
20 # 4. Made optional components truly optional -- leave the
21 # corresponding archive names blank and they will be ignored.
22 # 5. Included an option to purge the installation directory before
23 # installing the current cross-compiler.
25 # NOTE: If you choose a destination directory for the installation (set
26 # in the macro PREFIX) for which you do not have write access, you will
27 # need to run this script with root (or equivalent) privileges.
30 # Updated by Igor Mikolic-Torreira <igormt@alumni.caltech.edu>
34 #-----------------------------------------------------
38 # You need to review and adjust the macros that follow
40 #-----------------------------------------------------
43 # Directory where cross-tools directory will be placed
45 TOPDIR=${DEPKGS:-${TOPDIR}}
46 # Cross tools directory name under TOPDIR
47 CROSSTOOLS=cross-tools
48 # What flavor of GCC cross-compiler are we building? under CROSSTOOLS
54 # Make TOPDIR absolute
56 TOPDIR=${DEPKGS:-${TOPDIR}}
58 cd ${cwd}/`dirname $0`
63 BUILDDIR=${TOPDIR}/${CROSSTOOLS}
65 # Where does the cross-compiler go?
66 # This should be the directory into which your cross-compiler
67 # will be installed. Remember that if you set this to a directory
68 # that only root has write access to, you will need to run this
71 PREFIX=${BUILDDIR}/${TARGET}
73 mkdir -p ${BUILDDIR}/${TARGET}
75 echo "Building cross tools in ${BUILDDIR} ..."
78 # Purge anything and everything already in the $PREFIX
79 #(also known as the destination or installation) directory?
80 # Set to "Y" to purge, any other value omits the purge step.
85 # Set the following to the files from the current MinGW release
86 # (or whichever MinGW release you wish to build and install)
87 # You need to set both the URL they will be downloaded from
88 # and the exact name of the individual component files.
90 #MINGW_URL="http://superb-west.dl.sourceforge.net/sourceforge/mingw"
91 MINGW_URL="http://www.bacula.org/cross-tools"
93 # GCC_CORE is required; the other components are optional.
94 # Set any you don't want to "". You need binutils,
95 # mingw runtime and w32api; do not ever set those to "".
97 GCC_CORE_ARCHIVE="gcc-core-3.4.5-20060117-1-src.tar.gz"
98 GCC_GPP_ARCHIVE="gcc-g++-3.4.5-20060117-1-src.tar.gz"
105 # GCC_G77_ARCHIVE="gcc-g77-3.4.2-20040916-1-src.tar.gz"
106 # GCC_OBJC_ARCHIVE="gcc-objc-3.4.2-20040916-1-src.tar.gz"
107 # GCC_JAVA_ARCHIVE="gcc-java-3.4.2-20040916-1-src.tar.gz"
109 BINUTILS_ARCHIVE="binutils-2.16.91-20060119-1-src.tar.gz"
110 BINUTILS_PATCH="binutils_texinfo_version.patch"
112 MINGW_ARCHIVE="mingw-runtime-3.9.tar.gz"
114 W32API_ARCHIVE="w32api-3.7.tar.gz"
116 MINGW_UTILS_ARCHIVE="mingw-utils-0.3-src.tar.gz"
118 # These are the files from the SDL website
119 # These are optional, set them to "" if you don't want them)
121 #SDL_URL="http://www.libsdl.org/extras/win32/common"
122 SDL_URL="http://www.bacula.org/cross-tools"
124 OPENGL_ARCHIVE="opengl-devel.tar.gz"
125 DIRECTX_ARCHIVE="directx-devel.tar.gz"
129 #-----------------------------------------------------
133 # The remainder of the script should not neet any edits
135 #-----------------------------------------------------
139 # Make sure these are initialized as we want them
146 # Set our build directory and where our sources will go
148 if [ "x$BUILDDIR" = "x" ]; then
149 # Default to the current directory
152 SRCDIR="$BUILDDIR/source"
155 # Need install directory first on the path so gcc can find binutils
157 PATH="$PREFIX/bin:$PATH"
161 #-----------------------------------------------------
163 # Functions that do most of the work
165 #-----------------------------------------------------
170 # Download a file from a given url, only if it is not present
173 # Make sure wget is installed
174 if test "x`which wget`" = "x" ; then
175 echo "You need to install wget."
178 download_file "$GCC_CORE_ARCHIVE" "$MINGW_URL"
179 if [ "x$GCC_GPP_ARCHIVE" != "x" ]; then
180 download_file "$GCC_GPP_ARCHIVE" "$MINGW_URL"
182 if [ "x$GCC_G77_ARCHIVE" != "x" ]; then
183 download_file "$GCC_G77_ARCHIVE" "$MINGW_URL"
185 if [ "x$GCC_OBJC_ARCHIVE" != "x" ]; then
186 download_file "$GCC_OBJC_ARCHIVE" "$MINGW_URL"
188 if [ "x$GCC_JAVA_ARCHIVE" != "x" ]; then
189 download_file "$GCC_JAVA_ARCHIVE" "$MINGW_URL"
191 if [ "x$GCC_ADA_ARCHIVE" != "x" ]; then
192 download_file "$GCC_ADA_ARCHIVE" "$MINGW_URL"
195 download_file "$BINUTILS_ARCHIVE" "$MINGW_URL"
196 download_file "$MINGW_ARCHIVE" "$MINGW_URL"
197 download_file "$W32API_ARCHIVE" "$MINGW_URL"
199 if [ "x$MINGW_UTILS_ARCHIVE" != "x" ]; then
200 download_file "$MINGW_UTILS_ARCHIVE" "$MINGW_URL"
203 if [ "x$OPENGL_ARCHIVE" != "x" ]; then
204 download_file "$OPENGL_ARCHIVE" "$SDL_URL"
206 if [ "x$DIRECTX_ARCHIVE" != "x" ]; then
207 download_file "$DIRECTX_ARCHIVE" "$SDL_URL"
215 if test ! -f $1 ; then
216 echo "Downloading $1"
218 if test ! -f $1 ; then
219 echo "Could not download $1"
223 echo "Found $1 in the srcdir $SRCDIR"
229 purge_existing_install()
231 echo "Purging the existing files in $PREFIX"
232 if cd "$PREFIX"; then
241 echo "Installing cross libs and includes"
242 mkdir -p "$PREFIX/$TARGET"
245 tar -xzf "$SRCDIR/$MINGW_ARCHIVE"
246 tar -xzf "$SRCDIR/$W32API_ARCHIVE"
248 if [ "x$OPENGL_ARCHIVE" != "x" ]; then
249 tar -xzf "$SRCDIR/$OPENGL_ARCHIVE"
252 if [ "x$DIRECTX_ARCHIVE" != "x" ]; then
253 tar -xzf "$SRCDIR/$DIRECTX_ARCHIVE"
263 BINUTILS=`tar -tzf "$SRCDIR/$BINUTILS_ARCHIVE" | head -n 1`
264 if [ "x${BINUTILS}" != "x./" ]; then
267 echo "Extracting binutils"
268 tar -xzf "$SRCDIR/$BINUTILS_ARCHIVE"
274 if [ "$BINUTILS_PATCH" != "" ]; then
275 echo "Patching binutils"
276 cd "$SRCDIR/$BINUTILS"
277 patch -p1 < "${cwd}/patches/$BINUTILS_PATCH"
286 rm -rf "binutils-$TARGET"
287 mkdir "binutils-$TARGET"
288 cd "binutils-$TARGET"
289 echo "Configuring binutils"
290 "$SRCDIR/$BINUTILS/configure" --prefix="$PREFIX" --target=$TARGET --disable-nls \
291 --with-gcc --with-gnu-as --with-gnu-ld --disable-shared 2>&1 > configure.log
298 cd "$BUILDDIR/binutils-$TARGET"
299 echo "Building binutils"
300 make CFLAGS="-O2 -fno-exceptions" LDFLAGS="-s" 2>&1 > make.log
301 if test $? -ne 0; then
302 echo "make of binutils failed - log available: binutils-$TARGET/make.log"
311 cd "$BUILDDIR/binutils-$TARGET"
312 echo "Installing binutils"
313 make install 2>&1 > make-install.log
314 if test $? -ne 0; then
315 echo "install of binutils failed - log available: binutils-$TARGET/make-install.log"
325 GCC=`tar -tzf "$SRCDIR/$GCC_CORE_ARCHIVE" | head -n 1`
327 echo "====== GCC ${GCC} ========="
328 echo "Extracting gcc"
329 tar -xzf "$SRCDIR/$GCC_CORE_ARCHIVE"
330 if [ "x$GCC_GPP_ARCHIVE" != "x" ]; then
331 GCC_LANGS=${GCC_LANGS}",c++"
332 tar -xzf "$SRCDIR/$GCC_GPP_ARCHIVE"
334 if [ "x$GCC_G77_ARCHIVE" != "x" ]; then
335 GCC_LANGS=${GCC_LANGS}",f77"
336 tar -xzf "$SRCDIR/$GCC_G77_ARCHIVE"
338 if [ "x$GCC_OBJC_ARCHIVE" != "x" ]; then
339 GCC_LANGS=${GCC_LANGS}",objc"
340 tar -xzf "$SRCDIR/$GCC_OBJC_ARCHIVE"
342 if [ "x$GCC_JAVA_ARCHIVE" != "x" ]; then
343 GCC_LANGS=${GCC_LANGS}",java"
344 tar -xzf "$SRCDIR/$GCC_JAVA_ARCHIVE"
346 if [ "x$GCC_ADA_ARCHIVE" != "x" ]; then
347 GCC_LANGS=${GCC_LANGS}",ada"
348 tar -xzf "$SRCDIR/$GCC_ADA_ARCHIVE"
356 if [ "$GCC_PATCH" != "" ]; then
359 patch -p1 < "$SRCDIR/$GCC_PATCH"
371 echo "Configuring gcc"
372 "$SRCDIR/$GCC/configure" -v \
373 --prefix="$PREFIX" --target=$TARGET \
374 --with-headers="$PREFIX/$TARGET/include" \
375 --with-gcc --with-gnu-ld --with-gnu-as \
376 --enable-threads --disable-nls --enable-languages=$GCC_LANGS \
377 --disable-win32-registry --disable-shared --enable-sjlj-exceptions --enable-libgcj \
378 --disable-java-awt --without-x --enable-java-gc=boehm --disable-libgcj-debug \
379 --enable-interpreter --enable-hash-synchronization --enable-libstdcxx-debug \
387 cd "$BUILDDIR/gcc-$TARGET"
389 make CFLAGS="-O2" CXXFLAGS="-O2" GCJFLAGS="-O2" LDFLAGS="-s" DEBUG_FLAGS="-g0" 2>&1 > make.log
390 if test $? -ne 0; then
391 echo "make of gcc failed - log available: gcc-$TARGET/make.log"
394 if [ "x$GCC_ADA" != "x" ]; then
396 make "CFLAGS=-O2" "LDFLAGS=-s" gnatlib_and_tools 2>&1 > make-gnatlib.log
397 if test $? -ne 0; then
398 echo "make of gnatlib and tools failed - log available: gcc-$TARGET/make-gnatlib.log"
408 cd "$BUILDDIR/gcc-$TARGET"
409 echo "Installing gcc"
410 make install 2>&1 > make-install.log
411 if test $? -ne 0; then
412 echo "install of gcc failed - log available: gcc-$TARGET/make-install.log"
419 extract_mingw_utils()
422 MINGW_UTILS=`tar -tzf "$SRCDIR/$MINGW_UTILS_ARCHIVE" | head -n 1`
423 if [ "x${MINGW_UTILS}" != "x./" ]; then
424 rm -rf "$MINGW_UTILS"
426 echo "Extracting mingw-utils"
427 tar -xzf "$SRCDIR/$MINGW_UTILS_ARCHIVE"
434 MINGW_UTILS=`tar -tzf "$SRCDIR/$MINGW_UTILS_ARCHIVE" | head -n 1`
435 cd "${SRCDIR}/${MINGW_UTILS}"
436 echo "Patching mingw-utils"
437 patch -p0 < ${SCRIPTDIR}/patches/mingw-utils.patch 2>&1 > patch.log
442 configure_mingw_utils()
448 echo "Configuring mingw-utils"
449 "$SRCDIR/$MINGW_UTILS/configure" --disable-nonportable --prefix="${PREFIX}/${TARGET}" 2>&1 > configure.log
456 cd "$BUILDDIR/mingw-utils"
457 echo "Building mingw-utils"
458 make CFLAGS="-O2 -fno-exceptions" LDFLAGS="-s" 2>&1 > make.log
459 if test $? -ne 0; then
460 echo "make of mingw-utils failed - log available: mingw-utils/make.log"
467 install_mingw_utils()
469 cd "$BUILDDIR/mingw-utils"
470 echo "Installing mingw-utils"
471 make install 2>&1 > make-install.log
472 if test $? -ne 0; then
473 echo "install of mingw-utils failed - log available: mingw-utils/make-install.log"
482 echo "Finalizing installation"
484 # remove gcc build headers
485 rm -rf "$PREFIX/$TARGET/sys-include"
487 # Add extra binary links
488 if [ ! -f "$PREFIX/$TARGET/bin/objdump" ]; then
489 ln "$PREFIX/bin/$TARGET-objdump" "$PREFIX/$TARGET/bin/objdump"
491 if [ ! -f "$PREFIX/$TARGET/bin/dllwrap" ]; then
492 ln "$PREFIX/bin/$TARGET-dllwrap" "$PREFIX/$TARGET/bin/dllwrap"
495 # make cc and c++ symlinks to gcc and g++
496 if [ ! -f "$PREFIX/$TARGET/bin/g++" ]; then
497 ln "$PREFIX/bin/$TARGET-g++" "$PREFIX/$TARGET/bin/g++"
499 if [ ! -f "$PREFIX/$TARGET/bin/cc" ]; then
500 ln -s "gcc" "$PREFIX/$TARGET/bin/cc"
502 if [ ! -f "$PREFIX/$TARGET/bin/c++" ]; then
503 ln -s "g++" "$PREFIX/$TARGET/bin/c++"
506 # strip all the binaries
507 ls "$PREFIX"/bin/* "$PREFIX/$TARGET"/bin/* | egrep -v '.dll$' | egrep -v 'gccbug$' |
512 echo "Installation complete!"
518 # Main part of the script
523 if [ "x$PURGE_DIR" = "xY" ]; then
524 purge_existing_install
541 if test -n "${MINGW_UTILS_ARCHIVE}"; then
544 configure_mingw_utils