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