share/libgc: compile libgc for Android on more than arm: x86 and mips
[nit.git] / share / libgc / android-setup-libgc.sh
1 #!/bin/bash
2 # This file is part of NIT ( http://www.nitlanguage.org ).
3 #
4 # Licensed under the Apache License, Version 2.0 (the "License");
5 # you may not use this file except in compliance with the License.
6 # You may obtain a copy of the License at
7 #
8 # http://www.apache.org/licenses/LICENSE-2.0
9 #
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS,
12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
15
16 # Fetch, configure and build libgc (the Boehm GC) for Android
17 #
18 # Will produce libgc.a which can be linked to Android NDK applications.
19 #
20 # The `ndk-build` tool from the Android NDK must be in PATH before
21 # invoking this tool. It will be used to guess the path to the NDK.
22 #
23 # Alternatively, you may define a custom path to the NDK by setting
24 # `ANDROID_NDK`.
25
26 # If ANDROID_NDK is not set, get it from the path to `ndk-build`
27 if test -z "$ANDROID_NDK"; then
28 ndk_build_path=`which ndk-build`
29 if test $? -ne 0; then
30 echo "Error: ndk-build from the Android NDK must be in your PATH"
31 exit 1
32 fi
33
34 ANDROID_NDK=`dirname $ndk_build_path`
35 fi
36
37 # Information on the currently targeted libgc and libatomic_ops source URL
38 # These may have to be updated according to server-side changes and newer
39 # versions of the Boehm GC.
40 libgc_url=http://www.hboehm.info/gc/gc_source/gc-7.4.0.tar.gz
41 libgc_dir=gc-7.4.0
42 libatomic_ops_url=http://www.hboehm.info/gc/gc_source/libatomic_ops-7.4.0.tar.gz
43 libatomic_ops_dir=libatomic_ops-7.4.0
44
45 # Absolute installation path
46 if expr match "$0" "^/.*"; then
47 install="`dirname "$0"`"
48 else
49 install="`pwd`/`dirname "$0"`"
50 fi
51
52 # Local source directory
53 mkdir -p "$install/src"
54 cd "$install/src"
55
56 # Download libs
57 for url in $libgc_url $libatomic_ops_url; do
58 echo "Downloading $url..."
59 curl --progress-bar -o `basename $url` $url || exit 1
60 done
61
62 if test -d $libgc_dir; then
63 rm -r $libgc_dir
64 fi
65
66 # Extract
67 tar -xzf `basename $libgc_url` || exit 1
68 tar -xzf `basename $libatomic_ops_url` || exit 1
69 mv $libatomic_ops_dir $libgc_dir/libatomic_ops || exit 1
70
71 cd $libgc_dir || exit 1
72
73 archs=( arm x86 mips)
74 tools_dirs=( arm-linux-androideabi-4.6 x86-4.6 mipsel-linux-android-4.6)
75 tools_prefixes=(arm-linux-androideabi i686-linux-android mipsel-linux-android)
76 hosts=( arm-linux-androideabi x86-linux-android mips-linux-android)
77
78 n_archs=$(( ${#archs[@]} - 1 ))
79 for i in $(eval echo "{0..$n_archs}"); do
80 arch=${archs[i]}
81 tools_dir=${tools_dirs[i]}
82 tools_prefix=${tools_prefixes[i]}
83 host=${hosts[i]}
84
85 # Get the first platform available (it shouldn't change much, but it may
86 # have to be adjusted)
87 for platform in `echo $ANDROID_NDK/platforms/android-*/arch-$arch`; do
88 sys_root=$platform
89 break
90 done
91
92 if test -z "$sys_root"; then
93 echo "Error: could not an Android platform for $arch in the NDK, define ANDROID_NDK to the correct path."
94 exit 1
95 fi
96
97 # Configure for Android
98 path="$ANDROID_NDK/toolchains/$tools_dir/prebuilt/linux-x86_64/bin/"
99 export CC="$path/$tools_prefix-gcc --sysroot=$sys_root"
100 export CXX="$path/$tools_prefix-g++ --sysroot=$sys_root"
101 export LD="$path/$tools_prefix-ld"
102 export AR="$path/$tools_prefix-ar"
103 export RANLIB="$path/$tools_prefix-ranlib"
104 export STRIP="$path/$tools_prefix-strip"
105 export CFLAGS="-DIGNORE_DYNAMIC_LOADING -DPLATFORM_ANDROID -I libatomic_ops/src/"
106 export LIBS="-lc -lgcc"
107 ./configure --host=$host --enable-static --disable-shared --prefix="$install/$arch/" || exit 1
108
109 # Compile and install locally
110 make install -j 4 || exit 1
111 done