Add an inherit-package macro to the build system.

This lets you use the source code from one apk to build a new one
with a new module name, a new android package name and optionally,
a new certificate.

To use this, in a makefile, add this:

$(call inherit-package, \
    packages/apps/Music/Android.mk, \
    Music, \
    MusicFork, \
    my_cert, \
    com.example.music)

You don't need the LOCAL_PATH and CLEAR_VARS stuff.  It will override
only the packages that are defined in the makefile name you give, but
if other modules of other types are defined, you will get an error
saying that it's defined twice.  In that case, you need to move the
duplicate library (for example a .so) out into a different makefile.

A LOCAL_OVERRIDES_PACKAGES entry is automatically created for the
forked app.
diff --git a/core/clear_vars.mk b/core/clear_vars.mk
index 99d7eb0..58c1800 100644
--- a/core/clear_vars.mk
+++ b/core/clear_vars.mk
@@ -19,6 +19,7 @@
 LOCAL_PACKAGE_NAME:=
 LOCAL_OVERRIDES_PACKAGES:=
 LOCAL_EXPORT_PACKAGE_RESOURCES:=
+LOCAL_PACKAGE_MANIFEST_NAME:=
 LOCAL_REQUIRED_MODULES:=
 LOCAL_ACP_UNAVAILABLE:=
 LOCAL_MODULE_TAGS:=
diff --git a/core/definitions.mk b/core/definitions.mk
index 7e7f9f1..3401eb7 100644
--- a/core/definitions.mk
+++ b/core/definitions.mk
@@ -1701,6 +1701,55 @@
 
 
 ###########################################################
+# Override the package defined in $(1), setting the
+# variables listed below differently.
+#
+#  $(1): The makefile to override (relative to the source
+#        tree root)
+#  $(2): Old LOCAL_PACKAGE_NAME value.
+#  $(3): New LOCAL_PACKAGE_NAME value.
+#  $(4): New LOCALE_MANIFEST_PACKAGE_NAME value.
+#  $(5): New LOCAL_CERTIFICATE value.
+#
+# Note that LOCAL_PACKAGE_OVERRIDES is NOT cleared in
+# clear_vars.mk.
+###########################################################
+define inherit-package
+  $(eval $(call inherit-package-internal,$(1),$(2),$(3),$(4)))
+endef
+
+define inherit-package-internal
+  LOCAL_PACKAGE_OVERRIDES \
+      := $(strip $(1))||$(strip $(2))||$(strip $(3))||$(strip $(4))||$(strip $(5)) $(LOCAL_PACKAGE_OVERRIDES)
+  include $(1)
+  LOCAL_PACKAGE_OVERRIDES \
+      := $(wordlist 1,$(words $(LOCAL_PACKAGE_OVERRIDES)), $(LOCAL_PACKAGE_OVERRIDES))
+endef
+
+# To be used with inherit-package above
+# Evalutes to true if the package was overridden
+define set-inherited-package-variables
+$(strip $(call set-inherited-package-variables-internal))
+endef
+
+define keep-or-override
+$(eval $(1) := $(if $(2),$(2),$($(1))))
+endef
+
+define set-inherited-package-variables-internal
+  $(eval _o := $(subst ||, ,$(lastword $(LOCAL_PACKAGE_OVERRIDES))))
+  $(eval _n := $(subst ||, ,$(firstword $(LOCAL_PACKAGE_OVERRIDES))))
+  $(if $(filter $(word 2,$(_n)),$(LOCAL_PACKAGE_NAME)), \
+    $(eval LOCAL_PACKAGE_NAME := $(word 3,$(_o))) \
+    $(eval LOCAL_MANIFEST_PACKAGE_NAME := $(word 4,$(_o))) \
+    $(call keep-or-override,LOCAL_CERTIFICATE,$(word 5,$(_o))) \
+    $(eval LOCAL_OVERRIDES_PACKAGES := $(sort $(LOCAL_OVERRIDES_PACKAGES) $(word 2,$(_o)))) \
+    true \
+  ,)
+endef
+
+
+###########################################################
 ## Other includes
 ###########################################################
 
diff --git a/core/package.mk b/core/package.mk
index 2ee3996..388435a 100644
--- a/core/package.mk
+++ b/core/package.mk
@@ -25,6 +25,19 @@
 ## be set for you.
 ###########################################################
 
+
+# If this makefile is being read from within an inheritance,
+# use the new values.
+skip_definition:=
+ifdef LOCAL_PACKAGE_OVERRIDES
+  package_overridden := $(call set-inherited-package-variables)
+  ifeq ($(strip $(package_overridden)),)
+    skip_definition := true
+  endif
+endif
+
+ifndef skip_definition
+
 LOCAL_PACKAGE_NAME := $(strip $(LOCAL_PACKAGE_NAME))
 ifeq ($(LOCAL_PACKAGE_NAME),)
 $(error $(LOCAL_PATH): Package modules must define LOCAL_PACKAGE_NAME)
@@ -291,3 +304,5 @@
 PACKAGES.$(LOCAL_PACKAGE_NAME).RESOURCE_FILES := $(all_resources)
 
 PACKAGES := $(PACKAGES) $(LOCAL_PACKAGE_NAME)
+
+endif # skip_definition