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