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