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