-
Notifications
You must be signed in to change notification settings - Fork 29
/
build-arch
executable file
·349 lines (307 loc) · 11.8 KB
/
build-arch
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
#!/bin/bash
#set -x
set -e
[ -e /etc/makepkg.conf ] && source /etc/makepkg.conf
MAKEFLAGS=${MAKEFLAGS:--j6}
# Basic configuration
GHCHOME=${GHCHOME:-/opt/ghc}
BASEDIR="$(dirname $0)"
cd "$BASEDIR"
BASEDIR="$(pwd)"
# Basic parameters
NDK_RELEASE=${NDK_RELEASE:-r9d}
NDK_MD5=c7c775ab3342965408d20fd18e71aa45
NDK_PLATFORM=${NDK_PLATFORM:-android-14}
if [ "x$1" == "x--x86" ] ; then
NDK_TOOLCHAIN=${NDK_TOOLCHAIN:-x86-4.8}
NDK_TARGET=${NDK_TARGET:-i686-linux-android}
ARCH_OPTS=""
GHC_TARGET=${GHC_TARGET:-x86-4.8}
elif [ "x$1" == "x--arm" ] ; then
NDK_TOOLCHAIN=${NDK_TOOLCHAIN:-arm-linux-androideabi-4.8}
NDK_TARGET=${NDK_TARGET:-arm-linux-androideabi}
ARCH_OPTS="-fllvm"
GHC_TARGET=${GHC_TARGET:-arm-unknown-linux-androideabi}
else
echo Please select an architecture to build with --arm or --x86.
exit 1
fi
NDK_DESC=$NDK_PLATFORM-$NDK_TOOLCHAIN
NDK="$GHCHOME/$NDK_PLATFORM/$NDK_TOOLCHAIN"
NDK_ADDON_SRC="$BASEDIR/build-$NDK_DESC"
NDK_ADDON_PREFIX="$NDK/sysroot/usr"
GHC_RELEASE=${GHC_RELEASE:-7.8.3}
GHC_STAGE0_MD5=4dd880afc51c6ba6126e26d17b6340d9
GHC_SRC_MD5=5e34b2a29564596c9ed83fb8667b47d4
GHC_STAGE0_SRC="$BASEDIR/ghc-$GHC_RELEASE"
GHC_STAGE0_PREFIX="$GHCHOME"
GHC_STAGE0="$GHC_STAGE0_PREFIX/bin/ghc"
GHC_PREFIX="$NDK"
GHC_SRC="$NDK_ADDON_SRC/ghc-$GHC_RELEASE"
NCURSES_RELEASE=5.9
NCURSES_MD5=8cb9c412e5f2d96bc6f459aa8c6282a1
GMP_RELEASE=6.0.0a
GMP_MD5=1e6da4e434553d2811437aa42c7f7c76
CONFIG_SUB_SRC=${CONFIG_SUB_SRC:-/usr/share/automake-1.15}
BUILD_GCC=gcc
BUILD_ARCH=$($BUILD_GCC -v 2>&1 | grep ^Target: | cut -f 2 -d ' ')
mkdir -p "$GHCHOME"
mkdir -p "$NDK_ADDON_SRC"
mkdir -p "${BASEDIR}/tarfiles"
TARDIR="${BASEDIR}/tarfiles"
function check_md5() {
FILENAME="$1"
MD5="$2"
[ -e "${FILENAME}" ] || return 1;
[ "$(md5sum "$FILENAME" | cut -f1 -d ' ')" == "$MD5" ]
}
function apply_patches() {
pushd $2 > /dev/null
for p in $(find "$BASEDIR/patches" -name "$1") ; do
echo Applying patch $p in $(pwd)
patch -p1 < "$p"
done
popd > /dev/null
}
# Download and configure the Android NDK toolchain
NDK_TAR_FILE=android-ndk-${NDK_RELEASE}-linux-x86_64.tar.bz2
NDK_TAR_PATH="${TARDIR}/${NDK_TAR_FILE}"
NDK_PATH="$BASEDIR/android-ndk-$NDK_RELEASE"
if ! [ -d "$NDK" ] ; then
if ! [ -d "$NDK_PATH" ] ; then
if ! check_md5 "$NDK_TAR_PATH" "$NDK_MD5" ; then
echo Downloading the Android NDK $NDK_RELEASE
curl -o "${TARDIR}/${NDK_TAR_FILE}" http://dl.google.com/android/ndk/${NDK_TAR_FILE}
check_md5 "$NDK_TAR_PATH" "$NDK_MD5"
fi
echo Unpacking the Android NDK $NDK_RELEASE
tar xf "$NDK_TAR_PATH"
fi
echo Preparing the Android NDK toolchain in $NDK
"$NDK_PATH/build/tools/make-standalone-toolchain.sh" --toolchain=$NDK_TOOLCHAIN --platform=$NDK_PLATFORM --install-dir="$NDK"
#TMP hack, fake pthread library for ghc linker
pushd "$NDK_ADDON_PREFIX/lib" > /dev/null
ln -s libcharset.a libpthread.a
popd > /dev/null
fi
# Add toolchain to path
export PATH="$NDK/bin":$PATH
# Unpack ncurses
NCURSES_TAR_FILE=ncurses-${NCURSES_RELEASE}.tar.gz
NCURSES_TAR_PATH="${TARDIR}/${NCURSES_TAR_FILE}"
NCURSES_SRC="$NDK_ADDON_SRC/ncurses-${NCURSES_RELEASE}"
if ! [ -d "$NCURSES_SRC" ] ; then
if ! check_md5 "$NCURSES_TAR_PATH" "$NCURSES_MD5" ; then
echo Downloading ncurses $NCURSES_RELEASE
curl -o "${TARDIR}/${NCURSES_TAR_FILE}" http://ftp.gnu.org/pub/gnu/ncurses/${NCURSES_TAR_FILE}
check_md5 "$NCURSES_TAR_PATH" "$NCURSES_MD5"
fi
(cd $NDK_ADDON_SRC; tar xf "$TARDIR/$NCURSES_TAR_FILE")
fi
if ! [ -e "$CONFIG_SUB_SRC/config.sub" ] ; then
CONFIG_SUB_SRC=${CONFIG_SUB_SRC:-$NCURSES_SRC}
fi
# Build and install iconv
ICONV_SRC="$NDK_ADDON_SRC/iconv"
if ! [ -e "$NDK_ADDON_PREFIX/lib/libiconv.a" ] ; then
if ! [ -d "$ICONV_SRC" ] ; then
git clone https://github.com/ironsteel/iconv-android.git $ICONV_SRC
# Update config.sub and config.guess
cp -v "$CONFIG_SUB_SRC/config.sub" "$ICONV_SRC/build-aux"
cp -v "$CONFIG_SUB_SRC/config.guess" "$ICONV_SRC/build-aux"
cp -v "$CONFIG_SUB_SRC/config.sub" "$ICONV_SRC/libcharset/build-aux"
cp -v "$CONFIG_SUB_SRC/config.guess" "$ICONV_SRC/libcharset/build-aux"
apply_patches 'iconv-*' $ICONV_SRC
fi
pushd $ICONV_SRC > /dev/null
if ! [ -e "lib/.libs/libiconv.a" ] ; then
./configure --prefix="$NDK_ADDON_PREFIX" --host=$NDK_TARGET --build=$BUILD_ARCH --with-build-cc=$BUILD_GCC --enable-static --disable-shared
make $MAKEFLAGS
fi
make install
popd > /dev/null
fi
# Build and install ncurses
if ! [ -e "$NDK_ADDON_PREFIX/lib/libncurses.a" ] ; then
pushd $NCURSES_SRC > /dev/null
if ! [ -e "lib/libncurses.a" ] ; then
./configure --prefix="$NDK_ADDON_PREFIX" --host=$NDK_TARGET --build=$BUILD_ARCH --with-build-cc=$BUILD_GCC --enable-static --disable-shared --includedir="$NDK_ADDON_PREFIX/include" --without-manpages
echo '#undef HAVE_LOCALE_H' >> "$NCURSES_SRC/include/ncurses_cfg.h" # TMP hack
make $MAKEFLAGS
fi
make install
popd > /dev/null
fi
# Build and install gmp
GMP_TAR_FILE=gmp-${GMP_RELEASE}.tar.xz
GMP_TAR_PATH="${TARDIR}/${GMP_TAR_FILE}"
GMP_SRC="$NDK_ADDON_SRC/gmp-6.0.0"
if ! [ -d "$GMP_SRC" ] ; then
if ! check_md5 "$GMP_TAR_PATH" "$GMP_MD5" ; then
echo Downloading gmp $GMP_RELEASE
curl -o "${TARDIR}/${GMP_TAR_FILE}" https://gmplib.org/download/gmp/${GMP_TAR_FILE}
check_md5 "$GMP_TAR_PATH" "$GMP_MD5"
fi
(cd $NDK_ADDON_SRC; tar xf "$TARDIR/$GMP_TAR_FILE")
fi
if ! [ -e "$NDK_ADDON_PREFIX/lib/libgmp.a" ] ; then
pushd $GMP_SRC > /dev/null
if ! [ -e ".libs/libgmp.a" ] ; then
./configure --prefix="$NDK_ADDON_PREFIX" --host=$NDK_TARGET --build=$BUILD_ARCH --with-build-cc=$BUILD_GCC --enable-static --disable-shared
make $MAKEFLAGS
fi
make install
popd > /dev/null
fi
GHC_STAGE0_TAR_FILE=ghc-${GHC_RELEASE}-x86_64-unknown-linux-deb7.tar.xz
GHC_STAGE0_TAR_PATH="${TARDIR}/${GHC_STAGE0_TAR_FILE}"
if ! [ -e "$GHC_STAGE0" ] ; then
if ! check_md5 "$GHC_STAGE0_TAR_PATH" "$GHC_STAGE0_MD5" ; then
echo Downloading the GHC $GHC_RELEASE binary release
curl -o "${TARDIR}/${GHC_STAGE0_TAR_FILE}" http://www.haskell.org/ghc/dist/$GHC_RELEASE/$GHC_STAGE0_TAR_FILE
check_md5 "$GHC_STAGE0_TAR_PATH" "$GHC_STAGE0_MD5"
fi
echo Unpacking the GHC $GHC_RELEASE binary release
tar xf "$GHC_STAGE0_TAR_PATH"
pushd "$GHC_STAGE0_SRC" > /dev/null
./configure --prefix="$GHC_STAGE0_PREFIX"
make $MAKEFLAGS install
popd > /dev/null
fi
GHC_SRC_TAR_FILE=ghc-${GHC_RELEASE}-src.tar.xz
GHC_SRC_TAR_PATH="${TARDIR}/${GHC_SRC_TAR_FILE}"
if ! [ -d "$GHC_SRC" ] ; then
if ! check_md5 "$GHC_SRC_TAR_PATH" "$GHC_SRC_MD5" ; then
echo Downloading the GHC $GHC_RELEASE source release
curl -o "${TARDIR}/${GHC_SRC_TAR_FILE}" http://www.haskell.org/ghc/dist/$GHC_RELEASE/$GHC_SRC_TAR_FILE
check_md5 "$GHC_SRC_TAR_PATH" "$GHC_SRC_MD5"
fi
pushd "$NDK_ADDON_SRC" > /dev/null
echo Unpacking the GHC $GHC_RELEASE source release
tar xf "$GHC_SRC_TAR_PATH"
popd > /dev/null
apply_patches 'ghc-*' "$GHC_SRC"
pushd "$GHC_SRC" > /dev/null
# Setup build.mk
cat > mk/build.mk <<EOF
Stage1Only = YES
DYNAMIC_GHC_PROGRAMS = NO
SRC_HC_OPTS = -O -H64m
GhcStage1HcOpts = -O2 -fasm
GhcStage2HcOpts = -O2 -fasm $ARCH_OPTS
GhcHcOpts = -Rghc-timing
GhcLibHcOpts = -O2
GhcLibWays = v
HADDOCK_DOCS = NO
BUILD_DOCBOOK_HTML = NO
BUILD_DOCBOOK_PS = NO
BUILD_DOCBOOK_PDF = NO
EOF
# Update config.sub and config.guess
for x in $(find . -name "config.sub") ; do
dir=$(dirname $x)
cp -v "$CONFIG_SUB_SRC/config.sub" "$dir"
cp -v "$CONFIG_SUB_SRC/config.guess" "$dir"
done
# Apply library patches
apply_patches "hsc2hs-*" "$GHC_SRC/utils/hsc2hs"
apply_patches "haskeline-*" "$GHC_SRC/libraries/haskeline"
apply_patches "unix-*" "$GHC_SRC/libraries/unix"
apply_patches "base-*" "$GHC_SRC/libraries/base"
# Configure
./configure --prefix="$GHC_PREFIX" --target=$NDK_TARGET --with-ghc=$GHC_STAGE0 --with-gcc=$NDK/bin/$NDK_TARGET-gcc
popd > /dev/null
fi
GHC_STAGE1=$NDK/bin/$GHC_TARGET-ghc
if ! [ -e "$GHC_STAGE1" ] ; then
pushd "$GHC_SRC" > /dev/null
function check_install_gmp_constants() {
GMPDCHDR="libraries/integer-gmp/mkGmpDerivedConstants/dist/GmpDerivedConstants.h"
if ! [ -e "$GMPDCHDR" ] ; then
if [ -e "$BASEDIR/patches/gmp-$NDK_DESC-GmpDerivedConstants.h" ] ; then
cp -v "$BASEDIR/patches/gmp-$NDK_DESC-GmpDerivedConstants.h" "$GMPDCHDR"
else
echo \#\#\# Execute the following commands to generate a GmpDerivedConstants.h for your target, then run build again:
echo \#\#\# adb push ghc-$NDK_DESC/libraries/integer-gmp/cbits/mkGmpDerivedConstants /data/local
echo \#\#\# adb shell /data/local/mkGmpDerivedConstants \> $BASEDIR/patches/gmp-$NDK_DESC-GmpDerivedConstants.h
echo \#\#\# adb shell rm /data/local/mkGmpDerivedConstants
exit 1
fi
fi
}
make $MAKEFLAGS || true # TMP hack, see http://hackage.haskell.org/trac/ghc/ticket/7490
make $MAKEFLAGS || true # TMP hack, target mkGmpDerivedConstants fails on build host
check_install_gmp_constants
make $MAKEFLAGS || true # TMP hack, tries to execut inplace stage2
make $MAKEFLAGS || true # TMP hack, one more for luck
make install
popd > /dev/null
fi
# Write hsc2hs wrapper
HSC2HS_TARGET=$(ls $GHC_PREFIX/bin/$GHC_TARGET-hsc2hs)
echo \#/bin/bash > $HSC2HS_TARGET
echo 'exec /usr/bin/hsc2hs --cross-compile "$@" --define=linux_android_HOST_OS=1' >> $HSC2HS_TARGET
chmod +x $HSC2HS_TARGET
GHC_STAGE0_CABAL="$GHC_STAGE0_PREFIX/bin/cabal"
if ! [ -e "$GHC_STAGE0_CABAL" ] ; then
# install a cabal that supports cross compilation
CABAL_SRC="$NDK_ADDON_SRC/cabal"
if ! [ -d "$CABAL_SRC" ] ; then
git clone https://github.com/haskell/cabal.git $CABAL_SRC
fi
pushd $CABAL_SRC/Cabal > /dev/null
$GHC_STAGE0 -threaded --make Setup
./Setup configure \
--prefix=$GHC_STAGE0_PREFIX \
--bindir=$GHC_STAGE0_PREFIX/bin/ \
--with-compiler=$GHC_STAGE0 \
--with-ghc=$GHC_STAGE0 \
--with-ghc-pkg=$GHC_STAGE0_PREFIX/bin/ghc-pkg \
--with-hsc2hs=$GHC_STAGE0_PREFIX/bin/hsc2hs \
--package-db=$GHC_STAGE0_PREFIX/lib/ghc-$GHC_RELEASE/package.conf.d
./Setup build
./Setup install
popd > /dev/null
pushd $CABAL_SRC/cabal-install > /dev/null
(
set -e
GHC=$GHC_STAGE0
GHC_PKG=$GHC_STAGE0_PREFIX/bin/ghc-pkg
HADDOCK=$GHC_STAGE0_PREFIX/bin/haddock
PREFIX=$GHC_STAGE0_PREFIX
. ./bootstrap.sh --global
)
popd > /dev/null
$GHC_STAGE0_CABAL update
fi
CABAL_WRAPPER="$GHC_PREFIX/bin/$GHC_TARGET-cabal"
if ! [ -e "$CABAL_WRAPPER" ] ; then
# Write cabal wrapper
echo \#/bin/bash > "$CABAL_WRAPPER"
echo NDK=$GHC_PREFIX >> "$CABAL_WRAPPER"
echo NDK_TARGET=$NDK_TARGET >> "$CABAL_WRAPPER"
echo GHC_TARGET=$GHC_TARGET >> "$CABAL_WRAPPER"
echo GHC_RELEASE=$GHC_RELEASE >> "$CABAL_WRAPPER"
echo BIN_CABAL=$GHC_STAGE0_PREFIX/bin/cabal >> "$CABAL_WRAPPER"
cat "$BASEDIR/patches/cabal-wrapper" >> "$CABAL_WRAPPER"
chmod +x "$CABAL_WRAPPER"
fi
# Add target bindir links
function maybe_link() {
if [ -e "$1" -a ! -e "$2" ] ; then ln -sf "$1" "$2" ; fi
}
function bindir_link() {
maybe_link $GHC_PREFIX/bin/$GHC_TARGET-$1 $GHC_PREFIX/$NDK_TARGET/bin/$1
maybe_link $GHC_PREFIX/bin/$GHC_TARGET-$1 $GHC_PREFIX/bin/$NDK_TARGET-$1
}
bindir_link ghc
bindir_link ghc-pkg
bindir_link hp2ps
bindir_link hsc2hs
bindir_link cabal
for f in $(ls $GHC_PREFIX/bin/$GHC_TARGET-*) ; do
maybe_link $f $GHC_STAGE0_PREFIX/bin/$(basename $f)
done
for f in $(ls $NDK/bin/$NDK_TARGET-*) ; do
maybe_link $f $GHC_STAGE0_PREFIX/bin/$(basename $f)
done