]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/win32/build-win32-cross-tools
Update mingw32 build instructions
[bacula/bacula] / bacula / src / win32 / build-win32-cross-tools
1 #!/bin/sh
2
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.
6 #
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.
10 #
11 # My changes:
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
17 #          expected patterns.
18 #       3. Grouped together and simplified the macros that users need to
19 #          define.
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.
24 #
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.
28 #
29 #
30 # Updated by Igor Mikolic-Torreira <igormt@alumni.caltech.edu>
31
32
33
34 #-----------------------------------------------------
35 #
36 # BEGIN USER SETTINGS
37 #
38 # You need to review and adjust the macros that follow
39 #
40 #-----------------------------------------------------
41
42
43 # Directory where cross-tools directory will be placed
44 TOPDIR=../../..
45 # Cross tools directory name under TOPDIR
46 CROSSTOOLS=cross-tools
47 # What flavor of GCC cross-compiler are we building? under CROSSTOOLS
48 TARGET=mingw32
49
50 cwd=`pwd`
51
52 cd ${TOPDIR}
53 # Make TOPDIR absolute 
54 TOPDIR=`pwd`
55
56 cd ${cwd}/`dirname $0`
57 SCRIPTDIR=`pwd`
58
59 cd ${cwd}
60
61 BUILDDIR=${TOPDIR}/${CROSSTOOLS}
62
63 # Where does the cross-compiler go?
64 # This should be the directory into which your cross-compiler
65 # will be installed.  Remember that if you set this to a directory
66 # that only root has write access to, you will need to run this
67 # script as root.
68
69 PREFIX=${BUILDDIR}/${TARGET}
70 mkdir -p ${BUILDDIR}
71 mkdir -p ${BUILDDIR}/${TARGET}
72
73 echo "Building cross tools in ${BUILDDIR} ..."
74 echo " "
75
76 # Purge anything and everything already in the $PREFIX
77 #(also known as the destination or installation) directory?
78 # Set to "Y" to purge, any other value omits the purge step.
79
80 PURGE_DIR="Y"
81
82
83 # Set the following to the files from the current MinGW release
84 # (or whichever MinGW release you wish to build and install)
85 # You need to set both the URL they will be downloaded from
86 # and the exact name of the individual component files.
87
88 MINGW_URL="http://heanet.dl.sourceforge.net/sourceforge/mingw"
89
90 # GCC_CORE is required; the other components are optional.
91 # Set any you don't want to "".  You need binutils,
92 # mingw runtime and w32api; do not ever set those to "".
93
94 GCC_CORE_ARCHIVE="gcc-core-3.4.5-20060117-1-src.tar.gz"
95 GCC_GPP_ARCHIVE="gcc-g++-3.4.5-20060117-1-src.tar.gz"
96 GCC_G77_ARCHIVE=""
97 GCC_OBJC_ARCHIVE=""
98 GCC_JAVA_ARCHIVE=""
99 GCC_ADA_ARCHIVE=""
100 GCC_PATCH=""
101
102 # GCC_G77_ARCHIVE="gcc-g77-3.4.2-20040916-1-src.tar.gz"
103 # GCC_OBJC_ARCHIVE="gcc-objc-3.4.2-20040916-1-src.tar.gz"
104 # GCC_JAVA_ARCHIVE="gcc-java-3.4.2-20040916-1-src.tar.gz"
105
106 BINUTILS_ARCHIVE="binutils-2.16.91-20060119-1-src.tar.gz"
107
108 MINGW_ARCHIVE="mingw-runtime-3.9.tar.gz"
109
110 W32API_ARCHIVE="w32api-3.7.tar.gz"
111
112 MINGW_UTILS_ARCHIVE="mingw-utils-0.3-src.tar.gz"
113
114 # These are the files from the SDL website
115 # These are optional, set them to "" if you don't want them)
116
117 SDL_URL="http://www.libsdl.org/extras/win32/common"
118
119 OPENGL_ARCHIVE="opengl-devel.tar.gz"
120 DIRECTX_ARCHIVE="directx-devel.tar.gz"
121
122
123
124 #-----------------------------------------------------
125 #
126 # END USER SETTINGS
127 #
128 # The remainder of the script should not neet any edits
129 #
130 #-----------------------------------------------------
131
132
133
134 # Make sure these are initialized as we want them
135
136 GCC_CORE=""
137 BINUTILS=""
138 GCC_LANGS="c"
139
140
141 # Set our build directory and where our sources will go
142
143 if [ "x$BUILDDIR" = "x" ]; then
144         # Default to the current directory
145         BUILDDIR=$(pwd)
146 fi
147 SRCDIR="$BUILDDIR/source"
148
149
150 # Need install directory first on the path so gcc can find binutils
151
152 PATH="$PREFIX/bin:$PATH"
153
154
155
156 #-----------------------------------------------------
157 #
158 # Functions that do most of the work
159 #
160 #-----------------------------------------------------
161
162
163 function download_files
164 {
165         # Download a file from a given url, only if it is not present
166         mkdir -p "$SRCDIR"
167
168         # Make sure wget is installed
169         if test "x`which wget`" = "x" ; then
170                 echo "You need to install wget."
171                 exit 1
172         fi
173         download_file "$GCC_CORE_ARCHIVE" "$MINGW_URL"
174         if [ "x$GCC_GPP_ARCHIVE" != "x" ]; then
175                 download_file "$GCC_GPP_ARCHIVE" "$MINGW_URL"
176         fi
177         if [ "x$GCC_G77_ARCHIVE" != "x" ]; then
178                 download_file "$GCC_G77_ARCHIVE" "$MINGW_URL"
179         fi
180         if [ "x$GCC_OBJC_ARCHIVE" != "x" ]; then
181                 download_file "$GCC_OBJC_ARCHIVE" "$MINGW_URL"
182         fi
183         if [ "x$GCC_JAVA_ARCHIVE" != "x" ]; then
184                 download_file "$GCC_JAVA_ARCHIVE" "$MINGW_URL"
185         fi
186         if [ "x$GCC_ADA_ARCHIVE" != "x" ]; then
187                 download_file "$GCC_ADA_ARCHIVE" "$MINGW_URL"
188         fi
189
190         download_file "$BINUTILS_ARCHIVE" "$MINGW_URL"
191         download_file "$MINGW_ARCHIVE" "$MINGW_URL"
192         download_file "$W32API_ARCHIVE" "$MINGW_URL"
193
194         if [ "x$MINGW_UTILS_ARCHIVE" != "x" ]; then
195                 download_file "$MINGW_UTILS_ARCHIVE" "$MINGW_URL"
196         fi
197
198         if [ "x$OPENGL_ARCHIVE" != "x" ]; then
199                 download_file "$OPENGL_ARCHIVE" "$SDL_URL"
200         fi
201         if [ "x$DIRECTX_ARCHIVE" != "x" ]; then
202                 download_file "$DIRECTX_ARCHIVE" "$SDL_URL"
203         fi
204 }
205
206
207 function download_file
208 {
209         cd "$SRCDIR"
210         if test ! -f $1 ; then
211                 echo "Downloading $1"
212                 wget "$2/$1"
213                 if test ! -f $1 ; then
214                         echo "Could not download $1"
215                         exit 1
216                 fi
217         else
218                 echo "Found $1 in the srcdir $SRCDIR"
219         fi
220         cd "$BUILDDIR"
221 }
222
223
224 function purge_existing_install
225 {
226         echo "Purging the existing files in $PREFIX"
227         if cd "$PREFIX"; then
228                 rm -rf *
229         fi
230         cd "$BUILDDIR"
231 }
232
233
234 function install_libs
235 {
236         echo "Installing cross libs and includes"
237         mkdir -p "$PREFIX/$TARGET"
238         cd "$PREFIX/$TARGET"
239
240         tar -xzf "$SRCDIR/$MINGW_ARCHIVE"
241         tar -xzf "$SRCDIR/$W32API_ARCHIVE"
242
243         if [ "x$OPENGL_ARCHIVE" != "x" ]; then
244                 tar -xzf "$SRCDIR/$OPENGL_ARCHIVE"
245         fi
246
247         if [ "x$DIRECTX_ARCHIVE" != "x" ]; then
248                 tar -xzf "$SRCDIR/$DIRECTX_ARCHIVE"
249         fi
250
251         cd "$BUILDDIR"
252 }
253
254
255 function extract_binutils
256 {
257         cd "$SRCDIR"
258         BINUTILS=`tar -tzf "$SRCDIR/$BINUTILS_ARCHIVE" | head -n 1`
259         if [ "x${BINUTILS}" != "x./" ]; then
260            rm -rf "$BINUTILS"
261         fi
262         echo "Extracting binutils"
263         tar -xzf "$SRCDIR/$BINUTILS_ARCHIVE"
264         cd "$BUILDDIR"
265 }
266
267
268 function configure_binutils
269 {
270         cd "$BUILDDIR"
271         rm -rf "binutils-$TARGET"
272         mkdir "binutils-$TARGET"
273         cd "binutils-$TARGET"
274         echo "Configuring binutils"
275         "$SRCDIR/$BINUTILS/configure" --prefix="$PREFIX" --target=$TARGET --disable-nls \
276                 --with-gcc --with-gnu-as --with-gnu-ld --disable-shared &> configure.log
277         cd "$BUILDDIR"
278 }
279
280
281 function build_binutils
282 {
283         cd "$BUILDDIR/binutils-$TARGET"
284         echo "Building binutils"
285         make CFLAGS="-O2 -fno-exceptions" LDFLAGS="-s" &> make.log
286         if test $? -ne 0; then
287                 echo "make of binutils failed - log available: binutils-$TARGET/make.log"
288                 exit 1
289         fi
290         cd "$BUILDDIR"
291 }
292
293
294 function install_binutils
295 {
296         cd "$BUILDDIR/binutils-$TARGET"
297         echo "Installing binutils"
298         make install &> make-install.log
299         if test $? -ne 0; then
300                 echo "install of binutils failed - log available: binutils-$TARGET/make-install.log"
301                 exit 1
302         fi
303         cd "$BUILDDIR"
304 }
305
306
307 function extract_gcc
308 {
309         cd "$SRCDIR"
310         GCC=`tar -tzf "$SRCDIR/$GCC_CORE_ARCHIVE" | head -n 1`
311 #       rm -rf "$GCC"
312         echo "====== GCC ${GCC} ========="
313         echo "Extracting gcc"
314         tar -xzf "$SRCDIR/$GCC_CORE_ARCHIVE"
315         if [ "x$GCC_GPP_ARCHIVE" != "x" ]; then
316                 GCC_LANGS=${GCC_LANGS}",c++"
317                 tar -xzf "$SRCDIR/$GCC_GPP_ARCHIVE"
318         fi
319         if [ "x$GCC_G77_ARCHIVE" != "x" ]; then
320                 GCC_LANGS=${GCC_LANGS}",f77"
321                 tar -xzf "$SRCDIR/$GCC_G77_ARCHIVE"
322         fi
323         if [ "x$GCC_OBJC_ARCHIVE" != "x" ]; then
324                 GCC_LANGS=${GCC_LANGS}",objc"
325                 tar -xzf "$SRCDIR/$GCC_OBJC_ARCHIVE"
326         fi
327         if [ "x$GCC_JAVA_ARCHIVE" != "x" ]; then
328                 GCC_LANGS=${GCC_LANGS}",java"
329                 tar -xzf "$SRCDIR/$GCC_JAVA_ARCHIVE"
330         fi
331         if [ "x$GCC_ADA_ARCHIVE" != "x" ]; then
332                 GCC_LANGS=${GCC_LANGS}",ada"
333                 tar -xzf "$SRCDIR/$GCC_ADA_ARCHIVE"
334         fi
335         cd "$BUILDDIR"
336 }
337
338
339 function patch_gcc
340 {
341         if [ "$GCC_PATCH" != "" ]; then
342                 echo "Patching gcc"
343                 cd "$SRCDIR/$GCC"
344                 patch -p1 < "$SRCDIR/$GCC_PATCH"
345                 cd "$BUILDDIR"
346         fi
347 }
348
349
350 function configure_gcc
351 {
352         cd "$BUILDDIR"
353         rm -rf "gcc-$TARGET"
354         mkdir "gcc-$TARGET"
355         cd "gcc-$TARGET"
356         echo "Configuring gcc"
357         "$SRCDIR/$GCC/configure" -v \
358                 --prefix="$PREFIX" --target=$TARGET \
359                 --with-headers="$PREFIX/$TARGET/include" \
360                 --with-gcc --with-gnu-ld --with-gnu-as \
361                 --enable-threads --disable-nls --enable-languages=$GCC_LANGS \
362                 --disable-win32-registry --disable-shared --enable-sjlj-exceptions --enable-libgcj \
363                 --disable-java-awt --without-x --enable-java-gc=boehm --disable-libgcj-debug \
364                 --enable-interpreter --enable-hash-synchronization --enable-libstdcxx-debug \
365                 &> configure.log
366         cd "$BUILDDIR"
367 }
368
369
370 function build_gcc
371 {
372         cd "$BUILDDIR/gcc-$TARGET"
373         echo "Building gcc"
374         make CFLAGS="-O2" CXXFLAGS="-O2" GCJFLAGS="-O2" LDFLAGS="-s" DEBUG_FLAGS="-g0" &> make.log
375         if test $? -ne 0; then
376                 echo "make of gcc failed - log available: gcc-$TARGET/make.log"
377                 exit 1
378         fi
379         if [ "x$GCC_ADA" != "x" ]; then
380                 cd gcc
381                 make "CFLAGS=-O2" "LDFLAGS=-s" gnatlib_and_tools &> make-gnatlib.log
382                 if test $? -ne 0; then
383                         echo "make of gnatlib and tools failed - log available: gcc-$TARGET/make-gnatlib.log"
384                         exit 1
385                 fi
386         fi
387         cd "$BUILDDIR"
388 }
389
390
391 function install_gcc
392 {
393         cd "$BUILDDIR/gcc-$TARGET"
394         echo "Installing gcc"
395         make install &> make-install.log
396         if test $? -ne 0; then
397                 echo "install of gcc failed - log available: gcc-$TARGET/make-install.log"
398                 exit 1
399         fi
400         cd "$BUILDDIR"
401 }
402
403
404 function extract_mingw_utils
405 {
406         cd "$SRCDIR"
407         MINGW_UTILS=`tar -tzf "$SRCDIR/$MINGW_UTILS_ARCHIVE" | head -n 1`
408         if [ "x${MINGW_UTILS}" != "x./" ]; then
409            rm -rf "$MINGW_UTILS"
410         fi
411         echo "Extracting mingw-utils"
412         tar -xzf "$SRCDIR/$MINGW_UTILS_ARCHIVE"
413         cd "$BUILDDIR"
414 }
415
416
417 function patch_mingw_utils
418 {
419         MINGW_UTILS=`tar -tzf "$SRCDIR/$MINGW_UTILS_ARCHIVE" | head -n 1`
420         cd "${SRCDIR}/${MINGW_UTILS}"
421         echo "Patching mingw-utils"
422         patch -p0 < ${SCRIPTDIR}/patches/mingw-utils.patch &> patch.log
423         cd "$BUILDDIR"
424 }
425
426
427 function configure_mingw_utils
428 {
429         cd "$BUILDDIR"
430         rm -rf "mingw-utils"
431         mkdir "mingw-utils"
432         cd "mingw-utils"
433         echo "Configuring mingw-utils"
434         "$SRCDIR/$MINGW_UTILS/configure" --disable-nonportable --prefix="${PREFIX}/${TARGET}" &> configure.log
435         cd "$BUILDDIR"
436 }
437
438
439 function build_mingw_utils
440 {
441         cd "$BUILDDIR/mingw-utils"
442         echo "Building mingw-utils"
443         make CFLAGS="-O2 -fno-exceptions" LDFLAGS="-s" &> make.log
444         if test $? -ne 0; then
445                 echo "make of mingw-utils failed - log available: mingw-utils/make.log"
446                 exit 1
447         fi
448         cd "$BUILDDIR"
449 }
450
451
452 function install_mingw_utils
453 {
454         cd "$BUILDDIR/mingw-utils"
455         echo "Installing mingw-utils"
456         make install &> make-install.log
457         if test $? -ne 0; then
458                 echo "install of mingw-utils failed - log available: mingw-utils/make-install.log"
459                 exit 1
460         fi
461         cd "$BUILDDIR"
462 }
463
464
465 function final_tweaks
466 {
467         echo "Finalizing installation"
468
469         # remove gcc build headers
470         rm -rf "$PREFIX/$TARGET/sys-include"
471
472         # Add extra binary links
473         if [ ! -f "$PREFIX/$TARGET/bin/objdump" ]; then
474                 ln "$PREFIX/bin/$TARGET-objdump" "$PREFIX/$TARGET/bin/objdump"
475         fi
476         if [ ! -f "$PREFIX/$TARGET/bin/dllwrap" ]; then
477                 ln "$PREFIX/bin/$TARGET-dllwrap" "$PREFIX/$TARGET/bin/dllwrap"
478         fi
479
480         # make cc and c++ symlinks to gcc and g++
481         if [ ! -f "$PREFIX/$TARGET/bin/g++" ]; then
482                 ln "$PREFIX/bin/$TARGET-g++" "$PREFIX/$TARGET/bin/g++"
483         fi
484         if [ ! -f "$PREFIX/$TARGET/bin/cc" ]; then
485                 ln -s "gcc" "$PREFIX/$TARGET/bin/cc"
486         fi
487         if [ ! -f "$PREFIX/$TARGET/bin/c++" ]; then
488                 ln -s "g++" "$PREFIX/$TARGET/bin/c++"
489         fi
490
491         # strip all the binaries
492         ls "$PREFIX"/bin/* "$PREFIX/$TARGET"/bin/* | egrep -v '.dll$' | egrep -v 'gccbug$' |
493         while read file; do
494                 strip "$file"
495         done
496
497         echo "Installation complete!"
498 }
499
500
501
502 #
503 # Main part of the script
504 #
505
506 download_files
507
508 if [ "x$PURGE_DIR" = "xY" ]; then
509         purge_existing_install
510 fi
511
512 install_libs
513
514 extract_binutils
515 configure_binutils
516 build_binutils
517 install_binutils
518
519 extract_gcc
520 patch_gcc
521 configure_gcc
522 build_gcc
523 install_gcc
524
525 if test -n "${MINGW_UTILS_ARCHIVE}"; then
526         extract_mingw_utils
527         patch_mingw_utils
528         configure_mingw_utils
529         build_mingw_utils
530         install_mingw_utils
531 fi
532
533 final_tweaks
534
535
536 #
537 # End
538 #