Merge "Inline some variables that are only used in 1 place"
diff --git a/core/dex_preopt_config_merger.py b/core/dex_preopt_config_merger.py
index 401e8a8..4efcc17 100755
--- a/core/dex_preopt_config_merger.py
+++ b/core/dex_preopt_config_merger.py
@@ -31,7 +31,6 @@
 
 import json
 from collections import OrderedDict
-import os
 import sys
 
 
@@ -43,9 +42,8 @@
   # Read all JSON configs.
   cfgs = []
   for arg in sys.argv[1:]:
-    if os.stat(arg).st_size != 0:
-      with open(arg, 'r') as f:
-        cfgs.append(json.load(f, object_pairs_hook=OrderedDict))
+    with open(arg, 'r') as f:
+      cfgs.append(json.load(f, object_pairs_hook=OrderedDict))
 
   # The first config is the dexpreopted library/app, the rest are its
   # <uses-library> dependencies.
@@ -90,33 +88,6 @@
       clcs2.append(clc)
     clc_map2[sdk_ver] = clcs2
 
-  # Go over all uses-libraries in dependency dexpreopt.config files (these don't
-  # have to be uses-libraries themselves, they can be e.g. transitive static
-  # library dependencies) and merge their CLC to the current one
-  for ulib, cfg in uses_libs.items():
-    any_sdk_ver = 'any' # not interested in compatibility libraries
-    clcs = cfg['ClassLoaderContexts'].get(any_sdk_ver, [])
-
-    # If the dependency is a uses-library itself, its uses-library dependencies
-    # are added as a subcontext, so don't add them to top-level CLC.
-    dep_is_a_uses_lib = False
-    for clc2 in clc_map2[any_sdk_ver]:
-      if clc2['Name'] == cfg['ProvidesUsesLibrary']:
-        dep_is_a_uses_lib = True
-    if dep_is_a_uses_lib:
-      continue
-
-    # Check if CLC for these libraries is already present (avoid duplicates).
-    # Don't bother optimizing quadratic loop, since CLC is typically small.
-    for clc in clcs:
-      already_in_clc = False
-      for clc2 in clc_map2[any_sdk_ver]:
-        if clc2['Name'] == clc['Name']:
-          already_in_clc = True
-          break
-      if not already_in_clc:
-        clc_map2[any_sdk_ver] += clcs
-
   # Overwrite the original class loader context with the patched one.
   cfg0['ClassLoaderContexts'] = clc_map2
 
diff --git a/core/dex_preopt_odex_install.mk b/core/dex_preopt_odex_install.mk
index 9d6436f..216168b 100644
--- a/core/dex_preopt_odex_install.mk
+++ b/core/dex_preopt_odex_install.mk
@@ -110,8 +110,7 @@
 # Local module variables and functions used in dexpreopt and manifest_check.
 ################################################################################
 
-my_dexpreopt_libs_required := $(LOCAL_USES_LIBRARIES)
-my_dexpreopt_libs_optional := $(filter-out $(INTERNAL_PLATFORM_MISSING_USES_LIBRARIES), \
+my_filtered_optional_uses_libraries := $(filter-out $(INTERNAL_PLATFORM_MISSING_USES_LIBRARIES), \
   $(LOCAL_OPTIONAL_USES_LIBRARIES))
 
 # TODO(b/132357300): This may filter out too much, as PRODUCT_PACKAGES doesn't
@@ -121,7 +120,8 @@
 # to load dexpreopt code on device. We should fix this, either by deferring
 # dependency computation until the full list of product packages is known, or
 # by adding product-specific lists of missing libraries.
-my_dexpreopt_libs_optional := $(filter $(PRODUCT_PACKAGES), $(my_dexpreopt_libs_optional))
+my_filtered_optional_uses_libraries := $(filter $(PRODUCT_PACKAGES), \
+  $(my_filtered_optional_uses_libraries))
 
 ifeq ($(LOCAL_MODULE_CLASS),APPS)
   # compatibility libraries are added to class loader context of an app only if
@@ -146,6 +146,10 @@
   my_dexpreopt_libs_compat :=
 endif
 
+my_dexpreopt_libs := \
+  $(LOCAL_USES_LIBRARIES) \
+  $(my_filtered_optional_uses_libraries)
+
 # Module dexpreopt.config depends on dexpreopt.config files of each
 # <uses-library> dependency, because these libraries may be processed after
 # the current module by Make (there's no topological order), so the dependency
@@ -153,12 +157,11 @@
 # this dexpreopt.config is generated. So it's necessary to add file-level
 # dependencies between dexpreopt.config files.
 my_dexpreopt_dep_configs := $(foreach lib, \
-  $(filter-out $(my_dexpreopt_libs_compat),$(my_dexpreopt_libs_required) $(my_dexpreopt_libs_optional)), \
+  $(filter-out $(my_dexpreopt_libs_compat),$(LOCAL_USES_LIBRARIES) $(my_filtered_optional_uses_libraries)), \
   $(call intermediates-dir-for,JAVA_LIBRARIES,$(lib),,)/dexpreopt.config)
 
 # 1: SDK version
 # 2: list of libraries
-# 3: boolean, true if optional, else required
 #
 # Make does not process modules in topological order wrt. <uses-library>
 # dependencies, therefore we cannot rely on variables to get the information
@@ -177,48 +180,12 @@
   $(foreach lib, $(2),\
     $(call add_json_map_anon) \
     $(call add_json_str, Name, $(lib)) \
-    $(call add_json_bool, Optional, $(filter true,$(3))) \
     $(call add_json_str, Host, $(call intermediates-dir-for,JAVA_LIBRARIES,$(lib),,COMMON)/javalib.jar) \
     $(call add_json_str, Device, /system/framework/$(lib).jar) \
     $(call add_json_val, Subcontexts, null) \
     $(call end_json_map)) \
   $(call end_json_array)
 
-my_dexpreopt_archs :=
-my_dexpreopt_images :=
-my_dexpreopt_images_deps :=
-my_dexpreopt_image_locations_on_host :=
-my_dexpreopt_image_locations_on_device :=
-# Infix can be 'boot' or 'art'. Soong creates a set of variables for Make, one
-# for each boot image (primary and the framework extension). The only reason why
-# the primary image is exposed to Make is testing (art gtests) and benchmarking
-# (art golem benchmarks). Install rules that use those variables are in
-# dex_preopt_libart.mk. Here for dexpreopt purposes the infix is always 'boot'.
-my_dexpreopt_infix := boot
-
-ifdef LOCAL_DEX_PREOPT
-  ifeq (,$(filter PRESIGNED,$(LOCAL_CERTIFICATE)))
-    # Store uncompressed dex files preopted in /system
-    ifeq ($(BOARD_USES_SYSTEM_OTHER_ODEX),true)
-      ifeq ($(call install-on-system-other, $(my_module_path)),)
-        LOCAL_UNCOMPRESS_DEX := true
-      endif  # install-on-system-other
-    else  # BOARD_USES_SYSTEM_OTHER_ODEX
-      LOCAL_UNCOMPRESS_DEX := true
-    endif
-  endif
-  my_create_dexpreopt_config := true
-endif
-
-# dexpreopt is disabled when TARGET_BUILD_UNBUNDLED_IMAGE is true,
-# but dexpreopt config files are required to dexpreopt in post-processing.
-ifeq ($(TARGET_BUILD_UNBUNDLED_IMAGE),true)
-  my_create_dexpreopt_config := true
-endif
-
-# This is needed for both <uses-library> check and dexpreopt command.
-my_dexpreopt_config := $(intermediates)/dexpreopt.config
-
 ################################################################################
 # Verify <uses-library> coherence between the build system and the manifest.
 ################################################################################
@@ -271,13 +238,7 @@
     $(LOCAL_OPTIONAL_USES_LIBRARIES))
   my_relax_check_arg := $(if $(filter true,$(RELAX_USES_LIBRARY_CHECK)), \
     --enforce-uses-libraries-relax,)
-
-  my_dexpreopt_config_deps := $(my_dexpreopt_dep_configs)
-  my_dexpreopt_config_args := $(patsubst %,--dexpreopt-dep-config %,$(my_dexpreopt_dep_configs))
-  ifeq ($(my_create_dexpreopt_config), true)
-    my_dexpreopt_config_deps += $(my_dexpreopt_config)
-    my_dexpreopt_config_args += --dexpreopt-config $(my_dexpreopt_config)
-  endif
+  my_dexpreopt_config_args := $(patsubst %,--dexpreopt-config %,$(my_dexpreopt_dep_configs))
 
   my_enforced_uses_libraries := $(intermediates.COMMON)/enforce_uses_libraries.status
   $(my_enforced_uses_libraries): PRIVATE_USES_LIBRARIES := $(my_uses_libs_args)
@@ -286,7 +247,7 @@
   $(my_enforced_uses_libraries): PRIVATE_RELAX_CHECK := $(my_relax_check_arg)
   $(my_enforced_uses_libraries): $(AAPT)
   $(my_enforced_uses_libraries): $(my_verify_script)
-  $(my_enforced_uses_libraries): $(my_dexpreopt_config_deps)
+  $(my_enforced_uses_libraries): $(my_dexpreopt_dep_configs)
   $(my_enforced_uses_libraries): $(my_manifest_or_apk)
 	@echo Verifying uses-libraries: $<
 	rm -f $@
@@ -306,6 +267,39 @@
 # Dexpreopt command.
 ################################################################################
 
+my_dexpreopt_archs :=
+my_dexpreopt_images :=
+my_dexpreopt_images_deps :=
+my_dexpreopt_image_locations_on_host :=
+my_dexpreopt_image_locations_on_device :=
+# Infix can be 'boot' or 'art'. Soong creates a set of variables for Make, one
+# for each boot image (primary and the framework extension). The only reason why
+# the primary image is exposed to Make is testing (art gtests) and benchmarking
+# (art golem benchmarks). Install rules that use those variables are in
+# dex_preopt_libart.mk. Here for dexpreopt purposes the infix is always 'boot'.
+my_dexpreopt_infix := boot
+my_create_dexpreopt_config :=
+
+ifdef LOCAL_DEX_PREOPT
+  ifeq (,$(filter PRESIGNED,$(LOCAL_CERTIFICATE)))
+    # Store uncompressed dex files preopted in /system
+    ifeq ($(BOARD_USES_SYSTEM_OTHER_ODEX),true)
+      ifeq ($(call install-on-system-other, $(my_module_path)),)
+        LOCAL_UNCOMPRESS_DEX := true
+      endif  # install-on-system-other
+    else  # BOARD_USES_SYSTEM_OTHER_ODEX
+      LOCAL_UNCOMPRESS_DEX := true
+    endif
+  endif
+  my_create_dexpreopt_config := true
+endif
+
+# dexpreopt is disabled when TARGET_BUILD_UNBUNDLED_IMAGE is true,
+# but dexpreopt config files are required to dexpreopt in post-processing.
+ifeq ($(TARGET_BUILD_UNBUNDLED_IMAGE),true)
+  my_create_dexpreopt_config := true
+endif
+
 ifeq ($(my_create_dexpreopt_config), true)
   ifeq ($(LOCAL_MODULE_CLASS),JAVA_LIBRARIES)
     my_module_multilib := $(LOCAL_MULTILIB)
@@ -395,11 +389,10 @@
   $(call add_json_bool, EnforceUsesLibraries,           $(filter true,$(LOCAL_ENFORCE_USES_LIBRARIES)))
   $(call add_json_str,  ProvidesUsesLibrary,            $(firstword $(LOCAL_PROVIDES_USES_LIBRARY) $(LOCAL_MODULE)))
   $(call add_json_map,  ClassLoaderContexts)
-  $(call add_json_class_loader_context, any, $(my_dexpreopt_libs_required),)
-  $(call add_json_class_loader_context, any, $(my_dexpreopt_libs_optional),true)
-  $(call add_json_class_loader_context,  28, $(my_dexpreopt_libs_compat_28),)
-  $(call add_json_class_loader_context,  29, $(my_dexpreopt_libs_compat_29),)
-  $(call add_json_class_loader_context,  30, $(my_dexpreopt_libs_compat_30),)
+  $(call add_json_class_loader_context, any, $(my_dexpreopt_libs))
+  $(call add_json_class_loader_context,  28, $(my_dexpreopt_libs_compat_28))
+  $(call add_json_class_loader_context,  29, $(my_dexpreopt_libs_compat_29))
+  $(call add_json_class_loader_context,  30, $(my_dexpreopt_libs_compat_30))
   $(call end_json_map)
   $(call add_json_list, Archs,                          $(my_dexpreopt_archs))
   $(call add_json_list, DexPreoptImages,                $(my_dexpreopt_images))
@@ -414,6 +407,7 @@
 
   $(call json_end)
 
+  my_dexpreopt_config := $(intermediates)/dexpreopt.config
   my_dexpreopt_config_for_postprocessing := $(PRODUCT_OUT)/dexpreopt_config/$(LOCAL_MODULE)_dexpreopt.config
   my_dexpreopt_config_merger := $(BUILD_SYSTEM)/dex_preopt_config_merger.py
 
@@ -472,7 +466,7 @@
   my_dexpreopt_deps := $(my_dex_jar)
   my_dexpreopt_deps += $(if $(my_process_profile),$(LOCAL_DEX_PREOPT_PROFILE))
   my_dexpreopt_deps += \
-    $(foreach lib, $(my_dexpreopt_libs_required) $(my_dexpreopt_libs_optional) $(my_dexpreopt_libs_compat), \
+    $(foreach lib, $(my_dexpreopt_libs) $(my_dexpreopt_libs_compat), \
       $(call intermediates-dir-for,JAVA_LIBRARIES,$(lib),,COMMON)/javalib.jar)
   my_dexpreopt_deps += $(my_dexpreopt_images_deps)
   my_dexpreopt_deps += $(DEXPREOPT_BOOTCLASSPATH_DEX_FILES)
@@ -512,4 +506,4 @@
   my_dexpreopt_zip :=
   my_dexpreopt_config_for_postprocessing :=
 endif # LOCAL_DEX_PREOPT
-endif # my_create_dexpreopt_config
+endif # my_create_dexpreopt_config
\ No newline at end of file
diff --git a/core/distdir.mk b/core/distdir.mk
index 3d48a48..bdfb490 100644
--- a/core/distdir.mk
+++ b/core/distdir.mk
@@ -46,10 +46,11 @@
 endef
 
 .PHONY: shareprojects
+#shareprojects:
 
 define __share-projects-rule
 $(1) : PRIVATE_TARGETS := $(2)
-$(1) : PRIVATE_ARGUMENT_FILE := $(call intermediates-dir-for,METAPACKAGING,codesharing)/$(1)/arguments
+$(1) : PRIVATE_ARGUMENT_FILE := $(call intermediates-dir-for,PACKAGING,codesharing)/$(1)/arguments
 $(1): $(2) $(COMPLIANCE_LISTSHARE)
 	$(hide) rm -f $$@
 	mkdir -p $$(dir $$@)
@@ -66,82 +67,20 @@
 $(eval $(call __share-projects-rule,$(1),$(call corresponding-license-metadata,$(2))))
 endef
 
-# Add a build dependency
-#
-# $(1): the goal phony target
-# $(2): the intermediate shareprojects file
-define _share-projects-dep
-$(1): $(2)
-endef
-
-.PHONY: alllicensetexts
-
-define __license-texts-rule
-$(2) : PRIVATE_GOAL := $(1)
-$(2) : PRIVATE_TARGETS := $(3)
-$(2) : PRIVATE_ROOTS := $(4)
-$(2) : PRIVATE_ARGUMENT_FILE := $(call intermediates-dir-for,METAPACKAGING,licensetexts)/$(2)/arguments
-$(2): $(3) $(TEXTNOTICE)
-	$(hide) rm -f $$@
-	mkdir -p $$(dir $$@)
-	mkdir -p $$(dir $$(PRIVATE_ARGUMENT_FILE))
-	$$(if $$(strip $$(PRIVATE_TARGETS)),$$(call dump-words-to-file,\
-            -product="$$(PRIVATE_GOAL)" -title="$$(PRIVATE_GOAL)" \
-            $$(addprefix -strip_prefix ,$$(PRIVATE_ROOTS)) \
-            -strip_prefix=$(PRODUCT_OUT)/ -strip_prefix=$(HOST_OUT)/\
-            $$(PRIVATE_TARGETS),\
-            $$(PRIVATE_ARGUMENT_FILE)))
-	$$(if $$(strip $$(PRIVATE_TARGETS)),OUT_DIR=$(OUT_DIR) $(TEXTNOTICE) -o $$@ @$$(PRIVATE_ARGUMENT_FILE),touch $$@)
-endef
-
-# build list of projects to share in $(2) for dist targets in $(3) for dist goal $(1)
-#
-# $(1): the name of the dist goal
-# $(2): the intermediate project sharing file
-# $(3): the dist files to base the sharing on
-define _license-texts-rule
-$(eval $(call __license-texts-rule,$(1),$(2),$(call corresponding-license-metadata,$(3)),$(sort $(dir $(3)))))
-endef
-
-# Add a build dependency
-#
-# $(1): the goal phony target
-# $(2): the intermediate shareprojects file
-define _license-texts-dep
-$(1): $(2)
-endef
-
 define _add_projects_to_share
-$(strip $(eval _idir := $(call intermediates-dir-for,METAPACKAGING,shareprojects))) \
-$(strip $(eval _tdir := $(call intermediates-dir-for,METAPACKAGING,licensetexts))) \
+$(strip $(eval _idir := $(call intermediates-dir-for,PACKAGING,shareprojects))) \
 $(strip $(eval _goals := $(sort $(_all_dist_goals)))) \
 $(strip $(eval _opairs := $(sort $(_all_dist_goal_output_pairs)))) \
 $(strip $(eval _dpairs := $(sort $(_all_dist_src_dst_pairs)))) \
 $(strip $(eval _allt :=)) \
 $(foreach goal,$(_goals), \
   $(eval _f := $(_idir)/$(goal).shareprojects) \
-  $(eval _n := $(_tdir)/$(goal).txt) \
   $(call dist-for-goals,$(goal),$(_f):shareprojects/$(basename $(notdir $(_f)))) \
-  $(call dist-for-goals,$(goal),$(_n):licensetexts/$(basename $(notdir $(_n)))) \
   $(eval _targets :=) \
   $(foreach op,$(filter $(goal):%,$(_opairs)),$(foreach p,$(filter %:$(call word-colon,2,$(op)),$(_dpairs)),$(eval _targets += $(call word-colon,1,$(p))))) \
   $(eval _allt += $(_targets)) \
   $(eval $(call _share-projects-rule,$(_f),$(_targets))) \
-  $(eval $(call _license-texts-rule,$(goal),$(_n),$(_targets))) \
-)\
-$(eval _f := $(_idir)/all.shareprojects)\
-$(eval _n := $(_tdir)/all.txt)\
-$(eval _idir :=)\
-$(eval _tdir :=)\
-$(eval $(call _share-projects-dep,shareprojects,$(_f))) \
-$(eval $(call _license-texts-dep,alllicensetexts,$(_n))) \
-$(call dist-for-goals,droid shareprojects,$(_f):shareprojects/all)\
-$(call dist-for-goals,droid alllicensetexts,$(_n):licensetexts/all)\
-$(eval _allt := $(sort $(_allt)))\
-$(eval $(call _share-projects-rule,$(_f),$(_allt)))\
-$(eval $(call _license-texts-rule,droid,$(_n),$(_allt)))\
-$(eval _f :=)\
-$(evan _n :=)
+)
 endef
 
 #------------------------------------------------------------------
diff --git a/envsetup.sh b/envsetup.sh
index c9b1b54..8856212 100644
--- a/envsetup.sh
+++ b/envsetup.sh
@@ -817,7 +817,9 @@
     local arch="$(echo $* | xargs -n 1 echo | \grep -E '^(arm|x86|arm64|x86_64)$' | xargs)"
     local variant="$(echo $* | xargs -n 1 echo | \grep -E '^(user|userdebug|eng)$' | xargs)"
     local density="$(echo $* | xargs -n 1 echo | \grep -E '^(ldpi|mdpi|tvdpi|hdpi|xhdpi|xxhdpi|xxxhdpi|alldpi)$' | xargs)"
-    local apps="$(echo $* | xargs -n 1 echo | \grep -E -v '^(user|userdebug|eng|arm|x86|arm64|x86_64|ldpi|mdpi|tvdpi|hdpi|xhdpi|xxhdpi|xxxhdpi|alldpi)$' | xargs)"
+    local keys="$(echo $* | xargs -n 1 echo | \grep -E '^(devkeys)$' | xargs)"
+    local apps="$(echo $* | xargs -n 1 echo | \grep -E -v '^(user|userdebug|eng|arm|x86|arm64|x86_64|ldpi|mdpi|tvdpi|hdpi|xhdpi|xxhdpi|xxxhdpi|alldpi|devkeys)$' | xargs)"
+
 
     if [ "$showHelp" != "" ]; then
       $(gettop)/build/make/tapasHelp.sh
@@ -836,6 +838,10 @@
         echo "tapas: Error: Multiple densities supplied: $density"
         return
     fi
+    if [ $(echo $keys | wc -w) -gt 1 ]; then
+        echo "tapas: Error: Multiple keys supplied: $keys"
+        return
+    fi
 
     local product=aosp_arm
     case $arch in
@@ -843,6 +849,10 @@
       arm64)  product=aosp_arm64;;
       x86_64) product=aosp_x86_64;;
     esac
+    if [ -n "$keys" ]; then
+        product=${product/aosp_/aosp_${keys}_}
+    fi;
+
     if [ -z "$variant" ]; then
         variant=eng
     fi
diff --git a/tapasHelp.sh b/tapasHelp.sh
index 0f46130..7cb5f2c 100755
--- a/tapasHelp.sh
+++ b/tapasHelp.sh
@@ -6,7 +6,7 @@
 cd ../..
 TOP="${PWD}"
 
-message='usage: tapas [<App1> <App2> ...] [arm|x86|arm64|x86_64] [eng|userdebug|user]
+message='usage: tapas [<App1> <App2> ...] [arm|x86|arm64|x86_64] [eng|userdebug|user] [devkeys]
 
 tapas selects individual apps to be built by the Android build system. Unlike
 "lunch", "tapas" does not request the building of images for a device.
diff --git a/tools/check_elf_file.py b/tools/check_elf_file.py
index 045cb1d..0b80226 100755
--- a/tools/check_elf_file.py
+++ b/tools/check_elf_file.py
@@ -72,9 +72,9 @@
 
 def _get_os_name():
   """Get the host OS name."""
-  if sys.platform == 'linux2':
+  if sys.platform.startswith('linux'):
     return 'linux'
-  if sys.platform == 'darwin':
+  if sys.platform.startswith('darwin'):
     return 'darwin'
   raise ValueError(sys.platform + ' is not supported')