1# dexpreopt_odex_install.mk is used to define odex creation rules for JARs and APKs
2# This file depends on variables set in base_rules.mk
3# Output variables: LOCAL_DEX_PREOPT, LOCAL_UNCOMPRESS_DEX
4
5ifeq (true,$(LOCAL_USE_EMBEDDED_DEX))
6  LOCAL_UNCOMPRESS_DEX := true
7else
8  LOCAL_UNCOMPRESS_DEX :=
9endif
10
11# We explicitly uncompress APKs of privileged apps, and used by
12# privileged apps
13ifneq (true,$(DONT_UNCOMPRESS_PRIV_APPS_DEXS))
14  ifeq (true,$(LOCAL_PRIVILEGED_MODULE))
15    LOCAL_UNCOMPRESS_DEX := true
16  endif
17
18  ifneq (,$(filter $(PRODUCT_LOADED_BY_PRIVILEGED_MODULES), $(LOCAL_MODULE)))
19    LOCAL_UNCOMPRESS_DEX := true
20  endif
21endif  # DONT_UNCOMPRESS_PRIV_APPS_DEXS
22
23# Setting LOCAL_DEX_PREOPT based on WITH_DEXPREOPT, LOCAL_DEX_PREOPT, etc
24LOCAL_DEX_PREOPT := $(strip $(LOCAL_DEX_PREOPT))
25ifndef LOCAL_DEX_PREOPT # LOCAL_DEX_PREOPT undefined
26  LOCAL_DEX_PREOPT := $(DEX_PREOPT_DEFAULT)
27endif
28
29ifeq (false,$(LOCAL_DEX_PREOPT))
30  LOCAL_DEX_PREOPT :=
31endif
32
33# Only enable preopt for non tests.
34ifneq (,$(filter $(LOCAL_MODULE_TAGS),tests))
35  LOCAL_DEX_PREOPT :=
36endif
37
38# If we have product-specific config for this module?
39ifneq (,$(filter $(LOCAL_MODULE),$(DEXPREOPT_DISABLED_MODULES)))
40  LOCAL_DEX_PREOPT :=
41endif
42
43# Disable preopt for TARGET_BUILD_APPS
44ifneq (,$(TARGET_BUILD_APPS))
45  LOCAL_DEX_PREOPT :=
46endif
47
48# Disable preopt if not WITH_DEXPREOPT
49ifneq (true,$(WITH_DEXPREOPT))
50  LOCAL_DEX_PREOPT :=
51endif
52
53ifdef LOCAL_UNINSTALLABLE_MODULE
54  LOCAL_DEX_PREOPT :=
55endif
56
57ifeq (,$(strip $(built_dex)$(my_prebuilt_src_file)$(LOCAL_SOONG_DEX_JAR))) # contains no java code
58  LOCAL_DEX_PREOPT :=
59endif
60
61# Don't preopt system server jars that are updatable.
62ifneq (,$(filter %:$(LOCAL_MODULE), $(PRODUCT_UPDATABLE_SYSTEM_SERVER_JARS)))
63  LOCAL_DEX_PREOPT :=
64endif
65
66# if WITH_DEXPREOPT_BOOT_IMG_AND_SYSTEM_SERVER_ONLY=true and module is not in boot class path skip
67# Also preopt system server jars since selinux prevents system server from loading anything from
68# /data. If we don't do this they will need to be extracted which is not favorable for RAM usage
69# or performance. If my_preopt_for_extracted_apk is true, we ignore the only preopt boot image
70# options.
71ifneq (true,$(my_preopt_for_extracted_apk))
72  ifeq (true,$(WITH_DEXPREOPT_BOOT_IMG_AND_SYSTEM_SERVER_ONLY))
73    ifeq ($(filter $(PRODUCT_SYSTEM_SERVER_JARS) $(DEXPREOPT_BOOT_JARS_MODULES),$(LOCAL_MODULE)),)
74      LOCAL_DEX_PREOPT :=
75    endif
76  endif
77endif
78
79my_process_profile :=
80my_profile_is_text_listing :=
81
82ifeq (false,$(WITH_DEX_PREOPT_GENERATE_PROFILE))
83  LOCAL_DEX_PREOPT_GENERATE_PROFILE := false
84endif
85
86ifndef LOCAL_DEX_PREOPT_GENERATE_PROFILE
87  # If LOCAL_DEX_PREOPT_GENERATE_PROFILE is not defined, default it based on the existence of the
88  # profile class listing. TODO: Use product specific directory here.
89  my_classes_directory := $(PRODUCT_DEX_PREOPT_PROFILE_DIR)
90  LOCAL_DEX_PREOPT_PROFILE := $(my_classes_directory)/$(LOCAL_MODULE).prof
91
92  ifneq (,$(wildcard $(LOCAL_DEX_PREOPT_PROFILE)))
93    my_process_profile := true
94    my_profile_is_text_listing :=
95  endif
96else
97  my_process_profile := $(LOCAL_DEX_PREOPT_GENERATE_PROFILE)
98  my_profile_is_text_listing := true
99  LOCAL_DEX_PREOPT_PROFILE := $(LOCAL_DEX_PREOPT_PROFILE_CLASS_LISTING)
100endif
101
102ifeq (true,$(my_process_profile))
103  ifndef LOCAL_DEX_PREOPT_PROFILE
104    $(call pretty-error,Must have specified class listing (LOCAL_DEX_PREOPT_PROFILE))
105  endif
106  ifeq (,$(dex_preopt_profile_src_file))
107    $(call pretty-error, Internal error: dex_preopt_profile_src_file must be set)
108  endif
109endif
110
111my_dexpreopt_archs :=
112my_dexpreopt_images :=
113my_dexpreopt_images_deps :=
114my_dexpreopt_image_locations :=
115my_dexpreopt_infix := boot
116ifeq (true, $(DEXPREOPT_USE_ART_IMAGE))
117  my_dexpreopt_infix := art
118endif
119
120ifdef LOCAL_DEX_PREOPT
121  ifeq (,$(filter PRESIGNED,$(LOCAL_CERTIFICATE)))
122    # Store uncompressed dex files preopted in /system
123    ifeq ($(BOARD_USES_SYSTEM_OTHER_ODEX),true)
124      ifeq ($(call install-on-system-other, $(my_module_path)),)
125        LOCAL_UNCOMPRESS_DEX := true
126      endif  # install-on-system-other
127    else  # BOARD_USES_SYSTEM_OTHER_ODEX
128      LOCAL_UNCOMPRESS_DEX := true
129    endif
130  endif
131
132  ifeq ($(LOCAL_MODULE_CLASS),JAVA_LIBRARIES)
133    my_module_multilib := $(LOCAL_MULTILIB)
134    # If the module is not an SDK library and it's a system server jar, only preopt the primary arch.
135    ifeq (,$(filter $(JAVA_SDK_LIBRARIES),$(LOCAL_MODULE)))
136      # For a Java library, by default we build odex for both 1st arch and 2nd arch.
137      # But it can be overridden with "LOCAL_MULTILIB := first".
138      ifneq (,$(filter $(PRODUCT_SYSTEM_SERVER_JARS),$(LOCAL_MODULE)))
139        # For system server jars, we build for only "first".
140        my_module_multilib := first
141      endif
142    endif
143
144    # Only preopt primary arch for translated arch since there is only an image there.
145    ifeq ($(TARGET_TRANSLATE_2ND_ARCH),true)
146      my_module_multilib := first
147    endif
148
149    # #################################################
150    # Odex for the 1st arch
151    my_dexpreopt_archs += $(TARGET_ARCH)
152    my_dexpreopt_images += $(DEXPREOPT_IMAGE_$(my_dexpreopt_infix)_$(TARGET_ARCH))
153    my_dexpreopt_images_deps += $(DEXPREOPT_IMAGE_DEPS_$(my_dexpreopt_infix)_$(TARGET_ARCH))
154    # Odex for the 2nd arch
155    ifdef TARGET_2ND_ARCH
156      ifneq ($(TARGET_TRANSLATE_2ND_ARCH),true)
157        ifneq (first,$(my_module_multilib))
158          my_dexpreopt_archs += $(TARGET_2ND_ARCH)
159          my_dexpreopt_images += $(DEXPREOPT_IMAGE_$(my_dexpreopt_infix)_$(TARGET_2ND_ARCH))
160          my_dexpreopt_images_deps += $(DEXPREOPT_IMAGE_DEPS_$(my_dexpreopt_infix)_$(TARGET_2ND_ARCH))
161        endif  # my_module_multilib is not first.
162      endif  # TARGET_TRANSLATE_2ND_ARCH not true
163    endif  # TARGET_2ND_ARCH
164    # #################################################
165  else  # must be APPS
166    # The preferred arch
167    # Save the module multilib since setup_one_odex modifies it.
168    my_2nd_arch_prefix := $(LOCAL_2ND_ARCH_VAR_PREFIX)
169    my_dexpreopt_archs += $(TARGET_$(my_2nd_arch_prefix)ARCH)
170    my_dexpreopt_images += \
171        $(DEXPREOPT_IMAGE_$(my_dexpreopt_infix)_$(TARGET_$(my_2nd_arch_prefix)ARCH))
172    my_dexpreopt_images_deps += \
173        $(DEXPREOPT_IMAGE_DEPS_$(my_dexpreopt_infix)_$(TARGET_$(my_2nd_arch_prefix)ARCH))
174    ifdef TARGET_2ND_ARCH
175      ifeq ($(my_module_multilib),both)
176        # The non-preferred arch
177        my_2nd_arch_prefix := $(if $(LOCAL_2ND_ARCH_VAR_PREFIX),,$(TARGET_2ND_ARCH_VAR_PREFIX))
178        my_dexpreopt_archs += $(TARGET_$(my_2nd_arch_prefix)ARCH)
179        my_dexpreopt_images += \
180            $(DEXPREOPT_IMAGE_$(my_dexpreopt_infix)_$(TARGET_$(my_2nd_arch_prefix)ARCH))
181        my_dexpreopt_images_deps += \
182            $(DEXPREOPT_IMAGE_DEPS_$(my_dexpreopt_infix)_$(TARGET_$(my_2nd_arch_prefix)ARCH))
183      endif  # LOCAL_MULTILIB is both
184    endif  # TARGET_2ND_ARCH
185  endif  # LOCAL_MODULE_CLASS
186
187  my_dexpreopt_image_locations += $(DEXPREOPT_IMAGE_LOCATIONS_$(my_dexpreopt_infix))
188
189  my_filtered_optional_uses_libraries := $(filter-out $(INTERNAL_PLATFORM_MISSING_USES_LIBRARIES), \
190    $(LOCAL_OPTIONAL_USES_LIBRARIES))
191
192  # dexpreopt needs the paths to the dex jars of these libraries in case
193  # construct_context.sh needs to pass them to dex2oat.
194  my_extra_dexpreopt_libs := \
195    org.apache.http.legacy \
196    android.hidl.base-V1.0-java \
197    android.hidl.manager-V1.0-java \
198
199  my_dexpreopt_libs := $(sort \
200    $(LOCAL_USES_LIBRARIES) \
201    $(my_filtered_optional_uses_libraries) \
202    $(my_extra_dexpreopt_libs) \
203  )
204
205  # Record dex-preopt config.
206  DEXPREOPT.$(LOCAL_MODULE).DEX_PREOPT := $(LOCAL_DEX_PREOPT)
207  DEXPREOPT.$(LOCAL_MODULE).MULTILIB := $(LOCAL_MULTILIB)
208  DEXPREOPT.$(LOCAL_MODULE).DEX_PREOPT_FLAGS := $(LOCAL_DEX_PREOPT_FLAGS)
209  DEXPREOPT.$(LOCAL_MODULE).PRIVILEGED_MODULE := $(LOCAL_PRIVILEGED_MODULE)
210  DEXPREOPT.$(LOCAL_MODULE).VENDOR_MODULE := $(LOCAL_VENDOR_MODULE)
211  DEXPREOPT.$(LOCAL_MODULE).TARGET_ARCH := $(LOCAL_MODULE_TARGET_ARCH)
212  DEXPREOPT.$(LOCAL_MODULE).INSTALLED_STRIPPED := $(LOCAL_INSTALLED_MODULE)
213  DEXPREOPT.MODULES.$(LOCAL_MODULE_CLASS) := $(sort \
214    $(DEXPREOPT.MODULES.$(LOCAL_MODULE_CLASS)) $(LOCAL_MODULE))
215
216  $(call json_start)
217
218  # DexPath is not set: it will be filled in by dexpreopt_gen.
219
220  $(call add_json_str,  Name,                           $(LOCAL_MODULE))
221  $(call add_json_str,  DexLocation,                    $(patsubst $(PRODUCT_OUT)%,%,$(LOCAL_INSTALLED_MODULE)))
222  $(call add_json_str,  BuildPath,                      $(LOCAL_BUILT_MODULE))
223  $(call add_json_str,  ManifestPath,                   $(full_android_manifest))
224  $(call add_json_str,  ExtrasOutputPath,               $$2)
225  $(call add_json_bool, Privileged,                     $(filter true,$(LOCAL_PRIVILEGED_MODULE)))
226  $(call add_json_bool, UncompressedDex,                $(filter true,$(LOCAL_UNCOMPRESS_DEX)))
227  $(call add_json_bool, HasApkLibraries,                $(LOCAL_APK_LIBRARIES))
228  $(call add_json_list, PreoptFlags,                    $(LOCAL_DEX_PREOPT_FLAGS))
229  $(call add_json_str,  ProfileClassListing,            $(if $(my_process_profile),$(LOCAL_DEX_PREOPT_PROFILE)))
230  $(call add_json_bool, ProfileIsTextListing,           $(my_profile_is_text_listing))
231  $(call add_json_bool, EnforceUsesLibraries,           $(LOCAL_ENFORCE_USES_LIBRARIES))
232  $(call add_json_list, OptionalUsesLibraries,          $(my_filtered_optional_uses_libraries))
233  $(call add_json_list, UsesLibraries,                  $(LOCAL_USES_LIBRARIES))
234  $(call add_json_map,  LibraryPaths)
235  $(foreach lib,$(my_dexpreopt_libs),\
236    $(call add_json_str, $(lib), $(call intermediates-dir-for,JAVA_LIBRARIES,$(lib),,COMMON)/javalib.jar))
237  $(call end_json_map)
238  $(call add_json_list, Archs,                          $(my_dexpreopt_archs))
239  $(call add_json_list, DexPreoptImages,                $(my_dexpreopt_images))
240  $(call add_json_list, DexPreoptImageLocations,        $(my_dexpreopt_image_locations))
241  $(call add_json_list, PreoptBootClassPathDexFiles,    $(DEXPREOPT_BOOTCLASSPATH_DEX_FILES))
242  $(call add_json_list, PreoptBootClassPathDexLocations,$(DEXPREOPT_BOOTCLASSPATH_DEX_LOCATIONS))
243  $(call add_json_bool, PreoptExtractedApk,             $(my_preopt_for_extracted_apk))
244  $(call add_json_bool, NoCreateAppImage,               $(filter false,$(LOCAL_DEX_PREOPT_APP_IMAGE)))
245  $(call add_json_bool, ForceCreateAppImage,            $(filter true,$(LOCAL_DEX_PREOPT_APP_IMAGE)))
246  $(call add_json_bool, PresignedPrebuilt,              $(filter PRESIGNED,$(LOCAL_CERTIFICATE)))
247
248  $(call json_end)
249
250  my_dexpreopt_config := $(intermediates)/dexpreopt.config
251  my_dexpreopt_script := $(intermediates)/dexpreopt.sh
252  my_dexpreopt_zip := $(intermediates)/dexpreopt.zip
253
254  $(my_dexpreopt_config): PRIVATE_MODULE := $(LOCAL_MODULE)
255  $(my_dexpreopt_config): PRIVATE_CONTENTS := $(json_contents)
256  $(my_dexpreopt_config):
257	@echo "$(PRIVATE_MODULE) dexpreopt.config"
258	echo -e -n '$(subst $(newline),\n,$(subst ','\'',$(subst \,\\,$(PRIVATE_CONTENTS))))' > $@
259
260  .KATI_RESTAT: $(my_dexpreopt_script)
261  $(my_dexpreopt_script): PRIVATE_MODULE := $(LOCAL_MODULE)
262  $(my_dexpreopt_script): PRIVATE_GLOBAL_SOONG_CONFIG := $(DEX_PREOPT_SOONG_CONFIG_FOR_MAKE)
263  $(my_dexpreopt_script): PRIVATE_GLOBAL_CONFIG := $(DEX_PREOPT_CONFIG_FOR_MAKE)
264  $(my_dexpreopt_script): PRIVATE_MODULE_CONFIG := $(my_dexpreopt_config)
265  $(my_dexpreopt_script): $(DEXPREOPT_GEN)
266  $(my_dexpreopt_script): $(my_dexpreopt_config) $(DEX_PREOPT_SOONG_CONFIG_FOR_MAKE) $(DEX_PREOPT_CONFIG_FOR_MAKE)
267	@echo "$(PRIVATE_MODULE) dexpreopt gen"
268	$(DEXPREOPT_GEN) \
269	-global_soong $(PRIVATE_GLOBAL_SOONG_CONFIG) \
270	-global $(PRIVATE_GLOBAL_CONFIG) \
271	-module $(PRIVATE_MODULE_CONFIG) \
272	-dexpreopt_script $@ \
273	-out_dir $(OUT_DIR)
274
275  my_dexpreopt_deps := $(my_dex_jar)
276  my_dexpreopt_deps += $(if $(my_process_profile),$(LOCAL_DEX_PREOPT_PROFILE))
277  my_dexpreopt_deps += \
278    $(foreach lib, $(my_dexpreopt_libs), \
279      $(call intermediates-dir-for,JAVA_LIBRARIES,$(lib),,COMMON)/javalib.jar)
280  my_dexpreopt_deps += $(my_dexpreopt_images_deps)
281  my_dexpreopt_deps += $(DEXPREOPT_BOOTCLASSPATH_DEX_FILES)
282
283  $(my_dexpreopt_zip): PRIVATE_MODULE := $(LOCAL_MODULE)
284  $(my_dexpreopt_zip): $(my_dexpreopt_deps)
285  $(my_dexpreopt_zip): | $(DEXPREOPT_GEN_DEPS)
286  $(my_dexpreopt_zip): .KATI_DEPFILE := $(my_dexpreopt_zip).d
287  $(my_dexpreopt_zip): PRIVATE_DEX := $(my_dex_jar)
288  $(my_dexpreopt_zip): PRIVATE_SCRIPT := $(my_dexpreopt_script)
289  $(my_dexpreopt_zip): $(my_dexpreopt_script)
290	@echo "$(PRIVATE_MODULE) dexpreopt"
291	bash $(PRIVATE_SCRIPT) $(PRIVATE_DEX) $@
292
293  ifdef LOCAL_POST_INSTALL_CMD
294    # Add a shell command separator
295    LOCAL_POST_INSTALL_CMD += &&
296  endif
297
298  LOCAL_POST_INSTALL_CMD += \
299    for i in $$(zipinfo -1 $(my_dexpreopt_zip)); \
300      do mkdir -p $(PRODUCT_OUT)/$$(dirname $$i); \
301    done && \
302    ( unzip -qoDD -d $(PRODUCT_OUT) $(my_dexpreopt_zip) 2>&1 | grep -v "zipfile is empty"; exit $${PIPESTATUS[0]} ) || \
303      ( code=$$?; if [ $$code -ne 0 -a $$code -ne 1 ]; then exit $$code; fi )
304
305  $(LOCAL_INSTALLED_MODULE): PRIVATE_POST_INSTALL_CMD := $(LOCAL_POST_INSTALL_CMD)
306  $(LOCAL_INSTALLED_MODULE): $(my_dexpreopt_zip)
307
308  $(my_all_targets): $(my_dexpreopt_zip)
309
310  my_dexpreopt_config :=
311  my_dexpreopt_script :=
312  my_dexpreopt_zip :=
313endif # LOCAL_DEX_PREOPT
314