1#!/bin/sh 2# 3# Copyright (C) 2010 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-platforms.sh 18# 19# This tool is used when packaging a new release, or when developing 20# the NDK itself. It will populate DST ($NDK/platforms by default) 21# with the content of SRC ($NDK/../development/ndk/platforms/ by default). 22# 23# The idea is that the content of $SRC/android-N/ only contains stuff 24# that is relevant to API level N, and not contain anything that is already 25# provided by API level N-1, N-2, etc.. 26# 27# More precisely, for each architecture A: 28# $SRC/android-N/include --> $DST/android-N/arch-A/usr/include 29# $SRC/android-N/arch-A/include --> $DST/android-N/arch-A/usr/include 30# $SRC/android-N/arch-A/lib --> $DST/android-N/arch-A/usr/lib 31# 32# For backwards compatibility: 33# $SRC/android-N/arch-A/usr/include --> $DST/android-N/arch-A/usr/include 34# $SRC/android-N/arch-A/usr/lib --> $DST/android-N/arch-A/usr/lib 35# 36# Repeat after that for N+1, N+2, etc.. 37# 38 39. `dirname $0`/prebuilt-common.sh 40 41# Return the list of platform supported from $1/platforms 42# as a single space-separated list of levels. (e.g. "3 4 5 8 9") 43# $1: source directory 44extract_platforms_from () 45{ 46 if [ -d "$1" ] ; then 47 (cd "$1/platforms" && ls -d android-*) | sed -e "s!android-!!" | tr '\n' ' ' 48 else 49 echo "" 50 fi 51} 52 53SRCDIR="../development/ndk" 54DSTDIR="$ANDROID_NDK_ROOT" 55 56ARCHS="arm" 57PLATFORMS=`extract_platforms_from "$SRCDIR"` 58 59OPTION_HELP=no 60OPTION_PLATFORMS= 61OPTION_SRCDIR= 62OPTION_DSTDIR= 63OPTION_NO_SAMPLES=no 64OPTION_NO_SYMLINKS=no 65OPTION_ARCH= 66OPTION_ABI= 67 68VERBOSE=no 69VERBOSE2=no 70 71for opt do 72 optarg=`expr "x$opt" : 'x[^=]*=\(.*\)'` 73 case "$opt" in 74 --help|-h|-\?) OPTION_HELP=yes 75 ;; 76 --verbose) 77 if [ "$VERBOSE" = "yes" ] ; then 78 VERBOSE2=yes 79 else 80 VERBOSE=yes 81 fi 82 ;; 83 --src-dir=*) 84 OPTION_SRCDIR="$optarg" 85 ;; 86 --dst-dir=*) 87 OPTION_DSTDIR="$optarg" 88 ;; 89 --platform=*) 90 OPTION_PLATFORM=$optarg 91 ;; 92 --arch=*) 93 OPTION_ARCH=$optarg 94 ;; 95 --abi=*) # We still support this for backwards-compatibility 96 OPTION_ABI=$optarg 97 ;; 98 --no-samples) 99 OPTION_NO_SAMPLES=yes 100 ;; 101 --no-symlinks) 102 OPTION_NO_SYMLINKS=yes 103 ;; 104 *) 105 echo "unknown option '$opt', use --help" 106 exit 1 107 esac 108done 109 110if [ $OPTION_HELP = "yes" ] ; then 111 echo "Collect files from an Android NDK development tree and assemble" 112 echo "the platform files appropriately into a final release structure." 113 echo "" 114 echo "options:" 115 echo "" 116 echo " --help Print this message" 117 echo " --verbose Enable verbose messages" 118 echo " --src-dir=<path> Source directory for development platform files [$SRCDIR]" 119 echo " --dst-dir=<path> Destination directory [$DSTDIR]" 120 echo " --platform=<list> List of API levels [$PLATFORMS]" 121 echo " --arch=<list> List of CPU architectures [$ARCHS]" 122 echo " --no-samples Ignore samples" 123 echo " --no-symlinks Don't create symlinks, copy files instead" 124 echo "" 125 exit 0 126fi 127 128if [ -n "$OPTION_SRCDIR" ] ; then 129 SRCDIR="$OPTION_SRCDIR"; 130 if [ ! -d "$SRCDIR" ] ; then 131 echo "ERROR: Source directory $SRCDIR does not exist !" 132 exit 1 133 fi 134 if [ ! -d "$SRCDIR/platforms/android-3" ] ; then 135 echo "ERROR: Invalid source directory: $SRCDIR" 136 echo "Please make sure it contains platforms/android-3 etc..." 137 exit 1 138 fi 139else 140 SRCDIR=`dirname $ANDROID_NDK_ROOT`/development/ndk 141 log "Using source directory: $SRCDIR" 142fi 143 144if [ -n "$OPTION_PLATFORM" ] ; then 145 PLATFORMS=$(commas_to_spaces $OPTION_PLATFORM) 146else 147 # Build the list from the content of SRCDIR 148 PLATFORMS=`extract_platforms_from "$SRCDIR"` 149 log "Using platforms: $PLATFORMS" 150fi 151 152# Remove the android- prefix of any platform name 153PLATFORMS=$(echo $PLATFORMS | tr ' ' '\n' | sed -e 's!android-!!g' | tr '\n' ' ') 154 155if [ -n "$OPTION_DSTDIR" ] ; then 156 DSTDIR="$OPTION_DSTDIR" 157else 158 log "Using destination directory: $DSTDIR" 159fi 160 161# Handle architecture list 162# 163# We support both --arch and --abi for backwards compatibility reasons 164# --arch is the new hotness, --abi is deprecated. 165# 166if [ -n "$OPTION_ARCH" ]; then 167 OPTION_ARCH=$(commas_to_spaces $OPTION_ARCH) 168fi 169 170if [ -n "$OPTION_ABI" ] ; then 171 echo "WARNING: --abi=<names> is deprecated. Use --arch=<names> instead!" 172 OPTION_ABI=$(commas_to_spaces $OPTION_ABI) 173 if [ -n "$OPTION_ARCH" -a "$OPTION_ARCH" != "$OPTION_ABI" ]; then 174 echo "ERROR: You can't use both --abi and --arch with different values!" 175 exit 1 176 fi 177 OPTION_ARCH=$OPTION_ABI 178fi 179 180if [ -n "$OPTION_ARCH" ] ; then 181 ARCHS="$OPTION_ARCH" 182fi 183log "Using architectures: $(commas_to_spaces $ARCHS)" 184 185log "Checking source platforms." 186for PLATFORM in $PLATFORMS; do 187 DIR="$SRCDIR/platforms/android-$PLATFORM" 188 if [ ! -d $DIR ] ; then 189 echo "ERROR: Directory missing: $DIR" 190 echo "Please check your --platform=<list> option and try again." 191 exit 2 192 else 193 log " $DIR" 194 fi 195done 196 197log "Checking source platform architectures." 198BAD_ARCHS= 199for ARCH in $ARCHS; do 200 eval CHECK_$ARCH=no 201done 202for PLATFORM in $PLATFORMS; do 203 for ARCH in $ARCHS; do 204 DIR="$SRCDIR/platforms/android-$PLATFORM/arch-$ARCH" 205 if [ -d $DIR ] ; then 206 log " $DIR" 207 eval CHECK_$ARCH=yes 208 fi 209 done 210done 211 212BAD_ARCHS= 213for ARCH in $ARCHS; do 214 CHECK=`var_value CHECK_$ARCH` 215 log " $ARCH check: $CHECK" 216 if [ "$CHECK" = no ] ; then 217 if [ -z "$BAD_ARCHS" ] ; then 218 BAD_ARCHS=$ARCH 219 else 220 BAD_ARCHS="$BAD_ARCHS $ARCH" 221 fi 222 fi 223done 224 225if [ -n "$BAD_ARCHS" ] ; then 226 echo "ERROR: Source directory doesn't support these ARCHs: $BAD_ARCHS" 227 exit 3 228fi 229 230# $1: source directory (relative to $SRCDIR) 231# $2: destination directory (relative to $DSTDIR) 232# $3: description of directory contents (e.g. "sysroot" or "samples") 233copy_src_directory () 234{ 235 local SDIR="$SRCDIR/$1" 236 local DDIR="$DSTDIR/$2" 237 if [ -d "$SDIR" ] ; then 238 log "Copying $3 from \$SRC/$1 to \$DST/$2." 239 mkdir -p "$DDIR" && (cd "$SDIR" && tar chf - *) | (tar xf - -C "$DDIR") 240 if [ $? != 0 ] ; then 241 echo "ERROR: Could not copy $3 directory $SDIR into $DDIR !" 242 exit 5 243 fi 244 fi 245} 246 247# Create a symlink-copy of directory $1 into $2 248# This function is recursive. 249# 250# $1: source directory (relative to $SRCDIR) 251# $2: destination directory (relative to $DSTDIR) 252symlink_src_directory () 253{ 254 local files subdirs file subdir rev 255 mkdir -p "$DSTDIR/$2" 256 files=`cd $DSTDIR/$1 && ls -1p | grep -v -e '.*/'` 257 subdirs=`cd $DSTDIR/$1 && ls -1p | grep -e '.*/' | sed -e 's!/$!!g'` 258 rev=`reverse_path $1` 259 for file in $files; do 260 ln -s $rev/$1/$file $DSTDIR/$2/$file 261 done 262 for subdir in $subdirs; do 263 symlink_src_directory $1/$subdir $2/$subdir 264 done 265} 266 267# Copy platform sysroot and samples into your destination 268# 269 270# $SRC/android-$PLATFORM/include --> $DST/platforms/android-$PLATFORM/arch-$ARCH/usr/include 271# $SRC/android-$PLATFORM/arch-$ARCH/include --> $DST/platforms/android-$PLATFORM/arch-$ARCH/usr/include 272# for compatibility: 273# $SRC/android-$PLATFORM/arch-$ARCH/usr/include --> $DST/platforms/android-$PLATFORM/arch-$ARCH/usr/include 274 275 276 277# $SRC/android-$PLATFORM/arch-$ARCH/usr --> $DST/platforms/android-$PLATFORM/arch-$ARCH/usr 278# $SRC/android-$PLATFORM/samples --> $DST/samples 279# 280rm -rf $DSTDIR/platforms && mkdir -p $DSTDIR/platforms 281PREV_PLATFORM_DST= 282for PLATFORM in $PLATFORMS; do 283 NEW_PLATFORM=platforms/android-$PLATFORM 284 PLATFORM_SRC=$NEW_PLATFORM 285 PLATFORM_DST=$NEW_PLATFORM 286 dump "Copying android-$PLATFORM platform files" 287 if [ -n "$PREV_PLATFORM_DST" ] ; then 288 if [ "$OPTION_NO_SYMLINKS" = "yes" ] ; then 289 log "Copying \$DST/$PREV_PLATFORM_DST to \$DST/$PLATFORM_DST" 290 #cp -r $DSTDIR/$PREV_PLATFORM_DST $DSTDIR/$PLATFORM_DST 291 copy_directory "$DSTDIR/$PREV_PLATFORM_DST" "$DSTDIR/$PLATFORM_DST" 292 else 293 log "Symlink-copying \$DST/$PREV_PLATFORM_DST to \$DST/$PLATFORM_DST" 294 symlink_src_directory $PREV_PLATFORM_DST $PLATFORM_DST 295 fi 296 if [ $? != 0 ] ; then 297 echo "ERROR: Could not copy previous sysroot to $DSTDIR/$NEW_PLATFORM" 298 exit 4 299 fi 300 fi 301 for ARCH in $ARCHS; do 302 SYSROOT=arch-$ARCH/usr 303 log "Copy $ARCH sysroot files from \$SRC/android-$PLATFORM over \$DST/android-$PLATFORM/arch-$ARCH" 304 copy_src_directory $PLATFORM_SRC/include $PLATFORM_DST/$SYSROOT/include "sysroot headers" 305 copy_src_directory $PLATFORM_SRC/arch-$ARCH/include $PLATFORM_DST/$SYSROOT/include "sysroot headers" 306 copy_src_directory $PLATFORM_SRC/arch-$ARCH/lib $PLATFORM_DST/$SYSROOT/lib "sysroot libs" 307 copy_src_directory $PLATFORM_SRC/$SYSROOT $PLATFORM_DST/$SYSROOT "sysroot" 308 done 309 PREV_PLATFORM_DST=$PLATFORM_DST 310done 311 312if [ "$OPTION_NO_SAMPLES" = no ] ; then 313 # Copy platform samples and generic samples into your destination 314 # 315 # $SRC/samples/ --> $DST/samples/ 316 # $SRC/android-$PLATFORM/samples/ --> $DST/samples 317 # 318 dump "Copying generic samples" 319 rm -rf $DSTDIR/samples && mkdir -p $DSTDIR/samples 320 copy_src_directory samples samples samples 321 322 for PLATFORM in $PLATFORMS; do 323 dump "Copy android-$PLATFORM samples" 324 # $SRC/platform-$PLATFORM/samples --> $DST/samples 325 copy_src_directory platforms/android-$PLATFORM/samples samples samples 326 done 327 328 # Cleanup generated files in samples 329 rm -rf "$DSTDIR/samples/*/obj" 330 rm -rf "$DSTDIR/samples/*/libs" 331fi 332 333log "Done !" 334