]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/win32/build-win32-cross-tools
Tweak fix MySQL quoting again :-(
[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 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
49 TARGET=mingw32
50
51 cwd=`pwd`
52
53 cd ${TOPDIR}
54 # Make TOPDIR absolute 
55 TOPDIR=`pwd`
56 TOPDIR=${DEPKGS:-${TOPDIR}}
57
58 cd ${cwd}/`dirname $0`
59 SCRIPTDIR=`pwd`
60
61 cd ${cwd}
62
63 BUILDDIR=${TOPDIR}/${CROSSTOOLS}
64
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
69 # script as root.
70
71 PREFIX=${BUILDDIR}/${TARGET}
72 mkdir -p ${BUILDDIR}
73 mkdir -p ${BUILDDIR}/${TARGET}
74
75 echo "Building cross tools in ${BUILDDIR} ..."
76 echo " "
77
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.
81
82 PURGE_DIR="Y"
83
84
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.
89
90 #MINGW_URL="http://superb-west.dl.sourceforge.net/sourceforge/mingw"
91 MINGW_URL="http://www.bacula.org/cross-tools"
92
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 "".
96
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"
99 GCC_G77_ARCHIVE=""
100 GCC_OBJC_ARCHIVE=""
101 GCC_JAVA_ARCHIVE=""
102 GCC_ADA_ARCHIVE=""
103 GCC_PATCH=""
104
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"
108
109 BINUTILS_ARCHIVE="binutils-2.16.91-20060119-1-src.tar.gz"
110 BINUTILS_PATCH="binutils_texinfo_version.patch"
111
112 MINGW_ARCHIVE="mingw-runtime-3.9.tar.gz"
113
114 W32API_ARCHIVE="w32api-3.7.tar.gz"
115
116 MINGW_UTILS_ARCHIVE="mingw-utils-0.3-src.tar.gz"
117
118 # These are the files from the SDL website
119 # These are optional, set them to "" if you don't want them)
120
121 #SDL_URL="http://www.libsdl.org/extras/win32/common"
122 SDL_URL="http://www.bacula.org/cross-tools"
123
124 OPENGL_ARCHIVE="opengl-devel.tar.gz"
125 DIRECTX_ARCHIVE="directx-devel.tar.gz"
126
127
128
129 #-----------------------------------------------------
130 #
131 # END USER SETTINGS
132 #
133 # The remainder of the script should not neet any edits
134 #
135 #-----------------------------------------------------
136
137
138
139 # Make sure these are initialized as we want them
140
141 GCC_CORE=""
142 BINUTILS=""
143 GCC_LANGS="c"
144
145
146 # Set our build directory and where our sources will go
147
148 if [ "x$BUILDDIR" = "x" ]; then
149         # Default to the current directory
150         BUILDDIR=$(pwd)
151 fi
152 SRCDIR="$BUILDDIR/source"
153
154
155 # Need install directory first on the path so gcc can find binutils
156
157 PATH="$PREFIX/bin:$PATH"
158
159
160
161 #-----------------------------------------------------
162 #
163 # Functions that do most of the work
164 #
165 #-----------------------------------------------------
166
167
168 download_files()
169 {
170         # Download a file from a given url, only if it is not present
171         mkdir -p "$SRCDIR"
172
173         # Make sure wget is installed
174         if test "x`which wget`" = "x" ; then
175                 echo "You need to install wget."
176                 exit 1
177         fi
178         download_file "$GCC_CORE_ARCHIVE" "$MINGW_URL"
179         if [ "x$GCC_GPP_ARCHIVE" != "x" ]; then
180                 download_file "$GCC_GPP_ARCHIVE" "$MINGW_URL"
181         fi
182         if [ "x$GCC_G77_ARCHIVE" != "x" ]; then
183                 download_file "$GCC_G77_ARCHIVE" "$MINGW_URL"
184         fi
185         if [ "x$GCC_OBJC_ARCHIVE" != "x" ]; then
186                 download_file "$GCC_OBJC_ARCHIVE" "$MINGW_URL"
187         fi
188         if [ "x$GCC_JAVA_ARCHIVE" != "x" ]; then
189                 download_file "$GCC_JAVA_ARCHIVE" "$MINGW_URL"
190         fi
191         if [ "x$GCC_ADA_ARCHIVE" != "x" ]; then
192                 download_file "$GCC_ADA_ARCHIVE" "$MINGW_URL"
193         fi
194
195         download_file "$BINUTILS_ARCHIVE" "$MINGW_URL"
196         download_file "$MINGW_ARCHIVE" "$MINGW_URL"
197         download_file "$W32API_ARCHIVE" "$MINGW_URL"
198
199         if [ "x$MINGW_UTILS_ARCHIVE" != "x" ]; then
200                 download_file "$MINGW_UTILS_ARCHIVE" "$MINGW_URL"
201         fi
202
203         if [ "x$OPENGL_ARCHIVE" != "x" ]; then
204                 download_file "$OPENGL_ARCHIVE" "$SDL_URL"
205         fi
206         if [ "x$DIRECTX_ARCHIVE" != "x" ]; then
207                 download_file "$DIRECTX_ARCHIVE" "$SDL_URL"
208         fi
209 }
210
211
212 download_file()
213 {
214         cd "$SRCDIR"
215         if test ! -f $1 ; then
216                 echo "Downloading $1"
217                 wget "$2/$1"
218                 if test ! -f $1 ; then
219                         echo "Could not download $1"
220                         exit 1
221                 fi
222         else
223                 echo "Found $1 in the srcdir $SRCDIR"
224         fi
225         cd "$BUILDDIR"
226 }
227
228
229 purge_existing_install()
230 {
231         echo "Purging the existing files in $PREFIX"
232         if cd "$PREFIX"; then
233                 rm -rf *
234         fi
235         cd "$BUILDDIR"
236 }
237
238
239 install_libs()
240 {
241         echo "Installing cross libs and includes"
242         mkdir -p "$PREFIX/$TARGET"
243         cd "$PREFIX/$TARGET"
244
245         tar -xzf "$SRCDIR/$MINGW_ARCHIVE"
246         tar -xzf "$SRCDIR/$W32API_ARCHIVE"
247
248         if [ "x$OPENGL_ARCHIVE" != "x" ]; then
249                 tar -xzf "$SRCDIR/$OPENGL_ARCHIVE"
250         fi
251
252         if [ "x$DIRECTX_ARCHIVE" != "x" ]; then
253                 tar -xzf "$SRCDIR/$DIRECTX_ARCHIVE"
254         fi
255
256         cd "$BUILDDIR"
257 }
258
259
260 extract_binutils()
261 {
262         cd "$SRCDIR"
263         BINUTILS=`tar -tzf "$SRCDIR/$BINUTILS_ARCHIVE" | head -n 1`
264         if [ "x${BINUTILS}" != "x./" ]; then
265            rm -rf "$BINUTILS"
266         fi
267         echo "Extracting binutils"
268         tar -xzf "$SRCDIR/$BINUTILS_ARCHIVE"
269         cd "$BUILDDIR"
270 }
271
272 patch_binutils()
273 {
274         if [ "$BINUTILS_PATCH" != "" ]; then
275                 echo "Patching binutils"
276                 cd "$SRCDIR/$BINUTILS"
277                 patch -p1 < "${cwd}/patches/$BINUTILS_PATCH"
278                 cd "$BUILDDIR"
279         fi
280 }
281
282
283 configure_binutils()
284 {
285         cd "$BUILDDIR"
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
292         cd "$BUILDDIR"
293 }
294
295
296 build_binutils()
297 {
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"
303                 exit 1
304         fi
305         cd "$BUILDDIR"
306 }
307
308
309 install_binutils()
310 {
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"
316                 exit 1
317         fi
318         cd "$BUILDDIR"
319 }
320
321
322 extract_gcc()
323 {
324         cd "$SRCDIR"
325         GCC=`tar -tzf "$SRCDIR/$GCC_CORE_ARCHIVE" | head -n 1`
326 #       rm -rf "$GCC"
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"
333         fi
334         if [ "x$GCC_G77_ARCHIVE" != "x" ]; then
335                 GCC_LANGS=${GCC_LANGS}",f77"
336                 tar -xzf "$SRCDIR/$GCC_G77_ARCHIVE"
337         fi
338         if [ "x$GCC_OBJC_ARCHIVE" != "x" ]; then
339                 GCC_LANGS=${GCC_LANGS}",objc"
340                 tar -xzf "$SRCDIR/$GCC_OBJC_ARCHIVE"
341         fi
342         if [ "x$GCC_JAVA_ARCHIVE" != "x" ]; then
343                 GCC_LANGS=${GCC_LANGS}",java"
344                 tar -xzf "$SRCDIR/$GCC_JAVA_ARCHIVE"
345         fi
346         if [ "x$GCC_ADA_ARCHIVE" != "x" ]; then
347                 GCC_LANGS=${GCC_LANGS}",ada"
348                 tar -xzf "$SRCDIR/$GCC_ADA_ARCHIVE"
349         fi
350         cd "$BUILDDIR"
351 }
352
353
354 patch_gcc()
355 {
356         if [ "$GCC_PATCH" != "" ]; then
357                 echo "Patching gcc"
358                 cd "$SRCDIR/$GCC"
359                 patch -p1 < "$SRCDIR/$GCC_PATCH"
360                 cd "$BUILDDIR"
361         fi
362 }
363
364
365 configure_gcc()
366 {
367         cd "$BUILDDIR"
368         rm -rf "gcc-$TARGET"
369         mkdir "gcc-$TARGET"
370         cd "gcc-$TARGET"
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 \
380                 2>&1 > configure.log
381         cd "$BUILDDIR"
382 }
383
384
385 build_gcc()
386 {
387         cd "$BUILDDIR/gcc-$TARGET"
388         echo "Building gcc"
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"
392                 exit 1
393         fi
394         if [ "x$GCC_ADA" != "x" ]; then
395                 cd gcc
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"
399                         exit 1
400                 fi
401         fi
402         cd "$BUILDDIR"
403 }
404
405
406 install_gcc()
407 {
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"
413                 exit 1
414         fi
415         cd "$BUILDDIR"
416 }
417
418
419 extract_mingw_utils()
420 {
421         cd "$SRCDIR"
422         MINGW_UTILS=`tar -tzf "$SRCDIR/$MINGW_UTILS_ARCHIVE" | head -n 1`
423         if [ "x${MINGW_UTILS}" != "x./" ]; then
424            rm -rf "$MINGW_UTILS"
425         fi
426         echo "Extracting mingw-utils"
427         tar -xzf "$SRCDIR/$MINGW_UTILS_ARCHIVE"
428         cd "$BUILDDIR"
429 }
430
431
432 patch_mingw_utils()
433 {
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
438         cd "$BUILDDIR"
439 }
440
441
442 configure_mingw_utils()
443 {
444         cd "$BUILDDIR"
445         rm -rf "mingw-utils"
446         mkdir "mingw-utils"
447         cd "mingw-utils"
448         echo "Configuring mingw-utils"
449         "$SRCDIR/$MINGW_UTILS/configure" --disable-nonportable --prefix="${PREFIX}/${TARGET}" 2>&1 > configure.log
450         cd "$BUILDDIR"
451 }
452
453
454 build_mingw_utils()
455 {
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"
461                 exit 1
462         fi
463         cd "$BUILDDIR"
464 }
465
466
467 install_mingw_utils()
468 {
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"
474                 exit 1
475         fi
476         cd "$BUILDDIR"
477 }
478
479
480 final_tweaks()
481 {
482         echo "Finalizing installation"
483
484         # remove gcc build headers
485         rm -rf "$PREFIX/$TARGET/sys-include"
486
487         # Add extra binary links
488         if [ ! -f "$PREFIX/$TARGET/bin/objdump" ]; then
489                 ln "$PREFIX/bin/$TARGET-objdump" "$PREFIX/$TARGET/bin/objdump"
490         fi
491         if [ ! -f "$PREFIX/$TARGET/bin/dllwrap" ]; then
492                 ln "$PREFIX/bin/$TARGET-dllwrap" "$PREFIX/$TARGET/bin/dllwrap"
493         fi
494
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++"
498         fi
499         if [ ! -f "$PREFIX/$TARGET/bin/cc" ]; then
500                 ln -s "gcc" "$PREFIX/$TARGET/bin/cc"
501         fi
502         if [ ! -f "$PREFIX/$TARGET/bin/c++" ]; then
503                 ln -s "g++" "$PREFIX/$TARGET/bin/c++"
504         fi
505
506         # strip all the binaries
507         ls "$PREFIX"/bin/* "$PREFIX/$TARGET"/bin/* | egrep -v '.dll$' | egrep -v 'gccbug$' |
508         while read file; do
509                 strip "$file"
510         done
511
512         echo "Installation complete!"
513 }
514
515
516
517 #
518 # Main part of the script
519 #
520
521 download_files
522
523 if [ "x$PURGE_DIR" = "xY" ]; then
524         purge_existing_install
525 fi
526
527 install_libs
528
529 extract_binutils
530 patch_binutils
531 configure_binutils
532 build_binutils
533 install_binutils
534
535 extract_gcc
536 patch_gcc
537 configure_gcc
538 build_gcc
539 install_gcc
540
541 if test -n "${MINGW_UTILS_ARCHIVE}"; then
542         extract_mingw_utils
543         patch_mingw_utils
544         configure_mingw_utils
545         build_mingw_utils
546         install_mingw_utils
547 fi
548
549 final_tweaks
550
551
552 #
553 # End
554 #