definitions.mk revision 946a08cf51af7bb4f4ca833cca3510e78ccb93a3
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###########################################################
914## Track source files compiled to objects
915###########################################################
916# $(1): list of sources
917# $(2): list of matching objects
918define track-src-file-obj
919$(eval $(call _track-src-file-obj,$(1)))
920endef
921define _track-src-file-obj
922i := w
923$(foreach s,$(1),
924my_tracked_src_files += $(s)
925my_src_file_obj_$(s) := $$(word $$(words $$(i)),$$(2))
926i += w)
927endef
928
929# $(1): list of sources
930# $(2): list of matching generated sources
931define track-src-file-gen
932$(eval $(call _track-src-file-gen,$(2)))
933endef
934define _track-src-file-gen
935i := w
936$(foreach s,$(1),
937my_tracked_gen_files += $(s)
938my_src_file_gen_$(s) := $$(word $$(words $$(i)),$$(1))
939i += w)
940endef
941
942# $(1): list of generated sources
943# $(2): list of matching objects
944define track-gen-file-obj
945$(call track-src-file-obj,$(foreach f,$(1),\
946  $(or $(my_src_file_gen_$(f)),$(f))),$(2))
947endef
948
949###########################################################
950## Commands for running lex
951###########################################################
952
953define transform-l-to-c-or-cpp
954@echo "Lex: $(PRIVATE_MODULE) <= $<"
955@mkdir -p $(dir $@)
956$(hide) $(LEX) -o$@ $<
957endef
958
959###########################################################
960## Commands for running yacc
961##
962###########################################################
963
964define transform-y-to-c-or-cpp
965@echo "Yacc: $(PRIVATE_MODULE) <= $<"
966@mkdir -p $(dir $@)
967$(YACC) $(PRIVATE_YACCFLAGS) \
968  --defines=$(basename $@).h \
969  -o $@ $<
970endef
971
972###########################################################
973## Commands to compile RenderScript to Java
974###########################################################
975
976## Merge multiple .d files generated by llvm-rs-cc. This is necessary
977## because ninja can handle only a single depfile per build target.
978## .d files generated by llvm-rs-cc define .stamp, .bc, and optionally
979## .java as build targets. However, there's no way to let ninja know
980## dependencies to .bc files and .java files, so we give up build
981## targets for them. As we write the .stamp file as the target by
982## ourselves, the awk script removes the first lines before the colon
983## and append a backslash to the last line to concatenate contents of
984## multiple files.
985# $(1): .d files to be merged
986# $(2): merged .d file
987define _merge-renderscript-d
988$(hide) echo '$@: $(backslash)' > $2
989$(foreach d,$1, \
990  $(hide) awk 'start { sub(/( \\)?$$/, " \\"); print } /:/ { start=1 }' < $d >> $2$(newline))
991$(hide) echo >> $2
992endef
993
994define transform-renderscripts-to-java-and-bc
995@echo "RenderScript: $(PRIVATE_MODULE) <= $(PRIVATE_RS_SOURCE_FILES)"
996$(hide) rm -rf $(PRIVATE_RS_OUTPUT_DIR)
997$(hide) mkdir -p $(PRIVATE_RS_OUTPUT_DIR)/res/raw
998$(hide) mkdir -p $(PRIVATE_RS_OUTPUT_DIR)/src
999$(hide) $(PRIVATE_RS_CC) \
1000  -o $(PRIVATE_RS_OUTPUT_DIR)/res/raw \
1001  -p $(PRIVATE_RS_OUTPUT_DIR)/src \
1002  -d $(PRIVATE_RS_OUTPUT_DIR) \
1003  -a $@ -MD \
1004  $(addprefix -target-api , $(PRIVATE_RS_TARGET_API)) \
1005  $(PRIVATE_RS_FLAGS) \
1006  $(foreach inc,$(PRIVATE_RS_INCLUDES),$(addprefix -I , $(inc))) \
1007  $(PRIVATE_RS_SOURCE_FILES)
1008$(call _merge-renderscript-d,$(PRIVATE_DEP_FILES),$@.d)
1009$(call transform-d-to-p-args,$@.d,$@.P)
1010$(hide) mkdir -p $(dir $@)
1011$(hide) touch $@
1012endef
1013
1014define transform-bc-to-so
1015@echo "Renderscript compatibility: $(notdir $@) <= $(notdir $<)"
1016$(hide) mkdir -p $(dir $@)
1017$(hide) $(BCC_COMPAT) -O3 -o $(dir $@)/$(notdir $(<:.bc=.o)) -fPIC -shared \
1018	-rt-path $(RS_PREBUILT_CLCORE) -mtriple $(RS_COMPAT_TRIPLE) $<
1019$(hide) $(PRIVATE_CXX) -shared -Wl,-soname,$(notdir $@) -nostdlib \
1020	-Wl,-rpath,\$$ORIGIN/../lib \
1021	$(dir $@)/$(notdir $(<:.bc=.o)) \
1022	$(RS_PREBUILT_COMPILER_RT) \
1023	-o $@ $(TARGET_GLOBAL_LDFLAGS) -Wl,--hash-style=sysv -L prebuilts/gcc/ \
1024	$(RS_PREBUILT_LIBPATH) -L $(TARGET_OUT_INTERMEDIATE_LIBRARIES) \
1025	-lRSSupport -lm -lc
1026endef
1027
1028###########################################################
1029## Commands to compile RenderScript to C++
1030###########################################################
1031
1032define transform-renderscripts-to-cpp-and-bc
1033@echo "RenderScript: $(PRIVATE_MODULE) <= $(PRIVATE_RS_SOURCE_FILES)"
1034$(hide) rm -rf $(PRIVATE_RS_OUTPUT_DIR)
1035$(hide) mkdir -p $(PRIVATE_RS_OUTPUT_DIR)/
1036$(hide) $(PRIVATE_RS_CC) \
1037  -o $(PRIVATE_RS_OUTPUT_DIR)/ \
1038  -d $(PRIVATE_RS_OUTPUT_DIR) \
1039  -a $@ -MD \
1040  -reflect-c++ \
1041  $(addprefix -target-api , $(PRIVATE_RS_TARGET_API)) \
1042  $(PRIVATE_RS_FLAGS) \
1043  $(addprefix -I , $(PRIVATE_RS_INCLUDES)) \
1044  $(PRIVATE_RS_SOURCE_FILES)
1045$(call _merge-renderscript-d,$(PRIVATE_DEP_FILES),$@.d)
1046$(call transform-d-to-p-args,$@.d,$@.P)
1047$(hide) mkdir -p $(dir $@)
1048$(hide) touch $@
1049endef
1050
1051
1052###########################################################
1053## Commands for running aidl
1054###########################################################
1055
1056define transform-aidl-to-java
1057@mkdir -p $(dir $@)
1058@echo "Aidl: $(PRIVATE_MODULE) <= $<"
1059$(hide) $(AIDL) -d$(patsubst %.java,%.P,$@) $(PRIVATE_AIDL_FLAGS) $< $@
1060endef
1061#$(AIDL) $(PRIVATE_AIDL_FLAGS) $< - | indent -nut -br -npcs -l1000 > $@
1062
1063define transform-aidl-to-cpp
1064@mkdir -p $(dir $@)
1065@mkdir -p $(PRIVATE_HEADER_OUTPUT_DIR)
1066@echo "Generating C++ from AIDL: $(PRIVATE_MODULE) <= $<"
1067$(hide) $(AIDL_CPP) -d$(basename $@).aidl.P $(PRIVATE_AIDL_FLAGS) \
1068    $< $(PRIVATE_HEADER_OUTPUT_DIR) $@
1069endef
1070
1071## Given a .aidl file path generate the rule to compile it a .cpp file.
1072# $(1): a .aidl source file
1073# $(2): a directory to place the generated .cpp files in
1074# $(3): name of a variable to add the path to the generated source file to
1075#
1076# You must call this with $(eval).
1077define define-aidl-cpp-rule
1078define-aidl-cpp-rule-src := $(patsubst %.aidl,%$(LOCAL_CPP_EXTENSION),$(subst ../,dotdot/,$(addprefix $(2)/,$(1))))
1079$$(define-aidl-cpp-rule-src) : $(LOCAL_PATH)/$(1) $(AIDL_CPP)
1080	$$(transform-aidl-to-cpp)
1081$(3) += $$(define-aidl-cpp-rule-src)
1082endef
1083
1084###########################################################
1085## Commands for running java-event-log-tags.py
1086###########################################################
1087
1088define transform-logtags-to-java
1089@mkdir -p $(dir $@)
1090@echo "logtags: $@ <= $<"
1091$(hide) $(JAVATAGS) -o $@ $^
1092endef
1093
1094
1095###########################################################
1096## Commands for running protoc to compile .proto into .java
1097###########################################################
1098
1099define transform-proto-to-java
1100@mkdir -p $(dir $@)
1101@echo "Protoc: $@ <= $(PRIVATE_PROTO_SRC_FILES)"
1102@rm -rf $(PRIVATE_PROTO_JAVA_OUTPUT_DIR)
1103@mkdir -p $(PRIVATE_PROTO_JAVA_OUTPUT_DIR)
1104$(hide) for f in $(PRIVATE_PROTO_SRC_FILES); do \
1105        $(PROTOC) \
1106        $(addprefix --proto_path=, $(PRIVATE_PROTO_INCLUDES)) \
1107        $(PRIVATE_PROTO_JAVA_OUTPUT_OPTION)="$(PRIVATE_PROTO_JAVA_OUTPUT_PARAMS):$(PRIVATE_PROTO_JAVA_OUTPUT_DIR)" \
1108        $(PRIVATE_PROTOC_FLAGS) \
1109        $$f || exit 33; \
1110        done
1111$(hide) touch $@
1112endef
1113
1114######################################################################
1115## Commands for running protoc to compile .proto into .pb.cc (or.pb.c) and .pb.h
1116######################################################################
1117define transform-proto-to-cc
1118@echo "Protoc: $@ <= $<"
1119@mkdir -p $(dir $@)
1120$(hide) $(PROTOC) \
1121	$(addprefix --proto_path=, $(PRIVATE_PROTO_INCLUDES)) \
1122	$(PRIVATE_PROTOC_FLAGS) \
1123	$<
1124endef
1125
1126
1127######################################################################
1128## Commands for generating DBus adaptors from .dbus-xml files.
1129######################################################################
1130define generate-dbus-adaptors
1131@echo "Generating DBus adaptors for $(PRIVATE_MODULE)"
1132@mkdir -p $(dir $@)
1133$(hide) $(DBUS_GENERATOR) \
1134	--service-config=$(PRIVATE_DBUS_SERVICE_CONFIG) \
1135	--adaptor=$@ \
1136	$<
1137endef
1138
1139######################################################################
1140## Commands for generating DBus proxies from .dbus-xml files.
1141######################################################################
1142define generate-dbus-proxies
1143@echo "Generating DBus proxies for $(PRIVATE_MODULE)"
1144@mkdir -p $(dir $@)
1145$(hide) $(DBUS_GENERATOR) \
1146	--service-config=$(PRIVATE_DBUS_SERVICE_CONFIG) \
1147	--proxy=$@ \
1148	$(filter %.dbus-xml,$^)
1149endef
1150
1151
1152###########################################################
1153## Commands for running gcc to compile a C++ file
1154###########################################################
1155
1156define transform-cpp-to-o
1157@echo "target $(PRIVATE_ARM_MODE) C++: $(PRIVATE_MODULE) <= $<"
1158@mkdir -p $(dir $@)
1159$(hide) $(RELATIVE_PWD) $(PRIVATE_CXX) \
1160	$(addprefix -I , $(PRIVATE_C_INCLUDES)) \
1161	$(shell cat $(PRIVATE_IMPORT_INCLUDES)) \
1162	$(addprefix -isystem ,\
1163	    $(if $(PRIVATE_NO_DEFAULT_COMPILER_FLAGS),, \
1164	        $(filter-out $(PRIVATE_C_INCLUDES), \
1165	            $(PRIVATE_TARGET_PROJECT_INCLUDES) \
1166	            $(PRIVATE_TARGET_C_INCLUDES)))) \
1167	-c \
1168	$(if $(PRIVATE_NO_DEFAULT_COMPILER_FLAGS),, \
1169	    $(PRIVATE_TARGET_GLOBAL_CFLAGS) \
1170	    $(PRIVATE_TARGET_GLOBAL_CPPFLAGS) \
1171	    $(PRIVATE_ARM_CFLAGS) \
1172	 ) \
1173	$(PRIVATE_RTTI_FLAG) \
1174	$(PRIVATE_CFLAGS) \
1175	$(PRIVATE_CPPFLAGS) \
1176	$(PRIVATE_DEBUG_CFLAGS) \
1177	$(PRIVATE_CFLAGS_NO_OVERRIDE) \
1178	$(PRIVATE_CPPFLAGS_NO_OVERRIDE) \
1179	-MD -MF $(patsubst %.o,%.d,$@) -o $@ $<
1180$(transform-d-to-p)
1181endef
1182
1183
1184###########################################################
1185## Commands for running gcc to compile a C file
1186###########################################################
1187
1188# $(1): extra flags
1189define transform-c-or-s-to-o-no-deps
1190@mkdir -p $(dir $@)
1191$(hide) $(RELATIVE_PWD) $(PRIVATE_CC) \
1192	$(addprefix -I , $(PRIVATE_C_INCLUDES)) \
1193	$(shell cat $(PRIVATE_IMPORT_INCLUDES)) \
1194	$(addprefix -isystem ,\
1195	    $(if $(PRIVATE_NO_DEFAULT_COMPILER_FLAGS),, \
1196	        $(filter-out $(PRIVATE_C_INCLUDES), \
1197	            $(PRIVATE_TARGET_PROJECT_INCLUDES) \
1198	            $(PRIVATE_TARGET_C_INCLUDES)))) \
1199	-c \
1200	$(if $(PRIVATE_NO_DEFAULT_COMPILER_FLAGS),, \
1201	    $(PRIVATE_TARGET_GLOBAL_CFLAGS) \
1202	    $(PRIVATE_TARGET_GLOBAL_CONLYFLAGS) \
1203	    $(PRIVATE_ARM_CFLAGS) \
1204	 ) \
1205	 $(1) \
1206	-MD -MF $(patsubst %.o,%.d,$@) -o $@ $<
1207endef
1208
1209define transform-c-to-o-no-deps
1210@echo "target $(PRIVATE_ARM_MODE) C: $(PRIVATE_MODULE) <= $<"
1211$(call transform-c-or-s-to-o-no-deps, \
1212    $(PRIVATE_CFLAGS) \
1213    $(PRIVATE_CONLYFLAGS) \
1214    $(PRIVATE_DEBUG_CFLAGS) \
1215    $(PRIVATE_CFLAGS_NO_OVERRIDE))
1216endef
1217
1218define transform-s-to-o-no-deps
1219@echo "target asm: $(PRIVATE_MODULE) <= $<"
1220$(call transform-c-or-s-to-o-no-deps, $(PRIVATE_ASFLAGS))
1221endef
1222
1223define transform-c-to-o
1224$(transform-c-to-o-no-deps)
1225$(transform-d-to-p)
1226endef
1227
1228define transform-s-to-o
1229$(transform-s-to-o-no-deps)
1230$(transform-d-to-p)
1231endef
1232
1233# YASM compilation
1234define transform-asm-to-o
1235@mkdir -p $(dir $@)
1236$(hide) $(YASM) \
1237    $(addprefix -I , $(PRIVATE_C_INCLUDES)) \
1238    $($(PRIVATE_2ND_ARCH_VAR_PREFIX)TARGET_GLOBAL_YASM_FLAGS) \
1239    $(PRIVATE_ASFLAGS) \
1240    -o $@ $<
1241endef
1242
1243###########################################################
1244## Commands for running gcc to compile an Objective-C file
1245## This should never happen for target builds but this
1246## will error at build time.
1247###########################################################
1248
1249define transform-m-to-o-no-deps
1250@echo "target ObjC: $(PRIVATE_MODULE) <= $<"
1251$(call transform-c-or-s-to-o-no-deps, $(PRIVATE_CFLAGS) $(PRIVATE_DEBUG_CFLAGS))
1252endef
1253
1254define transform-m-to-o
1255$(transform-m-to-o-no-deps)
1256$(transform-d-to-p)
1257endef
1258
1259###########################################################
1260## Commands for running gcc to compile a host C++ file
1261###########################################################
1262
1263define transform-host-cpp-to-o
1264@echo "$($(PRIVATE_PREFIX)DISPLAY) C++: $(PRIVATE_MODULE) <= $<"
1265@mkdir -p $(dir $@)
1266$(hide) $(RELATIVE_PWD) $(PRIVATE_CXX) \
1267	$(addprefix -I , $(PRIVATE_C_INCLUDES)) \
1268	$(shell cat $(PRIVATE_IMPORT_INCLUDES)) \
1269	$(addprefix -isystem ,\
1270	    $(if $(PRIVATE_NO_DEFAULT_COMPILER_FLAGS),, \
1271	        $(filter-out $(PRIVATE_C_INCLUDES), \
1272	            $($(PRIVATE_PREFIX)PROJECT_INCLUDES) \
1273	            $(PRIVATE_HOST_C_INCLUDES)))) \
1274	-c \
1275	$(if $(PRIVATE_NO_DEFAULT_COMPILER_FLAGS),, \
1276	    $(PRIVATE_HOST_GLOBAL_CFLAGS) \
1277	    $(PRIVATE_HOST_GLOBAL_CPPFLAGS) \
1278	 ) \
1279	$(PRIVATE_CFLAGS) \
1280	$(PRIVATE_CPPFLAGS) \
1281	$(PRIVATE_DEBUG_CFLAGS) \
1282	$(PRIVATE_CFLAGS_NO_OVERRIDE) \
1283	$(PRIVATE_CPPFLAGS_NO_OVERRIDE) \
1284	-MD -MF $(patsubst %.o,%.d,$@) -o $@ $<
1285$(transform-d-to-p)
1286endef
1287
1288
1289###########################################################
1290## Commands for running gcc to compile a host C file
1291###########################################################
1292
1293# $(1): extra flags
1294define transform-host-c-or-s-to-o-no-deps
1295@mkdir -p $(dir $@)
1296$(hide) $(RELATIVE_PWD) $(PRIVATE_CC) \
1297	$(addprefix -I , $(PRIVATE_C_INCLUDES)) \
1298	$(shell cat $(PRIVATE_IMPORT_INCLUDES)) \
1299	$(addprefix -isystem ,\
1300	    $(if $(PRIVATE_NO_DEFAULT_COMPILER_FLAGS),, \
1301	        $(filter-out $(PRIVATE_C_INCLUDES), \
1302	            $($(PRIVATE_PREFIX)PROJECT_INCLUDES) \
1303	            $(PRIVATE_HOST_C_INCLUDES)))) \
1304	-c \
1305	$(if $(PRIVATE_NO_DEFAULT_COMPILER_FLAGS),, \
1306	    $(PRIVATE_HOST_GLOBAL_CFLAGS) \
1307	    $(PRIVATE_HOST_GLOBAL_CONLYFLAGS) \
1308	 ) \
1309	$(1) \
1310	$(PRIVATE_CFLAGS_NO_OVERRIDE) \
1311	-MD -MF $(patsubst %.o,%.d,$@) -o $@ $<
1312endef
1313
1314define transform-host-c-to-o-no-deps
1315@echo "$($(PRIVATE_PREFIX)DISPLAY) C: $(PRIVATE_MODULE) <= $<"
1316$(call transform-host-c-or-s-to-o-no-deps, $(PRIVATE_CFLAGS) $(PRIVATE_CONLYFLAGS) $(PRIVATE_DEBUG_CFLAGS))
1317endef
1318
1319define transform-host-s-to-o-no-deps
1320@echo "$($(PRIVATE_PREFIX)DISPLAY) asm: $(PRIVATE_MODULE) <= $<"
1321$(call transform-host-c-or-s-to-o-no-deps, $(PRIVATE_ASFLAGS))
1322endef
1323
1324define transform-host-c-to-o
1325$(transform-host-c-to-o-no-deps)
1326$(transform-d-to-p)
1327endef
1328
1329define transform-host-s-to-o
1330$(transform-host-s-to-o-no-deps)
1331$(transform-d-to-p)
1332endef
1333
1334###########################################################
1335## Commands for running gcc to compile a host Objective-C file
1336###########################################################
1337
1338define transform-host-m-to-o-no-deps
1339@echo "$($(PRIVATE_PREFIX)DISPLAY) ObjC: $(PRIVATE_MODULE) <= $<"
1340$(call transform-host-c-or-s-to-o-no-deps, $(PRIVATE_CFLAGS) $(PRIVATE_DEBUG_CFLAGS))
1341endef
1342
1343define transform-host-m-to-o
1344$(transform-host-m-to-o-no-deps)
1345$(transform-d-to-p)
1346endef
1347
1348###########################################################
1349## Commands for running gcc to compile a host Objective-C++ file
1350###########################################################
1351
1352define transform-host-mm-to-o
1353$(transform-host-cpp-to-o)
1354endef
1355
1356
1357###########################################################
1358## Rules to compile a single C/C++ source with ../ in the path
1359###########################################################
1360# Replace "../" in object paths with $(DOTDOT_REPLACEMENT).
1361DOTDOT_REPLACEMENT := dotdot/
1362
1363## Rule to compile a C++ source file with ../ in the path.
1364## Must be called with $(eval).
1365# $(1): the C++ source file in LOCAL_SRC_FILES.
1366# $(2): the additional dependencies.
1367# $(3): the variable name to collect the output object file.
1368define compile-dotdot-cpp-file
1369o := $(intermediates)/$(patsubst %$(LOCAL_CPP_EXTENSION),%.o,$(subst ../,$(DOTDOT_REPLACEMENT),$(1)))
1370$$(o) : $(TOPDIR)$(LOCAL_PATH)/$(1) $(2)
1371	$$(transform-$$(PRIVATE_HOST)cpp-to-o)
1372-include $$(o:%.o=%.P)
1373$(3) += $$(o)
1374endef
1375
1376## Rule to compile a C source file with ../ in the path.
1377## Must be called with $(eval).
1378# $(1): the C source file in LOCAL_SRC_FILES.
1379# $(2): the additional dependencies.
1380# $(3): the variable name to collect the output object file.
1381define compile-dotdot-c-file
1382o := $(intermediates)/$(patsubst %.c,%.o,$(subst ../,$(DOTDOT_REPLACEMENT),$(1)))
1383$$(o) : $(TOPDIR)$(LOCAL_PATH)/$(1) $(2)
1384	$$(transform-$$(PRIVATE_HOST)c-to-o)
1385-include $$(o:%.o=%.P)
1386$(3) += $$(o)
1387endef
1388
1389## Rule to compile a .S source file with ../ in the path.
1390## Must be called with $(eval).
1391# $(1): the .S source file in LOCAL_SRC_FILES.
1392# $(2): the additional dependencies.
1393# $(3): the variable name to collect the output object file.
1394define compile-dotdot-s-file
1395o := $(intermediates)/$(patsubst %.S,%.o,$(subst ../,$(DOTDOT_REPLACEMENT),$(1)))
1396$$(o) : $(TOPDIR)$(LOCAL_PATH)/$(1) $(2)
1397	$$(transform-$$(PRIVATE_HOST)s-to-o)
1398-include $$(o:%.o=%.P)
1399$(3) += $$(o)
1400endef
1401
1402## Rule to compile a .s source file with ../ in the path.
1403## Must be called with $(eval).
1404# $(1): the .s source file in LOCAL_SRC_FILES.
1405# $(2): the additional dependencies.
1406# $(3): the variable name to collect the output object file.
1407define compile-dotdot-s-file-no-deps
1408o := $(intermediates)/$(patsubst %.s,%.o,$(subst ../,$(DOTDOT_REPLACEMENT),$(1)))
1409$$(o) : $(TOPDIR)$(LOCAL_PATH)/$(1) $(2)
1410	$$(transform-$$(PRIVATE_HOST)s-to-o-no-deps)
1411$(3) += $$(o)
1412endef
1413
1414###########################################################
1415## Commands for running ar
1416###########################################################
1417
1418define _concat-if-arg2-not-empty
1419$(if $(2),$(hide) $(1) $(2))
1420endef
1421
1422# Split long argument list into smaller groups and call the command repeatedly
1423# Call the command at least once even if there are no arguments, as otherwise
1424# the output file won't be created.
1425#
1426# $(1): the command without arguments
1427# $(2): the arguments
1428define split-long-arguments
1429$(hide) $(1) $(wordlist 1,500,$(2))
1430$(call _concat-if-arg2-not-empty,$(1),$(wordlist 501,1000,$(2)))
1431$(call _concat-if-arg2-not-empty,$(1),$(wordlist 1001,1500,$(2)))
1432$(call _concat-if-arg2-not-empty,$(1),$(wordlist 1501,2000,$(2)))
1433$(call _concat-if-arg2-not-empty,$(1),$(wordlist 2001,2500,$(2)))
1434$(call _concat-if-arg2-not-empty,$(1),$(wordlist 2501,3000,$(2)))
1435$(call _concat-if-arg2-not-empty,$(1),$(wordlist 3001,99999,$(2)))
1436endef
1437
1438# $(1): the full path of the source static library.
1439define _extract-and-include-single-target-whole-static-lib
1440$(hide) ldir=$(PRIVATE_INTERMEDIATES_DIR)/WHOLE/$(basename $(notdir $(1)))_objs;\
1441    rm -rf $$ldir; \
1442    mkdir -p $$ldir; \
1443    cp $(1) $$ldir; \
1444    lib_to_include=$$ldir/$(notdir $(1)); \
1445    filelist=; \
1446    subdir=0; \
1447    for f in `$($(PRIVATE_2ND_ARCH_VAR_PREFIX)TARGET_AR) t $(1)`; do \
1448        if [ -e $$ldir/$$f ]; then \
1449            mkdir $$ldir/$$subdir; \
1450            ext=$$subdir/; \
1451            subdir=$$((subdir+1)); \
1452            $($(PRIVATE_2ND_ARCH_VAR_PREFIX)TARGET_AR) m $$lib_to_include $$f; \
1453        else \
1454            ext=; \
1455        fi; \
1456        $($(PRIVATE_2ND_ARCH_VAR_PREFIX)TARGET_AR) p $$lib_to_include $$f > $$ldir/$$ext$$f; \
1457        filelist="$$filelist $$ldir/$$ext$$f"; \
1458    done ; \
1459    $($(PRIVATE_2ND_ARCH_VAR_PREFIX)TARGET_AR) $($(PRIVATE_2ND_ARCH_VAR_PREFIX)TARGET_GLOBAL_ARFLAGS) \
1460        $(PRIVATE_ARFLAGS) $@ $$filelist
1461
1462endef
1463
1464# $(1): the full path of the source static library.
1465define extract-and-include-whole-static-libs-first
1466$(if $(strip $(1)),
1467$(hide) cp $(1) $@)
1468endef
1469
1470define extract-and-include-target-whole-static-libs
1471$(call extract-and-include-whole-static-libs-first, $(firstword $(PRIVATE_ALL_WHOLE_STATIC_LIBRARIES)))
1472$(foreach lib,$(wordlist 2,999,$(PRIVATE_ALL_WHOLE_STATIC_LIBRARIES)), \
1473    $(call _extract-and-include-single-target-whole-static-lib, $(lib)))
1474endef
1475
1476# Explicitly delete the archive first so that ar doesn't
1477# try to add to an existing archive.
1478define transform-o-to-static-lib
1479@echo "target StaticLib: $(PRIVATE_MODULE) ($@)"
1480@mkdir -p $(dir $@)
1481@rm -f $@
1482$(extract-and-include-target-whole-static-libs)
1483$(call split-long-arguments,$($(PRIVATE_2ND_ARCH_VAR_PREFIX)TARGET_AR) \
1484    $($(PRIVATE_2ND_ARCH_VAR_PREFIX)TARGET_GLOBAL_ARFLAGS) \
1485    $(PRIVATE_ARFLAGS) $@,$(PRIVATE_ALL_OBJECTS))
1486endef
1487
1488###########################################################
1489## Commands for running host ar
1490###########################################################
1491
1492# $(1): the full path of the source static library.
1493define _extract-and-include-single-host-whole-static-lib
1494$(hide) ldir=$(PRIVATE_INTERMEDIATES_DIR)/WHOLE/$(basename $(notdir $(1)))_objs;\
1495    rm -rf $$ldir; \
1496    mkdir -p $$ldir; \
1497    cp $(1) $$ldir; \
1498    lib_to_include=$$ldir/$(notdir $(1)); \
1499    filelist=; \
1500    subdir=0; \
1501    for f in `$($(PRIVATE_2ND_ARCH_VAR_PREFIX)$(PRIVATE_PREFIX)AR) t $(1) | \grep '\.o$$'`; do \
1502        if [ -e $$ldir/$$f ]; then \
1503           mkdir $$ldir/$$subdir; \
1504           ext=$$subdir/; \
1505           subdir=$$((subdir+1)); \
1506           $($(PRIVATE_2ND_ARCH_VAR_PREFIX)$(PRIVATE_PREFIX)AR) m $$lib_to_include $$f; \
1507        else \
1508           ext=; \
1509        fi; \
1510        $($(PRIVATE_2ND_ARCH_VAR_PREFIX)$(PRIVATE_PREFIX)AR) p $$lib_to_include $$f > $$ldir/$$ext$$f; \
1511        filelist="$$filelist $$ldir/$$ext$$f"; \
1512    done ; \
1513    $($(PRIVATE_2ND_ARCH_VAR_PREFIX)$(PRIVATE_PREFIX)AR) $($(PRIVATE_2ND_ARCH_VAR_PREFIX)$(PRIVATE_PREFIX)GLOBAL_ARFLAGS) \
1514        $(PRIVATE_ARFLAGS) $@ $$filelist
1515
1516endef
1517
1518define extract-and-include-host-whole-static-libs
1519$(call extract-and-include-whole-static-libs-first, $(firstword $(PRIVATE_ALL_WHOLE_STATIC_LIBRARIES)))
1520$(foreach lib,$(wordlist 2,999,$(PRIVATE_ALL_WHOLE_STATIC_LIBRARIES)), \
1521    $(call _extract-and-include-single-host-whole-static-lib, $(lib)))
1522endef
1523
1524# Explicitly delete the archive first so that ar doesn't
1525# try to add to an existing archive.
1526define transform-host-o-to-static-lib
1527@echo "$($(PRIVATE_PREFIX)DISPLAY) StaticLib: $(PRIVATE_MODULE) ($@)"
1528@mkdir -p $(dir $@)
1529@rm -f $@
1530$(extract-and-include-host-whole-static-libs)
1531$(call split-long-arguments,$($(PRIVATE_2ND_ARCH_VAR_PREFIX)$(PRIVATE_PREFIX)AR) \
1532    $($(PRIVATE_2ND_ARCH_VAR_PREFIX)$(PRIVATE_PREFIX)GLOBAL_ARFLAGS) \
1533    $(PRIVATE_ARFLAGS) $@,$(PRIVATE_ALL_OBJECTS))
1534endef
1535
1536
1537###########################################################
1538## Commands for running gcc to link a shared library or package
1539###########################################################
1540
1541# ld just seems to be so finicky with command order that we allow
1542# it to be overriden en-masse see combo/linux-arm.make for an example.
1543ifneq ($(HOST_CUSTOM_LD_COMMAND),true)
1544define transform-host-o-to-shared-lib-inner
1545$(hide) $(PRIVATE_CXX) \
1546	-Wl,-rpath-link=$($(PRIVATE_2ND_ARCH_VAR_PREFIX)$(PRIVATE_PREFIX)OUT_INTERMEDIATE_LIBRARIES) \
1547	-Wl,-rpath,\$$ORIGIN/../$(notdir $($(PRIVATE_2ND_ARCH_VAR_PREFIX)$(PRIVATE_PREFIX)OUT_SHARED_LIBRARIES)) \
1548	-Wl,-rpath,\$$ORIGIN/$(notdir $($(PRIVATE_2ND_ARCH_VAR_PREFIX)$(PRIVATE_PREFIX)OUT_SHARED_LIBRARIES)) \
1549	-shared -Wl,-soname,$(notdir $@) \
1550	$($(PRIVATE_2ND_ARCH_VAR_PREFIX)$(PRIVATE_PREFIX)GLOBAL_LD_DIRS) \
1551	$(if $(PRIVATE_NO_DEFAULT_COMPILER_FLAGS),, \
1552	   $(PRIVATE_HOST_GLOBAL_LDFLAGS) \
1553	) \
1554	$(PRIVATE_LDFLAGS) \
1555	$(PRIVATE_ALL_OBJECTS) \
1556	-Wl,--whole-archive \
1557	$(call normalize-host-libraries,$(PRIVATE_ALL_WHOLE_STATIC_LIBRARIES)) \
1558	-Wl,--no-whole-archive \
1559	$(if $(PRIVATE_GROUP_STATIC_LIBRARIES),-Wl$(comma)--start-group) \
1560	$(call normalize-host-libraries,$(PRIVATE_ALL_STATIC_LIBRARIES)) \
1561	$(if $(PRIVATE_GROUP_STATIC_LIBRARIES),-Wl$(comma)--end-group) \
1562	$(if $(filter true,$(NATIVE_COVERAGE)),-lgcov) \
1563	$(if $(filter true,$(NATIVE_COVERAGE)),$(PRIVATE_HOST_LIBPROFILE_RT)) \
1564	$(call normalize-host-libraries,$(PRIVATE_ALL_SHARED_LIBRARIES)) \
1565	-o $@ \
1566	$(PRIVATE_LDLIBS)
1567endef
1568endif
1569
1570define transform-host-o-to-shared-lib
1571@echo "$($(PRIVATE_PREFIX)DISPLAY) SharedLib: $(PRIVATE_MODULE) ($@)"
1572@mkdir -p $(dir $@)
1573$(transform-host-o-to-shared-lib-inner)
1574endef
1575
1576define transform-host-o-to-package
1577@echo "$($(PRIVATE_PREFIX)DISPLAY) Package: $(PRIVATE_MODULE) ($@)"
1578@mkdir -p $(dir $@)
1579$(transform-host-o-to-shared-lib-inner)
1580endef
1581
1582
1583###########################################################
1584## Commands for running gcc to link a shared library or package
1585###########################################################
1586
1587define transform-o-to-shared-lib-inner
1588$(hide) $(PRIVATE_CXX) \
1589	-nostdlib -Wl,-soname,$(notdir $@) \
1590	-Wl,--gc-sections \
1591	$(if $(filter true,$(PRIVATE_CLANG)),-shared,-Wl$(comma)-shared) \
1592	$(PRIVATE_TARGET_GLOBAL_LD_DIRS) \
1593	$(PRIVATE_TARGET_CRTBEGIN_SO_O) \
1594	$(PRIVATE_ALL_OBJECTS) \
1595	-Wl,--whole-archive \
1596	$(call normalize-target-libraries,$(PRIVATE_ALL_WHOLE_STATIC_LIBRARIES)) \
1597	-Wl,--no-whole-archive \
1598	$(if $(PRIVATE_GROUP_STATIC_LIBRARIES),-Wl$(comma)--start-group) \
1599	$(call normalize-target-libraries,$(PRIVATE_ALL_STATIC_LIBRARIES)) \
1600	$(if $(PRIVATE_GROUP_STATIC_LIBRARIES),-Wl$(comma)--end-group) \
1601	$(if $(filter true,$(NATIVE_COVERAGE)),$(PRIVATE_TARGET_COVERAGE_LIB)) \
1602	$(PRIVATE_TARGET_LIBATOMIC) \
1603	$(PRIVATE_TARGET_LIBGCC) \
1604	$(call normalize-target-libraries,$(PRIVATE_ALL_SHARED_LIBRARIES)) \
1605	-o $@ \
1606	$(PRIVATE_TARGET_GLOBAL_LDFLAGS) \
1607	$(PRIVATE_LDFLAGS) \
1608	$(PRIVATE_TARGET_CRTEND_SO_O) \
1609	$(PRIVATE_LDLIBS)
1610endef
1611
1612define transform-o-to-shared-lib
1613@echo "target SharedLib: $(PRIVATE_MODULE) ($@)"
1614@mkdir -p $(dir $@)
1615$(transform-o-to-shared-lib-inner)
1616endef
1617
1618###########################################################
1619## Commands for filtering a target executable or library
1620###########################################################
1621
1622ifneq ($(TARGET_BUILD_VARIANT),user)
1623  TARGET_STRIP_EXTRA = && $(PRIVATE_OBJCOPY) --add-gnu-debuglink=$< $@
1624  TARGET_STRIP_KEEP_SYMBOLS_EXTRA = --add-gnu-debuglink=$<
1625endif
1626
1627define transform-to-stripped
1628@echo "target Strip: $(PRIVATE_MODULE) ($@)"
1629@mkdir -p $(dir $@)
1630$(hide) $(PRIVATE_STRIP) --strip-all $< -o $@ \
1631  $(if $(PRIVATE_NO_DEBUGLINK),,$(TARGET_STRIP_EXTRA))
1632endef
1633
1634define transform-to-stripped-keep-symbols
1635@echo "target Strip (keep symbols): $(PRIVATE_MODULE) ($@)"
1636@mkdir -p $(dir $@)
1637$(hide) $(PRIVATE_OBJCOPY) \
1638    `$(PRIVATE_READELF) -S $< | awk '/.debug_/ {print "-R " $$2}' | xargs` \
1639    $(TARGET_STRIP_KEEP_SYMBOLS_EXTRA) $< $@
1640endef
1641
1642###########################################################
1643## Commands for packing a target executable or library
1644###########################################################
1645
1646define pack-elf-relocations
1647@echo "target Pack Relocations: $(PRIVATE_MODULE) ($@)"
1648$(copy-file-to-target)
1649$(hide) $(RELOCATION_PACKER) $@
1650endef
1651
1652###########################################################
1653## Commands for running gcc to link an executable
1654###########################################################
1655
1656define transform-o-to-executable-inner
1657$(hide) $(PRIVATE_CXX) -pie \
1658	-nostdlib -Bdynamic \
1659	-Wl,-dynamic-linker,$(PRIVATE_LINKER) \
1660	-Wl,--gc-sections \
1661	-Wl,-z,nocopyreloc \
1662	$(PRIVATE_TARGET_GLOBAL_LD_DIRS) \
1663	-Wl,-rpath-link=$(PRIVATE_TARGET_OUT_INTERMEDIATE_LIBRARIES) \
1664	$(PRIVATE_TARGET_CRTBEGIN_DYNAMIC_O) \
1665	$(PRIVATE_ALL_OBJECTS) \
1666	-Wl,--whole-archive \
1667	$(call normalize-target-libraries,$(PRIVATE_ALL_WHOLE_STATIC_LIBRARIES)) \
1668	-Wl,--no-whole-archive \
1669	$(if $(PRIVATE_GROUP_STATIC_LIBRARIES),-Wl$(comma)--start-group) \
1670	$(call normalize-target-libraries,$(PRIVATE_ALL_STATIC_LIBRARIES)) \
1671	$(if $(PRIVATE_GROUP_STATIC_LIBRARIES),-Wl$(comma)--end-group) \
1672	$(if $(filter true,$(NATIVE_COVERAGE)),$(PRIVATE_TARGET_COVERAGE_LIB)) \
1673	$(PRIVATE_TARGET_LIBATOMIC) \
1674	$(PRIVATE_TARGET_LIBGCC) \
1675	$(call normalize-target-libraries,$(PRIVATE_ALL_SHARED_LIBRARIES)) \
1676	-o $@ \
1677	$(PRIVATE_TARGET_GLOBAL_LDFLAGS) \
1678	$(PRIVATE_LDFLAGS) \
1679	$(PRIVATE_TARGET_CRTEND_O) \
1680	$(PRIVATE_LDLIBS)
1681endef
1682
1683define transform-o-to-executable
1684@echo "target Executable: $(PRIVATE_MODULE) ($@)"
1685@mkdir -p $(dir $@)
1686$(transform-o-to-executable-inner)
1687endef
1688
1689
1690###########################################################
1691## Commands for linking a static executable. In practice,
1692## we only use this on arm, so the other platforms don't
1693## have transform-o-to-static-executable defined.
1694## Clang driver needs -static to create static executable.
1695## However, bionic/linker uses -shared to overwrite.
1696## Linker for x86 targets does not allow coexistance of -static and -shared,
1697## so we add -static only if -shared is not used.
1698###########################################################
1699
1700define transform-o-to-static-executable-inner
1701$(hide) $(PRIVATE_CXX) \
1702	-nostdlib -Bstatic \
1703	$(if $(filter $(PRIVATE_LDFLAGS),-shared),,-static) \
1704	-Wl,--gc-sections \
1705	-o $@ \
1706	$(PRIVATE_TARGET_GLOBAL_LD_DIRS) \
1707	$(PRIVATE_TARGET_CRTBEGIN_STATIC_O) \
1708	$(PRIVATE_TARGET_GLOBAL_LDFLAGS) \
1709	$(PRIVATE_LDFLAGS) \
1710	$(PRIVATE_ALL_OBJECTS) \
1711	-Wl,--whole-archive \
1712	$(call normalize-target-libraries,$(PRIVATE_ALL_WHOLE_STATIC_LIBRARIES)) \
1713	-Wl,--no-whole-archive \
1714	$(call normalize-target-libraries,$(filter-out %libcompiler_rt.a,$(filter-out %libc_nomalloc.a,$(filter-out %libc.a,$(PRIVATE_ALL_STATIC_LIBRARIES))))) \
1715	-Wl,--start-group \
1716	$(call normalize-target-libraries,$(filter %libc.a,$(PRIVATE_ALL_STATIC_LIBRARIES))) \
1717	$(call normalize-target-libraries,$(filter %libc_nomalloc.a,$(PRIVATE_ALL_STATIC_LIBRARIES))) \
1718	$(if $(filter true,$(NATIVE_COVERAGE)),$(PRIVATE_TARGET_COVERAGE_LIB)) \
1719	$(PRIVATE_TARGET_LIBATOMIC) \
1720	$(call normalize-target-libraries,$(filter %libcompiler_rt.a,$(PRIVATE_ALL_STATIC_LIBRARIES))) \
1721	$(PRIVATE_TARGET_LIBGCC) \
1722	-Wl,--end-group \
1723	$(PRIVATE_TARGET_CRTEND_O)
1724endef
1725
1726define transform-o-to-static-executable
1727@echo "target StaticExecutable: $(PRIVATE_MODULE) ($@)"
1728@mkdir -p $(dir $@)
1729$(transform-o-to-static-executable-inner)
1730endef
1731
1732
1733###########################################################
1734## Commands for running gcc to link a host executable
1735###########################################################
1736ifdef BUILD_HOST_static
1737HOST_FPIE_FLAGS :=
1738else
1739HOST_FPIE_FLAGS := -pie
1740# Force the correct entry point to workaround a bug in binutils that manifests with -pie
1741ifeq ($(HOST_CROSS_OS),windows)
1742HOST_CROSS_FPIE_FLAGS += -Wl,-e_mainCRTStartup
1743endif
1744endif
1745
1746ifneq ($(HOST_CUSTOM_LD_COMMAND),true)
1747define transform-host-o-to-executable-inner
1748$(hide) $(PRIVATE_CXX) \
1749	$(PRIVATE_ALL_OBJECTS) \
1750	-Wl,--whole-archive \
1751	$(call normalize-host-libraries,$(PRIVATE_ALL_WHOLE_STATIC_LIBRARIES)) \
1752	-Wl,--no-whole-archive \
1753	$(if $(PRIVATE_GROUP_STATIC_LIBRARIES),-Wl$(comma)--start-group) \
1754	$(call normalize-host-libraries,$(PRIVATE_ALL_STATIC_LIBRARIES)) \
1755	$(if $(PRIVATE_GROUP_STATIC_LIBRARIES),-Wl$(comma)--end-group) \
1756	$(if $(filter true,$(NATIVE_COVERAGE)),-lgcov) \
1757	$(if $(filter true,$(NATIVE_COVERAGE)),$(PRIVATE_HOST_LIBPROFILE_RT)) \
1758	$(call normalize-host-libraries,$(PRIVATE_ALL_SHARED_LIBRARIES)) \
1759	-Wl,-rpath-link=$($(PRIVATE_2ND_ARCH_VAR_PREFIX)$(PRIVATE_PREFIX)OUT_INTERMEDIATE_LIBRARIES) \
1760	-Wl,-rpath,\$$ORIGIN/../$(notdir $($(PRIVATE_2ND_ARCH_VAR_PREFIX)$(PRIVATE_PREFIX)OUT_SHARED_LIBRARIES)) \
1761	-Wl,-rpath,\$$ORIGIN/$(notdir $($(PRIVATE_2ND_ARCH_VAR_PREFIX)$(PRIVATE_PREFIX)OUT_SHARED_LIBRARIES)) \
1762	$($(PRIVATE_2ND_ARCH_VAR_PREFIX)$(PRIVATE_PREFIX)GLOBAL_LD_DIRS) \
1763	$(if $(PRIVATE_NO_DEFAULT_COMPILER_FLAGS),, \
1764		$(PRIVATE_HOST_GLOBAL_LDFLAGS) \
1765	) \
1766	$(PRIVATE_LDFLAGS) \
1767	-o $@ \
1768	$(PRIVATE_LDLIBS)
1769endef
1770endif
1771
1772define transform-host-o-to-executable
1773@echo "$($(PRIVATE_PREFIX)DISPLAY) Executable: $(PRIVATE_MODULE) ($@)"
1774@mkdir -p $(dir $@)
1775$(transform-host-o-to-executable-inner)
1776endef
1777
1778
1779###########################################################
1780## Commands for running javac to make .class files
1781###########################################################
1782
1783# Add BUILD_NUMBER to apps default version name if it's unbundled build.
1784ifdef TARGET_BUILD_APPS
1785APPS_DEFAULT_VERSION_NAME := $(PLATFORM_VERSION)-$(BUILD_NUMBER_FROM_FILE)
1786else
1787APPS_DEFAULT_VERSION_NAME := $(PLATFORM_VERSION)
1788endif
1789
1790# TODO: Right now we generate the asset resources twice, first as part
1791# of generating the Java classes, then at the end when packaging the final
1792# assets.  This should be changed to do one of two things: (1) Don't generate
1793# any resource files the first time, only create classes during that stage;
1794# or (2) Don't use the -c flag with the second stage, instead taking the
1795# resource files from the first stage as additional input.  My original intent
1796# was to use approach (2), but this requires a little more work in the tool.
1797# Maybe we should just use approach (1).
1798
1799# This rule creates the R.java and Manifest.java files, both of which
1800# are PRODUCT-neutral.  Don't pass PRIVATE_PRODUCT_AAPT_CONFIG to this invocation.
1801define create-resource-java-files
1802@mkdir -p $(PRIVATE_SOURCE_INTERMEDIATES_DIR)
1803@mkdir -p $(dir $(PRIVATE_RESOURCE_PUBLICS_OUTPUT))
1804$(hide) $(AAPT) package $(PRIVATE_AAPT_FLAGS) -m \
1805    $(eval # PRIVATE_PRODUCT_AAPT_CONFIG is intentionally missing-- see comment.) \
1806    $(addprefix -J , $(PRIVATE_SOURCE_INTERMEDIATES_DIR)) \
1807    $(addprefix -M , $(PRIVATE_ANDROID_MANIFEST)) \
1808    $(addprefix -P , $(PRIVATE_RESOURCE_PUBLICS_OUTPUT)) \
1809    $(addprefix -S , $(PRIVATE_RESOURCE_DIR)) \
1810    $(addprefix -A , $(PRIVATE_ASSET_DIR)) \
1811    $(addprefix -I , $(PRIVATE_AAPT_INCLUDES)) \
1812    $(addprefix -G , $(PRIVATE_PROGUARD_OPTIONS_FILE)) \
1813    $(addprefix --min-sdk-version , $(PRIVATE_DEFAULT_APP_TARGET_SDK)) \
1814    $(addprefix --target-sdk-version , $(PRIVATE_DEFAULT_APP_TARGET_SDK)) \
1815    $(if $(filter --version-code,$(PRIVATE_AAPT_FLAGS)),,--version-code $(PLATFORM_SDK_VERSION)) \
1816    $(if $(filter --version-name,$(PRIVATE_AAPT_FLAGS)),,--version-name $(APPS_DEFAULT_VERSION_NAME)) \
1817    $(addprefix --rename-manifest-package , $(PRIVATE_MANIFEST_PACKAGE_NAME)) \
1818    $(addprefix --rename-instrumentation-target-package , $(PRIVATE_MANIFEST_INSTRUMENTATION_FOR)) \
1819    --skip-symbols-without-default-localization
1820endef
1821
1822# Search for generated R.java/Manifest.java, copy the found R.java as $@.
1823# Also copy them to a central 'R' directory to make it easier to add the files to an IDE.
1824define find-generated-R.java
1825$(hide) for GENERATED_MANIFEST_FILE in `find $(PRIVATE_SOURCE_INTERMEDIATES_DIR) \
1826  -name Manifest.java 2> /dev/null`; do \
1827    dir=`awk '/package/{gsub(/\./,"/",$$2);gsub(/;/,"",$$2);print $$2;exit}' $$GENERATED_MANIFEST_FILE`; \
1828    mkdir -p $(TARGET_COMMON_OUT_ROOT)/R/$$dir; \
1829    $(ACP) -fp $$GENERATED_MANIFEST_FILE $(TARGET_COMMON_OUT_ROOT)/R/$$dir; \
1830  done;
1831$(hide) for GENERATED_R_FILE in `find $(PRIVATE_SOURCE_INTERMEDIATES_DIR) \
1832  -name R.java 2> /dev/null`; do \
1833    dir=`awk '/package/{gsub(/\./,"/",$$2);gsub(/;/,"",$$2);print $$2;exit}' $$GENERATED_R_FILE`; \
1834    mkdir -p $(TARGET_COMMON_OUT_ROOT)/R/$$dir; \
1835    $(ACP) -fp $$GENERATED_R_FILE $(TARGET_COMMON_OUT_ROOT)/R/$$dir \
1836      || exit 31; \
1837    $(ACP) -fp $$GENERATED_R_FILE $@ || exit 32; \
1838  done;
1839@# Ensure that the target file is always created, i.e. also in case we did not
1840@# enter the GENERATED_R_FILE-loop above. This avoids unnecessary rebuilding.
1841$(hide) touch $@
1842endef
1843
1844###########################################################
1845# AAPT2 compilation and link
1846###########################################################
1847define aapt2-compile-one-resource-file
1848@mkdir -p $(dir $@)
1849$(hide) $(AAPT2) compile -o $(dir $@) $(PRIVATE_AAPT2_CFLAGS) --legacy $<
1850endef
1851
1852define aapt2-compile-resource-dirs
1853@mkdir -p $(dir $@)
1854$(hide) $(AAPT2) compile -o $@ $(addprefix --dir ,$(PRIVATE_SOURCE_RES_DIRS)) \
1855  $(PRIVATE_AAPT2_CFLAGS) --legacy
1856endef
1857
1858# Set up rule to compile one resource file with aapt2.
1859# Must be called with $(eval).
1860# $(1): the source file
1861# $(2): the output file
1862define aapt2-compile-one-resource-file-rule
1863$(2) : $(1) $(AAPT2)
1864	@echo "AAPT2 compile $$@ <- $$<"
1865	$$(call aapt2-compile-one-resource-file)
1866endef
1867
1868# Convert input resource file path to output file path.
1869# values-[config]/<file>.xml -> values-[config]_<file>.arsc.flat;
1870# For other resource file, just replace the last "/" with "_" and
1871# add .flat extension.
1872#
1873# $(1): the input resource file path
1874# $(2): the base dir of the output file path
1875# Returns: the compiled output file path
1876define aapt2-compiled-resource-out-file
1877$(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
1878endef
1879
1880define aapt2-link
1881$(hide) $(AAPT2) link -o $@ \
1882  $(PRIVATE_AAPT_FLAGS) \
1883  $(addprefix --manifest ,$(PRIVATE_ANDROID_MANIFEST)) \
1884  $(addprefix -I ,$(PRIVATE_AAPT_INCLUDES)) \
1885  $(addprefix -I ,$(PRIVATE_SHARED_ANDROID_LIBRARIES)) \
1886  $(addprefix --java ,$(PRIVATE_SOURCE_INTERMEDIATES_DIR)) \
1887  $(addprefix --proguard ,$(PRIVATE_PROGUARD_OPTIONS_FILE)) \
1888  $(addprefix --min-sdk-version ,$(PRIVATE_DEFAULT_APP_TARGET_SDK)) \
1889  $(addprefix --target-sdk-version ,$(PRIVATE_DEFAULT_APP_TARGET_SDK)) \
1890  $(if $(filter --product,$(PRIVATE_AAPT_FLAGS)),,$(addprefix --product ,$(PRIVATE_TARGET_AAPT_CHARACTERISTICS))) \
1891  $(addprefix -c ,$(PRIVATE_PRODUCT_AAPT_CONFIG)) \
1892  $(addprefix --preferred-density ,$(PRIVATE_PRODUCT_AAPT_PREF_CONFIG)) \
1893  $(if $(filter --version-code,$(PRIVATE_AAPT_FLAGS)),,--version-code $(PLATFORM_SDK_VERSION)) \
1894  $(if $(filter --version-name,$(PRIVATE_AAPT_FLAGS)),,--version-name $(APPS_DEFAULT_VERSION_NAME)) \
1895  $(addprefix --rename-manifest-package ,$(PRIVATE_MANIFEST_PACKAGE_NAME)) \
1896  $(addprefix --rename-instrumentation-target-package ,$(PRIVATE_MANIFEST_INSTRUMENTATION_FOR)) \
1897  $(addprefix -R , $(PRIVATE_OVERLAY_FLAT)) \
1898  $(PRIVATE_RES_FLAT)
1899endef
1900
1901###########################################################
1902xlint_unchecked := -Xlint:unchecked
1903
1904# emit-line, <word list>, <output file>
1905define emit-line
1906   $(if $(1),echo -n '$(strip $(1)) ' >> $(2))
1907endef
1908
1909# dump-words-to-file, <word list>, <output file>
1910define dump-words-to-file
1911        @rm -f $(2)
1912        @touch $(2)
1913        @$(call emit-line,$(wordlist 1,200,$(1)),$(2))
1914        @$(call emit-line,$(wordlist 201,400,$(1)),$(2))
1915        @$(call emit-line,$(wordlist 401,600,$(1)),$(2))
1916        @$(call emit-line,$(wordlist 601,800,$(1)),$(2))
1917        @$(call emit-line,$(wordlist 801,1000,$(1)),$(2))
1918        @$(call emit-line,$(wordlist 1001,1200,$(1)),$(2))
1919        @$(call emit-line,$(wordlist 1201,1400,$(1)),$(2))
1920        @$(call emit-line,$(wordlist 1401,1600,$(1)),$(2))
1921        @$(call emit-line,$(wordlist 1601,1800,$(1)),$(2))
1922        @$(call emit-line,$(wordlist 1801,2000,$(1)),$(2))
1923        @$(call emit-line,$(wordlist 2001,2200,$(1)),$(2))
1924        @$(call emit-line,$(wordlist 2201,2400,$(1)),$(2))
1925        @$(call emit-line,$(wordlist 2401,2600,$(1)),$(2))
1926        @$(call emit-line,$(wordlist 2601,2800,$(1)),$(2))
1927        @$(call emit-line,$(wordlist 2801,3000,$(1)),$(2))
1928        @$(call emit-line,$(wordlist 3001,3200,$(1)),$(2))
1929        @$(call emit-line,$(wordlist 3201,3400,$(1)),$(2))
1930        @$(call emit-line,$(wordlist 3401,3600,$(1)),$(2))
1931        @$(call emit-line,$(wordlist 3601,3800,$(1)),$(2))
1932        @$(call emit-line,$(wordlist 3801,4000,$(1)),$(2))
1933        @$(call emit-line,$(wordlist 4001,4200,$(1)),$(2))
1934        @$(call emit-line,$(wordlist 4201,4400,$(1)),$(2))
1935        @$(call emit-line,$(wordlist 4401,4600,$(1)),$(2))
1936        @$(call emit-line,$(wordlist 4601,4800,$(1)),$(2))
1937        @$(call emit-line,$(wordlist 4801,5000,$(1)),$(2))
1938        @$(call emit-line,$(wordlist 5001,5200,$(1)),$(2))
1939        @$(call emit-line,$(wordlist 5201,5400,$(1)),$(2))
1940        @$(if $(wordlist 5401,5402,$(1)),$(error Too many words ($(words $(1)))))
1941endef
1942
1943# For a list of jar files, unzip them to a specified directory,
1944# but make sure that no META-INF files come along for the ride,
1945# unless PRIVATE_DONT_DELETE_JAR_META_INF is set.
1946#
1947# $(1): files to unzip
1948# $(2): destination directory
1949define unzip-jar-files
1950  $(hide) for f in $(1); \
1951  do \
1952    if [ ! -f $$f ]; then \
1953      echo Missing file $$f; \
1954      exit 1; \
1955    fi; \
1956    unzip -qo $$f -d $(2); \
1957  done
1958  $(if $(PRIVATE_DONT_DELETE_JAR_META_INF),,$(hide) rm -rf $(2)/META-INF)
1959endef
1960
1961# Call jack
1962#
1963define call-jack
1964 JACK_VERSION=$(PRIVATE_JACK_VERSION) $(JACK) $(DEFAULT_JACK_EXTRA_ARGS)
1965endef
1966
1967# Common definition to invoke javac on the host and target.
1968#
1969# Some historical notes:
1970# - below we write the list of java files to java-source-list to avoid argument
1971#   list length problems with Cygwin
1972# - we filter out duplicate java file names because eclipse's compiler
1973#   doesn't like them.
1974#
1975# $(1): javac
1976# $(2): bootclasspath
1977define compile-java
1978$(hide) rm -f $@
1979$(hide) rm -rf $(PRIVATE_CLASS_INTERMEDIATES_DIR)
1980$(hide) mkdir -p $(dir $@)
1981$(hide) mkdir -p $(PRIVATE_CLASS_INTERMEDIATES_DIR)
1982$(call unzip-jar-files,$(PRIVATE_STATIC_JAVA_LIBRARIES),$(PRIVATE_CLASS_INTERMEDIATES_DIR))
1983$(call dump-words-to-file,$(PRIVATE_JAVA_SOURCES),$(PRIVATE_CLASS_INTERMEDIATES_DIR)/java-source-list)
1984$(hide) if [ -d "$(PRIVATE_SOURCE_INTERMEDIATES_DIR)" ]; then \
1985          find $(PRIVATE_SOURCE_INTERMEDIATES_DIR) -name '*.java' -and -not -name '.*' >> $(PRIVATE_CLASS_INTERMEDIATES_DIR)/java-source-list; \
1986fi
1987$(hide) tr ' ' '\n' < $(PRIVATE_CLASS_INTERMEDIATES_DIR)/java-source-list \
1988    | $(NORMALIZE_PATH) | sort -u > $(PRIVATE_CLASS_INTERMEDIATES_DIR)/java-source-list-uniq
1989$(hide) if [ -s $(PRIVATE_CLASS_INTERMEDIATES_DIR)/java-source-list-uniq ] ; then \
1990    $(1) -encoding UTF-8 \
1991    $(if $(findstring true,$(PRIVATE_WARNINGS_ENABLE)),$(xlint_unchecked),) \
1992    $(2) \
1993    $(addprefix -classpath ,$(strip \
1994        $(call normalize-path-list,$(PRIVATE_ALL_JAVA_LIBRARIES)))) \
1995    $(if $(findstring true,$(PRIVATE_WARNINGS_ENABLE)),$(xlint_unchecked),) \
1996    -extdirs "" -d $(PRIVATE_CLASS_INTERMEDIATES_DIR) \
1997    $(PRIVATE_JAVACFLAGS) \
1998    \@$(PRIVATE_CLASS_INTERMEDIATES_DIR)/java-source-list-uniq \
1999    || ( rm -rf $(PRIVATE_CLASS_INTERMEDIATES_DIR) ; exit 41 ) \
2000fi
2001$(if $(PRIVATE_JAVA_LAYERS_FILE), $(hide) build/tools/java-layers.py \
2002    $(PRIVATE_JAVA_LAYERS_FILE) \@$(PRIVATE_CLASS_INTERMEDIATES_DIR)/java-source-list-uniq,)
2003$(hide) rm -f $(PRIVATE_CLASS_INTERMEDIATES_DIR)/java-source-list
2004$(hide) rm -f $(PRIVATE_CLASS_INTERMEDIATES_DIR)/java-source-list-uniq
2005$(if $(PRIVATE_JAR_EXCLUDE_FILES), $(hide) find $(PRIVATE_CLASS_INTERMEDIATES_DIR) \
2006    -name $(word 1, $(PRIVATE_JAR_EXCLUDE_FILES)) \
2007    $(addprefix -o -name , $(wordlist 2, 999, $(PRIVATE_JAR_EXCLUDE_FILES))) \
2008    | xargs rm -rf)
2009$(if $(PRIVATE_JAR_PACKAGES), \
2010    $(hide) find $(PRIVATE_CLASS_INTERMEDIATES_DIR) -mindepth 1 -type f \
2011        $(foreach pkg, $(PRIVATE_JAR_PACKAGES), \
2012            -not -path $(PRIVATE_CLASS_INTERMEDIATES_DIR)/$(subst .,/,$(pkg))/\*) -delete ; \
2013        find $(PRIVATE_CLASS_INTERMEDIATES_DIR) -empty -delete)
2014$(if $(PRIVATE_JAR_EXCLUDE_PACKAGES), $(hide) rm -rf \
2015    $(foreach pkg, $(PRIVATE_JAR_EXCLUDE_PACKAGES), \
2016        $(PRIVATE_CLASS_INTERMEDIATES_DIR)/$(subst .,/,$(pkg))))
2017$(if $(PRIVATE_RMTYPEDEFS), $(hide) $(RMTYPEDEFS) -v $(PRIVATE_CLASS_INTERMEDIATES_DIR))
2018$(if $(PRIVATE_JAR_MANIFEST), \
2019    $(hide) sed -e "s/%BUILD_NUMBER%/$(BUILD_NUMBER_FROM_FILE)/" \
2020            $(PRIVATE_JAR_MANIFEST) > $(dir $@)/manifest.mf && \
2021        jar -cfm $@ $(dir $@)/manifest.mf \
2022            -C $(PRIVATE_CLASS_INTERMEDIATES_DIR) ., \
2023    $(hide) jar -cf $@ -C $(PRIVATE_CLASS_INTERMEDIATES_DIR) .)
2024$(if $(PRIVATE_EXTRA_JAR_ARGS),$(call add-java-resources-to,$@))
2025endef
2026
2027define transform-java-to-classes.jar
2028@echo "target Java: $(PRIVATE_MODULE) ($(PRIVATE_CLASS_INTERMEDIATES_DIR))"
2029$(call compile-java,$(TARGET_JAVAC),$(PRIVATE_BOOTCLASSPATH))
2030endef
2031
2032# Invoke Jack to compile java from source to dex and jack files.
2033#
2034# Some historical notes:
2035# - below we write the list of java files to java-source-list to avoid argument
2036#   list length problems with Cygwin
2037# - we filter out duplicate java file names because Jack doesn't like them.
2038define jack-java-to-dex
2039$(hide) rm -f $@
2040$(hide) rm -f $(PRIVATE_CLASSES_JACK)
2041$(hide) rm -rf $(PRIVATE_JACK_INTERMEDIATES_DIR)
2042$(hide) mkdir -p $(dir $@)
2043$(hide) mkdir -p $(dir $(PRIVATE_CLASSES_JACK))
2044$(hide) mkdir -p $(PRIVATE_JACK_INTERMEDIATES_DIR)
2045$(if $(PRIVATE_JACK_INCREMENTAL_DIR),$(hide) mkdir -p $(PRIVATE_JACK_INCREMENTAL_DIR))
2046$(call dump-words-to-file,$(PRIVATE_JAVA_SOURCES),$(PRIVATE_JACK_INTERMEDIATES_DIR)/java-source-list)
2047$(hide) if [ -d "$(PRIVATE_SOURCE_INTERMEDIATES_DIR)" ]; then \
2048          find $(PRIVATE_SOURCE_INTERMEDIATES_DIR) -name '*.java' >> $(PRIVATE_JACK_INTERMEDIATES_DIR)/java-source-list; \
2049fi
2050$(hide) tr ' ' '\n' < $(PRIVATE_JACK_INTERMEDIATES_DIR)/java-source-list \
2051    | $(NORMALIZE_PATH) | sort -u > $(PRIVATE_JACK_INTERMEDIATES_DIR)/java-source-list-uniq
2052$(if $(PRIVATE_JACK_PROGUARD_FLAGS), \
2053    $(hide) echo -basedirectory $(CURDIR) > $@.flags; \
2054    echo $(PRIVATE_JACK_PROGUARD_FLAGS) >> $@.flags; \
2055)
2056$(if $(PRIVATE_EXTRA_JAR_ARGS),
2057    $(hide) mkdir -p $@.res.tmp
2058    $(hide) $(call create-empty-package-at,$@.res.tmp.zip)
2059    $(hide) $(call add-java-resources-to,$@.res.tmp.zip)
2060    $(hide) unzip -qo $@.res.tmp.zip -d $@.res.tmp
2061    $(hide) rm $@.res.tmp.zip)
2062$(hide) if [ -s $(PRIVATE_JACK_INTERMEDIATES_DIR)/java-source-list-uniq ] ; then \
2063    export tmpEcjArg="@$(PRIVATE_JACK_INTERMEDIATES_DIR)/java-source-list-uniq"; \
2064else \
2065    export tmpEcjArg=""; \
2066fi; \
2067$(call call-jack) \
2068    $(strip $(PRIVATE_JACK_FLAGS)) \
2069    $(strip $(PRIVATE_JACK_COVERAGE_OPTIONS)) \
2070    $(if $(NO_OPTIMIZE_DX), \
2071        -D jack.dex.optimize="false") \
2072    $(if $(PRIVATE_RMTYPEDEFS), \
2073        -D jack.android.remove-typedef="true") \
2074    $(addprefix --classpath ,$(strip \
2075        $(call normalize-path-list,$(PRIVATE_BOOTCLASSPATH_JAVA_LIBRARIES) $(PRIVATE_ALL_JACK_LIBRARIES)))) \
2076    $(addprefix --import ,$(call reverse-list,$(PRIVATE_STATIC_JACK_LIBRARIES))) \
2077    $(if $(PRIVATE_EXTRA_JAR_ARGS),--import-resource $@.res.tmp) \
2078    -D jack.android.min-api-level=$(PRIVATE_JACK_MIN_SDK_VERSION) \
2079    -D jack.import.resource.policy=keep-first \
2080    -D jack.import.type.policy=keep-first \
2081    --output-jack $(PRIVATE_CLASSES_JACK) \
2082    $(if $(PRIVATE_JACK_INCREMENTAL_DIR),--incremental-folder $(PRIVATE_JACK_INCREMENTAL_DIR)) \
2083    --output-dex $(PRIVATE_JACK_INTERMEDIATES_DIR) \
2084    $(addprefix --config-jarjar ,$(strip $(PRIVATE_JARJAR_RULES))) \
2085    $(if $(PRIVATE_JACK_PROGUARD_FLAGS),--config-proguard $@.flags) \
2086    $$tmpEcjArg \
2087    || ( rm -rf $(PRIVATE_CLASSES_JACK); exit 41 )
2088$(hide) mv $(PRIVATE_JACK_INTERMEDIATES_DIR)/classes*.dex $(dir $@)
2089$(hide) rm -f $(PRIVATE_JACK_INTERMEDIATES_DIR)/java-source-list
2090$(if $(PRIVATE_EXTRA_JAR_ARGS),$(hide) rm -rf $@.res.tmp)
2091$(hide) mv $(PRIVATE_JACK_INTERMEDIATES_DIR)/java-source-list-uniq $(PRIVATE_JACK_INTERMEDIATES_DIR).java-source-list
2092$(if $(PRIVATE_JAR_PACKAGES), $(hide) echo unsupported options PRIVATE_JAR_PACKAGES in $@; exit 53)
2093$(if $(PRIVATE_JAR_EXCLUDE_PACKAGES), $(hide) echo unsupported options JAR_EXCLUDE_PACKAGES in $@; exit 53)
2094$(if $(PRIVATE_JAR_MANIFEST), $(hide) echo unsupported options JAR_MANIFEST in $@; exit 53)
2095endef
2096
2097# Invoke Jack to compile java source just to check it compiles correctly.
2098#
2099# Some historical notes:
2100# - below we write the list of java files to java-source-list to avoid argument
2101#   list length problems with Cygwin
2102# - we filter out duplicate java file names because Jack doesn't like them.
2103define jack-check-java
2104$(hide) rm -f $@
2105$(hide) rm -f $@.java-source-list
2106$(hide) rm -f $@.java-source-list-uniq
2107$(hide) mkdir -p $(dir $@)
2108$(if $(PRIVATE_JACK_INCREMENTAL_DIR),$(hide) mkdir -p $(PRIVATE_JACK_INCREMENTAL_DIR))
2109$(call dump-words-to-file,$(PRIVATE_JAVA_SOURCES),$@.java-source-list)
2110$(hide) if [ -d "$(PRIVATE_SOURCE_INTERMEDIATES_DIR)" ]; then \
2111          find $(PRIVATE_SOURCE_INTERMEDIATES_DIR) -name '*.java' >> $@.java-source-list; \
2112fi
2113$(hide) tr ' ' '\n' < $@.java-source-list \
2114    | sort -u > $@.java-source-list-uniq
2115$(hide) if [ -s $@.java-source-list-uniq ] ; then \
2116	$(call call-jack,$(PRIVATE_JACK_EXTRA_ARGS)) \
2117	    $(strip $(PRIVATE_JACK_FLAGS)) \
2118	    $(strip $(PRIVATE_JACK_DEBUG_FLAGS)) \
2119	    $(addprefix --classpath ,$(strip \
2120	        $(call normalize-path-list,$(call reverse-list,$(PRIVATE_STATIC_JACK_LIBRARIES)) $(PRIVATE_BOOTCLASSPATH_JAVA_LIBRARIES) $(PRIVATE_ALL_JACK_LIBRARIES)))) \
2121	    -D jack.import.resource.policy=keep-first \
2122	    -D jack.android.min-api-level=$(PRIVATE_JACK_MIN_SDK_VERSION) \
2123	    -D jack.import.type.policy=keep-first \
2124	    $(if $(PRIVATE_JACK_INCREMENTAL_DIR),--incremental-folder $(PRIVATE_JACK_INCREMENTAL_DIR)) \
2125	    @$@.java-source-list-uniq; \
2126fi
2127touch $@
2128endef
2129
2130define transform-jar-to-jack
2131	$(hide) mkdir -p $(dir $@)
2132	$(hide) mkdir -p $@.tmpjill.res
2133	$(hide) unzip -qo $< -d $@.tmpjill.res
2134	$(hide) find $@.tmpjill.res -iname "*.class" -delete
2135	$(hide) $(call call-jack) \
2136	    $(PRIVATE_JACK_FLAGS) \
2137        -D jack.import.resource.policy=keep-first \
2138        -D jack.import.type.policy=keep-first \
2139        -D jack.android.min-api-level=$(PRIVATE_JACK_MIN_SDK_VERSION) \
2140	    --import $< \
2141	    --import-resource $@.tmpjill.res \
2142	    --output-jack $@
2143	$(hide) rm -rf $@.tmpjill.res
2144endef
2145
2146# Moves $1.tmp to $1 if necessary. This is designed to be used with
2147# .KATI_RESTAT. For kati, this function doesn't update the timestamp
2148# of $1 when $1.tmp is identical to $1 so that ninja won't rebuild
2149# targets which depend on $1. For GNU make, this function simply
2150# copies $1.tmp to $1.
2151ifeq ($(BUILDING_WITH_NINJA),true)
2152define commit-change-for-toc
2153$(hide) if cmp -s $1.tmp $1 ; then \
2154 rm $1.tmp ; \
2155else \
2156 mv $1.tmp $1 ; \
2157fi
2158endef
2159else
2160define commit-change-for-toc
2161@# make doesn't support restat. We always update .toc files so the dependents will always be updated too.
2162$(hide) mv $1.tmp $1
2163endef
2164endif
2165
2166## Rule to create a table of contents from a .jar file.
2167## Must be called with $(eval).
2168# $(1): A .jar file
2169define _transform-jar-to-toc
2170$1.toc: $1 | $(IJAR)
2171	@echo Generating TOC: $$@
2172	$(hide) $(IJAR) $$< $$@.tmp
2173	$$(call commit-change-for-toc,$$@)
2174endef
2175
2176## Define a rule which generates .jar.toc and mark it as .KATI_RESTAT.
2177# $(1): A .jar file
2178define define-jar-to-toc-rule
2179$(eval $(call _transform-jar-to-toc,$1))\
2180$(eval .KATI_RESTAT: $1.toc)
2181endef
2182
2183ifeq (,$(TARGET_BUILD_APPS))
2184
2185## Rule to create a table of contents from a .dex file.
2186## Must be called with $(eval).
2187# $(1): The directory which contains classes*.dex files
2188define _transform-dex-to-toc
2189$1/classes.dex.toc: PRIVATE_INPUT_DEX_FILES := $1/classes*.dex
2190$1/classes.dex.toc: $1/classes.dex $(DEXDUMP)
2191	@echo Generating TOC: $$@
2192	$(hide) $(DEXDUMP) -l xml $$(PRIVATE_INPUT_DEX_FILES) > $$@.tmp
2193	$$(call commit-change-for-toc,$$@)
2194endef
2195
2196## Define a rule which generates .dex.toc and mark it as .KATI_RESTAT.
2197# $(1): The directory which contains classes*.dex files
2198define define-dex-to-toc-rule
2199$(eval $(call _transform-dex-to-toc,$1))\
2200$(eval .KATI_RESTAT: $1/classes.dex.toc)
2201endef
2202
2203else
2204
2205# Turn off .toc optimization for apps build as we cannot build dexdump.
2206define define-dex-to-toc-rule
2207endef
2208
2209endif  # TARGET_BUILD_APPS
2210
2211
2212# Invoke Jack to compile java from source to jack files without shrink or obfuscation.
2213#
2214# Some historical notes:
2215# - below we write the list of java files to java-source-list to avoid argument
2216#   list length problems with Cygwin
2217# - we filter out duplicate java file names because Jack doesn't like them.
2218define java-to-jack
2219$(hide) rm -f $@
2220$(hide) rm -rf $(PRIVATE_JACK_INTERMEDIATES_DIR)
2221$(hide) mkdir -p $(dir $@)
2222$(hide) mkdir -p $(PRIVATE_JACK_INTERMEDIATES_DIR)
2223$(if $(PRIVATE_JACK_INCREMENTAL_DIR),$(hide) mkdir -p $(PRIVATE_JACK_INCREMENTAL_DIR))
2224$(call dump-words-to-file,$(PRIVATE_JAVA_SOURCES),$(PRIVATE_JACK_INTERMEDIATES_DIR)/java-source-list)
2225$(hide) if [ -d "$(PRIVATE_SOURCE_INTERMEDIATES_DIR)" ]; then \
2226          find $(PRIVATE_SOURCE_INTERMEDIATES_DIR) -name '*.java' >> $(PRIVATE_JACK_INTERMEDIATES_DIR)/java-source-list; \
2227fi
2228$(hide) tr ' ' '\n' < $(PRIVATE_JACK_INTERMEDIATES_DIR)/java-source-list \
2229    | $(NORMALIZE_PATH) | sort -u > $(PRIVATE_JACK_INTERMEDIATES_DIR)/java-source-list-uniq
2230$(if $(PRIVATE_JACK_PROGUARD_FLAGS), \
2231    $(hide) echo -basedirectory $(CURDIR) > $@.flags; \
2232    echo $(PRIVATE_JACK_PROGUARD_FLAGS) >> $@.flags; \
2233)
2234$(if $(PRIVATE_EXTRA_JAR_ARGS),
2235	$(hide) mkdir -p $@.res.tmp
2236	$(hide) $(call create-empty-package-at,$@.res.tmp.zip)
2237	$(hide) $(call add-java-resources-to,$@.res.tmp.zip)
2238	$(hide) unzip -qo $@.res.tmp.zip -d $@.res.tmp
2239	$(hide) rm $@.res.tmp.zip)
2240$(hide) if [ -s $(PRIVATE_JACK_INTERMEDIATES_DIR)/java-source-list-uniq ] ; then \
2241    export tmpEcjArg="@$(PRIVATE_JACK_INTERMEDIATES_DIR)/java-source-list-uniq"; \
2242else \
2243    export tmpEcjArg=""; \
2244fi; \
2245$(call call-jack) \
2246    $(strip $(PRIVATE_JACK_FLAGS)) \
2247    $(if $(NO_OPTIMIZE_DX), \
2248        -D jack.dex.optimize="false") \
2249    $(addprefix --classpath ,$(strip \
2250        $(call normalize-path-list,$(PRIVATE_BOOTCLASSPATH_JAVA_LIBRARIES) $(PRIVATE_ALL_JACK_LIBRARIES)))) \
2251    $(addprefix --import ,$(call reverse-list,$(PRIVATE_STATIC_JACK_LIBRARIES))) \
2252    $(if $(PRIVATE_EXTRA_JAR_ARGS),--import-resource $@.res.tmp) \
2253    -D jack.import.resource.policy=keep-first \
2254    -D jack.import.type.policy=keep-first \
2255    -D jack.android.min-api-level=$(PRIVATE_JACK_MIN_SDK_VERSION) \
2256    $(if $(PRIVATE_JACK_INCREMENTAL_DIR),--incremental-folder $(PRIVATE_JACK_INCREMENTAL_DIR)) \
2257    --output-jack $@ \
2258    $(addprefix --config-jarjar ,$(strip $(PRIVATE_JARJAR_RULES))) \
2259    $(if $(PRIVATE_JACK_PROGUARD_FLAGS),--config-proguard $@.flags) \
2260    $$tmpEcjArg \
2261    || ( rm -f $@ ; exit 41 )
2262$(hide) rm -f $(PRIVATE_JACK_INTERMEDIATES_DIR)/java-source-list
2263$(if $(PRIVATE_EXTRA_JAR_ARGS),$(hide) rm -rf $@.res.tmp)
2264$(hide) mv $(PRIVATE_JACK_INTERMEDIATES_DIR)/java-source-list-uniq $(PRIVATE_JACK_INTERMEDIATES_DIR).java-source-list
2265$(if $(PRIVATE_JAR_PACKAGES), $(hide) echo unsupported options PRIVATE_JAR_PACKAGES in $@; exit 53)
2266$(if $(PRIVATE_JAR_EXCLUDE_PACKAGES), $(hide) echo unsupported options JAR_EXCLUDE_PACKAGES in $@; exit 53)
2267$(if $(PRIVATE_JAR_MANIFEST), $(hide) echo unsupported options JAR_MANIFEST in $@; exit 53)
2268endef
2269
2270define transform-classes.jar-to-emma
2271$(hide) java -classpath $(EMMA_JAR) emma instr -outmode fullcopy -outfile \
2272    $(PRIVATE_EMMA_COVERAGE_FILE) -ip $< -d $(PRIVATE_EMMA_INTERMEDIATES_DIR) \
2273    $(addprefix -ix , $(PRIVATE_EMMA_COVERAGE_FILTER))
2274endef
2275
2276# Create a mostly-empty .jar file that we'll add to later.
2277# The MacOS jar tool doesn't like creating empty jar files,
2278# so we need to give it something.
2279# $(1) package to create
2280define create-empty-package-at
2281@mkdir -p $(dir $(1))
2282$(hide) touch $(dir $(1))zipdummy
2283$(hide) (cd $(dir $(1)) && jar cf $(notdir $(1)) zipdummy)
2284$(hide) zip -qd $(1) zipdummy
2285$(hide) rm $(dir $(1))zipdummy
2286endef
2287
2288# Create a mostly-empty .jar file that we'll add to later.
2289# The MacOS jar tool doesn't like creating empty jar files,
2290# so we need to give it something.
2291define create-empty-package
2292$(call create-empty-package-at,$@)
2293endef
2294
2295# Copy an arhchive file and delete any class files and empty folders inside.
2296# $(1): the source archive file.
2297# $(2): the destination archive file.
2298define initialize-package-file
2299@mkdir -p $(dir $(2))
2300$(hide) cp -f $(1) $(2)
2301$(hide) zip -qd $(2) "*.class" \
2302    $(if $(strip $(PRIVATE_DONT_DELETE_JAR_DIRS)),,"*/") \
2303    || true # Ignore the error when nothing to delete.
2304endef
2305
2306#TODO: we kinda want to build different asset packages for
2307#      different configurations, then combine them later (or something).
2308#      Per-locale, etc.
2309#      A list of dynamic and static parameters;  build layers for
2310#      dynamic params that lay over the static ones.
2311#TODO: update the manifest to point to the package file
2312#Note that the version numbers are given to aapt as simple default
2313#values; applications can override these by explicitly stating
2314#them in their manifest.
2315define add-assets-to-package
2316$(hide) $(AAPT) package -u $(PRIVATE_AAPT_FLAGS) \
2317    $(addprefix -c , $(PRIVATE_PRODUCT_AAPT_CONFIG)) \
2318    $(addprefix --preferred-density , $(PRIVATE_PRODUCT_AAPT_PREF_CONFIG)) \
2319    $(addprefix -M , $(PRIVATE_ANDROID_MANIFEST)) \
2320    $(addprefix -S , $(PRIVATE_RESOURCE_DIR)) \
2321    $(addprefix -A , $(PRIVATE_ASSET_DIR)) \
2322    $(addprefix -I , $(PRIVATE_AAPT_INCLUDES)) \
2323    $(addprefix --min-sdk-version , $(PRIVATE_DEFAULT_APP_TARGET_SDK)) \
2324    $(addprefix --target-sdk-version , $(PRIVATE_DEFAULT_APP_TARGET_SDK)) \
2325    $(if $(filter --product,$(PRIVATE_AAPT_FLAGS)),,$(addprefix --product , $(PRIVATE_TARGET_AAPT_CHARACTERISTICS))) \
2326    $(if $(filter --version-code,$(PRIVATE_AAPT_FLAGS)),,--version-code $(PLATFORM_SDK_VERSION)) \
2327    $(if $(filter --version-name,$(PRIVATE_AAPT_FLAGS)),,--version-name $(APPS_DEFAULT_VERSION_NAME)) \
2328    $(addprefix --rename-manifest-package , $(PRIVATE_MANIFEST_PACKAGE_NAME)) \
2329    $(addprefix --rename-instrumentation-target-package , $(PRIVATE_MANIFEST_INSTRUMENTATION_FOR)) \
2330    --skip-symbols-without-default-localization \
2331    -F $@
2332endef
2333
2334# We need the extra blank line, so that the command will be on a separate line.
2335# $(1): the ABI name
2336# $(2): the list of shared libraies
2337define _add-jni-shared-libs-to-package-per-abi
2338$(hide) cp $(2) $(dir $@)lib/$(1)
2339
2340endef
2341
2342# For apps_only build, don't uncompress/page-align the jni libraries,
2343# because the apk may be run on older platforms that don't support loading jni directly from apk.
2344ifdef TARGET_BUILD_APPS
2345JNI_COMPRESS_FLAGS :=
2346ZIPALIGN_PAGE_ALIGN_FLAGS :=
2347else
2348JNI_COMPRESS_FLAGS := -0
2349ZIPALIGN_PAGE_ALIGN_FLAGS := -p
2350endif
2351
2352define add-jni-shared-libs-to-package
2353$(hide) rm -rf $(dir $@)lib
2354$(hide) mkdir -p $(addprefix $(dir $@)lib/,$(PRIVATE_JNI_SHARED_LIBRARIES_ABI))
2355$(foreach abi,$(PRIVATE_JNI_SHARED_LIBRARIES_ABI),\
2356  $(call _add-jni-shared-libs-to-package-per-abi,$(abi),\
2357    $(patsubst $(abi):%,%,$(filter $(abi):%,$(PRIVATE_JNI_SHARED_LIBRARIES)))))
2358$(hide) (cd $(dir $@) && zip -qrX $(JNI_COMPRESS_FLAGS) $(notdir $@) lib)
2359$(hide) rm -rf $(dir $@)lib
2360endef
2361
2362#TODO: update the manifest to point to the dex file
2363define add-dex-to-package
2364$(call add-dex-to-package-arg,$@)
2365endef
2366
2367# $(1): the package file.
2368define add-dex-to-package-arg
2369$(hide) find $(dir $(PRIVATE_DEX_FILE)) -maxdepth 1 -name "classes*.dex" | sort | xargs zip -qjX $(1)
2370endef
2371
2372# Add java resources added by the current module.
2373# $(1) destination package
2374#
2375define add-java-resources-to
2376$(call dump-words-to-file, $(PRIVATE_EXTRA_JAR_ARGS), $(1).jar-arg-list)
2377$(hide) jar uf $(1) @$(1).jar-arg-list
2378@rm -f $(1).jar-arg-list
2379endef
2380
2381# Add resources carried by static Jack libraries.
2382#
2383define add-carried-jack-resources
2384 $(hide) if [ -d $(PRIVATE_JACK_INTERMEDIATES_DIR) ] ; then \
2385    find $(PRIVATE_JACK_INTERMEDIATES_DIR) -type f | sort \
2386        | sed -e "s?^$(PRIVATE_JACK_INTERMEDIATES_DIR)/? -C \"$(PRIVATE_JACK_INTERMEDIATES_DIR)\" \"?" -e "s/$$/\"/" \
2387        > $(dir $@)jack_res_jar_flags; \
2388    if [ -s $(dir $@)jack_res_jar_flags ] ; then \
2389        jar uf $@ @$(dir $@)jack_res_jar_flags; \
2390    fi; \
2391fi
2392endef
2393
2394# Returns the minSdkVersion of the specified APK as a decimal number. If the
2395# version is a codename, returns the current platform SDK version (always a
2396# decimal number) instead. If the APK does not specify a minSdkVersion, returns
2397# 0 to match how the Android platform interprets this situation at runtime.
2398#
2399define get-package-min-sdk-version-int
2400$$(($(AAPT) dump badging $(1) 2>&1 | grep '^sdkVersion' || echo "sdkVersion:'0'") \
2401    | cut -d"'" -f2 | \
2402    sed -e s/^$(PLATFORM_VERSION_CODENAME)$$/$(PLATFORM_SDK_VERSION)/)
2403endef
2404
2405# Sign a package using the specified key/cert.
2406#
2407define sign-package
2408$(call sign-package-arg,$@)
2409endef
2410
2411# $(1): the package file we are signing.
2412define sign-package-arg
2413$(hide) mv $(1) $(1).unsigned
2414$(hide) java -Djava.library.path=$(SIGNAPK_JNI_LIBRARY_PATH) -jar $(SIGNAPK_JAR) \
2415    --min-sdk-version $(call get-package-min-sdk-version-int,$@.unsigned) \
2416    $(PRIVATE_CERTIFICATE) $(PRIVATE_PRIVATE_KEY) \
2417    $(PRIVATE_ADDITIONAL_CERTIFICATES) $(1).unsigned $(1).signed
2418$(hide) mv $(1).signed $(1)
2419endef
2420
2421# Align STORED entries of a package on 4-byte boundaries to make them easier to mmap.
2422#
2423define align-package
2424$(hide) mv $@ $@.unaligned
2425$(hide) $(ZIPALIGN) \
2426    -f \
2427    $(ZIPALIGN_PAGE_ALIGN_FLAGS) \
2428    4 \
2429    $@.unaligned $@.aligned
2430$(hide) mv $@.aligned $@
2431endef
2432
2433# Remove dynamic timestamps from packages
2434#
2435ifndef TARGET_BUILD_APPS
2436define remove-timestamps-from-package
2437$(hide) $(ZIPTIME) $@
2438endef
2439endif
2440
2441# Uncompress shared libraries embedded in an apk.
2442#
2443define uncompress-shared-libs
2444$(hide) if unzip -l $@ $(PRIVATE_EMBEDDED_JNI_LIBS) >/dev/null ; then \
2445  rm -rf $(dir $@)uncompressedlibs && mkdir $(dir $@)uncompressedlibs; \
2446  unzip $@ $(PRIVATE_EMBEDDED_JNI_LIBS) -d $(dir $@)uncompressedlibs && \
2447  zip -d $@ 'lib/*.so' && \
2448  ( cd $(dir $@)uncompressedlibs && find lib -type f | sort | zip -D -X -0 ../$(notdir $@) -@ ) && \
2449  rm -rf $(dir $@)uncompressedlibs; \
2450  fi
2451endef
2452
2453define install-dex-debug
2454$(hide) if [ -f "$(PRIVATE_INTERMEDIATES_DIR)/classes.dex" ]; then \
2455	    mkdir -p $(TOP)/dalvik/DEBUG-FILES; \
2456	    $(ACP) $(PRIVATE_INTERMEDIATES_DIR)/classes.dex \
2457		$(TOP)/dalvik/DEBUG-FILES/$(PRIVATE_MODULE).dex; \
2458	fi
2459$(hide) if [ -f "$(PRIVATE_INTERMEDIATES_DIR)/classes.lst" ]; then \
2460	    mkdir -p $(TOP)/dalvik/DEBUG-FILES; \
2461	    $(ACP) $(PRIVATE_INTERMEDIATES_DIR)/classes.lst \
2462		$(TOP)/dalvik/DEBUG-FILES/$(PRIVATE_MODULE).lst; \
2463	fi
2464endef
2465
2466# TODO(joeo): If we can ever upgrade to post 3.81 make and get the
2467# new prebuilt rules to work, we should change this to copy the
2468# resources to the out directory and then copy the resources.
2469
2470# Note: we intentionally don't clean PRIVATE_CLASS_INTERMEDIATES_DIR
2471# in transform-java-to-classes for the sake of vm-tests.
2472define transform-host-java-to-package
2473@echo "$($(PRIVATE_PREFIX)DISPLAY) Java: $(PRIVATE_MODULE) ($(PRIVATE_CLASS_INTERMEDIATES_DIR))"
2474$(call compile-java,$(HOST_JAVAC),$(PRIVATE_BOOTCLASSPATH))
2475endef
2476
2477###########################################################
2478## Commands for copying files
2479###########################################################
2480
2481# Define a rule to copy a header.  Used via $(eval) by copy_headers.make.
2482# $(1): source header
2483# $(2): destination header
2484define copy-one-header
2485$(2): $(1)
2486	@echo "Header: $$@"
2487	$$(copy-file-to-new-target-with-cp)
2488endef
2489
2490# Define a rule to copy a file.  For use via $(eval).
2491# $(1): source file
2492# $(2): destination file
2493define copy-one-file
2494$(2): $(1) | $(ACP)
2495	@echo "Copy: $$@"
2496	$$(copy-file-to-target)
2497endef
2498
2499# Copies many files.
2500# $(1): The files to copy.  Each entry is a ':' separated src:dst pair
2501# Evaluates to the list of the dst files (ie suitable for a dependency list)
2502define copy-many-files
2503$(foreach f, $(1), $(strip \
2504    $(eval _cmf_tuple := $(subst :, ,$(f))) \
2505    $(eval _cmf_src := $(word 1,$(_cmf_tuple))) \
2506    $(eval _cmf_dest := $(word 2,$(_cmf_tuple))) \
2507    $(eval $(call copy-one-file,$(_cmf_src),$(_cmf_dest))) \
2508    $(_cmf_dest)))
2509endef
2510
2511# Copy the file only if it's a well-formed xml file. For use via $(eval).
2512# $(1): source file
2513# $(2): destination file, must end with .xml.
2514define copy-xml-file-checked
2515$(2): $(1) | $(ACP)
2516	@echo "Copy xml: $$@"
2517	$(hide) xmllint $$< >/dev/null  # Don't print the xml file to stdout.
2518	$$(copy-file-to-target)
2519endef
2520
2521# The -t option to acp and the -p option to cp is
2522# required for OSX.  OSX has a ridiculous restriction
2523# where it's an error for a .a file's modification time
2524# to disagree with an internal timestamp, and this
2525# macro is used to install .a files (among other things).
2526
2527# Copy a single file from one place to another,
2528# preserving permissions and overwriting any existing
2529# file.
2530# We disable the "-t" option for acp cannot handle
2531# high resolution timestamp correctly on file systems like ext4.
2532# Therefore copy-file-to-target is the same as copy-file-to-new-target.
2533define copy-file-to-target
2534@mkdir -p $(dir $@)
2535$(hide) $(ACP) -fp $< $@
2536endef
2537
2538# The same as copy-file-to-target, but use the local
2539# cp command instead of acp.
2540define copy-file-to-target-with-cp
2541@mkdir -p $(dir $@)
2542$(hide) cp -fp $< $@
2543endef
2544
2545# The same as copy-file-to-target, but use the zipalign tool to do so.
2546define copy-file-to-target-with-zipalign
2547@mkdir -p $(dir $@)
2548$(hide) $(ZIPALIGN) -f 4 $< $@
2549endef
2550
2551# The same as copy-file-to-target, but strip out "# comment"-style
2552# comments (for config files and such).
2553define copy-file-to-target-strip-comments
2554@mkdir -p $(dir $@)
2555$(hide) sed -e 's/#.*$$//' -e 's/[ \t]*$$//' -e '/^$$/d' < $< > $@
2556endef
2557
2558# The same as copy-file-to-target, but don't preserve
2559# the old modification time.
2560define copy-file-to-new-target
2561@mkdir -p $(dir $@)
2562$(hide) $(ACP) -fp $< $@
2563endef
2564
2565# The same as copy-file-to-new-target, but use the local
2566# cp command instead of acp.
2567define copy-file-to-new-target-with-cp
2568@mkdir -p $(dir $@)
2569$(hide) cp -f $< $@
2570endef
2571
2572# Copy a prebuilt file to a target location.
2573define transform-prebuilt-to-target
2574@echo "$(if $(PRIVATE_IS_HOST_MODULE),host,target) Prebuilt: $(PRIVATE_MODULE) ($@)"
2575$(copy-file-to-target)
2576endef
2577
2578# Copy a prebuilt file to a target location, using zipalign on it.
2579define transform-prebuilt-to-target-with-zipalign
2580@echo "$(if $(PRIVATE_IS_HOST_MODULE),host,target) Prebuilt APK: $(PRIVATE_MODULE) ($@)"
2581$(copy-file-to-target-with-zipalign)
2582endef
2583
2584# Copy a prebuilt file to a target location, stripping "# comment" comments.
2585define transform-prebuilt-to-target-strip-comments
2586@echo "$(if $(PRIVATE_IS_HOST_MODULE),host,target) Prebuilt: $(PRIVATE_MODULE) ($@)"
2587$(copy-file-to-target-strip-comments)
2588endef
2589
2590# Copy a list of files/directories to target location, with sub dir structure preserved.
2591# For example $(HOST_OUT_EXECUTABLES)/aapt -> $(staging)/bin/aapt .
2592# $(1): the source list of files/directories.
2593# $(2): the path prefix to strip. In the above example it would be $(HOST_OUT).
2594# $(3): the target location.
2595define copy-files-with-structure
2596$(foreach t,$(1),\
2597  $(eval s := $(patsubst $(2)%,%,$(t)))\
2598  $(hide) mkdir -p $(dir $(3)/$(s)); cp -Rf $(t) $(3)/$(s)$(newline))
2599endef
2600
2601
2602###########################################################
2603## Commands to call Proguard
2604###########################################################
2605define transform-jar-to-proguard
2606@echo Proguard: $@
2607$(hide) $(PROGUARD) -injars $< -outjars $@ $(PRIVATE_PROGUARD_FLAGS) \
2608    $(addprefix -injars , $(PRIVATE_EXTRA_INPUT_JAR))
2609endef
2610
2611###########################################################
2612## Stuff source generated from one-off tools
2613###########################################################
2614
2615define transform-generated-source
2616@echo "target Generated: $(PRIVATE_MODULE) <= $<"
2617@mkdir -p $(dir $@)
2618$(hide) $(PRIVATE_CUSTOM_TOOL)
2619endef
2620
2621
2622###########################################################
2623## Assertions about attributes of the target
2624###########################################################
2625
2626# $(1): The file to check
2627ifndef get-file-size
2628$(error HOST_OS must define get-file-size)
2629endif
2630
2631# Convert a partition data size (eg, as reported in /proc/mtd) to the
2632# size of the image used to flash that partition (which includes a
2633# spare area for each page).
2634# $(1): the partition data size
2635define image-size-from-data-size
2636$(strip $(eval _isfds_value := $$(shell echo $$$$(($(1) / $(BOARD_NAND_PAGE_SIZE) * \
2637  ($(BOARD_NAND_PAGE_SIZE)+$(BOARD_NAND_SPARE_SIZE))))))\
2638$(if $(filter 0, $(_isfds_value)),$(shell echo $$(($(BOARD_NAND_PAGE_SIZE)+$(BOARD_NAND_SPARE_SIZE)))),$(_isfds_value))\
2639$(eval _isfds_value :=))
2640endef
2641
2642# $(1): The file(s) to check (often $@)
2643# $(2): The maximum total image size, in decimal bytes.
2644#    Make sure to take into account any reserved space needed for the FS.
2645#
2646# If $(2) is empty, evaluates to "true"
2647#
2648# Reserve bad blocks.  Make sure that MAX(1% of partition size, 2 blocks)
2649# is left over after the image has been flashed.  Round the 1% up to the
2650# next whole flash block size.
2651define assert-max-file-size
2652$(if $(2), \
2653  size=$$(for i in $(1); do $(call get-file-size,$$i); echo +; done; echo 0); \
2654  total=$$(( $$( echo "$$size" ) )); \
2655  printname=$$(echo -n "$(1)" | tr " " +); \
2656  img_blocksize=$(call image-size-from-data-size,$(BOARD_FLASH_BLOCK_SIZE)); \
2657  twoblocks=$$((img_blocksize * 2)); \
2658  onepct=$$((((($(2) / 100) - 1) / img_blocksize + 1) * img_blocksize)); \
2659  reserve=$$((twoblocks > onepct ? twoblocks : onepct)); \
2660  maxsize=$$(($(2) - reserve)); \
2661  echo "$$printname maxsize=$$maxsize blocksize=$$img_blocksize total=$$total reserve=$$reserve"; \
2662  if [ "$$total" -gt "$$maxsize" ]; then \
2663    echo "error: $$printname too large ($$total > [$(2) - $$reserve])"; \
2664    false; \
2665  elif [ "$$total" -gt $$((maxsize - 32768)) ]; then \
2666    echo "WARNING: $$printname approaching size limit ($$total now; limit $$maxsize)"; \
2667  fi \
2668 , \
2669  true \
2670 )
2671endef
2672
2673# Like assert-max-file-size, but the second argument is a partition
2674# size, which we'll convert to a max image size before checking it
2675# against the files.
2676#
2677# $(1): The file(s) to check (often $@)
2678# $(2): The partition size.
2679define assert-max-image-size
2680$(if $(2), \
2681  $(call assert-max-file-size,$(1),$(call image-size-from-data-size,$(2))))
2682endef
2683
2684
2685###########################################################
2686## Define device-specific radio files
2687###########################################################
2688INSTALLED_RADIOIMAGE_TARGET :=
2689
2690# Copy a radio image file to the output location, and add it to
2691# INSTALLED_RADIOIMAGE_TARGET.
2692# $(1): filename
2693define add-radio-file
2694  $(eval $(call add-radio-file-internal,$(1),$(notdir $(1))))
2695endef
2696define add-radio-file-internal
2697INSTALLED_RADIOIMAGE_TARGET += $$(PRODUCT_OUT)/$(2)
2698$$(PRODUCT_OUT)/$(2) : $$(LOCAL_PATH)/$(1) | $$(ACP)
2699	$$(transform-prebuilt-to-target)
2700endef
2701
2702# Version of add-radio-file that also arranges for the version of the
2703# file to be checked against the contents of
2704# $(TARGET_BOARD_INFO_FILE).
2705# $(1): filename
2706# $(2): name of version variable in board-info (eg, "version-baseband")
2707define add-radio-file-checked
2708  $(eval $(call add-radio-file-checked-internal,$(1),$(notdir $(1)),$(2)))
2709endef
2710define add-radio-file-checked-internal
2711INSTALLED_RADIOIMAGE_TARGET += $$(PRODUCT_OUT)/$(2)
2712BOARD_INFO_CHECK += $(3):$(LOCAL_PATH)/$(1)
2713$$(PRODUCT_OUT)/$(2) : $$(LOCAL_PATH)/$(1) | $$(ACP)
2714	$$(transform-prebuilt-to-target)
2715endef
2716
2717
2718###########################################################
2719# Override the package defined in $(1), setting the
2720# variables listed below differently.
2721#
2722#  $(1): The makefile to override (relative to the source
2723#        tree root)
2724#  $(2): Old LOCAL_PACKAGE_NAME value.
2725#  $(3): New LOCAL_PACKAGE_NAME value.
2726#  $(4): New LOCAL_MANIFEST_PACKAGE_NAME value.
2727#  $(5): New LOCAL_CERTIFICATE value.
2728#  $(6): New LOCAL_INSTRUMENTATION_FOR value.
2729#  $(7): New LOCAL_MANIFEST_INSTRUMENTATION_FOR value.
2730#
2731# Note that LOCAL_PACKAGE_OVERRIDES is NOT cleared in
2732# clear_vars.mk.
2733###########################################################
2734define inherit-package
2735  $(eval $(call inherit-package-internal,$(1),$(2),$(3),$(4),$(5),$(6),$(7)))
2736endef
2737
2738define inherit-package-internal
2739  LOCAL_PACKAGE_OVERRIDES \
2740      := $(strip $(1))||$(strip $(2))||$(strip $(3))||$(strip $(4))||&&$(strip $(5))||&&$(strip $(6))||&&$(strip $(7)) $(LOCAL_PACKAGE_OVERRIDES)
2741  include $(1)
2742  LOCAL_PACKAGE_OVERRIDES \
2743      := $(wordlist 1,$(words $(LOCAL_PACKAGE_OVERRIDES)), $(LOCAL_PACKAGE_OVERRIDES))
2744endef
2745
2746# To be used with inherit-package above
2747# Evalutes to true if the package was overridden
2748define set-inherited-package-variables
2749$(strip $(call set-inherited-package-variables-internal))
2750endef
2751
2752define keep-or-override
2753$(eval $(1) := $(if $(2),$(2),$($(1))))
2754endef
2755
2756define set-inherited-package-variables-internal
2757  $(eval _o := $(subst ||, ,$(lastword $(LOCAL_PACKAGE_OVERRIDES))))
2758  $(eval _n := $(subst ||, ,$(firstword $(LOCAL_PACKAGE_OVERRIDES))))
2759  $(if $(filter $(word 2,$(_n)),$(LOCAL_PACKAGE_NAME)), \
2760    $(eval LOCAL_PACKAGE_NAME := $(word 3,$(_o))) \
2761    $(eval LOCAL_MANIFEST_PACKAGE_NAME := $(word 4,$(_o))) \
2762    $(call keep-or-override,LOCAL_CERTIFICATE,$(patsubst &&%,%,$(word 5,$(_o)))) \
2763    $(call keep-or-override,LOCAL_INSTRUMENTATION_FOR,$(patsubst &&%,%,$(word 6,$(_o)))) \
2764    $(call keep-or-override,LOCAL_MANIFEST_INSTRUMENTATION_FOR,$(patsubst &&%,%,$(word 7,$(_o)))) \
2765    $(eval LOCAL_OVERRIDES_PACKAGES := $(sort $(LOCAL_OVERRIDES_PACKAGES) $(word 2,$(_o)))) \
2766    true \
2767  ,)
2768endef
2769
2770###########################################################
2771## API Check
2772###########################################################
2773
2774# eval this to define a rule that runs apicheck.
2775#
2776# Args:
2777#    $(1)  target
2778#    $(2)  stable api file
2779#    $(3)  api file to be tested
2780#    $(4)  stable removed api file
2781#    $(5)  removed api file to be tested
2782#    $(6)  arguments for apicheck
2783#    $(7)  command to run if apicheck failed
2784#    $(8)  target dependent on this api check
2785#    $(9)  additional dependencies
2786define check-api
2787$(TARGET_OUT_COMMON_INTERMEDIATES)/PACKAGING/$(strip $(1))-timestamp: $(2) $(3) $(4) $(APICHECK) $(9)
2788	@echo "Checking API:" $(1)
2789	$(hide) ( $(APICHECK_COMMAND) $(6) $(2) $(3) $(4) $(5) || ( $(7) ; exit 38 ) )
2790	$(hide) mkdir -p $$(dir $$@)
2791	$(hide) touch $$@
2792$(8): $(TARGET_OUT_COMMON_INTERMEDIATES)/PACKAGING/$(strip $(1))-timestamp
2793endef
2794
2795## Whether to build from source if prebuilt alternative exists
2796###########################################################
2797# $(1): module name
2798# $(2): LOCAL_PATH
2799# Expands to empty string if not from source.
2800ifeq (true,$(ANDROID_BUILD_FROM_SOURCE))
2801define if-build-from-source
2802true
2803endef
2804else
2805define if-build-from-source
2806$(if $(filter $(ANDROID_NO_PREBUILT_MODULES),$(1))$(filter \
2807    $(addsuffix %,$(ANDROID_NO_PREBUILT_PATHS)),$(2)),true)
2808endef
2809endif
2810
2811# Include makefile $(1) if build from source for module $(2)
2812# $(1): the makefile to include
2813# $(2): module name
2814# $(3): LOCAL_PATH
2815define include-if-build-from-source
2816$(if $(call if-build-from-source,$(2),$(3)),$(eval include $(1)))
2817endef
2818
2819# Return the arch for the source file of a prebuilt
2820# Return "none" if no matching arch found and return empty
2821# if the input is empty, so the result can be passed to
2822# LOCAL_MODULE_TARGET_ARCH.
2823# $(1) the list of archs supported by the prebuilt
2824define get-prebuilt-src-arch
2825$(strip $(if $(filter $(TARGET_ARCH),$(1)),$(TARGET_ARCH),\
2826  $(if $(filter $(TARGET_2ND_ARCH),$(1)),$(TARGET_2ND_ARCH),$(if $(1),none))))
2827endef
2828
2829###########################################################
2830## Other includes
2831###########################################################
2832
2833# -----------------------------------------------------------------
2834# Rules and functions to help copy important files to DIST_DIR
2835# when requested.
2836include $(BUILD_SYSTEM)/distdir.mk
2837
2838# Include any vendor specific definitions.mk file
2839-include $(TOPDIR)vendor/*/build/core/definitions.mk
2840-include $(TOPDIR)device/*/build/core/definitions.mk
2841-include $(TOPDIR)product/*/build/core/definitions.mk
2842
2843# broken:
2844#	$(foreach file,$^,$(if $(findstring,.a,$(suffix $file)),-l$(file),$(file)))
2845
2846###########################################################
2847## Misc notes
2848###########################################################
2849
2850#DEPDIR = .deps
2851#df = $(DEPDIR)/$(*F)
2852
2853#SRCS = foo.c bar.c ...
2854
2855#%.o : %.c
2856#	@$(MAKEDEPEND); \
2857#	  cp $(df).d $(df).P; \
2858#	  sed -e 's/#.*//' -e 's/^[^:]*: *//' -e 's/ *\\$$//' \
2859#	      -e '/^$$/ d' -e 's/$$/ :/' < $(df).d >> $(df).P; \
2860#	  rm -f $(df).d
2861#	$(COMPILE.c) -o $@ $<
2862
2863#-include $(SRCS:%.c=$(DEPDIR)/%.P)
2864
2865
2866#%.o : %.c
2867#	$(COMPILE.c) -MD -o $@ $<
2868#	@cp $*.d $*.P; \
2869#	  sed -e 's/#.*//' -e 's/^[^:]*: *//' -e 's/ *\\$$//' \
2870#	      -e '/^$$/ d' -e 's/$$/ :/' < $*.d >> $*.P; \
2871#	  rm -f $*.d
2872