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 # Purge anything and everything already in the $PREFIX
69 #(also known as the destination or installation) directory?
70 # Set to "Y" to purge, any other value omits the purge step.
75 # Set the following to the files from the current MinGW release
76 # (or whichever MinGW release you wish to build and install)
77 # You need to set both the URL they will be downloaded from
78 # and the exact name of the individual component files.
80 MINGW_URL="http://heanet.dl.sourceforge.net/sourceforge/mingw"
82 # GCC_CORE is required; the other components are optional.
83 # Set any you don't want to "". You need binutils,
84 # mingw runtime and w32api; do not ever set those to "".
86 GCC_CORE_ARCHIVE="gcc-core-3.4.5-20060117-1-src.tar.gz"
87 GCC_GPP_ARCHIVE="gcc-g++-3.4.5-20060117-1-src.tar.gz"
94 # GCC_G77_ARCHIVE="gcc-g77-3.4.2-20040916-1-src.tar.gz"
95 # GCC_OBJC_ARCHIVE="gcc-objc-3.4.2-20040916-1-src.tar.gz"
96 # GCC_JAVA_ARCHIVE="gcc-java-3.4.2-20040916-1-src.tar.gz"
98 BINUTILS_ARCHIVE="binutils-2.16.91-20060119-1-src.tar.gz"
100 MINGW_ARCHIVE="mingw-runtime-3.9.tar.gz"
102 W32API_ARCHIVE="w32api-3.7.tar.gz"
105 # These are the files from the SDL website
106 # These are optional, set them to "" if you don't want them)
108 SDL_URL="http://www.libsdl.org/extras/win32/common"
110 OPENGL_ARCHIVE="opengl-devel.tar.gz"
111 DIRECTX_ARCHIVE="directx-devel.tar.gz"
115 #-----------------------------------------------------
119 # The remainder of the script should not neet any edits
121 #-----------------------------------------------------
125 # Make sure these are initialized as we want them
132 # Set our build directory and where our sources will go
134 if [ "x$BUILDDIR" = "x" ]; then
135 # Default to the current directory
138 SRCDIR="$BUILDDIR/source"
141 # Need install directory first on the path so gcc can find binutils
143 PATH="$PREFIX/bin:$PATH"
147 #-----------------------------------------------------
149 # Functions that do most of the work
151 #-----------------------------------------------------
154 function download_files
156 # Download a file from a given url, only if it is not present
159 # Make sure wget is installed
160 if test "x`which wget`" = "x" ; then
161 echo "You need to install wget."
164 download_file "$GCC_CORE_ARCHIVE" "$MINGW_URL"
165 if [ "x$GCC_GPP_ARCHIVE" != "x" ]; then
166 download_file "$GCC_GPP_ARCHIVE" "$MINGW_URL"
168 if [ "x$GCC_G77_ARCHIVE" != "x" ]; then
169 download_file "$GCC_G77_ARCHIVE" "$MINGW_URL"
171 if [ "x$GCC_OBJC_ARCHIVE" != "x" ]; then
172 download_file "$GCC_OBJC_ARCHIVE" "$MINGW_URL"
174 if [ "x$GCC_JAVA_ARCHIVE" != "x" ]; then
175 download_file "$GCC_JAVA_ARCHIVE" "$MINGW_URL"
177 if [ "x$GCC_ADA_ARCHIVE" != "x" ]; then
178 download_file "$GCC_ADA_ARCHIVE" "$MINGW_URL"
181 download_file "$BINUTILS_ARCHIVE" "$MINGW_URL"
182 download_file "$MINGW_ARCHIVE" "$MINGW_URL"
183 download_file "$W32API_ARCHIVE" "$MINGW_URL"
185 if [ "x$OPENGL_ARCHIVE" != "x" ]; then
186 download_file "$OPENGL_ARCHIVE" "$SDL_URL"
188 if [ "x$DIRECTX_ARCHIVE" != "x" ]; then
189 download_file "$DIRECTX_ARCHIVE" "$SDL_URL"
194 function download_file
197 if test ! -f $1 ; then
198 echo "Downloading $1"
200 if test ! -f $1 ; then
201 echo "Could not download $1"
205 echo "Found $1 in the srcdir $SRCDIR"
211 function purge_existing_install
213 echo "Purging the existing files in $PREFIX"
214 if cd "$PREFIX"; then
221 function install_libs
223 echo "Installing cross libs and includes"
224 mkdir -p "$PREFIX/$TARGET"
227 tar -xzf "$SRCDIR/$MINGW_ARCHIVE"
228 tar -xzf "$SRCDIR/$W32API_ARCHIVE"
230 if [ "x$OPENGL_ARCHIVE" != "x" ]; then
231 tar -xzf "$SRCDIR/$OPENGL_ARCHIVE"
234 if [ "x$DIRECTX_ARCHIVE" != "x" ]; then
235 tar -xzf "$SRCDIR/$DIRECTX_ARCHIVE"
242 function extract_binutils
245 BINUTILS=`tar -tzf "$SRCDIR/$BINUTILS_ARCHIVE" | head -n 1`
246 if [ "x${BINUTILS}" != "x./" ]; then
249 echo "Extracting binutils"
250 tar -xzf "$SRCDIR/$BINUTILS_ARCHIVE"
255 function configure_binutils
258 rm -rf "binutils-$TARGET"
259 mkdir "binutils-$TARGET"
260 cd "binutils-$TARGET"
261 echo "Configuring binutils"
262 "$SRCDIR/$BINUTILS/configure" --prefix="$PREFIX" --target=$TARGET --disable-nls \
263 --with-gcc --with-gnu-as --with-gnu-ld --disable-shared &> configure.log
268 function build_binutils
270 cd "$BUILDDIR/binutils-$TARGET"
271 echo "Building binutils"
272 make CFLAGS="-O2 -fno-exceptions" LDFLAGS="-s" &> make.log
273 if test $? -ne 0; then
274 echo "make of binutils failed - log available: binutils-$TARGET/make.log"
281 function install_binutils
283 cd "$BUILDDIR/binutils-$TARGET"
284 echo "Installing binutils"
285 make install &> make-install.log
286 if test $? -ne 0; then
287 echo "install of binutils failed - log available: binutils-$TARGET/make-install.log"
297 GCC=`tar -tzf "$SRCDIR/$GCC_CORE_ARCHIVE" | head -n 1`
299 echo "====== GCC ${GCC} ========="
300 echo "Extracting gcc"
301 tar -xzf "$SRCDIR/$GCC_CORE_ARCHIVE"
302 if [ "x$GCC_GPP_ARCHIVE" != "x" ]; then
303 GCC_LANGS=${GCC_LANGS}",c++"
304 tar -xzf "$SRCDIR/$GCC_GPP_ARCHIVE"
306 if [ "x$GCC_G77_ARCHIVE" != "x" ]; then
307 GCC_LANGS=${GCC_LANGS}",f77"
308 tar -xzf "$SRCDIR/$GCC_G77_ARCHIVE"
310 if [ "x$GCC_OBJC_ARCHIVE" != "x" ]; then
311 GCC_LANGS=${GCC_LANGS}",objc"
312 tar -xzf "$SRCDIR/$GCC_OBJC_ARCHIVE"
314 if [ "x$GCC_JAVA_ARCHIVE" != "x" ]; then
315 GCC_LANGS=${GCC_LANGS}",java"
316 tar -xzf "$SRCDIR/$GCC_JAVA_ARCHIVE"
318 if [ "x$GCC_ADA_ARCHIVE" != "x" ]; then
319 GCC_LANGS=${GCC_LANGS}",ada"
320 tar -xzf "$SRCDIR/$GCC_ADA_ARCHIVE"
328 if [ "$GCC_PATCH" != "" ]; then
331 patch -p1 < "$SRCDIR/$GCC_PATCH"
337 function configure_gcc
343 echo "Configuring gcc"
344 "$SRCDIR/$GCC/configure" -v \
345 --prefix="$PREFIX" --target=$TARGET \
346 --with-headers="$PREFIX/$TARGET/include" \
347 --with-gcc --with-gnu-ld --with-gnu-as \
348 --enable-threads --disable-nls --enable-languages=$GCC_LANGS \
349 --disable-win32-registry --disable-shared --enable-sjlj-exceptions --enable-libgcj \
350 --disable-java-awt --without-x --enable-java-gc=boehm --disable-libgcj-debug \
351 --enable-interpreter --enable-hash-synchronization --enable-libstdcxx-debug \
359 cd "$BUILDDIR/gcc-$TARGET"
361 make CFLAGS="-O2" CXXFLAGS="-O2" GCJFLAGS="-O2" LDFLAGS="-s" DEBUG_FLAGS="-g0" &> make.log
362 if test $? -ne 0; then
363 echo "make of gcc failed - log available: gcc-$TARGET/make.log"
366 if [ "x$GCC_ADA" != "x" ]; then
368 make "CFLAGS=-O2" "LDFLAGS=-s" gnatlib_and_tools &> make-gnatlib.log
369 if test $? -ne 0; then
370 echo "make of gnatlib and tools failed - log available: gcc-$TARGET/make-gnatlib.log"
380 cd "$BUILDDIR/gcc-$TARGET"
381 echo "Installing gcc"
382 make install &> make-install.log
383 if test $? -ne 0; then
384 echo "install of gcc failed - log available: gcc-$TARGET/make-install.log"
391 function final_tweaks
393 echo "Finalizing installation"
395 # remove gcc build headers
396 rm -rf "$PREFIX/$TARGET/sys-include"
398 # Add extra binary links
399 if [ ! -f "$PREFIX/$TARGET/bin/objdump" ]; then
400 ln "$PREFIX/bin/$TARGET-objdump" "$PREFIX/$TARGET/bin/objdump"
403 # make cc and c++ symlinks to gcc and g++
404 if [ ! -f "$PREFIX/$TARGET/bin/g++" ]; then
405 ln "$PREFIX/bin/$TARGET-g++" "$PREFIX/$TARGET/bin/g++"
407 if [ ! -f "$PREFIX/$TARGET/bin/cc" ]; then
408 ln -s "gcc" "$PREFIX/$TARGET/bin/cc"
410 if [ ! -f "$PREFIX/$TARGET/bin/c++" ]; then
411 ln -s "g++" "$PREFIX/$TARGET/bin/c++"
414 # strip all the binaries
415 ls "$PREFIX"/bin/* "$PREFIX/$TARGET"/bin/* | egrep -v '.dll$' | egrep -v 'gccbug$' |
420 echo "Installation complete!"
426 # Main part of the script
431 if [ "x$PURGE_DIR" = "xY" ]; then
432 purge_existing_install