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