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 . `dirname $0`/ndk-common.sh 32 33 # PLATFORM is the name of the current Android system platform 34 PLATFORM=android-3 35 36 # ABI is the target ABI name for the NDK 37 ABI=arm 38 39 OPTION_HELP=no 40 OPTION_BUILD_OUT= 41 OPTION_PLATFORM= 42 OPTION_PACKAGE=no 43 OPTION_ABI= 44 45 for opt do 46 optarg=`expr "x$opt" : 'x[^=]*=\(.*\)'` 47 case "$opt" in 48 --help|-h|-\?) OPTION_HELP=yes 49 ;; 50 --verbose) 51 if [ "$VERBOSE" = "yes" ] ; then 52 VERBOSE2=yes 53 else 54 VERBOSE=yes 55 fi 56 ;; 57 --platform=*) 58 OPTION_PLATFORM=$optarg 59 ;; 60 --build-out=*) 61 OPTION_BUILD_OUT=$optarg 62 ;; 63 --package) 64 OPTION_PACKAGE=yes 65 ;; 66 --abi=*) 67 OPTION_ABI=$optarg 68 ;; 69 *) 70 echo "unknown option '$opt', use --help" 71 exit 1 72 esac 73 done 74 75 if [ $OPTION_HELP = "yes" ] ; then 76 echo "Collect files from an Android build tree and assembles a sysroot" 77 echo "suitable for building a standalone toolchain or be used by the" 78 echo "Android NDK." 79 echo "" 80 echo "options:" 81 echo "" 82 echo " --help print this message" 83 echo " --verbose enable verbose messages" 84 echo " --platform=<name> generate sysroot for platform <name> (default is $PLATFORM)" 85 echo " --abi=<name> generate sysroot for abi <name> (default is $ABI)" 86 echo " --build-out=<path> set Android build out directory" 87 echo " --package generate sysroot package tarball" 88 echo "" 89 exit 0 90 fi 91 92 if [ -n "$OPTION_PLATFORM" ] ; then 93 PLATFORM=$OPTION_PLATFORM 94 fi 95 96 if [ -n "$OPTION_ABI" ] ; then 97 ABI=$OPTION_ABI 98 fi 99 100 case "$ABI" in 101 arm*) 102 ARCH=arm 103 ;; 104 *) 105 ARCH=$ABI 106 ;; 107 esac 108 109 110 # Get the root of the NDK from the current program location 111 NDK_ROOT=`dirname $0` 112 NDK_ROOT=`dirname $NDK_ROOT` 113 NDK_ROOT=`dirname $NDK_ROOT` 114 115 # Get the Android out directory 116 if [ -z "$OPTION_BUILD_OUT" ] ; then 117 if [ -z "$ANDROID_PRODUCT_OUT" ] ; then 118 echo "ANDROID_PRODUCT_OUT is not defined in your environment. Aborting" 119 exit 1 120 fi 121 if [ ! -d $ANDROID_PRODUCT_OUT ] ; then 122 echo "ANDROID_PRODUCT_OUT does not point to a valid directory. Aborting" 123 exit 1 124 fi 125 else 126 ANDROID_PRODUCT_OUT=$OPTION_BUILD_OUT 127 if [ ! -d $ANDROID_PRODUCT_OUT ] ; then 128 echo "The build out path is not a valid directory: $OPTION_BUILD_OUT" 129 exit 1 130 fi 131 fi 132 133 PRODUCT_DIR=$ANDROID_PRODUCT_OUT 134 SYSROOT=$NDK_ROOT/build/platforms/$PLATFORM/arch-$ABI 135 COMMON_ROOT=$NDK_ROOT/build/platforms/$PLATFORM/common 136 137 # clean up everything in existing sysroot 138 rm -rf $SYSROOT 139 mkdir -p $SYSROOT 140 141 rm -rf $COMMON_ROOT 142 mkdir -p $COMMON_ROOT 143 144 LIB_ROOT=$SYSROOT/usr/lib 145 146 install_file () 147 { 148 mkdir -p $2/`dirname $1` 149 cp -fp $1 $2/$1 150 } 151 152 install_helper () 153 { 154 (cd $1 && find . -type f | while read ff; do install_file $ff $2; done) 155 } 156 157 TOP=$PRODUCT_DIR/../../../.. 158 159 # CRT objects that need to be copied 160 CRT_OBJS_DIR=$PRODUCT_DIR/obj/lib 161 CRT_OBJS="$CRT_OBJS_DIR/crtbegin_static.o \ 162 $CRT_OBJS_DIR/crtbegin_dynamic.o \ 163 $CRT_OBJS_DIR/crtend_android.o" 164 165 # static libraries that need to be copied. 166 STATIC_LIBS_DIR=$PRODUCT_DIR/obj/STATIC_LIBRARIES 167 STATIC_LIBS="$STATIC_LIBS_DIR/libc_intermediates/libc.a \ 168 $STATIC_LIBS_DIR/libm_intermediates/libm.a \ 169 $STATIC_LIBS_DIR/libstdc++_intermediates/libstdc++.a 170 $STATIC_LIBS_DIR/libthread_db_intermediates/libthread_db.a" 171 172 # dynamic libraries that need to be copied. 173 DYNAMIC_LIBS_DIR=$PRODUCT_DIR/symbols/system/lib 174 DYNAMIC_LIBS="$DYNAMIC_LIBS_DIR/libdl.so \ 175 $DYNAMIC_LIBS_DIR/libc.so \ 176 $DYNAMIC_LIBS_DIR/libm.so \ 177 $DYNAMIC_LIBS_DIR/libstdc++.so \ 178 $DYNAMIC_LIBS_DIR/libthread_db.so" 179 180 # Copy all CRT objects and libraries 181 rm -rf $LIB_ROOT 182 mkdir -p $LIB_ROOT 183 cp -f $CRT_OBJS $STATIC_LIBS $DYNAMIC_LIBS $LIB_ROOT 184 185 # Check $TOP/bionic to see if this is new source layout. 186 if [ -d $TOP/bionic ] ;then 187 BIONIC_ROOT=$TOP/bionic 188 LIBC_ROOT=$BIONIC_ROOT/libc 189 else 190 BIONIC_ROOT=$TOP/system 191 LIBC_ROOT=$BIONIC_ROOT/bionic 192 fi 193 194 # Copy headers. This need to be done in the reverse order of inclusion 195 # in case there are different headers with the same name. 196 ARCH_INCLUDE=$SYSROOT/usr/include 197 rm -rf $ARCH_INCLUDE 198 mkdir -p $ARCH_INCLUDE 199 200 COMMON_INCLUDE=$COMMON_ROOT/include 201 rm -rf $COMMON_INCLUDE 202 mkdir -p $COMMON_INCLUDE 203 204 # Install a common header and create the appropriate arch-specific 205 # directory for it. 206 # 207 # $1: source directory 208 # $2: header path, relative to source directory 209 # 210 common_header () 211 { 212 echo "Copy: $COMMON_INCLUDE/$2" 213 mkdir -p `dirname $COMMON_INCLUDE/$2` 214 install $1/$2 $COMMON_INCLUDE/$2 215 # just to be safe 216 chmod a-x $COMMON_INCLUDE/$2 217 218 # the link prefix, used to point to common/ 219 # from arch-$ARCH/usr/ 220 link_prefix=../../common/include 221 222 # we need to count the number of directory separators in $2 223 # for each one of them, we're going to prepend ../ to the 224 # link prefix 225 for item in `echo $2 | tr '/' ' '`; do 226 link_prefix=../$link_prefix 227 done 228 229 echo "Link: $ARCH_INCLUDE/$2" 230 mkdir -p `dirname $ARCH_INCLUDE/$2` 231 ln -s $link_prefix/$2 $ARCH_INCLUDE/$2 232 } 233 234 common_headers () 235 { 236 srcs=`cd $1 && find . -type f` 237 # remove leading ./ 238 srcs=`echo $srcs | sed -e "s%\./%%g"` 239 240 for src in $srcs; do 241 common_header $1 $src 242 done 243 } 244 245 arch_header () 246 { 247 echo "Copy: $ARCH_INCLUDE/$2" 248 mkdir -p `dirname $ARCH_INCLUDE/$2` 249 install $1/$2 $ARCH_INCLUDE/$2 250 # just to be safe 251 chmod a-x $ARCH_INCLUDE/$2 252 } 253 254 arch_headers () 255 { 256 srcs=`cd $1 && find . -type f` 257 # remove leading ./ 258 srcs=`echo $srcs | sed -e "s%\./%%g"` 259 260 for src in $srcs; do 261 arch_header $1 $src 262 done 263 } 264 265 # ZLib headers 266 common_header $TOP/external/zlib zlib.h 267 common_header $TOP/external/zlib zconf.h 268 269 # Jni header 270 common_header $TOP/libnativehelper/include/nativehelper jni.h 271 272 # libthread_db headers, not sure if this is needed for the NDK 273 common_headers $BIONIC_ROOT/libthread_db/include 274 275 # for libm, just copy math.h and fenv.h 276 common_header $BIONIC_ROOT/libm/include math.h 277 if [ "$ARCH" = "x86" ]; then 278 arch_header $BIONIC_ROOT/libm/include i387/fenv.h 279 else 280 arch_header $BIONIC_ROOT/libm/include $ARCH/fenv.h 281 fi 282 283 # our tiny C++ standard library 284 common_headers $BIONIC_ROOT/libstdc++/include 285 286 # C library kernel headers 287 common_headers $LIBC_ROOT/kernel/common 288 arch_headers $LIBC_ROOT/kernel/arch-$ARCH 289 290 # C library headers 291 common_headers $LIBC_ROOT/include 292 arch_headers $LIBC_ROOT/arch-$ARCH/include 293 294 # Do we need to package the result 295 if [ $OPTION_PACKAGE = yes ] ; then 296 DATE=`date +%Y%m%d` 297 PKGFILE=/tmp/ndk-$USER/android-ndk-sysroot-$DATE.tar.bz2 298 tar cjf $PKGFILE build/platforms/$PLATFORM/arch-$ARCH 299 echo "Packaged in $PKGFILE" 300 fi 301