Merge "Sort files in directories passed to jar"
am: 966ed15a1d

Change-Id: If01ba7d294e4308a28580b157b9efdb6c860bb35
diff --git a/core/definitions.mk b/core/definitions.mk
index 449ea40..c43b156 100644
--- a/core/definitions.mk
+++ b/core/definitions.mk
@@ -2195,6 +2195,17 @@
  JACK_VERSION=$(PRIVATE_JACK_VERSION) $(JACK) $(DEFAULT_JACK_EXTRA_ARGS)
 endef
 
+# Return jar arguments to compress files in a given directory
+# $(1): directory
+#
+# Returns an @-file argument that contains the output of a subshell
+# that looks like -C $(1) path/to/file1 -C $(1) path/to/file2
+# Also adds "-C out/empty ." which avoids errors in jar when
+# there are no files in the directory.
+define jar-args-sorted-files-in-directory
+    @<(find $(1) -type f | sort | $(JAR_ARGS) $(1); echo "-C $(EMPTY_DIRECTORY) .")
+endef
+
 # Common definition to invoke javac on the host and target.
 #
 # Some historical notes:
@@ -2252,9 +2263,9 @@
 $(if $(PRIVATE_JAR_MANIFEST), \
     $(hide) sed -e "s/%BUILD_NUMBER%/$(BUILD_NUMBER_FROM_FILE)/" \
             $(PRIVATE_JAR_MANIFEST) > $(dir $@)/manifest.mf && \
-        $(JAR) -cfm $@ $(dir $@)/manifest.mf \
-            -C $(PRIVATE_CLASS_INTERMEDIATES_DIR) ., \
-    $(hide) $(JAR) -cf $@ -C $(PRIVATE_CLASS_INTERMEDIATES_DIR) .)
+        $(JAR) -cfm $@ $(dir $@)/manifest.mf, \
+    $(hide) $(JAR) -cf $@) \
+        $(call jar-args-sorted-files-in-directory,$(PRIVATE_CLASS_INTERMEDIATES_DIR))
 $(if $(PRIVATE_EXTRA_JAR_ARGS),$(call add-java-resources-to,$@))
 endef
 
@@ -2701,7 +2712,7 @@
   rm -rf $(3)
   mkdir -p $(3)
   unzip -qo $(2) -d $(3) $$(zipinfo -1 $(2) | grep -v -E "\.class$$")
-  $(JAR) uf $(1) -C $(3) .
+  $(JAR) uf $(1) $(call jar-args-sorted-files-in-directory,$(3))
 endef
 
 # Sign a package using the specified key/cert.
diff --git a/core/host_dalvik_java_library.mk b/core/host_dalvik_java_library.mk
index 4f3069f..0aabd51 100644
--- a/core/host_dalvik_java_library.mk
+++ b/core/host_dalvik_java_library.mk
@@ -98,6 +98,7 @@
         $(proto_java_sources_file_stamp) \
         $(annotation_processor_deps) \
         $(NORMALIZE_PATH) \
+        $(JAR_ARGS) \
         $(LOCAL_ADDITIONAL_DEPENDENCIES) \
         | $(SOONG_JAVAC_WRAPPER)
 	$(transform-host-java-to-package)
diff --git a/core/host_java_library.mk b/core/host_java_library.mk
index a68be6a..033297a 100644
--- a/core/host_java_library.mk
+++ b/core/host_java_library.mk
@@ -71,6 +71,7 @@
         $(proto_java_sources_file_stamp) \
         $(annotation_processor_deps) \
         $(NORMALIZE_PATH) \
+        $(JAR_ARGS) \
         $(ZIPTIME) \
         $(LOCAL_ADDITIONAL_DEPENDENCIES) \
         | $(SOONG_JAVAC_WRAPPER)
diff --git a/core/jacoco.mk b/core/jacoco.mk
index a04d25e..f4788bf 100644
--- a/core/jacoco.mk
+++ b/core/jacoco.mk
@@ -120,12 +120,13 @@
 $(LOCAL_FULL_CLASSES_JACOCO_JAR): PRIVATE_TEMP_JAR_PATH := $(my_temp_jar_path)
 $(LOCAL_FULL_CLASSES_JACOCO_JAR): PRIVATE_INSTRUMENTED_PATH := $(my_instrumented_path)
 $(LOCAL_FULL_CLASSES_JACOCO_JAR): PRIVATE_FULL_CLASSES_PRE_JACOCO_JAR := $(LOCAL_FULL_CLASSES_PRE_JACOCO_JAR)
+$(LOCAL_FULL_CLASSES_JACOCO_JAR): $(JAR_ARGS)
 $(LOCAL_FULL_CLASSES_JACOCO_JAR): $(my_instrumented_timestamp_path) $(LOCAL_FULL_CLASSES_PRE_JACOCO_JAR)
 	rm -f $@ $(PRIVATE_TEMP_JAR_PATH)
 	# copy the pre-jacoco jar (containing files excluded from instrumentation)
 	cp $(PRIVATE_FULL_CLASSES_PRE_JACOCO_JAR) $(PRIVATE_TEMP_JAR_PATH)
 	# copy instrumented files back into the resultant jar
-	$(JAR) -uf $(PRIVATE_TEMP_JAR_PATH) -C $(PRIVATE_INSTRUMENTED_PATH) .
+	$(JAR) -uf $(PRIVATE_TEMP_JAR_PATH) $(call jar-args-sorted-files-in-directory,$(PRIVATE_INSTRUMENTED_PATH))
 	mv $(PRIVATE_TEMP_JAR_PATH) $@
 
   # this is used to trigger $(my_classes_to_report_on_path) to build
diff --git a/core/java.mk b/core/java.mk
index 8fde7c2..673b2b1 100644
--- a/core/java.mk
+++ b/core/java.mk
@@ -449,6 +449,7 @@
         $(proto_java_sources_file_stamp) \
         $(annotation_processor_deps) \
         $(NORMALIZE_PATH) \
+        $(JAR_ARGS) \
         $(LOCAL_ADDITIONAL_DEPENDENCIES) \
         | $(SOONG_JAVAC_WRAPPER)
 	$(transform-java-to-classes.jar)
diff --git a/core/main.mk b/core/main.mk
index 18346bb..b1312ee 100644
--- a/core/main.mk
+++ b/core/main.mk
@@ -70,6 +70,10 @@
 DATE_FROM_FILE := date -d @$(BUILD_DATETIME_FROM_FILE)
 endif
 
+# Make an empty directory, which can be used to make empty jars
+EMPTY_DIRECTORY := $(OUT_DIR)/empty
+$(shell mkdir -p $(EMPTY_DIRECTORY) && rm -rf $(EMPTY_DIRECTORY)/*)
+
 # CTS-specific config.
 -include cts/build/config.mk
 # VTS-specific config.
diff --git a/core/package_internal.mk b/core/package_internal.mk
index c4a0d62..941afc6 100644
--- a/core/package_internal.mk
+++ b/core/package_internal.mk
@@ -568,6 +568,7 @@
 $(LOCAL_BUILT_MODULE): PRIVATE_RESOURCE_INTERMEDIATES_DIR := $(intermediates.COMMON)/resources
 $(LOCAL_BUILT_MODULE): PRIVATE_FULL_CLASSES_JAR := $(full_classes_jar)
 $(LOCAL_BUILT_MODULE) : $(jni_shared_libraries)
+$(LOCAL_BUILT_MODULE) : $(JAR_ARGS)
 ifdef LOCAL_USE_AAPT2
 $(LOCAL_BUILT_MODULE): PRIVATE_RES_PACKAGE := $(my_res_package)
 $(LOCAL_BUILT_MODULE) : $(my_res_package) $(AAPT2) | $(ACP)
diff --git a/core/static_java_library.mk b/core/static_java_library.mk
index cea8764..2a80661 100644
--- a/core/static_java_library.mk
+++ b/core/static_java_library.mk
@@ -199,6 +199,7 @@
 $(built_aar): PRIVATE_CLASSES_JAR := $(aar_classes_jar)
 $(built_aar): PRIVATE_RESOURCE_DIR := $(LOCAL_RESOURCE_DIR)
 $(built_aar): PRIVATE_R_TXT := $(LOCAL_INTERMEDIATE_SOURCE_DIR)/R.txt
+$(built_aar): $(JAR_ARGS)
 $(built_aar) : $(aar_classes_jar) $(full_android_manifest)
 	@echo "target AAR:  $(PRIVATE_MODULE) ($@)"
 	$(hide) rm -rf $(dir $@)aar && mkdir -p $(dir $@)aar/res
@@ -208,7 +209,7 @@
 	$(hide) $(foreach res,$(PRIVATE_RESOURCE_DIR),cp -Rfn $(res)/* $(dir $@)aar/res;)
 	$(hide) cp $(PRIVATE_R_TXT) $(dir $@)aar/R.txt
 	$(hide) $(JAR) -cMf $@ \
-	  -C $(dir $@)aar .
+	  $(call jar-args-sorted-files-in-directory,(dir $@)aar)
 
 # Register the aar file.
 ALL_MODULES.$(LOCAL_MODULE).AAR := $(built_aar)