David 'Digit' Turner | b9a8479 | 2009-05-07 20:39:04 +0200 | [diff] [blame] | 1 | #!/bin/sh |
| 2 | # |
| 3 | # Copyright (C) 2009 The Android Open Source Project |
| 4 | # |
| 5 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | # you may not use this file except in compliance with the License. |
| 7 | # You may obtain a copy of the License at |
| 8 | # |
| 9 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | # |
| 11 | # Unless required by applicable law or agreed to in writing, software |
| 12 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | # See the License for the specific language governing permissions and |
| 15 | # limitations under the License. |
| 16 | # |
| 17 | # build-ndk-sysroot.sh |
| 18 | # |
| 19 | # collect files from an Android tree to assemble a sysroot suitable for |
| 20 | # building a standable toolchain. |
| 21 | # |
| 22 | # after that, you may use build/tools/package-ndk-sysroot.sh to package |
| 23 | # the resulting files for distribution. |
| 24 | # |
| 25 | # NOTE: this is different from the Android toolchain original build-sysroot.sh |
| 26 | # script because we place target files differently. |
| 27 | # |
| 28 | # WARNING: For now, only a single target ABI/Architecture us supported |
| 29 | # |
| 30 | |
| 31 | source `dirname $0`/../core/ndk-common.sh |
| 32 | |
| 33 | # PLATFORM is the name of the current Android system platform |
David 'Digit' Turner | fdc5ea2 | 2009-07-24 17:56:51 +0200 | [diff] [blame] | 34 | PLATFORM=android-3 |
David 'Digit' Turner | b9a8479 | 2009-05-07 20:39:04 +0200 | [diff] [blame] | 35 | |
| 36 | # ABI is the target ABI name for the NDK |
| 37 | ABI=arm |
| 38 | |
| 39 | # ARCH is the target ABI name in the Android sources |
| 40 | ARCH=arm |
| 41 | |
| 42 | OPTION_HELP=no |
| 43 | OPTION_BUILD_OUT= |
| 44 | OPTION_PLATFORM= |
| 45 | OPTION_PACKAGE=no |
| 46 | for opt do |
| 47 | optarg=`expr "x$opt" : 'x[^=]*=\(.*\)'` |
| 48 | case "$opt" in |
| 49 | --help|-h|-\?) OPTION_HELP=yes |
| 50 | ;; |
| 51 | --verbose) |
| 52 | if [ "$VERBOSE" = "yes" ] ; then |
| 53 | VERBOSE2=yes |
| 54 | else |
| 55 | VERBOSE=yes |
| 56 | fi |
| 57 | ;; |
| 58 | --platform=*) |
| 59 | OPTION_PLATFORM=$optarg |
| 60 | ;; |
| 61 | --build-out=*) |
| 62 | OPTION_BUILD_OUT=$optarg |
| 63 | ;; |
| 64 | --package) |
| 65 | OPTION_PACKAGE=yes |
| 66 | ;; |
| 67 | *) |
| 68 | echo "unknown option '$opt', use --help" |
| 69 | exit 1 |
| 70 | esac |
| 71 | done |
| 72 | |
| 73 | if [ $OPTION_HELP = "yes" ] ; then |
| 74 | echo "Collect files from an Android build tree and assembles a sysroot" |
| 75 | echo "suitable for building a standalone toolchain or be used by the" |
| 76 | echo "Android NDK." |
| 77 | echo "" |
| 78 | echo "options:" |
| 79 | echo "" |
| 80 | echo " --help print this message" |
| 81 | echo " --verbose enable verbose messages" |
| 82 | echo " --platform=<name> generate sysroot for platform <name> (default is $PLATFORM)" |
| 83 | echo " --build-out=<path> set Android build out directory" |
| 84 | echo " --package generate sysroot package tarball" |
| 85 | echo "" |
| 86 | exit 0 |
| 87 | fi |
| 88 | |
| 89 | if [ -n "$OPTION_PLATFORM" ] ; then |
| 90 | PLATFORM=$OPTION_PLATFORM |
| 91 | fi |
| 92 | |
| 93 | # Get the root of the NDK from the current program location |
| 94 | NDK_ROOT=`dirname $0` |
| 95 | NDK_ROOT=`dirname $NDK_ROOT` |
| 96 | NDK_ROOT=`dirname $NDK_ROOT` |
| 97 | |
| 98 | # Get the Android out directory |
| 99 | if [ -z "$OPTION_BUILD_OUT" ] ; then |
| 100 | if [ -z "$ANDROID_PRODUCT_OUT" ] ; then |
| 101 | echo "ANDROID_PRODUCT_OUT is not defined in your environment. Aborting" |
| 102 | exit 1 |
| 103 | fi |
| 104 | if [ ! -d $ANDROID_PRODUCT_OUT ] ; then |
| 105 | echo "ANDROID_PRODUCT_OUT does not point to a valid directory. Aborting" |
| 106 | exit 1 |
| 107 | fi |
| 108 | else |
| 109 | ANDROID_PRODUCT_OUT=$OPTION_BUILD_OUT |
| 110 | if [ ! -d $ANDROID_PRODUCT_OUT ] ; then |
| 111 | echo "The build out path is not a valid directory: $OPTION_BUILD_OUT" |
| 112 | exit 1 |
| 113 | fi |
| 114 | fi |
| 115 | |
| 116 | PRODUCT_DIR=$ANDROID_PRODUCT_OUT |
| 117 | SYSROOT=$NDK_ROOT/build/platforms/$PLATFORM/arch-$ABI |
| 118 | COMMON_ROOT=$NDK_ROOT/build/platforms/$PLATFORM/common |
| 119 | |
| 120 | # clean up everything in existing sysroot |
| 121 | rm -rf $SYSROOT |
| 122 | mkdir -p $SYSROOT |
| 123 | |
| 124 | rm -rf $COMMON_ROOT |
| 125 | mkdir -p $COMMON_ROOT |
| 126 | |
| 127 | LIB_ROOT=$SYSROOT/usr/lib |
| 128 | INCLUDE_ROOT=$SYSROOT/usr/include |
| 129 | |
| 130 | install_file () |
| 131 | { |
| 132 | mkdir -p $2/`dirname $1` |
| 133 | cp -fp $1 $2/$1 |
| 134 | } |
| 135 | |
| 136 | install_helper () |
| 137 | { |
| 138 | (cd $1 && find . -type f | while read ff; do install_file $ff $2; done) |
| 139 | } |
| 140 | |
| 141 | TOP=$PRODUCT_DIR/../../../.. |
| 142 | |
| 143 | # CRT objects that need to be copied |
| 144 | CRT_OBJS_DIR=$PRODUCT_DIR/obj/lib |
| 145 | CRT_OBJS="$CRT_OBJS_DIR/crtbegin_static.o \ |
| 146 | $CRT_OBJS_DIR/crtbegin_dynamic.o \ |
| 147 | $CRT_OBJS_DIR/crtend_android.o" |
| 148 | |
| 149 | # static libraries that need to be copied. |
| 150 | STATIC_LIBS_DIR=$PRODUCT_DIR/obj/STATIC_LIBRARIES |
| 151 | STATIC_LIBS="$STATIC_LIBS_DIR/libc_intermediates/libc.a \ |
| 152 | $STATIC_LIBS_DIR/libm_intermediates/libm.a \ |
| 153 | $STATIC_LIBS_DIR/libstdc++_intermediates/libstdc++.a |
| 154 | $STATIC_LIBS_DIR/libthread_db_intermediates/libthread_db.a" |
| 155 | |
| 156 | # dynamic libraries that need to be copied. |
| 157 | DYNAMIC_LIBS_DIR=$PRODUCT_DIR/symbols/system/lib |
| 158 | DYNAMIC_LIBS="$DYNAMIC_LIBS_DIR/libdl.so \ |
| 159 | $DYNAMIC_LIBS_DIR/libc.so \ |
| 160 | $DYNAMIC_LIBS_DIR/libm.so \ |
| 161 | $DYNAMIC_LIBS_DIR/libstdc++.so \ |
| 162 | $DYNAMIC_LIBS_DIR/libthread_db.so" |
| 163 | |
| 164 | # Copy all CRT objects and libraries |
| 165 | rm -rf $LIB_ROOT |
| 166 | mkdir -p $LIB_ROOT |
| 167 | cp -f $CRT_OBJS $STATIC_LIBS $DYNAMIC_LIBS $LIB_ROOT |
| 168 | |
David 'Digit' Turner | b20725d | 2009-05-11 16:37:42 +0200 | [diff] [blame] | 169 | # Check $TOP/bionic to see if this is new source layout. |
David 'Digit' Turner | b9a8479 | 2009-05-07 20:39:04 +0200 | [diff] [blame] | 170 | if [ -d $TOP/bionic ] ;then |
| 171 | BIONIC_ROOT=$TOP/bionic |
| 172 | LIBC_ROOT=$BIONIC_ROOT/libc |
| 173 | else |
| 174 | BIONIC_ROOT=$TOP/system |
| 175 | LIBC_ROOT=$BIONIC_ROOT/bionic |
| 176 | fi |
| 177 | |
| 178 | # Copy headers. This need to be done in the reverse order of inclusion |
| 179 | # in case there are different headers with the same name. |
| 180 | ARCH_INCLUDE=$SYSROOT/usr/include |
| 181 | rm -rf $ARCH_INCLUDE |
| 182 | mkdir -p $ARCH_INCLUDE |
| 183 | |
| 184 | COMMON_INCLUDE=$COMMON_ROOT/include |
| 185 | rm -rf $COMMON_INCLUDE |
| 186 | mkdir -p $COMMON_INCLUDE |
| 187 | |
| 188 | # Install a common header and create the appropriate arch-specific |
| 189 | # directory for it. |
| 190 | # |
| 191 | # $1: source directory |
| 192 | # $2: header path, relative to source directory |
| 193 | # |
| 194 | common_header () |
| 195 | { |
| 196 | echo "Copy: $COMMON_INCLUDE/$2" |
| 197 | mkdir -p `dirname $COMMON_INCLUDE/$2` |
| 198 | install $1/$2 $COMMON_INCLUDE/$2 |
| 199 | # just to be safe |
| 200 | chmod a-x $COMMON_INCLUDE/$2 |
| 201 | |
| 202 | # the link prefix, used to point to common/ |
| 203 | # from arch-$ARCH/usr/ |
| 204 | link_prefix=../../common/include |
| 205 | |
| 206 | # we need to count the number of directory separators in $2 |
| 207 | # for each one of them, we're going to prepend ../ to the |
| 208 | # link prefix |
| 209 | for item in `echo $2 | tr '/' ' '`; do |
| 210 | link_prefix=../$link_prefix |
| 211 | done |
| 212 | |
| 213 | echo "Link: $ARCH_INCLUDE/$2" |
| 214 | mkdir -p `dirname $ARCH_INCLUDE/$2` |
| 215 | ln -s $link_prefix/$2 $ARCH_INCLUDE/$2 |
| 216 | } |
| 217 | |
| 218 | common_headers () |
| 219 | { |
| 220 | srcs=`cd $1 && find . -type f` |
| 221 | # remove leading ./ |
| 222 | srcs=`echo $srcs | sed -e "s%\./%%g"` |
| 223 | |
| 224 | for src in $srcs; do |
| 225 | common_header $1 $src |
| 226 | done |
| 227 | } |
| 228 | |
| 229 | arch_header () |
| 230 | { |
| 231 | echo "Copy: $ARCH_INCLUDE/$2" |
| 232 | mkdir -p `dirname $ARCH_INCLUDE/$2` |
| 233 | install $1/$2 $ARCH_INCLUDE/$2 |
| 234 | # just to be safe |
| 235 | chmod a-x $ARCH_INCLUDE/$2 |
| 236 | } |
| 237 | |
| 238 | arch_headers () |
| 239 | { |
| 240 | srcs=`cd $1 && find . -type f` |
| 241 | # remove leading ./ |
| 242 | srcs=`echo $srcs | sed -e "s%\./%%g"` |
| 243 | |
| 244 | for src in $srcs; do |
| 245 | arch_header $1 $src |
| 246 | done |
| 247 | } |
| 248 | |
David 'Digit' Turner | 985338c | 2009-05-19 12:10:09 +0200 | [diff] [blame] | 249 | # ZLib headers |
| 250 | common_header $TOP/external/zlib zlib.h |
| 251 | common_header $TOP/external/zlib zconf.h |
| 252 | |
| 253 | # Jni header |
David 'Digit' Turner | b9a8479 | 2009-05-07 20:39:04 +0200 | [diff] [blame] | 254 | common_header $TOP/dalvik/libnativehelper/include/nativehelper jni.h |
David 'Digit' Turner | 985338c | 2009-05-19 12:10:09 +0200 | [diff] [blame] | 255 | |
| 256 | # libthread_db headers, not sure if this is needed for the NDK |
David 'Digit' Turner | b9a8479 | 2009-05-07 20:39:04 +0200 | [diff] [blame] | 257 | common_headers $BIONIC_ROOT/libthread_db/include |
| 258 | |
| 259 | # for libm, just copy math.h and fenv.h |
| 260 | common_header $BIONIC_ROOT/libm/include math.h |
| 261 | arch_header $BIONIC_ROOT/libm/include $ARCH/fenv.h |
| 262 | |
David 'Digit' Turner | 985338c | 2009-05-19 12:10:09 +0200 | [diff] [blame] | 263 | # our tiny C++ standard library |
David 'Digit' Turner | b9a8479 | 2009-05-07 20:39:04 +0200 | [diff] [blame] | 264 | common_headers $BIONIC_ROOT/libstdc++/include |
| 265 | |
David 'Digit' Turner | 985338c | 2009-05-19 12:10:09 +0200 | [diff] [blame] | 266 | # C library kernel headers |
David 'Digit' Turner | b9a8479 | 2009-05-07 20:39:04 +0200 | [diff] [blame] | 267 | common_headers $LIBC_ROOT/kernel/common |
| 268 | arch_headers $LIBC_ROOT/kernel/arch-arm |
| 269 | |
David 'Digit' Turner | 985338c | 2009-05-19 12:10:09 +0200 | [diff] [blame] | 270 | # C library headers |
David 'Digit' Turner | b9a8479 | 2009-05-07 20:39:04 +0200 | [diff] [blame] | 271 | common_headers $LIBC_ROOT/include |
| 272 | arch_headers $LIBC_ROOT/arch-$ARCH/include |
| 273 | |
| 274 | # Do we need to package the result |
| 275 | if [ $OPTION_PACKAGE = yes ] ; then |
| 276 | DATE=`date +%Y%m%d` |
| 277 | PKGFILE=/tmp/android-ndk-sysroot-$DATE.tar.bz2 |
| 278 | tar cjf $PKGFILE build/platforms/$PLATFORM |
| 279 | echo "Packaged in $PKGFILE" |
| 280 | fi |