Generate SDK Repository XML.

When the "sdk_repo" modified is used during the SDK build,
this also generates the repository.xml and addon.xml files
for the new SDK repository packages and places them in
the DIST_DIR.

Example usage:
  make -j 16 PRODUCT-sdk-sdk showcommands sdk_repo dist DIST_DIR=/dist/sdk
  make -j 16 PRODUCT-google_sdk-sdk_addon showcommands sdk_repo dist DIST_DIR=/dist/addon

Change-Id: Ibaad66f4b1cc476a4146afde7087a10a0557f74c
diff --git a/build/tools/sdk_repo.mk b/build/tools/sdk_repo.mk
index 70356c7..c709502 100644
--- a/build/tools/sdk_repo.mk
+++ b/build/tools/sdk_repo.mk
@@ -2,13 +2,16 @@
 
 .PHONY: sdk_repo
 
+SDK_REPO_DEPS :=
+SDK_REPO_XML_ARGS :=
+
 # Define the name of a package zip file to generate
 # $1=OS (e.g. linux-x86, windows, etc)
 # $2=sdk zip (e.g. out/host/linux.../android-eng-sdk.zip)
 # $3=package to create (e.g. tools, docs, etc.)
 #
 define sdk-repo-pkg-zip
-$(dir $(2))/sdk-repo-$(1)-$(3).zip
+$(dir $(2))/sdk-repo-$(1)-$(3)-$(FILE_NAME_TAG).zip
 endef
 
 # Defines the rule to build an SDK repository package by zipping all
@@ -25,10 +28,11 @@
 define mk-sdk-repo-pkg-1
 $(call sdk-repo-pkg-zip,$(1),$(2),$(3)): $(2)
 	@echo "Building SDK repository package $(3) from $(notdir $(2))"
-	$(hide) cd $(dir $(2)) && \
-			zip -9rq ../$(notdir $(call sdk-repo-pkg-zip,$(1),$(2),$(3))) \
-					 $(basename $(2))/*
+	$(hide) cd $(basename $(2)) && \
+			zip -9rq ../$(notdir $(call sdk-repo-pkg-zip,$(1),$(2),$(3))) $(3)/*
 $(call dist-for-goals, sdk_repo, $(call sdk-repo-pkg-zip,$(1),$(2),$(3)))
+SDK_REPO_XML_ARGS += $(3) $(1) \
+	$(call sdk-repo-pkg-zip,$(1),$(2),$(3)):$(notdir $(call sdk-repo-pkg-zip,$(1),$(2),$(3)))
 endef
 
 # Defines the rule to build an SDK repository package when the
@@ -45,15 +49,15 @@
 define mk-sdk-repo-pkg-2
 $(call sdk-repo-pkg-zip,$(1),$(2),$(3)): $(2)
 	@echo "Building SDK repository package $(3) from $(notdir $(2))"
-	$(hide) cd $(dir $(2))/$(3) && \
-			zip -9rq ../../$(notdir $(call sdk-repo-pkg-zip,$(1),$(2),$(3))) \
-					 $(basename $(2))/*
+	$(hide) cd $(basename $(2))/$(3) && \
+			zip -9rq ../../$(notdir $(call sdk-repo-pkg-zip,$(1),$(2),$(3))) *
 $(call dist-for-goals, sdk_repo, $(call sdk-repo-pkg-zip,$(1),$(2),$(3)))
+SDK_REPO_XML_ARGS += $(3) $(1) \
+	$(call sdk-repo-pkg-zip,$(1),$(2),$(3)):$(notdir $(call sdk-repo-pkg-zip,$(1),$(2),$(3)))
 endef
 
 
-SDK_REPO_DEPS :=
-
+# -----------------------------------------------------------------
 # Rules for win_sdk
 
 ifneq ($(WIN_SDK_ZIP),)
@@ -68,6 +72,7 @@
 
 endif
 
+# -----------------------------------------------------------------
 # Rules for main host sdk
 
 ifneq ($(filter sdk win_sdk,$(MAKECMDGOALS)),)
@@ -87,6 +92,7 @@
 
 endif
 
+# -----------------------------------------------------------------
 # Rules for sdk addon
 
 ifneq ($(ADDON_SDK_ZIP),)
@@ -95,12 +101,56 @@
 # already packaged correctly. All we have to do is dist it with
 # a different destination name.
 
-$(call dist-for-goals, sdk_repo, \
-	$(ADDON_SDK_ZIP):$(notdir $(call sdk-repo-pkg-zip,$(HOST_OS),$(ADDON_SDK_ZIP),addon)))
+RENAMED_ADDON_ZIP := $(ADDON_SDK_ZIP):$(notdir $(call sdk-repo-pkg-zip,$(HOST_OS),$(ADDON_SDK_ZIP),addon))
+
+$(call dist-for-goals, sdk_repo, $(RENAMED_ADDON_ZIP))
+
+# Also generate the addon.xml using the latest schema and the renamed addon zip
+
+SDK_ADDON_XML := $(dir $(ADDON_SDK_ZIP))/addon.xml
+
+SDK_ADDON_XSD := \
+	$(lastword \
+	  $(wildcard \
+	    $(TOPDIR)sdk/sdkmanager/libs/sdklib/src/com/android/sdklib/repository/sdk-addon-*.xsd \
+	))
+
+$(SDK_ADDON_XML): $(ADDON_SDK_ZIP)
+	$(hide) $(TOPDIR)development/build/tools/mk_sdk_repo_xml.sh \
+		$(SDK_ADDON_XML) $(SDK_ADDON_XSD) add-on $(HOST_OS) $(RENAMED_ADDON_ZIP)
+
+$(call dist-for-goals, sdk_repo, $(SDK_ADDON_XML))
 
 endif
 
+# -----------------------------------------------------------------
+# Rules for the SDK Repository XML
 
-sdk_repo: $(SDK_REPO_DEPS)
+SDK_REPO_XML := $(HOST_OUT)/sdk/repository.xml
+
+ifneq ($(SDK_REPO_XML_ARGS),)
+
+# Pickup the most recent xml schema
+SDK_REPO_XSD := \
+	$(lastword \
+	  $(wildcard \
+	    $(TOPDIR)sdk/sdkmanager/libs/sdklib/src/com/android/sdklib/repository/sdk-repository-*.xsd \
+	))
+
+$(SDK_REPO_XML): $(SDK_REPO_DEPS)
+	$(hide) $(TOPDIR)development/build/tools/mk_sdk_repo_xml.sh \
+		$(SDK_REPO_XML) $(SDK_REPO_XSD) $(SDK_REPO_XML_ARGS)
+
+$(call dist-for-goals, sdk_repo, $(SDK_REPO_XML))
+
+else
+
+$(SDK_REPO_XML): ;
+
+endif
+
+# -----------------------------------------------------------------
+
+sdk_repo: $(SDK_REPO_DEPS) $(SDK_REPO_XML)
 	@echo "Packing of SDK repository done"