Move platform files from build/platforms to platforms.

This also prepares for a major shift in the way files are stored under
development/ndk. Essentially, after this patch, you can have:

development/ndk/android-N/arch-$ABI/
  ==> contains only files relevant to API level 'N' that are not
      already part of API level 'N-1', 'N-2', etc...

development/ndk/android-N/samples/
  ==> contains samples that depend on features of a given API level 'N'.

The idea is to be able to work on API level 'N+1' by adding a single
directory under development/ndk. This is useful when branching between
several platform release branches. And it makes platform additions
trivial to examine and check.

+ build/tools/build-platforms.sh: group all files under development/ndk
  under $NDK/platforms and $NDK/samples, grouping everything together

+ build/tools/dev-cleanup.sh: get rid of all intermediate files,
  including $NDK/platforms and $NDK/samples

+ update make-release.h to deal with the new hotness.

Change-Id: I1c26052b862af4c319b9c0a5757c585929650562
diff --git a/.gitignore b/.gitignore
index 7b44afe..f7b9ce0 100644
--- a/.gitignore
+++ b/.gitignore
@@ -4,6 +4,8 @@
 Thumbs.db
 local.properties
 build.xml
+samples/*
+build/platforms/*
 samples/*/libs/
 samples/*/bin/
 samples/*/obj/
diff --git a/build/core/init.mk b/build/core/init.mk
index 0eddab7..627f9b8 100644
--- a/build/core/init.mk
+++ b/build/core/init.mk
@@ -276,15 +276,18 @@
 #
 NDK_PLATFORMS_ROOT := $(strip $(NDK_PLATFORMS_ROOT))
 ifndef NDK_PLATFORMS_ROOT
-    NDK_PLATFORMS_ROOT := $(strip $(wildcard $(NDK_ROOT)/build/platforms))
+    NDK_PLATFORMS_ROOT := $(strip $(wildcard $(NDK_ROOT)/platforms))
     ifndef NDK_PLATFORMS_ROOT
-    NDK_PLATFORMS_ROOT := $(strip $(wildcard $(call parent-dir,$(NDK_ROOT))/development/ndk/platforms))
+        NDK_PLATFORMS_ROOT := $(strip $(wildcard $(NDK_ROOT)/build/platforms))
     endif
 
     ifndef NDK_PLATFORMS_ROOT
-    $(call __ndk_info,Could not find platform files (headers and libraries))
-        $(call __ndk_info,Please define NDK_PLATFORMS_ROOT to point to a valid)
-        $(call __ndk_info,root directory for these.)
+        $(call __ndk_info,Could not find platform files (headers and libraries))
+        $(if $(strip $(wildcard $(NDK_ROOT)/RELEASE.TXT)),\
+            $(call __ndk_info,Please define NDK_PLATFORMS_ROOT to point to a valid directory.)\
+        ,\
+            $(call __ndk_info,Please run build/tools/build-platforms.sh to build the corresponding directory.)\
+        )
         $(call __ndk_error,Aborting)
     endif
 
diff --git a/build/tools/build-platforms.sh b/build/tools/build-platforms.sh
new file mode 100755
index 0000000..b22bfaf
--- /dev/null
+++ b/build/tools/build-platforms.sh
@@ -0,0 +1,230 @@
+#!/bin/sh
+#
+# Copyright (C) 2010 The Android Open Source Project
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#      http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+# build-platforms.sh
+#
+# This tool is used when packaging a new release, or when developing
+# the NDK itself. It will populate DST ($NDK/platforms by default)
+# with the content of SRC ($NDK/../development/ndk/platforms/ by default).
+#
+# The idea is that the content of $SRC/android-N/ only contains stuff
+# that is relevant to API level N, and not contain anything that is already
+# provided by API level N-1, N-2, etc..
+#
+. `dirname $0`/../core/ndk-common.sh
+
+extract_platforms_from ()
+{
+    (cd $SRCDIR/platforms && ls -d android-*) | sed -e "s!android-!!" | tr '\n' ' '
+}
+
+SRCDIR="../development/ndk"
+DSTDIR="$ANDROID_NDK_ROOT"
+
+ABIS="arm"
+PLATFORMS=`extract_platforms_from $SRCDIR`
+
+OPTION_HELP=no
+OPTION_PLATFORMS=
+OPTION_SRCDIR=
+OPTION_DSTDIR=
+
+VERBOSE=no
+VERBOSE2=no
+
+for opt do
+  optarg=`expr "x$opt" : 'x[^=]*=\(.*\)'`
+  case "$opt" in
+  --help|-h|-\?) OPTION_HELP=yes
+  ;;
+  --verbose)
+    if [ "$VERBOSE" = "yes" ] ; then
+        VERBOSE2=yes
+    else
+        VERBOSE=yes
+    fi
+    ;;
+  --src-dir=*)
+    OPTION_SRCDIR="$optarg"
+    ;;
+  --dst-dir=*)
+    OPTION_DSTDIR="$optarg"
+    ;;
+  --platform=*)
+    OPTION_PLATFORM=$optarg
+    ;;
+  --abi=*)
+    OPTION_ABI=$optarg
+    ;;
+  *)
+    echo "unknown option '$opt', use --help"
+    exit 1
+  esac
+done
+
+if [ $OPTION_HELP = "yes" ] ; then
+    echo "Collect files from an Android NDK development tree and assemble"
+    echo "the platform files appropriately into a final release structure."
+    echo ""
+    echo "options:"
+    echo ""
+    echo "  --help             print this message"
+    echo "  --verbose          enable verbose messages"
+    echo "  --src-dir=<path>   source directory for development platform files [$SRCDIR]"
+    echo "  --dst-dir=<path>   destination directory [$DSTDIR]"
+    echo "  --platform=<list>  space-separated list of API levels [$PLATFORMS]"
+    echo "  --abi=<list>       space-separated list of ABIs [$ABIS]"
+    echo ""
+    exit 0
+fi
+
+if [ -n "$OPTION_SRCDIR" ] ; then
+    SRCDIR="$OPTION_SRCDIR";
+    if [ ! -d "$SRCDIR" ] ; then
+        echo "ERROR: Source directory $SRCDIR does not exist !"
+        exit 1
+    fi
+    if [ ! -d "$SRCDIR/platforms/android-3" ] ; then
+        echo "ERROR: Invalid source directory: $SRCDIR"
+        echo "Please make sure it contains android-3 / android-4 etc..."
+        exit 1
+    fi
+else
+    SRCDIR=`dirname $ANDROID_NDK_ROOT`/development/ndk
+    log "Using source directory: $SRCDIR"
+fi
+
+if [ -n "$OPTION_PLATFORM" ] ; then
+    PLATFORMS="$OPTION_PLATFORM"
+else
+    # Build the list from the content of SRCDIR
+    PLATFORMS=`(cd $SRCDIR/platforms && ls -d android-*) | sed -e "s!android-!!" | tr '\n' ' '`
+    log "Using platforms: $PLATFORMS"
+fi
+
+
+if [ -n "$OPTION_DSTDIR" ] ; then
+    DSTDIR="$OPTION_DSTDIR"
+else
+    log "Using destination directory: $DSTDIR"
+fi
+
+if [ -n "$OPTION_ABI" ] ; then
+    ABIS="$OPTION_ABI"
+fi
+
+log "Checking source platforms."
+for PLATFORM in $PLATFORMS; do
+    DIR="$SRCDIR/platforms/android-$PLATFORM"
+    if [ ! -d $DIR ] ; then
+        echo "ERROR: Directory missing: $DIR"
+        echo "Please check your --platform=<list> option and try again."
+        exit 2
+    else
+        log "  $DIR"
+    fi
+done
+
+log "Checking source platform ABIs."
+BAD_ABIS=
+for ABI in $ABIS; do
+    eval CHECK_$ABI=no
+done
+for PLATFORM in $PLATFORMS; do
+    for ABI in $ABIS; do
+        DIR="$SRCDIR/platforms/android-$PLATFORM/arch-$ABI"
+        if [ -d $DIR ] ; then
+            log "  $DIR"
+            eval CHECK_$ABI=yes
+        fi
+    done
+done
+
+BAD_ABIS=
+for ABI in $ABIS; do
+    CHECK=`var_value CHECK_$ABI`
+    log "  $ABI check: $CHECK"
+    if [ "$CHECK" = no ] ; then
+        if [ -z "$BAD_ABIS" ] ; then
+            BAD_ABIS=$ABI
+        else
+            BAD_ABIS="$BAD_ABIS $ABI"
+        fi
+    fi
+done
+
+if [ -n "$BAD_ABIS" ] ; then
+    echo "ERROR: Source directory doesn't support these ABIs: $BAD_ABIS"
+    exit 3
+fi
+
+# $1: source directory (relative to $SRCDIR)
+# $2: destination directory (relative to $DSTDIR)
+# $3: description of directory contents (e.g. "sysroot" or "samples")
+copy_directory ()
+{
+    local SDIR="$SRCDIR/$1"
+    local DDIR="$DSTDIR/$2"
+    log "SDIR=$SDIR DDIR=$DDIR"
+    if [ -d "$SDIR" ] ; then
+        log "Copying $3 from \$SRC/$1 to \$DST/$2."
+        mkdir -p "$DDIR" && cp -rf "$SDIR"/* "$DDIR"
+        if [ $? != 0 ] ; then
+            echo "ERROR: Could not copy $3 directory $SDIR into $DDIR !"
+            exit 5
+        fi
+    fi
+}
+
+
+# Copy platform sysroot and samples into your destination
+#
+# $SRC/android-$PLATFORM/arch-$ABI/usr --> $DST/platforms/android-$PLATFORM/arch-$ABI/usr
+# $SRC/android-$PLATFORM/samples       --> $DST/samples
+#
+PREV_PLATFORM_DST=
+for PLATFORM in $PLATFORMS; do
+    NEW_PLATFORM=platforms/android-$PLATFORM
+    PLATFORM_SRC=$NEW_PLATFORM
+    PLATFORM_DST=$NEW_PLATFORM
+    if [ -n "$PREV_PLATFORM_DST" ] ; then
+        log "Copying \$DST/$PREV_PLATFORM_DST to \$DST/$PLATFORM_DST"
+        cp -r "$DSTDIR/$PREV_PLATFORM_DST" "$DSTDIR/$PLATFORM_DST"
+        if [ $? != 0 ] ; then
+            echo "ERROR: Could not copy previous sysroot to $DSTDIR/$NEW_PLATFORM"
+            exit 4
+        fi
+    fi
+    for ABI in $ABIS; do
+        SYSROOT=arch-$ABI/usr
+        copy_directory $PLATFORM_SRC/$SYSROOT $PLATFORM_DST/$SYSROOT sysroot
+    done
+    PREV_PLATFORM_DST=$PLATFORM_DST
+done
+
+# Copy platform samples and generic samples into your destination
+#
+# $SRC/samples/ --> $DST/samples/
+# $SRC/android-$PLATFORM/arch-$ABI/samples/ --> $DST/samples/
+#
+copy_directory  samples samples samples
+
+for PLATFORM in $PLATFORMS; do
+    # $SRC/platform-$PLATFORM/samples --> $DST/samples
+    copy_directory platforms/android-$PLATFORM/samples samples samples
+done
+
+log "Done !"
diff --git a/build/tools/dev-cleanup.sh b/build/tools/dev-cleanup.sh
new file mode 100755
index 0000000..a10064d
--- /dev/null
+++ b/build/tools/dev-cleanup.sh
@@ -0,0 +1,78 @@
+#!/bin/sh
+#
+# Copyright (C) 2010 The Android Open Source Project
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#      http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+# dev-cleanup.sh
+#
+# Remove any intermediate files (e.g. object files) from the development
+# directories.
+#
+. `dirname $0`/../core/ndk-common.sh
+
+DIR=$ANDROID_NDK_ROOT
+
+if [ -f $DIR/RELEASE/TXT ]; then
+    echo "ERROR: You cannot run this script in a release directory !"
+    exit 1
+fi
+if [ ! -d $DIR/.git ] ; then
+    echo "ERROR: You must call this script in a development directory !"
+    exit 1
+fi
+
+rm -rf $DIR/build/platforms
+rm -rf $DIR/platforms
+rm -rf $DIR/samples
+rm -rf $DIR/apps
+rm -rf $DIR/out
+
+DIR=`dirname $ANDROID_NDK_ROOT`/development/ndk
+if [ ! -d $DIR ] ; then
+    echo "WARNING: Development directory missing: $DIR"
+    exit 0
+fi
+
+clean_dir ()
+{
+    if [ -d "$1" ] ; then
+        echo "rm -rf $1"
+        rm -rf $1
+    fi
+}
+
+clean_file ()
+{
+    if [ -f "$1" ] ; then
+        echo "rm -f $1"
+        rm -f $1
+    fi
+}
+
+cleanup_project ()
+{
+    clean_dir $1/obj
+    clean_dir  $1/libs
+    clean_dir  $1/bin
+    clean_dir  $1/gen
+    clean_file $1/build.xml
+    clean_file $1/local.properties
+}
+
+for PROJECT in $DIR/samples/*; do
+    cleanup_project $PROJECT
+done
+for PROJECT in $DIR/platforms/android-*/samples/*; do
+    cleanup_project $PROJECT
+done
diff --git a/build/tools/make-release.sh b/build/tools/make-release.sh
index 4902b44..e76df56 100755
--- a/build/tools/make-release.sh
+++ b/build/tools/make-release.sh
@@ -47,17 +47,11 @@
 # be copied into the archive.
 USE_GIT_FILES=yes
 
-# Find the location of the platforms root directory
-PLATFORMS_ROOT=$NDK_ROOT_DIR/build/platforms
-if [ ! -d $PLATFORMS_ROOT ] ; then
-    PLATFORMS_ROOT=`dirname $NDK_ROOT_DIR`/development/ndk/platforms
-fi
+# set of platforms to package (all by default)
+PLATFORMS=
 
-# Find the location of the samples
-SAMPLES_ROOT=$NDK_ROOT_DIR/samples
-if [ ! -d $SAMPLES_ROOT ] ; then
-    SAMPLES_ROOT=`dirname $NDK_ROOT_DIR`/development/ndk/samples
-fi
+# Find the location of the platforms root directory
+DEVELOPMENT_ROOT=`dirname $NDK_ROOT_DIR`/development/ndk
 
 OPTION_HELP=no
 OPTION_OUT_DIR=
@@ -84,11 +78,11 @@
   ;;
   --systems=*) PREBUILT_SYSTEMS=$optarg
   ;;
+  --platforms=*) PLATFORMS=$optarg
+  ;;
   --no-git) USE_GIT_FILES=no
   ;;
-  --platforms-root=*) PLATFORMS_ROOT=$optarg
-  ;;
-  --samples-root=*) SAMPLES_ROOT=$optarg
+  --development-root=*) DEVELOPMENT_ROOT=$optarg
   ;;
   --out-dir=*) OPTION_OUT_DIR=$optarg
   ;;
@@ -127,9 +121,9 @@
     echo "  --prebuilt-prefix=PREFIX  Prefix of prebuilt binary tarballs [$PREBUILT_PREFIX]"
     echo "  --prebuilt-ndk=FILE       Specify a previous NDK package [$PREBUILT_NDK]"
     echo "  --systems=SYSTEMS         List of host system packages [$PREBUILT_SYSTEMS]"
+    echo "  --platforms=PLATFORMS     List of platforms to include [all]"
     echo "  --no-git                  Don't use git to list input files, take all of them."
-    echo "  --platforms-root=PATH     Specify platforms root directory [$PLATFORMS_ROOT]"
-    echo "  --samples-root=PATH       Specify samples root directory [$SAMPLES_ROOT]"
+    echo "  --development-root=PATH   Specify platforms/samples directory [$DEVELOPMENT_ROOT]"
     echo "  --out-dir=PATH            Specify output package directory [$OUT_DIR]"
     echo ""
     exit 1
@@ -235,27 +229,28 @@
     exit 2
 fi
 
-# copy platform files if needed
-echo "Copying platform files"
-if [ ! -d $REFERENCE/build/platforms ] ; then
-    mkdir -p $REFERENCE/build/platforms &&
-    (cd $PLATFORMS_ROOT && tar cf - android-*) | (cd $REFERENCE/build/platforms && tar xf -)
-    if [ $? != 0 ] ; then
-        echo "Could not copy platform files. Aborting."
-        exit 2
-    fi
+# copy platform and sample files
+echo "Copying platform and sample files"
+FLAGS="--src-dir=$DEVELOPMENT_ROOT --dst-dir=$REFERENCE"
+if [ "$VERBOSE2" = "yes" ] ; then
+  FLAGS="$FLAGS --verbose"
+fi
+PLATFORM_FLAGS=
+if [ -n "$PLATFORMS" ] ; then
+    PLATFORM_FLAGS="--platform=$PLATFORMS"
+fi
+$NDK_ROOT_DIR/build/tools/build-platforms.sh $FLAGS "$PLATFORM_FLAGS"
+if [ $? != 0 ] ; then
+    echo "Could not copy platform files. Aborting."
+    exit 2
 fi
 
-# copy sample files if needed
-echo "Copying samples"
-if [ ! -d $REFERENCE/samples ] ; then
-    mkdir -p $REFERENCE/samples &&
-    (cd $SAMPLES_ROOT && tar cf - *) | (cd $REFERENCE/samples && tar xf -)
-    if [ $? != 0 ] ; then
-        echo "Could not copy samples. Aborting."
-        exit 2
-    fi
-fi
+# create a release file named 'RELEASE.TXT' containing the release
+# name. This is used by the build script to detect whether you're
+# invoking the NDK from a release package or from the development
+# tree.
+#
+echo "$RELEASE" > $REFERENCE/RELEASE.TXT
 
 # now, for each system, create a package
 #
diff --git a/docs/CHANGES.TXT b/docs/CHANGES.TXT
index 4d685fa..47c88c6 100644
--- a/docs/CHANGES.TXT
+++ b/docs/CHANGES.TXT
@@ -88,6 +88,8 @@
 - build/tools/build-gcc.sh: copy the sysroot to the build directory. This avoids
   the generated toolchain binaries from hard-coding host build paths.
 
+- Platforms files are now under $NDK/platforms instead of $NDK/build/platforms
+
 -------------------------------------------------------------------------------
 android-ndk-r4b
 
diff --git a/docs/DEVELOPMENT.TXT b/docs/DEVELOPMENT.TXT
index ef2b9cc..47cb8a0 100644
--- a/docs/DEVELOPMENT.TXT
+++ b/docs/DEVELOPMENT.TXT
@@ -23,8 +23,31 @@
   git clone git://android.git.kernel.org/platform/development.git development
   export NDK=`pwd`/ndk
 
-II. Prebuilt binaries:
-======================
+II. Building the platforms tree:
+================================
+
+You need to do that once if you want to use the content of $NDK to build
+samples, tests or anything else:
+
+  $NDK/build/tools/build-platforms.sh
+
+What the script does is populate the $NDK/platforms and $NDK/samples
+directories from the content of development/ndk.
+
+What is under development/ndk is segregated by API level. This makes it
+easier to add a new platform to the tree, but is not well-suited to building
+stuff. The build-platforms.sh script will gather all files appropriately
+and place the result inside $NDK/platforms and $NDK/samples.
+
+Note: These directories are listed by $NDK/.gitignore, so they won't appear
+      on your git status. You can remove them if you want by running:
+
+        $NDK/build/tools/dev-cleanup.sh
+
+      which also removes all intermediate files and directories from $NDK.
+
+III. Prebuilt binaries:
+=======================
 
 The source tree does not contain the prebuilt binaries for the cross-compiler
 and other stuff that are necessary to generate machine code with the NDK.
@@ -72,8 +95,8 @@
       The generated package can easily be shared with other people.
 
 
-III. Generate new package releases:
-===================================
+IV. Generate new package releases:
+==================================
 
 Use the 'build/tools/make-release.sh' script to generate experimental
 NDK release packages. You will need prebuilt binaries to do that properly,
@@ -142,4 +165,3 @@
         The generated packages will be named android-ndk-<release>-<system>.zip
 
     Other options are supported, use --help to list them all.
-