definitions.mk revision 18a2229babcb5f6fd57010d2408ae2355ed1e2a2
1#
2# Copyright (C) 2008 The Android Open Source Project
3#
4# Licensed under the Apache License, Version 2.0 (the "License");
5# you may not use this file except in compliance with the License.
6# You may obtain a copy of the License at
7#
8#      http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS,
12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13# See the License for the specific language governing permissions and
14# limitations under the License.
15#
16
17##
18## Common build system definitions.  Mostly standard
19## commands for building various types of targets, which
20## are used by others to construct the final targets.
21##
22
23# These are variables we use to collect overall lists
24# of things being processed.
25
26# Full paths to all of the documentation
27ALL_DOCS:=
28
29# The short names of all of the targets in the system.
30# For each element of ALL_MODULES, two other variables
31# are defined:
32#   $(ALL_MODULES.$(target)).BUILT
33#   $(ALL_MODULES.$(target)).INSTALLED
34# The BUILT variable contains LOCAL_BUILT_MODULE for that
35# target, and the INSTALLED variable contains the LOCAL_INSTALLED_MODULE.
36# Some targets may have multiple files listed in the BUILT and INSTALLED
37# sub-variables.
38ALL_MODULES:=
39
40# Full paths to targets that should be added to the "make droid"
41# set of installed targets.
42ALL_DEFAULT_INSTALLED_MODULES:=
43
44# The list of tags that have been defined by
45# LOCAL_MODULE_TAGS.  Each word in this variable maps
46# to a corresponding ALL_MODULE_TAGS.<tagname> variable
47# that contains all of the INSTALLED_MODULEs with that tag.
48ALL_MODULE_TAGS:=
49
50# Similar to ALL_MODULE_TAGS, but contains the short names
51# of all targets for a particular tag.  The top-level variable
52# won't have the list of tags;  ust ALL_MODULE_TAGS to get
53# the list of all known tags.  (This means that this variable
54# will always be empty; it's just here as a placeholder for
55# its sub-variables.)
56ALL_MODULE_NAME_TAGS:=
57
58# Full path to all files that are made by some tool
59ALL_GENERATED_SOURCES:=
60
61# Full path to all asm, C, C++, lex and yacc generated C files.
62# These all have an order-only dependency on the copied headers
63ALL_C_CPP_ETC_OBJECTS:=
64
65# The list of dynamic binaries that haven't been stripped/compressed/etc.
66ALL_ORIGINAL_DYNAMIC_BINARIES:=
67
68# These files go into the SDK
69ALL_SDK_FILES:=
70
71# Files for dalvik.  This is often build without building the rest of the OS.
72INTERNAL_DALVIK_MODULES:=
73
74# All findbugs xml files
75ALL_FINDBUGS_FILES:=
76
77# GPL module license files
78ALL_GPL_MODULE_LICENSE_FILES:=
79
80# Target and host installed module's dependencies on shared libraries.
81# They are list of "<module_name>:<installed_file>:lib1,lib2...".
82TARGET_DEPENDENCIES_ON_SHARED_LIBRARIES :=
83$(TARGET_2ND_ARCH_VAR_PREFIX)TARGET_DEPENDENCIES_ON_SHARED_LIBRARIES :=
84HOST_DEPENDENCIES_ON_SHARED_LIBRARIES :=
85$(HOST_2ND_ARCH_VAR_PREFIX)HOST_DEPENDENCIES_ON_SHARED_LIBRARIES :=
86HOST_CROSS_DEPENDENCIES_ON_SHARED_LIBRARIES :=
87$(HOST_CROSS_2ND_ARCH_VAR_PREFIX)HOST_CROSS_DEPENDENCIES_ON_SHARED_LIBRARIES :=
88
89# Generated class file names for Android resource.
90# They are escaped and quoted so can be passed safely to a bash command.
91ANDROID_RESOURCE_GENERATED_CLASSES := 'R.class' 'R$$*.class' 'Manifest.class' 'Manifest$$*.class'
92
93# Display names for various build targets
94TARGET_DISPLAY := target
95HOST_DISPLAY := host
96HOST_CROSS_DISPLAY := host cross
97
98###########################################################
99## Debugging; prints a variable list to stdout
100###########################################################
101
102# $(1): variable name list, not variable values
103define print-vars
104$(foreach var,$(1), \
105  $(info $(var):) \
106  $(foreach word,$($(var)), \
107    $(info $(space)$(space)$(word)) \
108   ) \
109 )
110endef
111
112###########################################################
113## Evaluates to true if the string contains the word true,
114## and empty otherwise
115## $(1): a var to test
116###########################################################
117
118define true-or-empty
119$(filter true, $(1))
120endef
121
122
123###########################################################
124## Retrieve the directory of the current makefile
125## Must be called before including any other makefile!!
126###########################################################
127
128# Figure out where we are.
129define my-dir
130$(strip \
131  $(eval LOCAL_MODULE_MAKEFILE := $$(lastword $$(MAKEFILE_LIST))) \
132  $(if $(filter $(BUILD_SYSTEM)/% $(OUT_DIR)/%,$(LOCAL_MODULE_MAKEFILE)), \
133    $(error my-dir must be called before including any other makefile.) \
134   , \
135    $(patsubst %/,%,$(dir $(LOCAL_MODULE_MAKEFILE))) \
136   ) \
137 )
138endef
139
140###########################################################
141## Remove any makefiles that are being handled by soong
142###########################################################
143ifeq ($(USE_SOONG),true)
144define filter-soong-makefiles
145$(foreach mk,$(1),\
146  $(if $(wildcard $(patsubst %/Android.mk,%/Android.bp,$(mk))),\
147    $(if $(wildcard $(patsubst %/Android.mk,%/Android.soong.mk,$(mk))),\
148      $(info skipping $(mk), but including Android.soong.mk ...)\
149        $(patsubst %/Android.mk,%/Android.soong.mk,$(mk)),\
150      $(info skipping $(mk) ...)),\
151    $(mk)))
152endef
153else
154define filter-soong-makefiles
155$(1)
156endef
157endif
158
159###########################################################
160## Retrieve a list of all makefiles immediately below some directory
161###########################################################
162
163define all-makefiles-under
164$(sort $(call filter-soong-makefiles,$(wildcard $(1)/*/Android.mk)))
165endef
166
167###########################################################
168## Look under a directory for makefiles that don't have parent
169## makefiles.
170###########################################################
171
172# $(1): directory to search under
173# Ignores $(1)/Android.mk
174define first-makefiles-under
175$(call filter-soong-makefiles,\
176  $(shell build/tools/findleaves.py $(FIND_LEAVES_EXCLUDES) \
177        --mindepth=2 $(1) Android.mk))
178endef
179
180###########################################################
181## Retrieve a list of all makefiles immediately below your directory
182## Must be called before including any other makefile!!
183###########################################################
184
185define all-subdir-makefiles
186$(call all-makefiles-under,$(call my-dir))
187endef
188
189###########################################################
190## Look in the named list of directories for makefiles,
191## relative to the current directory.
192## Must be called before including any other makefile!!
193###########################################################
194
195# $(1): List of directories to look for under this directory
196define all-named-subdir-makefiles
197$(sort $(call filter-soong-makefiles,\
198  $(wildcard $(addsuffix /Android.mk, $(addprefix $(call my-dir)/,$(1))))))
199endef
200
201###########################################################
202## Find all of the directories under the named directories with
203## the specified name.
204## Meant to be used like:
205##    INC_DIRS := $(call all-named-dirs-under,inc,.)
206###########################################################
207
208define all-named-dirs-under
209$(call find-subdir-files,$(2) -type d -name "$(1)")
210endef
211
212###########################################################
213## Find all the directories under the current directory that
214## haves name that match $(1)
215###########################################################
216
217define all-subdir-named-dirs
218$(call all-named-dirs-under,$(1),.)
219endef
220
221###########################################################
222## Find all of the files under the named directories with
223## the specified name.
224## Meant to be used like:
225##    SRC_FILES := $(call all-named-files-under,*.h,src tests)
226###########################################################
227
228define all-named-files-under
229$(call find-files-in-subdirs,$(LOCAL_PATH),"$(1)",$(2))
230endef
231
232###########################################################
233## Find all of the files under the current directory with
234## the specified name.
235###########################################################
236
237define all-subdir-named-files
238$(call all-named-files-under,$(1),.)
239endef
240
241###########################################################
242## Find all of the java files under the named directories.
243## Meant to be used like:
244##    SRC_FILES := $(call all-java-files-under,src tests)
245###########################################################
246
247define all-java-files-under
248$(call all-named-files-under,*.java,$(1))
249endef
250
251###########################################################
252## Find all of the java files from here.  Meant to be used like:
253##    SRC_FILES := $(call all-subdir-java-files)
254###########################################################
255
256define all-subdir-java-files
257$(call all-java-files-under,.)
258endef
259
260###########################################################
261## Find all of the c files under the named directories.
262## Meant to be used like:
263##    SRC_FILES := $(call all-c-files-under,src tests)
264###########################################################
265
266define all-c-files-under
267$(call all-named-files-under,*.c,$(1))
268endef
269
270###########################################################
271## Find all of the c files from here.  Meant to be used like:
272##    SRC_FILES := $(call all-subdir-c-files)
273###########################################################
274
275define all-subdir-c-files
276$(call all-c-files-under,.)
277endef
278
279###########################################################
280## Find all of the cpp files under the named directories.
281## LOCAL_CPP_EXTENSION is respected if set.
282## Meant to be used like:
283##    SRC_FILES := $(call all-cpp-files-under,src tests)
284###########################################################
285
286define all-cpp-files-under
287$(sort $(patsubst ./%,%, \
288  $(shell cd $(LOCAL_PATH) ; \
289          find -L $(1) -name "*$(or $(LOCAL_CPP_EXTENSION),.cpp)" -and -not -name ".*") \
290 ))
291endef
292
293###########################################################
294## Find all of the cpp files from here.  Meant to be used like:
295##    SRC_FILES := $(call all-subdir-cpp-files)
296###########################################################
297
298define all-subdir-cpp-files
299$(call all-cpp-files-under,.)
300endef
301
302###########################################################
303## Find all files named "I*.aidl" under the named directories,
304## which must be relative to $(LOCAL_PATH).  The returned list
305## is relative to $(LOCAL_PATH).
306###########################################################
307
308define all-Iaidl-files-under
309$(call all-named-files-under,I*.aidl,$(1))
310endef
311
312###########################################################
313## Find all of the "I*.aidl" files under $(LOCAL_PATH).
314###########################################################
315
316define all-subdir-Iaidl-files
317$(call all-Iaidl-files-under,.)
318endef
319
320###########################################################
321## Find all files named "*.vts" under the named directories,
322## which must be relative to $(LOCAL_PATH).  The returned list
323## is relative to $(LOCAL_PATH).
324###########################################################
325
326define all-vts-files-under
327$(call all-named-files-under,*.vts,$(1))
328endef
329
330###########################################################
331## Find all of the "*.vts" files under $(LOCAL_PATH).
332###########################################################
333
334define all-subdir-vts-files
335$(call all-vts-files-under,.)
336endef
337
338###########################################################
339## Find all of the logtags files under the named directories.
340## Meant to be used like:
341##    SRC_FILES := $(call all-logtags-files-under,src)
342###########################################################
343
344define all-logtags-files-under
345$(call all-named-files-under,*.logtags,$(1))
346endef
347
348###########################################################
349## Find all of the .proto files under the named directories.
350## Meant to be used like:
351##    SRC_FILES := $(call all-proto-files-under,src)
352###########################################################
353
354define all-proto-files-under
355$(call all-named-files-under,*.proto,$(1))
356endef
357
358###########################################################
359## Find all of the RenderScript files under the named directories.
360##  Meant to be used like:
361##    SRC_FILES := $(call all-renderscript-files-under,src)
362###########################################################
363
364define all-renderscript-files-under
365$(call find-subdir-files,$(1) \( -name "*.rs" -or -name "*.fs" \) -and -not -name ".*")
366endef
367
368###########################################################
369## Find all of the S files under the named directories.
370## Meant to be used like:
371##    SRC_FILES := $(call all-c-files-under,src tests)
372###########################################################
373
374define all-S-files-under
375$(call all-named-files-under,*.S,$(1))
376endef
377
378###########################################################
379## Find all of the html files under the named directories.
380## Meant to be used like:
381##    SRC_FILES := $(call all-html-files-under,src tests)
382###########################################################
383
384define all-html-files-under
385$(call all-named-files-under,*.html,$(1))
386endef
387
388###########################################################
389## Find all of the html files from here.  Meant to be used like:
390##    SRC_FILES := $(call all-subdir-html-files)
391###########################################################
392
393define all-subdir-html-files
394$(call all-html-files-under,.)
395endef
396
397###########################################################
398## Find all of the files matching pattern
399##    SRC_FILES := $(call find-subdir-files, <pattern>)
400###########################################################
401
402define find-subdir-files
403$(sort $(patsubst ./%,%,$(shell cd $(LOCAL_PATH) ; find -L $(1))))
404endef
405
406###########################################################
407# find the files in the subdirectory $1 of LOCAL_DIR
408# matching pattern $2, filtering out files $3
409# e.g.
410#     SRC_FILES += $(call find-subdir-subdir-files, \
411#                         css, *.cpp, DontWantThis.cpp)
412###########################################################
413
414define find-subdir-subdir-files
415$(sort $(filter-out $(patsubst %,$(1)/%,$(3)),$(patsubst ./%,%,$(shell cd \
416            $(LOCAL_PATH) ; find -L $(1) -maxdepth 1 -name $(2)))))
417endef
418
419###########################################################
420## Find all of the files matching pattern
421##    SRC_FILES := $(call all-subdir-java-files)
422###########################################################
423
424define find-subdir-assets
425$(sort $(if $(1),$(patsubst ./%,%, \
426	$(shell if [ -d $(1) ] ; then cd $(1) ; find -L ./ -not -name '.*' -and -type f ; fi)), \
427	$(warning Empty argument supplied to find-subdir-assets) \
428))
429endef
430
431###########################################################
432## Find various file types in a list of directories relative to $(LOCAL_PATH)
433###########################################################
434
435define find-other-java-files
436$(call all-java-files-under,$(1))
437endef
438
439define find-other-html-files
440$(call all-html-files-under,$(1))
441endef
442
443###########################################################
444# Use utility find to find given files in the given subdirs.
445# This function uses $(1), instead of LOCAL_PATH as the base.
446# $(1): the base dir, relative to the root of the source tree.
447# $(2): the file name pattern to be passed to find as "-name".
448# $(3): a list of subdirs of the base dir.
449# Returns: a list of paths relative to the base dir.
450###########################################################
451
452define find-files-in-subdirs
453$(sort $(patsubst ./%,%, \
454  $(shell cd $(1) ; \
455          find -L $(3) -name $(2) -and -not -name ".*") \
456 ))
457endef
458
459###########################################################
460## Scan through each directory of $(1) looking for files
461## that match $(2) using $(wildcard).  Useful for seeing if
462## a given directory or one of its parents contains
463## a particular file.  Returns the first match found,
464## starting furthest from the root.
465###########################################################
466
467define find-parent-file
468$(strip \
469  $(eval _fpf := $(sort $(wildcard $(foreach f, $(2), $(strip $(1))/$(f))))) \
470  $(if $(_fpf),$(_fpf), \
471       $(if $(filter-out ./ .,$(1)), \
472             $(call find-parent-file,$(patsubst %/,%,$(dir $(1))),$(2)) \
473        ) \
474   ) \
475)
476endef
477
478###########################################################
479## Function we can evaluate to introduce a dynamic dependency
480###########################################################
481
482define add-dependency
483$(1): $(2)
484endef
485
486###########################################################
487## Reverse order of a list
488###########################################################
489
490define reverse-list
491$(if $(1),$(call reverse-list,$(wordlist 2,$(words $(1)),$(1)))) $(firstword $(1))
492endef
493
494###########################################################
495## The intermediates directory.  Where object files go for
496## a given target.  We could technically get away without
497## the "_intermediates" suffix on the directory, but it's
498## nice to be able to grep for that string to find out if
499## anyone's abusing the system.
500###########################################################
501
502# $(1): target class, like "APPS"
503# $(2): target name, like "NotePad"
504# $(3): if non-empty, this is a HOST target.
505# $(4): if non-empty, force the intermediates to be COMMON
506# $(5): if non-empty, force the intermediates to be for the 2nd arch
507# $(6): if non-empty, force the intermediates to be for the host cross os
508define intermediates-dir-for
509$(strip \
510    $(eval _idfClass := $(strip $(1))) \
511    $(if $(_idfClass),, \
512        $(error $(LOCAL_PATH): Class not defined in call to intermediates-dir-for)) \
513    $(eval _idfName := $(strip $(2))) \
514    $(if $(_idfName),, \
515        $(error $(LOCAL_PATH): Name not defined in call to intermediates-dir-for)) \
516    $(eval _idfPrefix := $(if $(strip $(3)),$(if $(strip $(6)),HOST_CROSS,HOST),TARGET)) \
517    $(eval _idf2ndArchPrefix := $(if $(strip $(5)),$(TARGET_2ND_ARCH_VAR_PREFIX))) \
518    $(if $(filter $(_idfPrefix)-$(_idfClass),$(COMMON_MODULE_CLASSES))$(4), \
519        $(eval _idfIntBase := $($(_idfPrefix)_OUT_COMMON_INTERMEDIATES)) \
520      ,$(if $(filter $(_idfClass),$(PER_ARCH_MODULE_CLASSES)),\
521          $(eval _idfIntBase := $($(_idf2ndArchPrefix)$(_idfPrefix)_OUT_INTERMEDIATES)) \
522       ,$(eval _idfIntBase := $($(_idfPrefix)_OUT_INTERMEDIATES)) \
523       ) \
524     ) \
525    $(_idfIntBase)/$(_idfClass)/$(_idfName)_intermediates \
526)
527endef
528
529# Uses LOCAL_MODULE_CLASS, LOCAL_MODULE, and LOCAL_IS_HOST_MODULE
530# to determine the intermediates directory.
531#
532# $(1): if non-empty, force the intermediates to be COMMON
533# $(2): if non-empty, force the intermediates to be for the 2nd arch
534# $(3): if non-empty, force the intermediates to be for the host cross os
535define local-intermediates-dir
536$(strip \
537    $(if $(strip $(LOCAL_MODULE_CLASS)),, \
538        $(error $(LOCAL_PATH): LOCAL_MODULE_CLASS not defined before call to local-intermediates-dir)) \
539    $(if $(strip $(LOCAL_MODULE)),, \
540        $(error $(LOCAL_PATH): LOCAL_MODULE not defined before call to local-intermediates-dir)) \
541    $(call intermediates-dir-for,$(LOCAL_MODULE_CLASS),$(LOCAL_MODULE),$(LOCAL_IS_HOST_MODULE),$(1),$(2),$(3)) \
542)
543endef
544
545###########################################################
546## The generated sources directory.  Placing generated
547## source files directly in the intermediates directory
548## causes problems for multiarch builds, where there are
549## two intermediates directories for a single target. Put
550## them in a separate directory, and they will be copied to
551## each intermediates directory automatically.
552###########################################################
553
554# $(1): target class, like "APPS"
555# $(2): target name, like "NotePad"
556# $(3): if non-empty, this is a HOST target.
557# $(4): if non-empty, force the generated sources to be COMMON
558define generated-sources-dir-for
559$(strip \
560    $(eval _idfClass := $(strip $(1))) \
561    $(if $(_idfClass),, \
562        $(error $(LOCAL_PATH): Class not defined in call to generated-sources-dir-for)) \
563    $(eval _idfName := $(strip $(2))) \
564    $(if $(_idfName),, \
565        $(error $(LOCAL_PATH): Name not defined in call to generated-sources-dir-for)) \
566    $(eval _idfPrefix := $(if $(strip $(3)),HOST,TARGET)) \
567    $(if $(filter $(_idfPrefix)-$(_idfClass),$(COMMON_MODULE_CLASSES))$(4), \
568        $(eval _idfIntBase := $($(_idfPrefix)_OUT_COMMON_GEN)) \
569      , \
570        $(eval _idfIntBase := $($(_idfPrefix)_OUT_GEN)) \
571     ) \
572    $(_idfIntBase)/$(_idfClass)/$(_idfName)_intermediates \
573)
574endef
575
576# Uses LOCAL_MODULE_CLASS, LOCAL_MODULE, and LOCAL_IS_HOST_MODULE
577# to determine the generated sources directory.
578#
579# $(1): if non-empty, force the intermediates to be COMMON
580define local-generated-sources-dir
581$(strip \
582    $(if $(strip $(LOCAL_MODULE_CLASS)),, \
583        $(error $(LOCAL_PATH): LOCAL_MODULE_CLASS not defined before call to local-generated-sources-dir)) \
584    $(if $(strip $(LOCAL_MODULE)),, \
585        $(error $(LOCAL_PATH): LOCAL_MODULE not defined before call to local-generated-sources-dir)) \
586    $(call generated-sources-dir-for,$(LOCAL_MODULE_CLASS),$(LOCAL_MODULE),$(LOCAL_IS_HOST_MODULE),$(1)) \
587)
588endef
589
590###########################################################
591## Convert "path/to/libXXX.so" to "-lXXX".
592## Any "path/to/libXXX.a" elements pass through unchanged.
593###########################################################
594
595define normalize-libraries
596$(foreach so,$(filter %.so,$(1)),-l$(patsubst lib%.so,%,$(notdir $(so))))\
597$(filter-out %.so,$(1))
598endef
599
600# TODO: change users to call the common version.
601define normalize-host-libraries
602$(call normalize-libraries,$(1))
603endef
604
605define normalize-target-libraries
606$(call normalize-libraries,$(1))
607endef
608
609###########################################################
610## Convert a list of short module names (e.g., "framework", "Browser")
611## into the list of files that are built for those modules.
612## NOTE: this won't return reliable results until after all
613## sub-makefiles have been included.
614## $(1): target list
615###########################################################
616
617define module-built-files
618$(foreach module,$(1),$(ALL_MODULES.$(module).BUILT))
619endef
620
621###########################################################
622## Convert a list of short modules names (e.g., "framework", "Browser")
623## into the list of files that are installed for those modules.
624## NOTE: this won't return reliable results until after all
625## sub-makefiles have been included.
626## $(1): target list
627###########################################################
628
629define module-installed-files
630$(foreach module,$(1),$(ALL_MODULES.$(module).INSTALLED))
631endef
632
633###########################################################
634## Convert a list of short modules names (e.g., "framework", "Browser")
635## into the list of files that should be used when linking
636## against that module as a public API.
637## TODO: Allow this for more than JAVA_LIBRARIES modules
638## NOTE: this won't return reliable results until after all
639## sub-makefiles have been included.
640## $(1): target list
641###########################################################
642
643define module-stubs-files
644$(foreach module,$(1),$(ALL_MODULES.$(module).STUBS))
645endef
646
647###########################################################
648## Evaluates to the timestamp file for a doc module, which
649## is the dependency that should be used.
650## $(1): doc module
651###########################################################
652
653define doc-timestamp-for
654$(OUT_DOCS)/$(strip $(1))-timestamp
655endef
656
657
658###########################################################
659## Convert "core ext framework" to "out/.../javalib.jar ..."
660## $(1): library list
661## $(2): Non-empty if IS_HOST_MODULE
662###########################################################
663
664# $(1): library name
665# $(2): Non-empty if IS_HOST_MODULE
666define _java-lib-dir
667$(call intermediates-dir-for, \
668	JAVA_LIBRARIES,$(1),$(2),COMMON)
669endef
670
671# $(1): library name
672# $(2): Non-empty if IS_HOST_MODULE
673define _java-lib-full-classes.jar
674$(call _java-lib-dir,$(1),$(2))/$(if $(2),javalib,classes)$(COMMON_JAVA_PACKAGE_SUFFIX)
675endef
676
677# Get the jar files (you can pass to "javac -classpath") of static or shared
678# Java libraries that you want to link against.
679# $(1): library name list
680# $(2): Non-empty if IS_HOST_MODULE
681define java-lib-files
682$(foreach lib,$(1),$(call _java-lib-full-classes.jar,$(lib),$(2)))
683endef
684
685# Get the dependency files (you can put on the right side of "|" of a build rule)
686# of the Java libraries.
687# $(1): library name list
688# $(2): Non-empty if IS_HOST_MODULE
689# Historically for target Java libraries we used a different file (javalib.jar)
690# as the dependency.
691# Now we can use classes.jar as dependency, so java-lib-deps is the same
692# as java-lib-files.
693define java-lib-deps
694$(call java-lib-files,$(1),$(2))
695endef
696
697# Get the jar files (you can pass to "javac -classpath") of host dalvik Java libraries.
698# You can also use them as dependency files.
699# A host dalvik Java library is different from a host Java library in that
700# the java lib file is classes.jar, not javalib.jar.
701# $(1): library name list
702define host-dex-java-lib-files
703$(foreach lib,$(1),$(call _java-lib-dir,$(lib),true)/classes.jar)
704endef
705
706###########################################################
707## Convert "core ext framework" to "out/.../classes.jack ..."
708## $(1): library list
709## $(2): Non-empty if IS_HOST_MODULE
710###########################################################
711
712# $(1): library name
713# $(2): Non-empty if IS_HOST_MODULE
714define _jack-lib-full-classes
715$(call _java-lib-dir,$(1),$(2))/classes.jack
716endef
717
718# $(1): library name list
719# $(2): Non-empty if IS_HOST_MODULE
720define jack-lib-files
721$(foreach lib,$(1),$(call _jack-lib-full-classes,$(lib),$(2)))
722endef
723
724###########################################################
725## Run rot13 on a string
726## $(1): the string.  Must be one line.
727###########################################################
728define rot13
729$(shell echo $(1) | tr 'a-zA-Z' 'n-za-mN-ZA-M')
730endef
731
732
733###########################################################
734## Returns true if $(1) and $(2) are equal.  Returns
735## the empty string if they are not equal.
736###########################################################
737define streq
738$(strip $(if $(strip $(1)),\
739  $(if $(strip $(2)),\
740    $(if $(filter-out __,_$(subst $(strip $(1)),,$(strip $(2)))$(subst $(strip $(2)),,$(strip $(1)))_),,true), \
741    ),\
742  $(if $(strip $(2)),\
743    ,\
744    true)\
745 ))
746endef
747
748###########################################################
749## Convert "a b c" into "a:b:c"
750###########################################################
751define normalize-path-list
752$(subst $(space),:,$(strip $(1)))
753endef
754
755###########################################################
756## Read the word out of a colon-separated list of words.
757## This has the same behavior as the built-in function
758## $(word n,str).
759##
760## The individual words may not contain spaces.
761##
762## $(1): 1 based index
763## $(2): value of the form a:b:c...
764###########################################################
765
766define word-colon
767$(word $(1),$(subst :,$(space),$(2)))
768endef
769
770###########################################################
771## Convert "a=b c= d e = f" into "a=b c=d e=f"
772##
773## $(1): list to collapse
774## $(2): if set, separator word; usually "=", ":", or ":="
775##       Defaults to "=" if not set.
776###########################################################
777
778define collapse-pairs
779$(eval _cpSEP := $(strip $(if $(2),$(2),=)))\
780$(subst $(space)$(_cpSEP)$(space),$(_cpSEP),$(strip \
781    $(subst $(_cpSEP), $(_cpSEP) ,$(1))))
782endef
783
784###########################################################
785## Given a list of pairs, if multiple pairs have the same
786## first components, keep only the first pair.
787##
788## $(1): list of pairs
789## $(2): the separator word, such as ":", "=", etc.
790define uniq-pairs-by-first-component
791$(eval _upbfc_fc_set :=)\
792$(strip $(foreach w,$(1), $(eval _first := $(word 1,$(subst $(2),$(space),$(w))))\
793    $(if $(filter $(_upbfc_fc_set),$(_first)),,$(w)\
794        $(eval _upbfc_fc_set += $(_first)))))\
795$(eval _upbfc_fc_set :=)\
796$(eval _first:=)
797endef
798
799###########################################################
800## MODULE_TAG set operations
801###########################################################
802
803# Given a list of tags, return the targets that specify
804# any of those tags.
805# $(1): tag list
806define modules-for-tag-list
807$(sort $(foreach tag,$(1),$(foreach m,$(ALL_MODULE_NAME_TAGS.$(tag)),$(ALL_MODULES.$(m).INSTALLED))))
808endef
809
810# Same as modules-for-tag-list, but operates on
811# ALL_MODULE_NAME_TAGS.
812# $(1): tag list
813define module-names-for-tag-list
814$(sort $(foreach tag,$(1),$(ALL_MODULE_NAME_TAGS.$(tag))))
815endef
816
817# Given an accept and reject list, find the matching
818# set of targets.  If a target has multiple tags and
819# any of them are rejected, the target is rejected.
820# Reject overrides accept.
821# $(1): list of tags to accept
822# $(2): list of tags to reject
823#TODO(dbort): do $(if $(strip $(1)),$(1),$(ALL_MODULE_TAGS))
824#TODO(jbq): as of 20100106 nobody uses the second parameter
825define get-tagged-modules
826$(filter-out \
827	$(call modules-for-tag-list,$(2)), \
828	    $(call modules-for-tag-list,$(1)))
829endef
830
831###########################################################
832## Append a leaf to a base path.  Properly deals with
833## base paths ending in /.
834##
835## $(1): base path
836## $(2): leaf path
837###########################################################
838
839define append-path
840$(subst //,/,$(1)/$(2))
841endef
842
843
844###########################################################
845## Package filtering
846###########################################################
847
848# Given a list of installed modules (short or long names)
849# return a list of the packages (yes, .apk packages, not
850# modules in general) that are overridden by this list and,
851# therefore, should not be installed.
852# $(1): mixed list of installed modules
853# TODO: This is fragile; find a reliable way to get this information.
854define _get-package-overrides
855 $(eval ### Discard any words containing slashes, unless they end in .apk, \
856        ### in which case trim off the directory component and the suffix. \
857        ### If there are no slashes, keep the entire word.)
858 $(eval _gpo_names := $(subst /,@@@ @@@,$(1)))
859 $(eval _gpo_names := \
860     $(filter %.apk,$(_gpo_names)) \
861     $(filter-out %@@@ @@@%,$(_gpo_names)))
862 $(eval _gpo_names := $(patsubst %.apk,%,$(_gpo_names)))
863 $(eval _gpo_names := $(patsubst @@@%,%,$(_gpo_names)))
864
865 $(eval ### Remove any remaining words that contain dots.)
866 $(eval _gpo_names := $(subst .,@@@ @@@,$(_gpo_names)))
867 $(eval _gpo_names := $(filter-out %@@@ @@@%,$(_gpo_names)))
868
869 $(eval ### Now we have a list of any words that could possibly refer to \
870        ### packages, although there may be words that do not.  Only \
871        ### real packages will be present under PACKAGES.*, though.)
872 $(foreach _gpo_name,$(_gpo_names),$(PACKAGES.$(_gpo_name).OVERRIDES))
873endef
874
875define get-package-overrides
876$(sort $(strip $(call _get-package-overrides,$(1))))
877endef
878
879###########################################################
880## Output the command lines, or not
881###########################################################
882
883ifeq ($(strip $(SHOW_COMMANDS)),)
884define pretty
885@echo $1
886endef
887else
888define pretty
889endef
890endif
891
892###########################################################
893## Commands for munging the dependency files the compiler generates
894###########################################################
895# $(1): the input .d file
896# $(2): the output .P file
897define transform-d-to-p-args
898$(hide) cp $(1) $(2); \
899	sed -e 's/#.*//' -e 's/^[^:]*: *//' -e 's/ *\\$$//' \
900		-e '/^$$/ d' -e 's/$$/ :/' < $(1) >> $(2); \
901	rm -f $(1)
902endef
903
904define transform-d-to-p
905$(call transform-d-to-p-args,$(@:%.o=%.d),$(@:%.o=%.P))
906endef
907
908###########################################################
909## Commands for including the dependency files the compiler generates
910###########################################################
911# $(1): the .P file
912# $(2): the main build target
913define include-depfile
914$(eval $(2) : .KATI_DEPFILE := $1)
915endef
916
917# $(1): object files
918define include-depfiles-for-objs
919$(foreach obj, $(1), $(call include-depfile, $(obj:%.o=%.P), $(obj)))
920endef
921
922###########################################################
923## Track source files compiled to objects
924###########################################################
925# $(1): list of sources
926# $(2): list of matching objects
927define track-src-file-obj
928$(eval $(call _track-src-file-obj,$(1)))
929endef
930define _track-src-file-obj
931i := w
932$(foreach s,$(1),
933my_tracked_src_files += $(s)
934my_src_file_obj_$(s) := $$(word $$(words $$(i)),$$(2))
935i += w)
936endef
937
938# $(1): list of sources
939# $(2): list of matching generated sources
940define track-src-file-gen
941$(eval $(call _track-src-file-gen,$(2)))
942endef
943define _track-src-file-gen
944i := w
945$(foreach s,$(1),
946my_tracked_gen_files += $(s)
947my_src_file_gen_$(s) := $$(word $$(words $$(i)),$$(1))
948i += w)
949endef
950
951# $(1): list of generated sources
952# $(2): list of matching objects
953define track-gen-file-obj
954$(call track-src-file-obj,$(foreach f,$(1),\
955  $(or $(my_src_file_gen_$(f)),$(f))),$(2))
956endef
957
958###########################################################
959## Commands for running lex
960###########################################################
961
962define transform-l-to-c-or-cpp
963@echo "Lex: $(PRIVATE_MODULE) <= $<"
964@mkdir -p $(dir $@)
965$(hide) $(LEX) -o$@ $<
966endef
967
968###########################################################
969## Commands for running yacc
970##
971###########################################################
972
973define transform-y-to-c-or-cpp
974@echo "Yacc: $(PRIVATE_MODULE) <= $<"
975@mkdir -p $(dir $@)
976$(YACC) $(PRIVATE_YACCFLAGS) \
977  --defines=$(basename $@).h \
978  -o $@ $<
979endef
980
981###########################################################
982## Commands to compile RenderScript to Java
983###########################################################
984
985## Merge multiple .d files generated by llvm-rs-cc. This is necessary
986## because ninja can handle only a single depfile per build target.
987## .d files generated by llvm-rs-cc define .stamp, .bc, and optionally
988## .java as build targets. However, there's no way to let ninja know
989## dependencies to .bc files and .java files, so we give up build
990## targets for them. As we write the .stamp file as the target by
991## ourselves, the awk script removes the first lines before the colon
992## and append a backslash to the last line to concatenate contents of
993## multiple files.
994# $(1): .d files to be merged
995# $(2): merged .d file
996define _merge-renderscript-d
997$(hide) echo '$@: $(backslash)' > $2
998$(foreach d,$1, \
999  $(hide) awk 'start { sub(/( \\)?$$/, " \\"); print } /:/ { start=1 }' < $d >> $2$(newline))
1000$(hide) echo >> $2
1001endef
1002
1003define transform-renderscripts-to-java-and-bc
1004@echo "RenderScript: $(PRIVATE_MODULE) <= $(PRIVATE_RS_SOURCE_FILES)"
1005$(hide) rm -rf $(PRIVATE_RS_OUTPUT_DIR)
1006$(hide) mkdir -p $(PRIVATE_RS_OUTPUT_DIR)/res/raw
1007$(hide) mkdir -p $(PRIVATE_RS_OUTPUT_DIR)/src
1008$(hide) $(PRIVATE_RS_CC) \
1009  -o $(PRIVATE_RS_OUTPUT_DIR)/res/raw \
1010  -p $(PRIVATE_RS_OUTPUT_DIR)/src \
1011  -d $(PRIVATE_RS_OUTPUT_DIR) \
1012  -a $@ -MD \
1013  $(addprefix -target-api , $(PRIVATE_RS_TARGET_API)) \
1014  $(PRIVATE_RS_FLAGS) \
1015  $(foreach inc,$(PRIVATE_RS_INCLUDES),$(addprefix -I , $(inc))) \
1016  $(PRIVATE_RS_SOURCE_FILES)
1017$(call _merge-renderscript-d,$(PRIVATE_DEP_FILES),$@.d)
1018$(call transform-d-to-p-args,$@.d,$@.P)
1019$(hide) mkdir -p $(dir $@)
1020$(hide) touch $@
1021endef
1022
1023define transform-bc-to-so
1024@echo "Renderscript compatibility: $(notdir $@) <= $(notdir $<)"
1025$(hide) mkdir -p $(dir $@)
1026$(hide) $(BCC_COMPAT) -O3 -o $(dir $@)/$(notdir $(<:.bc=.o)) -fPIC -shared \
1027	-rt-path $(RS_PREBUILT_CLCORE) -mtriple $(RS_COMPAT_TRIPLE) $<
1028$(hide) $(PRIVATE_CXX) -shared -Wl,-soname,$(notdir $@) -nostdlib \
1029	-Wl,-rpath,\$$ORIGIN/../lib \
1030	$(dir $@)/$(notdir $(<:.bc=.o)) \
1031	$(RS_PREBUILT_COMPILER_RT) \
1032	-o $@ $(TARGET_GLOBAL_LDFLAGS) -Wl,--hash-style=sysv -L prebuilts/gcc/ \
1033	$(RS_PREBUILT_LIBPATH) -L $(TARGET_OUT_INTERMEDIATE_LIBRARIES) \
1034	-lRSSupport -lm -lc
1035endef
1036
1037###########################################################
1038## Commands to compile RenderScript to C++
1039###########################################################
1040
1041define transform-renderscripts-to-cpp-and-bc
1042@echo "RenderScript: $(PRIVATE_MODULE) <= $(PRIVATE_RS_SOURCE_FILES)"
1043$(hide) rm -rf $(PRIVATE_RS_OUTPUT_DIR)
1044$(hide) mkdir -p $(PRIVATE_RS_OUTPUT_DIR)/
1045$(hide) $(PRIVATE_RS_CC) \
1046  -o $(PRIVATE_RS_OUTPUT_DIR)/ \
1047  -d $(PRIVATE_RS_OUTPUT_DIR) \
1048  -a $@ -MD \
1049  -reflect-c++ \
1050  $(addprefix -target-api , $(PRIVATE_RS_TARGET_API)) \
1051  $(PRIVATE_RS_FLAGS) \
1052  $(addprefix -I , $(PRIVATE_RS_INCLUDES)) \
1053  $(PRIVATE_RS_SOURCE_FILES)
1054$(call _merge-renderscript-d,$(PRIVATE_DEP_FILES),$@.d)
1055$(call transform-d-to-p-args,$@.d,$@.P)
1056$(hide) mkdir -p $(dir $@)
1057$(hide) touch $@
1058endef
1059
1060
1061###########################################################
1062## Commands for running aidl
1063###########################################################
1064
1065define transform-aidl-to-java
1066@mkdir -p $(dir $@)
1067@echo "Aidl: $(PRIVATE_MODULE) <= $<"
1068$(hide) $(AIDL) -d$(patsubst %.java,%.P,$@) $(PRIVATE_AIDL_FLAGS) $< $@
1069endef
1070#$(AIDL) $(PRIVATE_AIDL_FLAGS) $< - | indent -nut -br -npcs -l1000 > $@
1071
1072define transform-aidl-to-cpp
1073@mkdir -p $(dir $@)
1074@mkdir -p $(PRIVATE_HEADER_OUTPUT_DIR)
1075@echo "Generating C++ from AIDL: $(PRIVATE_MODULE) <= $<"
1076$(hide) $(AIDL_CPP) -d$(basename $@).aidl.P $(PRIVATE_AIDL_FLAGS) \
1077    $< $(PRIVATE_HEADER_OUTPUT_DIR) $@
1078endef
1079
1080## Given a .aidl file path generate the rule to compile it a .cpp file.
1081# $(1): a .aidl source file
1082# $(2): a directory to place the generated .cpp files in
1083# $(3): name of a variable to add the path to the generated source file to
1084#
1085# You must call this with $(eval).
1086define define-aidl-cpp-rule
1087define-aidl-cpp-rule-src := $(patsubst %.aidl,%$(LOCAL_CPP_EXTENSION),$(subst ../,dotdot/,$(addprefix $(2)/,$(1))))
1088$$(define-aidl-cpp-rule-src) : $(LOCAL_PATH)/$(1) $(AIDL_CPP)
1089	$$(transform-aidl-to-cpp)
1090$(3) += $$(define-aidl-cpp-rule-src)
1091endef
1092
1093###########################################################
1094## Commands for running vts
1095###########################################################
1096
1097define transform-vts-to-cpp
1098@mkdir -p $(dir $@)
1099@mkdir -p $(PRIVATE_HEADER_OUTPUT_DIR)
1100@echo "Generating C++ from VTS: $(PRIVATE_MODULE) <= $<"
1101$(hide) $(VTSC) -d$(basename $@).vts.P $(PRIVATE_VTS_FLAGS) \
1102    $< $(PRIVATE_HEADER_OUTPUT_DIR) $@
1103endef
1104
1105## Given a .vts file path generate the rule to compile it a .cpp file.
1106# $(1): a .vts source file
1107# $(2): a directory to place the generated .cpp files in
1108# $(3): name of a variable to add the path to the generated source file to
1109#
1110# You must call this with $(eval).
1111define define-vts-cpp-rule
1112define-vts-cpp-rule-src := $(patsubst %.vts,%$(LOCAL_CPP_EXTENSION),$(subst ../,dotdot/,$(addprefix $(2)/,$(1))))
1113$$(define-vts-cpp-rule-src) : $(LOCAL_PATH)/$(1) $(VTSC)
1114	$$(transform-vts-to-cpp)
1115$(3) += $$(define-vts-cpp-rule-src)
1116endef
1117
1118###########################################################
1119## Commands for running java-event-log-tags.py
1120###########################################################
1121
1122define transform-logtags-to-java
1123@mkdir -p $(dir $@)
1124@echo "logtags: $@ <= $<"
1125$(hide) $(JAVATAGS) -o $@ $< $(PRIVATE_MERGED_TAG)
1126endef
1127
1128
1129###########################################################
1130## Commands for running protoc to compile .proto into .java
1131###########################################################
1132
1133define transform-proto-to-java
1134@mkdir -p $(dir $@)
1135@echo "Protoc: $@ <= $(PRIVATE_PROTO_SRC_FILES)"
1136@rm -rf $(PRIVATE_PROTO_JAVA_OUTPUT_DIR)
1137@mkdir -p $(PRIVATE_PROTO_JAVA_OUTPUT_DIR)
1138$(hide) for f in $(PRIVATE_PROTO_SRC_FILES); do \
1139        $(PROTOC) \
1140        $(addprefix --proto_path=, $(PRIVATE_PROTO_INCLUDES)) \
1141        $(PRIVATE_PROTO_JAVA_OUTPUT_OPTION)="$(PRIVATE_PROTO_JAVA_OUTPUT_PARAMS):$(PRIVATE_PROTO_JAVA_OUTPUT_DIR)" \
1142        $(PRIVATE_PROTOC_FLAGS) \
1143        $$f || exit 33; \
1144        done
1145$(hide) touch $@
1146endef
1147
1148######################################################################
1149## Commands for running protoc to compile .proto into .pb.cc (or.pb.c) and .pb.h
1150######################################################################
1151define transform-proto-to-cc
1152@echo "Protoc: $@ <= $<"
1153@mkdir -p $(dir $@)
1154$(hide) $(PROTOC) \
1155	$(addprefix --proto_path=, $(PRIVATE_PROTO_INCLUDES)) \
1156	$(PRIVATE_PROTOC_FLAGS) \
1157	$<
1158@# aprotoc outputs only .cc. Rename it to .cpp if necessary.
1159$(if $(PRIVATE_RENAME_CPP_EXT),\
1160  $(hide) mv $(basename $@).cc $@)
1161endef
1162
1163
1164######################################################################
1165## Commands for generating DBus adaptors from .dbus-xml files.
1166######################################################################
1167define generate-dbus-adaptors
1168@echo "Generating DBus adaptors for $(PRIVATE_MODULE)"
1169@mkdir -p $(dir $@)
1170$(hide) $(DBUS_GENERATOR) \
1171	--service-config=$(PRIVATE_DBUS_SERVICE_CONFIG) \
1172	--adaptor=$@ \
1173	$<
1174endef
1175
1176######################################################################
1177## Commands for generating DBus proxies from .dbus-xml files.
1178######################################################################
1179define generate-dbus-proxies
1180@echo "Generating DBus proxies for $(PRIVATE_MODULE)"
1181@mkdir -p $(dir $@)
1182$(hide) $(DBUS_GENERATOR) \
1183	--service-config=$(PRIVATE_DBUS_SERVICE_CONFIG) \
1184	--proxy=$@ \
1185	$(filter %.dbus-xml,$^)
1186endef
1187
1188
1189###########################################################
1190## Commands for running gcc to compile a C++ file
1191###########################################################
1192
1193define transform-cpp-to-o-compiler-args
1194	$(addprefix -I , $(PRIVATE_C_INCLUDES)) \
1195	$$(cat $(PRIVATE_IMPORT_INCLUDES)) \
1196	$(addprefix -isystem ,\
1197	    $(if $(PRIVATE_NO_DEFAULT_COMPILER_FLAGS),, \
1198	        $(filter-out $(PRIVATE_C_INCLUDES), \
1199	            $(PRIVATE_TARGET_PROJECT_INCLUDES) \
1200	            $(PRIVATE_TARGET_C_INCLUDES)))) \
1201	-c \
1202	$(if $(PRIVATE_NO_DEFAULT_COMPILER_FLAGS),, \
1203	    $(PRIVATE_TARGET_GLOBAL_CFLAGS) \
1204	    $(PRIVATE_TARGET_GLOBAL_CPPFLAGS) \
1205	    $(PRIVATE_ARM_CFLAGS) \
1206	 ) \
1207	$(PRIVATE_RTTI_FLAG) \
1208	$(PRIVATE_CFLAGS) \
1209	$(PRIVATE_CPPFLAGS) \
1210	$(PRIVATE_DEBUG_CFLAGS) \
1211	$(PRIVATE_CFLAGS_NO_OVERRIDE) \
1212	$(PRIVATE_CPPFLAGS_NO_OVERRIDE)
1213endef
1214
1215define clang-tidy-cpp
1216$(hide) $(PATH_TO_CLANG_TIDY) $(PRIVATE_TIDY_FLAGS) \
1217  -checks=$(PRIVATE_TIDY_CHECKS) \
1218  $< -- $(transform-cpp-to-o-compiler-args)
1219endef
1220
1221ifneq (,$(filter 1 true,$(WITH_TIDY_ONLY)))
1222define transform-cpp-to-o
1223$(if $(PRIVATE_TIDY_CHECKS),
1224  @echo "target tidy $(PRIVATE_ARM_MODE) C++: $<"
1225  $(clang-tidy-cpp))
1226endef
1227else
1228define transform-cpp-to-o
1229@echo "target $(PRIVATE_ARM_MODE) C++: $(PRIVATE_MODULE) <= $<"
1230@mkdir -p $(dir $@)
1231$(if $(PRIVATE_TIDY_CHECKS),$(clang-tidy-cpp))
1232$(hide) $(RELATIVE_PWD) $(PRIVATE_CXX) \
1233  $(transform-cpp-to-o-compiler-args) \
1234  -MD -MF $(patsubst %.o,%.d,$@) -o $@ $<
1235$(hide) $(transform-d-to-p)
1236endef
1237endif
1238
1239
1240###########################################################
1241## Commands for running gcc to compile a C file
1242###########################################################
1243
1244# $(1): extra flags
1245define transform-c-or-s-to-o-compiler-args
1246	$(addprefix -I , $(PRIVATE_C_INCLUDES)) \
1247	$$(cat $(PRIVATE_IMPORT_INCLUDES)) \
1248	$(addprefix -isystem ,\
1249	    $(if $(PRIVATE_NO_DEFAULT_COMPILER_FLAGS),, \
1250	        $(filter-out $(PRIVATE_C_INCLUDES), \
1251	            $(PRIVATE_TARGET_PROJECT_INCLUDES) \
1252	            $(PRIVATE_TARGET_C_INCLUDES)))) \
1253	-c \
1254	$(if $(PRIVATE_NO_DEFAULT_COMPILER_FLAGS),, \
1255	    $(PRIVATE_TARGET_GLOBAL_CFLAGS) \
1256	    $(PRIVATE_TARGET_GLOBAL_CONLYFLAGS) \
1257	    $(PRIVATE_ARM_CFLAGS) \
1258	 ) \
1259	 $(1)
1260endef
1261
1262define transform-c-to-o-compiler-args
1263$(call transform-c-or-s-to-o-compiler-args, \
1264  $(PRIVATE_CFLAGS) \
1265  $(PRIVATE_CONLYFLAGS) \
1266  $(PRIVATE_DEBUG_CFLAGS) \
1267  $(PRIVATE_CFLAGS_NO_OVERRIDE))
1268endef
1269
1270define clang-tidy-c
1271$(hide) $(PATH_TO_CLANG_TIDY) $(PRIVATE_TIDY_FLAGS) \
1272  -checks=$(PRIVATE_TIDY_CHECKS) \
1273  $< -- $(transform-c-to-o-compiler-args)
1274endef
1275
1276ifneq (,$(filter 1 true,$(WITH_TIDY_ONLY)))
1277define transform-c-to-o
1278$(if $(PRIVATE_TIDY_CHECKS),
1279  @echo "target tidy $(PRIVATE_ARM_MODE) C: $<"
1280  $(clang-tidy-c))
1281endef
1282else
1283define transform-c-to-o
1284@echo "target $(PRIVATE_ARM_MODE) C: $(PRIVATE_MODULE) <= $<"
1285@mkdir -p $(dir $@)
1286$(if $(PRIVATE_TIDY_CHECKS),$(clang-tidy-c))
1287$(hide) $(RELATIVE_PWD) $(PRIVATE_CC) \
1288  $(transform-c-to-o-compiler-args) \
1289  -MD -MF $(patsubst %.o,%.d,$@) -o $@ $<
1290$(hide) $(transform-d-to-p)
1291endef
1292endif
1293
1294define transform-s-to-o-no-deps
1295@echo "target asm: $(PRIVATE_MODULE) <= $<"
1296@mkdir -p $(dir $@)
1297$(RELATIVE_PWD) $(PRIVATE_CC) \
1298  $(call transform-c-or-s-to-o-compiler-args, $(PRIVATE_ASFLAGS)) \
1299  -MD -MF $(patsubst %.o,%.d,$@) -o $@ $<
1300endef
1301
1302define transform-s-to-o
1303$(transform-s-to-o-no-deps)
1304$(transform-d-to-p)
1305endef
1306
1307# YASM compilation
1308define transform-asm-to-o
1309@mkdir -p $(dir $@)
1310$(hide) $(YASM) \
1311    $(addprefix -I , $(PRIVATE_C_INCLUDES)) \
1312    $($(PRIVATE_2ND_ARCH_VAR_PREFIX)TARGET_GLOBAL_YASM_FLAGS) \
1313    $(PRIVATE_ASFLAGS) \
1314    -o $@ $<
1315endef
1316
1317###########################################################
1318## Commands for running gcc to compile an Objective-C file
1319## This should never happen for target builds but this
1320## will error at build time.
1321###########################################################
1322
1323define transform-m-to-o-no-deps
1324@echo "target ObjC: $(PRIVATE_MODULE) <= $<"
1325$(call transform-c-or-s-to-o-no-deps, $(PRIVATE_CFLAGS) $(PRIVATE_DEBUG_CFLAGS))
1326endef
1327
1328define transform-m-to-o
1329$(transform-m-to-o-no-deps)
1330$(transform-d-to-p)
1331endef
1332
1333###########################################################
1334## Commands for running gcc to compile a host C++ file
1335###########################################################
1336
1337define transform-host-cpp-to-o-compiler-args
1338	$(addprefix -I , $(PRIVATE_C_INCLUDES)) \
1339	$$(cat $(PRIVATE_IMPORT_INCLUDES)) \
1340	$(addprefix -isystem ,\
1341	    $(if $(PRIVATE_NO_DEFAULT_COMPILER_FLAGS),, \
1342	        $(filter-out $(PRIVATE_C_INCLUDES), \
1343	            $($(PRIVATE_PREFIX)PROJECT_INCLUDES) \
1344	            $(PRIVATE_HOST_C_INCLUDES)))) \
1345	-c \
1346	$(if $(PRIVATE_NO_DEFAULT_COMPILER_FLAGS),, \
1347	    $(PRIVATE_HOST_GLOBAL_CFLAGS) \
1348	    $(PRIVATE_HOST_GLOBAL_CPPFLAGS) \
1349	 ) \
1350	$(PRIVATE_CFLAGS) \
1351	$(PRIVATE_CPPFLAGS) \
1352	$(PRIVATE_DEBUG_CFLAGS) \
1353	$(PRIVATE_CFLAGS_NO_OVERRIDE) \
1354	$(PRIVATE_CPPFLAGS_NO_OVERRIDE)
1355endef
1356
1357define clang-tidy-host-cpp
1358$(hide) $(PATH_TO_CLANG_TIDY) $(PRIVATE_TIDY_FLAGS) \
1359  -checks=$(PRIVATE_TIDY_CHECKS) \
1360  $< -- $(transform-host-cpp-to-o-compiler-args)
1361endef
1362
1363ifneq (,$(filter 1 true,$(WITH_TIDY_ONLY)))
1364define transform-host-cpp-to-o
1365$(if $(PRIVATE_TIDY_CHECKS),
1366  @echo "tidy $($(PRIVATE_PREFIX)DISPLAY) C++: $<"
1367  $(clang-tidy-host-cpp))
1368endef
1369else
1370define transform-host-cpp-to-o
1371@echo "$($(PRIVATE_PREFIX)DISPLAY) C++: $(PRIVATE_MODULE) <= $<"
1372@mkdir -p $(dir $@)
1373$(if $(PRIVATE_TIDY_CHECKS),$(clang-tidy-host-cpp))
1374$(hide) $(RELATIVE_PWD) $(PRIVATE_CXX) \
1375  $(transform-host-cpp-to-o-compiler-args) \
1376  -MD -MF $(patsubst %.o,%.d,$@) -o $@ $<
1377$(hide) $(transform-d-to-p)
1378endef
1379endif
1380
1381
1382###########################################################
1383## Commands for running gcc to compile a host C file
1384###########################################################
1385
1386define transform-host-c-or-s-to-o-common-args
1387	$(addprefix -I , $(PRIVATE_C_INCLUDES)) \
1388	$$(cat $(PRIVATE_IMPORT_INCLUDES)) \
1389	$(addprefix -isystem ,\
1390	    $(if $(PRIVATE_NO_DEFAULT_COMPILER_FLAGS),, \
1391	        $(filter-out $(PRIVATE_C_INCLUDES), \
1392	            $($(PRIVATE_PREFIX)PROJECT_INCLUDES) \
1393	            $(PRIVATE_HOST_C_INCLUDES)))) \
1394	-c \
1395	$(if $(PRIVATE_NO_DEFAULT_COMPILER_FLAGS),, \
1396	    $(PRIVATE_HOST_GLOBAL_CFLAGS) \
1397	    $(PRIVATE_HOST_GLOBAL_CONLYFLAGS) \
1398	 )
1399endef
1400
1401# $(1): extra flags
1402define transform-host-c-or-s-to-o-no-deps
1403@mkdir -p $(dir $@)
1404$(hide) $(RELATIVE_PWD) $(PRIVATE_CC) \
1405  $(transform-host-c-or-s-to-o-common-args) \
1406  $(1) \
1407  -MD -MF $(patsubst %.o,%.d,$@) -o $@ $<
1408endef
1409
1410define transform-host-c-to-o-compiler-args
1411  $(transform-host-c-or-s-to-o-common-args) \
1412  $(PRIVATE_CFLAGS) $(PRIVATE_CONLYFLAGS) \
1413  $(PRIVATE_DEBUG_CFLAGS) $(PRIVATE_CFLAGS_NO_OVERRIDE)
1414endef
1415
1416define clang-tidy-host-c
1417$(hide) $(PATH_TO_CLANG_TIDY) $(PRIVATE_TIDY_FLAGS) \
1418  -checks=$(PRIVATE_TIDY_CHECKS) \
1419  $< -- $(transform-host-c-to-o-compiler-args)
1420endef
1421
1422ifneq (,$(filter 1 true,$(WITH_TIDY_ONLY)))
1423define transform-host-c-to-o
1424$(if $(PRIVATE_TIDY_CHECKS),
1425  @echo "tidy $($(PRIVATE_PREFIX)DISPLAY) C: $<"
1426  $(clang-tidy-host-c))
1427endef
1428else
1429define transform-host-c-to-o
1430@echo "$($(PRIVATE_PREFIX)DISPLAY) C: $(PRIVATE_MODULE) <= $<"
1431@mkdir -p $(dir $@)
1432$(if $(PRIVATE_TIDY_CHECKS), $(clang-tidy-host-c))
1433$(hide) $(RELATIVE_PWD) $(PRIVATE_CC) \
1434  $(transform-host-c-to-o-compiler-args) \
1435  -MD -MF $(patsubst %.o,%.d,$@) -o $@ $<
1436$(hide) $(transform-d-to-p)
1437endef
1438endif
1439
1440define transform-host-s-to-o-no-deps
1441@echo "$($(PRIVATE_PREFIX)DISPLAY) asm: $(PRIVATE_MODULE) <= $<"
1442$(call transform-host-c-or-s-to-o-no-deps, $(PRIVATE_ASFLAGS))
1443endef
1444
1445define transform-host-s-to-o
1446$(transform-host-s-to-o-no-deps)
1447$(transform-d-to-p)
1448endef
1449
1450###########################################################
1451## Commands for running gcc to compile a host Objective-C file
1452###########################################################
1453
1454define transform-host-m-to-o-no-deps
1455@echo "$($(PRIVATE_PREFIX)DISPLAY) ObjC: $(PRIVATE_MODULE) <= $<"
1456$(call transform-host-c-or-s-to-o-no-deps, $(PRIVATE_CFLAGS) $(PRIVATE_DEBUG_CFLAGS) $(PRIVATE_CFLAGS_NO_OVERRIDE))
1457endef
1458
1459define transform-host-m-to-o
1460$(transform-host-m-to-o-no-deps)
1461$(transform-d-to-p)
1462endef
1463
1464###########################################################
1465## Commands for running gcc to compile a host Objective-C++ file
1466###########################################################
1467
1468define transform-host-mm-to-o
1469$(transform-host-cpp-to-o)
1470endef
1471
1472
1473###########################################################
1474## Rules to compile a single C/C++ source with ../ in the path
1475###########################################################
1476# Replace "../" in object paths with $(DOTDOT_REPLACEMENT).
1477DOTDOT_REPLACEMENT := dotdot/
1478
1479## Rule to compile a C++ source file with ../ in the path.
1480## Must be called with $(eval).
1481# $(1): the C++ source file in LOCAL_SRC_FILES.
1482# $(2): the additional dependencies.
1483# $(3): the variable name to collect the output object file.
1484define compile-dotdot-cpp-file
1485o := $(intermediates)/$(patsubst %$(LOCAL_CPP_EXTENSION),%.o,$(subst ../,$(DOTDOT_REPLACEMENT),$(1)))
1486$$(o) : $(TOPDIR)$(LOCAL_PATH)/$(1) $(2)
1487	$$(transform-$$(PRIVATE_HOST)cpp-to-o)
1488$$(call include-depfiles-for-objs, $$(o))
1489$(3) += $$(o)
1490endef
1491
1492## Rule to compile a C source file with ../ in the path.
1493## Must be called with $(eval).
1494# $(1): the C source file in LOCAL_SRC_FILES.
1495# $(2): the additional dependencies.
1496# $(3): the variable name to collect the output object file.
1497define compile-dotdot-c-file
1498o := $(intermediates)/$(patsubst %.c,%.o,$(subst ../,$(DOTDOT_REPLACEMENT),$(1)))
1499$$(o) : $(TOPDIR)$(LOCAL_PATH)/$(1) $(2)
1500	$$(transform-$$(PRIVATE_HOST)c-to-o)
1501$$(call include-depfiles-for-objs, $$(o))
1502$(3) += $$(o)
1503endef
1504
1505## Rule to compile a .S source file with ../ in the path.
1506## Must be called with $(eval).
1507# $(1): the .S source file in LOCAL_SRC_FILES.
1508# $(2): the additional dependencies.
1509# $(3): the variable name to collect the output object file.
1510define compile-dotdot-s-file
1511o := $(intermediates)/$(patsubst %.S,%.o,$(subst ../,$(DOTDOT_REPLACEMENT),$(1)))
1512$$(o) : $(TOPDIR)$(LOCAL_PATH)/$(1) $(2)
1513	$$(transform-$$(PRIVATE_HOST)s-to-o)
1514$$(call include-depfiles-for-objs, $$(o))
1515$(3) += $$(o)
1516endef
1517
1518## Rule to compile a .s source file with ../ in the path.
1519## Must be called with $(eval).
1520# $(1): the .s source file in LOCAL_SRC_FILES.
1521# $(2): the additional dependencies.
1522# $(3): the variable name to collect the output object file.
1523define compile-dotdot-s-file-no-deps
1524o := $(intermediates)/$(patsubst %.s,%.o,$(subst ../,$(DOTDOT_REPLACEMENT),$(1)))
1525$$(o) : $(TOPDIR)$(LOCAL_PATH)/$(1) $(2)
1526	$$(transform-$$(PRIVATE_HOST)s-to-o-no-deps)
1527$(3) += $$(o)
1528endef
1529
1530###########################################################
1531## Commands for running ar
1532###########################################################
1533
1534define _concat-if-arg2-not-empty
1535$(if $(2),$(hide) $(1) $(2))
1536endef
1537
1538# Split long argument list into smaller groups and call the command repeatedly
1539# Call the command at least once even if there are no arguments, as otherwise
1540# the output file won't be created.
1541#
1542# $(1): the command without arguments
1543# $(2): the arguments
1544define split-long-arguments
1545$(hide) $(1) $(wordlist 1,500,$(2))
1546$(call _concat-if-arg2-not-empty,$(1),$(wordlist 501,1000,$(2)))
1547$(call _concat-if-arg2-not-empty,$(1),$(wordlist 1001,1500,$(2)))
1548$(call _concat-if-arg2-not-empty,$(1),$(wordlist 1501,2000,$(2)))
1549$(call _concat-if-arg2-not-empty,$(1),$(wordlist 2001,2500,$(2)))
1550$(call _concat-if-arg2-not-empty,$(1),$(wordlist 2501,3000,$(2)))
1551$(call _concat-if-arg2-not-empty,$(1),$(wordlist 3001,99999,$(2)))
1552endef
1553
1554# $(1): the full path of the source static library.
1555define _extract-and-include-single-target-whole-static-lib
1556$(hide) ldir=$(PRIVATE_INTERMEDIATES_DIR)/WHOLE/$(basename $(notdir $(1)))_objs;\
1557    rm -rf $$ldir; \
1558    mkdir -p $$ldir; \
1559    cp $(1) $$ldir; \
1560    lib_to_include=$$ldir/$(notdir $(1)); \
1561    filelist=; \
1562    subdir=0; \
1563    for f in `$($(PRIVATE_2ND_ARCH_VAR_PREFIX)TARGET_AR) t $(1)`; do \
1564        if [ -e $$ldir/$$f ]; then \
1565            mkdir $$ldir/$$subdir; \
1566            ext=$$subdir/; \
1567            subdir=$$((subdir+1)); \
1568            $($(PRIVATE_2ND_ARCH_VAR_PREFIX)TARGET_AR) m $$lib_to_include $$f; \
1569        else \
1570            ext=; \
1571        fi; \
1572        $($(PRIVATE_2ND_ARCH_VAR_PREFIX)TARGET_AR) p $$lib_to_include $$f > $$ldir/$$ext$$f; \
1573        filelist="$$filelist $$ldir/$$ext$$f"; \
1574    done ; \
1575    $($(PRIVATE_2ND_ARCH_VAR_PREFIX)TARGET_AR) $($(PRIVATE_2ND_ARCH_VAR_PREFIX)TARGET_GLOBAL_ARFLAGS) \
1576        $(PRIVATE_ARFLAGS) $@ $$filelist
1577
1578endef
1579
1580# $(1): the full path of the source static library.
1581define extract-and-include-whole-static-libs-first
1582$(if $(strip $(1)),
1583$(hide) cp $(1) $@)
1584endef
1585
1586define extract-and-include-target-whole-static-libs
1587$(call extract-and-include-whole-static-libs-first, $(firstword $(PRIVATE_ALL_WHOLE_STATIC_LIBRARIES)))
1588$(foreach lib,$(wordlist 2,999,$(PRIVATE_ALL_WHOLE_STATIC_LIBRARIES)), \
1589    $(call _extract-and-include-single-target-whole-static-lib, $(lib)))
1590endef
1591
1592# Explicitly delete the archive first so that ar doesn't
1593# try to add to an existing archive.
1594define transform-o-to-static-lib
1595@echo "target StaticLib: $(PRIVATE_MODULE) ($@)"
1596@mkdir -p $(dir $@)
1597@rm -f $@
1598$(extract-and-include-target-whole-static-libs)
1599$(call split-long-arguments,$($(PRIVATE_2ND_ARCH_VAR_PREFIX)TARGET_AR) \
1600    $($(PRIVATE_2ND_ARCH_VAR_PREFIX)TARGET_GLOBAL_ARFLAGS) \
1601    $(PRIVATE_ARFLAGS) $@,$(PRIVATE_ALL_OBJECTS))
1602endef
1603
1604###########################################################
1605## Commands for running host ar
1606###########################################################
1607
1608# $(1): the full path of the source static library.
1609define _extract-and-include-single-host-whole-static-lib
1610$(hide) ldir=$(PRIVATE_INTERMEDIATES_DIR)/WHOLE/$(basename $(notdir $(1)))_objs;\
1611    rm -rf $$ldir; \
1612    mkdir -p $$ldir; \
1613    cp $(1) $$ldir; \
1614    lib_to_include=$$ldir/$(notdir $(1)); \
1615    filelist=; \
1616    subdir=0; \
1617    for f in `$($(PRIVATE_2ND_ARCH_VAR_PREFIX)$(PRIVATE_PREFIX)AR) t $(1) | \grep '\.o$$'`; do \
1618        if [ -e $$ldir/$$f ]; then \
1619           mkdir $$ldir/$$subdir; \
1620           ext=$$subdir/; \
1621           subdir=$$((subdir+1)); \
1622           $($(PRIVATE_2ND_ARCH_VAR_PREFIX)$(PRIVATE_PREFIX)AR) m $$lib_to_include $$f; \
1623        else \
1624           ext=; \
1625        fi; \
1626        $($(PRIVATE_2ND_ARCH_VAR_PREFIX)$(PRIVATE_PREFIX)AR) p $$lib_to_include $$f > $$ldir/$$ext$$f; \
1627        filelist="$$filelist $$ldir/$$ext$$f"; \
1628    done ; \
1629    $($(PRIVATE_2ND_ARCH_VAR_PREFIX)$(PRIVATE_PREFIX)AR) $($(PRIVATE_2ND_ARCH_VAR_PREFIX)$(PRIVATE_PREFIX)GLOBAL_ARFLAGS) \
1630        $(PRIVATE_ARFLAGS) $@ $$filelist
1631
1632endef
1633
1634define extract-and-include-host-whole-static-libs
1635$(call extract-and-include-whole-static-libs-first, $(firstword $(PRIVATE_ALL_WHOLE_STATIC_LIBRARIES)))
1636$(foreach lib,$(wordlist 2,999,$(PRIVATE_ALL_WHOLE_STATIC_LIBRARIES)), \
1637    $(call _extract-and-include-single-host-whole-static-lib, $(lib)))
1638endef
1639
1640ifeq ($(HOST_OS),darwin)
1641# On Darwin the host ar fails if there is nothing to add to .a at all.
1642# We work around by adding a dummy.o and then deleting it.
1643define create-dummy.o-if-no-objs
1644$(if $(PRIVATE_ALL_OBJECTS),,$(hide) touch $(dir $@)dummy.o)
1645endef
1646
1647define get-dummy.o-if-no-objs
1648$(if $(PRIVATE_ALL_OBJECTS),,$(dir $@)dummy.o)
1649endef
1650
1651define delete-dummy.o-if-no-objs
1652$(if $(PRIVATE_ALL_OBJECTS),,$(hide) $($(PRIVATE_2ND_ARCH_VAR_PREFIX)$(PRIVATE_PREFIX)AR) d $@ $(dir $@)dummy.o \
1653  && rm -f $(dir $@)dummy.o)
1654endef
1655endif  # HOST_OS is darwin
1656
1657# Explicitly delete the archive first so that ar doesn't
1658# try to add to an existing archive.
1659define transform-host-o-to-static-lib
1660@echo "$($(PRIVATE_PREFIX)DISPLAY) StaticLib: $(PRIVATE_MODULE) ($@)"
1661@mkdir -p $(dir $@)
1662@rm -f $@
1663$(extract-and-include-host-whole-static-libs)
1664$(create-dummy.o-if-no-objs)
1665$(call split-long-arguments,$($(PRIVATE_2ND_ARCH_VAR_PREFIX)$(PRIVATE_PREFIX)AR) \
1666    $($(PRIVATE_2ND_ARCH_VAR_PREFIX)$(PRIVATE_PREFIX)GLOBAL_ARFLAGS) \
1667    $(PRIVATE_ARFLAGS) $@,$(PRIVATE_ALL_OBJECTS) $(get-dummy.o-if-no-objs))
1668$(delete-dummy.o-if-no-objs)
1669endef
1670
1671
1672###########################################################
1673## Commands for running gcc to link a shared library or package
1674###########################################################
1675
1676# ld just seems to be so finicky with command order that we allow
1677# it to be overriden en-masse see combo/linux-arm.make for an example.
1678ifneq ($(HOST_CUSTOM_LD_COMMAND),true)
1679define transform-host-o-to-shared-lib-inner
1680$(hide) $(PRIVATE_CXX) \
1681	-Wl,-rpath-link=$($(PRIVATE_2ND_ARCH_VAR_PREFIX)$(PRIVATE_PREFIX)OUT_INTERMEDIATE_LIBRARIES) \
1682	-Wl,-rpath,\$$ORIGIN/../$(notdir $($(PRIVATE_2ND_ARCH_VAR_PREFIX)$(PRIVATE_PREFIX)OUT_SHARED_LIBRARIES)) \
1683	-Wl,-rpath,\$$ORIGIN/$(notdir $($(PRIVATE_2ND_ARCH_VAR_PREFIX)$(PRIVATE_PREFIX)OUT_SHARED_LIBRARIES)) \
1684	-shared -Wl,-soname,$(notdir $@) \
1685	$($(PRIVATE_2ND_ARCH_VAR_PREFIX)$(PRIVATE_PREFIX)GLOBAL_LD_DIRS) \
1686	$(if $(PRIVATE_NO_DEFAULT_COMPILER_FLAGS),, \
1687	   $(PRIVATE_HOST_GLOBAL_LDFLAGS) \
1688	) \
1689	$(PRIVATE_LDFLAGS) \
1690	$(PRIVATE_ALL_OBJECTS) \
1691	-Wl,--whole-archive \
1692	$(call normalize-host-libraries,$(PRIVATE_ALL_WHOLE_STATIC_LIBRARIES)) \
1693	-Wl,--no-whole-archive \
1694	$(if $(PRIVATE_GROUP_STATIC_LIBRARIES),-Wl$(comma)--start-group) \
1695	$(call normalize-host-libraries,$(PRIVATE_ALL_STATIC_LIBRARIES)) \
1696	$(if $(PRIVATE_GROUP_STATIC_LIBRARIES),-Wl$(comma)--end-group) \
1697	$(if $(filter true,$(NATIVE_COVERAGE)),-lgcov) \
1698	$(if $(filter true,$(NATIVE_COVERAGE)),$(PRIVATE_HOST_LIBPROFILE_RT)) \
1699	$(call normalize-host-libraries,$(PRIVATE_ALL_SHARED_LIBRARIES)) \
1700	-o $@ \
1701	$(PRIVATE_LDLIBS)
1702endef
1703endif
1704
1705define transform-host-o-to-shared-lib
1706@echo "$($(PRIVATE_PREFIX)DISPLAY) SharedLib: $(PRIVATE_MODULE) ($@)"
1707@mkdir -p $(dir $@)
1708$(transform-host-o-to-shared-lib-inner)
1709endef
1710
1711define transform-host-o-to-package
1712@echo "$($(PRIVATE_PREFIX)DISPLAY) Package: $(PRIVATE_MODULE) ($@)"
1713@mkdir -p $(dir $@)
1714$(transform-host-o-to-shared-lib-inner)
1715endef
1716
1717
1718###########################################################
1719## Commands for running gcc to link a shared library or package
1720###########################################################
1721
1722define transform-o-to-shared-lib-inner
1723$(hide) $(PRIVATE_CXX) \
1724	-nostdlib -Wl,-soname,$(notdir $@) \
1725	-Wl,--gc-sections \
1726	$(if $(filter true,$(PRIVATE_CLANG)),-shared,-Wl$(comma)-shared) \
1727	$(PRIVATE_TARGET_GLOBAL_LD_DIRS) \
1728	$(PRIVATE_TARGET_CRTBEGIN_SO_O) \
1729	$(PRIVATE_ALL_OBJECTS) \
1730	-Wl,--whole-archive \
1731	$(call normalize-target-libraries,$(PRIVATE_ALL_WHOLE_STATIC_LIBRARIES)) \
1732	-Wl,--no-whole-archive \
1733	$(if $(PRIVATE_GROUP_STATIC_LIBRARIES),-Wl$(comma)--start-group) \
1734	$(call normalize-target-libraries,$(PRIVATE_ALL_STATIC_LIBRARIES)) \
1735	$(if $(PRIVATE_GROUP_STATIC_LIBRARIES),-Wl$(comma)--end-group) \
1736	$(if $(filter true,$(NATIVE_COVERAGE)),$(PRIVATE_TARGET_COVERAGE_LIB)) \
1737	$(PRIVATE_TARGET_LIBATOMIC) \
1738	$(PRIVATE_TARGET_LIBGCC) \
1739	$(PRIVATE_TARGET_GLOBAL_LDFLAGS) \
1740	$(PRIVATE_LDFLAGS) \
1741	$(call normalize-target-libraries,$(PRIVATE_ALL_SHARED_LIBRARIES)) \
1742	-o $@ \
1743	$(PRIVATE_TARGET_CRTEND_SO_O) \
1744	$(PRIVATE_LDLIBS)
1745endef
1746
1747define transform-o-to-shared-lib
1748@echo "target SharedLib: $(PRIVATE_MODULE) ($@)"
1749@mkdir -p $(dir $@)
1750$(transform-o-to-shared-lib-inner)
1751endef
1752
1753###########################################################
1754## Commands for filtering a target executable or library
1755###########################################################
1756
1757ifneq ($(TARGET_BUILD_VARIANT),user)
1758  TARGET_STRIP_EXTRA = && $(PRIVATE_OBJCOPY) --add-gnu-debuglink=$< $@
1759  TARGET_STRIP_KEEP_SYMBOLS_EXTRA = --add-gnu-debuglink=$<
1760endif
1761
1762define transform-to-stripped
1763@echo "target Strip: $(PRIVATE_MODULE) ($@)"
1764@mkdir -p $(dir $@)
1765$(hide) $(PRIVATE_STRIP) --strip-all $< -o $@ \
1766  $(if $(PRIVATE_NO_DEBUGLINK),,$(TARGET_STRIP_EXTRA))
1767endef
1768
1769define transform-to-stripped-keep-mini-debug-info
1770@echo "target Strip (mini debug info): $(PRIVATE_MODULE) ($@)"
1771@mkdir -p $(dir $@)
1772$(hide) $(PRIVATE_NM) -D $< --format=posix --defined-only | awk '{ print $$1 }' | sort >$@.dynsyms
1773$(hide) $(PRIVATE_NM) $< --format=posix --defined-only | awk '{ if ($$2 == "T" || $$2 == "t" || $$2 == "D") print $$1 }' | sort >$@.funcsyms
1774$(hide) comm -13 $@.dynsyms $@.funcsyms >$@.keep_symbols
1775$(hide) $(PRIVATE_OBJCOPY) --only-keep-debug $< $@.debug
1776$(hide) $(PRIVATE_OBJCOPY) --rename-section .debug_frame=saved_debug_frame $@.debug $@.mini_debuginfo
1777$(hide) $(PRIVATE_OBJCOPY) -S --remove-section .gdb_index --remove-section .comment --keep-symbols=$@.keep_symbols $@.mini_debuginfo
1778$(hide) $(PRIVATE_OBJCOPY) --rename-section saved_debug_frame=.debug_frame $@.mini_debuginfo
1779$(hide) $(PRIVATE_STRIP) --strip-all -R .comment $< -o $@
1780$(hide) rm -f $@.mini_debuginfo.xz
1781$(hide) xz $@.mini_debuginfo
1782$(hide) $(PRIVATE_OBJCOPY) --add-section .gnu_debugdata=$@.mini_debuginfo.xz $@
1783$(hide) rm -f $@.dynsyms $@.funcsyms $@.keep_symbols $@.debug $@.mini_debuginfo.xz
1784endef
1785
1786define transform-to-stripped-keep-symbols
1787@echo "target Strip (keep symbols): $(PRIVATE_MODULE) ($@)"
1788@mkdir -p $(dir $@)
1789$(hide) $(PRIVATE_OBJCOPY) \
1790    `$(PRIVATE_READELF) -S $< | awk '/.debug_/ {print "-R " $$2}' | xargs` \
1791    $(TARGET_STRIP_KEEP_SYMBOLS_EXTRA) $< $@
1792endef
1793
1794###########################################################
1795## Commands for packing a target executable or library
1796###########################################################
1797
1798define pack-elf-relocations
1799@echo "target Pack Relocations: $(PRIVATE_MODULE) ($@)"
1800$(copy-file-to-target)
1801$(hide) $(RELOCATION_PACKER) $@
1802endef
1803
1804###########################################################
1805## Commands for running gcc to link an executable
1806###########################################################
1807
1808define transform-o-to-executable-inner
1809$(hide) $(PRIVATE_CXX) -pie \
1810	-nostdlib -Bdynamic \
1811	-Wl,-dynamic-linker,$(PRIVATE_LINKER) \
1812	-Wl,--gc-sections \
1813	-Wl,-z,nocopyreloc \
1814	$(PRIVATE_TARGET_GLOBAL_LD_DIRS) \
1815	-Wl,-rpath-link=$(PRIVATE_TARGET_OUT_INTERMEDIATE_LIBRARIES) \
1816	$(PRIVATE_TARGET_CRTBEGIN_DYNAMIC_O) \
1817	$(PRIVATE_ALL_OBJECTS) \
1818	-Wl,--whole-archive \
1819	$(call normalize-target-libraries,$(PRIVATE_ALL_WHOLE_STATIC_LIBRARIES)) \
1820	-Wl,--no-whole-archive \
1821	$(if $(PRIVATE_GROUP_STATIC_LIBRARIES),-Wl$(comma)--start-group) \
1822	$(call normalize-target-libraries,$(PRIVATE_ALL_STATIC_LIBRARIES)) \
1823	$(if $(PRIVATE_GROUP_STATIC_LIBRARIES),-Wl$(comma)--end-group) \
1824	$(if $(filter true,$(NATIVE_COVERAGE)),$(PRIVATE_TARGET_COVERAGE_LIB)) \
1825	$(PRIVATE_TARGET_LIBATOMIC) \
1826	$(PRIVATE_TARGET_LIBGCC) \
1827	$(PRIVATE_TARGET_GLOBAL_LDFLAGS) \
1828	$(PRIVATE_LDFLAGS) \
1829	$(call normalize-target-libraries,$(PRIVATE_ALL_SHARED_LIBRARIES)) \
1830	-o $@ \
1831	$(PRIVATE_TARGET_CRTEND_O) \
1832	$(PRIVATE_LDLIBS)
1833endef
1834
1835define transform-o-to-executable
1836@echo "target Executable: $(PRIVATE_MODULE) ($@)"
1837@mkdir -p $(dir $@)
1838$(transform-o-to-executable-inner)
1839endef
1840
1841
1842###########################################################
1843## Commands for linking a static executable. In practice,
1844## we only use this on arm, so the other platforms don't
1845## have transform-o-to-static-executable defined.
1846## Clang driver needs -static to create static executable.
1847## However, bionic/linker uses -shared to overwrite.
1848## Linker for x86 targets does not allow coexistance of -static and -shared,
1849## so we add -static only if -shared is not used.
1850###########################################################
1851
1852define transform-o-to-static-executable-inner
1853$(hide) $(PRIVATE_CXX) \
1854	-nostdlib -Bstatic \
1855	$(if $(filter $(PRIVATE_LDFLAGS),-shared),,-static) \
1856	-Wl,--gc-sections \
1857	-o $@ \
1858	$(PRIVATE_TARGET_GLOBAL_LD_DIRS) \
1859	$(PRIVATE_TARGET_CRTBEGIN_STATIC_O) \
1860	$(PRIVATE_TARGET_GLOBAL_LDFLAGS) \
1861	$(PRIVATE_LDFLAGS) \
1862	$(PRIVATE_ALL_OBJECTS) \
1863	-Wl,--whole-archive \
1864	$(call normalize-target-libraries,$(PRIVATE_ALL_WHOLE_STATIC_LIBRARIES)) \
1865	-Wl,--no-whole-archive \
1866	$(call normalize-target-libraries,$(filter-out %libcompiler_rt.a,$(filter-out %libc_nomalloc.a,$(filter-out %libc.a,$(PRIVATE_ALL_STATIC_LIBRARIES))))) \
1867	-Wl,--start-group \
1868	$(call normalize-target-libraries,$(filter %libc.a,$(PRIVATE_ALL_STATIC_LIBRARIES))) \
1869	$(call normalize-target-libraries,$(filter %libc_nomalloc.a,$(PRIVATE_ALL_STATIC_LIBRARIES))) \
1870	$(if $(filter true,$(NATIVE_COVERAGE)),$(PRIVATE_TARGET_COVERAGE_LIB)) \
1871	$(PRIVATE_TARGET_LIBATOMIC) \
1872	$(call normalize-target-libraries,$(filter %libcompiler_rt.a,$(PRIVATE_ALL_STATIC_LIBRARIES))) \
1873	$(PRIVATE_TARGET_LIBGCC) \
1874	-Wl,--end-group \
1875	$(PRIVATE_TARGET_CRTEND_O)
1876endef
1877
1878define transform-o-to-static-executable
1879@echo "target StaticExecutable: $(PRIVATE_MODULE) ($@)"
1880@mkdir -p $(dir $@)
1881$(transform-o-to-static-executable-inner)
1882endef
1883
1884
1885###########################################################
1886## Commands for running gcc to link a host executable
1887###########################################################
1888ifdef BUILD_HOST_static
1889HOST_FPIE_FLAGS :=
1890else
1891HOST_FPIE_FLAGS := -pie
1892# Force the correct entry point to workaround a bug in binutils that manifests with -pie
1893ifeq ($(HOST_CROSS_OS),windows)
1894HOST_CROSS_FPIE_FLAGS += -Wl,-e_mainCRTStartup
1895endif
1896endif
1897
1898ifneq ($(HOST_CUSTOM_LD_COMMAND),true)
1899define transform-host-o-to-executable-inner
1900$(hide) $(PRIVATE_CXX) \
1901	$(PRIVATE_ALL_OBJECTS) \
1902	-Wl,--whole-archive \
1903	$(call normalize-host-libraries,$(PRIVATE_ALL_WHOLE_STATIC_LIBRARIES)) \
1904	-Wl,--no-whole-archive \
1905	$(if $(PRIVATE_GROUP_STATIC_LIBRARIES),-Wl$(comma)--start-group) \
1906	$(call normalize-host-libraries,$(PRIVATE_ALL_STATIC_LIBRARIES)) \
1907	$(if $(PRIVATE_GROUP_STATIC_LIBRARIES),-Wl$(comma)--end-group) \
1908	$(if $(filter true,$(NATIVE_COVERAGE)),-lgcov) \
1909	$(if $(filter true,$(NATIVE_COVERAGE)),$(PRIVATE_HOST_LIBPROFILE_RT)) \
1910	$(call normalize-host-libraries,$(PRIVATE_ALL_SHARED_LIBRARIES)) \
1911	-Wl,-rpath-link=$($(PRIVATE_2ND_ARCH_VAR_PREFIX)$(PRIVATE_PREFIX)OUT_INTERMEDIATE_LIBRARIES) \
1912	-Wl,-rpath,\$$ORIGIN/../$(notdir $($(PRIVATE_2ND_ARCH_VAR_PREFIX)$(PRIVATE_PREFIX)OUT_SHARED_LIBRARIES)) \
1913	-Wl,-rpath,\$$ORIGIN/$(notdir $($(PRIVATE_2ND_ARCH_VAR_PREFIX)$(PRIVATE_PREFIX)OUT_SHARED_LIBRARIES)) \
1914	$($(PRIVATE_2ND_ARCH_VAR_PREFIX)$(PRIVATE_PREFIX)GLOBAL_LD_DIRS) \
1915	$(if $(PRIVATE_NO_DEFAULT_COMPILER_FLAGS),, \
1916		$(PRIVATE_HOST_GLOBAL_LDFLAGS) \
1917	) \
1918	$(PRIVATE_LDFLAGS) \
1919	-o $@ \
1920	$(PRIVATE_LDLIBS)
1921endef
1922endif
1923
1924define transform-host-o-to-executable
1925@echo "$($(PRIVATE_PREFIX)DISPLAY) Executable: $(PRIVATE_MODULE) ($@)"
1926@mkdir -p $(dir $@)
1927$(transform-host-o-to-executable-inner)
1928endef
1929
1930
1931###########################################################
1932## Commands for running javac to make .class files
1933###########################################################
1934
1935# Add BUILD_NUMBER to apps default version name if it's unbundled build.
1936ifdef TARGET_BUILD_APPS
1937APPS_DEFAULT_VERSION_NAME := $(PLATFORM_VERSION)-$(BUILD_NUMBER_FROM_FILE)
1938else
1939APPS_DEFAULT_VERSION_NAME := $(PLATFORM_VERSION)
1940endif
1941
1942# TODO: Right now we generate the asset resources twice, first as part
1943# of generating the Java classes, then at the end when packaging the final
1944# assets.  This should be changed to do one of two things: (1) Don't generate
1945# any resource files the first time, only create classes during that stage;
1946# or (2) Don't use the -c flag with the second stage, instead taking the
1947# resource files from the first stage as additional input.  My original intent
1948# was to use approach (2), but this requires a little more work in the tool.
1949# Maybe we should just use approach (1).
1950
1951# This rule creates the R.java and Manifest.java files, both of which
1952# are PRODUCT-neutral.  Don't pass PRIVATE_PRODUCT_AAPT_CONFIG to this invocation.
1953define create-resource-java-files
1954@mkdir -p $(PRIVATE_SOURCE_INTERMEDIATES_DIR)
1955@mkdir -p $(dir $(PRIVATE_RESOURCE_PUBLICS_OUTPUT))
1956$(hide) $(AAPT) package $(PRIVATE_AAPT_FLAGS) -m \
1957    $(eval # PRIVATE_PRODUCT_AAPT_CONFIG is intentionally missing-- see comment.) \
1958    $(addprefix -J , $(PRIVATE_SOURCE_INTERMEDIATES_DIR)) \
1959    $(addprefix -M , $(PRIVATE_ANDROID_MANIFEST)) \
1960    $(addprefix -P , $(PRIVATE_RESOURCE_PUBLICS_OUTPUT)) \
1961    $(addprefix -S , $(PRIVATE_RESOURCE_DIR)) \
1962    $(addprefix -A , $(PRIVATE_ASSET_DIR)) \
1963    $(addprefix -I , $(PRIVATE_AAPT_INCLUDES)) \
1964    $(addprefix -G , $(PRIVATE_PROGUARD_OPTIONS_FILE)) \
1965    $(addprefix --min-sdk-version , $(PRIVATE_DEFAULT_APP_TARGET_SDK)) \
1966    $(addprefix --target-sdk-version , $(PRIVATE_DEFAULT_APP_TARGET_SDK)) \
1967    $(if $(filter --version-code,$(PRIVATE_AAPT_FLAGS)),,--version-code $(PLATFORM_SDK_VERSION)) \
1968    $(if $(filter --version-name,$(PRIVATE_AAPT_FLAGS)),,--version-name $(APPS_DEFAULT_VERSION_NAME)) \
1969    $(addprefix --rename-manifest-package , $(PRIVATE_MANIFEST_PACKAGE_NAME)) \
1970    $(addprefix --rename-instrumentation-target-package , $(PRIVATE_MANIFEST_INSTRUMENTATION_FOR)) \
1971    --skip-symbols-without-default-localization
1972endef
1973
1974# Search for generated R.java/Manifest.java, copy the found R.java as $@.
1975# Also copy them to a central 'R' directory to make it easier to add the files to an IDE.
1976define find-generated-R.java
1977$(hide) for GENERATED_MANIFEST_FILE in `find $(PRIVATE_SOURCE_INTERMEDIATES_DIR) \
1978  -name Manifest.java 2> /dev/null`; do \
1979    dir=`awk '/package/{gsub(/\./,"/",$$2);gsub(/;/,"",$$2);print $$2;exit}' $$GENERATED_MANIFEST_FILE`; \
1980    mkdir -p $(TARGET_COMMON_OUT_ROOT)/R/$$dir; \
1981    $(ACP) -fp $$GENERATED_MANIFEST_FILE $(TARGET_COMMON_OUT_ROOT)/R/$$dir; \
1982  done;
1983$(hide) for GENERATED_R_FILE in `find $(PRIVATE_SOURCE_INTERMEDIATES_DIR) \
1984  -name R.java 2> /dev/null`; do \
1985    dir=`awk '/package/{gsub(/\./,"/",$$2);gsub(/;/,"",$$2);print $$2;exit}' $$GENERATED_R_FILE`; \
1986    mkdir -p $(TARGET_COMMON_OUT_ROOT)/R/$$dir; \
1987    $(ACP) -fp $$GENERATED_R_FILE $(TARGET_COMMON_OUT_ROOT)/R/$$dir \
1988      || exit 31; \
1989    $(ACP) -fp $$GENERATED_R_FILE $@ || exit 32; \
1990  done;
1991@# Ensure that the target file is always created, i.e. also in case we did not
1992@# enter the GENERATED_R_FILE-loop above. This avoids unnecessary rebuilding.
1993$(hide) touch $@
1994endef
1995
1996###########################################################
1997# AAPT2 compilation and link
1998###########################################################
1999define aapt2-compile-one-resource-file
2000@mkdir -p $(dir $@)
2001$(hide) $(AAPT2) compile -o $(dir $@) $(PRIVATE_AAPT2_CFLAGS) --legacy $<
2002endef
2003
2004define aapt2-compile-resource-dirs
2005@mkdir -p $(dir $@)
2006$(hide) $(AAPT2) compile -o $@ $(addprefix --dir ,$(PRIVATE_SOURCE_RES_DIRS)) \
2007  $(PRIVATE_AAPT2_CFLAGS) --legacy
2008endef
2009
2010# Set up rule to compile one resource file with aapt2.
2011# Must be called with $(eval).
2012# $(1): the source file
2013# $(2): the output file
2014define aapt2-compile-one-resource-file-rule
2015$(2) : $(1) $(AAPT2)
2016	@echo "AAPT2 compile $$@ <- $$<"
2017	$$(call aapt2-compile-one-resource-file)
2018endef
2019
2020# Convert input resource file path to output file path.
2021# values-[config]/<file>.xml -> values-[config]_<file>.arsc.flat;
2022# For other resource file, just replace the last "/" with "_" and
2023# add .flat extension.
2024#
2025# $(1): the input resource file path
2026# $(2): the base dir of the output file path
2027# Returns: the compiled output file path
2028define aapt2-compiled-resource-out-file
2029$(eval _p_w := $(strip $(subst /,$(space),$(dir $(1)))))$(2)/$(subst $(space),/,$(_p_w))_$(if $(filter values%,$(lastword $(_p_w))),$(patsubst %.xml,%.arsc,$(notdir $(1))),$(notdir $(1))).flat
2030endef
2031
2032define aapt2-link
2033@mkdir -p $(dir $@)
2034$(call dump-words-to-file,$(PRIVATE_RES_FLAT),$(dir $@)aapt2-flat-list)
2035$(hide) $(AAPT2) link -o $@ \
2036  $(PRIVATE_AAPT_FLAGS) \
2037  $(addprefix --manifest ,$(PRIVATE_ANDROID_MANIFEST)) \
2038  $(addprefix -I ,$(PRIVATE_AAPT_INCLUDES)) \
2039  $(addprefix -I ,$(PRIVATE_SHARED_ANDROID_LIBRARIES)) \
2040  $(addprefix --java ,$(PRIVATE_SOURCE_INTERMEDIATES_DIR)) \
2041  $(addprefix --proguard ,$(PRIVATE_PROGUARD_OPTIONS_FILE)) \
2042  $(addprefix --min-sdk-version ,$(PRIVATE_DEFAULT_APP_TARGET_SDK)) \
2043  $(addprefix --target-sdk-version ,$(PRIVATE_DEFAULT_APP_TARGET_SDK)) \
2044  $(if $(filter --product,$(PRIVATE_AAPT_FLAGS)),,$(addprefix --product ,$(PRIVATE_TARGET_AAPT_CHARACTERISTICS))) \
2045  $(addprefix -c ,$(PRIVATE_PRODUCT_AAPT_CONFIG)) \
2046  $(addprefix --preferred-density ,$(PRIVATE_PRODUCT_AAPT_PREF_CONFIG)) \
2047  $(if $(filter --version-code,$(PRIVATE_AAPT_FLAGS)),,--version-code $(PLATFORM_SDK_VERSION)) \
2048  $(if $(filter --version-name,$(PRIVATE_AAPT_FLAGS)),,--version-name $(APPS_DEFAULT_VERSION_NAME)) \
2049  $(addprefix --rename-manifest-package ,$(PRIVATE_MANIFEST_PACKAGE_NAME)) \
2050  $(addprefix --rename-instrumentation-target-package ,$(PRIVATE_MANIFEST_INSTRUMENTATION_FOR)) \
2051  $(addprefix -R , $(PRIVATE_OVERLAY_FLAT)) \
2052  \@$(dir $@)aapt2-flat-list
2053endef
2054
2055###########################################################
2056xlint_unchecked := -Xlint:unchecked
2057
2058# emit-line, <word list>, <output file>
2059define emit-line
2060   $(if $(1),echo -n '$(strip $(1)) ' >> $(2))
2061endef
2062
2063# dump-words-to-file, <word list>, <output file>
2064define dump-words-to-file
2065        @rm -f $(2)
2066        @touch $(2)
2067        @$(call emit-line,$(wordlist 1,500,$(1)),$(2))
2068        @$(call emit-line,$(wordlist 501,1000,$(1)),$(2))
2069        @$(call emit-line,$(wordlist 1001,1500,$(1)),$(2))
2070        @$(call emit-line,$(wordlist 1501,2000,$(1)),$(2))
2071        @$(call emit-line,$(wordlist 2001,2500,$(1)),$(2))
2072        @$(call emit-line,$(wordlist 2501,3000,$(1)),$(2))
2073        @$(call emit-line,$(wordlist 3001,3500,$(1)),$(2))
2074        @$(call emit-line,$(wordlist 3501,4000,$(1)),$(2))
2075        @$(call emit-line,$(wordlist 4001,4500,$(1)),$(2))
2076        @$(call emit-line,$(wordlist 4501,5000,$(1)),$(2))
2077        @$(call emit-line,$(wordlist 5001,5500,$(1)),$(2))
2078        @$(call emit-line,$(wordlist 5501,6000,$(1)),$(2))
2079        @$(call emit-line,$(wordlist 6001,6500,$(1)),$(2))
2080        @$(call emit-line,$(wordlist 6501,7000,$(1)),$(2))
2081        @$(call emit-line,$(wordlist 7001,7500,$(1)),$(2))
2082        @$(call emit-line,$(wordlist 7501,8000,$(1)),$(2))
2083        @$(call emit-line,$(wordlist 8001,8500,$(1)),$(2))
2084        @$(call emit-line,$(wordlist 8501,9000,$(1)),$(2))
2085        @$(call emit-line,$(wordlist 9001,9500,$(1)),$(2))
2086        @$(call emit-line,$(wordlist 9501,10000,$(1)),$(2))
2087        @$(call emit-line,$(wordlist 10001,10500,$(1)),$(2))
2088        @$(call emit-line,$(wordlist 10501,11000,$(1)),$(2))
2089        @$(call emit-line,$(wordlist 11001,11500,$(1)),$(2))
2090        @$(call emit-line,$(wordlist 11501,12000,$(1)),$(2))
2091        @$(call emit-line,$(wordlist 12001,12500,$(1)),$(2))
2092        @$(call emit-line,$(wordlist 12501,13000,$(1)),$(2))
2093        @$(call emit-line,$(wordlist 13001,13500,$(1)),$(2))
2094        @$(if $(wordlist 13501,13502,$(1)),$(error Too many words ($(words $(1)))))
2095endef
2096
2097# For a list of jar files, unzip them to a specified directory,
2098# but make sure that no META-INF files come along for the ride,
2099# unless PRIVATE_DONT_DELETE_JAR_META_INF is set.
2100#
2101# $(1): files to unzip
2102# $(2): destination directory
2103define unzip-jar-files
2104  $(hide) for f in $(1); \
2105  do \
2106    if [ ! -f $$f ]; then \
2107      echo Missing file $$f; \
2108      exit 1; \
2109    fi; \
2110    unzip -qo $$f -d $(2); \
2111  done
2112  $(if $(PRIVATE_DONT_DELETE_JAR_META_INF),,$(hide) rm -rf $(2)/META-INF)
2113endef
2114
2115# Call jack
2116#
2117define call-jack
2118 JACK_VERSION=$(PRIVATE_JACK_VERSION) $(JACK) $(DEFAULT_JACK_EXTRA_ARGS)
2119endef
2120
2121# Common definition to invoke javac on the host and target.
2122#
2123# Some historical notes:
2124# - below we write the list of java files to java-source-list to avoid argument
2125#   list length problems with Cygwin
2126# - we filter out duplicate java file names because eclipse's compiler
2127#   doesn't like them.
2128#
2129# $(1): javac
2130# $(2): bootclasspath
2131define compile-java
2132$(hide) rm -f $@
2133$(hide) rm -rf $(PRIVATE_CLASS_INTERMEDIATES_DIR)
2134$(hide) mkdir -p $(dir $@)
2135$(hide) mkdir -p $(PRIVATE_CLASS_INTERMEDIATES_DIR)
2136$(call unzip-jar-files,$(PRIVATE_STATIC_JAVA_LIBRARIES),$(PRIVATE_CLASS_INTERMEDIATES_DIR))
2137$(call dump-words-to-file,$(PRIVATE_JAVA_SOURCES),$(PRIVATE_CLASS_INTERMEDIATES_DIR)/java-source-list)
2138$(hide) if [ -d "$(PRIVATE_SOURCE_INTERMEDIATES_DIR)" ]; then \
2139          find $(PRIVATE_SOURCE_INTERMEDIATES_DIR) -name '*.java' -and -not -name '.*' >> $(PRIVATE_CLASS_INTERMEDIATES_DIR)/java-source-list; \
2140fi
2141$(hide) tr ' ' '\n' < $(PRIVATE_CLASS_INTERMEDIATES_DIR)/java-source-list \
2142    | $(NORMALIZE_PATH) | sort -u > $(PRIVATE_CLASS_INTERMEDIATES_DIR)/java-source-list-uniq
2143$(hide) if [ -s $(PRIVATE_CLASS_INTERMEDIATES_DIR)/java-source-list-uniq ] ; then \
2144    $(1) -encoding UTF-8 \
2145    $(if $(findstring true,$(PRIVATE_WARNINGS_ENABLE)),$(xlint_unchecked),) \
2146    $(2) \
2147    $(addprefix -classpath ,$(strip \
2148        $(call normalize-path-list,$(PRIVATE_ALL_JAVA_LIBRARIES)))) \
2149    $(if $(findstring true,$(PRIVATE_WARNINGS_ENABLE)),$(xlint_unchecked),) \
2150    -extdirs "" -d $(PRIVATE_CLASS_INTERMEDIATES_DIR) \
2151    $(PRIVATE_JAVACFLAGS) \
2152    \@$(PRIVATE_CLASS_INTERMEDIATES_DIR)/java-source-list-uniq \
2153    || ( rm -rf $(PRIVATE_CLASS_INTERMEDIATES_DIR) ; exit 41 ) \
2154fi
2155$(if $(PRIVATE_JAVA_LAYERS_FILE), $(hide) build/tools/java-layers.py \
2156    $(PRIVATE_JAVA_LAYERS_FILE) \@$(PRIVATE_CLASS_INTERMEDIATES_DIR)/java-source-list-uniq,)
2157$(hide) rm -f $(PRIVATE_CLASS_INTERMEDIATES_DIR)/java-source-list
2158$(hide) rm -f $(PRIVATE_CLASS_INTERMEDIATES_DIR)/java-source-list-uniq
2159$(if $(PRIVATE_JAR_EXCLUDE_FILES), $(hide) find $(PRIVATE_CLASS_INTERMEDIATES_DIR) \
2160    -name $(word 1, $(PRIVATE_JAR_EXCLUDE_FILES)) \
2161    $(addprefix -o -name , $(wordlist 2, 999, $(PRIVATE_JAR_EXCLUDE_FILES))) \
2162    | xargs rm -rf)
2163$(if $(PRIVATE_JAR_PACKAGES), \
2164    $(hide) find $(PRIVATE_CLASS_INTERMEDIATES_DIR) -mindepth 1 -type f \
2165        $(foreach pkg, $(PRIVATE_JAR_PACKAGES), \
2166            -not -path $(PRIVATE_CLASS_INTERMEDIATES_DIR)/$(subst .,/,$(pkg))/\*) -delete ; \
2167        find $(PRIVATE_CLASS_INTERMEDIATES_DIR) -empty -delete)
2168$(if $(PRIVATE_JAR_EXCLUDE_PACKAGES), $(hide) rm -rf \
2169    $(foreach pkg, $(PRIVATE_JAR_EXCLUDE_PACKAGES), \
2170        $(PRIVATE_CLASS_INTERMEDIATES_DIR)/$(subst .,/,$(pkg))))
2171$(if $(PRIVATE_JAR_MANIFEST), \
2172    $(hide) sed -e "s/%BUILD_NUMBER%/$(BUILD_NUMBER_FROM_FILE)/" \
2173            $(PRIVATE_JAR_MANIFEST) > $(dir $@)/manifest.mf && \
2174        jar -cfm $@ $(dir $@)/manifest.mf \
2175            -C $(PRIVATE_CLASS_INTERMEDIATES_DIR) ., \
2176    $(hide) jar -cf $@ -C $(PRIVATE_CLASS_INTERMEDIATES_DIR) .)
2177$(if $(PRIVATE_EXTRA_JAR_ARGS),$(call add-java-resources-to,$@))
2178endef
2179
2180define transform-java-to-classes.jar
2181@echo "target Java: $(PRIVATE_MODULE) ($(PRIVATE_CLASS_INTERMEDIATES_DIR))"
2182$(call compile-java,$(TARGET_JAVAC),$(PRIVATE_BOOTCLASSPATH))
2183endef
2184
2185# Invoke Jack to compile java from source to dex and jack files.
2186#
2187# Some historical notes:
2188# - below we write the list of java files to java-source-list to avoid argument
2189#   list length problems with Cygwin
2190# - we filter out duplicate java file names because Jack doesn't like them.
2191define jack-java-to-dex
2192$(hide) rm -f $@
2193$(hide) rm -f $(PRIVATE_CLASSES_JACK)
2194$(hide) rm -rf $(PRIVATE_JACK_INTERMEDIATES_DIR)
2195$(hide) mkdir -p $(dir $@)
2196$(hide) mkdir -p $(dir $(PRIVATE_CLASSES_JACK))
2197$(hide) mkdir -p $(PRIVATE_JACK_INTERMEDIATES_DIR)
2198$(if $(PRIVATE_JACK_INCREMENTAL_DIR),$(hide) mkdir -p $(PRIVATE_JACK_INCREMENTAL_DIR))
2199$(call dump-words-to-file,$(PRIVATE_JAVA_SOURCES),$(PRIVATE_JACK_INTERMEDIATES_DIR)/java-source-list)
2200$(hide) if [ -d "$(PRIVATE_SOURCE_INTERMEDIATES_DIR)" ]; then \
2201          find $(PRIVATE_SOURCE_INTERMEDIATES_DIR) -name '*.java' >> $(PRIVATE_JACK_INTERMEDIATES_DIR)/java-source-list; \
2202fi
2203$(hide) tr ' ' '\n' < $(PRIVATE_JACK_INTERMEDIATES_DIR)/java-source-list \
2204    | $(NORMALIZE_PATH) | sort -u > $(PRIVATE_JACK_INTERMEDIATES_DIR)/java-source-list-uniq
2205$(if $(PRIVATE_JACK_PROGUARD_FLAGS), \
2206    $(hide) echo -basedirectory $(CURDIR) > $@.flags; \
2207    echo $(PRIVATE_JACK_PROGUARD_FLAGS) >> $@.flags; \
2208)
2209$(if $(PRIVATE_EXTRA_JAR_ARGS),
2210    $(hide) mkdir -p $@.res.tmp
2211    $(hide) $(call create-empty-package-at,$@.res.tmp.zip)
2212    $(hide) $(call add-java-resources-to,$@.res.tmp.zip)
2213    $(hide) unzip -qo $@.res.tmp.zip -d $@.res.tmp
2214    $(hide) rm $@.res.tmp.zip)
2215$(hide) if [ -s $(PRIVATE_JACK_INTERMEDIATES_DIR)/java-source-list-uniq ] ; then \
2216    export tmpEcjArg="@$(PRIVATE_JACK_INTERMEDIATES_DIR)/java-source-list-uniq"; \
2217else \
2218    export tmpEcjArg=""; \
2219fi; \
2220$(call call-jack) \
2221    $(strip $(PRIVATE_JACK_FLAGS)) \
2222    $(strip $(PRIVATE_JACK_COVERAGE_OPTIONS)) \
2223    $(if $(NO_OPTIMIZE_DX), \
2224        -D jack.dex.optimize="false") \
2225    $(if $(PRIVATE_RMTYPEDEFS), \
2226        -D jack.android.remove-typedef="true") \
2227    $(addprefix --classpath ,$(strip \
2228        $(call normalize-path-list,$(PRIVATE_JACK_SHARED_LIBRARIES)))) \
2229    $(addprefix --import ,$(call reverse-list,$(PRIVATE_STATIC_JACK_LIBRARIES))) \
2230    $(if $(PRIVATE_EXTRA_JAR_ARGS),--import-resource $@.res.tmp) \
2231    -D jack.android.min-api-level=$(PRIVATE_JACK_MIN_SDK_VERSION) \
2232    -D jack.import.resource.policy=keep-first \
2233    -D jack.import.type.policy=keep-first \
2234    --output-jack $(PRIVATE_CLASSES_JACK) \
2235    $(if $(PRIVATE_JACK_INCREMENTAL_DIR),--incremental-folder $(PRIVATE_JACK_INCREMENTAL_DIR)) \
2236    --output-dex $(PRIVATE_JACK_INTERMEDIATES_DIR) \
2237    $(addprefix --config-jarjar ,$(strip $(PRIVATE_JARJAR_RULES))) \
2238    $(if $(PRIVATE_JACK_PROGUARD_FLAGS),--config-proguard $@.flags) \
2239    $$tmpEcjArg \
2240    || ( rm -rf $(PRIVATE_CLASSES_JACK); exit 41 )
2241$(hide) mv $(PRIVATE_JACK_INTERMEDIATES_DIR)/classes*.dex $(dir $@)
2242$(hide) rm -f $(PRIVATE_JACK_INTERMEDIATES_DIR)/java-source-list
2243$(if $(PRIVATE_EXTRA_JAR_ARGS),$(hide) rm -rf $@.res.tmp)
2244$(hide) mv $(PRIVATE_JACK_INTERMEDIATES_DIR)/java-source-list-uniq $(PRIVATE_JACK_INTERMEDIATES_DIR).java-source-list
2245$(if $(PRIVATE_JAR_PACKAGES), $(hide) echo unsupported options PRIVATE_JAR_PACKAGES in $@; exit 53)
2246$(if $(PRIVATE_JAR_EXCLUDE_PACKAGES), $(hide) echo unsupported options JAR_EXCLUDE_PACKAGES in $@; exit 53)
2247$(if $(PRIVATE_JAR_MANIFEST), $(hide) echo unsupported options JAR_MANIFEST in $@; exit 53)
2248endef
2249
2250# Invoke Jack to compile java source just to check it compiles correctly.
2251#
2252# Some historical notes:
2253# - below we write the list of java files to java-source-list to avoid argument
2254#   list length problems with Cygwin
2255# - we filter out duplicate java file names because Jack doesn't like them.
2256define jack-check-java
2257$(hide) rm -f $@
2258$(hide) rm -f $@.java-source-list
2259$(hide) rm -f $@.java-source-list-uniq
2260$(hide) mkdir -p $(dir $@)
2261$(if $(PRIVATE_JACK_INCREMENTAL_DIR),$(hide) mkdir -p $(PRIVATE_JACK_INCREMENTAL_DIR))
2262$(call dump-words-to-file,$(PRIVATE_JAVA_SOURCES),$@.java-source-list)
2263$(hide) if [ -d "$(PRIVATE_SOURCE_INTERMEDIATES_DIR)" ]; then \
2264          find $(PRIVATE_SOURCE_INTERMEDIATES_DIR) -name '*.java' >> $@.java-source-list; \
2265fi
2266$(hide) tr ' ' '\n' < $@.java-source-list \
2267    | sort -u > $@.java-source-list-uniq
2268$(hide) if [ -s $@.java-source-list-uniq ] ; then \
2269	$(call call-jack) \
2270	    $(strip $(PRIVATE_JACK_FLAGS)) \
2271	    $(addprefix --classpath ,$(strip \
2272	        $(call normalize-path-list,$(call reverse-list,$(PRIVATE_STATIC_JACK_LIBRARIES)) $(PRIVATE_JACK_SHARED_LIBRARIES)))) \
2273	    -D jack.import.resource.policy=keep-first \
2274	    -D jack.android.min-api-level=$(PRIVATE_JACK_MIN_SDK_VERSION) \
2275	    -D jack.import.type.policy=keep-first \
2276	    $(if $(PRIVATE_JACK_INCREMENTAL_DIR),--incremental-folder $(PRIVATE_JACK_INCREMENTAL_DIR)) \
2277	    @$@.java-source-list-uniq; \
2278fi
2279touch $@
2280endef
2281
2282define transform-jar-to-jack
2283	$(hide) mkdir -p $(dir $@)
2284	$(hide) mkdir -p $@.tmpjill.res
2285	$(hide) unzip -qo $< -d $@.tmpjill.res
2286	$(hide) find $@.tmpjill.res -iname "*.class" -delete
2287	$(hide) $(call call-jack) \
2288	    $(PRIVATE_JACK_FLAGS) \
2289        -D jack.import.resource.policy=keep-first \
2290        -D jack.import.type.policy=keep-first \
2291        -D jack.android.min-api-level=$(PRIVATE_JACK_MIN_SDK_VERSION) \
2292	    --import $< \
2293	    --import-resource $@.tmpjill.res \
2294	    --output-jack $@
2295	$(hide) rm -rf $@.tmpjill.res
2296endef
2297
2298# Moves $1.tmp to $1 if necessary. This is designed to be used with
2299# .KATI_RESTAT. For kati, this function doesn't update the timestamp
2300# of $1 when $1.tmp is identical to $1 so that ninja won't rebuild
2301# targets which depend on $1.
2302define commit-change-for-toc
2303$(hide) if cmp -s $1.tmp $1 ; then \
2304 rm $1.tmp ; \
2305else \
2306 mv $1.tmp $1 ; \
2307fi
2308endef
2309
2310## Rule to create a table of contents from a .jar file.
2311## Must be called with $(eval).
2312# $(1): A .jar file
2313define _transform-jar-to-toc
2314$1.toc: $1 | $(IJAR)
2315	@echo Generating TOC: $$@
2316	$(hide) $(IJAR) $$< $$@.tmp
2317	$$(call commit-change-for-toc,$$@)
2318endef
2319
2320## Define a rule which generates .jar.toc and mark it as .KATI_RESTAT.
2321# $(1): A .jar file
2322define define-jar-to-toc-rule
2323$(eval $(call _transform-jar-to-toc,$1))\
2324$(eval .KATI_RESTAT: $1.toc)
2325endef
2326
2327ifeq (,$(TARGET_BUILD_APPS))
2328
2329## Rule to create a table of contents from a .dex file.
2330## Must be called with $(eval).
2331# $(1): The directory which contains classes*.dex files
2332define _transform-dex-to-toc
2333$1/classes.dex.toc: PRIVATE_INPUT_DEX_FILES := $1/classes*.dex
2334$1/classes.dex.toc: $1/classes.dex $(DEXDUMP)
2335	@echo Generating TOC: $$@
2336	$(hide) $(DEXDUMP) -l xml $$(PRIVATE_INPUT_DEX_FILES) > $$@.tmp
2337	$$(call commit-change-for-toc,$$@)
2338endef
2339
2340## Define a rule which generates .dex.toc and mark it as .KATI_RESTAT.
2341# $(1): The directory which contains classes*.dex files
2342define define-dex-to-toc-rule
2343$(eval $(call _transform-dex-to-toc,$1))\
2344$(eval .KATI_RESTAT: $1/classes.dex.toc)
2345endef
2346
2347else
2348
2349# Turn off .toc optimization for apps build as we cannot build dexdump.
2350define define-dex-to-toc-rule
2351endef
2352
2353endif  # TARGET_BUILD_APPS
2354
2355
2356# Invoke Jack to compile java from source to jack files without shrink or obfuscation.
2357#
2358# Some historical notes:
2359# - below we write the list of java files to java-source-list to avoid argument
2360#   list length problems with Cygwin
2361# - we filter out duplicate java file names because Jack doesn't like them.
2362define java-to-jack
2363$(hide) rm -f $@
2364$(hide) rm -rf $(PRIVATE_JACK_INTERMEDIATES_DIR)
2365$(hide) mkdir -p $(dir $@)
2366$(hide) mkdir -p $(PRIVATE_JACK_INTERMEDIATES_DIR)
2367$(if $(PRIVATE_JACK_INCREMENTAL_DIR),$(hide) mkdir -p $(PRIVATE_JACK_INCREMENTAL_DIR))
2368$(call dump-words-to-file,$(PRIVATE_JAVA_SOURCES),$(PRIVATE_JACK_INTERMEDIATES_DIR)/java-source-list)
2369$(hide) if [ -d "$(PRIVATE_SOURCE_INTERMEDIATES_DIR)" ]; then \
2370          find $(PRIVATE_SOURCE_INTERMEDIATES_DIR) -name '*.java' >> $(PRIVATE_JACK_INTERMEDIATES_DIR)/java-source-list; \
2371fi
2372$(hide) tr ' ' '\n' < $(PRIVATE_JACK_INTERMEDIATES_DIR)/java-source-list \
2373    | $(NORMALIZE_PATH) | sort -u > $(PRIVATE_JACK_INTERMEDIATES_DIR)/java-source-list-uniq
2374$(if $(PRIVATE_JACK_PROGUARD_FLAGS), \
2375    $(hide) echo -basedirectory $(CURDIR) > $@.flags; \
2376    echo $(PRIVATE_JACK_PROGUARD_FLAGS) >> $@.flags; \
2377)
2378$(if $(PRIVATE_EXTRA_JAR_ARGS),
2379	$(hide) mkdir -p $@.res.tmp
2380	$(hide) $(call create-empty-package-at,$@.res.tmp.zip)
2381	$(hide) $(call add-java-resources-to,$@.res.tmp.zip)
2382	$(hide) unzip -qo $@.res.tmp.zip -d $@.res.tmp
2383	$(hide) rm $@.res.tmp.zip)
2384$(hide) if [ -s $(PRIVATE_JACK_INTERMEDIATES_DIR)/java-source-list-uniq ] ; then \
2385    export tmpEcjArg="@$(PRIVATE_JACK_INTERMEDIATES_DIR)/java-source-list-uniq"; \
2386else \
2387    export tmpEcjArg=""; \
2388fi; \
2389$(call call-jack) \
2390    $(strip $(PRIVATE_JACK_FLAGS)) \
2391    $(if $(NO_OPTIMIZE_DX), \
2392        -D jack.dex.optimize="false") \
2393    $(addprefix --classpath ,$(strip \
2394        $(call normalize-path-list,$(PRIVATE_JACK_SHARED_LIBRARIES)))) \
2395    $(addprefix --import ,$(call reverse-list,$(PRIVATE_STATIC_JACK_LIBRARIES))) \
2396    $(if $(PRIVATE_EXTRA_JAR_ARGS),--import-resource $@.res.tmp) \
2397    -D jack.import.resource.policy=keep-first \
2398    -D jack.import.type.policy=keep-first \
2399    -D jack.android.min-api-level=$(PRIVATE_JACK_MIN_SDK_VERSION) \
2400    $(if $(PRIVATE_JACK_INCREMENTAL_DIR),--incremental-folder $(PRIVATE_JACK_INCREMENTAL_DIR)) \
2401    --output-jack $@ \
2402    $(addprefix --config-jarjar ,$(strip $(PRIVATE_JARJAR_RULES))) \
2403    $(if $(PRIVATE_JACK_PROGUARD_FLAGS),--config-proguard $@.flags) \
2404    $$tmpEcjArg \
2405    || ( rm -f $@ ; exit 41 )
2406$(hide) rm -f $(PRIVATE_JACK_INTERMEDIATES_DIR)/java-source-list
2407$(if $(PRIVATE_EXTRA_JAR_ARGS),$(hide) rm -rf $@.res.tmp)
2408$(hide) mv $(PRIVATE_JACK_INTERMEDIATES_DIR)/java-source-list-uniq $(PRIVATE_JACK_INTERMEDIATES_DIR).java-source-list
2409$(if $(PRIVATE_JAR_PACKAGES), $(hide) echo unsupported options PRIVATE_JAR_PACKAGES in $@; exit 53)
2410$(if $(PRIVATE_JAR_EXCLUDE_PACKAGES), $(hide) echo unsupported options JAR_EXCLUDE_PACKAGES in $@; exit 53)
2411$(if $(PRIVATE_JAR_MANIFEST), $(hide) echo unsupported options JAR_MANIFEST in $@; exit 53)
2412endef
2413
2414define transform-classes.jar-to-emma
2415$(hide) java -classpath $(EMMA_JAR) emma instr -outmode fullcopy -outfile \
2416    $(PRIVATE_EMMA_COVERAGE_FILE) -ip $< -d $(PRIVATE_EMMA_INTERMEDIATES_DIR) \
2417    $(addprefix -ix , $(PRIVATE_EMMA_COVERAGE_FILTER))
2418endef
2419
2420# Create a mostly-empty .jar file that we'll add to later.
2421# The MacOS jar tool doesn't like creating empty jar files,
2422# so we need to give it something.
2423# $(1) package to create
2424define create-empty-package-at
2425@mkdir -p $(dir $(1))
2426$(hide) touch $(dir $(1))zipdummy
2427$(hide) (cd $(dir $(1)) && jar cf $(notdir $(1)) zipdummy)
2428$(hide) zip -qd $(1) zipdummy
2429$(hide) rm $(dir $(1))zipdummy
2430endef
2431
2432# Create a mostly-empty .jar file that we'll add to later.
2433# The MacOS jar tool doesn't like creating empty jar files,
2434# so we need to give it something.
2435define create-empty-package
2436$(call create-empty-package-at,$@)
2437endef
2438
2439#TODO: we kinda want to build different asset packages for
2440#      different configurations, then combine them later (or something).
2441#      Per-locale, etc.
2442#      A list of dynamic and static parameters;  build layers for
2443#      dynamic params that lay over the static ones.
2444#TODO: update the manifest to point to the package file
2445#Note that the version numbers are given to aapt as simple default
2446#values; applications can override these by explicitly stating
2447#them in their manifest.
2448define add-assets-to-package
2449$(hide) $(AAPT) package -u $(PRIVATE_AAPT_FLAGS) \
2450    $(addprefix -c , $(PRIVATE_PRODUCT_AAPT_CONFIG)) \
2451    $(addprefix --preferred-density , $(PRIVATE_PRODUCT_AAPT_PREF_CONFIG)) \
2452    $(addprefix -M , $(PRIVATE_ANDROID_MANIFEST)) \
2453    $(addprefix -S , $(PRIVATE_RESOURCE_DIR)) \
2454    $(addprefix -A , $(PRIVATE_ASSET_DIR)) \
2455    $(addprefix -I , $(PRIVATE_AAPT_INCLUDES)) \
2456    $(addprefix --min-sdk-version , $(PRIVATE_DEFAULT_APP_TARGET_SDK)) \
2457    $(addprefix --target-sdk-version , $(PRIVATE_DEFAULT_APP_TARGET_SDK)) \
2458    $(if $(filter --product,$(PRIVATE_AAPT_FLAGS)),,$(addprefix --product , $(PRIVATE_TARGET_AAPT_CHARACTERISTICS))) \
2459    $(if $(filter --version-code,$(PRIVATE_AAPT_FLAGS)),,--version-code $(PLATFORM_SDK_VERSION)) \
2460    $(if $(filter --version-name,$(PRIVATE_AAPT_FLAGS)),,--version-name $(APPS_DEFAULT_VERSION_NAME)) \
2461    $(addprefix --rename-manifest-package , $(PRIVATE_MANIFEST_PACKAGE_NAME)) \
2462    $(addprefix --rename-instrumentation-target-package , $(PRIVATE_MANIFEST_INSTRUMENTATION_FOR)) \
2463    --skip-symbols-without-default-localization \
2464    -F $@
2465endef
2466
2467# We need the extra blank line, so that the command will be on a separate line.
2468# $(1): the ABI name
2469# $(2): the list of shared libraies
2470define _add-jni-shared-libs-to-package-per-abi
2471$(hide) cp $(2) $(dir $@)lib/$(1)
2472
2473endef
2474
2475# For apps_only build, don't uncompress/page-align the jni libraries,
2476# because the apk may be run on older platforms that don't support loading jni directly from apk.
2477ifdef TARGET_BUILD_APPS
2478JNI_COMPRESS_FLAGS :=
2479ZIPALIGN_PAGE_ALIGN_FLAGS :=
2480else
2481JNI_COMPRESS_FLAGS := -0
2482ZIPALIGN_PAGE_ALIGN_FLAGS := -p
2483endif
2484
2485define add-jni-shared-libs-to-package
2486$(hide) rm -rf $(dir $@)lib
2487$(hide) mkdir -p $(addprefix $(dir $@)lib/,$(PRIVATE_JNI_SHARED_LIBRARIES_ABI))
2488$(foreach abi,$(PRIVATE_JNI_SHARED_LIBRARIES_ABI),\
2489  $(call _add-jni-shared-libs-to-package-per-abi,$(abi),\
2490    $(patsubst $(abi):%,%,$(filter $(abi):%,$(PRIVATE_JNI_SHARED_LIBRARIES)))))
2491$(hide) (cd $(dir $@) && zip -qrX $(JNI_COMPRESS_FLAGS) $(notdir $@) lib)
2492$(hide) rm -rf $(dir $@)lib
2493endef
2494
2495#TODO: update the manifest to point to the dex file
2496define add-dex-to-package
2497$(call add-dex-to-package-arg,$@)
2498endef
2499
2500# $(1): the package file.
2501define add-dex-to-package-arg
2502$(hide) find $(dir $(PRIVATE_DEX_FILE)) -maxdepth 1 -name "classes*.dex" | sort | xargs zip -qjX $(1)
2503endef
2504
2505# Add java resources added by the current module.
2506# $(1) destination package
2507#
2508define add-java-resources-to
2509$(call dump-words-to-file, $(PRIVATE_EXTRA_JAR_ARGS), $(1).jar-arg-list)
2510$(hide) jar uf $(1) @$(1).jar-arg-list
2511@rm -f $(1).jar-arg-list
2512endef
2513
2514# Add resources carried by static Jack libraries.
2515#
2516define add-carried-jack-resources
2517 $(hide) if [ -d $(PRIVATE_JACK_INTERMEDIATES_DIR) ] ; then \
2518    find $(PRIVATE_JACK_INTERMEDIATES_DIR) -type f | sort \
2519        | sed -e "s?^$(PRIVATE_JACK_INTERMEDIATES_DIR)/? -C \"$(PRIVATE_JACK_INTERMEDIATES_DIR)\" \"?" -e "s/$$/\"/" \
2520        > $(dir $@)jack_res_jar_flags; \
2521    if [ -s $(dir $@)jack_res_jar_flags ] ; then \
2522        jar uf $@ @$(dir $@)jack_res_jar_flags; \
2523    fi; \
2524fi
2525endef
2526
2527# Returns the minSdkVersion of the specified APK as a decimal number. If the
2528# version is a codename, returns the current platform SDK version (always a
2529# decimal number) instead. If the APK does not specify a minSdkVersion, returns
2530# 0 to match how the Android platform interprets this situation at runtime.
2531#
2532# This currently substitutes any version which contains characters other than
2533# digits with the current platform's API Level number. This is because I
2534# couldn't figure out an easy way to perform the substitution only for the
2535# version codes listed in PLATFORM_VERSION_ALL_CODENAMES.
2536define get-package-min-sdk-version-int
2537$$(($(AAPT) dump badging $(1) 2>&1 | grep '^sdkVersion' || echo "sdkVersion:'0'") \
2538    | cut -d"'" -f2 | \
2539    sed -e s/^.*[^0-9].*$$/$(PLATFORM_SDK_VERSION)/)
2540endef
2541
2542# Sign a package using the specified key/cert.
2543#
2544define sign-package
2545$(call sign-package-arg,$@)
2546endef
2547
2548# $(1): the package file we are signing.
2549define sign-package-arg
2550$(hide) mv $(1) $(1).unsigned
2551$(hide) java -Djava.library.path=$(SIGNAPK_JNI_LIBRARY_PATH) -jar $(SIGNAPK_JAR) \
2552    --min-sdk-version $(call get-package-min-sdk-version-int,$@.unsigned) \
2553    $(PRIVATE_CERTIFICATE) $(PRIVATE_PRIVATE_KEY) \
2554    $(PRIVATE_ADDITIONAL_CERTIFICATES) $(1).unsigned $(1).signed
2555$(hide) mv $(1).signed $(1)
2556endef
2557
2558# Align STORED entries of a package on 4-byte boundaries to make them easier to mmap.
2559#
2560define align-package
2561$(hide) if ! $(ZIPALIGN) -c $(ZIPALIGN_PAGE_ALIGN_FLAGS) 4 $@ >/dev/null ; then \
2562  mv $@ $@.unaligned; \
2563  $(ZIPALIGN) \
2564    -f \
2565    $(ZIPALIGN_PAGE_ALIGN_FLAGS) \
2566    4 \
2567    $@.unaligned $@.aligned; \
2568  mv $@.aligned $@; \
2569  fi
2570endef
2571
2572# Remove dynamic timestamps from packages
2573#
2574ifndef TARGET_BUILD_APPS
2575define remove-timestamps-from-package
2576$(hide) $(ZIPTIME) $@
2577endef
2578endif
2579
2580# Uncompress shared libraries embedded in an apk.
2581#
2582define uncompress-shared-libs
2583$(hide) if unzip -l $@ $(PRIVATE_EMBEDDED_JNI_LIBS) >/dev/null ; then \
2584  rm -rf $(dir $@)uncompressedlibs && mkdir $(dir $@)uncompressedlibs; \
2585  unzip $@ $(PRIVATE_EMBEDDED_JNI_LIBS) -d $(dir $@)uncompressedlibs && \
2586  zip -d $@ 'lib/*.so' && \
2587  ( cd $(dir $@)uncompressedlibs && find lib -type f | sort | zip -D -X -0 ../$(notdir $@) -@ ) && \
2588  rm -rf $(dir $@)uncompressedlibs; \
2589  fi
2590endef
2591
2592# TODO(joeo): If we can ever upgrade to post 3.81 make and get the
2593# new prebuilt rules to work, we should change this to copy the
2594# resources to the out directory and then copy the resources.
2595
2596# Note: we intentionally don't clean PRIVATE_CLASS_INTERMEDIATES_DIR
2597# in transform-java-to-classes for the sake of vm-tests.
2598define transform-host-java-to-package
2599@echo "$($(PRIVATE_PREFIX)DISPLAY) Java: $(PRIVATE_MODULE) ($(PRIVATE_CLASS_INTERMEDIATES_DIR))"
2600$(call compile-java,$(HOST_JAVAC),$(PRIVATE_BOOTCLASSPATH))
2601endef
2602
2603###########################################################
2604## Commands for copying files
2605###########################################################
2606
2607# Define a rule to copy a header.  Used via $(eval) by copy_headers.make.
2608# $(1): source header
2609# $(2): destination header
2610define copy-one-header
2611$(2): $(1)
2612	@echo "Header: $$@"
2613	$$(copy-file-to-new-target-with-cp)
2614endef
2615
2616# Define a rule to copy a file.  For use via $(eval).
2617# $(1): source file
2618# $(2): destination file
2619define copy-one-file
2620$(2): $(1)
2621	@echo "Copy: $$@"
2622	$$(copy-file-to-target)
2623endef
2624
2625# Copies many files.
2626# $(1): The files to copy.  Each entry is a ':' separated src:dst pair
2627# Evaluates to the list of the dst files (ie suitable for a dependency list)
2628define copy-many-files
2629$(foreach f, $(1), $(strip \
2630    $(eval _cmf_tuple := $(subst :, ,$(f))) \
2631    $(eval _cmf_src := $(word 1,$(_cmf_tuple))) \
2632    $(eval _cmf_dest := $(word 2,$(_cmf_tuple))) \
2633    $(eval $(call copy-one-file,$(_cmf_src),$(_cmf_dest))) \
2634    $(_cmf_dest)))
2635endef
2636
2637# Copy the file only if it's a well-formed xml file. For use via $(eval).
2638# $(1): source file
2639# $(2): destination file, must end with .xml.
2640define copy-xml-file-checked
2641$(2): $(1)
2642	@echo "Copy xml: $$@"
2643	$(hide) xmllint $$< >/dev/null  # Don't print the xml file to stdout.
2644	$$(copy-file-to-target)
2645endef
2646
2647# The -t option to acp and the -p option to cp is
2648# required for OSX.  OSX has a ridiculous restriction
2649# where it's an error for a .a file's modification time
2650# to disagree with an internal timestamp, and this
2651# macro is used to install .a files (among other things).
2652
2653# Copy a single file from one place to another,
2654# preserving permissions and overwriting any existing
2655# file.
2656# When we used acp, it could not handle high resolution timestamps
2657# on file systems like ext4. Because of that, '-t' option was disabled
2658# and copy-file-to-target was identical to copy-file-to-new-target.
2659# Keep the behavior until we audit and ensure that switching this back
2660# won't break anything.
2661define copy-file-to-target
2662@mkdir -p $(dir $@)
2663$(hide) rm -f $@
2664$(hide) cp $< $@
2665endef
2666
2667# The same as copy-file-to-target, but use the local
2668# cp command instead of acp.
2669define copy-file-to-target-with-cp
2670@mkdir -p $(dir $@)
2671$(hide) rm -f $@
2672$(hide) cp -p $< $@
2673endef
2674
2675# The same as copy-file-to-target, but use the zipalign tool to do so.
2676define copy-file-to-target-with-zipalign
2677@mkdir -p $(dir $@)
2678$(hide) rm -f $@
2679$(hide) $(ZIPALIGN) -f 4 $< $@
2680endef
2681
2682# The same as copy-file-to-target, but strip out "# comment"-style
2683# comments (for config files and such).
2684define copy-file-to-target-strip-comments
2685@mkdir -p $(dir $@)
2686$(hide) rm -f $@
2687$(hide) sed -e 's/#.*$$//' -e 's/[ \t]*$$//' -e '/^$$/d' < $< > $@
2688endef
2689
2690# The same as copy-file-to-target, but don't preserve
2691# the old modification time.
2692define copy-file-to-new-target
2693@mkdir -p $(dir $@)
2694$(hide) rm -f $@
2695$(hide) cp $< $@
2696endef
2697
2698# The same as copy-file-to-new-target, but use the local
2699# cp command instead of acp.
2700define copy-file-to-new-target-with-cp
2701@mkdir -p $(dir $@)
2702$(hide) rm -f $@
2703$(hide) cp $< $@
2704endef
2705
2706# Copy a prebuilt file to a target location.
2707define transform-prebuilt-to-target
2708@echo "$(if $(PRIVATE_IS_HOST_MODULE),host,target) Prebuilt: $(PRIVATE_MODULE) ($@)"
2709$(copy-file-to-target)
2710endef
2711
2712# Copy a prebuilt file to a target location, using zipalign on it.
2713define transform-prebuilt-to-target-with-zipalign
2714@echo "$(if $(PRIVATE_IS_HOST_MODULE),host,target) Prebuilt APK: $(PRIVATE_MODULE) ($@)"
2715$(copy-file-to-target-with-zipalign)
2716endef
2717
2718# Copy a prebuilt file to a target location, stripping "# comment" comments.
2719define transform-prebuilt-to-target-strip-comments
2720@echo "$(if $(PRIVATE_IS_HOST_MODULE),host,target) Prebuilt: $(PRIVATE_MODULE) ($@)"
2721$(copy-file-to-target-strip-comments)
2722endef
2723
2724# Copy a list of files/directories to target location, with sub dir structure preserved.
2725# For example $(HOST_OUT_EXECUTABLES)/aapt -> $(staging)/bin/aapt .
2726# $(1): the source list of files/directories.
2727# $(2): the path prefix to strip. In the above example it would be $(HOST_OUT).
2728# $(3): the target location.
2729define copy-files-with-structure
2730$(foreach t,$(1),\
2731  $(eval s := $(patsubst $(2)%,%,$(t)))\
2732  $(hide) mkdir -p $(dir $(3)/$(s)); cp -Rf $(t) $(3)/$(s)$(newline))
2733endef
2734
2735
2736###########################################################
2737## Commands to call Proguard
2738###########################################################
2739define transform-jar-to-proguard
2740@echo Proguard: $@
2741$(hide) $(PROGUARD) -injars $< -outjars $@ $(PRIVATE_PROGUARD_FLAGS) \
2742    $(addprefix -injars , $(PRIVATE_EXTRA_INPUT_JAR))
2743endef
2744
2745###########################################################
2746## Stuff source generated from one-off tools
2747###########################################################
2748
2749define transform-generated-source
2750@echo "target Generated: $(PRIVATE_MODULE) <= $<"
2751@mkdir -p $(dir $@)
2752$(hide) $(PRIVATE_CUSTOM_TOOL)
2753endef
2754
2755
2756###########################################################
2757## Assertions about attributes of the target
2758###########################################################
2759
2760# $(1): The file to check
2761ifndef get-file-size
2762$(error HOST_OS must define get-file-size)
2763endif
2764
2765# Convert a partition data size (eg, as reported in /proc/mtd) to the
2766# size of the image used to flash that partition (which includes a
2767# spare area for each page).
2768# $(1): the partition data size
2769define image-size-from-data-size
2770$(strip $(eval _isfds_value := $$(shell echo $$$$(($(1) / $(BOARD_NAND_PAGE_SIZE) * \
2771  ($(BOARD_NAND_PAGE_SIZE)+$(BOARD_NAND_SPARE_SIZE))))))\
2772$(if $(filter 0, $(_isfds_value)),$(shell echo $$(($(BOARD_NAND_PAGE_SIZE)+$(BOARD_NAND_SPARE_SIZE)))),$(_isfds_value))\
2773$(eval _isfds_value :=))
2774endef
2775
2776# $(1): The file(s) to check (often $@)
2777# $(2): The maximum total image size, in decimal bytes.
2778#    Make sure to take into account any reserved space needed for the FS.
2779#
2780# If $(2) is empty, evaluates to "true"
2781#
2782# Reserve bad blocks.  Make sure that MAX(1% of partition size, 2 blocks)
2783# is left over after the image has been flashed.  Round the 1% up to the
2784# next whole flash block size.
2785define assert-max-file-size
2786$(if $(2), \
2787  size=$$(for i in $(1); do $(call get-file-size,$$i); echo +; done; echo 0); \
2788  total=$$(( $$( echo "$$size" ) )); \
2789  printname=$$(echo -n "$(1)" | tr " " +); \
2790  img_blocksize=$(call image-size-from-data-size,$(BOARD_FLASH_BLOCK_SIZE)); \
2791  twoblocks=$$((img_blocksize * 2)); \
2792  onepct=$$((((($(2) / 100) - 1) / img_blocksize + 1) * img_blocksize)); \
2793  reserve=$$((twoblocks > onepct ? twoblocks : onepct)); \
2794  maxsize=$$(($(2) - reserve)); \
2795  echo "$$printname maxsize=$$maxsize blocksize=$$img_blocksize total=$$total reserve=$$reserve"; \
2796  if [ "$$total" -gt "$$maxsize" ]; then \
2797    echo "error: $$printname too large ($$total > [$(2) - $$reserve])"; \
2798    false; \
2799  elif [ "$$total" -gt $$((maxsize - 32768)) ]; then \
2800    echo "WARNING: $$printname approaching size limit ($$total now; limit $$maxsize)"; \
2801  fi \
2802 , \
2803  true \
2804 )
2805endef
2806
2807# Like assert-max-file-size, but the second argument is a partition
2808# size, which we'll convert to a max image size before checking it
2809# against the files.
2810#
2811# $(1): The file(s) to check (often $@)
2812# $(2): The partition size.
2813define assert-max-image-size
2814$(if $(2), \
2815  $(call assert-max-file-size,$(1),$(call image-size-from-data-size,$(2))))
2816endef
2817
2818
2819###########################################################
2820## Define device-specific radio files
2821###########################################################
2822INSTALLED_RADIOIMAGE_TARGET :=
2823
2824# Copy a radio image file to the output location, and add it to
2825# INSTALLED_RADIOIMAGE_TARGET.
2826# $(1): filename
2827define add-radio-file
2828  $(eval $(call add-radio-file-internal,$(1),$(notdir $(1))))
2829endef
2830define add-radio-file-internal
2831INSTALLED_RADIOIMAGE_TARGET += $$(PRODUCT_OUT)/$(2)
2832$$(PRODUCT_OUT)/$(2) : $$(LOCAL_PATH)/$(1)
2833	$$(transform-prebuilt-to-target)
2834endef
2835
2836# Version of add-radio-file that also arranges for the version of the
2837# file to be checked against the contents of
2838# $(TARGET_BOARD_INFO_FILE).
2839# $(1): filename
2840# $(2): name of version variable in board-info (eg, "version-baseband")
2841define add-radio-file-checked
2842  $(eval $(call add-radio-file-checked-internal,$(1),$(notdir $(1)),$(2)))
2843endef
2844define add-radio-file-checked-internal
2845INSTALLED_RADIOIMAGE_TARGET += $$(PRODUCT_OUT)/$(2)
2846BOARD_INFO_CHECK += $(3):$(LOCAL_PATH)/$(1)
2847$$(PRODUCT_OUT)/$(2) : $$(LOCAL_PATH)/$(1)
2848	$$(transform-prebuilt-to-target)
2849endef
2850
2851
2852###########################################################
2853# Override the package defined in $(1), setting the
2854# variables listed below differently.
2855#
2856#  $(1): The makefile to override (relative to the source
2857#        tree root)
2858#  $(2): Old LOCAL_PACKAGE_NAME value.
2859#  $(3): New LOCAL_PACKAGE_NAME value.
2860#  $(4): New LOCAL_MANIFEST_PACKAGE_NAME value.
2861#  $(5): New LOCAL_CERTIFICATE value.
2862#  $(6): New LOCAL_INSTRUMENTATION_FOR value.
2863#  $(7): New LOCAL_MANIFEST_INSTRUMENTATION_FOR value.
2864#
2865# Note that LOCAL_PACKAGE_OVERRIDES is NOT cleared in
2866# clear_vars.mk.
2867###########################################################
2868define inherit-package
2869  $(eval $(call inherit-package-internal,$(1),$(2),$(3),$(4),$(5),$(6),$(7)))
2870endef
2871
2872define inherit-package-internal
2873  LOCAL_PACKAGE_OVERRIDES \
2874      := $(strip $(1))||$(strip $(2))||$(strip $(3))||$(strip $(4))||&&$(strip $(5))||&&$(strip $(6))||&&$(strip $(7)) $(LOCAL_PACKAGE_OVERRIDES)
2875  include $(1)
2876  LOCAL_PACKAGE_OVERRIDES \
2877      := $(wordlist 1,$(words $(LOCAL_PACKAGE_OVERRIDES)), $(LOCAL_PACKAGE_OVERRIDES))
2878endef
2879
2880# To be used with inherit-package above
2881# Evalutes to true if the package was overridden
2882define set-inherited-package-variables
2883$(strip $(call set-inherited-package-variables-internal))
2884endef
2885
2886define keep-or-override
2887$(eval $(1) := $(if $(2),$(2),$($(1))))
2888endef
2889
2890define set-inherited-package-variables-internal
2891  $(eval _o := $(subst ||, ,$(lastword $(LOCAL_PACKAGE_OVERRIDES))))
2892  $(eval _n := $(subst ||, ,$(firstword $(LOCAL_PACKAGE_OVERRIDES))))
2893  $(if $(filter $(word 2,$(_n)),$(LOCAL_PACKAGE_NAME)), \
2894    $(eval LOCAL_PACKAGE_NAME := $(word 3,$(_o))) \
2895    $(eval LOCAL_MANIFEST_PACKAGE_NAME := $(word 4,$(_o))) \
2896    $(call keep-or-override,LOCAL_CERTIFICATE,$(patsubst &&%,%,$(word 5,$(_o)))) \
2897    $(call keep-or-override,LOCAL_INSTRUMENTATION_FOR,$(patsubst &&%,%,$(word 6,$(_o)))) \
2898    $(call keep-or-override,LOCAL_MANIFEST_INSTRUMENTATION_FOR,$(patsubst &&%,%,$(word 7,$(_o)))) \
2899    $(eval LOCAL_OVERRIDES_PACKAGES := $(sort $(LOCAL_OVERRIDES_PACKAGES) $(word 2,$(_o)))) \
2900    true \
2901  ,)
2902endef
2903
2904###########################################################
2905## API Check
2906###########################################################
2907
2908# eval this to define a rule that runs apicheck.
2909#
2910# Args:
2911#    $(1)  target
2912#    $(2)  stable api file
2913#    $(3)  api file to be tested
2914#    $(4)  stable removed api file
2915#    $(5)  removed api file to be tested
2916#    $(6)  arguments for apicheck
2917#    $(7)  command to run if apicheck failed
2918#    $(8)  target dependent on this api check
2919#    $(9)  additional dependencies
2920define check-api
2921$(TARGET_OUT_COMMON_INTERMEDIATES)/PACKAGING/$(strip $(1))-timestamp: $(2) $(3) $(4) $(APICHECK) $(9)
2922	@echo "Checking API:" $(1)
2923	$(hide) ( $(APICHECK_COMMAND) $(6) $(2) $(3) $(4) $(5) || ( $(7) ; exit 38 ) )
2924	$(hide) mkdir -p $$(dir $$@)
2925	$(hide) touch $$@
2926$(8): $(TARGET_OUT_COMMON_INTERMEDIATES)/PACKAGING/$(strip $(1))-timestamp
2927endef
2928
2929## Whether to build from source if prebuilt alternative exists
2930###########################################################
2931# $(1): module name
2932# $(2): LOCAL_PATH
2933# Expands to empty string if not from source.
2934ifeq (true,$(ANDROID_BUILD_FROM_SOURCE))
2935define if-build-from-source
2936true
2937endef
2938else
2939define if-build-from-source
2940$(if $(filter $(ANDROID_NO_PREBUILT_MODULES),$(1))$(filter \
2941    $(addsuffix %,$(ANDROID_NO_PREBUILT_PATHS)),$(2)),true)
2942endef
2943endif
2944
2945# Include makefile $(1) if build from source for module $(2)
2946# $(1): the makefile to include
2947# $(2): module name
2948# $(3): LOCAL_PATH
2949define include-if-build-from-source
2950$(if $(call if-build-from-source,$(2),$(3)),$(eval include $(1)))
2951endef
2952
2953# Return the arch for the source file of a prebuilt
2954# Return "none" if no matching arch found and return empty
2955# if the input is empty, so the result can be passed to
2956# LOCAL_MODULE_TARGET_ARCH.
2957# $(1) the list of archs supported by the prebuilt
2958define get-prebuilt-src-arch
2959$(strip $(if $(filter $(TARGET_ARCH),$(1)),$(TARGET_ARCH),\
2960  $(if $(filter $(TARGET_2ND_ARCH),$(1)),$(TARGET_2ND_ARCH),$(if $(1),none))))
2961endef
2962
2963###########################################################
2964## Other includes
2965###########################################################
2966
2967# -----------------------------------------------------------------
2968# Rules and functions to help copy important files to DIST_DIR
2969# when requested.
2970include $(BUILD_SYSTEM)/distdir.mk
2971
2972# Include any vendor specific definitions.mk file
2973-include $(TOPDIR)vendor/*/build/core/definitions.mk
2974-include $(TOPDIR)device/*/build/core/definitions.mk
2975-include $(TOPDIR)product/*/build/core/definitions.mk
2976
2977# broken:
2978#	$(foreach file,$^,$(if $(findstring,.a,$(suffix $file)),-l$(file),$(file)))
2979
2980###########################################################
2981## Misc notes
2982###########################################################
2983
2984#DEPDIR = .deps
2985#df = $(DEPDIR)/$(*F)
2986
2987#SRCS = foo.c bar.c ...
2988
2989#%.o : %.c
2990#	@$(MAKEDEPEND); \
2991#	  cp $(df).d $(df).P; \
2992#	  sed -e 's/#.*//' -e 's/^[^:]*: *//' -e 's/ *\\$$//' \
2993#	      -e '/^$$/ d' -e 's/$$/ :/' < $(df).d >> $(df).P; \
2994#	  rm -f $(df).d
2995#	$(COMPILE.c) -o $@ $<
2996
2997#-include $(SRCS:%.c=$(DEPDIR)/%.P)
2998
2999
3000#%.o : %.c
3001#	$(COMPILE.c) -MD -o $@ $<
3002#	@cp $*.d $*.P; \
3003#	  sed -e 's/#.*//' -e 's/^[^:]*: *//' -e 's/ *\\$$//' \
3004#	      -e '/^$$/ d' -e 's/$$/ :/' < $*.d >> $*.P; \
3005#	  rm -f $*.d
3006