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