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