definitions.mk revision 00985df83acf4c21e0e7243d74348248b1c69e91
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 $(call 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 .proto files under the named directories.
240## Meant to be used like:
241##    SRC_FILES := $(call all-proto-files-under,src)
242###########################################################
243
244define all-proto-files-under
245$(patsubst ./%,%, \
246  $(shell cd $(LOCAL_PATH) ; \
247          find $(1) -name "*.proto" -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 $(_idfPrefix)-$(_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 "core ext framework" to "out/.../javalib.jar ..."
490## $(1): library list
491## $(2): Non-empty if IS_HOST_MODULE
492###########################################################
493
494# $(1): library name
495# $(2): Non-empty if IS_HOST_MODULE
496define _java-lib-dir
497$(call intermediates-dir-for, \
498	JAVA_LIBRARIES,$(1),$(2),COMMON)
499endef
500
501# $(1): library name
502# $(2): Non-empty if IS_HOST_MODULE
503define _java-lib-full-classes.jar
504$(call _java-lib-dir,$(1),$(2))/classes$(COMMON_JAVA_PACKAGE_SUFFIX)
505endef
506
507# $(1): library name list
508# $(2): Non-empty if IS_HOST_MODULE
509define java-lib-files
510$(foreach lib,$(1),$(call _java-lib-full-classes.jar,$(lib),$(2)))
511endef
512
513# $(1): library name
514# $(2): Non-empty if IS_HOST_MODULE
515define _java-lib-full-dep
516$(call _java-lib-dir,$(1),$(2))/javalib$(COMMON_JAVA_PACKAGE_SUFFIX)
517endef
518
519# $(1): library name list
520# $(2): Non-empty if IS_HOST_MODULE
521define java-lib-deps
522$(foreach lib,$(1),$(call _java-lib-full-dep,$(lib),$(2)))
523endef
524
525###########################################################
526## Convert "a b c" into "a:b:c"
527###########################################################
528
529empty :=
530space := $(empty) $(empty)
531
532define normalize-path-list
533$(subst $(space),:,$(strip $(1)))
534endef
535
536###########################################################
537## Read the word out of a colon-separated list of words.
538## This has the same behavior as the built-in function
539## $(word n,str).
540##
541## The individual words may not contain spaces.
542##
543## $(1): 1 based index
544## $(2): value of the form a:b:c...
545###########################################################
546
547define word-colon
548$(word $(1),$(subst :,$(space),$(2)))
549endef
550
551###########################################################
552## Convert "a=b c= d e = f" into "a=b c=d e=f"
553##
554## $(1): list to collapse
555## $(2): if set, separator word; usually "=", ":", or ":="
556##       Defaults to "=" if not set.
557###########################################################
558
559define collapse-pairs
560$(eval _cpSEP := $(strip $(if $(2),$(2),=)))\
561$(subst $(space)$(_cpSEP)$(space),$(_cpSEP),$(strip \
562    $(subst $(_cpSEP), $(_cpSEP) ,$(1))))
563endef
564
565###########################################################
566## MODULE_TAG set operations
567###########################################################
568
569# Given a list of tags, return the targets that specify
570# any of those tags.
571# $(1): tag list
572define modules-for-tag-list
573$(sort $(foreach tag,$(1),$(ALL_MODULE_TAGS.$(tag))))
574endef
575
576# Same as modules-for-tag-list, but operates on
577# ALL_MODULE_NAME_TAGS.
578# $(1): tag list
579define module-names-for-tag-list
580$(sort $(foreach tag,$(1),$(ALL_MODULE_NAME_TAGS.$(tag))))
581endef
582
583# Given an accept and reject list, find the matching
584# set of targets.  If a target has multiple tags and
585# any of them are rejected, the target is rejected.
586# Reject overrides accept.
587# $(1): list of tags to accept
588# $(2): list of tags to reject
589#TODO(dbort): do $(if $(strip $(1)),$(1),$(ALL_MODULE_TAGS))
590#TODO(jbq): as of 20100106 nobody uses the second parameter
591define get-tagged-modules
592$(filter-out \
593	$(call modules-for-tag-list,$(2)), \
594	    $(call modules-for-tag-list,$(1)))
595endef
596
597###########################################################
598## Append a leaf to a base path.  Properly deals with
599## base paths ending in /.
600##
601## $(1): base path
602## $(2): leaf path
603###########################################################
604
605define append-path
606$(subst //,/,$(1)/$(2))
607endef
608
609
610###########################################################
611## Package filtering
612###########################################################
613
614# Given a list of installed modules (short or long names)
615# return a list of the packages (yes, .apk packages, not
616# modules in general) that are overridden by this list and,
617# therefore, should not be installed.
618# $(1): mixed list of installed modules
619# TODO: This is fragile; find a reliable way to get this information.
620define _get-package-overrides
621 $(eval ### Discard any words containing slashes, unless they end in .apk, \
622        ### in which case trim off the directory component and the suffix. \
623        ### If there are no slashes, keep the entire word.)
624 $(eval _gpo_names := $(subst /,@@@ @@@,$(1)))
625 $(eval _gpo_names := \
626     $(filter %.apk,$(_gpo_names)) \
627     $(filter-out %@@@ @@@%,$(_gpo_names)))
628 $(eval _gpo_names := $(patsubst %.apk,%,$(_gpo_names)))
629 $(eval _gpo_names := $(patsubst @@@%,%,$(_gpo_names)))
630
631 $(eval ### Remove any remaining words that contain dots.)
632 $(eval _gpo_names := $(subst .,@@@ @@@,$(_gpo_names)))
633 $(eval _gpo_names := $(filter-out %@@@ @@@%,$(_gpo_names)))
634
635 $(eval ### Now we have a list of any words that could possibly refer to \
636        ### packages, although there may be words that do not.  Only \
637        ### real packages will be present under PACKAGES.*, though.)
638 $(foreach _gpo_name,$(_gpo_names),$(PACKAGES.$(_gpo_name).OVERRIDES))
639endef
640
641define get-package-overrides
642$(strip $(sort $(call _get-package-overrides,$(1))))
643endef
644
645###########################################################
646## Output the command lines, or not
647###########################################################
648
649ifeq ($(strip $(SHOW_COMMANDS)),)
650define pretty
651@echo $1
652endef
653hide := @
654else
655define pretty
656endef
657hide :=
658endif
659
660###########################################################
661## Dump the variables that are associated with targets
662###########################################################
663
664define dump-module-variables
665@echo all_dependencies=$^
666@echo PRIVATE_YACCFLAGS=$(PRIVATE_YACCFLAGS);
667@echo PRIVATE_CFLAGS=$(PRIVATE_CFLAGS);
668@echo PRIVATE_CPPFLAGS=$(PRIVATE_CPPFLAGS);
669@echo PRIVATE_DEBUG_CFLAGS=$(PRIVATE_DEBUG_CFLAGS);
670@echo PRIVATE_C_INCLUDES=$(PRIVATE_C_INCLUDES);
671@echo PRIVATE_LDFLAGS=$(PRIVATE_LDFLAGS);
672@echo PRIVATE_LDLIBS=$(PRIVATE_LDLIBS);
673@echo PRIVATE_ARFLAGS=$(PRIVATE_ARFLAGS);
674@echo PRIVATE_AAPT_FLAGS=$(PRIVATE_AAPT_FLAGS);
675@echo PRIVATE_DX_FLAGS=$(PRIVATE_DX_FLAGS);
676@echo PRIVATE_JAVACFLAGS=$(PRIVATE_JAVACFLAGS);
677@echo PRIVATE_JAVA_LIBRARIES=$(PRIVATE_JAVA_LIBRARIES);
678@echo PRIVATE_ALL_SHARED_LIBRARIES=$(PRIVATE_ALL_SHARED_LIBRARIES);
679@echo PRIVATE_ALL_STATIC_LIBRARIES=$(PRIVATE_ALL_STATIC_LIBRARIES);
680@echo PRIVATE_ALL_WHOLE_STATIC_LIBRARIES=$(PRIVATE_ALL_WHOLE_STATIC_LIBRARIES);
681@echo PRIVATE_ALL_OBJECTS=$(PRIVATE_ALL_OBJECTS);
682@echo PRIVATE_NO_CRT=$(PRIVATE_NO_CRT);
683endef
684
685###########################################################
686## Commands for using sed to replace given variable values
687###########################################################
688
689define transform-variables
690@mkdir -p $(dir $@)
691@echo "Sed: $(if $(PRIVATE_MODULE),$(PRIVATE_MODULE),$@) <= $<"
692$(hide) sed $(foreach var,$(REPLACE_VARS),-e "s/{{$(var)}}/$(subst /,\/,$(PWD)/$($(var)))/g") $< >$@
693$(hide) if [ "$(suffix $@)" = ".sh" ]; then chmod a+rx $@; fi
694endef
695
696
697###########################################################
698## Commands for munging the dependency files GCC generates
699###########################################################
700
701define transform-d-to-p
702@cp $(@:%.o=%.d) $(@:%.o=%.P); \
703	sed -e 's/#.*//' -e 's/^[^:]*: *//' -e 's/ *\\$$//' \
704		-e '/^$$/ d' -e 's/$$/ :/' < $(@:%.o=%.d) >> $(@:%.o=%.P); \
705	rm -f $(@:%.o=%.d)
706endef
707
708###########################################################
709## Commands for running lex
710###########################################################
711
712define transform-l-to-cpp
713@mkdir -p $(dir $@)
714@echo "Lex: $(PRIVATE_MODULE) <= $<"
715$(hide) $(LEX) -o$@ $<
716endef
717
718###########################################################
719## Commands for running yacc
720##
721## Because the extension of c++ files can change, the
722## extension must be specified in $1.
723## E.g, "$(call transform-y-to-cpp,.cpp)"
724###########################################################
725
726define transform-y-to-cpp
727@mkdir -p $(dir $@)
728@echo "Yacc: $(PRIVATE_MODULE) <= $<"
729$(YACC) $(PRIVATE_YACCFLAGS) -o $@ $<
730touch $(@:$1=$(YACC_HEADER_SUFFIX))
731echo '#ifndef '$(@F:$1=_h) > $(@:$1=.h)
732echo '#define '$(@F:$1=_h) >> $(@:$1=.h)
733cat $(@:$1=$(YACC_HEADER_SUFFIX)) >> $(@:$1=.h)
734echo '#endif' >> $(@:$1=.h)
735rm -f $(@:$1=$(YACC_HEADER_SUFFIX))
736endef
737
738
739###########################################################
740## Commands for running aidl
741###########################################################
742
743define transform-aidl-to-java
744@mkdir -p $(dir $@)
745@echo "Aidl: $(PRIVATE_MODULE) <= $<"
746$(hide) $(AIDL) -d$(patsubst %.java,%.P,$@) $(PRIVATE_AIDL_FLAGS) $< $@
747endef
748#$(AIDL) $(PRIVATE_AIDL_FLAGS) $< - | indent -nut -br -npcs -l1000 > $@
749
750
751###########################################################
752## Commands for running java-event-log-tags.py
753###########################################################
754
755define transform-logtags-to-java
756@mkdir -p $(dir $@)
757@echo "logtags: $@ <= $<"
758$(hide) $(JAVATAGS) -o $@ $^
759endef
760
761
762###########################################################
763## Commands for running protoc to compile .proto into .java
764###########################################################
765
766define transform-proto-to-java
767@mkdir -p $(dir $@)
768@echo "Protoc: $@ <= $(PRIVATE_PROTO_SRC_FILES)"
769@rm -rf $(PRIVATE_PROTO_JAVA_OUTPUT_DIR)
770@mkdir -p $(PRIVATE_PROTO_JAVA_OUTPUT_DIR)
771$(hide) $(PROTOC) \
772	$(addprefix --proto_path=, $(PRIVATE_PROTO_INCLUDES)) \
773	$(PRIVATE_PROTO_JAVA_OUTPUT_OPTION)=$(PRIVATE_PROTO_JAVA_OUTPUT_DIR) \
774	$(PRIVATE_PROTOC_FLAGS) \
775	$(PRIVATE_PROTO_SRC_FILES)
776$(hide) touch $@
777endef
778
779######################################################################
780## Commands for running protoc to compile .proto into .pb.cc and .pb.h
781######################################################################
782define transform-proto-to-cc
783@mkdir -p $(dir $@)
784@echo "Protoc: $@ <= $<"
785$(hide) $(PROTOC) \
786	$(addprefix --proto_path=, $(PRIVATE_PROTO_INCLUDES)) \
787	$(PRIVATE_PROTOC_FLAGS) \
788	--cpp_out=$(PRIVATE_PROTO_CC_OUTPUT_DIR) $<
789endef
790
791
792###########################################################
793## Commands for running gcc to compile a C++ file
794###########################################################
795
796define transform-cpp-to-o
797@mkdir -p $(dir $@)
798@echo "target $(PRIVATE_ARM_MODE) C++: $(PRIVATE_MODULE) <= $<"
799$(hide) $(PRIVATE_CXX) \
800	$(foreach incdir, \
801	    $(PRIVATE_C_INCLUDES) \
802	    $(if $(PRIVATE_NO_DEFAULT_COMPILER_FLAGS),, \
803		$(PRIVATE_TARGET_PROJECT_INCLUDES) \
804		$(PRIVATE_TARGET_C_INCLUDES) \
805	     ) \
806	  , \
807	    -I $(incdir) \
808	 ) \
809	-c \
810	$(if $(PRIVATE_NO_DEFAULT_COMPILER_FLAGS),, \
811	    $(PRIVATE_TARGET_GLOBAL_CFLAGS) \
812	    $(PRIVATE_TARGET_GLOBAL_CPPFLAGS) \
813	    $(PRIVATE_ARM_CFLAGS) \
814	 ) \
815	-fno-rtti \
816	$(PRIVATE_CFLAGS) \
817	$(PRIVATE_CPPFLAGS) \
818	$(PRIVATE_DEBUG_CFLAGS) \
819	-MD -o $@ $<
820$(hide) $(transform-d-to-p)
821endef
822
823
824###########################################################
825## Commands for running gcc to compile a C file
826###########################################################
827
828# $(1): extra flags
829define transform-c-or-s-to-o-no-deps
830@mkdir -p $(dir $@)
831$(hide) $(PRIVATE_CC) \
832	$(foreach incdir, \
833	    $(PRIVATE_C_INCLUDES) \
834	    $(if $(PRIVATE_NO_DEFAULT_COMPILER_FLAGS),, \
835		$(PRIVATE_TARGET_PROJECT_INCLUDES) \
836		$(PRIVATE_TARGET_C_INCLUDES) \
837	     ) \
838	  , \
839	    -I $(incdir) \
840	 ) \
841	-c \
842	$(if $(PRIVATE_NO_DEFAULT_COMPILER_FLAGS),, \
843	    $(PRIVATE_TARGET_GLOBAL_CFLAGS) \
844	    $(PRIVATE_ARM_CFLAGS) \
845	 ) \
846	$(PRIVATE_CFLAGS) \
847	$(1) \
848	$(PRIVATE_DEBUG_CFLAGS) \
849	-MD -o $@ $<
850endef
851
852define transform-c-to-o-no-deps
853@echo "target $(PRIVATE_ARM_MODE) C: $(PRIVATE_MODULE) <= $<"
854$(call transform-c-or-s-to-o-no-deps, )
855endef
856
857define transform-s-to-o-no-deps
858@echo "target asm: $(PRIVATE_MODULE) <= $<"
859$(call transform-c-or-s-to-o-no-deps, $(PRIVATE_ASFLAGS))
860endef
861
862define transform-c-to-o
863$(transform-c-to-o-no-deps)
864$(hide) $(transform-d-to-p)
865endef
866
867define transform-s-to-o
868$(transform-s-to-o-no-deps)
869$(hide) $(transform-d-to-p)
870endef
871
872###########################################################
873## Commands for running gcc to compile an Objective-C file
874## This should never happen for target builds but this
875## will error at build time.
876###########################################################
877
878define transform-m-to-o-no-deps
879@echo "target ObjC: $(PRIVATE_MODULE) <= $<"
880$(call transform-c-or-s-to-o-no-deps)
881endef
882
883define transform-m-to-o
884$(transform-m-to-o-no-deps)
885$(hide) $(transform-d-to-p)
886endef
887
888###########################################################
889## Commands for running gcc to compile a host C++ file
890###########################################################
891
892define transform-host-cpp-to-o
893@mkdir -p $(dir $@)
894@echo "host C++: $(PRIVATE_MODULE) <= $<"
895$(hide) $(PRIVATE_CXX) \
896	$(foreach incdir, \
897	    $(PRIVATE_C_INCLUDES) \
898	    $(if $(PRIVATE_NO_DEFAULT_COMPILER_FLAGS),, \
899		$(HOST_PROJECT_INCLUDES) \
900		$(HOST_C_INCLUDES) \
901	     ) \
902	  , \
903	    -I $(incdir) \
904	 ) \
905	-c \
906	$(if $(PRIVATE_NO_DEFAULT_COMPILER_FLAGS),, \
907	    $(HOST_GLOBAL_CFLAGS) \
908	    $(HOST_GLOBAL_CPPFLAGS) \
909	 ) \
910	$(PRIVATE_CFLAGS) \
911	$(PRIVATE_CPPFLAGS) \
912	$(PRIVATE_DEBUG_CFLAGS) \
913	-MD -o $@ $<
914$(transform-d-to-p)
915endef
916
917
918###########################################################
919## Commands for running gcc to compile a host C file
920###########################################################
921
922# $(1): extra flags
923define transform-host-c-or-s-to-o-no-deps
924@mkdir -p $(dir $@)
925$(hide) $(PRIVATE_CC) \
926	$(foreach incdir, \
927	    $(PRIVATE_C_INCLUDES) \
928	    $(if $(PRIVATE_NO_DEFAULT_COMPILER_FLAGS),, \
929		$(HOST_PROJECT_INCLUDES) \
930		$(HOST_C_INCLUDES) \
931	     ) \
932	  , \
933	    -I $(incdir) \
934	 ) \
935	-c \
936	$(if $(PRIVATE_NO_DEFAULT_COMPILER_FLAGS),, \
937	    $(HOST_GLOBAL_CFLAGS) \
938	 ) \
939	$(PRIVATE_CFLAGS) \
940	$(1) \
941	$(PRIVATE_DEBUG_CFLAGS) \
942	-MD -o $@ $<
943endef
944
945define transform-host-c-to-o-no-deps
946@echo "host C: $(PRIVATE_MODULE) <= $<"
947$(call transform-host-c-or-s-to-o-no-deps, )
948endef
949
950define transform-host-s-to-o-no-deps
951@echo "host asm: $(PRIVATE_MODULE) <= $<"
952$(call transform-host-c-or-s-to-o-no-deps, $(PRIVATE_ASFLAGS))
953endef
954
955define transform-host-c-to-o
956$(transform-host-c-to-o-no-deps)
957$(transform-d-to-p)
958endef
959
960define transform-host-s-to-o
961$(transform-host-s-to-o-no-deps)
962$(transform-d-to-p)
963endef
964
965###########################################################
966## Commands for running gcc to compile a host Objective-C file
967###########################################################
968
969define transform-host-m-to-o-no-deps
970@echo "host ObjC: $(PRIVATE_MODULE) <= $<"
971$(call transform-host-c-or-s-to-o-no-deps)
972endef
973
974define transform-host-m-to-o
975$(transform-host-m-to-o-no-deps)
976$(transform-d-to-p)
977endef
978
979###########################################################
980## Commands for running ar
981###########################################################
982
983define _concat-if-arg2-not-empty
984$(if $(2),$(hide) $(1) $(2))
985endef
986
987# Split long argument list into smaller groups and call the command repeatedly
988#
989# $(1): the command without arguments
990# $(2): the arguments
991define split-long-arguments
992$(call _concat-if-arg2-not-empty,$(1),$(wordlist 1,500,$(2)))
993$(call _concat-if-arg2-not-empty,$(1),$(wordlist 501,1000,$(2)))
994$(call _concat-if-arg2-not-empty,$(1),$(wordlist 1001,1500,$(2)))
995$(call _concat-if-arg2-not-empty,$(1),$(wordlist 1501,2000,$(2)))
996$(call _concat-if-arg2-not-empty,$(1),$(wordlist 2001,2500,$(2)))
997$(call _concat-if-arg2-not-empty,$(1),$(wordlist 2501,3000,$(2)))
998$(call _concat-if-arg2-not-empty,$(1),$(wordlist 3001,99999,$(2)))
999endef
1000
1001define extract-and-include-target-whole-static-libs
1002$(foreach lib,$(PRIVATE_ALL_WHOLE_STATIC_LIBRARIES), \
1003	$(hide) echo "preparing StaticLib: $(PRIVATE_MODULE) [including $(lib)]"; \
1004	ldir=$(PRIVATE_INTERMEDIATES_DIR)/WHOLE/$(basename $(notdir $(lib)))_objs;\
1005	rm -rf $$ldir; \
1006	mkdir -p $$ldir; \
1007	filelist=; \
1008	for f in `$(TARGET_AR) t $(lib)`; do \
1009	    $(TARGET_AR) p $(lib) $$f > $$ldir/$$f; \
1010	    filelist="$$filelist $$ldir/$$f"; \
1011	done ; \
1012	$(TARGET_AR) $(TARGET_GLOBAL_ARFLAGS) $(PRIVATE_ARFLAGS) $@ $$filelist;\
1013)
1014endef
1015
1016# Explicitly delete the archive first so that ar doesn't
1017# try to add to an existing archive.
1018define transform-o-to-static-lib
1019@mkdir -p $(dir $@)
1020@rm -f $@
1021$(extract-and-include-target-whole-static-libs)
1022@echo "target StaticLib: $(PRIVATE_MODULE) ($@)"
1023$(call split-long-arguments,$(TARGET_AR) $(TARGET_GLOBAL_ARFLAGS) $(PRIVATE_ARFLAGS) $@,$(filter %.o, $^))
1024endef
1025
1026###########################################################
1027## Commands for running host ar
1028###########################################################
1029
1030define extract-and-include-host-whole-static-libs
1031$(foreach lib,$(PRIVATE_ALL_WHOLE_STATIC_LIBRARIES), \
1032	$(hide) echo "preparing StaticLib: $(PRIVATE_MODULE) [including $(lib)]"; \
1033	ldir=$(PRIVATE_INTERMEDIATES_DIR)/WHOLE/$(basename $(notdir $(lib)))_objs;\
1034	rm -rf $$ldir; \
1035	mkdir -p $$ldir; \
1036	filelist=; \
1037	for f in `$(HOST_AR) t $(lib) | grep '\.o$$'`; do \
1038	    $(HOST_AR) p $(lib) $$f > $$ldir/$$f; \
1039	    filelist="$$filelist $$ldir/$$f"; \
1040	done ; \
1041	$(HOST_AR) $(HOST_GLOBAL_ARFLAGS) $(PRIVATE_ARFLAGS) $@ $$filelist;\
1042)
1043endef
1044
1045# Explicitly delete the archive first so that ar doesn't
1046# try to add to an existing archive.
1047define transform-host-o-to-static-lib
1048@mkdir -p $(dir $@)
1049@rm -f $@
1050$(extract-and-include-host-whole-static-libs)
1051@echo "host StaticLib: $(PRIVATE_MODULE) ($@)"
1052$(call split-long-arguments,$(HOST_AR) $(HOST_GLOBAL_ARFLAGS) $(PRIVATE_ARFLAGS) $@,$(filter %.o, $^))
1053endef
1054
1055
1056###########################################################
1057## Commands for running gcc to link a shared library or package
1058###########################################################
1059
1060# ld just seems to be so finicky with command order that we allow
1061# it to be overriden en-masse see combo/linux-arm.make for an example.
1062ifneq ($(HOST_CUSTOM_LD_COMMAND),true)
1063define transform-host-o-to-shared-lib-inner
1064$(PRIVATE_CXX) \
1065	-Wl,-rpath-link=$(TARGET_OUT_INTERMEDIATE_LIBRARIES) \
1066	-Wl,-rpath,\$$ORIGIN/../lib \
1067	-shared -Wl,-soname,$(notdir $@) \
1068	$(PRIVATE_LDFLAGS) \
1069	$(HOST_GLOBAL_LD_DIRS) \
1070	$(if $(PRIVATE_NO_DEFAULT_COMPILER_FLAGS),, \
1071	   $(HOST_GLOBAL_LDFLAGS) \
1072	) \
1073	$(PRIVATE_ALL_OBJECTS) \
1074	-Wl,--whole-archive \
1075	$(call normalize-host-libraries,$(PRIVATE_ALL_WHOLE_STATIC_LIBRARIES)) \
1076	-Wl,--no-whole-archive \
1077	$(call normalize-host-libraries,$(PRIVATE_ALL_STATIC_LIBRARIES)) \
1078	$(call normalize-host-libraries,$(PRIVATE_ALL_SHARED_LIBRARIES)) \
1079	-o $@ \
1080	$(PRIVATE_LDLIBS)
1081endef
1082endif
1083
1084define transform-host-o-to-shared-lib
1085@mkdir -p $(dir $@)
1086@echo "host SharedLib: $(PRIVATE_MODULE) ($@)"
1087$(hide) $(transform-host-o-to-shared-lib-inner)
1088endef
1089
1090define transform-host-o-to-package
1091@mkdir -p $(dir $@)
1092@echo "host Package: $(PRIVATE_MODULE) ($@)"
1093$(hide) $(transform-host-o-to-shared-lib-inner)
1094endef
1095
1096
1097###########################################################
1098## Commands for running gcc to link a shared library or package
1099###########################################################
1100
1101#echo >$@.vers "{"; \
1102#echo >>$@.vers " global:"; \
1103#$(BUILD_SYSTEM)/filter_symbols.sh $(TARGET_NM) "  " ";" $(filter %.o,$^) | sort -u >>$@.vers; \
1104#echo >>$@.vers " local:"; \
1105#echo >>$@.vers "  *;"; \
1106#echo >>$@.vers "};"; \
1107
1108#	-Wl,--version-script=$@.vers \
1109
1110# ld just seems to be so finicky with command order that we allow
1111# it to be overriden en-masse see combo/linux-arm.make for an example.
1112ifneq ($(TARGET_CUSTOM_LD_COMMAND),true)
1113define transform-o-to-shared-lib-inner
1114$(PRIVATE_CXX) \
1115	$(PRIVATE_TARGET_GLOBAL_LDFLAGS) \
1116	-Wl,-rpath-link=$(TARGET_OUT_INTERMEDIATE_LIBRARIES) \
1117	-Wl,-rpath,\$$ORIGIN/../lib \
1118	-shared -Wl,-soname,$(notdir $@) \
1119	$(PRIVATE_LDFLAGS) \
1120	$(PRIVATE_TARGET_GLOBAL_LD_DIRS) \
1121	$(PRIVATE_ALL_OBJECTS) \
1122	-Wl,--whole-archive \
1123	$(call normalize-host-libraries,$(PRIVATE_ALL_WHOLE_STATIC_LIBRARIES)) \
1124	-Wl,--no-whole-archive \
1125	$(call normalize-target-libraries,$(PRIVATE_ALL_STATIC_LIBRARIES)) \
1126	$(call normalize-target-libraries,$(PRIVATE_ALL_SHARED_LIBRARIES)) \
1127	-o $@ \
1128	$(PRIVATE_LDLIBS)
1129endef
1130endif
1131
1132define transform-o-to-shared-lib
1133@mkdir -p $(dir $@)
1134@echo "target SharedLib: $(PRIVATE_MODULE) ($@)"
1135$(hide) $(transform-o-to-shared-lib-inner)
1136endef
1137
1138define transform-o-to-package
1139@mkdir -p $(dir $@)
1140@echo "target Package: $(PRIVATE_MODULE) ($@)"
1141$(hide) $(transform-o-to-shared-lib-inner)
1142endef
1143
1144
1145###########################################################
1146## Commands for filtering a target executable or library
1147###########################################################
1148
1149define transform-to-stripped
1150@mkdir -p $(dir $@)
1151@echo "target Strip: $(PRIVATE_MODULE) ($@)"
1152$(hide) $(TARGET_STRIP_COMMAND)
1153endef
1154
1155define transform-to-prelinked
1156@mkdir -p $(dir $@)
1157@echo "target Prelink: $(PRIVATE_MODULE) ($@)"
1158$(hide) $(APRIORI) \
1159		--prelinkmap $(TARGET_PRELINKER_MAP) \
1160		--locals-only \
1161		--quiet \
1162		$< \
1163		--output $@
1164endef
1165
1166
1167###########################################################
1168## Commands for running gcc to link an executable
1169###########################################################
1170
1171ifneq ($(TARGET_CUSTOM_LD_COMMAND),true)
1172define transform-o-to-executable-inner
1173$(PRIVATE_CXX) \
1174	$(TARGET_GLOBAL_LDFLAGS) \
1175	-Wl,-rpath-link=$(TARGET_OUT_INTERMEDIATE_LIBRARIES) \
1176	$(TARGET_GLOBAL_LD_DIRS) \
1177	-Wl,-rpath-link=$(TARGET_OUT_INTERMEDIATE_LIBRARIES) \
1178	-Wl,-rpath,\$$ORIGIN/../lib \
1179	$(PRIVATE_LDFLAGS) \
1180	$(TARGET_GLOBAL_LD_DIRS) \
1181	$(PRIVATE_ALL_OBJECTS) \
1182	-Wl,--whole-archive \
1183	$(call normalize-target-libraries,$(PRIVATE_ALL_WHOLE_STATIC_LIBRARIES)) \
1184	-Wl,--no-whole-archive \
1185	$(call normalize-target-libraries,$(PRIVATE_ALL_STATIC_LIBRARIES)) \
1186	$(call normalize-target-libraries,$(PRIVATE_ALL_SHARED_LIBRARIES)) \
1187	-o $@ \
1188	$(PRIVATE_LDLIBS)
1189endef
1190endif
1191
1192define transform-o-to-executable
1193@mkdir -p $(dir $@)
1194@echo "target Executable: $(PRIVATE_MODULE) ($@)"
1195$(hide) $(transform-o-to-executable-inner)
1196endef
1197
1198
1199###########################################################
1200## Commands for running gcc to link a statically linked
1201## executable.  In practice, we only use this on arm, so
1202## the other platforms don't have the
1203## transform-o-to-static-executable defined
1204###########################################################
1205
1206ifneq ($(TARGET_CUSTOM_LD_COMMAND),true)
1207define transform-o-to-static-executable-inner
1208endef
1209endif
1210
1211define transform-o-to-static-executable
1212@mkdir -p $(dir $@)
1213@echo "target StaticExecutable: $(PRIVATE_MODULE) ($@)"
1214$(hide) $(transform-o-to-static-executable-inner)
1215endef
1216
1217
1218###########################################################
1219## Commands for running gcc to link a host executable
1220###########################################################
1221
1222ifneq ($(HOST_CUSTOM_LD_COMMAND),true)
1223define transform-host-o-to-executable-inner
1224$(PRIVATE_CXX) \
1225	-Wl,-rpath-link=$(TARGET_OUT_INTERMEDIATE_LIBRARIES) \
1226	-Wl,-rpath,\$$ORIGIN/../lib \
1227	$(HOST_GLOBAL_LD_DIRS) \
1228	$(PRIVATE_LDFLAGS) \
1229	$(if $(PRIVATE_NO_DEFAULT_COMPILER_FLAGS),, \
1230		$(HOST_GLOBAL_LDFLAGS) \
1231	) \
1232	$(PRIVATE_ALL_OBJECTS) \
1233	-Wl,--whole-archive \
1234	$(call normalize-host-libraries,$(PRIVATE_ALL_WHOLE_STATIC_LIBRARIES)) \
1235	-Wl,--no-whole-archive \
1236	$(call normalize-host-libraries,$(PRIVATE_ALL_STATIC_LIBRARIES)) \
1237	$(call normalize-host-libraries,$(PRIVATE_ALL_SHARED_LIBRARIES)) \
1238	-o $@ \
1239	$(PRIVATE_LDLIBS)
1240endef
1241endif
1242
1243define transform-host-o-to-executable
1244@mkdir -p $(dir $@)
1245@echo "host Executable: $(PRIVATE_MODULE) ($@)"
1246$(hide) $(transform-host-o-to-executable-inner)
1247endef
1248
1249
1250###########################################################
1251## Commands for running javac to make .class files
1252###########################################################
1253
1254#@echo "Source intermediates dir: $(PRIVATE_SOURCE_INTERMEDIATES_DIR)"
1255#@echo "Source intermediates: $$(find $(PRIVATE_SOURCE_INTERMEDIATES_DIR) -name '*.java')"
1256
1257# TODO: Right now we generate the asset resources twice, first as part
1258# of generating the Java classes, then at the end when packaging the final
1259# assets.  This should be changed to do one of two things: (1) Don't generate
1260# any resource files the first time, only create classes during that stage;
1261# or (2) Don't use the -c flag with the second stage, instead taking the
1262# resource files from the first stage as additional input.  My original intent
1263# was to use approach (2), but this requires a little more work in the tool.
1264# Maybe we should just use approach (1).
1265
1266# This rule creates the R.java and Manifest.java files, both of which
1267# are PRODUCT-neutral.  Don't pass PRODUCT_AAPT_CONFIG to this invocation.
1268define create-resource-java-files
1269@mkdir -p $(PRIVATE_SOURCE_INTERMEDIATES_DIR)
1270@mkdir -p $(dir $(PRIVATE_RESOURCE_PUBLICS_OUTPUT))
1271$(hide) $(AAPT) package $(PRIVATE_AAPT_FLAGS) -m \
1272    $(eval # PRODUCT_AAPT_CONFIG is intentionally missing-- see comment.) \
1273    $(addprefix -J , $(PRIVATE_SOURCE_INTERMEDIATES_DIR)) \
1274    $(addprefix -M , $(PRIVATE_ANDROID_MANIFEST)) \
1275    $(addprefix -P , $(PRIVATE_RESOURCE_PUBLICS_OUTPUT)) \
1276    $(addprefix -S , $(PRIVATE_RESOURCE_DIR)) \
1277    $(addprefix -A , $(PRIVATE_ASSET_DIR)) \
1278    $(addprefix -I , $(PRIVATE_AAPT_INCLUDES)) \
1279    $(addprefix -G , $(PRIVATE_PROGUARD_OPTIONS_FILE)) \
1280    $(addprefix --min-sdk-version , $(DEFAULT_APP_TARGET_SDK)) \
1281    $(addprefix --target-sdk-version , $(DEFAULT_APP_TARGET_SDK)) \
1282    $(if $(filter --version-code,$(PRIVATE_AAPT_FLAGS)),,$(addprefix --version-code , $(PLATFORM_SDK_VERSION))) \
1283    $(if $(filter --version-name,$(PRIVATE_AAPT_FLAGS)),,$(addprefix --version-name , $(PLATFORM_VERSION))) \
1284    $(addprefix --rename-manifest-package , $(PRIVATE_MANIFEST_PACKAGE_NAME)) \
1285    $(addprefix --rename-instrumentation-target-package , $(PRIVATE_MANIFEST_INSTRUMENTATION_FOR))
1286endef
1287
1288ifeq ($(HOST_OS),windows)
1289xlint_unchecked :=
1290else
1291xlint_unchecked := -Xlint:unchecked
1292endif
1293
1294# emit-line, <word list>, <output file>
1295define emit-line
1296   $(if $(1),echo -n '$(strip $(1)) ' >> $(2))
1297endef
1298
1299# dump-words-to-file, <word list>, <output file>
1300define dump-words-to-file
1301        @rm -f $(2)
1302        @$(call emit-line,$(wordlist 1,200,$(1)),$(2))
1303        @$(call emit-line,$(wordlist 201,400,$(1)),$(2))
1304        @$(call emit-line,$(wordlist 401,600,$(1)),$(2))
1305        @$(call emit-line,$(wordlist 601,800,$(1)),$(2))
1306        @$(call emit-line,$(wordlist 801,1000,$(1)),$(2))
1307        @$(call emit-line,$(wordlist 1001,1200,$(1)),$(2))
1308        @$(call emit-line,$(wordlist 1201,1400,$(1)),$(2))
1309        @$(call emit-line,$(wordlist 1401,1600,$(1)),$(2))
1310        @$(call emit-line,$(wordlist 1601,1800,$(1)),$(2))
1311        @$(call emit-line,$(wordlist 1801,2000,$(1)),$(2))
1312        @$(call emit-line,$(wordlist 2001,2200,$(1)),$(2))
1313        @$(call emit-line,$(wordlist 2201,2400,$(1)),$(2))
1314        @$(call emit-line,$(wordlist 2401,2600,$(1)),$(2))
1315        @$(call emit-line,$(wordlist 2601,2800,$(1)),$(2))
1316        @$(call emit-line,$(wordlist 2801,3000,$(1)),$(2))
1317        @$(call emit-line,$(wordlist 3001,3200,$(1)),$(2))
1318        @$(call emit-line,$(wordlist 3201,3400,$(1)),$(2))
1319        @$(call emit-line,$(wordlist 3401,3600,$(1)),$(2))
1320        @$(call emit-line,$(wordlist 3601,3800,$(1)),$(2))
1321        @$(call emit-line,$(wordlist 3801,4000,$(1)),$(2))
1322        @$(if $(wordlist 4001,4002,$(1)),$(error Too many words ($(words $(1)))))
1323endef
1324
1325# For a list of jar files, unzip them to a specified directory,
1326# but make sure that no META-INF files come along for the ride.
1327#
1328# $(1): files to unzip
1329# $(2): destination directory
1330define unzip-jar-files
1331  $(hide) for f in $(1); \
1332  do \
1333    if [ ! -f $$f ]; then \
1334      echo Missing file $$f; \
1335      exit 1; \
1336    fi; \
1337    unzip -qo $$f -d $(2); \
1338    (cd $(2) && rm -rf META-INF); \
1339  done
1340endef
1341
1342# below we write the list of java files to java-source-list to avoid argument
1343# list length problems with Cygwin we filter out duplicate java file names
1344# because eclipse's compiler doesn't like them.
1345define transform-java-to-classes.jar
1346@echo "target Java: $(PRIVATE_MODULE) ($(PRIVATE_CLASS_INTERMEDIATES_DIR))"
1347$(hide) rm -f $@
1348$(hide) rm -rf $(PRIVATE_CLASS_INTERMEDIATES_DIR)
1349$(hide) mkdir -p $(PRIVATE_CLASS_INTERMEDIATES_DIR)
1350$(call unzip-jar-files,$(PRIVATE_STATIC_JAVA_LIBRARIES), \
1351    $(PRIVATE_CLASS_INTERMEDIATES_DIR))
1352$(call dump-words-to-file,$(PRIVATE_JAVA_SOURCES),$(dir $(PRIVATE_CLASS_INTERMEDIATES_DIR))/java-source-list)
1353$(hide) if [ -d "$(PRIVATE_SOURCE_INTERMEDIATES_DIR)" ]; then \
1354	    find $(PRIVATE_SOURCE_INTERMEDIATES_DIR) -name '*.java' >> $(dir $(PRIVATE_CLASS_INTERMEDIATES_DIR))/java-source-list; \
1355fi
1356$(hide) tr ' ' '\n' < $(dir $(PRIVATE_CLASS_INTERMEDIATES_DIR))/java-source-list \
1357    | sort -u > $(dir $(PRIVATE_CLASS_INTERMEDIATES_DIR))/java-source-list-uniq
1358$(hide) $(TARGET_JAVAC) -encoding ascii $(PRIVATE_BOOTCLASSPATH) \
1359    $(addprefix -classpath ,$(strip \
1360        $(call normalize-path-list,$(PRIVATE_ALL_JAVA_LIBRARIES)))) \
1361    $(PRIVATE_JAVACFLAGS) $(strip $(PRIVATE_JAVAC_DEBUG_FLAGS)) \
1362	$(if $(findstring true,$(LOCAL_WARNINGS_ENABLE)),$(xlint_unchecked),) \
1363    -extdirs "" -d $(PRIVATE_CLASS_INTERMEDIATES_DIR) \
1364    \@$(dir $(PRIVATE_CLASS_INTERMEDIATES_DIR))/java-source-list-uniq \
1365    || ( rm -rf $(PRIVATE_CLASS_INTERMEDIATES_DIR) ; exit 41 )
1366$(hide) rm -f $(dir $(PRIVATE_CLASS_INTERMEDIATES_DIR))/java-source-list
1367$(hide) rm -f $(dir $(PRIVATE_CLASS_INTERMEDIATES_DIR))/java-source-list-uniq
1368$(hide) mkdir -p $(dir $@)
1369$(hide) jar $(if $(strip $(PRIVATE_JAR_MANIFEST)),-cfm,-cf) \
1370    $@ $(PRIVATE_JAR_MANIFEST) -C $(PRIVATE_CLASS_INTERMEDIATES_DIR) .
1371$(hide) rm -rf $(PRIVATE_CLASS_INTERMEDIATES_DIR)
1372endef
1373
1374define transform-classes.jar-to-emma
1375$(hide) java -classpath $(EMMA_JAR) emma instr -outmode fullcopy -outfile \
1376    $(PRIVATE_EMMA_COVERAGE_FILE) -ip $< -d $(PRIVATE_EMMA_INTERMEDIATES_DIR) \
1377    $(addprefix -ix , $(PRIVATE_EMMA_COVERAGE_FILTER))
1378endef
1379
1380#TODO: use a smaller -Xmx value for most libraries;
1381#      only core.jar and framework.jar need a heap this big.
1382# Avoid the memory arguments on Windows, dx fails to load for some reason with them.
1383define transform-classes.jar-to-dex
1384@echo "target Dex: $(PRIVATE_MODULE)"
1385@mkdir -p $(dir $@)
1386$(hide) $(DX) \
1387    $(if $(findstring windows,$(HOST_OS)),,-JXms16M -JXmx1536M) \
1388    --dex --output=$@ \
1389    $(if $(NO_OPTIMIZE_DX), \
1390        --no-optimize) \
1391    $(if $(GENERATE_DEX_DEBUG), \
1392	    --debug --verbose \
1393	    --dump-to=$(@:.dex=.lst) \
1394	    --dump-width=1000) \
1395    $(PRIVATE_DX_FLAGS) \
1396    $<
1397endef
1398
1399# Create a mostly-empty .jar file that we'll add to later.
1400# The MacOS jar tool doesn't like creating empty jar files,
1401# so we need to give it something.
1402define create-empty-package
1403@mkdir -p $(dir $@)
1404$(hide) touch $(dir $@)/dummy
1405$(hide) (cd $(dir $@) && jar cf $(notdir $@) dummy)
1406$(hide) zip -qd $@ dummy
1407$(hide) rm $(dir $@)/dummy
1408endef
1409
1410#TODO: we kinda want to build different asset packages for
1411#      different configurations, then combine them later (or something).
1412#      Per-locale, etc.
1413#      A list of dynamic and static parameters;  build layers for
1414#      dynamic params that lay over the static ones.
1415#TODO: update the manifest to point to the package file
1416#Note that the version numbers are given to aapt as simple default
1417#values; applications can override these by explicitly stating
1418#them in their manifest.
1419define add-assets-to-package
1420$(hide) $(AAPT) package -u $(PRIVATE_AAPT_FLAGS) \
1421    $(addprefix -c , $(PRODUCT_AAPT_CONFIG)) \
1422    $(addprefix -M , $(PRIVATE_ANDROID_MANIFEST)) \
1423    $(addprefix -S , $(PRIVATE_RESOURCE_DIR)) \
1424    $(addprefix -A , $(PRIVATE_ASSET_DIR)) \
1425    $(addprefix -I , $(PRIVATE_AAPT_INCLUDES)) \
1426    $(addprefix --min-sdk-version , $(DEFAULT_APP_TARGET_SDK)) \
1427    $(addprefix --target-sdk-version , $(DEFAULT_APP_TARGET_SDK)) \
1428    $(addprefix --product , $(TARGET_AAPT_CHARACTERISTICS)) \
1429    $(if $(filter --version-code,$(PRIVATE_AAPT_FLAGS)),,$(addprefix --version-code , $(PLATFORM_SDK_VERSION))) \
1430    $(if $(filter --version-name,$(PRIVATE_AAPT_FLAGS)),,$(addprefix --version-name , $(PLATFORM_VERSION))) \
1431    $(addprefix --rename-manifest-package , $(PRIVATE_MANIFEST_PACKAGE_NAME)) \
1432    $(addprefix --rename-instrumentation-target-package , $(PRIVATE_MANIFEST_INSTRUMENTATION_FOR)) \
1433    -F $@
1434endef
1435
1436define add-jni-shared-libs-to-package
1437$(hide) rm -rf $(dir $@)lib
1438$(hide) mkdir -p $(dir $@)lib/$(TARGET_CPU_ABI)
1439$(hide) cp $(PRIVATE_JNI_SHARED_LIBRARIES) $(dir $@)lib/$(TARGET_CPU_ABI)
1440$(hide) (cd $(dir $@) && zip -r $(notdir $@) lib)
1441$(hide) rm -rf $(dir $@)lib
1442endef
1443
1444#TODO: update the manifest to point to the dex file
1445define add-dex-to-package
1446$(if $(filter classes.dex,$(notdir $(PRIVATE_DEX_FILE))),\
1447$(hide) $(AAPT) add -k $@ $(PRIVATE_DEX_FILE),\
1448$(eval _adtp_classes.dex := $(dir $(PRIVATE_DEX_FILE))/classes.dex)\
1449$(hide) cp $(PRIVATE_DEX_FILE) $(_adtp_classes.dex) && \
1450$(AAPT) add -k $@ $(_adtp_classes.dex) && \
1451rm -f $(_adtp_classes.dex))
1452endef
1453
1454define add-java-resources-to-package
1455$(hide) jar uf $@ $(PRIVATE_EXTRA_JAR_ARGS)
1456endef
1457
1458# Sign a package using the specified key/cert.
1459#
1460define sign-package
1461$(hide) mv $@ $@.unsigned
1462$(hide) java -jar $(SIGNAPK_JAR) \
1463	$(PRIVATE_CERTIFICATE) $(PRIVATE_PRIVATE_KEY) $@.unsigned $@.signed
1464$(hide) mv $@.signed $@
1465endef
1466
1467# Align STORED entries of a package on 4-byte boundaries
1468# to make them easier to mmap.
1469#
1470define align-package
1471$(hide) mv $@ $@.unaligned
1472$(hide) $(ZIPALIGN) -f 4 $@.unaligned $@.aligned
1473$(hide) mv $@.aligned $@
1474endef
1475
1476define install-dex-debug
1477$(hide) if [ -f "$(PRIVATE_INTERMEDIATES_DIR)/classes.dex" ]; then \
1478	    mkdir -p $(TOP)/dalvik/DEBUG-FILES; \
1479	    $(ACP) $(PRIVATE_INTERMEDIATES_DIR)/classes.dex \
1480		$(TOP)/dalvik/DEBUG-FILES/$(PRIVATE_MODULE).dex; \
1481	fi
1482$(hide) if [ -f "$(PRIVATE_INTERMEDIATES_DIR)/classes.lst" ]; then \
1483	    mkdir -p $(TOP)/dalvik/DEBUG-FILES; \
1484	    $(ACP) $(PRIVATE_INTERMEDIATES_DIR)/classes.lst \
1485		$(TOP)/dalvik/DEBUG-FILES/$(PRIVATE_MODULE).lst; \
1486	fi
1487endef
1488
1489# TODO(joeo): If we can ever upgrade to post 3.81 make and get the
1490# new prebuilt rules to work, we should change this to copy the
1491# resources to the out directory and then copy the resources.
1492
1493# Note: not using aapt tool for this because we aren't making
1494# an android package for the host.
1495define transform-host-java-to-package
1496@echo "host Java: $(PRIVATE_MODULE) ($(PRIVATE_CLASS_INTERMEDIATES_DIR))"
1497@rm -f $@
1498@rm -rf $(PRIVATE_CLASS_INTERMEDIATES_DIR)
1499@mkdir -p $(dir $@)
1500@mkdir -p $(PRIVATE_CLASS_INTERMEDIATES_DIR)
1501$(call unzip-jar-files,$(PRIVATE_STATIC_JAVA_LIBRARIES), \
1502    $(PRIVATE_CLASS_INTERMEDIATES_DIR))
1503$(call dump-words-to-file,$(sort\
1504	$(PRIVATE_JAVA_SOURCES)),\
1505	$(PRIVATE_INTERMEDIATES_DIR)/java-source-list-uniq)
1506$(hide) $(HOST_JAVAC) -encoding ascii -g \
1507	$(PRIVATE_JAVACFLAGS) $(xlint_unchecked) \
1508	$(addprefix -classpath ,$(strip \
1509		$(call normalize-path-list,$(PRIVATE_ALL_JAVA_LIBRARIES)))) \
1510	-extdirs "" -d $(PRIVATE_CLASS_INTERMEDIATES_DIR)\
1511        \@$(PRIVATE_INTERMEDIATES_DIR)/java-source-list-uniq || \
1512	( rm -rf $(PRIVATE_CLASS_INTERMEDIATES_DIR) ; exit 41 )
1513$(hide) rm -f $(PRIVATE_INTERMEDIATES_DIR)/java-source-list
1514$(hide) rm -f $(PRIVATE_INTERMEDIATES_DIR)/java-source-list-uniq
1515$(hide) jar $(if $(strip $(PRIVATE_JAR_MANIFEST)),-cfm,-cf) \
1516    $@ $(PRIVATE_JAR_MANIFEST) $(PRIVATE_EXTRA_JAR_ARGS) \
1517    -C $(PRIVATE_CLASS_INTERMEDIATES_DIR) .
1518endef
1519
1520###########################################################
1521## Obfuscate a jar file
1522###########################################################
1523
1524# PRIVATE_KEEP_FILE is a file containing a list of classes
1525# PRIVATE_INTERMEDIATES_DIR is a directory we can use for temporary files
1526# The module using this must depend on
1527#        $(HOST_OUT_JAVA_LIBRARIES)/proguard-4.0.1.jar
1528define obfuscate-jar
1529@echo "Obfuscate jar: $(notdir $@) ($@)"
1530@mkdir -p $(dir $@)
1531@rm -f $@
1532@mkdir -p $(PRIVATE_INTERMEDIATES_DIR)
1533$(hide) sed -e 's/^/-keep class /' < $(PRIVATE_KEEP_FILE) > \
1534		$(PRIVATE_INTERMEDIATES_DIR)/keep.pro
1535$(hide) java -Xmx512M -jar $(HOST_OUT_JAVA_LIBRARIES)/proguard-4.0.1.jar \
1536		-injars $< \
1537		-outjars $@ \
1538		-target 1.5 \
1539		-dontnote -dontwarn \
1540		-printmapping $(PRIVATE_INTERMEDIATES_DIR)/out.map \
1541		-forceprocessing \
1542		-renamesourcefileattribute SourceFile \
1543		-keepattributes Exceptions,InnerClasses,Signature,Deprecated,SourceFile,LineNumberTable,*Annotation*,EnclosingMethod \
1544		-repackageclasses \
1545		-keepclassmembers "class * { public protected *; }" \
1546		@$(PRIVATE_INTERMEDIATES_DIR)/keep.pro
1547endef
1548
1549###########################################################
1550## Commands for copying files
1551###########################################################
1552
1553# Define a rule to copy a header.  Used via $(eval) by copy_headers.make.
1554# $(1): source header
1555# $(2): destination header
1556define copy-one-header
1557$(2): $(1)
1558	@echo "Header: $$@"
1559	$$(copy-file-to-new-target-with-cp)
1560endef
1561
1562# Define a rule to copy a file.  For use via $(eval).
1563# $(1): source file
1564# $(2): destination file
1565define copy-one-file
1566$(2): $(1) | $(ACP)
1567	@echo "Copy: $$@"
1568	$$(copy-file-to-target)
1569endef
1570
1571# The -t option to acp and the -p option to cp is
1572# required for OSX.  OSX has a ridiculous restriction
1573# where it's an error for a .a file's modification time
1574# to disagree with an internal timestamp, and this
1575# macro is used to install .a files (among other things).
1576
1577# Copy a single file from one place to another,
1578# preserving permissions and overwriting any existing
1579# file.
1580define copy-file-to-target
1581@mkdir -p $(dir $@)
1582$(hide) $(ACP) -fpt $< $@
1583endef
1584
1585# The same as copy-file-to-target, but use the local
1586# cp command instead of acp.
1587define copy-file-to-target-with-cp
1588@mkdir -p $(dir $@)
1589$(hide) cp -fp $< $@
1590endef
1591
1592# The same as copy-file-to-target, but use the zipalign tool to do so.
1593define copy-file-to-target-with-zipalign
1594@mkdir -p $(dir $@)
1595$(hide) $(ZIPALIGN) -f 4 $< $@
1596endef
1597
1598# The same as copy-file-to-target, but strip out "# comment"-style
1599# comments (for config files and such).
1600define copy-file-to-target-strip-comments
1601@mkdir -p $(dir $@)
1602$(hide) sed -e 's/#.*$$//' -e 's/[ \t]*$$//' -e '/^$$/d' < $< > $@
1603endef
1604
1605# The same as copy-file-to-target, but don't preserve
1606# the old modification time.
1607define copy-file-to-new-target
1608@mkdir -p $(dir $@)
1609$(hide) $(ACP) -fp $< $@
1610endef
1611
1612# The same as copy-file-to-new-target, but use the local
1613# cp command instead of acp.
1614define copy-file-to-new-target-with-cp
1615@mkdir -p $(dir $@)
1616$(hide) cp -f $< $@
1617endef
1618
1619# Copy a prebuilt file to a target location.
1620define transform-prebuilt-to-target
1621@echo "$(if $(PRIVATE_IS_HOST_MODULE),host,target) Prebuilt: $(PRIVATE_MODULE) ($@)"
1622$(copy-file-to-target)
1623endef
1624
1625# Copy a prebuilt file to a target location, using zipalign on it.
1626define transform-prebuilt-to-target-with-zipalign
1627@echo "$(if $(PRIVATE_IS_HOST_MODULE),host,target) Prebuilt APK: $(PRIVATE_MODULE) ($@)"
1628$(copy-file-to-target-with-zipalign)
1629endef
1630
1631# Copy a prebuilt file to a target location, stripping "# comment" comments.
1632define transform-prebuilt-to-target-strip-comments
1633@echo "$(if $(PRIVATE_IS_HOST_MODULE),host,target) Prebuilt: $(PRIVATE_MODULE) ($@)"
1634$(copy-file-to-target-strip-comments)
1635endef
1636
1637###########################################################
1638## On some platforms (MacOS), after copying a static
1639## library, ranlib must be run to update an internal
1640## timestamp!?!?!
1641###########################################################
1642
1643ifeq ($(HOST_RUN_RANLIB_AFTER_COPYING),true)
1644define transform-host-ranlib-copy-hack
1645    $(hide) ranlib $@ || true
1646endef
1647else
1648define transform-host-ranlib-copy-hack
1649true
1650endef
1651endif
1652
1653ifeq ($(TARGET_RUN_RANLIB_AFTER_COPYING),true)
1654define transform-ranlib-copy-hack
1655    $(hide) ranlib $@
1656endef
1657else
1658define transform-ranlib-copy-hack
1659true
1660endef
1661endif
1662
1663
1664###########################################################
1665## Commands to call Proguard
1666###########################################################
1667
1668# Command to copy the file with acp, if proguard is disabled.
1669define proguard-disabled-commands
1670@echo Copying: $@
1671$(hide) $(ACP) $< $@
1672endef
1673
1674# Command to call Proguard
1675# $(1): extra flags for instrumentation.
1676define proguard-enabled-commands
1677@echo Proguard: $@
1678$(hide) $(PROGUARD) -injars $< -outjars $@ $(PRIVATE_PROGUARD_FLAGS) $(1)
1679endef
1680
1681# Figure out the proguard dictionary file of the module that is instrumentationed for.
1682define get-instrumentation-proguard-flags
1683$(if $(PRIVATE_INSTRUMENTATION_FOR),$(if $(ALL_MODULES.$(PRIVATE_INSTRUMENTATION_FOR).PROGUARD_ENABLED),-applymapping $(call intermediates-dir-for,APPS,$(PRIVATE_INSTRUMENTATION_FOR),,COMMON)/proguard_dictionary))
1684endef
1685
1686define transform-jar-to-proguard
1687$(eval _instrumentation_proguard_flags:=$(call get-instrumentation-proguard-flags))
1688$(eval _enable_proguard:=$(PRIVATE_PROGUARD_ENABLED)$(_instrumentation_proguard_flags))
1689$(if $(_enable_proguard),$(call proguard-enabled-commands,$(_instrumentation_proguard_flags)),$(call proguard-disabled-commands))
1690$(eval _instrumentation_proguard_flags:=)
1691$(eval _enable_proguard:=)
1692endef
1693
1694
1695###########################################################
1696## Stuff source generated from one-off tools
1697###########################################################
1698
1699define transform-generated-source
1700@echo "target Generated: $(PRIVATE_MODULE) <= $<"
1701@mkdir -p $(dir $@)
1702$(hide) $(PRIVATE_CUSTOM_TOOL)
1703endef
1704
1705
1706###########################################################
1707## Assertions about attributes of the target
1708###########################################################
1709
1710# $(1): The file to check
1711ifndef get-file-size
1712$(error HOST_OS must define get-file-size)
1713endif
1714
1715# Convert a partition data size (eg, as reported in /proc/mtd) to the
1716# size of the image used to flash that partition (which includes a
1717# spare area for each page).
1718# $(1): the partition data size
1719define image-size-from-data-size
1720$(shell echo $$(($(1) / $(BOARD_NAND_PAGE_SIZE) * \
1721  ($(BOARD_NAND_PAGE_SIZE)+$(BOARD_NAND_SPARE_SIZE)))))
1722endef
1723
1724# $(1): The file(s) to check (often $@)
1725# $(2): The maximum total image size, in decimal bytes
1726# $(3): the type of filesystem "yaffs" or "raw"
1727#
1728# If $(2) is empty, evaluates to "true"
1729#
1730# Reserve bad blocks.  Make sure that MAX(1% of partition size, 2 blocks)
1731# is left over after the image has been flashed.  Round the 1% up to the
1732# next whole flash block size.
1733define assert-max-file-size
1734$(if $(2), \
1735  size=$$(for i in $(1); do $(call get-file-size,$$i); echo +; done; echo 0); \
1736  total=$$(( $$( echo "$$size" ) )); \
1737  printname=$$(echo -n "$(1)" | tr " " +); \
1738  echo "$$printname total size is $$total"; \
1739  img_blocksize=$(call image-size-from-data-size,$(BOARD_FLASH_BLOCK_SIZE)); \
1740  if [ "$(3)" == "yaffs" ]; then \
1741    reservedblocks=8; \
1742  else \
1743    reservedblocks=0; \
1744  fi; \
1745  twoblocks=$$((img_blocksize * 2)); \
1746  onepct=$$((((($(2) / 100) - 1) / img_blocksize + 1) * img_blocksize)); \
1747  reserve=$$(((twoblocks > onepct ? twoblocks : onepct) + \
1748               reservedblocks * img_blocksize)); \
1749  maxsize=$$(($(2) - reserve)); \
1750  if [ "$$total" -gt "$$maxsize" ]; then \
1751    echo "error: $$printname too large ($$total > [$(2) - $$reserve])"; \
1752    false; \
1753  elif [ "$$total" -gt $$((maxsize - 32768)) ]; then \
1754    echo "WARNING: $$printname approaching size limit ($$total now; limit $$maxsize)"; \
1755  fi \
1756 , \
1757  true \
1758 )
1759endef
1760
1761# Like assert-max-file-size, but the second argument is a partition
1762# size, which we'll convert to a max image size before checking it
1763# against the files.
1764#
1765# $(1): The file(s) to check (often $@)
1766# $(2): The partition size.
1767define assert-max-image-size
1768$(if $(2), \
1769  $(call assert-max-file-size,$(1),$(call image-size-from-data-size,$(2))), \
1770  true)
1771endef
1772
1773
1774###########################################################
1775## Define device-specific radio files
1776###########################################################
1777
1778# Copy a radio image file to the output location, and add it to
1779# INSTALLED_RADIOIMAGE_TARGET.
1780# $(1): filename
1781define add-radio-file
1782  $(eval $(call add-radio-file-internal,$(1),$(notdir $(1))))
1783endef
1784define add-radio-file-internal
1785INSTALLED_RADIOIMAGE_TARGET += $$(PRODUCT_OUT)/$(2)
1786ALL_PREBUILT += $$(PRODUCT_OUT)/$(2)
1787$$(PRODUCT_OUT)/$(2) : $$(LOCAL_PATH)/$(1) | $$(ACP)
1788	$$(transform-prebuilt-to-target)
1789endef
1790
1791
1792###########################################################
1793# Override the package defined in $(1), setting the
1794# variables listed below differently.
1795#
1796#  $(1): The makefile to override (relative to the source
1797#        tree root)
1798#  $(2): Old LOCAL_PACKAGE_NAME value.
1799#  $(3): New LOCAL_PACKAGE_NAME value.
1800#  $(4): New LOCAL_MANIFEST_PACKAGE_NAME value.
1801#  $(5): New LOCAL_CERTIFICATE value.
1802#  $(6): New LOCAL_INSTRUMENTATION_FOR value.
1803#  $(7): New LOCAL_MANIFEST_INSTRUMENTATION_FOR value.
1804#
1805# Note that LOCAL_PACKAGE_OVERRIDES is NOT cleared in
1806# clear_vars.mk.
1807###########################################################
1808define inherit-package
1809  $(eval $(call inherit-package-internal,$(1),$(2),$(3),$(4),$(5),$(6),$(7)))
1810endef
1811
1812define inherit-package-internal
1813  LOCAL_PACKAGE_OVERRIDES \
1814      := $(strip $(1))||$(strip $(2))||$(strip $(3))||$(strip $(4))||&&$(strip $(5))||&&$(strip $(6))||&&$(strip $(7)) $(LOCAL_PACKAGE_OVERRIDES)
1815  include $(1)
1816  LOCAL_PACKAGE_OVERRIDES \
1817      := $(wordlist 1,$(words $(LOCAL_PACKAGE_OVERRIDES)), $(LOCAL_PACKAGE_OVERRIDES))
1818endef
1819
1820# To be used with inherit-package above
1821# Evalutes to true if the package was overridden
1822define set-inherited-package-variables
1823$(strip $(call set-inherited-package-variables-internal))
1824endef
1825
1826define keep-or-override
1827$(eval $(1) := $(if $(2),$(2),$($(1))))
1828endef
1829
1830define set-inherited-package-variables-internal
1831  $(eval _o := $(subst ||, ,$(lastword $(LOCAL_PACKAGE_OVERRIDES))))
1832  $(eval _n := $(subst ||, ,$(firstword $(LOCAL_PACKAGE_OVERRIDES))))
1833  $(if $(filter $(word 2,$(_n)),$(LOCAL_PACKAGE_NAME)), \
1834    $(eval LOCAL_PACKAGE_NAME := $(word 3,$(_o))) \
1835    $(eval LOCAL_MANIFEST_PACKAGE_NAME := $(word 4,$(_o))) \
1836    $(call keep-or-override,LOCAL_CERTIFICATE,$(patsubst &&%,%,$(word 5,$(_o)))) \
1837    $(call keep-or-override,LOCAL_INSTRUMENTATION_FOR,$(patsubst &&%,%,$(word 6,$(_o)))) \
1838    $(call keep-or-override,LOCAL_MANIFEST_INSTRUMENTATION_FOR,$(patsubst &&%,%,$(word 7,$(_o)))) \
1839    $(eval LOCAL_OVERRIDES_PACKAGES := $(sort $(LOCAL_OVERRIDES_PACKAGES) $(word 2,$(_o)))) \
1840    true \
1841  ,)
1842endef
1843
1844
1845###########################################################
1846## Other includes
1847###########################################################
1848
1849# -----------------------------------------------------------------
1850# Rules and functions to help copy important files to DIST_DIR
1851# when requested.
1852include $(BUILD_SYSTEM)/distdir.mk
1853
1854# -----------------------------------------------------------------
1855# The modules allowed to use a user tag
1856include $(BUILD_SYSTEM)/user_tags.mk
1857
1858# broken:
1859#	$(foreach file,$^,$(if $(findstring,.a,$(suffix $file)),-l$(file),$(file)))
1860
1861###########################################################
1862## Misc notes
1863###########################################################
1864
1865#DEPDIR = .deps
1866#df = $(DEPDIR)/$(*F)
1867
1868#SRCS = foo.c bar.c ...
1869
1870#%.o : %.c
1871#	@$(MAKEDEPEND); \
1872#	  cp $(df).d $(df).P; \
1873#	  sed -e 's/#.*//' -e 's/^[^:]*: *//' -e 's/ *\\$$//' \
1874#	      -e '/^$$/ d' -e 's/$$/ :/' < $(df).d >> $(df).P; \
1875#	  rm -f $(df).d
1876#	$(COMPILE.c) -o $@ $<
1877
1878#-include $(SRCS:%.c=$(DEPDIR)/%.P)
1879
1880
1881#%.o : %.c
1882#	$(COMPILE.c) -MD -o $@ $<
1883#	@cp $*.d $*.P; \
1884#	  sed -e 's/#.*//' -e 's/^[^:]*: *//' -e 's/ *\\$$//' \
1885#	      -e '/^$$/ d' -e 's/$$/ :/' < $*.d >> $*.P; \
1886#	  rm -f $*.d
1887