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