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 # What flavor of GCC cross-compiler are we building?
47 # What directory will the cross-compiler be built in?
48 # This is the directory into which source archives will
49 # be downloaded, expanded, compiled, etc. You need to
50 # have write-access to this directory. If you leave it
51 # blank, it defaults to the current directory.
53 #BUILDDIR=/data/cross-tools-4.1.0
54 BUILDDIR=../../../mingw
57 # Now make builddir absolute
62 mkdir -p ${BUILDDIR}/${TARGET}
64 # Where does the cross-compiler go?
65 # This should be the directory into which your cross-compiler
66 # will be installed. Remember that if you set this to a directory
67 # that only root has write access to, you will need to run this
70 PREFIX=${BUILDDIR}/mingw32
72 # Purge anything and everything already in the $PREFIX
73 #(also known as the destination or installation) directory?
74 # Set to "Y" to purge, any other value omits the purge step.
79 # Set the following to the files from the current MinGW release
80 # (or whichever MinGW release you wish to build and install)
81 # You need to set both the URL they will be downloaded from
82 # and the exact name of the individual component files.
84 MINGW_URL="http://heanet.dl.sourceforge.net/sourceforge/mingw"
86 # GCC_CORE is required; the other components are optional.
87 # Set any you don't want to "". You need binutils,
88 # mingw runtime and w32api; do not ever set those to "".
90 GCC_CORE_ARCHIVE="gcc-core-3.4.5-20060117-1-src.tar.gz"
91 GCC_GPP_ARCHIVE="gcc-g++-3.4.5-20060117-1-src.tar.gz"
98 # GCC_G77_ARCHIVE="gcc-g77-3.4.2-20040916-1-src.tar.gz"
99 # GCC_OBJC_ARCHIVE="gcc-objc-3.4.2-20040916-1-src.tar.gz"
100 # GCC_JAVA_ARCHIVE="gcc-java-3.4.2-20040916-1-src.tar.gz"
102 BINUTILS_ARCHIVE="binutils-2.16.91-20060119-1-src.tar.gz"
104 MINGW_ARCHIVE="mingw-runtime-3.9.tar.gz"
106 W32API_ARCHIVE="w32api-3.7.tar.gz"
109 # These are the files from the SDL website
110 # These are optional, set them to "" if you don't want them)
112 SDL_URL="http://www.libsdl.org/extras/win32/common"
114 OPENGL_ARCHIVE="opengl-devel.tar.gz"
115 DIRECTX_ARCHIVE="directx-devel.tar.gz"
119 #-----------------------------------------------------
123 # The remainder of the script should not neet any edits
125 #-----------------------------------------------------
129 # Make sure these are initialized as we want them
136 # Set our build directory and where our sources will go
138 if [ "x$BUILDDIR" = "x" ]; then
139 # Default to the current directory
142 SRCDIR="$BUILDDIR/source"
145 # Need install directory first on the path so gcc can find binutils
147 PATH="$PREFIX/bin:$PATH"
151 #-----------------------------------------------------
153 # Functions that do most of the work
155 #-----------------------------------------------------
158 function download_files
160 # Download a file from a given url, only if it is not present
163 # Make sure wget is installed
164 if test "x`which wget`" = "x" ; then
165 echo "You need to install wget."
168 download_file "$GCC_CORE_ARCHIVE" "$MINGW_URL"
169 if [ "x$GCC_GPP_ARCHIVE" != "x" ]; then
170 download_file "$GCC_GPP_ARCHIVE" "$MINGW_URL"
172 if [ "x$GCC_G77_ARCHIVE" != "x" ]; then
173 download_file "$GCC_G77_ARCHIVE" "$MINGW_URL"
175 if [ "x$GCC_OBJC_ARCHIVE" != "x" ]; then
176 download_file "$GCC_OBJC_ARCHIVE" "$MINGW_URL"
178 if [ "x$GCC_JAVA_ARCHIVE" != "x" ]; then
179 download_file "$GCC_JAVA_ARCHIVE" "$MINGW_URL"
181 if [ "x$GCC_ADA_ARCHIVE" != "x" ]; then
182 download_file "$GCC_ADA_ARCHIVE" "$MINGW_URL"
185 download_file "$BINUTILS_ARCHIVE" "$MINGW_URL"
186 download_file "$MINGW_ARCHIVE" "$MINGW_URL"
187 download_file "$W32API_ARCHIVE" "$MINGW_URL"
189 if [ "x$OPENGL_ARCHIVE" != "x" ]; then
190 download_file "$OPENGL_ARCHIVE" "$SDL_URL"
192 if [ "x$DIRECTX_ARCHIVE" != "x" ]; then
193 download_file "$DIRECTX_ARCHIVE" "$SDL_URL"
198 function download_file
201 if test ! -f $1 ; then
202 echo "Downloading $1"
204 if test ! -f $1 ; then
205 echo "Could not download $1"
209 echo "Found $1 in the srcdir $SRCDIR"
215 function purge_existing_install
217 echo "Purging the existing files in $PREFIX"
218 if cd "$PREFIX"; then
225 function install_libs
227 echo "Installing cross libs and includes"
228 mkdir -p "$PREFIX/$TARGET"
231 tar -xzf "$SRCDIR/$MINGW_ARCHIVE"
232 tar -xzf "$SRCDIR/$W32API_ARCHIVE"
234 if [ "x$OPENGL_ARCHIVE" != "x" ]; then
235 tar -xzf "$SRCDIR/$OPENGL_ARCHIVE"
238 if [ "x$DIRECTX_ARCHIVE" != "x" ]; then
239 tar -xzf "$SRCDIR/$DIRECTX_ARCHIVE"
246 function extract_binutils
249 BINUTILS=`tar -tzf "$SRCDIR/$BINUTILS_ARCHIVE" | head -n 1`
250 if [ "x${BINUTILS}" != "x./" ]; then
253 echo "Extracting binutils"
254 tar -xzf "$SRCDIR/$BINUTILS_ARCHIVE"
259 function configure_binutils
262 rm -rf "binutils-$TARGET"
263 mkdir "binutils-$TARGET"
264 cd "binutils-$TARGET"
265 echo "Configuring binutils"
266 "$SRCDIR/$BINUTILS/configure" --prefix="$PREFIX" --target=$TARGET --disable-nls \
267 --with-gcc --with-gnu-as --with-gnu-ld --disable-shared &> configure.log
272 function build_binutils
274 cd "$BUILDDIR/binutils-$TARGET"
275 echo "Building binutils"
276 make CFLAGS="-O2 -fno-exceptions" LDFLAGS="-s" &> make.log
277 if test $? -ne 0; then
278 echo "make of binutils failed - log available: binutils-$TARGET/make.log"
285 function install_binutils
287 cd "$BUILDDIR/binutils-$TARGET"
288 echo "Installing binutils"
289 make install &> make-install.log
290 if test $? -ne 0; then
291 echo "install of binutils failed - log available: binutils-$TARGET/make-install.log"
301 GCC=`tar -tzf "$SRCDIR/$GCC_CORE_ARCHIVE" | head -n 1`
303 echo "====== GCC ${GCC} ========="
304 echo "Extracting gcc"
305 tar -xzf "$SRCDIR/$GCC_CORE_ARCHIVE"
306 if [ "x$GCC_GPP_ARCHIVE" != "x" ]; then
307 GCC_LANGS=${GCC_LANGS}",c++"
308 tar -xzf "$SRCDIR/$GCC_GPP_ARCHIVE"
310 if [ "x$GCC_G77_ARCHIVE" != "x" ]; then
311 GCC_LANGS=${GCC_LANGS}",f77"
312 tar -xzf "$SRCDIR/$GCC_G77_ARCHIVE"
314 if [ "x$GCC_OBJC_ARCHIVE" != "x" ]; then
315 GCC_LANGS=${GCC_LANGS}",objc"
316 tar -xzf "$SRCDIR/$GCC_OBJC_ARCHIVE"
318 if [ "x$GCC_JAVA_ARCHIVE" != "x" ]; then
319 GCC_LANGS=${GCC_LANGS}",java"
320 tar -xzf "$SRCDIR/$GCC_JAVA_ARCHIVE"
322 if [ "x$GCC_ADA_ARCHIVE" != "x" ]; then
323 GCC_LANGS=${GCC_LANGS}",ada"
324 tar -xzf "$SRCDIR/$GCC_ADA_ARCHIVE"
332 if [ "$GCC_PATCH" != "" ]; then
335 patch -p1 < "$SRCDIR/$GCC_PATCH"
341 function configure_gcc
347 echo "Configuring gcc"
348 "$SRCDIR/$GCC/configure" -v \
349 --prefix="$PREFIX" --target=$TARGET \
350 --with-headers="$PREFIX/$TARGET/include" \
351 --with-gcc --with-gnu-ld --with-gnu-as \
352 --enable-threads --disable-nls --enable-languages=$GCC_LANGS \
353 --disable-win32-registry --disable-shared --enable-sjlj-exceptions --enable-libgcj \
354 --disable-java-awt --without-x --enable-java-gc=boehm --disable-libgcj-debug \
355 --enable-interpreter --enable-hash-synchronization --enable-libstdcxx-debug \
363 cd "$BUILDDIR/gcc-$TARGET"
365 make CFLAGS="-O2" CXXFLAGS="-O2" GCJFLAGS="-O2" LDFLAGS="-s" DEBUG_FLAGS="-g0" &> make.log
366 if test $? -ne 0; then
367 echo "make of gcc failed - log available: gcc-$TARGET/make.log"
370 if [ "x$GCC_ADA" != "x" ]; then
372 make "CFLAGS=-O2" "LDFLAGS=-s" gnatlib_and_tools &> make-gnatlib.log
373 if test $? -ne 0; then
374 echo "make of gnatlib and tools failed - log available: gcc-$TARGET/make-gnatlib.log"
384 cd "$BUILDDIR/gcc-$TARGET"
385 echo "Installing gcc"
386 make install &> make-install.log
387 if test $? -ne 0; then
388 echo "install of gcc failed - log available: gcc-$TARGET/make-install.log"
395 function final_tweaks
397 echo "Finalizing installation"
399 # remove gcc build headers
400 rm -rf "$PREFIX/$TARGET/sys-include"
402 # Add extra binary links
403 if [ ! -f "$PREFIX/$TARGET/bin/objdump" ]; then
404 ln "$PREFIX/bin/$TARGET-objdump" "$PREFIX/$TARGET/bin/objdump"
407 # make cc and c++ symlinks to gcc and g++
408 if [ ! -f "$PREFIX/$TARGET/bin/g++" ]; then
409 ln "$PREFIX/bin/$TARGET-g++" "$PREFIX/$TARGET/bin/g++"
411 if [ ! -f "$PREFIX/$TARGET/bin/cc" ]; then
412 ln -s "gcc" "$PREFIX/$TARGET/bin/cc"
414 if [ ! -f "$PREFIX/$TARGET/bin/c++" ]; then
415 ln -s "g++" "$PREFIX/$TARGET/bin/c++"
418 # strip all the binaries
419 ls "$PREFIX"/bin/* "$PREFIX/$TARGET"/bin/* | egrep -v '.dll$' | egrep -v 'gccbug$' |
424 echo "Installation complete!"
430 # Main part of the script
435 if [ "x$PURGE_DIR" = "xY" ]; then
436 purge_existing_install