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