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