1# Common to host and target Java modules.
2
3###########################################################
4## Java version
5###########################################################
6# Use the LOCAL_JAVA_LANGUAGE_VERSION if it is set, otherwise
7# use one based on the LOCAL_SDK_VERSION. If it is < 24
8# pass "1.7" to the tools, if it is unset, >= 24 or "current"
9# pass "1.8".
10#
11# The LOCAL_SDK_VERSION behavior is to ensure that, by default,
12# code that is expected to run on older releases of Android
13# does not use any 1.8 language features that are not supported
14# on earlier runtimes (like default / static interface methods).
15# Modules can override this logic by specifying
16# LOCAL_JAVA_LANGUAGE_VERSION explicitly.
17ifeq (,$(LOCAL_JAVA_LANGUAGE_VERSION))
18  private_sdk_versions_without_any_java_18_support := 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
19  ifneq (,$(filter $(LOCAL_SDK_VERSION), $(private_sdk_versions_without_any_java_18_support)))
20    LOCAL_JAVA_LANGUAGE_VERSION := 1.7
21  else
22    LOCAL_JAVA_LANGUAGE_VERSION := 1.8
23  endif
24endif
25LOCAL_JAVACFLAGS += -source $(LOCAL_JAVA_LANGUAGE_VERSION) -target $(LOCAL_JAVA_LANGUAGE_VERSION)
26
27###########################################################
28## .proto files: Compile proto files to .java
29###########################################################
30proto_sources := $(filter %.proto,$(LOCAL_SRC_FILES))
31# Because names of the .java files compiled from .proto files are unknown until the
32# .proto files are compiled, we use a timestamp file as depedency.
33proto_java_sources_file_stamp :=
34ifneq ($(proto_sources),)
35proto_sources_fullpath := $(addprefix $(LOCAL_PATH)/, $(proto_sources))
36
37# By putting the generated java files into $(LOCAL_INTERMEDIATE_SOURCE_DIR), they will be
38# automatically found by the java compiling function transform-java-to-classes.jar.
39proto_java_intemediate_dir := $(LOCAL_INTERMEDIATE_SOURCE_DIR)/proto
40proto_java_sources_file_stamp := $(proto_java_intemediate_dir)/Proto.stamp
41proto_java_sources_dir := $(proto_java_intemediate_dir)/src
42
43$(proto_java_sources_file_stamp): PRIVATE_PROTO_INCLUDES := $(TOP)
44$(proto_java_sources_file_stamp): PRIVATE_PROTO_SRC_FILES := $(proto_sources_fullpath)
45$(proto_java_sources_file_stamp): PRIVATE_PROTO_JAVA_OUTPUT_DIR := $(proto_java_sources_dir)
46ifeq ($(LOCAL_PROTOC_OPTIMIZE_TYPE),micro)
47$(proto_java_sources_file_stamp): PRIVATE_PROTO_JAVA_OUTPUT_OPTION := --javamicro_out
48else
49  ifeq ($(LOCAL_PROTOC_OPTIMIZE_TYPE),nano)
50$(proto_java_sources_file_stamp): PRIVATE_PROTO_JAVA_OUTPUT_OPTION := --javanano_out
51  else
52$(proto_java_sources_file_stamp): PRIVATE_PROTO_JAVA_OUTPUT_OPTION := --java_out
53  endif
54endif
55$(proto_java_sources_file_stamp): PRIVATE_PROTOC_FLAGS := $(LOCAL_PROTOC_FLAGS)
56$(proto_java_sources_file_stamp): PRIVATE_PROTO_JAVA_OUTPUT_PARAMS := $(LOCAL_PROTO_JAVA_OUTPUT_PARAMS)
57$(proto_java_sources_file_stamp) : $(proto_sources_fullpath) $(PROTOC)
58	$(call transform-proto-to-java)
59
60#TODO: protoc should output the dependencies introduced by imports.
61
62ALL_MODULES.$(my_register_name).PROTO_FILES := $(proto_sources_fullpath)
63endif # proto_sources
64
65#########################################
66## Java resources
67
68# Look for resource files in any specified directories.
69# Non-java and non-doc files will be picked up as resources
70# and included in the output jar file.
71java_resource_file_groups :=
72
73LOCAL_JAVA_RESOURCE_DIRS := $(strip $(LOCAL_JAVA_RESOURCE_DIRS))
74ifneq ($(LOCAL_JAVA_RESOURCE_DIRS),)
75  # This makes a list of words like
76  #     <dir1>::<file1>:<file2> <dir2>::<file1> <dir3>:
77  # where each of the files is relative to the directory it's grouped with.
78  # Directories that don't contain any resource files will result in groups
79  # that end with a colon, and they are stripped out in the next step.
80  java_resource_file_groups += \
81    $(foreach dir,$(LOCAL_JAVA_RESOURCE_DIRS), \
82	$(subst $(space),:,$(strip \
83		$(LOCAL_PATH)/$(dir): \
84	    $(patsubst ./%,%,$(sort $(shell cd $(LOCAL_PATH)/$(dir) && \
85		find . \
86		    -type d -a -name ".svn" -prune -o \
87		    -type f \
88			-a \! -name "*.java" \
89			-a \! -name "package.html" \
90			-a \! -name "overview.html" \
91			-a \! -name ".*.swp" \
92			-a \! -name ".DS_Store" \
93			-a \! -name "*~" \
94			-print \
95		    ))) \
96	)) \
97    )
98  java_resource_file_groups := $(filter-out %:,$(java_resource_file_groups))
99endif # LOCAL_JAVA_RESOURCE_DIRS
100
101LOCAL_JAVA_RESOURCE_FILES := $(strip $(LOCAL_JAVA_RESOURCE_FILES))
102ifneq ($(LOCAL_JAVA_RESOURCE_FILES),)
103  java_resource_file_groups += \
104    $(foreach f,$(LOCAL_JAVA_RESOURCE_FILES), \
105	$(patsubst %/,%,$(dir $(f)))::$(notdir $(f)) \
106     )
107endif # LOCAL_JAVA_RESOURCE_FILES
108
109ifdef java_resource_file_groups
110  # The full paths to all resources, used for dependencies.
111  java_resource_sources := \
112    $(foreach group,$(java_resource_file_groups), \
113	$(addprefix $(word 1,$(subst :,$(space),$(group)))/, \
114	    $(wordlist 2,9999,$(subst :,$(space),$(group))) \
115	) \
116    )
117  # The arguments to jar that will include these files in a jar file.
118  # Quote the file name to handle special characters (such as #) correctly.
119  extra_jar_args := \
120    $(foreach group,$(java_resource_file_groups), \
121	$(addprefix -C "$(word 1,$(subst :,$(space),$(group)))" , \
122	    $(foreach w, $(wordlist 2,9999,$(subst :,$(space),$(group))), "$(w)" ) \
123	) \
124    )
125  java_resource_file_groups :=
126else
127  java_resource_sources :=
128  extra_jar_args :=
129endif # java_resource_file_groups
130
131######################################
132## PRIVATE java vars
133# LOCAL_SOURCE_FILES_ALL_GENERATED is set only if the module does not have static source files,
134# but generated source files in its LOCAL_INTERMEDIATE_SOURCE_DIR.
135# You have to set up the dependency in some other way.
136need_compile_java := $(strip $(all_java_sources)$(all_res_assets)$(java_resource_sources))$(LOCAL_STATIC_JAVA_LIBRARIES)$(filter true,$(LOCAL_SOURCE_FILES_ALL_GENERATED))
137ifdef need_compile_java
138
139full_static_java_libs := \
140    $(foreach lib,$(LOCAL_STATIC_JAVA_LIBRARIES), \
141      $(call intermediates-dir-for, \
142        JAVA_LIBRARIES,$(lib),$(LOCAL_IS_HOST_MODULE),COMMON)/javalib.jar)
143
144$(LOCAL_INTERMEDIATE_TARGETS): PRIVATE_STATIC_JAVA_LIBRARIES := $(full_static_java_libs)
145
146$(LOCAL_INTERMEDIATE_TARGETS): PRIVATE_RESOURCE_DIR := $(LOCAL_RESOURCE_DIR)
147$(LOCAL_INTERMEDIATE_TARGETS): PRIVATE_ASSET_DIR := $(LOCAL_ASSET_DIR)
148
149$(LOCAL_INTERMEDIATE_TARGETS): PRIVATE_CLASS_INTERMEDIATES_DIR := $(intermediates.COMMON)/classes
150$(LOCAL_INTERMEDIATE_TARGETS): PRIVATE_SOURCE_INTERMEDIATES_DIR := $(intermediates.COMMON)/src
151$(LOCAL_INTERMEDIATE_TARGETS): PRIVATE_JAVA_SOURCES := $(all_java_sources)
152
153$(LOCAL_INTERMEDIATE_TARGETS): PRIVATE_RMTYPEDEFS := $(LOCAL_RMTYPEDEFS)
154
155# full_java_libs: The list of files that should be used as the classpath.
156#                 Using this list as a dependency list WILL NOT WORK.
157# full_java_lib_deps: Should be specified as a prerequisite of this module
158#                 to guarantee that the files in full_java_libs will
159#                 be up-to-date.
160ifndef LOCAL_IS_HOST_MODULE
161ifeq ($(LOCAL_SDK_VERSION),)
162ifeq ($(LOCAL_NO_STANDARD_LIBRARIES),true)
163# No bootclasspath. But we still need "" to prevent javac from using default host bootclasspath.
164my_bootclasspath := ""
165else  # LOCAL_NO_STANDARD_LIBRARIES
166my_bootclasspath := $(call java-lib-files,core-oj):$(call java-lib-files,core-libart)
167endif  # LOCAL_NO_STANDARD_LIBRARIES
168else
169ifeq ($(LOCAL_SDK_VERSION)$(TARGET_BUILD_APPS),current)
170# LOCAL_SDK_VERSION is current and no TARGET_BUILD_APPS.
171my_bootclasspath := $(call java-lib-files,android_stubs_current)
172else ifeq ($(LOCAL_SDK_VERSION)$(TARGET_BUILD_APPS),system_current)
173my_bootclasspath := $(call java-lib-files,android_system_stubs_current)
174else ifeq ($(LOCAL_SDK_VERSION)$(TARGET_BUILD_APPS),test_current)
175my_bootclasspath := $(call java-lib-files,android_test_stubs_current)
176else
177my_bootclasspath := $(call java-lib-files,sdk_v$(LOCAL_SDK_VERSION))
178endif # current, system_current, or test_current
179endif # LOCAL_SDK_VERSION
180$(LOCAL_INTERMEDIATE_TARGETS): PRIVATE_BOOTCLASSPATH := -bootclasspath $(my_bootclasspath)
181
182# In order to compile lambda code javac requires various invokedynamic-
183# related classes to be present. This change adds stubs needed for
184# javac to compile lambdas.
185my_additional_javac_libs :=
186ifndef TARGET_BUILD_APPS
187# TODO: support to build lamdbas using javac in unbundled build.
188# We may need to check in a prebuilt core-lambda-stubs to prebuilts/sdk.
189ifneq ($(LOCAL_NO_STANDARD_LIBRARIES),true)
190my_additional_javac_libs := core-lambda-stubs
191endif
192endif
193
194full_shared_java_libs := $(call java-lib-files,$(LOCAL_JAVA_LIBRARIES) $(my_additional_javac_libs),$(LOCAL_IS_HOST_MODULE))
195full_java_lib_deps := $(call java-lib-deps,$(LOCAL_JAVA_LIBRARIES) $(my_additional_javac_libs),$(LOCAL_IS_HOST_MODULE))
196full_java_lib_deps := $(addsuffix .toc, $(full_java_lib_deps))
197
198else # LOCAL_IS_HOST_MODULE
199
200ifeq ($(USE_CORE_LIB_BOOTCLASSPATH),true)
201ifeq ($(LOCAL_NO_STANDARD_LIBRARIES),true)
202my_bootclasspath := ""
203else
204my_bootclasspath := $(call normalize-path-list,$(call host-dex-java-lib-files,core-oj-hostdex core-libart-hostdex))
205endif
206$(LOCAL_INTERMEDIATE_TARGETS): PRIVATE_BOOTCLASSPATH := -bootclasspath $(my_bootclasspath)
207
208full_shared_java_libs := $(call host-dex-java-lib-files,$(LOCAL_JAVA_LIBRARIES))
209full_java_lib_deps := $(full_shared_java_libs)
210else # !USE_CORE_LIB_BOOTCLASSPATH
211$(LOCAL_INTERMEDIATE_TARGETS): PRIVATE_BOOTCLASSPATH :=
212
213full_shared_java_libs := $(addprefix $(HOST_OUT_JAVA_LIBRARIES)/,\
214    $(addsuffix $(COMMON_JAVA_PACKAGE_SUFFIX),$(LOCAL_JAVA_LIBRARIES)))
215full_java_lib_deps := $(full_shared_java_libs)
216endif # USE_CORE_LIB_BOOTCLASSPATH
217endif # !LOCAL_IS_HOST_MODULE
218
219full_java_libs := $(full_shared_java_libs) $(full_static_java_libs) $(LOCAL_CLASSPATH)
220full_java_lib_deps := $(full_java_lib_deps) $(full_static_java_libs) $(LOCAL_CLASSPATH)
221
222ifndef LOCAL_IS_HOST_MODULE
223# This is set by packages that are linking to other packages that export
224# shared libraries, allowing them to make use of the code in the linked apk.
225apk_libraries := $(sort $(LOCAL_APK_LIBRARIES) $(LOCAL_RES_LIBRARIES))
226ifneq ($(apk_libraries),)
227  link_apk_libraries := \
228      $(foreach lib,$(apk_libraries), \
229        $(call intermediates-dir-for, \
230              APPS,$(lib),,COMMON)/classes.jar)
231
232  # link against the jar with full original names (before proguard processing).
233  full_shared_java_libs += $(link_apk_libraries)
234  full_java_libs += $(link_apk_libraries)
235  full_java_lib_deps += $(link_apk_libraries)
236endif
237
238# This is set by packages that contain instrumentation, allowing them to
239# link against the package they are instrumenting.  Currently only one such
240# package is allowed.
241LOCAL_INSTRUMENTATION_FOR := $(strip $(LOCAL_INSTRUMENTATION_FOR))
242ifdef LOCAL_INSTRUMENTATION_FOR
243  ifneq ($(words $(LOCAL_INSTRUMENTATION_FOR)),1)
244    $(error \
245        $(LOCAL_PATH): Multiple LOCAL_INSTRUMENTATION_FOR members defined)
246  endif
247
248  link_instr_intermediates_dir.COMMON := $(call intermediates-dir-for, \
249      APPS,$(LOCAL_INSTRUMENTATION_FOR),,COMMON)
250  # link against the jar with full original names (before proguard processing).
251  link_instr_classes_jar := $(link_instr_intermediates_dir.COMMON)/classes.jar
252  full_java_libs += $(link_instr_classes_jar)
253  full_java_lib_deps += $(link_instr_classes_jar)
254endif  # LOCAL_INSTRUMENTATION_FOR
255endif  # LOCAL_IS_HOST_MODULE
256
257endif  # need_compile_java
258
259# We may want to add jar manifest or jar resource files even if there is no java code at all.
260$(LOCAL_INTERMEDIATE_TARGETS): PRIVATE_EXTRA_JAR_ARGS := $(extra_jar_args)
261jar_manifest_file :=
262ifneq ($(strip $(LOCAL_JAR_MANIFEST)),)
263jar_manifest_file := $(LOCAL_PATH)/$(LOCAL_JAR_MANIFEST)
264$(LOCAL_INTERMEDIATE_TARGETS): PRIVATE_JAR_MANIFEST := $(jar_manifest_file)
265else
266$(LOCAL_INTERMEDIATE_TARGETS): PRIVATE_JAR_MANIFEST :=
267endif
268
269##########################################################
270ifndef LOCAL_IS_HOST_MODULE
271## AAPT Flags
272# aapt doesn't accept multiple --extra-packages flags.
273# We have to collapse them into a single --extra-packages flag here.
274LOCAL_AAPT_FLAGS := $(strip $(LOCAL_AAPT_FLAGS))
275ifdef LOCAL_AAPT_FLAGS
276ifeq ($(filter 0 1,$(words $(filter --extra-packages,$(LOCAL_AAPT_FLAGS)))),)
277aapt_flags := $(subst --extra-packages$(space),--extra-packages@,$(LOCAL_AAPT_FLAGS))
278aapt_flags_extra_packages := $(patsubst --extra-packages@%,%,$(filter --extra-packages@%,$(aapt_flags)))
279aapt_flags_extra_packages := $(sort $(subst :,$(space),$(aapt_flags_extra_packages)))
280LOCAL_AAPT_FLAGS := $(filter-out --extra-packages@%,$(aapt_flags)) \
281    --extra-packages $(subst $(space),:,$(aapt_flags_extra_packages))
282aapt_flags_extra_packages :=
283aapt_flags :=
284endif
285endif
286
287$(LOCAL_INTERMEDIATE_TARGETS): PRIVATE_AAPT_FLAGS := $(LOCAL_AAPT_FLAGS) $(PRODUCT_AAPT_FLAGS)
288$(LOCAL_INTERMEDIATE_TARGETS): PRIVATE_TARGET_AAPT_CHARACTERISTICS := $(TARGET_AAPT_CHARACTERISTICS)
289$(LOCAL_INTERMEDIATE_TARGETS): PRIVATE_MANIFEST_PACKAGE_NAME := $(LOCAL_MANIFEST_PACKAGE_NAME)
290$(LOCAL_INTERMEDIATE_TARGETS): PRIVATE_MANIFEST_INSTRUMENTATION_FOR := $(LOCAL_MANIFEST_INSTRUMENTATION_FOR)
291
292ifdef aidl_sources
293ALL_MODULES.$(my_register_name).AIDL_FILES := $(aidl_sources)
294endif
295ifdef renderscript_sources
296ALL_MODULES.$(my_register_name).RS_FILES := $(renderscript_sources_fullpath)
297endif
298endif  # !LOCAL_IS_HOST_MODULE
299
300$(LOCAL_INTERMEDIATE_TARGETS): PRIVATE_ALL_JAVA_LIBRARIES := $(full_java_libs)
301
302ALL_MODULES.$(my_register_name).INTERMEDIATE_SOURCE_DIR := \
303    $(ALL_MODULES.$(my_register_name).INTERMEDIATE_SOURCE_DIR) $(LOCAL_INTERMEDIATE_SOURCE_DIR)
304
305###########################################################
306# JACK
307###########################################################
308ifdef LOCAL_JACK_ENABLED
309ifdef need_compile_java
310
311LOCAL_JACK_FLAGS += -D jack.java.source.version=$(LOCAL_JAVA_LANGUAGE_VERSION)
312
313full_static_jack_libs := \
314    $(foreach lib,$(LOCAL_STATIC_JAVA_LIBRARIES), \
315      $(call intermediates-dir-for, \
316        JAVA_LIBRARIES,$(lib),$(LOCAL_IS_HOST_MODULE),COMMON)/classes.jack)
317
318$(LOCAL_INTERMEDIATE_TARGETS): PRIVATE_STATIC_JACK_LIBRARIES := $(full_static_jack_libs)
319
320ifndef LOCAL_IS_HOST_MODULE
321ifeq ($(LOCAL_SDK_VERSION),)
322ifeq ($(LOCAL_NO_STANDARD_LIBRARIES),true)
323my_bootclasspath :=
324else
325my_bootclasspath := $(call jack-lib-files,core-oj core-libart)
326endif
327else  # LOCAL_SDK_VERSION
328ifeq ($(LOCAL_SDK_VERSION)$(TARGET_BUILD_APPS),current)
329# LOCAL_SDK_VERSION is current and no TARGET_BUILD_APPS.
330my_bootclasspath := $(call jack-lib-files,android_stubs_current)
331else ifeq ($(LOCAL_SDK_VERSION)$(TARGET_BUILD_APPS),system_current)
332my_bootclasspath := $(call jack-lib-files,android_system_stubs_current)
333else ifeq ($(LOCAL_SDK_VERSION)$(TARGET_BUILD_APPS),test_current)
334my_bootclasspath := $(call jack-lib-files,android_test_stubs_current)
335else
336my_bootclasspath :=$(call jack-lib-files,sdk_v$(LOCAL_SDK_VERSION))
337endif # current, system_current, or test_current
338endif # LOCAL_SDK_VERSION
339$(LOCAL_INTERMEDIATE_TARGETS): PRIVATE_BOOTCLASSPATH_JAVA_LIBRARIES := $(my_bootclasspath)
340
341full_shared_jack_libs := $(call jack-lib-files,$(LOCAL_JAVA_LIBRARIES),$(LOCAL_IS_HOST_MODULE))
342full_jack_deps := $(call jack-lib-deps,$(LOCAL_JAVA_LIBRARIES),$(LOCAL_IS_HOST_MODULE))
343# Turn off .toc optimization for apps build as we cannot build dexdump.
344ifeq (,$(TARGET_BUILD_APPS))
345full_jack_deps := $(patsubst %.jack, %.dex.toc, $(full_jack_deps))
346endif
347
348else # LOCAL_IS_HOST_MODULE
349
350ifeq ($(USE_CORE_LIB_BOOTCLASSPATH),true)
351ifeq ($(LOCAL_NO_STANDARD_LIBRARIES),true)
352my_bootclasspath :=
353else
354my_bootclasspath := $(call jack-lib-files,core-oj-hostdex core-libart-hostdex,$(LOCAL_IS_HOST_MODULE))
355endif
356$(LOCAL_INTERMEDIATE_TARGETS): PRIVATE_BOOTCLASSPATH_JAVA_LIBRARIES := $(my_bootclasspath)
357# Compiling against the final jack library. If we want to add support for obfuscated library
358# we'll need to change that to compile against the not obfuscated jack library.
359full_shared_jack_libs := $(call jack-lib-files,$(LOCAL_JAVA_LIBRARIES),$(LOCAL_IS_HOST_MODULE))
360full_jack_deps := $(call jack-lib-deps,$(LOCAL_JAVA_LIBRARIES),$(LOCAL_IS_HOST_MODULE))
361else
362$(LOCAL_INTERMEDIATE_TARGETS): PRIVATE_BOOTCLASSPATH_JAVA_LIBRARIES :=
363full_shared_jack_libs := $(call jack-lib-deps,$(LOCAL_JAVA_LIBRARIES),$(LOCAL_IS_HOST_MODULE))
364full_jack_deps := $(full_shared_jack_libs)
365endif # USE_CORE_LIB_BOOTCLASSPATH
366endif # !LOCAL_IS_HOST_MODULE
367full_jack_libs := $(full_shared_jack_libs) $(full_static_jack_libs) $(LOCAL_JACK_CLASSPATH)
368full_jack_deps += $(full_static_jack_libs) $(LOCAL_JACK_CLASSPATH)
369
370ifndef LOCAL_IS_HOST_MODULE
371# This is set by packages that are linking to other packages that export
372# shared libraries, allowing them to make use of the code in the linked apk.
373ifneq ($(apk_libraries),)
374  link_apk_jack_libraries := \
375      $(foreach lib,$(apk_libraries), \
376        $(call intermediates-dir-for, \
377              APPS,$(lib),,COMMON)/classes.jack)
378
379  # link against the jar with full original names (before proguard processing).
380  full_shared_jack_libs += $(link_apk_jack_libraries)
381  full_jack_libs += $(link_apk_jack_libraries)
382  full_jack_deps += $(link_apk_jack_libraries)
383endif
384
385# This is set by packages that contain instrumentation, allowing them to
386# link against the package they are instrumenting.  Currently only one such
387# package is allowed.
388ifdef LOCAL_INSTRUMENTATION_FOR
389   # link against the jar with full original names (before proguard processing).
390   link_instr_classes_jack := $(link_instr_intermediates_dir.COMMON)/classes.noshrob.jack
391   full_jack_libs += $(link_instr_classes_jack)
392   full_jack_deps += $(link_instr_classes_jack)
393endif  # LOCAL_INSTRUMENTATION_FOR
394endif  # !LOCAL_IS_HOST_MODULE
395
396# Propagate local configuration options to this target.
397$(LOCAL_INTERMEDIATE_TARGETS): PRIVATE_ALL_JACK_LIBRARIES:= $(full_jack_libs)
398$(LOCAL_INTERMEDIATE_TARGETS): PRIVATE_JARJAR_RULES := $(LOCAL_JARJAR_RULES)
399
400endif  # need_compile_java
401endif # LOCAL_JACK_ENABLED
402