definitions.mk revision a424bf781c1abd946acbc46aeb31bc1b1ef3e62e
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 :=
872ND_TARGET_DEPENDENCIES_ON_SHARED_LIBRARIES :=
88HOST_DEPENDENCIES_ON_SHARED_LIBRARIES :=
89
90# Generated class file names for Android resource.
91# They are escaped and quoted so can be passed safely to a bash command.
92ANDROID_RESOURCE_GENERATED_CLASSES := 'R.class' 'R$$*.class' 'Manifest.class' 'Manifest$$*.class'
93
94###########################################################
95## Debugging; prints a variable list to stdout
96###########################################################
97
98# $(1): variable name list, not variable values
99define print-vars
100$(foreach var,$(1), \
101  $(info $(var):) \
102  $(foreach word,$($(var)), \
103    $(info $(space)$(space)$(word)) \
104   ) \
105 )
106endef
107
108###########################################################
109## Evaluates to true if the string contains the word true,
110## and empty otherwise
111## $(1): a var to test
112###########################################################
113
114define true-or-empty
115$(filter true, $(1))
116endef
117
118
119###########################################################
120## Retrieve the directory of the current makefile
121###########################################################
122
123# Figure out where we are.
124define my-dir
125$(strip \
126  $(eval LOCAL_MODULE_MAKEFILE := $$(lastword $$(MAKEFILE_LIST))) \
127  $(if $(filter $(CLEAR_VARS),$(LOCAL_MODULE_MAKEFILE)), \
128    $(error LOCAL_PATH must be set before including $$(CLEAR_VARS)) \
129   , \
130    $(patsubst %/,%,$(dir $(LOCAL_MODULE_MAKEFILE))) \
131   ) \
132 )
133endef
134
135###########################################################
136## Retrieve a list of all makefiles immediately below some directory
137###########################################################
138
139define all-makefiles-under
140$(wildcard $(1)/*/Android.mk)
141endef
142
143###########################################################
144## Look under a directory for makefiles that don't have parent
145## makefiles.
146###########################################################
147
148# $(1): directory to search under
149# Ignores $(1)/Android.mk
150define first-makefiles-under
151$(shell build/tools/findleaves.py --prune=$(OUT_DIR) --prune=.repo --prune=.git \
152        --mindepth=2 $(1) Android.mk)
153endef
154
155###########################################################
156## Retrieve a list of all makefiles immediately below your directory
157###########################################################
158
159define all-subdir-makefiles
160$(call all-makefiles-under,$(call my-dir))
161endef
162
163###########################################################
164## Look in the named list of directories for makefiles,
165## relative to the current directory.
166###########################################################
167
168# $(1): List of directories to look for under this directory
169define all-named-subdir-makefiles
170$(wildcard $(addsuffix /Android.mk, $(addprefix $(call my-dir)/,$(1))))
171endef
172
173###########################################################
174## Find all of the java files under the named directories.
175## Meant to be used like:
176##    SRC_FILES := $(call all-java-files-under,src tests)
177###########################################################
178
179define all-java-files-under
180$(patsubst ./%,%, \
181  $(shell cd $(LOCAL_PATH) ; \
182          find -L $(1) -name "*.java" -and -not -name ".*") \
183 )
184endef
185
186###########################################################
187## Find all of the java files from here.  Meant to be used like:
188##    SRC_FILES := $(call all-subdir-java-files)
189###########################################################
190
191define all-subdir-java-files
192$(call all-java-files-under,.)
193endef
194
195###########################################################
196## Find all of the c files under the named directories.
197## Meant to be used like:
198##    SRC_FILES := $(call all-c-files-under,src tests)
199###########################################################
200
201define all-c-files-under
202$(patsubst ./%,%, \
203  $(shell cd $(LOCAL_PATH) ; \
204          find -L $(1) -name "*.c" -and -not -name ".*") \
205 )
206endef
207
208###########################################################
209## Find all of the c files from here.  Meant to be used like:
210##    SRC_FILES := $(call all-subdir-c-files)
211###########################################################
212
213define all-subdir-c-files
214$(call all-c-files-under,.)
215endef
216
217###########################################################
218## Find all files named "I*.aidl" under the named directories,
219## which must be relative to $(LOCAL_PATH).  The returned list
220## is relative to $(LOCAL_PATH).
221###########################################################
222
223define all-Iaidl-files-under
224$(patsubst ./%,%, \
225  $(shell cd $(LOCAL_PATH) ; \
226          find -L $(1) -name "I*.aidl" -and -not -name ".*") \
227 )
228endef
229
230###########################################################
231## Find all of the "I*.aidl" files under $(LOCAL_PATH).
232###########################################################
233
234define all-subdir-Iaidl-files
235$(call all-Iaidl-files-under,.)
236endef
237
238###########################################################
239## Find all of the logtags files under the named directories.
240## Meant to be used like:
241##    SRC_FILES := $(call all-logtags-files-under,src)
242###########################################################
243
244define all-logtags-files-under
245$(patsubst ./%,%, \
246  $(shell cd $(LOCAL_PATH) ; \
247          find -L $(1) -name "*.logtags" -and -not -name ".*") \
248  )
249endef
250
251###########################################################
252## Find all of the .proto files under the named directories.
253## Meant to be used like:
254##    SRC_FILES := $(call all-proto-files-under,src)
255###########################################################
256
257define all-proto-files-under
258$(patsubst ./%,%, \
259  $(shell cd $(LOCAL_PATH) ; \
260          find -L $(1) -name "*.proto" -and -not -name ".*") \
261  )
262endef
263
264###########################################################
265## Find all of the RenderScript files under the named directories.
266##  Meant to be used like:
267##    SRC_FILES := $(call all-renderscript-files-under,src)
268###########################################################
269
270define all-renderscript-files-under
271$(patsubst ./%,%, \
272  $(shell cd $(LOCAL_PATH) ; \
273          find -L $(1) \( -name "*.rs" -or -name "*.fs" \) -and -not -name ".*") \
274  )
275endef
276
277###########################################################
278## Find all of the html files under the named directories.
279## Meant to be used like:
280##    SRC_FILES := $(call all-html-files-under,src tests)
281###########################################################
282
283define all-html-files-under
284$(patsubst ./%,%, \
285  $(shell cd $(LOCAL_PATH) ; \
286          find -L $(1) -name "*.html" -and -not -name ".*") \
287 )
288endef
289
290###########################################################
291## Find all of the html files from here.  Meant to be used like:
292##    SRC_FILES := $(call all-subdir-html-files)
293###########################################################
294
295define all-subdir-html-files
296$(call all-html-files-under,.)
297endef
298
299###########################################################
300## Find all of the files matching pattern
301##    SRC_FILES := $(call find-subdir-files, <pattern>)
302###########################################################
303
304define find-subdir-files
305$(patsubst ./%,%,$(shell cd $(LOCAL_PATH) ; find -L $(1)))
306endef
307
308###########################################################
309# find the files in the subdirectory $1 of LOCAL_DIR
310# matching pattern $2, filtering out files $3
311# e.g.
312#     SRC_FILES += $(call find-subdir-subdir-files, \
313#                         css, *.cpp, DontWantThis.cpp)
314###########################################################
315
316define find-subdir-subdir-files
317$(filter-out $(patsubst %,$(1)/%,$(3)),$(patsubst ./%,%,$(shell cd \
318            $(LOCAL_PATH) ; find -L $(1) -maxdepth 1 -name $(2))))
319endef
320
321###########################################################
322## Find all of the files matching pattern
323##    SRC_FILES := $(call all-subdir-java-files)
324###########################################################
325
326define find-subdir-assets
327$(if $(1),$(patsubst ./%,%, \
328	$(shell if [ -d $(1) ] ; then cd $(1) ; find ./ -not -name '.*' -and -type f -and -not -type l ; fi)), \
329	$(warning Empty argument supplied to find-subdir-assets) \
330)
331endef
332
333###########################################################
334## Find various file types in a list of directories relative to $(LOCAL_PATH)
335###########################################################
336
337define find-other-java-files
338	$(call find-subdir-files,$(1) -name "*.java" -and -not -name ".*")
339endef
340
341define find-other-html-files
342	$(call find-subdir-files,$(1) -name "*.html" -and -not -name ".*")
343endef
344
345###########################################################
346# Use utility find to find given files in the given subdirs.
347# This function uses $(1), instead of LOCAL_PATH as the base.
348# $(1): the base dir, relative to the root of the source tree.
349# $(2): the file name pattern to be passed to find as "-name".
350# $(3): a list of subdirs of the base dir.
351# Returns: a list of paths relative to the base dir.
352###########################################################
353
354define find-files-in-subdirs
355$(patsubst ./%,%, \
356  $(shell cd $(1) ; \
357          find -L $(3) -name $(2) -and -not -name ".*") \
358 )
359endef
360
361###########################################################
362## Scan through each directory of $(1) looking for files
363## that match $(2) using $(wildcard).  Useful for seeing if
364## a given directory or one of its parents contains
365## a particular file.  Returns the first match found,
366## starting furthest from the root.
367###########################################################
368
369define find-parent-file
370$(strip \
371  $(eval _fpf := $(wildcard $(foreach f, $(2), $(strip $(1))/$(f)))) \
372  $(if $(_fpf),$(_fpf), \
373       $(if $(filter-out ./ .,$(1)), \
374             $(call find-parent-file,$(patsubst %/,%,$(dir $(1))),$(2)) \
375        ) \
376   ) \
377)
378endef
379
380###########################################################
381## Function we can evaluate to introduce a dynamic dependency
382###########################################################
383
384define add-dependency
385$(1): $(2)
386endef
387
388###########################################################
389## Set up the dependencies for a prebuilt target
390##  $(call add-prebuilt-file, srcfile, [targetclass])
391###########################################################
392
393define add-prebuilt-file
394    $(eval $(include-prebuilt))
395endef
396
397define include-prebuilt
398    include $$(CLEAR_VARS)
399    LOCAL_SRC_FILES := $(1)
400    LOCAL_BUILT_MODULE_STEM := $(1)
401    LOCAL_MODULE_SUFFIX := $$(suffix $(1))
402    LOCAL_MODULE := $$(basename $(1))
403    LOCAL_MODULE_CLASS := $(2)
404    include $$(BUILD_PREBUILT)
405endef
406
407###########################################################
408## do multiple prebuilts
409##  $(call target class, files ...)
410###########################################################
411
412define add-prebuilt-files
413    $(foreach f,$(2),$(call add-prebuilt-file,$f,$(1)))
414endef
415
416
417
418###########################################################
419## The intermediates directory.  Where object files go for
420## a given target.  We could technically get away without
421## the "_intermediates" suffix on the directory, but it's
422## nice to be able to grep for that string to find out if
423## anyone's abusing the system.
424###########################################################
425
426# $(1): target class, like "APPS"
427# $(2): target name, like "NotePad"
428# $(3): if non-empty, this is a HOST target.
429# $(4): if non-empty, force the intermediates to be COMMON
430# $(5): if non-empty, force the intermedistes to be for the 2nd arch
431define intermediates-dir-for
432$(strip \
433    $(eval _idfClass := $(strip $(1))) \
434    $(if $(_idfClass),, \
435        $(error $(LOCAL_PATH): Class not defined in call to intermediates-dir-for)) \
436    $(eval _idfName := $(strip $(2))) \
437    $(if $(_idfName),, \
438        $(error $(LOCAL_PATH): Name not defined in call to intermediates-dir-for)) \
439    $(eval _idfPrefix := $(if $(strip $(3)),HOST,TARGET)) \
440    $(eval _idf2ndArchPrefix := $(if $(strip $(5)),$(TARGET_2ND_ARCH_VAR_PREFIX))) \
441    $(if $(filter $(_idfPrefix)-$(_idfClass),$(COMMON_MODULE_CLASSES))$(4), \
442        $(eval _idfIntBase := $($(_idfPrefix)_OUT_COMMON_INTERMEDIATES)) \
443      , \
444        $(eval _idfIntBase := $($(_idf2ndArchPrefix)$(_idfPrefix)_OUT_INTERMEDIATES)) \
445     ) \
446    $(_idfIntBase)/$(_idfClass)/$(_idfName)_intermediates \
447)
448endef
449
450# Uses LOCAL_MODULE_CLASS, LOCAL_MODULE, and LOCAL_IS_HOST_MODULE
451# to determine the intermediates directory.
452#
453# $(1): if non-empty, force the intermediates to be COMMON
454# $(2): if non-empty, force the intermediates to be for the 2nd arch
455define local-intermediates-dir
456$(strip \
457    $(if $(strip $(LOCAL_MODULE_CLASS)),, \
458        $(error $(LOCAL_PATH): LOCAL_MODULE_CLASS not defined before call to local-intermediates-dir)) \
459    $(if $(strip $(LOCAL_MODULE)),, \
460        $(error $(LOCAL_PATH): LOCAL_MODULE not defined before call to local-intermediates-dir)) \
461    $(call intermediates-dir-for,$(LOCAL_MODULE_CLASS),$(LOCAL_MODULE),$(LOCAL_IS_HOST_MODULE),$(1),$(2)) \
462)
463endef
464
465###########################################################
466## The generated sources directory.  Placing generated
467## source files directly in the intermediates directory
468## causes problems for multiarch builds, where there are
469## two intermediates directories for a single target. Put
470## them in a separate directory, and they will be copied to
471## each intermediates directory automatically.
472###########################################################
473
474# $(1): target class, like "APPS"
475# $(2): target name, like "NotePad"
476# $(3): if non-empty, this is a HOST target.
477# $(4): if non-empty, force the generated sources to be COMMON
478define generated-sources-dir-for
479$(strip \
480    $(eval _idfClass := $(strip $(1))) \
481    $(if $(_idfClass),, \
482        $(error $(LOCAL_PATH): Class not defined in call to generated-sources-dir-for)) \
483    $(eval _idfName := $(strip $(2))) \
484    $(if $(_idfName),, \
485        $(error $(LOCAL_PATH): Name not defined in call to generated-sources-dir-for)) \
486    $(eval _idfPrefix := $(if $(strip $(3)),HOST,TARGET)) \
487    $(if $(filter $(_idfPrefix)-$(_idfClass),$(COMMON_MODULE_CLASSES))$(4), \
488        $(eval _idfIntBase := $($(_idfPrefix)_OUT_GEN_COMMON)) \
489      , \
490        $(eval _idfIntBase := $($(_idfPrefix)_OUT_GEN)) \
491     ) \
492    $(_idfIntBase)/$(_idfClass)/$(_idfName)_intermediates \
493)
494endef
495
496# Uses LOCAL_MODULE_CLASS, LOCAL_MODULE, and LOCAL_IS_HOST_MODULE
497# to determine the generated sources directory.
498#
499# $(1): if non-empty, force the intermediates to be COMMON
500define local-generated-sources-dir
501$(strip \
502    $(if $(strip $(LOCAL_MODULE_CLASS)),, \
503        $(error $(LOCAL_PATH): LOCAL_MODULE_CLASS not defined before call to local-generated-sources-dir)) \
504    $(if $(strip $(LOCAL_MODULE)),, \
505        $(error $(LOCAL_PATH): LOCAL_MODULE not defined before call to local-generated-sources-dir)) \
506    $(call generated-sources-dir-for,$(LOCAL_MODULE_CLASS),$(LOCAL_MODULE),$(LOCAL_IS_HOST_MODULE),$(1)) \
507)
508endef
509
510###########################################################
511## Convert "path/to/libXXX.so" to "-lXXX".
512## Any "path/to/libXXX.a" elements pass through unchanged.
513###########################################################
514
515define normalize-libraries
516$(foreach so,$(filter %.so,$(1)),-l$(patsubst lib%.so,%,$(notdir $(so))))\
517$(filter-out %.so,$(1))
518endef
519
520# TODO: change users to call the common version.
521define normalize-host-libraries
522$(call normalize-libraries,$(1))
523endef
524
525define normalize-target-libraries
526$(call normalize-libraries,$(1))
527endef
528
529###########################################################
530## Convert a list of short module names (e.g., "framework", "Browser")
531## into the list of files that are built for those modules.
532## NOTE: this won't return reliable results until after all
533## sub-makefiles have been included.
534## $(1): target list
535###########################################################
536
537define module-built-files
538$(foreach module,$(1),$(ALL_MODULES.$(module).BUILT))
539endef
540
541###########################################################
542## Convert a list of short modules names (e.g., "framework", "Browser")
543## into the list of files that are installed for those modules.
544## NOTE: this won't return reliable results until after all
545## sub-makefiles have been included.
546## $(1): target list
547###########################################################
548
549define module-installed-files
550$(foreach module,$(1),$(ALL_MODULES.$(module).INSTALLED))
551endef
552
553###########################################################
554## Convert a list of short modules names (e.g., "framework", "Browser")
555## into the list of files that should be used when linking
556## against that module as a public API.
557## TODO: Allow this for more than JAVA_LIBRARIES modules
558## NOTE: this won't return reliable results until after all
559## sub-makefiles have been included.
560## $(1): target list
561###########################################################
562
563define module-stubs-files
564$(foreach module,$(1),$(ALL_MODULES.$(module).STUBS))
565endef
566
567###########################################################
568## Evaluates to the timestamp file for a doc module, which
569## is the dependency that should be used.
570## $(1): doc module
571###########################################################
572
573define doc-timestamp-for
574$(OUT_DOCS)/$(strip $(1))-timestamp
575endef
576
577
578###########################################################
579## Convert "core ext framework" to "out/.../javalib.jar ..."
580## $(1): library list
581## $(2): Non-empty if IS_HOST_MODULE
582###########################################################
583
584# $(1): library name
585# $(2): Non-empty if IS_HOST_MODULE
586define _java-lib-dir
587$(call intermediates-dir-for, \
588	JAVA_LIBRARIES,$(1),$(2),COMMON)
589endef
590
591# $(1): library name
592# $(2): Non-empty if IS_HOST_MODULE
593define _java-lib-full-classes.jar
594$(call _java-lib-dir,$(1),$(2))/classes$(COMMON_JAVA_PACKAGE_SUFFIX)
595endef
596
597# $(1): library name list
598# $(2): Non-empty if IS_HOST_MODULE
599define java-lib-files
600$(foreach lib,$(1),$(call _java-lib-full-classes.jar,$(lib),$(2)))
601endef
602
603# $(1): library name
604# $(2): Non-empty if IS_HOST_MODULE
605define _java-lib-full-dep
606$(call _java-lib-dir,$(1),$(2))/$(if $(2),javalib,classes)$(COMMON_JAVA_PACKAGE_SUFFIX)
607endef
608
609# $(1): library name list
610# $(2): Non-empty if IS_HOST_MODULE
611define java-lib-deps
612$(foreach lib,$(1),$(call _java-lib-full-dep,$(lib),$(2)))
613endef
614
615###########################################################
616## Run rot13 on a string
617## $(1): the string.  Must be one line.
618###########################################################
619define rot13
620$(shell echo $(1) | tr 'a-zA-Z' 'n-za-mN-ZA-M')
621endef
622
623
624###########################################################
625## Returns true if $(1) and $(2) are equal.  Returns
626## the empty string if they are not equal.
627###########################################################
628define streq
629$(strip $(if $(strip $(1)),\
630  $(if $(strip $(2)),\
631    $(if $(filter-out __,_$(subst $(strip $(1)),,$(strip $(2)))$(subst $(strip $(2)),,$(strip $(1)))_),,true), \
632    ),\
633  $(if $(strip $(2)),\
634    ,\
635    true)\
636 ))
637endef
638
639###########################################################
640## Convert "a b c" into "a:b:c"
641###########################################################
642define normalize-path-list
643$(subst $(space),:,$(strip $(1)))
644endef
645
646###########################################################
647## Read the word out of a colon-separated list of words.
648## This has the same behavior as the built-in function
649## $(word n,str).
650##
651## The individual words may not contain spaces.
652##
653## $(1): 1 based index
654## $(2): value of the form a:b:c...
655###########################################################
656
657define word-colon
658$(word $(1),$(subst :,$(space),$(2)))
659endef
660
661###########################################################
662## Convert "a=b c= d e = f" into "a=b c=d e=f"
663##
664## $(1): list to collapse
665## $(2): if set, separator word; usually "=", ":", or ":="
666##       Defaults to "=" if not set.
667###########################################################
668
669define collapse-pairs
670$(eval _cpSEP := $(strip $(if $(2),$(2),=)))\
671$(subst $(space)$(_cpSEP)$(space),$(_cpSEP),$(strip \
672    $(subst $(_cpSEP), $(_cpSEP) ,$(1))))
673endef
674
675###########################################################
676## Given a list of pairs, if multiple pairs have the same
677## first components, keep only the first pair.
678##
679## $(1): list of pairs
680## $(2): the separator word, such as ":", "=", etc.
681define uniq-pairs-by-first-component
682$(eval _upbfc_fc_set :=)\
683$(strip $(foreach w,$(1), $(eval _first := $(word 1,$(subst $(2),$(space),$(w))))\
684    $(if $(filter $(_upbfc_fc_set),$(_first)),,$(w)\
685        $(eval _upbfc_fc_set += $(_first)))))\
686$(eval _upbfc_fc_set :=)\
687$(eval _first:=)
688endef
689
690###########################################################
691## MODULE_TAG set operations
692###########################################################
693
694# Given a list of tags, return the targets that specify
695# any of those tags.
696# $(1): tag list
697define modules-for-tag-list
698$(sort $(foreach tag,$(1),$(ALL_MODULE_TAGS.$(tag))))
699endef
700
701# Same as modules-for-tag-list, but operates on
702# ALL_MODULE_NAME_TAGS.
703# $(1): tag list
704define module-names-for-tag-list
705$(sort $(foreach tag,$(1),$(ALL_MODULE_NAME_TAGS.$(tag))))
706endef
707
708# Given an accept and reject list, find the matching
709# set of targets.  If a target has multiple tags and
710# any of them are rejected, the target is rejected.
711# Reject overrides accept.
712# $(1): list of tags to accept
713# $(2): list of tags to reject
714#TODO(dbort): do $(if $(strip $(1)),$(1),$(ALL_MODULE_TAGS))
715#TODO(jbq): as of 20100106 nobody uses the second parameter
716define get-tagged-modules
717$(filter-out \
718	$(call modules-for-tag-list,$(2)), \
719	    $(call modules-for-tag-list,$(1)))
720endef
721
722###########################################################
723## Append a leaf to a base path.  Properly deals with
724## base paths ending in /.
725##
726## $(1): base path
727## $(2): leaf path
728###########################################################
729
730define append-path
731$(subst //,/,$(1)/$(2))
732endef
733
734
735###########################################################
736## Package filtering
737###########################################################
738
739# Given a list of installed modules (short or long names)
740# return a list of the packages (yes, .apk packages, not
741# modules in general) that are overridden by this list and,
742# therefore, should not be installed.
743# $(1): mixed list of installed modules
744# TODO: This is fragile; find a reliable way to get this information.
745define _get-package-overrides
746 $(eval ### Discard any words containing slashes, unless they end in .apk, \
747        ### in which case trim off the directory component and the suffix. \
748        ### If there are no slashes, keep the entire word.)
749 $(eval _gpo_names := $(subst /,@@@ @@@,$(1)))
750 $(eval _gpo_names := \
751     $(filter %.apk,$(_gpo_names)) \
752     $(filter-out %@@@ @@@%,$(_gpo_names)))
753 $(eval _gpo_names := $(patsubst %.apk,%,$(_gpo_names)))
754 $(eval _gpo_names := $(patsubst @@@%,%,$(_gpo_names)))
755
756 $(eval ### Remove any remaining words that contain dots.)
757 $(eval _gpo_names := $(subst .,@@@ @@@,$(_gpo_names)))
758 $(eval _gpo_names := $(filter-out %@@@ @@@%,$(_gpo_names)))
759
760 $(eval ### Now we have a list of any words that could possibly refer to \
761        ### packages, although there may be words that do not.  Only \
762        ### real packages will be present under PACKAGES.*, though.)
763 $(foreach _gpo_name,$(_gpo_names),$(PACKAGES.$(_gpo_name).OVERRIDES))
764endef
765
766define get-package-overrides
767$(sort $(strip $(call _get-package-overrides,$(1))))
768endef
769
770###########################################################
771## Output the command lines, or not
772###########################################################
773
774ifeq ($(strip $(SHOW_COMMANDS)),)
775define pretty
776@echo $1
777endef
778hide := @
779else
780define pretty
781endef
782hide :=
783endif
784
785###########################################################
786## Dump the variables that are associated with targets
787###########################################################
788
789define dump-module-variables
790@echo all_dependencies=$^
791@echo PRIVATE_YACCFLAGS=$(PRIVATE_YACCFLAGS);
792@echo PRIVATE_CFLAGS=$(PRIVATE_CFLAGS);
793@echo PRIVATE_CPPFLAGS=$(PRIVATE_CPPFLAGS);
794@echo PRIVATE_DEBUG_CFLAGS=$(PRIVATE_DEBUG_CFLAGS);
795@echo PRIVATE_C_INCLUDES=$(PRIVATE_C_INCLUDES);
796@echo PRIVATE_LDFLAGS=$(PRIVATE_LDFLAGS);
797@echo PRIVATE_LDLIBS=$(PRIVATE_LDLIBS);
798@echo PRIVATE_ARFLAGS=$(PRIVATE_ARFLAGS);
799@echo PRIVATE_AAPT_FLAGS=$(PRIVATE_AAPT_FLAGS);
800@echo PRIVATE_DX_FLAGS=$(PRIVATE_DX_FLAGS);
801@echo PRIVATE_JAVACFLAGS=$(PRIVATE_JAVACFLAGS);
802@echo PRIVATE_JAVA_LIBRARIES=$(PRIVATE_JAVA_LIBRARIES);
803@echo PRIVATE_ALL_SHARED_LIBRARIES=$(PRIVATE_ALL_SHARED_LIBRARIES);
804@echo PRIVATE_ALL_STATIC_LIBRARIES=$(PRIVATE_ALL_STATIC_LIBRARIES);
805@echo PRIVATE_ALL_WHOLE_STATIC_LIBRARIES=$(PRIVATE_ALL_WHOLE_STATIC_LIBRARIES);
806@echo PRIVATE_ALL_OBJECTS=$(PRIVATE_ALL_OBJECTS);
807@echo PRIVATE_NO_CRT=$(PRIVATE_NO_CRT);
808endef
809
810###########################################################
811## Commands for using sed to replace given variable values
812###########################################################
813
814define transform-variables
815@mkdir -p $(dir $@)
816@echo "Sed: $(if $(PRIVATE_MODULE),$(PRIVATE_MODULE),$@) <= $<"
817$(hide) sed $(foreach var,$(REPLACE_VARS),-e "s/{{$(var)}}/$(subst /,\/,$(PWD)/$($(var)))/g") $< >$@
818$(hide) if [ "$(suffix $@)" = ".sh" ]; then chmod a+rx $@; fi
819endef
820
821
822###########################################################
823## Commands for munging the dependency files GCC generates
824###########################################################
825# $(1): the input .d file
826# $(2): the output .P file
827define transform-d-to-p-args
828$(hide) cp $(1) $(2); \
829	sed -e 's/#.*//' -e 's/^[^:]*: *//' -e 's/ *\\$$//' \
830		-e '/^$$/ d' -e 's/$$/ :/' < $(1) >> $(2); \
831	rm -f $(1)
832endef
833
834define transform-d-to-p
835$(call transform-d-to-p-args,$(@:%.o=%.d),$(@:%.o=%.P))
836endef
837
838###########################################################
839## Commands for running lex
840###########################################################
841
842define transform-l-to-cpp
843@mkdir -p $(dir $@)
844@echo "Lex: $(PRIVATE_MODULE) <= $<"
845$(hide) $(LEX) -o$@ $<
846endef
847
848###########################################################
849## Commands for running yacc
850##
851## Because the extension of c++ files can change, the
852## extension must be specified in $1.
853## E.g, "$(call transform-y-to-cpp,.cpp)"
854###########################################################
855
856define transform-y-to-cpp
857@mkdir -p $(dir $@)
858@echo "Yacc: $(PRIVATE_MODULE) <= $<"
859$(YACC) $(PRIVATE_YACCFLAGS) -o $@ $<
860touch $(@:$1=$(YACC_HEADER_SUFFIX))
861echo '#ifndef '$(@F:$1=_h) > $(@:$1=.h)
862echo '#define '$(@F:$1=_h) >> $(@:$1=.h)
863cat $(@:$1=$(YACC_HEADER_SUFFIX)) >> $(@:$1=.h)
864echo '#endif' >> $(@:$1=.h)
865rm -f $(@:$1=$(YACC_HEADER_SUFFIX))
866endef
867
868###########################################################
869## Commands to compile RenderScript to Java
870###########################################################
871
872define transform-renderscripts-to-java-and-bc
873@echo "RenderScript: $(PRIVATE_MODULE) <= $(PRIVATE_RS_SOURCE_FILES)"
874$(hide) rm -rf $(PRIVATE_RS_OUTPUT_DIR)
875$(hide) mkdir -p $(PRIVATE_RS_OUTPUT_DIR)/res/raw
876$(hide) mkdir -p $(PRIVATE_RS_OUTPUT_DIR)/src
877$(hide) $(PRIVATE_RS_CC) \
878  -o $(PRIVATE_RS_OUTPUT_DIR)/res/raw \
879  -p $(PRIVATE_RS_OUTPUT_DIR)/src \
880  -d $(PRIVATE_RS_OUTPUT_DIR) \
881  -a $@ -MD \
882  $(addprefix -target-api , $(PRIVATE_RS_TARGET_API)) \
883  $(PRIVATE_RS_FLAGS) \
884  $(foreach inc,$(PRIVATE_RS_INCLUDES),$(addprefix -I , $(inc))) \
885  $(PRIVATE_RS_SOURCE_FILES)
886$(hide) mkdir -p $(dir $@)
887$(hide) touch $@
888endef
889
890define transform-bc-to-so
891@echo "Renderscript compatibility: $(notdir $@) <= $(notdir $<)"
892$(hide) mkdir -p $(dir $@)
893$(hide) $(BCC_COMPAT) -O3 -o $(dir $@)/$(notdir $(<:.bc=.o)) -fPIC -shared \
894	-rt-path $(RS_PREBUILT_CLCORE) -mtriple $(RS_TRIPLE) $<
895$(hide) $(PRIVATE_CXX) -shared -Wl,-soname,$(notdir $@) -nostdlib \
896	-Wl,-rpath,\$$ORIGIN/../lib \
897	$(dir $@)/$(notdir $(<:.bc=.o)) \
898	$(RS_PREBUILT_COMPILER_RT) \
899	-o $@ -L prebuilts/gcc/ \
900	-L $(TARGET_OUT_INTERMEDIATE_LIBRARIES) $(RS_PREBUILT_LIBPATH) \
901	-lRSSupport -lm -lc
902endef
903
904###########################################################
905## Commands to compile RenderScript to C++
906###########################################################
907
908define transform-renderscripts-to-cpp-and-bc
909@echo "RenderScript: $(PRIVATE_MODULE) <= $(PRIVATE_RS_SOURCE_FILES)"
910$(hide) rm -rf $(PRIVATE_RS_OUTPUT_DIR)
911$(hide) mkdir -p $(PRIVATE_RS_OUTPUT_DIR)/
912$(hide) $(PRIVATE_RS_CC) \
913  -o $(PRIVATE_RS_OUTPUT_DIR)/ \
914  -d $(PRIVATE_RS_OUTPUT_DIR) \
915  -a $@ -MD \
916  -reflect-c++ \
917  $(PRIVATE_RS_FLAGS) \
918  $(foreach inc,$(PRIVATE_RS_INCLUDES),$(addprefix -I , $(inc))) \
919  $(PRIVATE_RS_SOURCE_FILES)
920$(hide) mkdir -p $(dir $@)
921$(hide) touch $@
922endef
923
924
925###########################################################
926## Commands for running aidl
927###########################################################
928
929define transform-aidl-to-java
930@mkdir -p $(dir $@)
931@echo "Aidl: $(PRIVATE_MODULE) <= $<"
932$(hide) $(AIDL) -d$(patsubst %.java,%.P,$@) $(PRIVATE_AIDL_FLAGS) $< $@
933endef
934#$(AIDL) $(PRIVATE_AIDL_FLAGS) $< - | indent -nut -br -npcs -l1000 > $@
935
936
937###########################################################
938## Commands for running java-event-log-tags.py
939###########################################################
940
941define transform-logtags-to-java
942@mkdir -p $(dir $@)
943@echo "logtags: $@ <= $<"
944$(hide) $(JAVATAGS) -o $@ $^
945endef
946
947
948###########################################################
949## Commands for running protoc to compile .proto into .java
950###########################################################
951
952define transform-proto-to-java
953@mkdir -p $(dir $@)
954@echo "Protoc: $@ <= $(PRIVATE_PROTO_SRC_FILES)"
955@rm -rf $(PRIVATE_PROTO_JAVA_OUTPUT_DIR)
956@mkdir -p $(PRIVATE_PROTO_JAVA_OUTPUT_DIR)
957$(hide) for f in $(PRIVATE_PROTO_SRC_FILES); do \
958        $(PROTOC) \
959        $(addprefix --proto_path=, $(PRIVATE_PROTO_INCLUDES)) \
960        $(PRIVATE_PROTO_JAVA_OUTPUT_OPTION)="$(PRIVATE_PROTO_JAVA_OUTPUT_PARAMS):$(PRIVATE_PROTO_JAVA_OUTPUT_DIR)" \
961        $(PRIVATE_PROTOC_FLAGS) \
962        $$f || exit 33; \
963        done
964$(hide) touch $@
965endef
966
967######################################################################
968## Commands for running protoc to compile .proto into .pb.cc and .pb.h
969######################################################################
970define transform-proto-to-cc
971@mkdir -p $(dir $@)
972@echo "Protoc: $@ <= $<"
973$(hide) $(PROTOC) \
974	$(addprefix --proto_path=, $(PRIVATE_PROTO_INCLUDES)) \
975	$(PRIVATE_PROTOC_FLAGS) \
976	--cpp_out=$(PRIVATE_PROTO_CC_OUTPUT_DIR) $<
977endef
978
979
980###########################################################
981## Commands for running gcc to compile a C++ file
982###########################################################
983
984define transform-cpp-to-o
985@mkdir -p $(dir $@)
986@echo "target $(PRIVATE_ARM_MODE) C++: $(PRIVATE_MODULE) <= $<"
987$(hide) $(PRIVATE_CXX) \
988	$(addprefix -I , $(PRIVATE_C_INCLUDES)) \
989	$(shell cat $(PRIVATE_IMPORT_INCLUDES)) \
990	$(addprefix -isystem ,\
991	    $(if $(PRIVATE_NO_DEFAULT_COMPILER_FLAGS),, \
992	        $(filter-out $(PRIVATE_C_INCLUDES), \
993	            $(PRIVATE_TARGET_PROJECT_INCLUDES) \
994	            $(PRIVATE_TARGET_C_INCLUDES)))) \
995	-c \
996	$(if $(PRIVATE_NO_DEFAULT_COMPILER_FLAGS),, \
997	    $(PRIVATE_TARGET_GLOBAL_CFLAGS) \
998	    $(PRIVATE_TARGET_GLOBAL_CPPFLAGS) \
999	    $(PRIVATE_ARM_CFLAGS) \
1000	 ) \
1001	$(PRIVATE_RTTI_FLAG) \
1002	$(PRIVATE_CFLAGS) \
1003	$(PRIVATE_CPPFLAGS) \
1004	$(PRIVATE_DEBUG_CFLAGS) \
1005	-MD -MF $(patsubst %.o,%.d,$@) -o $@ $<
1006$(transform-d-to-p)
1007endef
1008
1009
1010###########################################################
1011## Commands for running gcc to compile a C file
1012###########################################################
1013
1014# $(1): extra flags
1015define transform-c-or-s-to-o-no-deps
1016@mkdir -p $(dir $@)
1017$(hide) $(PRIVATE_CC) \
1018	$(addprefix -I , $(PRIVATE_C_INCLUDES)) \
1019	$(shell cat $(PRIVATE_IMPORT_INCLUDES)) \
1020	$(addprefix -isystem ,\
1021	    $(if $(PRIVATE_NO_DEFAULT_COMPILER_FLAGS),, \
1022	        $(filter-out $(PRIVATE_C_INCLUDES), \
1023	            $(PRIVATE_TARGET_PROJECT_INCLUDES) \
1024	            $(PRIVATE_TARGET_C_INCLUDES)))) \
1025	-c \
1026	$(if $(PRIVATE_NO_DEFAULT_COMPILER_FLAGS),, \
1027	    $(PRIVATE_TARGET_GLOBAL_CFLAGS) \
1028	    $(PRIVATE_ARM_CFLAGS) \
1029	 ) \
1030	 $(1) \
1031	-MD -MF $(patsubst %.o,%.d,$@) -o $@ $<
1032endef
1033
1034define transform-c-to-o-no-deps
1035@echo "target $(PRIVATE_ARM_MODE) C: $(PRIVATE_MODULE) <= $<"
1036$(call transform-c-or-s-to-o-no-deps, $(PRIVATE_CFLAGS) $(PRIVATE_CONLYFLAGS) $(PRIVATE_DEBUG_CFLAGS))
1037endef
1038
1039define transform-s-to-o-no-deps
1040@echo "target asm: $(PRIVATE_MODULE) <= $<"
1041$(call transform-c-or-s-to-o-no-deps, $(PRIVATE_ASFLAGS))
1042endef
1043
1044define transform-c-to-o
1045$(transform-c-to-o-no-deps)
1046$(transform-d-to-p)
1047endef
1048
1049define transform-s-to-o
1050$(transform-s-to-o-no-deps)
1051$(transform-d-to-p)
1052endef
1053
1054###########################################################
1055## Commands for running gcc to compile an Objective-C file
1056## This should never happen for target builds but this
1057## will error at build time.
1058###########################################################
1059
1060define transform-m-to-o-no-deps
1061@echo "target ObjC: $(PRIVATE_MODULE) <= $<"
1062$(call transform-c-or-s-to-o-no-deps, $(PRIVATE_CFLAGS) $(PRIVATE_DEBUG_CFLAGS))
1063endef
1064
1065define transform-m-to-o
1066$(transform-m-to-o-no-deps)
1067$(transform-d-to-p)
1068endef
1069
1070###########################################################
1071## Commands for running gcc to compile a host C++ file
1072###########################################################
1073
1074define transform-host-cpp-to-o
1075@mkdir -p $(dir $@)
1076@echo "host C++: $(PRIVATE_MODULE) <= $<"
1077$(hide) $(PRIVATE_CXX) \
1078	$(addprefix -I , $(PRIVATE_C_INCLUDES)) \
1079	$(shell cat $(PRIVATE_IMPORT_INCLUDES)) \
1080	$(addprefix -isystem ,\
1081	    $(if $(PRIVATE_NO_DEFAULT_COMPILER_FLAGS),, \
1082	        $(filter-out $(PRIVATE_C_INCLUDES), \
1083	            $(HOST_PROJECT_INCLUDES) \
1084	            $(PRIVATE_HOST_C_INCLUDES)))) \
1085	-c \
1086	$(if $(PRIVATE_NO_DEFAULT_COMPILER_FLAGS),, \
1087	    $(PRIVATE_HOST_GLOBAL_CFLAGS) \
1088	    $(PRIVATE_HOST_GLOBAL_CPPFLAGS) \
1089	 ) \
1090	$(PRIVATE_CFLAGS) \
1091	$(PRIVATE_CPPFLAGS) \
1092	$(PRIVATE_DEBUG_CFLAGS) \
1093	-MD -MF $(patsubst %.o,%.d,$@) -o $@ $<
1094$(transform-d-to-p)
1095endef
1096
1097
1098###########################################################
1099## Commands for running gcc to compile a host C file
1100###########################################################
1101
1102# $(1): extra flags
1103define transform-host-c-or-s-to-o-no-deps
1104@mkdir -p $(dir $@)
1105$(hide) $(PRIVATE_CC) \
1106	$(addprefix -I , $(PRIVATE_C_INCLUDES)) \
1107	$(shell cat $(PRIVATE_IMPORT_INCLUDES)) \
1108	$(addprefix -isystem ,\
1109	    $(if $(PRIVATE_NO_DEFAULT_COMPILER_FLAGS),, \
1110	        $(filter-out $(PRIVATE_C_INCLUDES), \
1111	            $(HOST_PROJECT_INCLUDES) \
1112	            $(PRIVATE_HOST_C_INCLUDES)))) \
1113	-c \
1114	$(if $(PRIVATE_NO_DEFAULT_COMPILER_FLAGS),, \
1115	    $(PRIVATE_HOST_GLOBAL_CFLAGS) \
1116	 ) \
1117	$(1) \
1118	-MD -MF $(patsubst %.o,%.d,$@) -o $@ $<
1119endef
1120
1121define transform-host-c-to-o-no-deps
1122@echo "host C: $(PRIVATE_MODULE) <= $<"
1123$(call transform-host-c-or-s-to-o-no-deps, $(PRIVATE_CFLAGS) $(PRIVATE_CONLYFLAGS) $(PRIVATE_DEBUG_CFLAGS))
1124endef
1125
1126define transform-host-s-to-o-no-deps
1127@echo "host asm: $(PRIVATE_MODULE) <= $<"
1128$(call transform-host-c-or-s-to-o-no-deps, $(PRIVATE_ASFLAGS))
1129endef
1130
1131define transform-host-c-to-o
1132$(transform-host-c-to-o-no-deps)
1133$(transform-d-to-p)
1134endef
1135
1136define transform-host-s-to-o
1137$(transform-host-s-to-o-no-deps)
1138$(transform-d-to-p)
1139endef
1140
1141###########################################################
1142## Commands for running gcc to compile a host Objective-C file
1143###########################################################
1144
1145define transform-host-m-to-o-no-deps
1146@echo "host ObjC: $(PRIVATE_MODULE) <= $<"
1147$(call transform-host-c-or-s-to-o-no-deps, $(PRIVATE_CFLAGS) $(PRIVATE_DEBUG_CFLAGS))
1148endef
1149
1150define transform-host-m-to-o
1151$(transform-host-m-to-o-no-deps)
1152$(transform-d-to-p)
1153endef
1154
1155###########################################################
1156## Commands for running ar
1157###########################################################
1158
1159define _concat-if-arg2-not-empty
1160$(if $(2),$(hide) $(1) $(2))
1161endef
1162
1163# Split long argument list into smaller groups and call the command repeatedly
1164# Call the command at least once even if there are no arguments, as otherwise
1165# the output file won't be created.
1166#
1167# $(1): the command without arguments
1168# $(2): the arguments
1169define split-long-arguments
1170$(hide) $(1) $(wordlist 1,500,$(2))
1171$(call _concat-if-arg2-not-empty,$(1),$(wordlist 501,1000,$(2)))
1172$(call _concat-if-arg2-not-empty,$(1),$(wordlist 1001,1500,$(2)))
1173$(call _concat-if-arg2-not-empty,$(1),$(wordlist 1501,2000,$(2)))
1174$(call _concat-if-arg2-not-empty,$(1),$(wordlist 2001,2500,$(2)))
1175$(call _concat-if-arg2-not-empty,$(1),$(wordlist 2501,3000,$(2)))
1176$(call _concat-if-arg2-not-empty,$(1),$(wordlist 3001,99999,$(2)))
1177endef
1178
1179# $(1): the full path of the source static library.
1180define _extract-and-include-single-target-whole-static-lib
1181@echo "preparing StaticLib: $(PRIVATE_MODULE) [including $(1)]"
1182$(hide) ldir=$(PRIVATE_INTERMEDIATES_DIR)/WHOLE/$(basename $(notdir $(1)))_objs;\
1183    rm -rf $$ldir; \
1184    mkdir -p $$ldir; \
1185    filelist=; \
1186    for f in `$(TARGET_AR) t $(1)`; do \
1187        $($(PRIVATE_2ND_ARCH_VAR_PREFIX)TARGET_AR) p $(1) $$f > $$ldir/$$f; \
1188        filelist="$$filelist $$ldir/$$f"; \
1189    done ; \
1190    $($(PRIVATE_2ND_ARCH_VAR_PREFIX)TARGET_AR) $($(PRIVATE_2ND_ARCH_VAR_PREFIX)TARGET_GLOBAL_ARFLAGS) \
1191        $(PRIVATE_ARFLAGS) $@ $$filelist
1192
1193endef
1194
1195define extract-and-include-target-whole-static-libs
1196$(foreach lib,$(PRIVATE_ALL_WHOLE_STATIC_LIBRARIES), \
1197    $(call _extract-and-include-single-target-whole-static-lib, $(lib)))
1198endef
1199
1200# Explicitly delete the archive first so that ar doesn't
1201# try to add to an existing archive.
1202define transform-o-to-static-lib
1203@mkdir -p $(dir $@)
1204@rm -f $@
1205$(extract-and-include-target-whole-static-libs)
1206@echo "target StaticLib: $(PRIVATE_MODULE) ($@)"
1207$(call split-long-arguments,$($(PRIVATE_2ND_ARCH_VAR_PREFIX)TARGET_AR) $($(PRIVATE_2ND_ARCH_VAR_PREFIX)TARGET_GLOBAL_ARFLAGS) \
1208    $(PRIVATE_ARFLAGS) $@,$(filter %.o, $^))
1209endef
1210
1211###########################################################
1212## Commands for running host ar
1213###########################################################
1214
1215# $(1): the full path of the source static library.
1216define _extract-and-include-single-host-whole-static-lib
1217@echo "preparing StaticLib: $(PRIVATE_MODULE) [including $(1)]"
1218$(hide) ldir=$(PRIVATE_INTERMEDIATES_DIR)/WHOLE/$(basename $(notdir $(1)))_objs;\
1219    rm -rf $$ldir; \
1220    mkdir -p $$ldir; \
1221    filelist=; \
1222    for f in `$(HOST_AR) t $(1) | \grep '\.o$$'`; do \
1223        $(HOST_AR) p $(1) $$f > $$ldir/$$f; \
1224        filelist="$$filelist $$ldir/$$f"; \
1225    done ; \
1226    $(HOST_AR) $(HOST_GLOBAL_ARFLAGS) $(PRIVATE_ARFLAGS) $@ $$filelist
1227
1228endef
1229
1230define extract-and-include-host-whole-static-libs
1231$(foreach lib,$(PRIVATE_ALL_WHOLE_STATIC_LIBRARIES), \
1232    $(call _extract-and-include-single-host-whole-static-lib, $(lib)))
1233endef
1234
1235# Explicitly delete the archive first so that ar doesn't
1236# try to add to an existing archive.
1237define transform-host-o-to-static-lib
1238@mkdir -p $(dir $@)
1239@rm -f $@
1240$(extract-and-include-host-whole-static-libs)
1241@echo "host StaticLib: $(PRIVATE_MODULE) ($@)"
1242$(call split-long-arguments,$(HOST_AR) $(HOST_GLOBAL_ARFLAGS) $(PRIVATE_ARFLAGS) $@,$(filter %.o, $^))
1243endef
1244
1245
1246###########################################################
1247## Commands for running gcc to link a shared library or package
1248###########################################################
1249
1250# ld just seems to be so finicky with command order that we allow
1251# it to be overriden en-masse see combo/linux-arm.make for an example.
1252ifneq ($(HOST_CUSTOM_LD_COMMAND),true)
1253define transform-host-o-to-shared-lib-inner
1254$(hide) $(PRIVATE_CXX) \
1255	-Wl,-rpath-link=$(HOST_OUT_INTERMEDIATE_LIBRARIES) \
1256	-Wl,-rpath,\$$ORIGIN/../lib \
1257	-shared -Wl,-soname,$(notdir $@) \
1258	$(HOST_GLOBAL_LD_DIRS) \
1259	$(if $(PRIVATE_NO_DEFAULT_COMPILER_FLAGS),, \
1260	   $(PRIVATE_HOST_GLOBAL_LDFLAGS) \
1261	) \
1262	$(PRIVATE_LDFLAGS) \
1263	$(PRIVATE_ALL_OBJECTS) \
1264	-Wl,--whole-archive \
1265	$(call normalize-host-libraries,$(PRIVATE_ALL_WHOLE_STATIC_LIBRARIES)) \
1266	-Wl,--no-whole-archive \
1267	$(if $(PRIVATE_GROUP_STATIC_LIBRARIES),-Wl$(comma)--start-group) \
1268	$(call normalize-host-libraries,$(PRIVATE_ALL_STATIC_LIBRARIES)) \
1269	$(if $(PRIVATE_GROUP_STATIC_LIBRARIES),-Wl$(comma)--end-group) \
1270	$(call normalize-host-libraries,$(PRIVATE_ALL_SHARED_LIBRARIES)) \
1271	-o $@ \
1272	$(PRIVATE_LDLIBS)
1273endef
1274endif
1275
1276define transform-host-o-to-shared-lib
1277@mkdir -p $(dir $@)
1278@echo "host SharedLib: $(PRIVATE_MODULE) ($@)"
1279$(transform-host-o-to-shared-lib-inner)
1280endef
1281
1282define transform-host-o-to-package
1283@mkdir -p $(dir $@)
1284@echo "host Package: $(PRIVATE_MODULE) ($@)"
1285$(transform-host-o-to-shared-lib-inner)
1286endef
1287
1288
1289###########################################################
1290## Commands for running gcc to link a shared library or package
1291###########################################################
1292
1293#echo >$@.vers "{"; \
1294#echo >>$@.vers " global:"; \
1295#$(BUILD_SYSTEM)/filter_symbols.sh $(TARGET_NM) "  " ";" $(filter %.o,$^) | sort -u >>$@.vers; \
1296#echo >>$@.vers " local:"; \
1297#echo >>$@.vers "  *;"; \
1298#echo >>$@.vers "};"; \
1299
1300#	-Wl,--version-script=$@.vers \
1301
1302# ld just seems to be so finicky with command order that we allow
1303# it to be overriden en-masse see combo/linux-arm.make for an example.
1304ifneq ($(TARGET_CUSTOM_LD_COMMAND),true)
1305define transform-o-to-shared-lib-inner
1306$(hide) $(PRIVATE_CXX) \
1307	$(PRIVATE_TARGET_GLOBAL_LDFLAGS) \
1308	-Wl,-rpath-link=$(TARGET_OUT_INTERMEDIATE_LIBRARIES) \
1309	-Wl,-rpath,\$$ORIGIN/../lib \
1310	-shared -Wl,-soname,$(notdir $@) \
1311	$(PRIVATE_LDFLAGS) \
1312	$(PRIVATE_TARGET_GLOBAL_LD_DIRS) \
1313	$(PRIVATE_ALL_OBJECTS) \
1314	-Wl,--whole-archive \
1315	$(call normalize-target-libraries,$(PRIVATE_ALL_WHOLE_STATIC_LIBRARIES)) \
1316	-Wl,--no-whole-archive \
1317	$(if $(PRIVATE_GROUP_STATIC_LIBRARIES),-Wl$(comma)--start-group) \
1318	$(call normalize-target-libraries,$(PRIVATE_ALL_STATIC_LIBRARIES)) \
1319	$(if $(PRIVATE_GROUP_STATIC_LIBRARIES),-Wl$(comma)--end-group) \
1320	$(call normalize-target-libraries,$(PRIVATE_ALL_SHARED_LIBRARIES)) \
1321	-o $@ \
1322	$(PRIVATE_LDLIBS)
1323endef
1324endif
1325
1326define transform-o-to-shared-lib
1327@mkdir -p $(dir $@)
1328@echo "target SharedLib: $(PRIVATE_MODULE) ($@)"
1329$($(PRIVATE_2ND_ARCH_VAR_PREFIX)transform-o-to-shared-lib-inner)
1330endef
1331
1332
1333###########################################################
1334## Commands for filtering a target executable or library
1335###########################################################
1336
1337define transform-to-stripped
1338@mkdir -p $(dir $@)
1339@echo "target Strip: $(PRIVATE_MODULE) ($@)"
1340$(hide) $($(PRIVATE_2ND_ARCH_VAR_PREFIX)TARGET_STRIP_COMMAND)
1341endef
1342
1343
1344###########################################################
1345## Commands for running gcc to link an executable
1346###########################################################
1347
1348ifneq ($(TARGET_CUSTOM_LD_COMMAND),true)
1349define transform-o-to-executable-inner
1350$(hide) $(PRIVATE_CXX) \
1351	$(PRIVATE_TARGET_GLOBAL_LDFLAGS) \
1352	$(PRIVATE_TARGET_GLOBAL_LD_DIRS) \
1353	-Wl,-rpath-link=$(TARGET_OUT_INTERMEDIATE_LIBRARIES) \
1354	-Wl,-rpath,\$$ORIGIN/../lib \
1355	$(PRIVATE_LDFLAGS) \
1356	$(PRIVATE_ALL_OBJECTS) \
1357	-Wl,--whole-archive \
1358	$(call normalize-target-libraries,$(PRIVATE_ALL_WHOLE_STATIC_LIBRARIES)) \
1359	-Wl,--no-whole-archive \
1360	$(if $(PRIVATE_GROUP_STATIC_LIBRARIES),-Wl$(comma)--start-group) \
1361	$(call normalize-target-libraries,$(PRIVATE_ALL_STATIC_LIBRARIES)) \
1362	$(if $(PRIVATE_GROUP_STATIC_LIBRARIES),-Wl$(comma)--end-group) \
1363	$(call normalize-target-libraries,$(PRIVATE_ALL_SHARED_LIBRARIES)) \
1364	-o $@ \
1365	$(PRIVATE_LDLIBS)
1366endef
1367endif
1368
1369define transform-o-to-executable
1370@mkdir -p $(dir $@)
1371@echo "target Executable: $(PRIVATE_MODULE) ($@)"
1372$($(PRIVATE_2ND_ARCH_VAR_PREFIX)transform-o-to-executable-inner)
1373endef
1374
1375
1376###########################################################
1377## Commands for running gcc to link a statically linked
1378## executable.  In practice, we only use this on arm, so
1379## the other platforms don't have the
1380## transform-o-to-static-executable defined
1381###########################################################
1382
1383ifneq ($(TARGET_CUSTOM_LD_COMMAND),true)
1384define transform-o-to-static-executable-inner
1385endef
1386endif
1387
1388define transform-o-to-static-executable
1389@mkdir -p $(dir $@)
1390@echo "target StaticExecutable: $(PRIVATE_MODULE) ($@)"
1391$($(PRIVATE_2ND_ARCH_VAR_PREFIX)transform-o-to-static-executable-inner)
1392endef
1393
1394
1395###########################################################
1396## Commands for running gcc to link a host executable
1397###########################################################
1398
1399ifneq ($(HOST_CUSTOM_LD_COMMAND),true)
1400define transform-host-o-to-executable-inner
1401$(hide) $(PRIVATE_CXX) \
1402	$(PRIVATE_ALL_OBJECTS) \
1403	-Wl,--whole-archive \
1404	$(call normalize-host-libraries,$(PRIVATE_ALL_WHOLE_STATIC_LIBRARIES)) \
1405	-Wl,--no-whole-archive \
1406	$(if $(PRIVATE_GROUP_STATIC_LIBRARIES),-Wl$(comma)--start-group) \
1407	$(call normalize-host-libraries,$(PRIVATE_ALL_STATIC_LIBRARIES)) \
1408	$(if $(PRIVATE_GROUP_STATIC_LIBRARIES),-Wl$(comma)--end-group) \
1409	$(call normalize-host-libraries,$(PRIVATE_ALL_SHARED_LIBRARIES)) \
1410	-Wl,-rpath-link=$(HOST_OUT_INTERMEDIATE_LIBRARIES) \
1411	-Wl,-rpath,\$$ORIGIN/../lib \
1412	$(HOST_GLOBAL_LD_DIRS) \
1413	$(if $(PRIVATE_NO_DEFAULT_COMPILER_FLAGS),, \
1414		$(PRIVATE_HOST_GLOBAL_LDFLAGS) \
1415		-fPIE -pie \
1416	) \
1417	$(PRIVATE_LDFLAGS) \
1418	-o $@ \
1419	$(PRIVATE_LDLIBS)
1420endef
1421endif
1422
1423define transform-host-o-to-executable
1424@mkdir -p $(dir $@)
1425@echo "host Executable: $(PRIVATE_MODULE) ($@)"
1426$(transform-host-o-to-executable-inner)
1427endef
1428
1429
1430###########################################################
1431## Commands for running javac to make .class files
1432###########################################################
1433
1434#@echo "Source intermediates dir: $(PRIVATE_SOURCE_INTERMEDIATES_DIR)"
1435#@echo "Source intermediates: $$(find $(PRIVATE_SOURCE_INTERMEDIATES_DIR) -name '*.java')"
1436
1437# TODO: Right now we generate the asset resources twice, first as part
1438# of generating the Java classes, then at the end when packaging the final
1439# assets.  This should be changed to do one of two things: (1) Don't generate
1440# any resource files the first time, only create classes during that stage;
1441# or (2) Don't use the -c flag with the second stage, instead taking the
1442# resource files from the first stage as additional input.  My original intent
1443# was to use approach (2), but this requires a little more work in the tool.
1444# Maybe we should just use approach (1).
1445
1446# This rule creates the R.java and Manifest.java files, both of which
1447# are PRODUCT-neutral.  Don't pass PRIVATE_PRODUCT_AAPT_CONFIG to this invocation.
1448define create-resource-java-files
1449@mkdir -p $(PRIVATE_SOURCE_INTERMEDIATES_DIR)
1450@mkdir -p $(dir $(PRIVATE_RESOURCE_PUBLICS_OUTPUT))
1451$(hide) $(AAPT) package $(PRIVATE_AAPT_FLAGS) -m \
1452    $(eval # PRIVATE_PRODUCT_AAPT_CONFIG is intentionally missing-- see comment.) \
1453    $(addprefix -J , $(PRIVATE_SOURCE_INTERMEDIATES_DIR)) \
1454    $(addprefix -M , $(PRIVATE_ANDROID_MANIFEST)) \
1455    $(addprefix -P , $(PRIVATE_RESOURCE_PUBLICS_OUTPUT)) \
1456    $(addprefix -S , $(PRIVATE_RESOURCE_DIR)) \
1457    $(addprefix -A , $(PRIVATE_ASSET_DIR)) \
1458    $(addprefix -I , $(PRIVATE_AAPT_INCLUDES)) \
1459    $(addprefix -G , $(PRIVATE_PROGUARD_OPTIONS_FILE)) \
1460    $(addprefix --min-sdk-version , $(PRIVATE_DEFAULT_APP_TARGET_SDK)) \
1461    $(addprefix --target-sdk-version , $(PRIVATE_DEFAULT_APP_TARGET_SDK)) \
1462    $(if $(filter --version-code,$(PRIVATE_AAPT_FLAGS)),,$(addprefix --version-code , $(PLATFORM_SDK_VERSION))) \
1463    $(if $(filter --version-name,$(PRIVATE_AAPT_FLAGS)),,$(addprefix --version-name , $(PLATFORM_VERSION)-$(BUILD_NUMBER))) \
1464    $(addprefix --rename-manifest-package , $(PRIVATE_MANIFEST_PACKAGE_NAME)) \
1465    $(addprefix --rename-instrumentation-target-package , $(PRIVATE_MANIFEST_INSTRUMENTATION_FOR))
1466endef
1467
1468ifeq ($(HOST_OS),windows)
1469xlint_unchecked :=
1470else
1471xlint_unchecked := -Xlint:unchecked
1472endif
1473
1474ifeq (true, $(ENABLE_INCREMENTALJAVAC))
1475incremental_dex := --incremental
1476else
1477incremental_dex :=
1478endif
1479
1480# emit-line, <word list>, <output file>
1481define emit-line
1482   $(if $(1),echo -n '$(strip $(1)) ' >> $(2))
1483endef
1484
1485# dump-words-to-file, <word list>, <output file>
1486define dump-words-to-file
1487        @rm -f $(2)
1488        @$(call emit-line,$(wordlist 1,200,$(1)),$(2))
1489        @$(call emit-line,$(wordlist 201,400,$(1)),$(2))
1490        @$(call emit-line,$(wordlist 401,600,$(1)),$(2))
1491        @$(call emit-line,$(wordlist 601,800,$(1)),$(2))
1492        @$(call emit-line,$(wordlist 801,1000,$(1)),$(2))
1493        @$(call emit-line,$(wordlist 1001,1200,$(1)),$(2))
1494        @$(call emit-line,$(wordlist 1201,1400,$(1)),$(2))
1495        @$(call emit-line,$(wordlist 1401,1600,$(1)),$(2))
1496        @$(call emit-line,$(wordlist 1601,1800,$(1)),$(2))
1497        @$(call emit-line,$(wordlist 1801,2000,$(1)),$(2))
1498        @$(call emit-line,$(wordlist 2001,2200,$(1)),$(2))
1499        @$(call emit-line,$(wordlist 2201,2400,$(1)),$(2))
1500        @$(call emit-line,$(wordlist 2401,2600,$(1)),$(2))
1501        @$(call emit-line,$(wordlist 2601,2800,$(1)),$(2))
1502        @$(call emit-line,$(wordlist 2801,3000,$(1)),$(2))
1503        @$(call emit-line,$(wordlist 3001,3200,$(1)),$(2))
1504        @$(call emit-line,$(wordlist 3201,3400,$(1)),$(2))
1505        @$(call emit-line,$(wordlist 3401,3600,$(1)),$(2))
1506        @$(call emit-line,$(wordlist 3601,3800,$(1)),$(2))
1507        @$(call emit-line,$(wordlist 3801,4000,$(1)),$(2))
1508        @$(call emit-line,$(wordlist 4001,4200,$(1)),$(2))
1509        @$(call emit-line,$(wordlist 4201,4400,$(1)),$(2))
1510        @$(call emit-line,$(wordlist 4401,4600,$(1)),$(2))
1511        @$(call emit-line,$(wordlist 4601,4800,$(1)),$(2))
1512        @$(call emit-line,$(wordlist 4801,5000,$(1)),$(2))
1513        @$(if $(wordlist 5001,5002,$(1)),$(error Too many words ($(words $(1)))))
1514endef
1515
1516# For a list of jar files, unzip them to a specified directory,
1517# but make sure that no META-INF files come along for the ride.
1518#
1519# $(1): files to unzip
1520# $(2): destination directory
1521define unzip-jar-files
1522  $(hide) for f in $(1); \
1523  do \
1524    if [ ! -f $$f ]; then \
1525      echo Missing file $$f; \
1526      exit 1; \
1527    fi; \
1528    unzip -qo $$f -d $(2); \
1529  done \
1530  $(if $(PRIVATE_DONT_DELETE_JAR_META_INF),,;rm -rf $(2)/META-INF)
1531endef
1532
1533# Common definition to invoke javac on the host and target.
1534#
1535# Some historical notes:
1536# - below we write the list of java files to java-source-list to avoid argument
1537#   list length problems with Cygwin
1538# - we filter out duplicate java file names because eclipse's compiler
1539#   doesn't like them.
1540#
1541# $(1): javac
1542# $(2): bootclasspath
1543define compile-java
1544$(hide) rm -f $@
1545$(hide) rm -rf $(PRIVATE_CLASS_INTERMEDIATES_DIR)
1546$(hide) mkdir -p $(dir $@)
1547$(hide) mkdir -p $(PRIVATE_CLASS_INTERMEDIATES_DIR)
1548$(call unzip-jar-files,$(PRIVATE_STATIC_JAVA_LIBRARIES),$(PRIVATE_CLASS_INTERMEDIATES_DIR))
1549$(call dump-words-to-file,$(PRIVATE_JAVA_SOURCES),$(PRIVATE_CLASS_INTERMEDIATES_DIR)/java-source-list)
1550$(hide) if [ -d "$(PRIVATE_SOURCE_INTERMEDIATES_DIR)" ]; then \
1551          find $(PRIVATE_SOURCE_INTERMEDIATES_DIR) -name '*.java' >> $(PRIVATE_CLASS_INTERMEDIATES_DIR)/java-source-list; \
1552fi
1553$(hide) tr ' ' '\n' < $(PRIVATE_CLASS_INTERMEDIATES_DIR)/java-source-list \
1554    | sort -u > $(PRIVATE_CLASS_INTERMEDIATES_DIR)/java-source-list-uniq
1555$(hide) if [ -s $(PRIVATE_CLASS_INTERMEDIATES_DIR)/java-source-list-uniq ] ; then \
1556    $(1) -encoding UTF-8 \
1557    $(strip $(PRIVATE_JAVAC_DEBUG_FLAGS)) \
1558    $(if $(findstring true,$(PRIVATE_WARNINGS_ENABLE)),$(xlint_unchecked),) \
1559    $(2) \
1560    $(addprefix -classpath ,$(strip \
1561        $(call normalize-path-list,$(PRIVATE_ALL_JAVA_LIBRARIES)))) \
1562    $(if $(findstring true,$(PRIVATE_WARNINGS_ENABLE)),$(xlint_unchecked),) \
1563    -extdirs "" -d $(PRIVATE_CLASS_INTERMEDIATES_DIR) \
1564    $(PRIVATE_JAVACFLAGS) \
1565    \@$(PRIVATE_CLASS_INTERMEDIATES_DIR)/java-source-list-uniq \
1566    || ( rm -rf $(PRIVATE_CLASS_INTERMEDIATES_DIR) ; exit 41 ) \
1567fi
1568$(if $(PRIVATE_JAVA_LAYERS_FILE), $(hide) build/tools/java-layers.py \
1569    $(PRIVATE_JAVA_LAYERS_FILE) \@$(PRIVATE_CLASS_INTERMEDIATES_DIR)/java-source-list-uniq,)
1570$(hide) rm -f $(PRIVATE_CLASS_INTERMEDIATES_DIR)/java-source-list
1571$(hide) rm -f $(PRIVATE_CLASS_INTERMEDIATES_DIR)/java-source-list-uniq
1572$(if $(PRIVATE_JAR_EXCLUDE_FILES), $(hide) find $(PRIVATE_CLASS_INTERMEDIATES_DIR) \
1573    -name $(word 1, $(PRIVATE_JAR_EXCLUDE_FILES)) \
1574    $(addprefix -o -name , $(wordlist 2, 999, $(PRIVATE_JAR_EXCLUDE_FILES))) \
1575    | xargs rm -rf)
1576$(if $(PRIVATE_JAR_PACKAGES), $(hide) find $(PRIVATE_CLASS_INTERMEDIATES_DIR) -mindepth 1 -type d \
1577    $(foreach pkg, $(PRIVATE_JAR_PACKAGES), \
1578        -not -path $(PRIVATE_CLASS_INTERMEDIATES_DIR)/$(subst .,/,$(pkg))) \
1579    | xargs rm -rf)
1580$(if $(PRIVATE_RMTYPEDEFS), $(hide) $(RMTYPEDEFS) -v $(PRIVATE_CLASS_INTERMEDIATES_DIR))
1581$(if $(PRIVATE_JAR_MANIFEST), \
1582    $(hide) sed -e 's/%BUILD_NUMBER%/$(BUILD_NUMBER)/' \
1583            $(PRIVATE_JAR_MANIFEST) > $(dir $@)/manifest.mf && \
1584        jar -cfm $@ $(dir $@)/manifest.mf \
1585            -C $(PRIVATE_CLASS_INTERMEDIATES_DIR) ., \
1586    $(hide) jar -cf $@ -C $(PRIVATE_CLASS_INTERMEDIATES_DIR) .)
1587endef
1588
1589define transform-java-to-classes.jar
1590@echo "target Java: $(PRIVATE_MODULE) ($(PRIVATE_CLASS_INTERMEDIATES_DIR))"
1591$(call compile-java,$(TARGET_JAVAC),$(PRIVATE_BOOTCLASSPATH))
1592endef
1593
1594# Override the above definitions if we want to do incremetal javac
1595ifeq (true, $(ENABLE_INCREMENTALJAVAC))
1596define compile-java
1597$(hide) mkdir -p $(dir $@)
1598$(hide) mkdir -p $(PRIVATE_CLASS_INTERMEDIATES_DIR)
1599$(hide) touch $(PRIVATE_CLASS_INTERMEDIATES_DIR)/newstamp
1600$(call unzip-jar-files,$(PRIVATE_STATIC_JAVA_LIBRARIES),$(PRIVATE_CLASS_INTERMEDIATES_DIR))
1601$(hide) if [ -e $(PRIVATE_CLASS_INTERMEDIATES_DIR)/stamp ] ; then \
1602        newerFlag=$$(echo -n "-newer $(PRIVATE_CLASS_INTERMEDIATES_DIR)/stamp") ; \
1603    fi ; \
1604    find $(PRIVATE_JAVA_SOURCES) $$newerFlag > $(PRIVATE_CLASS_INTERMEDIATES_DIR)/java-source-list ; \
1605    if [ -d "$(PRIVATE_SOURCE_INTERMEDIATES_DIR)" ]; then \
1606        find $(PRIVATE_SOURCE_INTERMEDIATES_DIR) -name '*.java' $$newerFlag >> $(PRIVATE_CLASS_INTERMEDIATES_DIR)/java-source-list; \
1607    fi
1608$(hide) tr ' ' '\n' < $(PRIVATE_CLASS_INTERMEDIATES_DIR)/java-source-list \
1609    | sort -u > $(PRIVATE_CLASS_INTERMEDIATES_DIR)/java-source-list-uniq
1610@echo "(Incremental) build source files:"
1611@cat $(PRIVATE_CLASS_INTERMEDIATES_DIR)/java-source-list-uniq
1612$(hide) if [ -s $(PRIVATE_CLASS_INTERMEDIATES_DIR)/java-source-list-uniq ] ; then \
1613    $(1) -encoding UTF-8 \
1614    $(strip $(PRIVATE_JAVAC_DEBUG_FLAGS)) \
1615    $(if $(findstring true,$(PRIVATE_WARNINGS_ENABLE)),$(xlint_unchecked),) \
1616    $(2) \
1617    $(addprefix -classpath ,$(strip \
1618        $(call normalize-path-list,$(PRIVATE_ALL_JAVA_LIBRARIES) $(PRIVATE_CLASS_INTERMEDIATES_DIR)))) \
1619    $(if $(findstring true,$(PRIVATE_WARNINGS_ENABLE)),$(xlint_unchecked),) \
1620    -extdirs "" -d $(PRIVATE_CLASS_INTERMEDIATES_DIR) \
1621    $(PRIVATE_JAVACFLAGS) \
1622    \@$(PRIVATE_CLASS_INTERMEDIATES_DIR)/java-source-list-uniq \
1623    || ( exit 41 ) \
1624fi
1625$(hide) rm -f $(PRIVATE_CLASS_INTERMEDIATES_DIR)/java-source-list
1626$(hide) rm -f $(PRIVATE_CLASS_INTERMEDIATES_DIR)/java-source-list-uniq
1627$(hide) rm -f $@
1628$(if $(PRIVATE_JAR_EXCLUDE_FILES), $(hide) find $(PRIVATE_CLASS_INTERMEDIATES_DIR) \
1629    -name $(word 1, $(PRIVATE_JAR_EXCLUDE_FILES)) \
1630    $(addprefix -o -name , $(wordlist 2, 999, $(PRIVATE_JAR_EXCLUDE_FILES))) \
1631    | xargs rm -rf)
1632$(if $(PRIVATE_JAR_PACKAGES), $(hide) find $(PRIVATE_CLASS_INTERMEDIATES_DIR) -mindepth 1 -type d \
1633    $(foreach pkg, $(PRIVATE_JAR_PACKAGES), \
1634        -not -path $(PRIVATE_CLASS_INTERMEDIATES_DIR)/$(subst .,/,$(pkg))) \
1635    | xargs rm -rf)
1636$(if $(PRIVATE_RMTYPEDEFS), $(hide) $(RMTYPEDEFS) -v $(PRIVATE_CLASS_INTERMEDIATES_DIR))
1637$(if $(PRIVATE_JAR_MANIFEST), \
1638    $(hide) sed -e 's/%BUILD_NUMBER%/$(BUILD_NUMBER)/' \
1639            $(PRIVATE_JAR_MANIFEST) > $(dir $@)/manifest.mf && \
1640        jar -cfm $@ $(dir $@)/manifest.mf \
1641            -C $(PRIVATE_CLASS_INTERMEDIATES_DIR) ., \
1642    $(hide) jar -cf $@ -C $(PRIVATE_CLASS_INTERMEDIATES_DIR) .)
1643$(hide) mv $(PRIVATE_CLASS_INTERMEDIATES_DIR)/newstamp $(PRIVATE_CLASS_INTERMEDIATES_DIR)/stamp
1644endef
1645
1646define transform-java-to-classes.jar
1647@echo "target Java: $(PRIVATE_MODULE) ($(PRIVATE_CLASS_INTERMEDIATES_DIR))"
1648$(call compile-java,$(TARGET_JAVAC),$(PRIVATE_BOOTCLASSPATH))
1649endef
1650endif # ENABLE_INCREMENTALJAVAC
1651
1652define transform-classes.jar-to-emma
1653$(hide) java -classpath $(EMMA_JAR) emma instr -outmode fullcopy -outfile \
1654    $(PRIVATE_EMMA_COVERAGE_FILE) -ip $< -d $(PRIVATE_EMMA_INTERMEDIATES_DIR) \
1655    $(addprefix -ix , $(PRIVATE_EMMA_COVERAGE_FILTER))
1656endef
1657
1658#TODO: use a smaller -Xmx value for most libraries;
1659#      only core.jar and framework.jar need a heap this big.
1660# Avoid the memory arguments on Windows, dx fails to load for some reason with them.
1661define transform-classes.jar-to-dex
1662@echo "target Dex: $(PRIVATE_MODULE)"
1663@mkdir -p $(dir $@)
1664$(hide) rm -f $(dir $@)/classes*.dex
1665$(hide) $(DX) \
1666    $(if $(findstring windows,$(HOST_OS)),,-JXms16M -JXmx2048M) \
1667    --dex --output=$(dir $@) \
1668    $(incremental_dex) \
1669    $(if $(NO_OPTIMIZE_DX), \
1670        --no-optimize) \
1671    $(if $(GENERATE_DEX_DEBUG), \
1672	    --debug --verbose \
1673	    --dump-to=$(@:.dex=.lst) \
1674	    --dump-width=1000) \
1675    $(PRIVATE_DX_FLAGS) \
1676    $<
1677endef
1678
1679# Create a mostly-empty .jar file that we'll add to later.
1680# The MacOS jar tool doesn't like creating empty jar files,
1681# so we need to give it something.
1682define create-empty-package
1683@mkdir -p $(dir $@)
1684$(hide) touch $(dir $@)/dummy
1685$(hide) (cd $(dir $@) && jar cf $(notdir $@) dummy)
1686$(hide) zip -qd $@ dummy
1687$(hide) rm $(dir $@)/dummy
1688endef
1689
1690#TODO: we kinda want to build different asset packages for
1691#      different configurations, then combine them later (or something).
1692#      Per-locale, etc.
1693#      A list of dynamic and static parameters;  build layers for
1694#      dynamic params that lay over the static ones.
1695#TODO: update the manifest to point to the package file
1696#Note that the version numbers are given to aapt as simple default
1697#values; applications can override these by explicitly stating
1698#them in their manifest.
1699define add-assets-to-package
1700$(hide) $(AAPT) package -u $(PRIVATE_AAPT_FLAGS) \
1701    $(addprefix -c , $(PRIVATE_PRODUCT_AAPT_CONFIG)) \
1702    $(addprefix --preferred-configurations , $(PRIVATE_PRODUCT_AAPT_PREF_CONFIG)) \
1703    $(addprefix -M , $(PRIVATE_ANDROID_MANIFEST)) \
1704    $(addprefix -S , $(PRIVATE_RESOURCE_DIR)) \
1705    $(addprefix -A , $(PRIVATE_ASSET_DIR)) \
1706    $(addprefix -I , $(PRIVATE_AAPT_INCLUDES)) \
1707    $(addprefix --min-sdk-version , $(PRIVATE_DEFAULT_APP_TARGET_SDK)) \
1708    $(addprefix --target-sdk-version , $(PRIVATE_DEFAULT_APP_TARGET_SDK)) \
1709    $(addprefix --product , $(TARGET_AAPT_CHARACTERISTICS)) \
1710    $(if $(filter --version-code,$(PRIVATE_AAPT_FLAGS)),,$(addprefix --version-code , $(PLATFORM_SDK_VERSION))) \
1711    $(if $(filter --version-name,$(PRIVATE_AAPT_FLAGS)),,$(addprefix --version-name , $(PLATFORM_VERSION)-$(BUILD_NUMBER))) \
1712    $(addprefix --rename-manifest-package , $(PRIVATE_MANIFEST_PACKAGE_NAME)) \
1713    $(addprefix --rename-instrumentation-target-package , $(PRIVATE_MANIFEST_INSTRUMENTATION_FOR)) \
1714    -F $@
1715endef
1716
1717define add-jni-shared-libs-to-package
1718$(hide) rm -rf $(dir $@)lib
1719$(hide) mkdir -p $(dir $@)lib/$(PRIVATE_JNI_SHARED_LIBRARIES_ABI)
1720$(hide) cp $(PRIVATE_JNI_SHARED_LIBRARIES) $(dir $@)lib/$(PRIVATE_JNI_SHARED_LIBRARIES_ABI)
1721$(hide) (cd $(dir $@) && zip -r $(notdir $@) lib)
1722$(hide) rm -rf $(dir $@)lib
1723endef
1724
1725#TODO: update the manifest to point to the dex file
1726define add-dex-to-package
1727$(hide) zip -qj $@ $(dir $(PRIVATE_DEX_FILE))/classes*.dex
1728endef
1729
1730# Add java resources added by the current module.
1731#
1732define add-java-resources-to-package
1733$(call dump-words-to-file, $(PRIVATE_EXTRA_JAR_ARGS), $(dir $@)jar-arg-list)
1734$(hide) jar uf $@ @$(dir $@)jar-arg-list
1735@rm -f $(dir $@)jar-arg-list
1736endef
1737
1738# Add java resources carried by static Java libraries.
1739#
1740define add-carried-java-resources
1741$(hide) if [ -d $(PRIVATE_CLASS_INTERMEDIATES_DIR) ] ; then \
1742    java_res_jar_flags=$$(find $(PRIVATE_CLASS_INTERMEDIATES_DIR) -type f -a -not -name "*.class" \
1743        | sed -e "s?^$(PRIVATE_CLASS_INTERMEDIATES_DIR)/? -C $(PRIVATE_CLASS_INTERMEDIATES_DIR) ?"); \
1744    if [ -n "$$java_res_jar_flags" ] ; then \
1745        echo $$java_res_jar_flags >$(dir $@)java_res_jar_flags; \
1746        jar uf $@ $$java_res_jar_flags; \
1747    fi; \
1748fi
1749endef
1750
1751# Sign a package using the specified key/cert.
1752#
1753define sign-package
1754$(hide) mv $@ $@.unsigned
1755$(hide) java -jar $(SIGNAPK_JAR) \
1756    $(PRIVATE_CERTIFICATE) $(PRIVATE_PRIVATE_KEY) \
1757    $(PRIVATE_ADDITIONAL_CERTIFICATES) $@.unsigned $@.signed
1758$(hide) mv $@.signed $@
1759endef
1760
1761# Align STORED entries of a package on 4-byte boundaries
1762# to make them easier to mmap.
1763#
1764define align-package
1765$(hide) mv $@ $@.unaligned
1766$(hide) $(ZIPALIGN) -f 4 $@.unaligned $@.aligned
1767$(hide) mv $@.aligned $@
1768endef
1769
1770define install-dex-debug
1771$(hide) if [ -f "$(PRIVATE_INTERMEDIATES_DIR)/classes.dex" ]; then \
1772	    mkdir -p $(TOP)/dalvik/DEBUG-FILES; \
1773	    $(ACP) $(PRIVATE_INTERMEDIATES_DIR)/classes.dex \
1774		$(TOP)/dalvik/DEBUG-FILES/$(PRIVATE_MODULE).dex; \
1775	fi
1776$(hide) if [ -f "$(PRIVATE_INTERMEDIATES_DIR)/classes.lst" ]; then \
1777	    mkdir -p $(TOP)/dalvik/DEBUG-FILES; \
1778	    $(ACP) $(PRIVATE_INTERMEDIATES_DIR)/classes.lst \
1779		$(TOP)/dalvik/DEBUG-FILES/$(PRIVATE_MODULE).lst; \
1780	fi
1781endef
1782
1783# TODO(joeo): If we can ever upgrade to post 3.81 make and get the
1784# new prebuilt rules to work, we should change this to copy the
1785# resources to the out directory and then copy the resources.
1786
1787# Note: we intentionally don't clean PRIVATE_CLASS_INTERMEDIATES_DIR
1788# in transform-java-to-classes for the sake of vm-tests.
1789define transform-host-java-to-package
1790@echo "host Java: $(PRIVATE_MODULE) ($(PRIVATE_CLASS_INTERMEDIATES_DIR))"
1791$(call compile-java,$(HOST_JAVAC),$(PRIVATE_BOOTCLASSPATH))
1792$(if $(PRIVATE_EXTRA_JAR_ARGS), $(call add-java-resources-to-package))
1793endef
1794
1795###########################################################
1796## Commands for copying files
1797###########################################################
1798
1799# Define a rule to copy a header.  Used via $(eval) by copy_headers.make.
1800# $(1): source header
1801# $(2): destination header
1802define copy-one-header
1803$(2): $(1)
1804	@echo "Header: $$@"
1805	$$(copy-file-to-new-target-with-cp)
1806endef
1807
1808# Define a rule to copy a file.  For use via $(eval).
1809# $(1): source file
1810# $(2): destination file
1811define copy-one-file
1812$(2): $(1) | $(ACP)
1813	@echo "Copy: $$@"
1814	$$(copy-file-to-target)
1815endef
1816
1817# Copies many files.
1818# $(1): The files to copy.  Each entry is a ':' separated src:dst pair
1819# Evaluates to the list of the dst files (ie suitable for a dependency list)
1820define copy-many-files
1821$(foreach f, $(1), $(strip \
1822    $(eval _cmf_tuple := $(subst :, ,$(f))) \
1823    $(eval _cmf_src := $(word 1,$(_cmf_tuple))) \
1824    $(eval _cmf_dest := $(word 2,$(_cmf_tuple))) \
1825    $(eval $(call copy-one-file,$(_cmf_src),$(_cmf_dest))) \
1826    $(_cmf_dest)))
1827endef
1828
1829# Copy the file only if it's a well-formed xml file. For use via $(eval).
1830# $(1): source file
1831# $(2): destination file, must end with .xml.
1832define copy-xml-file-checked
1833$(2): $(1) | $(ACP)
1834	@echo "Copy xml: $$@"
1835	$(hide) xmllint $$< >/dev/null  # Don't print the xml file to stdout.
1836	$$(copy-file-to-target)
1837endef
1838
1839# The -t option to acp and the -p option to cp is
1840# required for OSX.  OSX has a ridiculous restriction
1841# where it's an error for a .a file's modification time
1842# to disagree with an internal timestamp, and this
1843# macro is used to install .a files (among other things).
1844
1845# Copy a single file from one place to another,
1846# preserving permissions and overwriting any existing
1847# file.
1848# We disable the "-t" option for acp cannot handle
1849# high resolution timestamp correctly on file systems like ext4.
1850# Therefore copy-file-to-target is the same as copy-file-to-new-target.
1851define copy-file-to-target
1852@mkdir -p $(dir $@)
1853$(hide) $(ACP) -fp $< $@
1854endef
1855
1856# The same as copy-file-to-target, but use the local
1857# cp command instead of acp.
1858define copy-file-to-target-with-cp
1859@mkdir -p $(dir $@)
1860$(hide) cp -fp $< $@
1861endef
1862
1863# The same as copy-file-to-target, but use the zipalign tool to do so.
1864define copy-file-to-target-with-zipalign
1865@mkdir -p $(dir $@)
1866$(hide) $(ZIPALIGN) -f 4 $< $@
1867endef
1868
1869# The same as copy-file-to-target, but strip out "# comment"-style
1870# comments (for config files and such).
1871define copy-file-to-target-strip-comments
1872@mkdir -p $(dir $@)
1873$(hide) sed -e 's/#.*$$//' -e 's/[ \t]*$$//' -e '/^$$/d' < $< > $@
1874endef
1875
1876# The same as copy-file-to-target, but don't preserve
1877# the old modification time.
1878define copy-file-to-new-target
1879@mkdir -p $(dir $@)
1880$(hide) $(ACP) -fp $< $@
1881endef
1882
1883# The same as copy-file-to-new-target, but use the local
1884# cp command instead of acp.
1885define copy-file-to-new-target-with-cp
1886@mkdir -p $(dir $@)
1887$(hide) cp -f $< $@
1888endef
1889
1890# Copy a prebuilt file to a target location.
1891define transform-prebuilt-to-target
1892@echo "$(if $(PRIVATE_IS_HOST_MODULE),host,target) Prebuilt: $(PRIVATE_MODULE) ($@)"
1893$(copy-file-to-target)
1894endef
1895
1896# Copy a prebuilt file to a target location, using zipalign on it.
1897define transform-prebuilt-to-target-with-zipalign
1898@echo "$(if $(PRIVATE_IS_HOST_MODULE),host,target) Prebuilt APK: $(PRIVATE_MODULE) ($@)"
1899$(copy-file-to-target-with-zipalign)
1900endef
1901
1902# Copy a prebuilt file to a target location, stripping "# comment" comments.
1903define transform-prebuilt-to-target-strip-comments
1904@echo "$(if $(PRIVATE_IS_HOST_MODULE),host,target) Prebuilt: $(PRIVATE_MODULE) ($@)"
1905$(copy-file-to-target-strip-comments)
1906endef
1907
1908###########################################################
1909## On some platforms (MacOS), after copying a static
1910## library, ranlib must be run to update an internal
1911## timestamp!?!?!
1912###########################################################
1913
1914ifeq ($(HOST_RUN_RANLIB_AFTER_COPYING),true)
1915define transform-host-ranlib-copy-hack
1916    $(hide) ranlib $@ || true
1917endef
1918else
1919define transform-host-ranlib-copy-hack
1920@true
1921endef
1922endif
1923
1924ifeq ($(TARGET_RUN_RANLIB_AFTER_COPYING),true)
1925define transform-ranlib-copy-hack
1926    $(hide) ranlib $@
1927endef
1928else
1929define transform-ranlib-copy-hack
1930@true
1931endef
1932endif
1933
1934
1935###########################################################
1936## Commands to call Proguard
1937###########################################################
1938define transform-jar-to-proguard
1939@echo Proguard: $@
1940$(hide) $(PROGUARD) -injars $< -outjars $@ $(PRIVATE_PROGUARD_FLAGS) \
1941    $(addprefix -injars , $(PRIVATE_EXTRA_INPUT_JAR))
1942endef
1943
1944###########################################################
1945## Stuff source generated from one-off tools
1946###########################################################
1947
1948define transform-generated-source
1949@echo "target Generated: $(PRIVATE_MODULE) <= $<"
1950@mkdir -p $(dir $@)
1951$(hide) $(PRIVATE_CUSTOM_TOOL)
1952endef
1953
1954
1955###########################################################
1956## Assertions about attributes of the target
1957###########################################################
1958
1959# $(1): The file to check
1960ifndef get-file-size
1961$(error HOST_OS must define get-file-size)
1962endif
1963
1964# Convert a partition data size (eg, as reported in /proc/mtd) to the
1965# size of the image used to flash that partition (which includes a
1966# spare area for each page).
1967# $(1): the partition data size
1968define image-size-from-data-size
1969$(strip $(eval _isfds_value := $$(shell echo $$$$(($(1) / $(BOARD_NAND_PAGE_SIZE) * \
1970  ($(BOARD_NAND_PAGE_SIZE)+$(BOARD_NAND_SPARE_SIZE))))))\
1971$(if $(filter 0, $(_isfds_value)),$(shell echo $$(($(BOARD_NAND_PAGE_SIZE)+$(BOARD_NAND_SPARE_SIZE)))),$(_isfds_value))\
1972$(eval _isfds_value :=))
1973endef
1974
1975# $(1): The file(s) to check (often $@)
1976# $(2): The maximum total image size, in decimal bytes
1977# $(3): the type of filesystem "yaffs" or "raw"
1978#
1979# If $(2) is empty, evaluates to "true"
1980#
1981# Reserve bad blocks.  Make sure that MAX(1% of partition size, 2 blocks)
1982# is left over after the image has been flashed.  Round the 1% up to the
1983# next whole flash block size.
1984define assert-max-file-size
1985$(if $(2), \
1986  size=$$(for i in $(1); do $(call get-file-size,$$i); echo +; done; echo 0); \
1987  total=$$(( $$( echo "$$size" ) )); \
1988  printname=$$(echo -n "$(1)" | tr " " +); \
1989  img_blocksize=$(call image-size-from-data-size,$(BOARD_FLASH_BLOCK_SIZE)); \
1990  if [ "$(3)" == "yaffs" ]; then \
1991    reservedblocks=8; \
1992  else \
1993    reservedblocks=0; \
1994  fi; \
1995  twoblocks=$$((img_blocksize * 2)); \
1996  onepct=$$((((($(2) / 100) - 1) / img_blocksize + 1) * img_blocksize)); \
1997  reserve=$$(((twoblocks > onepct ? twoblocks : onepct) + \
1998               reservedblocks * img_blocksize)); \
1999  maxsize=$$(($(2) - reserve)); \
2000  echo "$$printname maxsize=$$maxsize blocksize=$$img_blocksize total=$$total reserve=$$reserve"; \
2001  if [ "$$total" -gt "$$maxsize" ]; then \
2002    echo "error: $$printname too large ($$total > [$(2) - $$reserve])"; \
2003    false; \
2004  elif [ "$$total" -gt $$((maxsize - 32768)) ]; then \
2005    echo "WARNING: $$printname approaching size limit ($$total now; limit $$maxsize)"; \
2006  fi \
2007 , \
2008  true \
2009 )
2010endef
2011
2012# Like assert-max-file-size, but the second argument is a partition
2013# size, which we'll convert to a max image size before checking it
2014# against the files.
2015#
2016# $(1): The file(s) to check (often $@)
2017# $(2): The partition size.
2018define assert-max-image-size
2019$(if $(2), \
2020  $(call assert-max-file-size,$(1),$(call image-size-from-data-size,$(2))), \
2021  true)
2022endef
2023
2024
2025###########################################################
2026## Define device-specific radio files
2027###########################################################
2028INSTALLED_RADIOIMAGE_TARGET :=
2029
2030# Copy a radio image file to the output location, and add it to
2031# INSTALLED_RADIOIMAGE_TARGET.
2032# $(1): filename
2033define add-radio-file
2034  $(eval $(call add-radio-file-internal,$(1),$(notdir $(1))))
2035endef
2036define add-radio-file-internal
2037INSTALLED_RADIOIMAGE_TARGET += $$(PRODUCT_OUT)/$(2)
2038$$(PRODUCT_OUT)/$(2) : $$(LOCAL_PATH)/$(1) | $$(ACP)
2039	$$(transform-prebuilt-to-target)
2040endef
2041
2042# Version of add-radio-file that also arranges for the version of the
2043# file to be checked against the contents of
2044# $(TARGET_BOARD_INFO_FILE).
2045# $(1): filename
2046# $(2): name of version variable in board-info (eg, "version-baseband")
2047define add-radio-file-checked
2048  $(eval $(call add-radio-file-checked-internal,$(1),$(notdir $(1)),$(2)))
2049endef
2050define add-radio-file-checked-internal
2051INSTALLED_RADIOIMAGE_TARGET += $$(PRODUCT_OUT)/$(2)
2052BOARD_INFO_CHECK += $(3):$(LOCAL_PATH)/$(1)
2053$$(PRODUCT_OUT)/$(2) : $$(LOCAL_PATH)/$(1) | $$(ACP)
2054	$$(transform-prebuilt-to-target)
2055endef
2056
2057
2058###########################################################
2059# Override the package defined in $(1), setting the
2060# variables listed below differently.
2061#
2062#  $(1): The makefile to override (relative to the source
2063#        tree root)
2064#  $(2): Old LOCAL_PACKAGE_NAME value.
2065#  $(3): New LOCAL_PACKAGE_NAME value.
2066#  $(4): New LOCAL_MANIFEST_PACKAGE_NAME value.
2067#  $(5): New LOCAL_CERTIFICATE value.
2068#  $(6): New LOCAL_INSTRUMENTATION_FOR value.
2069#  $(7): New LOCAL_MANIFEST_INSTRUMENTATION_FOR value.
2070#
2071# Note that LOCAL_PACKAGE_OVERRIDES is NOT cleared in
2072# clear_vars.mk.
2073###########################################################
2074define inherit-package
2075  $(eval $(call inherit-package-internal,$(1),$(2),$(3),$(4),$(5),$(6),$(7)))
2076endef
2077
2078define inherit-package-internal
2079  LOCAL_PACKAGE_OVERRIDES \
2080      := $(strip $(1))||$(strip $(2))||$(strip $(3))||$(strip $(4))||&&$(strip $(5))||&&$(strip $(6))||&&$(strip $(7)) $(LOCAL_PACKAGE_OVERRIDES)
2081  include $(1)
2082  LOCAL_PACKAGE_OVERRIDES \
2083      := $(wordlist 1,$(words $(LOCAL_PACKAGE_OVERRIDES)), $(LOCAL_PACKAGE_OVERRIDES))
2084endef
2085
2086# To be used with inherit-package above
2087# Evalutes to true if the package was overridden
2088define set-inherited-package-variables
2089$(strip $(call set-inherited-package-variables-internal))
2090endef
2091
2092define keep-or-override
2093$(eval $(1) := $(if $(2),$(2),$($(1))))
2094endef
2095
2096define set-inherited-package-variables-internal
2097  $(eval _o := $(subst ||, ,$(lastword $(LOCAL_PACKAGE_OVERRIDES))))
2098  $(eval _n := $(subst ||, ,$(firstword $(LOCAL_PACKAGE_OVERRIDES))))
2099  $(if $(filter $(word 2,$(_n)),$(LOCAL_PACKAGE_NAME)), \
2100    $(eval LOCAL_PACKAGE_NAME := $(word 3,$(_o))) \
2101    $(eval LOCAL_MANIFEST_PACKAGE_NAME := $(word 4,$(_o))) \
2102    $(call keep-or-override,LOCAL_CERTIFICATE,$(patsubst &&%,%,$(word 5,$(_o)))) \
2103    $(call keep-or-override,LOCAL_INSTRUMENTATION_FOR,$(patsubst &&%,%,$(word 6,$(_o)))) \
2104    $(call keep-or-override,LOCAL_MANIFEST_INSTRUMENTATION_FOR,$(patsubst &&%,%,$(word 7,$(_o)))) \
2105    $(eval LOCAL_OVERRIDES_PACKAGES := $(sort $(LOCAL_OVERRIDES_PACKAGES) $(word 2,$(_o)))) \
2106    true \
2107  ,)
2108endef
2109
2110###########################################################
2111## Expand a module name list with REQUIRED modules
2112###########################################################
2113# $(1): The variable name that holds the initial module name list.
2114#       the variable will be modified to hold the expanded results.
2115# $(2): The initial module name list.
2116# Returns empty string (maybe with some whitespaces).
2117define expand-required-modules
2118$(eval _erm_new_modules := $(sort $(filter-out $($(1)),\
2119  $(foreach m,$(2),$(ALL_MODULES.$(m).REQUIRED)))))\
2120$(if $(_erm_new_modules),$(eval $(1) += $(_erm_new_modules))\
2121  $(call expand-required-modules,$(1),$(_erm_new_modules)))
2122endef
2123
2124###########################################################
2125## API Check
2126###########################################################
2127
2128# eval this to define a rule that runs apicheck.
2129#
2130# Args:
2131#    $(1)  target
2132#    $(2)  stable api file
2133#    $(3)  api file to be tested
2134#    $(4)  arguments for apicheck
2135#    $(5)  command to run if apicheck failed
2136#    $(6)  target dependent on this api check
2137#    $(7)  additional dependencies
2138define check-api
2139$(TARGET_OUT_COMMON_INTERMEDIATES)/PACKAGING/$(strip $(1))-timestamp: $(2) $(3) $(APICHECK) $(7)
2140	@echo "Checking API:" $(1)
2141	$(hide) ( $(APICHECK_COMMAND) $(4) $(2) $(3) || ( $(5) ; exit 38 ) )
2142	$(hide) mkdir -p $$(dir $$@)
2143	$(hide) touch $$@
2144$(6): $(TARGET_OUT_COMMON_INTERMEDIATES)/PACKAGING/$(strip $(1))-timestamp
2145endef
2146
2147## Whether to build from source if prebuilt alternative exists
2148###########################################################
2149# $(1): module name
2150# $(2): LOCAL_PATH
2151# Expands to empty string if not from source.
2152ifeq (true,$(ANDROID_BUILD_FROM_SOURCE))
2153define if-build-from-source
2154true
2155endef
2156else
2157define if-build-from-source
2158$(if $(filter $(ANDROID_NO_PREBUILT_MODULES),$(1))$(filter \
2159    $(addsuffix %,$(ANDROID_NO_PREBUILT_PATHS)),$(2)),true)
2160endef
2161endif
2162
2163# Include makefile $(1) if build from source for module $(2)
2164# $(1): the makefile to include
2165# $(2): module name
2166# $(3): LOCAL_PATH
2167define include-if-build-from-source
2168$(if $(call if-build-from-source,$(2),$(3)),$(eval include $(1)))
2169endef
2170
2171###########################################################
2172## Other includes
2173###########################################################
2174
2175# -----------------------------------------------------------------
2176# Rules and functions to help copy important files to DIST_DIR
2177# when requested.
2178include $(BUILD_SYSTEM)/distdir.mk
2179
2180# Include any vendor specific definitions.mk file
2181-include $(TOPDIR)vendor/*/build/core/definitions.mk
2182
2183# broken:
2184#	$(foreach file,$^,$(if $(findstring,.a,$(suffix $file)),-l$(file),$(file)))
2185
2186###########################################################
2187## Misc notes
2188###########################################################
2189
2190#DEPDIR = .deps
2191#df = $(DEPDIR)/$(*F)
2192
2193#SRCS = foo.c bar.c ...
2194
2195#%.o : %.c
2196#	@$(MAKEDEPEND); \
2197#	  cp $(df).d $(df).P; \
2198#	  sed -e 's/#.*//' -e 's/^[^:]*: *//' -e 's/ *\\$$//' \
2199#	      -e '/^$$/ d' -e 's/$$/ :/' < $(df).d >> $(df).P; \
2200#	  rm -f $(df).d
2201#	$(COMPILE.c) -o $@ $<
2202
2203#-include $(SRCS:%.c=$(DEPDIR)/%.P)
2204
2205
2206#%.o : %.c
2207#	$(COMPILE.c) -MD -o $@ $<
2208#	@cp $*.d $*.P; \
2209#	  sed -e 's/#.*//' -e 's/^[^:]*: *//' -e 's/ *\\$$//' \
2210#	      -e '/^$$/ d' -e 's/$$/ :/' < $*.d >> $*.P; \
2211#	  rm -f $*.d
2212