]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/win32/build-win32-cross-tools
Fix bug in build-win32-cross-tools script reported by Howard
[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 cd ${TOPDIR}
52 # Make TOPDIR absolute 
53 TOPDIR=`pwd`
54 BUILDDIR=${TOPDIR}/${CROSSTOOLS}
55
56 cd ${cwd}
57
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
62 # script as root.
63
64 PREFIX=${BUILDDIR}/${TARGET}
65 mkdir -p ${BUILDDIR}
66 mkdir -p ${BUILDDIR}/${TARGET}
67
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.
71
72 PURGE_DIR="Y"
73
74
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.
79
80 MINGW_URL="http://heanet.dl.sourceforge.net/sourceforge/mingw"
81
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 "".
85
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"
88 GCC_G77_ARCHIVE=""
89 GCC_OBJC_ARCHIVE=""
90 GCC_JAVA_ARCHIVE=""
91 GCC_ADA_ARCHIVE=""
92 GCC_PATCH=""
93
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"
97
98 BINUTILS_ARCHIVE="binutils-2.16.91-20060119-1-src.tar.gz"
99
100 MINGW_ARCHIVE="mingw-runtime-3.9.tar.gz"
101
102 W32API_ARCHIVE="w32api-3.7.tar.gz"
103
104
105 # These are the files from the SDL website
106 # These are optional, set them to "" if you don't want them)
107
108 SDL_URL="http://www.libsdl.org/extras/win32/common"
109
110 OPENGL_ARCHIVE="opengl-devel.tar.gz"
111 DIRECTX_ARCHIVE="directx-devel.tar.gz"
112
113
114
115 #-----------------------------------------------------
116 #
117 # END USER SETTINGS
118 #
119 # The remainder of the script should not neet any edits
120 #
121 #-----------------------------------------------------
122
123
124
125 # Make sure these are initialized as we want them
126
127 GCC_CORE=""
128 BINUTILS=""
129 GCC_LANGS="c"
130
131
132 # Set our build directory and where our sources will go
133
134 if [ "x$BUILDDIR" = "x" ]; then
135         # Default to the current directory
136         BUILDDIR=$(pwd)
137 fi
138 SRCDIR="$BUILDDIR/source"
139
140
141 # Need install directory first on the path so gcc can find binutils
142
143 PATH="$PREFIX/bin:$PATH"
144
145
146
147 #-----------------------------------------------------
148 #
149 # Functions that do most of the work
150 #
151 #-----------------------------------------------------
152
153
154 function download_files
155 {
156         # Download a file from a given url, only if it is not present
157         mkdir -p "$SRCDIR"
158
159         # Make sure wget is installed
160         if test "x`which wget`" = "x" ; then
161                 echo "You need to install wget."
162                 exit 1
163         fi
164         download_file "$GCC_CORE_ARCHIVE" "$MINGW_URL"
165         if [ "x$GCC_GPP_ARCHIVE" != "x" ]; then
166                 download_file "$GCC_GPP_ARCHIVE" "$MINGW_URL"
167         fi
168         if [ "x$GCC_G77_ARCHIVE" != "x" ]; then
169                 download_file "$GCC_G77_ARCHIVE" "$MINGW_URL"
170         fi
171         if [ "x$GCC_OBJC_ARCHIVE" != "x" ]; then
172                 download_file "$GCC_OBJC_ARCHIVE" "$MINGW_URL"
173         fi
174         if [ "x$GCC_JAVA_ARCHIVE" != "x" ]; then
175                 download_file "$GCC_JAVA_ARCHIVE" "$MINGW_URL"
176         fi
177         if [ "x$GCC_ADA_ARCHIVE" != "x" ]; then
178                 download_file "$GCC_ADA_ARCHIVE" "$MINGW_URL"
179         fi
180
181         download_file "$BINUTILS_ARCHIVE" "$MINGW_URL"
182         download_file "$MINGW_ARCHIVE" "$MINGW_URL"
183         download_file "$W32API_ARCHIVE" "$MINGW_URL"
184
185         if [ "x$OPENGL_ARCHIVE" != "x" ]; then
186                 download_file "$OPENGL_ARCHIVE" "$SDL_URL"
187         fi
188         if [ "x$DIRECTX_ARCHIVE" != "x" ]; then
189                 download_file "$DIRECTX_ARCHIVE" "$SDL_URL"
190         fi
191 }
192
193
194 function download_file
195 {
196         cd "$SRCDIR"
197         if test ! -f $1 ; then
198                 echo "Downloading $1"
199                 wget "$2/$1"
200                 if test ! -f $1 ; then
201                         echo "Could not download $1"
202                         exit 1
203                 fi
204         else
205                 echo "Found $1 in the srcdir $SRCDIR"
206         fi
207         cd "$BUILDDIR"
208 }
209
210
211 function purge_existing_install
212 {
213         echo "Purging the existing files in $PREFIX"
214         if cd "$PREFIX"; then
215                 rm -rf *
216         fi
217         cd "$BUILDDIR"
218 }
219
220
221 function install_libs
222 {
223         echo "Installing cross libs and includes"
224         mkdir -p "$PREFIX/$TARGET"
225         cd "$PREFIX/$TARGET"
226
227         tar -xzf "$SRCDIR/$MINGW_ARCHIVE"
228         tar -xzf "$SRCDIR/$W32API_ARCHIVE"
229
230         if [ "x$OPENGL_ARCHIVE" != "x" ]; then
231                 tar -xzf "$SRCDIR/$OPENGL_ARCHIVE"
232         fi
233
234         if [ "x$DIRECTX_ARCHIVE" != "x" ]; then
235                 tar -xzf "$SRCDIR/$DIRECTX_ARCHIVE"
236         fi
237
238         cd "$BUILDDIR"
239 }
240
241
242 function extract_binutils
243 {
244         cd "$SRCDIR"
245         BINUTILS=`tar -tzf "$SRCDIR/$BINUTILS_ARCHIVE" | head -n 1`
246         if [ "x${BINUTILS}" != "x./" ]; then
247            rm -rf "$BINUTILS"
248         fi
249         echo "Extracting binutils"
250         tar -xzf "$SRCDIR/$BINUTILS_ARCHIVE"
251         cd "$BUILDDIR"
252 }
253
254
255 function configure_binutils
256 {
257         cd "$BUILDDIR"
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
264         cd "$BUILDDIR"
265 }
266
267
268 function build_binutils
269 {
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"
275                 exit 1
276         fi
277         cd "$BUILDDIR"
278 }
279
280
281 function install_binutils
282 {
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"
288                 exit 1
289         fi
290         cd "$BUILDDIR"
291 }
292
293
294 function extract_gcc
295 {
296         cd "$SRCDIR"
297         GCC=`tar -tzf "$SRCDIR/$GCC_CORE_ARCHIVE" | head -n 1`
298 #       rm -rf "$GCC"
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"
305         fi
306         if [ "x$GCC_G77_ARCHIVE" != "x" ]; then
307                 GCC_LANGS=${GCC_LANGS}",f77"
308                 tar -xzf "$SRCDIR/$GCC_G77_ARCHIVE"
309         fi
310         if [ "x$GCC_OBJC_ARCHIVE" != "x" ]; then
311                 GCC_LANGS=${GCC_LANGS}",objc"
312                 tar -xzf "$SRCDIR/$GCC_OBJC_ARCHIVE"
313         fi
314         if [ "x$GCC_JAVA_ARCHIVE" != "x" ]; then
315                 GCC_LANGS=${GCC_LANGS}",java"
316                 tar -xzf "$SRCDIR/$GCC_JAVA_ARCHIVE"
317         fi
318         if [ "x$GCC_ADA_ARCHIVE" != "x" ]; then
319                 GCC_LANGS=${GCC_LANGS}",ada"
320                 tar -xzf "$SRCDIR/$GCC_ADA_ARCHIVE"
321         fi
322         cd "$BUILDDIR"
323 }
324
325
326 function patch_gcc
327 {
328         if [ "$GCC_PATCH" != "" ]; then
329                 echo "Patching gcc"
330                 cd "$SRCDIR/$GCC"
331                 patch -p1 < "$SRCDIR/$GCC_PATCH"
332                 cd "$BUILDDIR"
333         fi
334 }
335
336
337 function configure_gcc
338 {
339         cd "$BUILDDIR"
340         rm -rf "gcc-$TARGET"
341         mkdir "gcc-$TARGET"
342         cd "gcc-$TARGET"
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 \
352                 &> configure.log
353         cd "$BUILDDIR"
354 }
355
356
357 function build_gcc
358 {
359         cd "$BUILDDIR/gcc-$TARGET"
360         echo "Building gcc"
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"
364                 exit 1
365         fi
366         if [ "x$GCC_ADA" != "x" ]; then
367                 cd gcc
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"
371                         exit 1
372                 fi
373         fi
374         cd "$BUILDDIR"
375 }
376
377
378 function install_gcc
379 {
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"
385                 exit 1
386         fi
387         cd "$BUILDDIR"
388 }
389
390
391 function final_tweaks
392 {
393         echo "Finalizing installation"
394
395         # remove gcc build headers
396         rm -rf "$PREFIX/$TARGET/sys-include"
397
398         # Add extra binary links
399         if [ ! -f "$PREFIX/$TARGET/bin/objdump" ]; then
400                 ln "$PREFIX/bin/$TARGET-objdump" "$PREFIX/$TARGET/bin/objdump"
401         fi
402
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++"
406         fi
407         if [ ! -f "$PREFIX/$TARGET/bin/cc" ]; then
408                 ln -s "gcc" "$PREFIX/$TARGET/bin/cc"
409         fi
410         if [ ! -f "$PREFIX/$TARGET/bin/c++" ]; then
411                 ln -s "g++" "$PREFIX/$TARGET/bin/c++"
412         fi
413
414         # strip all the binaries
415         ls "$PREFIX"/bin/* "$PREFIX/$TARGET"/bin/* | egrep -v '.dll$' | egrep -v 'gccbug$' |
416         while read file; do
417                 strip "$file"
418         done
419
420         echo "Installation complete!"
421 }
422
423
424
425 #
426 # Main part of the script
427 #
428
429 download_files
430
431 if [ "x$PURGE_DIR" = "xY" ]; then
432         purge_existing_install
433 fi
434
435 install_libs
436
437 extract_binutils
438 configure_binutils
439 build_binutils
440 install_binutils
441
442 extract_gcc
443 patch_gcc
444 configure_gcc
445 build_gcc
446 install_gcc
447
448 final_tweaks
449
450
451 #
452 # End
453 #