Merge: lib/config: fix doc
[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 # The source package should be downloaded and compiled only once.
27 # If the working directory is cleared, the cached version will be lost.
28 # On test servers, which clear the working directory frequently,
29 # it may be a good idea to use a local version of the source packages.
30 # To do so, download the following two packages and put them in your HOME directory.
31 # * http://www.hboehm.info/gc/gc_source/gc-7.4.0.tar.gz
32 # * http://www.hboehm.info/gc/gc_source/libatomic_ops-7.4.0.tar.gz
33
34 # If ANDROID_NDK is not set, get it from the path to `ndk-build`
35 if test -z "$ANDROID_NDK"; then
36 ndk_build_path=`which ndk-build`
37 if test $? -ne 0; then
38 echo "Error: ndk-build from the Android NDK must be in your PATH"
39 exit 1
40 fi
41
42 ANDROID_NDK=`dirname $ndk_build_path`
43 fi
44
45 # Information on the currently targeted libgc and libatomic_ops source URL
46 # These may have to be updated according to server-side changes and newer
47 # versions of the Boehm GC.
48 libgc_url=http://www.hboehm.info/gc/gc_source/gc-7.4.0.tar.gz
49 libgc_local=~/gc-7.4.0.tar.gz
50 libgc_dir=gc-7.4.0
51 libatomic_ops_url=http://www.hboehm.info/gc/gc_source/libatomic_ops-7.4.0.tar.gz
52 libatomic_ops_local=~/libatomic_ops-7.4.0.tar.gz
53 libatomic_ops_dir=libatomic_ops-7.4.0
54
55 # Absolute installation path
56 if expr match "$0" "^/.*"; then
57 install="`dirname "$0"`"
58 else
59 install="`pwd`/`dirname "$0"`"
60 fi
61
62 # Local source directory
63 mkdir -p "$install/src"
64 cd "$install/src"
65
66 # Download libs, trying to use a local version if possible
67 urls=""
68 if test -f $libgc_local; then
69 cp $libgc_local .
70 else
71 urls=$libgc_url
72 fi
73
74 if test -f $libatomic_ops_local; then
75 cp $libatomic_ops_local .
76 else
77 urls="$urls $libatomic_ops_url"
78 fi
79
80 for url in $urls; do
81 echo "Downloading $url..."
82 curl --progress-bar -o `basename $url` $url || exit 1
83 done
84
85 if test -d $libgc_dir; then
86 rm -r $libgc_dir
87 fi
88
89 # Extract
90 tar -xzf `basename $libgc_url` || exit 1
91 tar -xzf `basename $libatomic_ops_url` || exit 1
92 mv $libatomic_ops_dir $libgc_dir/libatomic_ops || exit 1
93
94 cd $libgc_dir || exit 1
95
96 archs=( arm x86 mips)
97 tools_dirs=( arm-linux-androideabi-4.9 x86-4.9 mipsel-linux-android-4.9)
98 tools_prefixes=(arm-linux-androideabi i686-linux-android mipsel-linux-android)
99 hosts=( arm-linux-androideabi x86-linux-android mips-linux-android)
100
101 n_archs=$(( ${#archs[@]} - 1 ))
102 for i in $(eval echo "{0..$n_archs}"); do
103 arch=${archs[i]}
104 tools_dir=${tools_dirs[i]}
105 tools_prefix=${tools_prefixes[i]}
106 host=${hosts[i]}
107
108 # Get the first platform available (it shouldn't change much, but it may
109 # have to be adjusted)
110 for platform in `echo $ANDROID_NDK/platforms/android-*/arch-$arch`; do
111 sys_root=$platform
112 break
113 done
114
115 if test -z "$sys_root"; then
116 echo "Error: could not an Android platform for $arch in the NDK, define ANDROID_NDK to the correct path."
117 exit 1
118 fi
119
120 # Configure for Android
121 path="$ANDROID_NDK/toolchains/$tools_dir/prebuilt/linux-x86_64/bin/"
122 export CC="$path/$tools_prefix-gcc --sysroot=$sys_root"
123 export CXX="$path/$tools_prefix-g++ --sysroot=$sys_root"
124 export LD="$path/$tools_prefix-ld"
125 export AR="$path/$tools_prefix-ar"
126 export RANLIB="$path/$tools_prefix-ranlib"
127 export STRIP="$path/$tools_prefix-strip"
128 export CFLAGS="-DIGNORE_DYNAMIC_LOADING -DPLATFORM_ANDROID -I libatomic_ops/src/"
129 export LIBS="-lc -lgcc"
130 ./configure --host=$host --enable-static --disable-shared --prefix="$install/$arch/" || exit 1
131
132 # Compile and install locally
133 make install -j 4 || exit 1
134 done