Allow platforms and samples to be located under development/ndk/{platforms,samples}

Change-Id: Idb631a91de71c60dc6edc7d1989925fff9adc1aa
diff --git a/build/core/definitions.mk b/build/core/definitions.mk
index f4a70a6..0c9ad42 100644
--- a/build/core/definitions.mk
+++ b/build/core/definitions.mk
@@ -303,6 +303,13 @@
 # =============================================================================
 
 # -----------------------------------------------------------------------------
+# Function : parent-dir
+# Arguments: 1: path
+# Returns  : Parent dir or path of $1, with final separator removed.
+# -----------------------------------------------------------------------------
+parent-dir = $(patsubst %/,%,$(dir $1))
+
+# -----------------------------------------------------------------------------
 # Function : check-user-define
 # Arguments: 1: name of variable that must be defined by the user
 #            2: name of Makefile where the variable should be defined
@@ -357,7 +364,7 @@
 # Returns  : the directory of the current Makefile
 # Usage    : $(my-dir)
 # -----------------------------------------------------------------------------
-my-dir = $(patsubst %/,%,$(dir $(lastword $(MAKEFILE_LIST))))
+my-dir = $(call parent-dir,$(lastword $(MAKEFILE_LIST)))
 
 # -----------------------------------------------------------------------------
 # Function : all-makefiles-under
diff --git a/build/core/init.mk b/build/core/init.mk
index 7d9ffa9..e61c079 100644
--- a/build/core/init.mk
+++ b/build/core/init.mk
@@ -263,7 +263,34 @@
 #
 # ====================================================================
 
-NDK_PLATFORMS_ROOT := $(NDK_ROOT)/build/platforms
+# The platform files were moved in the Android source tree from
+# $TOP/ndk/build/platforms to $TOP/development/ndk/platforms. However,
+# the official NDK release packages still place them under the old
+# location for now, so deal with this here
+#
+NDK_PLATFORMS_ROOT := $(strip $(NDK_PLATFORMS_ROOT))
+ifndef NDK_PLATFORMS_ROOT
+    NDK_PLATFORMS_ROOT := $(strip $(wildcard $(NDK_ROOT)/build/platforms))
+    ifndef NDK_PLATFORMS_ROOT
+    NDK_PLATFORMS_ROOT := $(strip $(wildcard $(call parent-dir,$(NDK_ROOT))/development/ndk/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_error,Aborting)
+    endif
+
+    $(call ndk_log,Found platform root directory: $(NDK_PLATFORMS_ROOT))
+else
+    ifeq ($(strip $(wildcard $(NDK_PLATFORMS_ROOT)/android-*)),)
+        $(call __ndk_info,Your NDK_PLATFORMS_ROOT points to an invalid directory)
+        $(call __ndk_info,Current value: $(NDK_PLATFORMS_ROOT))
+        $(call __ndk_error,Aborting)
+    endif
+endif
+
 NDK_ALL_PLATFORMS := $(strip $(notdir $(wildcard $(NDK_PLATFORMS_ROOT)/android-*)))
 $(call ndk_log,Found supported platforms: $(NDK_ALL_PLATFORMS))
 
diff --git a/build/tools/make-release.sh b/build/tools/make-release.sh
index 833717e..661b1bb 100755
--- a/build/tools/make-release.sh
+++ b/build/tools/make-release.sh
@@ -44,6 +44,18 @@
 # 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
+
+# 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
+
 OPTION_HELP=no
 
 for opt do
@@ -70,6 +82,10 @@
   ;;
   --no-git) USE_GIT_FILES=no
   ;;
+  --platforms-root=*) PLATFORMS_ROOT=$optarg
+  ;;
+  --samples-root=*) SAMPLES_ROOT=$optarg
+  ;;
   *)
     echo "unknown option '$opt', use --help"
     exit 1
@@ -106,6 +122,8 @@
     echo "  --prebuilt-ndk=FILE       Specify a previous NDK package [$PREBUILT_NDK]"
     echo "  --systems=SYSTEMS         List of host system packages [$PREBUILT_SYSTEMS]"
     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 ""
     exit 1
 fi
@@ -195,6 +213,29 @@
     exit 2
 fi
 
+# copy platform files if needed
+echo "Copying platform files"
+if [ ! -d $REFERENCE/build/platforms ] ; then
+    echo "REF=$REFERENCE/build/platforms"
+    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
+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
+
 # now, for each system, create a package
 #
 for SYSTEM in $PREBUILT_SYSTEMS; do
diff --git a/build/tools/prebuilt-common.sh b/build/tools/prebuilt-common.sh
index cd53e4e..43c6bae 100644
--- a/build/tools/prebuilt-common.sh
+++ b/build/tools/prebuilt-common.sh
@@ -327,6 +327,22 @@
     fi
 }
 
+
+# If SYSROOT is empty, check that $1/$2 contains a sysroot
+# and set the variable to it.
+#
+# $1: sysroot path
+# $2: platform/arch suffix
+check_sysroot ()
+{
+    if [ -z "$SYSROOT" ] ; then
+        log "Probing directory for sysroot: $1/$2"
+        if [ -d $1/$2 ] ; then
+            SYSROOT=$1/$2
+        fi
+    fi
+}
+
 # Determine sysroot
 # $1: Option value (or empty)
 #
@@ -336,13 +352,16 @@
         eval SYSROOT="$1"
         log "Using specified sysroot: $1"
     else
-        SYSROOT_SUFFIX=build/platforms/$PLATFORM/arch-$ARCH
-        if [ -d $NDK_DIR/$SYSROOT_SUFFIX ] ; then
-            SYSROOT=$NDK_DIR/$SYSROOT_SUFFIX
-            log "Using target NDK sysroot: $SYSROOT"
-        else
-            SYSROOT=$ANDROID_NDK_ROOT/$SYSROOT_SUFFIX
-            log "Using install NDK sysroot: $SYSROOT"
+        SYSROOT_SUFFIX=$PLATFORM/arch-$ARCH
+        SYSROOT=
+        check_sysroot $NDK_DIR/build/platforms $SYSROOT_SUFFIX
+        check_sysroot $ANDROID_NDK_ROOT/build/platforms $SYSROOT_SUFFIX
+        check_sysroot `dirname $ANDROID_NDK_ROOT`/development/ndk/platforms $SYSROOT_SUFFIX
+
+        if [ -z "$SYSROOT" ] ; then
+            echo "ERROR: Could not find NDK sysroot path for $SYSROOT_SUFFIX."
+            echo "       Use --sysroot=<path> to specify one."
+            exit 1
         fi
     fi