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