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 # Cross tools directory name under TOPDIR
46 CROSSTOOLS=cross-tools
47 # What flavor of GCC cross-compiler are we building? under CROSSTOOLS
52 # Make TOPDIR absolute
54 BUILDDIR=${TOPDIR}/${CROSSTOOLS}
58 # Where does the cross-compiler go?
59 # This should be the directory into which your cross-compiler
60 # will be installed. Remember that if you set this to a directory
61 # that only root has write access to, you will need to run this
64 PREFIX=${BUILDDIR}/${TARGET}
66 mkdir -p ${BUILDDIR}/${TARGET}
68 echo "Building cross tools in ${BUILDDIR} ..."
71 # Purge anything and everything already in the $PREFIX
72 #(also known as the destination or installation) directory?
73 # Set to "Y" to purge, any other value omits the purge step.
78 # Set the following to the files from the current MinGW release
79 # (or whichever MinGW release you wish to build and install)
80 # You need to set both the URL they will be downloaded from
81 # and the exact name of the individual component files.
83 MINGW_URL="http://heanet.dl.sourceforge.net/sourceforge/mingw"
85 # GCC_CORE is required; the other components are optional.
86 # Set any you don't want to "". You need binutils,
87 # mingw runtime and w32api; do not ever set those to "".
89 GCC_CORE_ARCHIVE="gcc-core-3.4.5-20060117-1-src.tar.gz"
90 GCC_GPP_ARCHIVE="gcc-g++-3.4.5-20060117-1-src.tar.gz"
97 # GCC_G77_ARCHIVE="gcc-g77-3.4.2-20040916-1-src.tar.gz"
98 # GCC_OBJC_ARCHIVE="gcc-objc-3.4.2-20040916-1-src.tar.gz"
99 # GCC_JAVA_ARCHIVE="gcc-java-3.4.2-20040916-1-src.tar.gz"
101 BINUTILS_ARCHIVE="binutils-2.16.91-20060119-1-src.tar.gz"
103 MINGW_ARCHIVE="mingw-runtime-3.9.tar.gz"
105 W32API_ARCHIVE="w32api-3.7.tar.gz"
108 # These are the files from the SDL website
109 # These are optional, set them to "" if you don't want them)
111 SDL_URL="http://www.libsdl.org/extras/win32/common"
113 OPENGL_ARCHIVE="opengl-devel.tar.gz"
114 DIRECTX_ARCHIVE="directx-devel.tar.gz"
118 #-----------------------------------------------------
122 # The remainder of the script should not neet any edits
124 #-----------------------------------------------------
128 # Make sure these are initialized as we want them
135 # Set our build directory and where our sources will go
137 if [ "x$BUILDDIR" = "x" ]; then
138 # Default to the current directory
141 SRCDIR="$BUILDDIR/source"
144 # Need install directory first on the path so gcc can find binutils
146 PATH="$PREFIX/bin:$PATH"
150 #-----------------------------------------------------
152 # Functions that do most of the work
154 #-----------------------------------------------------
157 function download_files
159 # Download a file from a given url, only if it is not present
162 # Make sure wget is installed
163 if test "x`which wget`" = "x" ; then
164 echo "You need to install wget."
167 download_file "$GCC_CORE_ARCHIVE" "$MINGW_URL"
168 if [ "x$GCC_GPP_ARCHIVE" != "x" ]; then
169 download_file "$GCC_GPP_ARCHIVE" "$MINGW_URL"
171 if [ "x$GCC_G77_ARCHIVE" != "x" ]; then
172 download_file "$GCC_G77_ARCHIVE" "$MINGW_URL"
174 if [ "x$GCC_OBJC_ARCHIVE" != "x" ]; then
175 download_file "$GCC_OBJC_ARCHIVE" "$MINGW_URL"
177 if [ "x$GCC_JAVA_ARCHIVE" != "x" ]; then
178 download_file "$GCC_JAVA_ARCHIVE" "$MINGW_URL"
180 if [ "x$GCC_ADA_ARCHIVE" != "x" ]; then
181 download_file "$GCC_ADA_ARCHIVE" "$MINGW_URL"
184 download_file "$BINUTILS_ARCHIVE" "$MINGW_URL"
185 download_file "$MINGW_ARCHIVE" "$MINGW_URL"
186 download_file "$W32API_ARCHIVE" "$MINGW_URL"
188 if [ "x$OPENGL_ARCHIVE" != "x" ]; then
189 download_file "$OPENGL_ARCHIVE" "$SDL_URL"
191 if [ "x$DIRECTX_ARCHIVE" != "x" ]; then
192 download_file "$DIRECTX_ARCHIVE" "$SDL_URL"
197 function download_file
200 if test ! -f $1 ; then
201 echo "Downloading $1"
203 if test ! -f $1 ; then
204 echo "Could not download $1"
208 echo "Found $1 in the srcdir $SRCDIR"
214 function purge_existing_install
216 echo "Purging the existing files in $PREFIX"
217 if cd "$PREFIX"; then
224 function install_libs
226 echo "Installing cross libs and includes"
227 mkdir -p "$PREFIX/$TARGET"
230 tar -xzf "$SRCDIR/$MINGW_ARCHIVE"
231 tar -xzf "$SRCDIR/$W32API_ARCHIVE"
233 if [ "x$OPENGL_ARCHIVE" != "x" ]; then
234 tar -xzf "$SRCDIR/$OPENGL_ARCHIVE"
237 if [ "x$DIRECTX_ARCHIVE" != "x" ]; then
238 tar -xzf "$SRCDIR/$DIRECTX_ARCHIVE"
245 function extract_binutils
248 BINUTILS=`tar -tzf "$SRCDIR/$BINUTILS_ARCHIVE" | head -n 1`
249 if [ "x${BINUTILS}" != "x./" ]; then
252 echo "Extracting binutils"
253 tar -xzf "$SRCDIR/$BINUTILS_ARCHIVE"
258 function configure_binutils
261 rm -rf "binutils-$TARGET"
262 mkdir "binutils-$TARGET"
263 cd "binutils-$TARGET"
264 echo "Configuring binutils"
265 "$SRCDIR/$BINUTILS/configure" --prefix="$PREFIX" --target=$TARGET --disable-nls \
266 --with-gcc --with-gnu-as --with-gnu-ld --disable-shared &> configure.log
271 function build_binutils
273 cd "$BUILDDIR/binutils-$TARGET"
274 echo "Building binutils"
275 make CFLAGS="-O2 -fno-exceptions" LDFLAGS="-s" &> make.log
276 if test $? -ne 0; then
277 echo "make of binutils failed - log available: binutils-$TARGET/make.log"
284 function install_binutils
286 cd "$BUILDDIR/binutils-$TARGET"
287 echo "Installing binutils"
288 make install &> make-install.log
289 if test $? -ne 0; then
290 echo "install of binutils failed - log available: binutils-$TARGET/make-install.log"
300 GCC=`tar -tzf "$SRCDIR/$GCC_CORE_ARCHIVE" | head -n 1`
302 echo "====== GCC ${GCC} ========="
303 echo "Extracting gcc"
304 tar -xzf "$SRCDIR/$GCC_CORE_ARCHIVE"
305 if [ "x$GCC_GPP_ARCHIVE" != "x" ]; then
306 GCC_LANGS=${GCC_LANGS}",c++"
307 tar -xzf "$SRCDIR/$GCC_GPP_ARCHIVE"
309 if [ "x$GCC_G77_ARCHIVE" != "x" ]; then
310 GCC_LANGS=${GCC_LANGS}",f77"
311 tar -xzf "$SRCDIR/$GCC_G77_ARCHIVE"
313 if [ "x$GCC_OBJC_ARCHIVE" != "x" ]; then
314 GCC_LANGS=${GCC_LANGS}",objc"
315 tar -xzf "$SRCDIR/$GCC_OBJC_ARCHIVE"
317 if [ "x$GCC_JAVA_ARCHIVE" != "x" ]; then
318 GCC_LANGS=${GCC_LANGS}",java"
319 tar -xzf "$SRCDIR/$GCC_JAVA_ARCHIVE"
321 if [ "x$GCC_ADA_ARCHIVE" != "x" ]; then
322 GCC_LANGS=${GCC_LANGS}",ada"
323 tar -xzf "$SRCDIR/$GCC_ADA_ARCHIVE"
331 if [ "$GCC_PATCH" != "" ]; then
334 patch -p1 < "$SRCDIR/$GCC_PATCH"
340 function configure_gcc
346 echo "Configuring gcc"
347 "$SRCDIR/$GCC/configure" -v \
348 --prefix="$PREFIX" --target=$TARGET \
349 --with-headers="$PREFIX/$TARGET/include" \
350 --with-gcc --with-gnu-ld --with-gnu-as \
351 --enable-threads --disable-nls --enable-languages=$GCC_LANGS \
352 --disable-win32-registry --disable-shared --enable-sjlj-exceptions --enable-libgcj \
353 --disable-java-awt --without-x --enable-java-gc=boehm --disable-libgcj-debug \
354 --enable-interpreter --enable-hash-synchronization --enable-libstdcxx-debug \
362 cd "$BUILDDIR/gcc-$TARGET"
364 make CFLAGS="-O2" CXXFLAGS="-O2" GCJFLAGS="-O2" LDFLAGS="-s" DEBUG_FLAGS="-g0" &> make.log
365 if test $? -ne 0; then
366 echo "make of gcc failed - log available: gcc-$TARGET/make.log"
369 if [ "x$GCC_ADA" != "x" ]; then
371 make "CFLAGS=-O2" "LDFLAGS=-s" gnatlib_and_tools &> make-gnatlib.log
372 if test $? -ne 0; then
373 echo "make of gnatlib and tools failed - log available: gcc-$TARGET/make-gnatlib.log"
383 cd "$BUILDDIR/gcc-$TARGET"
384 echo "Installing gcc"
385 make install &> make-install.log
386 if test $? -ne 0; then
387 echo "install of gcc failed - log available: gcc-$TARGET/make-install.log"
394 function final_tweaks
396 echo "Finalizing installation"
398 # remove gcc build headers
399 rm -rf "$PREFIX/$TARGET/sys-include"
401 # Add extra binary links
402 if [ ! -f "$PREFIX/$TARGET/bin/objdump" ]; then
403 ln "$PREFIX/bin/$TARGET-objdump" "$PREFIX/$TARGET/bin/objdump"
406 # make cc and c++ symlinks to gcc and g++
407 if [ ! -f "$PREFIX/$TARGET/bin/g++" ]; then
408 ln "$PREFIX/bin/$TARGET-g++" "$PREFIX/$TARGET/bin/g++"
410 if [ ! -f "$PREFIX/$TARGET/bin/cc" ]; then
411 ln -s "gcc" "$PREFIX/$TARGET/bin/cc"
413 if [ ! -f "$PREFIX/$TARGET/bin/c++" ]; then
414 ln -s "g++" "$PREFIX/$TARGET/bin/c++"
417 # strip all the binaries
418 ls "$PREFIX"/bin/* "$PREFIX/$TARGET"/bin/* | egrep -v '.dll$' | egrep -v 'gccbug$' |
423 echo "Installation complete!"
429 # Main part of the script
434 if [ "x$PURGE_DIR" = "xY" ]; then
435 purge_existing_install