Import Upstream version 20180207
[hcoop/debian/mlton.git] / bin / build-cross-gcc
1 #!/usr/bin/env bash
2
3 # This script builds and installs a gcc cross compiler.
4
5 # It has been used to build cross compilers from Linux to Cygwin,
6 # MinGW, and Solaris. It is unlikely that this script will work
7 # out-of-the-box. It is only intended as a template. You should read
8 # through it and understand what it does, and make changes as
9 # necessary. Feel free to add another targetType if you modify this
10 # script for another target.
11
12 # Notes from Anoq about the mingw target:
13 # I downloaded the following files from www.mingw.org:
14 # *) binutils-2.13.90-20030111-1-src.tar.gz which I unpacked to
15 # binutils-2.13.90-20030111-1-src.tar
16 # This script unpacks the .tar to binutils-2.13.90-20030111-1-src
17 # *) gcc-3.2.3-20030504-1-src.tar.gz which I unpacked to
18 # gcc-3.2.3-20030504-1-src.tar
19 # This script unpacks the .tar to gcc-3.2.3-20030504-1
20 # However when running make on gcc it complains about missing files
21 # stdlib.h and unistd.h
22
23 set -e
24
25 die () {
26 echo >&2 "$1"
27 exit 1
28 }
29
30 root=`pwd`
31 name=`basename "$0"`
32
33 usage () {
34 die "usage: $name {cygwin|mingw|sun}"
35 }
36
37 case "$#" in
38 1)
39 case "$1" in
40 cygwin|mingw|sun)
41 targetType="$1"
42 ;;
43 *)
44 usage
45 ;;
46 esac
47 ;;
48 *)
49 usage
50 esac
51
52 # You may want to change the installation prefix, which is where the
53 # script will install the cross-compiler tools.
54 prefix='/usr'
55
56 # You must have have the sources to binutils and gcc, and place the
57 # tarfiles in the current directory. You can find ftp sites to
58 # download binutils and gcc-core at gnu.org. You may need to change
59 # the version numbers below to match what you download.
60 binutils='binutils-2.12'
61 gccVers='2.95.3'
62 gccTar="gcc-core-$gccVers.tar"
63
64 # You may want to set the target.
65 case "$targetType" in
66 cygwin)
67 target='i386-pc-cygwin'
68 configureGCCFlags=''
69 makeGCCFlags=''
70 # For Cygwin, we also need the cygwin and w32api packages,
71 # which contain necessary header files and libraries. I got
72 # them by installing cygwin in a Windows machine (using #
73 # Cygwin's setup.exe program) and then getting the bzip'ed tar
74 # files out of their Cygwin packages dir. I had problems with
75 # cygwin-1.3.18-1, since its libcygwin.a contained a file,
76 # pseudo-reloc.o, with some strangeness that binutils didn't
77 # correctly handle.
78 cygwin='cygwin-1.3.17-1'
79 w32api='w32api-2.1-1'
80 ;;
81 mingw)
82 target='i386-pc-mingw32'
83 # target='mingw32'
84 # These flags are from build-cross.sh from www.libsdl.org except:
85 # I added --disable-nls because of undefined references to dcgettext__
86 configureGCCFlags='--with-headers=$prefix/$target/include --with-gnu-as --with-gnu-ld --without-newlib --disable-multilib --disable-nls'
87 makeGCCFlags='LANGUAGES=c'
88 # For MinGW, we also need the mingw-runtime and w32api packages,
89 # which contain necessary header files and libraries. I got
90 # them from www.mingw.org.
91 mingw='mingw-runtime-3.2'
92 w32api='w32api-2.4'
93 ;;
94 sun)
95 target='sparc-sun-solaris'
96 configureGCCFlags=''
97 makeGCCFlags=''
98 # For sun, we assume that you have already copied the includes
99 # and libraries from a Solaris machine to the host machine.
100 if ! [ -d "$prefix/$target/include" -a -d "$prefix/$target/lib" ]; then
101 die "Must create $prefix/$target/{include,lib}."
102 fi
103 # The GCC tools expect limits.h to be in sys-include, not include.
104 ( cd "$prefix/$target" &&
105 mkdir -p sys-include &&
106 mv include/limits.h sys-include )
107 ;;
108 esac
109
110 exists () {
111 if [ ! -r "$1" ]; then
112 die "$1 does not exist"
113 fi
114 }
115
116 echo 'Checking that needed files exist.'
117 exists $binutils.tar
118 exists $gccTar
119 case "$targetType" in
120 cygwin)
121 exists $cygwin.tar
122 exists $w32api.tar
123 echo 'Copying include files and libraries needed by cross compiler.'
124 cd "$root"
125 mkdir -p cygwin
126 cd cygwin
127 tar x <../$cygwin.tar
128 tar x <../$w32api.tar
129 mkdir -p "$prefix/$target" ||
130 die "Cannot create $prefix/$target."
131 (cd usr && tar c include lib) | (cd "$prefix/$target/" && tar x)
132 ;;
133 mingw)
134 exists $mingw.tar
135 exists $w32api.tar
136 echo 'Copying include files and libraries needed by cross compiler.'
137 cd "$root"
138 mkdir -p mingw
139 cd mingw
140 tar x <../$mingw.tar
141 tar x <../$w32api.tar
142 mkdir -p "$prefix/$target" ||
143 die "Cannot create $prefix/$target."
144 (tar c include lib) | (cd "$prefix/$target/" && tar x)
145 ;;
146 *)
147 ;;
148 esac
149
150 echo 'Building binutils.'
151 cd "$root"
152 if [ ! -d "$binutils" ]; then
153 tar x <$binutils.tar
154 fi
155 mkdir -p build-binutils
156 cd build-binutils
157 "../$binutils/configure" "--prefix=$prefix" "--target=$target" \
158 >"$root/configure-binutils-log" 2>&1 ||
159 die "Configure of binutils failed."
160 make all install >"$root/build-binutils-log" 2>&1 ||
161 die "Build of binutils failed."
162
163 echo 'Building gcc.'
164 cd "$root"
165 tar x <"$gccTar"
166 mkdir -p build-gcc
167 cd build-gcc
168 eval "../gcc-$gccVers/configure" -v $configureGCCFlags \
169 --enable-languages=c \
170 "--prefix=$prefix" \
171 "--target=$target" \
172 >"$root/configure-gcc-log" 2>&1 ||
173 die "Configure of gcc failed."
174 eval make $makeGCCFlags all install >"$root/build-gcc-log" 2>&1 ||
175 die "Build of gcc failed."
176
177 echo 'Success.'