Merge "Have all targets in build.ninja"
diff --git a/core/config.mk b/core/config.mk
index 0a01f41..0b1c020 100644
--- a/core/config.mk
+++ b/core/config.mk
@@ -708,7 +708,7 @@
 # Set up RS prebuilt variables for compatibility library
 
 RS_PREBUILT_CLCORE := prebuilts/sdk/renderscript/lib/$(TARGET_ARCH)/librsrt_$(TARGET_ARCH).bc
-RS_PREBUILT_LIBPATH := -L prebuilts/ndk/8/platforms/android-9/arch-$(TARGET_ARCH)/usr/lib
+RS_PREBUILT_LIBPATH := -L prebuilts/ndk/current/platforms/android-9/arch-$(TARGET_ARCH)/usr/lib
 RS_PREBUILT_COMPILER_RT := prebuilts/sdk/renderscript/lib/$(TARGET_ARCH)/libcompiler_rt.a
 
 # API Level lists for Renderscript Compat lib.
diff --git a/core/definitions.mk b/core/definitions.mk
index 5a8fc8d..f0abffb 100644
--- a/core/definitions.mk
+++ b/core/definitions.mk
@@ -226,6 +226,29 @@
 endef
 
 ###########################################################
+## Find all of the cpp files under the named directories.
+## LOCAL_CPP_EXTENSION is respected if set.
+## Meant to be used like:
+##    SRC_FILES := $(call all-cpp-files-under,src tests)
+###########################################################
+
+define all-cpp-files-under
+$(sort $(patsubst ./%,%, \
+  $(shell cd $(LOCAL_PATH) ; \
+          find -L $(1) -name "*$(or $(LOCAL_CPP_EXTENSION),.cpp)" -and -not -name ".*") \
+ ))
+endef
+
+###########################################################
+## Find all of the cpp files from here.  Meant to be used like:
+##    SRC_FILES := $(call all-subdir-cpp-files)
+###########################################################
+
+define all-subdir-cpp-files
+$(call all-cpp-files-under,.)
+endef
+
+###########################################################
 ## Find all files named "I*.aidl" under the named directories,
 ## which must be relative to $(LOCAL_PATH).  The returned list
 ## is relative to $(LOCAL_PATH).
@@ -392,7 +415,7 @@
 
 define find-parent-file
 $(strip \
-  $(eval _fpf := $(wildcard $(foreach f, $(2), $(strip $(1))/$(f)))) \
+  $(eval _fpf := $(sort $(wildcard $(foreach f, $(2), $(strip $(1))/$(f))))) \
   $(if $(_fpf),$(_fpf), \
        $(if $(filter-out ./ .,$(1)), \
              $(call find-parent-file,$(patsubst %/,%,$(dir $(1))),$(2)) \
diff --git a/core/droiddoc.mk b/core/droiddoc.mk
index ab2254f..cc2a915 100644
--- a/core/droiddoc.mk
+++ b/core/droiddoc.mk
@@ -125,7 +125,7 @@
 ##
 
 droiddoc_templates := \
-    $(shell find $(LOCAL_DROIDDOC_CUSTOM_TEMPLATE_DIR) -type f)
+    $(sort $(shell find $(LOCAL_DROIDDOC_CUSTOM_TEMPLATE_DIR) -type f))
 
 droiddoc := \
 	$(HOST_JDK_TOOLS_JAR) \
@@ -142,7 +142,7 @@
 html_dir_files :=
 ifneq ($(strip $(LOCAL_DROIDDOC_HTML_DIR)),)
 $(full_target): PRIVATE_DROIDDOC_HTML_DIR := -htmldir $(LOCAL_PATH)/$(LOCAL_DROIDDOC_HTML_DIR)
-html_dir_files := $(shell find $(LOCAL_PATH)/$(LOCAL_DROIDDOC_HTML_DIR) -type f)
+html_dir_files := $(sort $(shell find $(LOCAL_PATH)/$(LOCAL_DROIDDOC_HTML_DIR) -type f))
 else
 $(full_target): PRIVATE_DROIDDOC_HTML_DIR :=
 endif
diff --git a/core/envsetup.mk b/core/envsetup.mk
index 2899d80..772b294 100644
--- a/core/envsetup.mk
+++ b/core/envsetup.mk
@@ -134,11 +134,11 @@
 # make sure only one exists.
 # Real boards should always be associated with an OEM vendor.
 board_config_mk := \
-	$(strip $(wildcard \
+	$(strip $(sort $(wildcard \
 		$(SRC_TARGET_DIR)/board/$(TARGET_DEVICE)/BoardConfig.mk \
 		$(shell test -d device && find device -maxdepth 4 -path '*/$(TARGET_DEVICE)/BoardConfig.mk') \
 		$(shell test -d vendor && find vendor -maxdepth 4 -path '*/$(TARGET_DEVICE)/BoardConfig.mk') \
-	))
+	)))
 ifeq ($(board_config_mk),)
   $(error No config file found for TARGET_DEVICE $(TARGET_DEVICE))
 endif
diff --git a/core/java_common.mk b/core/java_common.mk
index 2195691..6d5c2dd 100644
--- a/core/java_common.mk
+++ b/core/java_common.mk
@@ -55,7 +55,7 @@
     $(foreach dir,$(LOCAL_JAVA_RESOURCE_DIRS), \
 	$(subst $(space),:,$(strip \
 		$(LOCAL_PATH)/$(dir): \
-	    $(patsubst ./%,%,$(shell cd $(LOCAL_PATH)/$(dir) && \
+	    $(patsubst ./%,%,$(sort $(shell cd $(LOCAL_PATH)/$(dir) && \
 		find . \
 		    -type d -a -name ".svn" -prune -o \
 		    -type f \
@@ -66,7 +66,7 @@
 			-a \! -name ".DS_Store" \
 			-a \! -name "*~" \
 			-print \
-		    )) \
+		    ))) \
 	)) \
     )
   java_resource_file_groups := $(filter-out %:,$(java_resource_file_groups))
diff --git a/core/legacy_prebuilts.mk b/core/legacy_prebuilts.mk
index c477900..f4633d0 100644
--- a/core/legacy_prebuilts.mk
+++ b/core/legacy_prebuilts.mk
@@ -22,68 +22,11 @@
 # to add any new such module in the system
 
 GRANDFATHERED_ALL_PREBUILT := \
-	akmd2 \
-	ap_gain.bin \
-	AVRCP.kl \
-	batch \
-	bitmap_size.txt \
 	bmgr \
-	bp.img \
-	brcm_guci_drv \
-	bypassfactory \
-	cdt.bin \
-	chat-ril \
-	cpcap-key.kl \
-	egl.cfg \
-	firmware_error.565 \
-	firmware_install.565 \
-	ftmipcd \
-	gps.conf \
-	gpsconfig.xml \
-	gps.stingray.so \
-	gralloc.omap3.so \
-	gralloc.tegra.so \
-	hwcomposer.tegra.so \
 	ime \
-	init.goldfish.rc \
-	init.goldfish.sh \
-	init.olympus.rc \
-	init.sholes.rc \
-	init.stingray.rc \
 	input \
-	kernel \
-	lbl \
-	libEGL_POWERVR_SGX530_121.so \
-	libEGL_tegra.so \
-	libGLESv1_CM_POWERVR_SGX530_121.so \
-	libGLESv1_CM_tegra.so \
-	libGLESv2_POWERVR_SGX530_121.so \
-	libGLESv2_tegra.so \
-	libmoto_ril.so \
-	libpppd_plugin-ril.so \
-	libril_rds.so \
-	location \
-	location.cfg \
-	main.conf \
-	mbm.bin \
-	mbm_consumer.bin \
-	mdm_panicd \
 	monkey \
 	pm \
-	pppd-ril \
-	pppd-ril.options \
-	qwerty.kl \
-	radio.img \
-	rdl.bin \
 	RFFspeed_501.bmd \
 	RFFstd_501.bmd \
-	savebpver \
-	sholes-keypad.kl \
-	suplcerts.bks \
-	svc \
-	tcmd \
-	ueventd.goldfish.rc \
-	ueventd.olympus.rc \
-	ueventd.stingray.rc \
-	vold.fstab \
-	wl1271.bin
+	svc
diff --git a/core/pdk_config.mk b/core/pdk_config.mk
index 838754f..35b06ae 100644
--- a/core/pdk_config.mk
+++ b/core/pdk_config.mk
@@ -21,11 +21,11 @@
 # Most PDK project paths should be using vendor/pdk/TARGET_DEVICE
 # but some legacy ones (e.g. mini_armv7a_neon generic PDK) were setup
 # with vendor/pdk/TARGET_PRODUCT.
-_pdk_fusion_default_platform_zip = $(wildcard \
-vendor/pdk/$(TARGET_DEVICE)/$(TARGET_PRODUCT)-$(TARGET_BUILD_VARIANT)/platform/platform.zip \
-vendor/pdk/$(TARGET_DEVICE)/$(patsubst aosp_%,full_%,$(TARGET_PRODUCT))-$(TARGET_BUILD_VARIANT)/platform/platform.zip \
-vendor/pdk/$(TARGET_PRODUCT)/$(TARGET_PRODUCT)-$(TARGET_BUILD_VARIANT)/platform/platform.zip \
-vendor/pdk/$(TARGET_PRODUCT)/$(patsubst aosp_%,full_%,$(TARGET_PRODUCT))-$(TARGET_BUILD_VARIANT)/platform/platform.zip)
+_pdk_fusion_default_platform_zip = $(strip \
+  $(wildcard vendor/pdk/$(TARGET_DEVICE)/$(TARGET_PRODUCT)-$(TARGET_BUILD_VARIANT)/platform/platform.zip) \
+  $(wildcard vendor/pdk/$(TARGET_DEVICE)/$(patsubst aosp_%,full_%,$(TARGET_PRODUCT))-$(TARGET_BUILD_VARIANT)/platform/platform.zip) \
+  $(wildcard vendor/pdk/$(TARGET_PRODUCT)/$(TARGET_PRODUCT)-$(TARGET_BUILD_VARIANT)/platform/platform.zip) \
+  $(wildcard vendor/pdk/$(TARGET_PRODUCT)/$(patsubst aosp_%,full_%,$(TARGET_PRODUCT))-$(TARGET_BUILD_VARIANT)/platform/platform.zip))
 ifneq (,$(_pdk_fusion_default_platform_zip))
 PDK_FUSION_PLATFORM_ZIP := $(word 1, $(_pdk_fusion_default_platform_zip))
 TARGET_BUILD_PDK := true
diff --git a/core/product.mk b/core/product.mk
index 9efb85d..95f1880 100644
--- a/core/product.mk
+++ b/core/product.mk
@@ -28,8 +28,8 @@
 # $(call ) isn't necessary.
 #
 define _find-android-products-files
-$(shell test -d device && find device -maxdepth 6 -name AndroidProducts.mk) \
-  $(shell test -d vendor && find vendor -maxdepth 6 -name AndroidProducts.mk) \
+$(sort $(shell test -d device && find device -maxdepth 6 -name AndroidProducts.mk)) \
+  $(sort $(shell test -d vendor && find vendor -maxdepth 6 -name AndroidProducts.mk)) \
   $(SRC_TARGET_DIR)/product/AndroidProducts.mk
 endef
 
diff --git a/core/sdk_font.mk b/core/sdk_font.mk
index 204403d..c10f19f 100644
--- a/core/sdk_font.mk
+++ b/core/sdk_font.mk
@@ -18,7 +18,7 @@
 SDK_FONT_TEMP := $(call intermediates-dir-for,PACKAGING,sdk-fonts,HOST,COMMON)
 
 # The font configuration files - system_fonts.xml, fallback_fonts.xml etc.
-sdk_font_config := $(wildcard frameworks/base/data/fonts/*.xml)
+sdk_font_config := $(sort $(wildcard frameworks/base/data/fonts/*.xml))
 sdk_font_config :=  $(addprefix $(SDK_FONT_TEMP)/, $(notdir $(sdk_font_config)))
 
 $(sdk_font_config): $(SDK_FONT_TEMP)/%.xml: \
diff --git a/envsetup.sh b/envsetup.sh
index c24e1f0..a3788f0 100644
--- a/envsetup.sh
+++ b/envsetup.sh
@@ -1,24 +1,25 @@
 function hmm() {
 cat <<EOF
 Invoke ". build/envsetup.sh" from your shell to add the following functions to your environment:
-- lunch:   lunch <product_name>-<build_variant>
-- tapas:   tapas [<App1> <App2> ...] [arm|x86|mips|armv5|arm64|x86_64|mips64] [eng|userdebug|user]
-- croot:   Changes directory to the top of the tree.
-- m:       Makes from the top of the tree.
-- mm:      Builds all of the modules in the current directory, but not their dependencies.
-- mmm:     Builds all of the modules in the supplied directories, but not their dependencies.
-           To limit the modules being built use the syntax: mmm dir/:target1,target2.
-- mma:     Builds all of the modules in the current directory, and their dependencies.
-- mmma:    Builds all of the modules in the supplied directories, and their dependencies.
-- cgrep:   Greps on all local C/C++ files.
-- ggrep:   Greps on all local Gradle files.
-- jgrep:   Greps on all local Java files.
-- resgrep: Greps on all local res/*.xml files.
-- mangrep: Greps on all local AndroidManifest.xml files.
-- mgrep:   Greps on all local Makefiles files.
-- sepgrep: Greps on all local sepolicy files.
-- sgrep:   Greps on all local source files.
-- godir:   Go to the directory containing a file.
+- lunch:     lunch <product_name>-<build_variant>
+- tapas:     tapas [<App1> <App2> ...] [arm|x86|mips|armv5|arm64|x86_64|mips64] [eng|userdebug|user]
+- croot:     Changes directory to the top of the tree.
+- m:         Makes from the top of the tree.
+- mm:        Builds all of the modules in the current directory, but not their dependencies.
+- mmm:       Builds all of the modules in the supplied directories, but not their dependencies.
+             To limit the modules being built use the syntax: mmm dir/:target1,target2.
+- mma:       Builds all of the modules in the current directory, and their dependencies.
+- mmma:      Builds all of the modules in the supplied directories, and their dependencies.
+- provision: Flash device with all required partitions. Options will be passed on to fastboot.
+- cgrep:     Greps on all local C/C++ files.
+- ggrep:     Greps on all local Gradle files.
+- jgrep:     Greps on all local Java files.
+- resgrep:   Greps on all local res/*.xml files.
+- mangrep:   Greps on all local AndroidManifest.xml files.
+- mgrep:     Greps on all local Makefiles files.
+- sepgrep:   Greps on all local sepolicy files.
+- sgrep:     Greps on all local source files.
+- godir:     Go to the directory containing a file.
 
 Environemnt options:
 - SANITIZE_HOST: Set to 'true' to use ASAN for all host modules. Note that
@@ -1470,6 +1471,34 @@
     return $ret
 }
 
+function provision()
+{
+    if [ ! "$ANDROID_PRODUCT_OUT" ]; then
+        echo "Couldn't locate output files.  Try running 'lunch' first." >&2
+        return 1
+    fi
+    if [ ! -e "$ANDROID_PRODUCT_OUT/provision-device" ]; then
+        echo "There is no provisioning script for the device." >&2
+        return 1
+    fi
+
+    # Check if user really wants to do this.
+    if [ "$1" = "--no-confirmation" ]; then
+        shift 1
+    else
+        echo "This action will reflash your device."
+        echo ""
+        echo "ALL DATA ON THE DEVICE WILL BE IRREVOCABLY ERASED."
+        echo ""
+        read -p "Are you sure you want to do this (yes/no)? "
+        if [[ "${REPLY}" != "yes" ]] ; then
+            echo "Not taking any action. Exiting." >&2
+            return 1
+        fi
+    fi
+    "$ANDROID_PRODUCT_OUT/provision-device" "$@"
+}
+
 if [ "x$SHELL" != "x/bin/bash" ]; then
     case `ps -o command -p $$` in
         *bash*)
diff --git a/tools/releasetools/add_img_to_target_files.py b/tools/releasetools/add_img_to_target_files.py
index bc2667d..6bdb9d1 100755
--- a/tools/releasetools/add_img_to_target_files.py
+++ b/tools/releasetools/add_img_to_target_files.py
@@ -28,6 +28,7 @@
   print >> sys.stderr, "Python 2.7 or newer is required."
   sys.exit(1)
 
+import datetime
 import errno
 import os
 import shutil
@@ -120,6 +121,12 @@
   if fstab:
     image_props["fs_type"] = fstab["/" + what].fs_type
 
+  # Use a fixed timestamp (01/01/2009) when packaging the image.
+  # Bug: 24377993
+  epoch = datetime.datetime.fromtimestamp(0)
+  timestamp = (datetime.datetime(2009, 1, 1) - epoch).total_seconds()
+  image_props["timestamp"] = int(timestamp)
+
   if what == "system":
     fs_config_prefix = ""
   else:
@@ -166,6 +173,12 @@
 
   print "creating userdata.img..."
 
+  # Use a fixed timestamp (01/01/2009) when packaging the image.
+  # Bug: 24377993
+  epoch = datetime.datetime.fromtimestamp(0)
+  timestamp = (datetime.datetime(2009, 1, 1) - epoch).total_seconds()
+  image_props["timestamp"] = int(timestamp)
+
   # The name of the directory it is making an image out of matters to
   # mkyaffs2image.  So we create a temp dir, and within it we create an
   # empty dir named "data", or a symlink to the DATA dir,
@@ -210,6 +223,12 @@
 
   print "creating cache.img..."
 
+  # Use a fixed timestamp (01/01/2009) when packaging the image.
+  # Bug: 24377993
+  epoch = datetime.datetime.fromtimestamp(0)
+  timestamp = (datetime.datetime(2009, 1, 1) - epoch).total_seconds()
+  image_props["timestamp"] = int(timestamp)
+
   # The name of the directory it is making an image out of matters to
   # mkyaffs2image.  So we create a temp dir, and within it we create an
   # empty dir named "cache", and build the image from that.
diff --git a/tools/releasetools/build_image.py b/tools/releasetools/build_image.py
index e1488dc..e5e2a24 100755
--- a/tools/releasetools/build_image.py
+++ b/tools/releasetools/build_image.py
@@ -20,7 +20,6 @@
 Usage:  build_image input_directory properties_file output_image_file
 
 """
-import datetime
 import os
 import os.path
 import re
@@ -387,11 +386,10 @@
   """
   d = {}
 
-  # Use a fixed timestamp (01/01/2009) for all the files in an image.
-  # Bug: 24377993
-  epoch = datetime.datetime.fromtimestamp(0)
-  timestamp = (datetime.datetime(2009, 1, 1) - epoch).total_seconds()
-  d["timestamp"] = int(timestamp)
+  if "build.prop" in glob_dict:
+    bp = glob_dict["build.prop"]
+    if "ro.build.date.utc" in bp:
+      d["timestamp"] = bp["ro.build.date.utc"]
 
   def copy_prop(src_p, dest_p):
     if src_p in glob_dict: