]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/win32/build-win32-cross-tools
Print cross tools directory name
[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 echo "Building cross tools in ${BUILDDIR} ..."
69 echo " "
70
71 # Purge anything and everything already in the $PREFIX
72 #(also known as the destination or installation) directory?
73 # Set to "Y" to purge, any other value omits the purge step.
74
75 PURGE_DIR="Y"
76
77
78 # Set the following to the files from the current MinGW release
79 # (or whichever MinGW release you wish to build and install)
80 # You need to set both the URL they will be downloaded from
81 # and the exact name of the individual component files.
82
83 MINGW_URL="http://heanet.dl.sourceforge.net/sourceforge/mingw"
84
85 # GCC_CORE is required; the other components are optional.
86 # Set any you don't want to "".  You need binutils,
87 # mingw runtime and w32api; do not ever set those to "".
88
89 GCC_CORE_ARCHIVE="gcc-core-3.4.5-20060117-1-src.tar.gz"
90 GCC_GPP_ARCHIVE="gcc-g++-3.4.5-20060117-1-src.tar.gz"
91 GCC_G77_ARCHIVE=""
92 GCC_OBJC_ARCHIVE=""
93 GCC_JAVA_ARCHIVE=""
94 GCC_ADA_ARCHIVE=""
95 GCC_PATCH=""
96
97 # GCC_G77_ARCHIVE="gcc-g77-3.4.2-20040916-1-src.tar.gz"
98 # GCC_OBJC_ARCHIVE="gcc-objc-3.4.2-20040916-1-src.tar.gz"
99 # GCC_JAVA_ARCHIVE="gcc-java-3.4.2-20040916-1-src.tar.gz"
100
101 BINUTILS_ARCHIVE="binutils-2.16.91-20060119-1-src.tar.gz"
102
103 MINGW_ARCHIVE="mingw-runtime-3.9.tar.gz"
104
105 W32API_ARCHIVE="w32api-3.7.tar.gz"
106
107
108 # These are the files from the SDL website
109 # These are optional, set them to "" if you don't want them)
110
111 SDL_URL="http://www.libsdl.org/extras/win32/common"
112
113 OPENGL_ARCHIVE="opengl-devel.tar.gz"
114 DIRECTX_ARCHIVE="directx-devel.tar.gz"
115
116
117
118 #-----------------------------------------------------
119 #
120 # END USER SETTINGS
121 #
122 # The remainder of the script should not neet any edits
123 #
124 #-----------------------------------------------------
125
126
127
128 # Make sure these are initialized as we want them
129
130 GCC_CORE=""
131 BINUTILS=""
132 GCC_LANGS="c"
133
134
135 # Set our build directory and where our sources will go
136
137 if [ "x$BUILDDIR" = "x" ]; then
138         # Default to the current directory
139         BUILDDIR=$(pwd)
140 fi
141 SRCDIR="$BUILDDIR/source"
142
143
144 # Need install directory first on the path so gcc can find binutils
145
146 PATH="$PREFIX/bin:$PATH"
147
148
149
150 #-----------------------------------------------------
151 #
152 # Functions that do most of the work
153 #
154 #-----------------------------------------------------
155
156
157 function download_files
158 {
159         # Download a file from a given url, only if it is not present
160         mkdir -p "$SRCDIR"
161
162         # Make sure wget is installed
163         if test "x`which wget`" = "x" ; then
164                 echo "You need to install wget."
165                 exit 1
166         fi
167         download_file "$GCC_CORE_ARCHIVE" "$MINGW_URL"
168         if [ "x$GCC_GPP_ARCHIVE" != "x" ]; then
169                 download_file "$GCC_GPP_ARCHIVE" "$MINGW_URL"
170         fi
171         if [ "x$GCC_G77_ARCHIVE" != "x" ]; then
172                 download_file "$GCC_G77_ARCHIVE" "$MINGW_URL"
173         fi
174         if [ "x$GCC_OBJC_ARCHIVE" != "x" ]; then
175                 download_file "$GCC_OBJC_ARCHIVE" "$MINGW_URL"
176         fi
177         if [ "x$GCC_JAVA_ARCHIVE" != "x" ]; then
178                 download_file "$GCC_JAVA_ARCHIVE" "$MINGW_URL"
179         fi
180         if [ "x$GCC_ADA_ARCHIVE" != "x" ]; then
181                 download_file "$GCC_ADA_ARCHIVE" "$MINGW_URL"
182         fi
183
184         download_file "$BINUTILS_ARCHIVE" "$MINGW_URL"
185         download_file "$MINGW_ARCHIVE" "$MINGW_URL"
186         download_file "$W32API_ARCHIVE" "$MINGW_URL"
187
188         if [ "x$OPENGL_ARCHIVE" != "x" ]; then
189                 download_file "$OPENGL_ARCHIVE" "$SDL_URL"
190         fi
191         if [ "x$DIRECTX_ARCHIVE" != "x" ]; then
192                 download_file "$DIRECTX_ARCHIVE" "$SDL_URL"
193         fi
194 }
195
196
197 function download_file
198 {
199         cd "$SRCDIR"
200         if test ! -f $1 ; then
201                 echo "Downloading $1"
202                 wget "$2/$1"
203                 if test ! -f $1 ; then
204                         echo "Could not download $1"
205                         exit 1
206                 fi
207         else
208                 echo "Found $1 in the srcdir $SRCDIR"
209         fi
210         cd "$BUILDDIR"
211 }
212
213
214 function purge_existing_install
215 {
216         echo "Purging the existing files in $PREFIX"
217         if cd "$PREFIX"; then
218                 rm -rf *
219         fi
220         cd "$BUILDDIR"
221 }
222
223
224 function install_libs
225 {
226         echo "Installing cross libs and includes"
227         mkdir -p "$PREFIX/$TARGET"
228         cd "$PREFIX/$TARGET"
229
230         tar -xzf "$SRCDIR/$MINGW_ARCHIVE"
231         tar -xzf "$SRCDIR/$W32API_ARCHIVE"
232
233         if [ "x$OPENGL_ARCHIVE" != "x" ]; then
234                 tar -xzf "$SRCDIR/$OPENGL_ARCHIVE"
235         fi
236
237         if [ "x$DIRECTX_ARCHIVE" != "x" ]; then
238                 tar -xzf "$SRCDIR/$DIRECTX_ARCHIVE"
239         fi
240
241         cd "$BUILDDIR"
242 }
243
244
245 function extract_binutils
246 {
247         cd "$SRCDIR"
248         BINUTILS=`tar -tzf "$SRCDIR/$BINUTILS_ARCHIVE" | head -n 1`
249         if [ "x${BINUTILS}" != "x./" ]; then
250            rm -rf "$BINUTILS"
251         fi
252         echo "Extracting binutils"
253         tar -xzf "$SRCDIR/$BINUTILS_ARCHIVE"
254         cd "$BUILDDIR"
255 }
256
257
258 function configure_binutils
259 {
260         cd "$BUILDDIR"
261         rm -rf "binutils-$TARGET"
262         mkdir "binutils-$TARGET"
263         cd "binutils-$TARGET"
264         echo "Configuring binutils"
265         "$SRCDIR/$BINUTILS/configure" --prefix="$PREFIX" --target=$TARGET --disable-nls \
266                 --with-gcc --with-gnu-as --with-gnu-ld --disable-shared &> configure.log
267         cd "$BUILDDIR"
268 }
269
270
271 function build_binutils
272 {
273         cd "$BUILDDIR/binutils-$TARGET"
274         echo "Building binutils"
275         make CFLAGS="-O2 -fno-exceptions" LDFLAGS="-s" &> make.log
276         if test $? -ne 0; then
277                 echo "make of binutils failed - log available: binutils-$TARGET/make.log"
278                 exit 1
279         fi
280         cd "$BUILDDIR"
281 }
282
283
284 function install_binutils
285 {
286         cd "$BUILDDIR/binutils-$TARGET"
287         echo "Installing binutils"
288         make install &> make-install.log
289         if test $? -ne 0; then
290                 echo "install of binutils failed - log available: binutils-$TARGET/make-install.log"
291                 exit 1
292         fi
293         cd "$BUILDDIR"
294 }
295
296
297 function extract_gcc
298 {
299         cd "$SRCDIR"
300         GCC=`tar -tzf "$SRCDIR/$GCC_CORE_ARCHIVE" | head -n 1`
301 #       rm -rf "$GCC"
302         echo "====== GCC ${GCC} ========="
303         echo "Extracting gcc"
304         tar -xzf "$SRCDIR/$GCC_CORE_ARCHIVE"
305         if [ "x$GCC_GPP_ARCHIVE" != "x" ]; then
306                 GCC_LANGS=${GCC_LANGS}",c++"
307                 tar -xzf "$SRCDIR/$GCC_GPP_ARCHIVE"
308         fi
309         if [ "x$GCC_G77_ARCHIVE" != "x" ]; then
310                 GCC_LANGS=${GCC_LANGS}",f77"
311                 tar -xzf "$SRCDIR/$GCC_G77_ARCHIVE"
312         fi
313         if [ "x$GCC_OBJC_ARCHIVE" != "x" ]; then
314                 GCC_LANGS=${GCC_LANGS}",objc"
315                 tar -xzf "$SRCDIR/$GCC_OBJC_ARCHIVE"
316         fi
317         if [ "x$GCC_JAVA_ARCHIVE" != "x" ]; then
318                 GCC_LANGS=${GCC_LANGS}",java"
319                 tar -xzf "$SRCDIR/$GCC_JAVA_ARCHIVE"
320         fi
321         if [ "x$GCC_ADA_ARCHIVE" != "x" ]; then
322                 GCC_LANGS=${GCC_LANGS}",ada"
323                 tar -xzf "$SRCDIR/$GCC_ADA_ARCHIVE"
324         fi
325         cd "$BUILDDIR"
326 }
327
328
329 function patch_gcc
330 {
331         if [ "$GCC_PATCH" != "" ]; then
332                 echo "Patching gcc"
333                 cd "$SRCDIR/$GCC"
334                 patch -p1 < "$SRCDIR/$GCC_PATCH"
335                 cd "$BUILDDIR"
336         fi
337 }
338
339
340 function configure_gcc
341 {
342         cd "$BUILDDIR"
343         rm -rf "gcc-$TARGET"
344         mkdir "gcc-$TARGET"
345         cd "gcc-$TARGET"
346         echo "Configuring gcc"
347         "$SRCDIR/$GCC/configure" -v \
348                 --prefix="$PREFIX" --target=$TARGET \
349                 --with-headers="$PREFIX/$TARGET/include" \
350                 --with-gcc --with-gnu-ld --with-gnu-as \
351                 --enable-threads --disable-nls --enable-languages=$GCC_LANGS \
352                 --disable-win32-registry --disable-shared --enable-sjlj-exceptions --enable-libgcj \
353                 --disable-java-awt --without-x --enable-java-gc=boehm --disable-libgcj-debug \
354                 --enable-interpreter --enable-hash-synchronization --enable-libstdcxx-debug \
355                 &> configure.log
356         cd "$BUILDDIR"
357 }
358
359
360 function build_gcc
361 {
362         cd "$BUILDDIR/gcc-$TARGET"
363         echo "Building gcc"
364         make CFLAGS="-O2" CXXFLAGS="-O2" GCJFLAGS="-O2" LDFLAGS="-s" DEBUG_FLAGS="-g0" &> make.log
365         if test $? -ne 0; then
366                 echo "make of gcc failed - log available: gcc-$TARGET/make.log"
367                 exit 1
368         fi
369         if [ "x$GCC_ADA" != "x" ]; then
370                 cd gcc
371                 make "CFLAGS=-O2" "LDFLAGS=-s" gnatlib_and_tools &> make-gnatlib.log
372                 if test $? -ne 0; then
373                         echo "make of gnatlib and tools failed - log available: gcc-$TARGET/make-gnatlib.log"
374                         exit 1
375                 fi
376         fi
377         cd "$BUILDDIR"
378 }
379
380
381 function install_gcc
382 {
383         cd "$BUILDDIR/gcc-$TARGET"
384         echo "Installing gcc"
385         make install &> make-install.log
386         if test $? -ne 0; then
387                 echo "install of gcc failed - log available: gcc-$TARGET/make-install.log"
388                 exit 1
389         fi
390         cd "$BUILDDIR"
391 }
392
393
394 function final_tweaks
395 {
396         echo "Finalizing installation"
397
398         # remove gcc build headers
399         rm -rf "$PREFIX/$TARGET/sys-include"
400
401         # Add extra binary links
402         if [ ! -f "$PREFIX/$TARGET/bin/objdump" ]; then
403                 ln "$PREFIX/bin/$TARGET-objdump" "$PREFIX/$TARGET/bin/objdump"
404         fi
405
406         # make cc and c++ symlinks to gcc and g++
407         if [ ! -f "$PREFIX/$TARGET/bin/g++" ]; then
408                 ln "$PREFIX/bin/$TARGET-g++" "$PREFIX/$TARGET/bin/g++"
409         fi
410         if [ ! -f "$PREFIX/$TARGET/bin/cc" ]; then
411                 ln -s "gcc" "$PREFIX/$TARGET/bin/cc"
412         fi
413         if [ ! -f "$PREFIX/$TARGET/bin/c++" ]; then
414                 ln -s "g++" "$PREFIX/$TARGET/bin/c++"
415         fi
416
417         # strip all the binaries
418         ls "$PREFIX"/bin/* "$PREFIX/$TARGET"/bin/* | egrep -v '.dll$' | egrep -v 'gccbug$' |
419         while read file; do
420                 strip "$file"
421         done
422
423         echo "Installation complete!"
424 }
425
426
427
428 #
429 # Main part of the script
430 #
431
432 download_files
433
434 if [ "x$PURGE_DIR" = "xY" ]; then
435         purge_existing_install
436 fi
437
438 install_libs
439
440 extract_binutils
441 configure_binutils
442 build_binutils
443 install_binutils
444
445 extract_gcc
446 patch_gcc
447 configure_gcc
448 build_gcc
449 install_gcc
450
451 final_tweaks
452
453
454 #
455 # End
456 #